1 | //vio_pipe.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | int roar_vio_open_pipe (struct roar_vio_calls * s0, struct roar_vio_calls * s1, int type, int flags) { |
---|
39 | #ifndef ROAR_WITHOUT_VIO_PIPE |
---|
40 | struct roar_vio_pipe * self; |
---|
41 | int rw = flags & (O_RDONLY|O_WRONLY|O_RDWR); |
---|
42 | |
---|
43 | if ( s0 == NULL || s1 == NULL ) |
---|
44 | return -1; |
---|
45 | |
---|
46 | if ( (self = roar_mm_malloc(sizeof(struct roar_vio_pipe))) == NULL ) |
---|
47 | return -1; |
---|
48 | |
---|
49 | memset(self, 0, sizeof(struct roar_vio_pipe)); |
---|
50 | |
---|
51 | self->refcount = 2; |
---|
52 | self->flags = flags; |
---|
53 | |
---|
54 | if ( type == ROAR_VIO_PIPE_TYPE_AUTO ) { |
---|
55 | #ifdef ROAR_TARGET_WIN32 |
---|
56 | type = ROAR_VIO_PIPE_TYPE_BUFFER; |
---|
57 | #else |
---|
58 | type = ROAR_VIO_PIPE_TYPE_SOCKET; |
---|
59 | #endif |
---|
60 | } |
---|
61 | |
---|
62 | self->type = type; |
---|
63 | |
---|
64 | switch (type) { |
---|
65 | case ROAR_VIO_PIPE_TYPE_BUFFER: |
---|
66 | // no buffers need to be set up here, |
---|
67 | // we handle the NULL pointer in the reader and writer func |
---|
68 | roar_mm_free(self); |
---|
69 | return -1; |
---|
70 | break; |
---|
71 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
72 | self->b.p[0] = self->b.p[1] = self->b.p[2] = self->b.p[3] = -1; |
---|
73 | |
---|
74 | if ( rw == O_RDWR || rw == O_RDONLY ) |
---|
75 | if ( pipe(self->b.p) == -1 ) { |
---|
76 | roar_mm_free(self); |
---|
77 | return -1; |
---|
78 | } |
---|
79 | if ( rw == O_RDWR || rw == O_WRONLY ) |
---|
80 | if ( pipe((self->b.p) + 2) == -1 ) { |
---|
81 | close(self->b.p[0]); |
---|
82 | close(self->b.p[1]); |
---|
83 | roar_mm_free(self); |
---|
84 | return -1; |
---|
85 | } |
---|
86 | break; |
---|
87 | #ifdef ROAR_HAVE_UNIX |
---|
88 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
89 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, self->b.p) == -1 ) { |
---|
90 | roar_mm_free(self); |
---|
91 | return -1; |
---|
92 | } |
---|
93 | |
---|
94 | if ( rw == O_RDONLY ) { |
---|
95 | ROAR_SHUTDOWN(self->b.p[0], SHUT_WR); |
---|
96 | ROAR_SHUTDOWN(self->b.p[1], SHUT_RD); |
---|
97 | } else if ( rw == O_WRONLY ) { |
---|
98 | ROAR_SHUTDOWN(self->b.p[0], SHUT_RD); |
---|
99 | ROAR_SHUTDOWN(self->b.p[1], SHUT_WR); |
---|
100 | } |
---|
101 | break; |
---|
102 | #endif |
---|
103 | default: |
---|
104 | roar_mm_free(self); |
---|
105 | return -1; |
---|
106 | } |
---|
107 | |
---|
108 | roar_vio_pipe_init(s0, self, flags); |
---|
109 | roar_vio_pipe_init(s1, self, flags); |
---|
110 | |
---|
111 | self->s0 = s0; |
---|
112 | |
---|
113 | return 0; |
---|
114 | #else |
---|
115 | return -1; |
---|
116 | #endif |
---|
117 | } |
---|
118 | |
---|
119 | #ifndef ROAR_WITHOUT_VIO_PIPE |
---|
120 | int roar_vio_pipe_init (struct roar_vio_calls * s, struct roar_vio_pipe * self, int flags) { |
---|
121 | int nonblock = ROAR_SOCKET_NONBLOCK; |
---|
122 | |
---|
123 | if ( s == NULL || self == NULL ) |
---|
124 | return -1; |
---|
125 | |
---|
126 | memset(s, 0, sizeof(struct roar_vio_calls)); |
---|
127 | s->flags = ROAR_VIO_FLAGS_NONE; |
---|
128 | s->refc = 1; |
---|
129 | |
---|
130 | s->close = roar_vio_pipe_close; |
---|
131 | s->read = roar_vio_pipe_read; |
---|
132 | s->write = roar_vio_pipe_write; |
---|
133 | s->sync = roar_vio_pipe_sync; |
---|
134 | |
---|
135 | s->inst = (void*) self; |
---|
136 | |
---|
137 | if ( flags & O_NONBLOCK ) { |
---|
138 | roar_vio_pipe_ctl(s, ROAR_VIO_CTL_NONBLOCK, &nonblock); |
---|
139 | } |
---|
140 | |
---|
141 | return 0; |
---|
142 | } |
---|
143 | |
---|
144 | int roar_vio_pipe_close (struct roar_vio_calls * vio) { |
---|
145 | struct roar_vio_pipe * self; |
---|
146 | int idx; |
---|
147 | |
---|
148 | if ( vio == NULL ) |
---|
149 | return -1; |
---|
150 | |
---|
151 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
152 | return -1; |
---|
153 | |
---|
154 | self->refcount--; |
---|
155 | |
---|
156 | switch (self->type) { |
---|
157 | case ROAR_VIO_PIPE_TYPE_BUFFER: |
---|
158 | // this will be a bit more complex as we need to change the flags, too. |
---|
159 | break; |
---|
160 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
161 | switch (ROAR_VIO_PIPE_S(self, vio)) { |
---|
162 | case 0: |
---|
163 | close(self->b.p[0]); |
---|
164 | close(self->b.p[3]); |
---|
165 | self->b.p[0] = -1; |
---|
166 | self->b.p[3] = -1; |
---|
167 | break; |
---|
168 | case 1: |
---|
169 | close(self->b.p[1]); |
---|
170 | close(self->b.p[2]); |
---|
171 | self->b.p[1] = -1; |
---|
172 | self->b.p[2] = -1; |
---|
173 | break; |
---|
174 | } |
---|
175 | break; |
---|
176 | #ifdef ROAR_HAVE_UNIX |
---|
177 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
178 | close(self->b.p[idx = ROAR_VIO_PIPE_S(self, vio)]); |
---|
179 | self->b.p[idx] = -1; |
---|
180 | break; |
---|
181 | #endif |
---|
182 | } |
---|
183 | |
---|
184 | if ( ! self->refcount ) { |
---|
185 | roar_mm_free(self); |
---|
186 | } |
---|
187 | |
---|
188 | vio->inst = NULL; |
---|
189 | return 0; |
---|
190 | } |
---|
191 | |
---|
192 | int roar_vio_pipe_sync (struct roar_vio_calls * vio) { |
---|
193 | // we may add fdatasync() calls here depending on the type |
---|
194 | // but in general they should not be needed on pipes. |
---|
195 | (void)vio; |
---|
196 | return 0; |
---|
197 | } |
---|
198 | |
---|
199 | int roar_vio_pipe_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
200 | struct roar_vio_pipe * self; |
---|
201 | |
---|
202 | if (vio == NULL || cmd == -1) |
---|
203 | return -1; |
---|
204 | |
---|
205 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
206 | return -1; |
---|
207 | |
---|
208 | switch (cmd) { |
---|
209 | case ROAR_VIO_CTL_GET_NAME: |
---|
210 | if ( data == NULL ) |
---|
211 | return -1; |
---|
212 | |
---|
213 | *(char**)data = "pipe"; |
---|
214 | return 0; |
---|
215 | break; |
---|
216 | case ROAR_VIO_CTL_GET_FH: |
---|
217 | case ROAR_VIO_CTL_GET_SELECT_FH: |
---|
218 | #ifdef ROAR_HAVE_UNIX |
---|
219 | if ( self->type == ROAR_VIO_PIPE_TYPE_SOCKET ) { |
---|
220 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
221 | return 0; |
---|
222 | } else { |
---|
223 | return -1; |
---|
224 | } |
---|
225 | #else |
---|
226 | return -1; |
---|
227 | #endif |
---|
228 | break; |
---|
229 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
230 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
231 | switch (self->type) { |
---|
232 | #ifdef ROAR_HAVE_UNIX |
---|
233 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
234 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
235 | return 0; |
---|
236 | break; |
---|
237 | #endif |
---|
238 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
239 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)*2]; |
---|
240 | return 0; |
---|
241 | break; |
---|
242 | } |
---|
243 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
244 | case ROAR_VIO_CTL_GET_SELECT_WRITE_FH: |
---|
245 | switch (self->type) { |
---|
246 | #ifdef ROAR_HAVE_UNIX |
---|
247 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
248 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
249 | return 0; |
---|
250 | break; |
---|
251 | #endif |
---|
252 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
253 | *(int*)data = self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1]; |
---|
254 | return 0; |
---|
255 | break; |
---|
256 | } |
---|
257 | break; |
---|
258 | case ROAR_VIO_CTL_NONBLOCK: |
---|
259 | switch (self->type) { |
---|
260 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
261 | if ( roar_socket_nonblock(self->b.p[ROAR_VIO_PIPE_S(self,vio)*2], *(int*)data) == -1 ) |
---|
262 | return -1; |
---|
263 | return roar_socket_nonblock(self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1], *(int*)data); |
---|
264 | break; |
---|
265 | #ifdef ROAR_HAVE_UNIX |
---|
266 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
267 | return roar_socket_nonblock(self->b.p[ROAR_VIO_PIPE_S(self,vio)], *(int*)data); |
---|
268 | break; |
---|
269 | #endif |
---|
270 | } |
---|
271 | break; |
---|
272 | } |
---|
273 | |
---|
274 | return -1; |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | ssize_t roar_vio_pipe_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
279 | struct roar_vio_pipe * self; |
---|
280 | int idx; |
---|
281 | |
---|
282 | if ( vio == NULL ) |
---|
283 | return -1; |
---|
284 | |
---|
285 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
286 | return -1; |
---|
287 | |
---|
288 | switch (self->type) { |
---|
289 | case ROAR_VIO_PIPE_TYPE_BUFFER: |
---|
290 | idx = ROAR_VIO_PIPE_S(self,vio); |
---|
291 | |
---|
292 | if ( (idx == 0 ? O_WRONLY : O_RDONLY) == (self->flags & (O_RDONLY|O_WRONLY|O_RDWR)) ) { |
---|
293 | raise(SIGPIPE); |
---|
294 | return -1; |
---|
295 | } |
---|
296 | |
---|
297 | if ( self->b.b[idx] == NULL ) |
---|
298 | return 0; |
---|
299 | |
---|
300 | if ( roar_buffer_shift_out(&(self->b.b[idx]), buf, &count) == -1 ) |
---|
301 | return -1; |
---|
302 | |
---|
303 | return count; |
---|
304 | break; |
---|
305 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
306 | return read(self->b.p[ROAR_VIO_PIPE_S(self,vio)*2], buf, count); |
---|
307 | break; |
---|
308 | #ifdef ROAR_HAVE_UNIX |
---|
309 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
310 | return read(self->b.p[ROAR_VIO_PIPE_S(self,vio)], buf, count); |
---|
311 | break; |
---|
312 | #endif |
---|
313 | } |
---|
314 | |
---|
315 | return -1; |
---|
316 | } |
---|
317 | |
---|
318 | ssize_t roar_vio_pipe_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
319 | struct roar_vio_pipe * self; |
---|
320 | struct roar_buffer * next; |
---|
321 | void * data; |
---|
322 | int idx; |
---|
323 | |
---|
324 | if ( vio == NULL ) |
---|
325 | return -1; |
---|
326 | |
---|
327 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
328 | return -1; |
---|
329 | |
---|
330 | switch (self->type) { |
---|
331 | case ROAR_VIO_PIPE_TYPE_BUFFER: |
---|
332 | if ( self->refcount < 2 ) { |
---|
333 | raise(SIGPIPE); |
---|
334 | return -1; |
---|
335 | } |
---|
336 | |
---|
337 | idx = ROAR_VIO_PIPE_SR(self,vio); |
---|
338 | |
---|
339 | if ( (idx == 0 ? O_WRONLY : O_RDONLY) == (self->flags & (O_RDONLY|O_WRONLY|O_RDWR)) ) { |
---|
340 | raise(SIGPIPE); |
---|
341 | return -1; |
---|
342 | } |
---|
343 | |
---|
344 | if ( roar_buffer_new_data(&next, count, &data) == -1 ) |
---|
345 | return -1; |
---|
346 | |
---|
347 | memcpy(data, buf, count); |
---|
348 | |
---|
349 | if ( self->b.b[idx] == NULL ) { |
---|
350 | self->b.b[idx] = next; |
---|
351 | } else { |
---|
352 | if ( roar_buffer_moveinto(self->b.b[idx], &next) == -1 ) { |
---|
353 | roar_buffer_free(next); |
---|
354 | return -1; |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | return count; |
---|
359 | break; |
---|
360 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
361 | return write(self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1], buf, count); |
---|
362 | break; |
---|
363 | #ifdef ROAR_HAVE_UNIX |
---|
364 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
365 | return write(self->b.p[ROAR_VIO_PIPE_S(self,vio)], buf, count); |
---|
366 | break; |
---|
367 | #endif |
---|
368 | } |
---|
369 | |
---|
370 | return -1; |
---|
371 | } |
---|
372 | #endif |
---|
373 | |
---|
374 | //ll |
---|