2004-07-16 19:56:36 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
2007-02-14 12:13:49 +00:00
|
|
|
Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
2007-02-13 15:35:19 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
License, or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2004-11-14 14:58:54 +00:00
|
|
|
Lesser General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2014-11-26 14:14:51 +01:00
|
|
|
License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
|
2004-07-16 19:56:36 +00:00
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-07-02 18:47:03 +00:00
|
|
|
#include <stdlib.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
#include <unistd.h>
|
2006-02-24 15:12:42 +00:00
|
|
|
#include <errno.h>
|
2011-01-04 17:03:13 +01:00
|
|
|
|
2006-02-24 16:27:39 +00:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/core-error.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
2011-01-04 17:03:13 +01:00
|
|
|
#include <pulsecore/socket.h>
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/socket-util.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
#include "iochannel.h"
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_iochannel {
|
2004-06-08 23:54:24 +00:00
|
|
|
int ifd, ofd;
|
2006-07-14 22:42:01 +00:00
|
|
|
int ifd_type, ofd_type;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_mainloop_api* mainloop;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-02-20 22:41:02 +00:00
|
|
|
pa_iochannel_cb_t callback;
|
2004-06-08 23:54:24 +00:00
|
|
|
void*userdata;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
bool readable:1;
|
|
|
|
|
bool writable:1;
|
|
|
|
|
bool hungup:1;
|
|
|
|
|
bool no_close:1;
|
2004-06-19 19:27:47 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_io_event* input_event, *output_event;
|
2004-06-08 23:54:24 +00:00
|
|
|
};
|
|
|
|
|
|
2010-02-23 01:20:20 +01:00
|
|
|
static void callback(pa_mainloop_api* m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata);
|
|
|
|
|
|
|
|
|
|
static void delete_events(pa_iochannel *io) {
|
|
|
|
|
pa_assert(io);
|
|
|
|
|
|
|
|
|
|
if (io->input_event)
|
|
|
|
|
io->mainloop->io_free(io->input_event);
|
|
|
|
|
|
|
|
|
|
if (io->output_event && io->output_event != io->input_event)
|
|
|
|
|
io->mainloop->io_free(io->output_event);
|
|
|
|
|
|
|
|
|
|
io->input_event = io->output_event = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void enable_events(pa_iochannel *io) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2010-02-23 01:20:20 +01:00
|
|
|
if (io->hungup) {
|
|
|
|
|
delete_events(io);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (io->ifd == io->ofd && io->ifd >= 0) {
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_io_event_flags_t f = PA_IO_EVENT_NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-20 15:00:16 +00:00
|
|
|
if (!io->readable)
|
2004-08-05 19:53:57 +00:00
|
|
|
f |= PA_IO_EVENT_INPUT;
|
2006-05-20 15:00:16 +00:00
|
|
|
if (!io->writable)
|
2004-08-05 19:53:57 +00:00
|
|
|
f |= PA_IO_EVENT_OUTPUT;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2010-02-23 01:20:20 +01:00
|
|
|
pa_assert(io->input_event == io->output_event);
|
|
|
|
|
|
|
|
|
|
if (f != PA_IO_EVENT_NULL) {
|
|
|
|
|
if (io->input_event)
|
|
|
|
|
io->mainloop->io_enable(io->input_event, f);
|
|
|
|
|
else
|
|
|
|
|
io->input_event = io->output_event = io->mainloop->io_new(io->mainloop, io->ifd, f, callback, io);
|
|
|
|
|
} else
|
|
|
|
|
delete_events(io);
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
} else {
|
2010-02-23 01:20:20 +01:00
|
|
|
|
|
|
|
|
if (io->ifd >= 0) {
|
|
|
|
|
if (!io->readable) {
|
|
|
|
|
if (io->input_event)
|
|
|
|
|
io->mainloop->io_enable(io->input_event, PA_IO_EVENT_INPUT);
|
|
|
|
|
else
|
|
|
|
|
io->input_event = io->mainloop->io_new(io->mainloop, io->ifd, PA_IO_EVENT_INPUT, callback, io);
|
|
|
|
|
} else if (io->input_event) {
|
|
|
|
|
io->mainloop->io_free(io->input_event);
|
|
|
|
|
io->input_event = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (io->ofd >= 0) {
|
|
|
|
|
if (!io->writable) {
|
|
|
|
|
if (io->output_event)
|
|
|
|
|
io->mainloop->io_enable(io->output_event, PA_IO_EVENT_OUTPUT);
|
|
|
|
|
else
|
|
|
|
|
io->output_event = io->mainloop->io_new(io->mainloop, io->ofd, PA_IO_EVENT_OUTPUT, callback, io);
|
2013-12-18 17:27:47 +01:00
|
|
|
} else if (io->output_event) {
|
2010-02-23 01:20:20 +01:00
|
|
|
io->mainloop->io_free(io->output_event);
|
|
|
|
|
io->output_event = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
static void callback(pa_mainloop_api* m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_iochannel *io = userdata;
|
2013-06-27 19:28:09 +02:00
|
|
|
bool changed = false;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
|
|
|
|
pa_assert(e);
|
|
|
|
|
pa_assert(fd >= 0);
|
|
|
|
|
pa_assert(userdata);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-05-11 21:38:16 +00:00
|
|
|
if ((f & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) && !io->hungup) {
|
2013-06-27 19:28:09 +02:00
|
|
|
io->hungup = true;
|
|
|
|
|
changed = true;
|
2006-02-20 04:05:16 +00:00
|
|
|
}
|
2004-09-27 17:21:27 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if ((f & PA_IO_EVENT_INPUT) && !io->readable) {
|
2013-06-27 19:28:09 +02:00
|
|
|
io->readable = true;
|
|
|
|
|
changed = true;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(e == io->input_event);
|
2006-02-20 04:05:16 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if ((f & PA_IO_EVENT_OUTPUT) && !io->writable) {
|
2013-06-27 19:28:09 +02:00
|
|
|
io->writable = true;
|
|
|
|
|
changed = true;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(e == io->output_event);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (changed) {
|
2010-02-23 01:20:20 +01:00
|
|
|
enable_events(io);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
if (io->callback)
|
|
|
|
|
io->callback(io, io->userdata);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_iochannel* pa_iochannel_new(pa_mainloop_api*m, int ifd, int ofd) {
|
|
|
|
|
pa_iochannel *io;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
|
|
|
|
pa_assert(ifd >= 0 || ofd >= 0);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2010-02-23 01:20:20 +01:00
|
|
|
io = pa_xnew0(pa_iochannel, 1);
|
2004-06-08 23:54:24 +00:00
|
|
|
io->ifd = ifd;
|
|
|
|
|
io->ofd = ofd;
|
|
|
|
|
io->mainloop = m;
|
|
|
|
|
|
2010-02-23 01:20:20 +01:00
|
|
|
if (io->ifd >= 0)
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_make_fd_nonblock(io->ifd);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2010-02-23 01:20:20 +01:00
|
|
|
if (io->ofd >= 0 && io->ofd != io->ifd)
|
|
|
|
|
pa_make_fd_nonblock(io->ofd);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2010-02-23 01:20:20 +01:00
|
|
|
enable_events(io);
|
2004-06-08 23:54:24 +00:00
|
|
|
return io;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_iochannel_free(pa_iochannel*io) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2010-02-23 01:20:20 +01:00
|
|
|
delete_events(io);
|
2004-08-05 19:53:57 +00:00
|
|
|
|
2004-06-19 19:27:47 +00:00
|
|
|
if (!io->no_close) {
|
|
|
|
|
if (io->ifd >= 0)
|
2007-02-14 12:13:49 +00:00
|
|
|
pa_close(io->ifd);
|
2004-06-19 19:27:47 +00:00
|
|
|
if (io->ofd >= 0 && io->ofd != io->ifd)
|
2007-02-14 12:13:49 +00:00
|
|
|
pa_close(io->ofd);
|
2004-06-19 19:27:47 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(io);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
bool pa_iochannel_is_readable(pa_iochannel*io) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-27 17:21:27 +00:00
|
|
|
return io->readable || io->hungup;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
bool pa_iochannel_is_writable(pa_iochannel*io) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-27 17:21:27 +00:00
|
|
|
return io->writable && !io->hungup;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
bool pa_iochannel_is_hungup(pa_iochannel*io) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-07-09 23:26:10 +00:00
|
|
|
return io->hungup;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
ssize_t pa_iochannel_write(pa_iochannel*io, const void*data, size_t l) {
|
2004-06-08 23:54:24 +00:00
|
|
|
ssize_t r;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
pa_assert(l);
|
|
|
|
|
pa_assert(io->ofd >= 0);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2013-06-13 14:26:09 +02:00
|
|
|
r = pa_write(io->ofd, data, l, &io->ofd_type);
|
|
|
|
|
|
|
|
|
|
if ((size_t) r == l)
|
|
|
|
|
return r; /* Fast path - we almost always successfully write everything */
|
|
|
|
|
|
|
|
|
|
if (r < 0) {
|
|
|
|
|
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
|
r = 0;
|
|
|
|
|
else
|
|
|
|
|
return r;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-13 14:26:09 +02:00
|
|
|
/* Partial write - let's get a notification when we can write more */
|
2013-06-27 19:28:09 +02:00
|
|
|
io->writable = io->hungup = false;
|
2013-06-13 14:26:09 +02:00
|
|
|
enable_events(io);
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
ssize_t pa_iochannel_read(pa_iochannel*io, void*data, size_t l) {
|
2004-06-08 23:54:24 +00:00
|
|
|
ssize_t r;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
pa_assert(io->ifd >= 0);
|
2006-01-10 17:51:06 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if ((r = pa_read(io->ifd, data, l, &io->ifd_type)) >= 0) {
|
2010-02-23 01:20:20 +01:00
|
|
|
|
|
|
|
|
/* We also reset the hangup flag here to ensure that another
|
|
|
|
|
* IO callback is triggered so that we will again call into
|
|
|
|
|
* user code */
|
2013-06-27 19:28:09 +02:00
|
|
|
io->readable = io->hungup = false;
|
2010-02-23 01:20:20 +01:00
|
|
|
enable_events(io);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2004-06-11 21:30:16 +00:00
|
|
|
|
2006-07-19 21:48:35 +00:00
|
|
|
#ifdef HAVE_CREDS
|
2006-02-24 15:12:42 +00:00
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
bool pa_iochannel_creds_supported(pa_iochannel *io) {
|
2010-02-23 01:23:41 +01:00
|
|
|
struct {
|
|
|
|
|
struct sockaddr sa;
|
2011-12-08 14:29:06 +05:30
|
|
|
#ifdef HAVE_SYS_UN_H
|
2010-02-23 01:23:41 +01:00
|
|
|
struct sockaddr_un un;
|
2011-12-08 14:29:06 +05:30
|
|
|
#endif
|
2010-02-23 01:23:41 +01:00
|
|
|
struct sockaddr_storage storage;
|
|
|
|
|
} sa;
|
|
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
socklen_t l;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
|
|
|
|
pa_assert(io->ifd >= 0);
|
|
|
|
|
pa_assert(io->ofd == io->ifd);
|
2006-02-24 15:12:42 +00:00
|
|
|
|
|
|
|
|
l = sizeof(sa);
|
2010-02-23 01:23:41 +01:00
|
|
|
if (getsockname(io->ifd, &sa.sa, &l) < 0)
|
2013-06-27 19:28:09 +02:00
|
|
|
return false;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2010-02-23 01:23:41 +01:00
|
|
|
return sa.sa.sa_family == AF_UNIX;
|
2006-02-24 15:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_iochannel_creds_enable(pa_iochannel *io) {
|
|
|
|
|
int t = 1;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
|
|
|
|
pa_assert(io->ifd >= 0);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
if (setsockopt(io->ifd, SOL_SOCKET, SO_PASSCRED, &t, sizeof(t)) < 0) {
|
2006-05-22 15:20:46 +00:00
|
|
|
pa_log_error("setsockopt(SOL_SOCKET, SO_PASSCRED): %s", pa_cstrerror(errno));
|
2006-02-24 15:12:42 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-19 21:48:35 +00:00
|
|
|
ssize_t pa_iochannel_write_with_creds(pa_iochannel*io, const void*data, size_t l, const pa_creds *ucred) {
|
2006-02-24 15:12:42 +00:00
|
|
|
ssize_t r;
|
|
|
|
|
struct msghdr mh;
|
|
|
|
|
struct iovec iov;
|
2007-11-04 16:51:26 +00:00
|
|
|
union {
|
|
|
|
|
struct cmsghdr hdr;
|
|
|
|
|
uint8_t data[CMSG_SPACE(sizeof(struct ucred))];
|
|
|
|
|
} cmsg;
|
2006-07-19 17:44:19 +00:00
|
|
|
struct ucred *u;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
pa_assert(l);
|
|
|
|
|
pa_assert(io->ofd >= 0);
|
2006-02-24 15:12:42 +00:00
|
|
|
|
2010-02-23 01:23:41 +01:00
|
|
|
pa_zero(iov);
|
2006-02-24 15:12:42 +00:00
|
|
|
iov.iov_base = (void*) data;
|
|
|
|
|
iov.iov_len = l;
|
|
|
|
|
|
2010-02-23 01:23:41 +01:00
|
|
|
pa_zero(cmsg);
|
2007-11-04 16:51:26 +00:00
|
|
|
cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(struct ucred));
|
|
|
|
|
cmsg.hdr.cmsg_level = SOL_SOCKET;
|
|
|
|
|
cmsg.hdr.cmsg_type = SCM_CREDENTIALS;
|
2006-02-24 15:12:42 +00:00
|
|
|
|
2007-11-04 16:51:26 +00:00
|
|
|
u = (struct ucred*) CMSG_DATA(&cmsg.hdr);
|
2006-07-19 17:44:19 +00:00
|
|
|
|
2006-07-19 21:48:35 +00:00
|
|
|
u->pid = getpid();
|
|
|
|
|
if (ucred) {
|
|
|
|
|
u->uid = ucred->uid;
|
|
|
|
|
u->gid = ucred->gid;
|
|
|
|
|
} else {
|
2006-07-19 17:44:19 +00:00
|
|
|
u->uid = getuid();
|
|
|
|
|
u->gid = getgid();
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2010-02-23 01:20:20 +01:00
|
|
|
pa_zero(mh);
|
2006-02-24 15:12:42 +00:00
|
|
|
mh.msg_iov = &iov;
|
|
|
|
|
mh.msg_iovlen = 1;
|
2007-11-04 16:51:26 +00:00
|
|
|
mh.msg_control = &cmsg;
|
|
|
|
|
mh.msg_controllen = sizeof(cmsg);
|
2006-02-24 15:12:42 +00:00
|
|
|
|
|
|
|
|
if ((r = sendmsg(io->ofd, &mh, MSG_NOSIGNAL)) >= 0) {
|
2013-06-27 19:28:09 +02:00
|
|
|
io->writable = io->hungup = false;
|
2010-02-23 01:20:20 +01:00
|
|
|
enable_events(io);
|
2006-02-24 15:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 16:12:25 +02:00
|
|
|
ssize_t pa_iochannel_write_with_fds(pa_iochannel*io, const void*data, size_t l, int nfd, const int *fds) {
|
|
|
|
|
ssize_t r;
|
|
|
|
|
int *msgdata;
|
|
|
|
|
struct msghdr mh;
|
|
|
|
|
struct iovec iov;
|
|
|
|
|
union {
|
|
|
|
|
struct cmsghdr hdr;
|
2014-08-06 07:48:19 +05:30
|
|
|
uint8_t data[CMSG_SPACE(sizeof(int) * MAX_ANCIL_DATA_FDS)];
|
2014-04-15 16:12:25 +02:00
|
|
|
} cmsg;
|
|
|
|
|
|
|
|
|
|
pa_assert(io);
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
pa_assert(l);
|
|
|
|
|
pa_assert(io->ofd >= 0);
|
|
|
|
|
pa_assert(fds);
|
|
|
|
|
pa_assert(nfd > 0);
|
2014-08-06 07:48:19 +05:30
|
|
|
pa_assert(nfd <= MAX_ANCIL_DATA_FDS);
|
2014-04-15 16:12:25 +02:00
|
|
|
|
|
|
|
|
pa_zero(iov);
|
|
|
|
|
iov.iov_base = (void*) data;
|
|
|
|
|
iov.iov_len = l;
|
|
|
|
|
|
|
|
|
|
pa_zero(cmsg);
|
|
|
|
|
cmsg.hdr.cmsg_level = SOL_SOCKET;
|
|
|
|
|
cmsg.hdr.cmsg_type = SCM_RIGHTS;
|
|
|
|
|
|
|
|
|
|
msgdata = (int*) CMSG_DATA(&cmsg.hdr);
|
|
|
|
|
memcpy(msgdata, fds, nfd * sizeof(int));
|
|
|
|
|
cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(int) * nfd);
|
|
|
|
|
|
|
|
|
|
pa_zero(mh);
|
|
|
|
|
mh.msg_iov = &iov;
|
|
|
|
|
mh.msg_iovlen = 1;
|
|
|
|
|
mh.msg_control = &cmsg;
|
|
|
|
|
mh.msg_controllen = sizeof(cmsg);
|
|
|
|
|
|
|
|
|
|
if ((r = sendmsg(io->ofd, &mh, MSG_NOSIGNAL)) >= 0) {
|
|
|
|
|
io->writable = io->hungup = false;
|
|
|
|
|
enable_events(io);
|
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-06 07:48:19 +05:30
|
|
|
ssize_t pa_iochannel_read_with_ancil_data(pa_iochannel*io, void*data, size_t l, pa_cmsg_ancil_data *ancil_data) {
|
2006-02-24 15:12:42 +00:00
|
|
|
ssize_t r;
|
|
|
|
|
struct msghdr mh;
|
|
|
|
|
struct iovec iov;
|
2007-11-04 16:51:26 +00:00
|
|
|
union {
|
|
|
|
|
struct cmsghdr hdr;
|
2014-08-06 07:48:19 +05:30
|
|
|
uint8_t data[CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int) * MAX_ANCIL_DATA_FDS)];
|
2007-11-04 16:51:26 +00:00
|
|
|
} cmsg;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
pa_assert(l);
|
|
|
|
|
pa_assert(io->ifd >= 0);
|
2014-08-06 07:48:19 +05:30
|
|
|
pa_assert(ancil_data);
|
2014-04-15 15:37:44 +02:00
|
|
|
|
|
|
|
|
if (io->ifd_type > 0) {
|
2014-08-06 07:48:19 +05:30
|
|
|
ancil_data->creds_valid = false;
|
|
|
|
|
ancil_data->nfd = 0;
|
2014-04-15 15:37:44 +02:00
|
|
|
return pa_iochannel_read(io, data, l);
|
|
|
|
|
}
|
2006-02-24 15:12:42 +00:00
|
|
|
|
|
|
|
|
iov.iov_base = data;
|
|
|
|
|
iov.iov_len = l;
|
|
|
|
|
|
2010-02-23 01:23:41 +01:00
|
|
|
pa_zero(mh);
|
2006-02-24 15:12:42 +00:00
|
|
|
mh.msg_iov = &iov;
|
|
|
|
|
mh.msg_iovlen = 1;
|
2007-11-04 16:51:26 +00:00
|
|
|
mh.msg_control = &cmsg;
|
|
|
|
|
mh.msg_controllen = sizeof(cmsg);
|
2006-02-24 15:12:42 +00:00
|
|
|
|
2006-07-14 23:00:20 +00:00
|
|
|
if ((r = recvmsg(io->ifd, &mh, 0)) >= 0) {
|
2007-11-04 16:51:26 +00:00
|
|
|
struct cmsghdr *cmh;
|
2006-02-24 15:12:42 +00:00
|
|
|
|
2014-08-06 07:48:19 +05:30
|
|
|
ancil_data->creds_valid = false;
|
|
|
|
|
ancil_data->nfd = 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-04 16:51:26 +00:00
|
|
|
for (cmh = CMSG_FIRSTHDR(&mh); cmh; cmh = CMSG_NXTHDR(&mh, cmh)) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2014-04-15 15:37:44 +02:00
|
|
|
if (cmh->cmsg_level != SOL_SOCKET)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (cmh->cmsg_type == SCM_CREDENTIALS) {
|
2006-07-19 21:48:35 +00:00
|
|
|
struct ucred u;
|
2007-11-04 16:51:26 +00:00
|
|
|
pa_assert(cmh->cmsg_len == CMSG_LEN(sizeof(struct ucred)));
|
|
|
|
|
memcpy(&u, CMSG_DATA(cmh), sizeof(struct ucred));
|
2006-07-19 21:48:35 +00:00
|
|
|
|
2014-08-06 07:48:19 +05:30
|
|
|
ancil_data->creds.gid = u.gid;
|
|
|
|
|
ancil_data->creds.uid = u.uid;
|
|
|
|
|
ancil_data->creds_valid = true;
|
2014-04-15 15:37:44 +02:00
|
|
|
}
|
|
|
|
|
else if (cmh->cmsg_type == SCM_RIGHTS) {
|
|
|
|
|
int nfd = (cmh->cmsg_len - CMSG_LEN(0)) / sizeof(int);
|
2014-08-06 07:48:19 +05:30
|
|
|
if (nfd > MAX_ANCIL_DATA_FDS) {
|
2014-04-15 15:37:44 +02:00
|
|
|
int i;
|
|
|
|
|
pa_log("Trying to receive too many file descriptors!");
|
|
|
|
|
for (i = 0; i < nfd; i++)
|
|
|
|
|
pa_close(((int*) CMSG_DATA(cmh))[i]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-08-06 07:48:19 +05:30
|
|
|
memcpy(ancil_data->fds, CMSG_DATA(cmh), nfd * sizeof(int));
|
|
|
|
|
ancil_data->nfd = nfd;
|
2016-03-13 01:12:18 +02:00
|
|
|
ancil_data->close_fds_on_cleanup = true;
|
2006-02-24 15:12:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
io->readable = io->hungup = false;
|
2010-02-23 01:20:20 +01:00
|
|
|
enable_events(io);
|
2006-02-24 15:12:42 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2014-04-15 15:37:44 +02:00
|
|
|
if (r == -1 && errno == ENOTSOCK) {
|
|
|
|
|
io->ifd_type = 1;
|
2014-08-06 07:48:19 +05:30
|
|
|
return pa_iochannel_read_with_ancil_data(io, data, l, ancil_data);
|
2014-04-15 15:37:44 +02:00
|
|
|
}
|
|
|
|
|
|
2006-02-24 15:12:42 +00:00
|
|
|
return r;
|
|
|
|
|
}
|
2006-02-24 16:27:39 +00:00
|
|
|
|
2006-07-19 21:48:35 +00:00
|
|
|
#endif /* HAVE_CREDS */
|
2006-02-24 15:12:42 +00:00
|
|
|
|
2006-02-20 22:41:02 +00:00
|
|
|
void pa_iochannel_set_callback(pa_iochannel*io, pa_iochannel_cb_t _callback, void *userdata) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
io->callback = _callback;
|
2004-06-11 21:30:16 +00:00
|
|
|
io->userdata = userdata;
|
|
|
|
|
}
|
2004-06-19 19:27:47 +00:00
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
void pa_iochannel_set_noclose(pa_iochannel*io, bool b) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2014-10-23 15:00:29 +02:00
|
|
|
io->no_close = b;
|
2004-06-19 19:27:47 +00:00
|
|
|
}
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_iochannel_socket_peer_to_string(pa_iochannel*io, char*s, size_t l) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
|
|
|
|
pa_assert(s);
|
|
|
|
|
pa_assert(l);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-07-11 01:09:46 +00:00
|
|
|
pa_socket_peer_to_string(io->ifd, s, l);
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
2004-07-09 23:26:10 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_iochannel_socket_set_rcvbuf(pa_iochannel *io, size_t l) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-07-09 23:26:10 +00:00
|
|
|
return pa_socket_set_rcvbuf(io->ifd, l);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_iochannel_socket_set_sndbuf(pa_iochannel *io, size_t l) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-07-09 23:26:10 +00:00
|
|
|
return pa_socket_set_sndbuf(io->ofd, l);
|
|
|
|
|
}
|
2004-09-16 00:05:56 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_mainloop_api* pa_iochannel_get_mainloop_api(pa_iochannel *io) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-18 20:50:44 +00:00
|
|
|
return io->mainloop;
|
|
|
|
|
}
|
2006-07-20 18:43:20 +00:00
|
|
|
|
|
|
|
|
int pa_iochannel_get_recv_fd(pa_iochannel *io) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(io);
|
2006-07-20 18:43:20 +00:00
|
|
|
|
|
|
|
|
return io->ifd;
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
int pa_iochannel_get_send_fd(pa_iochannel *io) {
|
|
|
|
|
pa_assert(io);
|
|
|
|
|
|
|
|
|
|
return io->ofd;
|
|
|
|
|
}
|
2008-06-16 18:55:20 +00:00
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
bool pa_iochannel_socket_is_local(pa_iochannel *io) {
|
2008-06-16 18:55:20 +00:00
|
|
|
pa_assert(io);
|
|
|
|
|
|
|
|
|
|
if (pa_socket_is_local(io->ifd))
|
2013-06-27 19:28:09 +02:00
|
|
|
return true;
|
2008-06-16 18:55:20 +00:00
|
|
|
|
|
|
|
|
if (io->ifd != io->ofd)
|
|
|
|
|
if (pa_socket_is_local(io->ofd))
|
2013-06-27 19:28:09 +02:00
|
|
|
return true;
|
2008-06-16 18:55:20 +00:00
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
return false;
|
2008-06-16 18:55:20 +00:00
|
|
|
}
|