4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
41 #include <pulse/xmalloc.h>
43 #include <pulsecore/core-error.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/socket-util.h>
46 #include <pulsecore/log.h>
48 #include "iochannel.h"
52 int ifd_type, ofd_type;
53 pa_mainloop_api* mainloop;
55 pa_iochannel_cb_t callback;
64 pa_io_event* input_event, *output_event;
67 static void enable_mainloop_sources(pa_iochannel *io) {
70 if (io->input_event == io->output_event && io->input_event) {
71 pa_io_event_flags_t f = PA_IO_EVENT_NULL;
72 assert(io->input_event);
75 f |= PA_IO_EVENT_INPUT;
77 f |= PA_IO_EVENT_OUTPUT;
79 io->mainloop->io_enable(io->input_event, f);
82 io->mainloop->io_enable(io->input_event, io->readable ? PA_IO_EVENT_NULL : PA_IO_EVENT_INPUT);
84 io->mainloop->io_enable(io->output_event, io->writable ? PA_IO_EVENT_NULL : PA_IO_EVENT_OUTPUT);
88 static void callback(pa_mainloop_api* m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
89 pa_iochannel *io = userdata;
97 if ((f & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) && !io->hungup) {
102 if ((f & PA_IO_EVENT_INPUT) && !io->readable) {
105 assert(e == io->input_event);
108 if ((f & PA_IO_EVENT_OUTPUT) && !io->writable) {
111 assert(e == io->output_event);
115 enable_mainloop_sources(io);
118 io->callback(io, io->userdata);
122 pa_iochannel* pa_iochannel_new(pa_mainloop_api*m, int ifd, int ofd) {
126 assert(ifd >= 0 || ofd >= 0);
128 io = pa_xnew(pa_iochannel, 1);
131 io->ifd_type = io->ofd_type = 0;
141 io->input_event = io->output_event = NULL;
145 pa_make_nonblock_fd(io->ifd);
146 io->input_event = io->output_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT|PA_IO_EVENT_OUTPUT, callback, io);
150 pa_make_nonblock_fd(io->ifd);
151 io->input_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT, callback, io);
155 pa_make_nonblock_fd(io->ofd);
156 io->output_event = m->io_new(m, ofd, PA_IO_EVENT_OUTPUT, callback, io);
163 void pa_iochannel_free(pa_iochannel*io) {
167 io->mainloop->io_free(io->input_event);
169 if (io->output_event && (io->output_event != io->input_event))
170 io->mainloop->io_free(io->output_event);
176 if (io->ofd >= 0 && io->ofd != io->ifd)
183 int pa_iochannel_is_readable(pa_iochannel*io) {
186 return io->readable || io->hungup;
189 int pa_iochannel_is_writable(pa_iochannel*io) {
192 return io->writable && !io->hungup;
195 int pa_iochannel_is_hungup(pa_iochannel*io) {
201 ssize_t pa_iochannel_write(pa_iochannel*io, const void*data, size_t l) {
207 assert(io->ofd >= 0);
209 r = pa_write(io->ofd, data, l, &io->ofd_type);
212 enable_mainloop_sources(io);
218 ssize_t pa_iochannel_read(pa_iochannel*io, void*data, size_t l) {
223 assert(io->ifd >= 0);
225 r = pa_read(io->ifd, data, l, &io->ifd_type);
228 enable_mainloop_sources(io);
234 #ifdef SCM_CREDENTIALS
236 int pa_iochannel_creds_supported(pa_iochannel *io) {
237 struct sockaddr_un sa;
241 assert(io->ifd >= 0);
242 assert(io->ofd == io->ifd);
246 if (getsockname(io->ifd, (struct sockaddr*) &sa, &l) < 0)
249 return sa.sun_family == AF_UNIX;
252 int pa_iochannel_creds_enable(pa_iochannel *io) {
256 assert(io->ifd >= 0);
258 if (setsockopt(io->ifd, SOL_SOCKET, SO_PASSCRED, &t, sizeof(t)) < 0) {
259 pa_log_error("setsockopt(SOL_SOCKET, SO_PASSCRED): %s", pa_cstrerror(errno));
266 ssize_t pa_iochannel_write_with_creds(pa_iochannel*io, const void*data, size_t l) {
270 uint8_t cmsg_data[CMSG_SPACE(sizeof(struct ucred))];
272 struct cmsghdr *cmsg;
277 assert(io->ofd >= 0);
279 memset(&iov, 0, sizeof(iov));
280 iov.iov_base = (void*) data;
283 memset(cmsg_data, 0, sizeof(cmsg_data));
284 cmsg = (struct cmsghdr*) cmsg_data;
285 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
286 cmsg->cmsg_level = SOL_SOCKET;
287 cmsg->cmsg_type = SCM_CREDENTIALS;
289 ucred = (struct ucred*) CMSG_DATA(cmsg);
290 ucred->pid = getpid();
291 ucred->uid = getuid();
292 ucred->gid = getgid();
294 memset(&mh, 0, sizeof(mh));
299 mh.msg_control = cmsg_data;
300 mh.msg_controllen = sizeof(cmsg_data);
303 if ((r = sendmsg(io->ofd, &mh, MSG_NOSIGNAL)) >= 0) {
305 enable_mainloop_sources(io);
311 ssize_t pa_iochannel_read_with_creds(pa_iochannel*io, void*data, size_t l, struct ucred *ucred, int *creds_valid) {
315 uint8_t cmsg_data[CMSG_SPACE(sizeof(struct ucred))];
320 assert(io->ifd >= 0);
324 memset(&iov, 0, sizeof(iov));
328 memset(cmsg_data, 0, sizeof(cmsg_data));
330 memset(&mh, 0, sizeof(mh));
335 mh.msg_control = cmsg_data;
336 mh.msg_controllen = sizeof(cmsg_data);
339 if ((r = recvmsg(io->ifd, &mh, 0)) >= 0) {
340 struct cmsghdr *cmsg;
344 for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
346 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_CREDENTIALS) {
347 assert(cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)));
348 memcpy(ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
355 enable_mainloop_sources(io);
360 #else /* SCM_CREDENTIALS */
362 int pa_iochannel_creds_supported(pa_iochannel *io) {
366 int pa_iochannel_creds_enable(pa_iochannel *io) {
370 ssize_t pa_iochannel_write_with_creds(pa_iochannel*io, const void*data, size_t l) {
371 pa_log_error("pa_iochannel_write_with_creds() not supported.");
375 ssize_t pa_iochannel_read_with_creds(pa_iochannel*io, void*data, size_t l, struct ucred *ucred, int *creds_valid) {
376 pa_log_error("pa_iochannel_read_with_creds() not supported.");
380 #endif /* SCM_CREDENTIALS */
382 void pa_iochannel_set_callback(pa_iochannel*io, pa_iochannel_cb_t _callback, void *userdata) {
385 io->callback = _callback;
386 io->userdata = userdata;
389 void pa_iochannel_set_noclose(pa_iochannel*io, int b) {
395 void pa_iochannel_socket_peer_to_string(pa_iochannel*io, char*s, size_t l) {
400 pa_socket_peer_to_string(io->ifd, s, l);
403 int pa_iochannel_socket_set_rcvbuf(pa_iochannel *io, size_t l) {
406 return pa_socket_set_rcvbuf(io->ifd, l);
409 int pa_iochannel_socket_set_sndbuf(pa_iochannel *io, size_t l) {
412 return pa_socket_set_sndbuf(io->ofd, l);
415 pa_mainloop_api* pa_iochannel_get_mainloop_api(pa_iochannel *io) {