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