split polypcore/util.[ch] into polypcore/core-util.[ch] and polyp/util.[ch]
[profile/ivi/pulseaudio.git] / src / polypcore / iochannel.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <errno.h>
31
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
34 #endif
35 #ifdef HAVE_SYS_UN_H
36 #include <sys/un.h>
37 #endif
38
39 #include "winsock.h"
40
41 #include <polyp/xmalloc.h>
42
43 #include <polypcore/core-util.h>
44 #include <polypcore/socket-util.h>
45 #include <polypcore/log.h>
46
47 #include "iochannel.h"
48
49 struct pa_iochannel {
50     int ifd, ofd;
51     pa_mainloop_api* mainloop;
52
53     pa_iochannel_cb_t callback;
54     void*userdata;
55     
56     int readable;
57     int writable;
58     int hungup;
59     
60     int no_close;
61
62     pa_io_event* input_event, *output_event;
63 };
64
65 static void enable_mainloop_sources(pa_iochannel *io) {
66     assert(io);
67
68     if (io->input_event == io->output_event && io->input_event) {
69         pa_io_event_flags_t f = PA_IO_EVENT_NULL;
70         assert(io->input_event);
71         
72         if (!pa_iochannel_is_readable(io))
73             f |= PA_IO_EVENT_INPUT;
74         if (!pa_iochannel_is_writable(io))
75             f |= PA_IO_EVENT_OUTPUT;
76
77         io->mainloop->io_enable(io->input_event, f);
78     } else {
79         if (io->input_event)
80             io->mainloop->io_enable(io->input_event, pa_iochannel_is_readable(io) ? PA_IO_EVENT_NULL : PA_IO_EVENT_INPUT);
81         if (io->output_event)
82             io->mainloop->io_enable(io->output_event, pa_iochannel_is_writable(io) ? PA_IO_EVENT_NULL : PA_IO_EVENT_OUTPUT);
83     }
84 }
85
86 static void callback(pa_mainloop_api* m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
87     pa_iochannel *io = userdata;
88     int changed = 0;
89     
90     assert(m);
91     assert(e);
92     assert(fd >= 0);
93     assert(userdata);
94
95     if ((f & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) && !io->hungup) {
96         io->hungup = 1;
97         changed = 1;
98     }
99
100     if ((f & PA_IO_EVENT_INPUT) && !io->readable) {
101         io->readable = 1;
102         changed = 1;
103         assert(e == io->input_event);
104     }
105     
106     if ((f & PA_IO_EVENT_OUTPUT) && !io->writable) {
107         io->writable = 1;
108         changed = 1;
109         assert(e == io->output_event);
110     }
111
112     if (changed) {
113         enable_mainloop_sources(io);
114         
115         if (io->callback)
116             io->callback(io, io->userdata);
117     }
118 }
119
120 pa_iochannel* pa_iochannel_new(pa_mainloop_api*m, int ifd, int ofd) {
121     pa_iochannel *io;
122     
123     assert(m);
124     assert(ifd >= 0 || ofd >= 0);
125
126     io = pa_xnew(pa_iochannel, 1);
127     io->ifd = ifd;
128     io->ofd = ofd;
129     io->mainloop = m;
130
131     io->userdata = NULL;
132     io->callback = NULL;
133     io->readable = 0;
134     io->writable = 0;
135     io->hungup = 0;
136     io->no_close = 0;
137
138     io->input_event = io->output_event = NULL;
139
140     if (ifd == ofd) {
141         assert(ifd >= 0);
142         pa_make_nonblock_fd(io->ifd);
143         io->input_event = io->output_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT|PA_IO_EVENT_OUTPUT, callback, io);
144     } else {
145
146         if (ifd >= 0) {
147             pa_make_nonblock_fd(io->ifd);
148             io->input_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT, callback, io);
149         }
150
151         if (ofd >= 0) {
152             pa_make_nonblock_fd(io->ofd);
153             io->output_event = m->io_new(m, ofd, PA_IO_EVENT_OUTPUT, callback, io);
154         }
155     }
156
157     return io;
158 }
159
160 void pa_iochannel_free(pa_iochannel*io) {
161     assert(io);
162
163     if (io->input_event)
164         io->mainloop->io_free(io->input_event);
165     
166     if (io->output_event && (io->output_event != io->input_event))
167         io->mainloop->io_free(io->output_event);
168
169     if (!io->no_close) {
170         if (io->ifd >= 0)
171             
172             close(io->ifd);
173         if (io->ofd >= 0 && io->ofd != io->ifd)
174             close(io->ofd);
175     }
176     
177     pa_xfree(io);
178 }
179
180 int pa_iochannel_is_readable(pa_iochannel*io) {
181     assert(io);
182     
183     return io->readable || io->hungup;
184 }
185
186 int pa_iochannel_is_writable(pa_iochannel*io) {
187     assert(io);
188     
189     return io->writable && !io->hungup;
190 }
191
192 int pa_iochannel_is_hungup(pa_iochannel*io) {
193     assert(io);
194     
195     return io->hungup;
196 }
197
198 ssize_t pa_iochannel_write(pa_iochannel*io, const void*data, size_t l) {
199     ssize_t r;
200     
201     assert(io);
202     assert(data);
203     assert(l);
204     assert(io->ofd >= 0);
205
206     r = pa_write(io->ofd, data, l);
207     if (r >= 0) {
208         io->writable = 0;
209         enable_mainloop_sources(io);
210     }
211
212     return r;
213 }
214
215 ssize_t pa_iochannel_read(pa_iochannel*io, void*data, size_t l) {
216     ssize_t r;
217     
218     assert(io);
219     assert(data);
220     assert(io->ifd >= 0);
221
222     r = pa_read(io->ifd, data, l);
223     if (r >= 0) {
224         io->readable = 0;
225         enable_mainloop_sources(io);
226     }
227
228     return r;
229 }
230
231 #ifdef SCM_CREDENTIALS
232
233 int pa_iochannel_creds_supported(pa_iochannel *io) {
234     struct sockaddr_un sa;
235     socklen_t l;
236     
237     assert(io);
238     assert(io->ifd >= 0);
239     assert(io->ofd == io->ifd);
240
241     l = sizeof(sa);
242     
243     if (getsockname(io->ifd, (struct sockaddr*) &sa, &l) < 0)
244         return 0;
245
246     return sa.sun_family == AF_UNIX;
247 }
248
249 int pa_iochannel_creds_enable(pa_iochannel *io) {
250     int t = 1;
251
252     assert(io);
253     assert(io->ifd >= 0);
254     
255     if (setsockopt(io->ifd, SOL_SOCKET, SO_PASSCRED, &t, sizeof(t)) < 0) {
256         pa_log_error("setsockopt(SOL_SOCKET, SO_PASSCRED): %s", strerror(errno));
257         return -1;
258     }
259
260     return 0;
261 }
262
263 ssize_t pa_iochannel_write_with_creds(pa_iochannel*io, const void*data, size_t l) {
264     ssize_t r;
265     struct msghdr mh;
266     struct iovec iov;
267     uint8_t cmsg_data[CMSG_SPACE(sizeof(struct ucred))];
268     struct ucred *ucred;
269     struct cmsghdr *cmsg;
270     
271     assert(io);
272     assert(data);
273     assert(l);
274     assert(io->ofd >= 0);
275
276     memset(&iov, 0, sizeof(iov));
277     iov.iov_base = (void*) data;
278     iov.iov_len = l;
279
280     memset(cmsg_data, 0, sizeof(cmsg_data));
281     cmsg = (struct cmsghdr*)  cmsg_data;
282     cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
283     cmsg->cmsg_level = SOL_SOCKET;
284     cmsg->cmsg_type = SCM_CREDENTIALS;
285
286     ucred = (struct ucred*) CMSG_DATA(cmsg);
287     ucred->pid = getpid();
288     ucred->uid = getuid();
289     ucred->gid = getgid();
290     
291     memset(&mh, 0, sizeof(mh));
292     mh.msg_name = NULL;
293     mh.msg_namelen = 0;
294     mh.msg_iov = &iov;
295     mh.msg_iovlen = 1;
296     mh.msg_control = cmsg_data;
297     mh.msg_controllen = sizeof(cmsg_data);
298     mh.msg_flags = 0;
299
300     if ((r = sendmsg(io->ofd, &mh, MSG_NOSIGNAL)) >= 0) {
301         io->writable = 0;
302         enable_mainloop_sources(io);
303     }
304
305     return r;
306 }
307
308 ssize_t pa_iochannel_read_with_creds(pa_iochannel*io, void*data, size_t l, struct ucred *ucred, int *creds_valid) {
309     ssize_t r;
310     struct msghdr mh;
311     struct iovec iov;
312     uint8_t cmsg_data[CMSG_SPACE(sizeof(struct ucred))];
313     
314     assert(io);
315     assert(data);
316     assert(l);
317     assert(io->ifd >= 0);
318     assert(ucred);
319     assert(creds_valid);
320
321     memset(&iov, 0, sizeof(iov));
322     iov.iov_base = data;
323     iov.iov_len = l;
324
325     memset(cmsg_data, 0, sizeof(cmsg_data));
326
327     memset(&mh, 0, sizeof(mh));
328     mh.msg_name = NULL;
329     mh.msg_namelen = 0;
330     mh.msg_iov = &iov;
331     mh.msg_iovlen = 1;
332     mh.msg_control = cmsg_data;
333     mh.msg_controllen = sizeof(cmsg_data);
334     mh.msg_flags = 0;
335
336     if ((r = recvmsg(io->ifd, &mh, MSG_NOSIGNAL)) >= 0) {
337         struct cmsghdr *cmsg;
338
339         *creds_valid = 0;
340     
341         for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
342             
343             if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_CREDENTIALS) {
344                 assert(cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)));
345                 memcpy(ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
346                 *creds_valid = 1;
347                 break;
348             }
349         }
350
351         io->readable = 0;
352         enable_mainloop_sources(io);
353     }
354     
355     return r;
356 }
357 #else /* SCM_CREDENTIALS */
358
359 int pa_iochannel_creds_supported(pa_iochannel *io) {
360     return 0;
361 }
362
363 int pa_iochannel_creds_enable(pa_iochannel *io) {
364     return -1;
365 }
366
367 ssize_t pa_iochannel_write_with_creds(pa_iochannel*io, const void*data, size_t l) {
368     pa_log_error("pa_iochannel_write_with_creds() not supported.");
369     return -1;
370 }
371
372 ssize_t pa_iochannel_read_with_creds(pa_iochannel*io, void*data, size_t l, struct ucred *ucred, int *creds_valid) {
373     pa_log_error("pa_iochannel_read_with_creds() not supported.");
374     return -1;
375 }
376
377 #endif /* SCM_CREDENTIALS */
378
379 void pa_iochannel_set_callback(pa_iochannel*io, pa_iochannel_cb_t _callback, void *userdata) {
380     assert(io);
381     
382     io->callback = _callback;
383     io->userdata = userdata;
384 }
385
386 void pa_iochannel_set_noclose(pa_iochannel*io, int b) {
387     assert(io);
388     
389     io->no_close = b;
390 }
391
392 void pa_iochannel_socket_peer_to_string(pa_iochannel*io, char*s, size_t l) {
393     assert(io);
394     assert(s);
395     assert(l);
396     
397     pa_socket_peer_to_string(io->ifd, s, l);
398 }
399
400 int pa_iochannel_socket_set_rcvbuf(pa_iochannel *io, size_t l) {
401     assert(io);
402     
403     return pa_socket_set_rcvbuf(io->ifd, l);
404 }
405
406 int pa_iochannel_socket_set_sndbuf(pa_iochannel *io, size_t l) {
407     assert(io);
408     
409     return pa_socket_set_sndbuf(io->ofd, l);
410 }
411
412 pa_mainloop_api* pa_iochannel_get_mainloop_api(pa_iochannel *io) {
413     assert(io);
414     
415     return io->mainloop;
416 }