4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; 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 <polyp/xmalloc.h>
43 #include <polypcore/core-error.h>
44 #include <polypcore/core-util.h>
45 #include <polypcore/socket-util.h>
46 #include <polypcore/log.h>
48 #include "iochannel.h"
52 pa_mainloop_api
* mainloop
;
54 pa_iochannel_cb_t callback
;
63 pa_io_event
* input_event
, *output_event
;
66 static void enable_mainloop_sources(pa_iochannel
*io
) {
69 if (io
->input_event
== io
->output_event
&& io
->input_event
) {
70 pa_io_event_flags_t f
= PA_IO_EVENT_NULL
;
71 assert(io
->input_event
);
74 f
|= PA_IO_EVENT_INPUT
;
76 f
|= PA_IO_EVENT_OUTPUT
;
78 io
->mainloop
->io_enable(io
->input_event
, f
);
81 io
->mainloop
->io_enable(io
->input_event
, io
->readable
? PA_IO_EVENT_NULL
: PA_IO_EVENT_INPUT
);
83 io
->mainloop
->io_enable(io
->output_event
, io
->writable
? PA_IO_EVENT_NULL
: PA_IO_EVENT_OUTPUT
);
87 static void callback(pa_mainloop_api
* m
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
) {
88 pa_iochannel
*io
= userdata
;
96 if ((f
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
)) && !io
->hungup
) {
101 if ((f
& PA_IO_EVENT_INPUT
) && !io
->readable
) {
104 assert(e
== io
->input_event
);
107 if ((f
& PA_IO_EVENT_OUTPUT
) && !io
->writable
) {
110 assert(e
== io
->output_event
);
114 enable_mainloop_sources(io
);
117 io
->callback(io
, io
->userdata
);
121 pa_iochannel
* pa_iochannel_new(pa_mainloop_api
*m
, int ifd
, int ofd
) {
125 assert(ifd
>= 0 || ofd
>= 0);
127 io
= pa_xnew(pa_iochannel
, 1);
139 io
->input_event
= io
->output_event
= NULL
;
143 pa_make_nonblock_fd(io
->ifd
);
144 io
->input_event
= io
->output_event
= m
->io_new(m
, ifd
, PA_IO_EVENT_INPUT
|PA_IO_EVENT_OUTPUT
, callback
, io
);
148 pa_make_nonblock_fd(io
->ifd
);
149 io
->input_event
= m
->io_new(m
, ifd
, PA_IO_EVENT_INPUT
, callback
, io
);
153 pa_make_nonblock_fd(io
->ofd
);
154 io
->output_event
= m
->io_new(m
, ofd
, PA_IO_EVENT_OUTPUT
, callback
, io
);
161 void pa_iochannel_free(pa_iochannel
*io
) {
165 io
->mainloop
->io_free(io
->input_event
);
167 if (io
->output_event
&& (io
->output_event
!= io
->input_event
))
168 io
->mainloop
->io_free(io
->output_event
);
174 if (io
->ofd
>= 0 && io
->ofd
!= io
->ifd
)
181 int pa_iochannel_is_readable(pa_iochannel
*io
) {
184 return io
->readable
|| io
->hungup
;
187 int pa_iochannel_is_writable(pa_iochannel
*io
) {
190 return io
->writable
&& !io
->hungup
;
193 int pa_iochannel_is_hungup(pa_iochannel
*io
) {
199 ssize_t
pa_iochannel_write(pa_iochannel
*io
, const void*data
, size_t l
) {
205 assert(io
->ofd
>= 0);
207 r
= pa_write(io
->ofd
, data
, l
);
210 enable_mainloop_sources(io
);
216 ssize_t
pa_iochannel_read(pa_iochannel
*io
, void*data
, size_t l
) {
221 assert(io
->ifd
>= 0);
223 r
= pa_read(io
->ifd
, data
, l
);
226 enable_mainloop_sources(io
);
232 #ifdef SCM_CREDENTIALS
234 int pa_iochannel_creds_supported(pa_iochannel
*io
) {
235 struct sockaddr_un sa
;
239 assert(io
->ifd
>= 0);
240 assert(io
->ofd
== io
->ifd
);
244 if (getsockname(io
->ifd
, (struct sockaddr
*) &sa
, &l
) < 0)
247 return sa
.sun_family
== AF_UNIX
;
250 int pa_iochannel_creds_enable(pa_iochannel
*io
) {
254 assert(io
->ifd
>= 0);
256 if (setsockopt(io
->ifd
, SOL_SOCKET
, SO_PASSCRED
, &t
, sizeof(t
)) < 0) {
257 pa_log_error("setsockopt(SOL_SOCKET, SO_PASSCRED): %s", pa_cstrerror(errno
));
264 ssize_t
pa_iochannel_write_with_creds(pa_iochannel
*io
, const void*data
, size_t l
) {
268 uint8_t cmsg_data
[CMSG_SPACE(sizeof(struct ucred
))];
270 struct cmsghdr
*cmsg
;
275 assert(io
->ofd
>= 0);
277 memset(&iov
, 0, sizeof(iov
));
278 iov
.iov_base
= (void*) data
;
281 memset(cmsg_data
, 0, sizeof(cmsg_data
));
282 cmsg
= (struct cmsghdr
*) cmsg_data
;
283 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
284 cmsg
->cmsg_level
= SOL_SOCKET
;
285 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
287 ucred
= (struct ucred
*) CMSG_DATA(cmsg
);
288 ucred
->pid
= getpid();
289 ucred
->uid
= getuid();
290 ucred
->gid
= getgid();
292 memset(&mh
, 0, sizeof(mh
));
297 mh
.msg_control
= cmsg_data
;
298 mh
.msg_controllen
= sizeof(cmsg_data
);
301 if ((r
= sendmsg(io
->ofd
, &mh
, MSG_NOSIGNAL
)) >= 0) {
303 enable_mainloop_sources(io
);
309 ssize_t
pa_iochannel_read_with_creds(pa_iochannel
*io
, void*data
, size_t l
, struct ucred
*ucred
, int *creds_valid
) {
313 uint8_t cmsg_data
[CMSG_SPACE(sizeof(struct ucred
))];
318 assert(io
->ifd
>= 0);
322 memset(&iov
, 0, sizeof(iov
));
326 memset(cmsg_data
, 0, sizeof(cmsg_data
));
328 memset(&mh
, 0, sizeof(mh
));
333 mh
.msg_control
= cmsg_data
;
334 mh
.msg_controllen
= sizeof(cmsg_data
);
337 if ((r
= recvmsg(io
->ifd
, &mh
, MSG_NOSIGNAL
)) >= 0) {
338 struct cmsghdr
*cmsg
;
342 for (cmsg
= CMSG_FIRSTHDR(&mh
); cmsg
; cmsg
= CMSG_NXTHDR(&mh
, cmsg
)) {
344 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
345 assert(cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)));
346 memcpy(ucred
, CMSG_DATA(cmsg
), sizeof(struct ucred
));
353 enable_mainloop_sources(io
);
358 #else /* SCM_CREDENTIALS */
360 int pa_iochannel_creds_supported(pa_iochannel
*io
) {
364 int pa_iochannel_creds_enable(pa_iochannel
*io
) {
368 ssize_t
pa_iochannel_write_with_creds(pa_iochannel
*io
, const void*data
, size_t l
) {
369 pa_log_error("pa_iochannel_write_with_creds() not supported.");
373 ssize_t
pa_iochannel_read_with_creds(pa_iochannel
*io
, void*data
, size_t l
, struct ucred
*ucred
, int *creds_valid
) {
374 pa_log_error("pa_iochannel_read_with_creds() not supported.");
378 #endif /* SCM_CREDENTIALS */
380 void pa_iochannel_set_callback(pa_iochannel
*io
, pa_iochannel_cb_t _callback
, void *userdata
) {
383 io
->callback
= _callback
;
384 io
->userdata
= userdata
;
387 void pa_iochannel_set_noclose(pa_iochannel
*io
, int b
) {
393 void pa_iochannel_socket_peer_to_string(pa_iochannel
*io
, char*s
, size_t l
) {
398 pa_socket_peer_to_string(io
->ifd
, s
, l
);
401 int pa_iochannel_socket_set_rcvbuf(pa_iochannel
*io
, size_t l
) {
404 return pa_socket_set_rcvbuf(io
->ifd
, l
);
407 int pa_iochannel_socket_set_sndbuf(pa_iochannel
*io
, size_t l
) {
410 return pa_socket_set_sndbuf(io
->ofd
, l
);
413 pa_mainloop_api
* pa_iochannel_get_mainloop_api(pa_iochannel
*io
) {