[1300] | 1 | //vio_pipe.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011 |
---|
[1300] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[1300] | 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 | |
---|
[1304] | 38 | int roar_vio_open_pipe (struct roar_vio_calls * s0, struct roar_vio_calls * s1, int type, int flags) { |
---|
[1388] | 39 | #ifndef ROAR_WITHOUT_VIO_PIPE |
---|
[1301] | 40 | struct roar_vio_pipe * self; |
---|
| 41 | int rw = flags & (O_RDONLY|O_WRONLY|O_RDWR); |
---|
| 42 | |
---|
[1304] | 43 | if ( s0 == NULL || s1 == NULL ) |
---|
[1301] | 44 | return -1; |
---|
| 45 | |
---|
[3063] | 46 | if ( (self = roar_mm_malloc(sizeof(struct roar_vio_pipe))) == NULL ) |
---|
[1301] | 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 | |
---|
[1304] | 62 | self->type = type; |
---|
| 63 | |
---|
[1301] | 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 |
---|
[3063] | 68 | roar_mm_free(self); |
---|
[1304] | 69 | return -1; |
---|
[1301] | 70 | break; |
---|
| 71 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
[1311] | 72 | self->b.p[0] = self->b.p[1] = self->b.p[2] = self->b.p[3] = -1; |
---|
| 73 | |
---|
[1301] | 74 | if ( rw == O_RDWR || rw == O_RDONLY ) |
---|
| 75 | if ( pipe(self->b.p) == -1 ) { |
---|
[3063] | 76 | roar_mm_free(self); |
---|
[1301] | 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]); |
---|
[3063] | 83 | roar_mm_free(self); |
---|
[1301] | 84 | return -1; |
---|
| 85 | } |
---|
| 86 | break; |
---|
[3909] | 87 | #ifdef ROAR_HAVE_UNIX |
---|
[1301] | 88 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 89 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, self->b.p) == -1 ) { |
---|
[3063] | 90 | roar_mm_free(self); |
---|
[1301] | 91 | return -1; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | if ( rw == O_RDONLY ) { |
---|
[1470] | 95 | ROAR_SHUTDOWN(self->b.p[0], SHUT_WR); |
---|
| 96 | ROAR_SHUTDOWN(self->b.p[1], SHUT_RD); |
---|
[1301] | 97 | } else if ( rw == O_WRONLY ) { |
---|
[1470] | 98 | ROAR_SHUTDOWN(self->b.p[0], SHUT_RD); |
---|
| 99 | ROAR_SHUTDOWN(self->b.p[1], SHUT_WR); |
---|
[1301] | 100 | } |
---|
| 101 | break; |
---|
[3909] | 102 | #endif |
---|
[1301] | 103 | default: |
---|
[3063] | 104 | roar_mm_free(self); |
---|
[1301] | 105 | return -1; |
---|
| 106 | } |
---|
| 107 | |
---|
[1304] | 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; |
---|
[1388] | 114 | #else |
---|
| 115 | return -1; |
---|
| 116 | #endif |
---|
[1304] | 117 | } |
---|
| 118 | |
---|
[1388] | 119 | #ifndef ROAR_WITHOUT_VIO_PIPE |
---|
[1304] | 120 | int roar_vio_pipe_init (struct roar_vio_calls * s, struct roar_vio_pipe * self, int flags) { |
---|
| 121 | if ( s == NULL || self == NULL ) |
---|
| 122 | return -1; |
---|
| 123 | |
---|
| 124 | memset(s, 0, sizeof(struct roar_vio_calls)); |
---|
| 125 | |
---|
| 126 | s->close = roar_vio_pipe_close; |
---|
| 127 | s->read = roar_vio_pipe_read; |
---|
| 128 | s->write = roar_vio_pipe_write; |
---|
| 129 | s->nonblock = roar_vio_pipe_nonblock; |
---|
| 130 | s->sync = roar_vio_pipe_sync; |
---|
| 131 | |
---|
| 132 | s->inst = (void*) self; |
---|
| 133 | |
---|
| 134 | if ( flags & O_NONBLOCK ) { |
---|
| 135 | roar_vio_pipe_nonblock(s, ROAR_SOCKET_NONBLOCK); |
---|
| 136 | } |
---|
| 137 | |
---|
[1301] | 138 | return 0; |
---|
| 139 | } |
---|
| 140 | |
---|
[1304] | 141 | int roar_vio_pipe_close (struct roar_vio_calls * vio) { |
---|
| 142 | struct roar_vio_pipe * self; |
---|
| 143 | int idx; |
---|
| 144 | |
---|
| 145 | if ( vio == NULL ) |
---|
| 146 | return -1; |
---|
| 147 | |
---|
| 148 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
| 149 | return -1; |
---|
| 150 | |
---|
| 151 | self->refcount--; |
---|
| 152 | |
---|
| 153 | switch (self->type) { |
---|
| 154 | case ROAR_VIO_PIPE_TYPE_BUFFER: |
---|
| 155 | // this will be a bit more complex as we need to change the flags, too. |
---|
| 156 | break; |
---|
| 157 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
| 158 | switch (ROAR_VIO_PIPE_S(self, vio)) { |
---|
| 159 | case 0: |
---|
| 160 | close(self->b.p[0]); |
---|
[1309] | 161 | close(self->b.p[3]); |
---|
[1304] | 162 | self->b.p[0] = -1; |
---|
[1309] | 163 | self->b.p[3] = -1; |
---|
[1304] | 164 | break; |
---|
| 165 | case 1: |
---|
[1309] | 166 | close(self->b.p[1]); |
---|
[1304] | 167 | close(self->b.p[2]); |
---|
[1309] | 168 | self->b.p[1] = -1; |
---|
[1304] | 169 | self->b.p[2] = -1; |
---|
| 170 | break; |
---|
| 171 | } |
---|
| 172 | break; |
---|
[3909] | 173 | #ifdef ROAR_HAVE_UNIX |
---|
[1304] | 174 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 175 | close(self->b.p[idx = ROAR_VIO_PIPE_S(self, vio)]); |
---|
| 176 | self->b.p[idx] = -1; |
---|
| 177 | break; |
---|
[3909] | 178 | #endif |
---|
[1304] | 179 | } |
---|
| 180 | |
---|
| 181 | if ( ! self->refcount ) { |
---|
[3063] | 182 | roar_mm_free(self); |
---|
[1304] | 183 | } |
---|
| 184 | |
---|
| 185 | vio->inst = NULL; |
---|
| 186 | return 0; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | int roar_vio_pipe_nonblock(struct roar_vio_calls * vio, int state) { |
---|
[1311] | 190 | struct roar_vio_pipe * self; |
---|
| 191 | |
---|
| 192 | if ( vio == NULL ) |
---|
| 193 | return -1; |
---|
| 194 | |
---|
| 195 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
| 196 | return -1; |
---|
| 197 | |
---|
| 198 | switch (self->type) { |
---|
| 199 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
| 200 | if ( roar_socket_nonblock(self->b.p[ROAR_VIO_PIPE_S(self,vio)*2], state) == -1 ) |
---|
| 201 | return -1; |
---|
| 202 | return roar_socket_nonblock(self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1], state); |
---|
| 203 | break; |
---|
[3909] | 204 | #ifdef ROAR_HAVE_UNIX |
---|
[1311] | 205 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 206 | return roar_socket_nonblock(self->b.p[ROAR_VIO_PIPE_S(self,vio)], state); |
---|
| 207 | break; |
---|
[3909] | 208 | #endif |
---|
[1311] | 209 | } |
---|
| 210 | |
---|
[1304] | 211 | return -1; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | int roar_vio_pipe_sync (struct roar_vio_calls * vio) { |
---|
| 215 | // we may add fdatasync() calls here depending on the type |
---|
| 216 | // but in general they should not be needed on pipes. |
---|
[5270] | 217 | (void)vio; |
---|
[1304] | 218 | return 0; |
---|
| 219 | } |
---|
| 220 | |
---|
[1505] | 221 | int roar_vio_pipe_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 222 | struct roar_vio_pipe * self; |
---|
| 223 | |
---|
| 224 | if (vio == NULL || cmd == -1) |
---|
| 225 | return -1; |
---|
| 226 | |
---|
| 227 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
| 228 | return -1; |
---|
| 229 | |
---|
| 230 | switch (cmd) { |
---|
[3795] | 231 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 232 | if ( data == NULL ) |
---|
| 233 | return -1; |
---|
| 234 | |
---|
| 235 | *(char**)data = "pipe"; |
---|
| 236 | return 0; |
---|
| 237 | break; |
---|
[1505] | 238 | case ROAR_VIO_CTL_GET_FH: |
---|
[3437] | 239 | case ROAR_VIO_CTL_GET_SELECT_FH: |
---|
[3909] | 240 | #ifdef ROAR_HAVE_UNIX |
---|
[1505] | 241 | if ( self->type == ROAR_VIO_PIPE_TYPE_SOCKET ) { |
---|
| 242 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
| 243 | return 0; |
---|
| 244 | } else { |
---|
| 245 | return -1; |
---|
| 246 | } |
---|
[3909] | 247 | #else |
---|
| 248 | return -1; |
---|
| 249 | #endif |
---|
[1505] | 250 | break; |
---|
| 251 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
[3437] | 252 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
[1505] | 253 | switch (self->type) { |
---|
[3909] | 254 | #ifdef ROAR_HAVE_UNIX |
---|
[1505] | 255 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 256 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
| 257 | return 0; |
---|
| 258 | break; |
---|
[3909] | 259 | #endif |
---|
[1505] | 260 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
| 261 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)*2]; |
---|
| 262 | return 0; |
---|
| 263 | break; |
---|
| 264 | } |
---|
| 265 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
[3437] | 266 | case ROAR_VIO_CTL_GET_SELECT_WRITE_FH: |
---|
[1505] | 267 | switch (self->type) { |
---|
[3909] | 268 | #ifdef ROAR_HAVE_UNIX |
---|
[1505] | 269 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 270 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
| 271 | return 0; |
---|
| 272 | break; |
---|
[3909] | 273 | #endif |
---|
[1505] | 274 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
| 275 | *(int*)data = self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1]; |
---|
| 276 | return 0; |
---|
| 277 | break; |
---|
| 278 | } |
---|
| 279 | break; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | return -1; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | |
---|
[1304] | 286 | ssize_t roar_vio_pipe_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 287 | struct roar_vio_pipe * self; |
---|
[1311] | 288 | int idx; |
---|
[1304] | 289 | |
---|
| 290 | if ( vio == NULL ) |
---|
| 291 | return -1; |
---|
| 292 | |
---|
| 293 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
| 294 | return -1; |
---|
| 295 | |
---|
| 296 | switch (self->type) { |
---|
| 297 | case ROAR_VIO_PIPE_TYPE_BUFFER: |
---|
[1311] | 298 | idx = ROAR_VIO_PIPE_S(self,vio); |
---|
| 299 | |
---|
| 300 | if ( (idx == 0 ? O_WRONLY : O_RDONLY) == (self->flags & (O_RDONLY|O_WRONLY|O_RDWR)) ) { |
---|
| 301 | raise(SIGPIPE); |
---|
| 302 | return -1; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | if ( self->b.b[idx] == NULL ) |
---|
| 306 | return 0; |
---|
| 307 | |
---|
| 308 | if ( roar_buffer_shift_out(&(self->b.b[idx]), buf, &count) == -1 ) |
---|
| 309 | return -1; |
---|
| 310 | |
---|
| 311 | return count; |
---|
[1304] | 312 | break; |
---|
| 313 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
| 314 | return read(self->b.p[ROAR_VIO_PIPE_S(self,vio)*2], buf, count); |
---|
| 315 | break; |
---|
[3909] | 316 | #ifdef ROAR_HAVE_UNIX |
---|
[1304] | 317 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 318 | return read(self->b.p[ROAR_VIO_PIPE_S(self,vio)], buf, count); |
---|
| 319 | break; |
---|
[3909] | 320 | #endif |
---|
[1304] | 321 | } |
---|
| 322 | |
---|
| 323 | return -1; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | ssize_t roar_vio_pipe_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 327 | struct roar_vio_pipe * self; |
---|
[1311] | 328 | struct roar_buffer * next; |
---|
| 329 | void * data; |
---|
| 330 | int idx; |
---|
[1304] | 331 | |
---|
| 332 | if ( vio == NULL ) |
---|
| 333 | return -1; |
---|
| 334 | |
---|
| 335 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
| 336 | return -1; |
---|
| 337 | |
---|
| 338 | switch (self->type) { |
---|
| 339 | case ROAR_VIO_PIPE_TYPE_BUFFER: |
---|
[1312] | 340 | if ( self->refcount < 2 ) { |
---|
| 341 | raise(SIGPIPE); |
---|
| 342 | return -1; |
---|
| 343 | } |
---|
| 344 | |
---|
[1311] | 345 | idx = ROAR_VIO_PIPE_SR(self,vio); |
---|
| 346 | |
---|
| 347 | if ( (idx == 0 ? O_WRONLY : O_RDONLY) == (self->flags & (O_RDONLY|O_WRONLY|O_RDWR)) ) { |
---|
| 348 | raise(SIGPIPE); |
---|
| 349 | return -1; |
---|
| 350 | } |
---|
| 351 | |
---|
[3764] | 352 | if ( roar_buffer_new_data(&next, count, &data) == -1 ) |
---|
[1311] | 353 | return -1; |
---|
| 354 | |
---|
| 355 | memcpy(data, buf, count); |
---|
| 356 | |
---|
| 357 | if ( self->b.b[idx] == NULL ) { |
---|
| 358 | self->b.b[idx] = next; |
---|
| 359 | } else { |
---|
| 360 | if ( roar_buffer_add(self->b.b[idx], next) == -1 ) { |
---|
| 361 | roar_buffer_free(next); |
---|
| 362 | return -1; |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | return count; |
---|
[1304] | 367 | break; |
---|
| 368 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
[1309] | 369 | return write(self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1], buf, count); |
---|
[1304] | 370 | break; |
---|
[3909] | 371 | #ifdef ROAR_HAVE_UNIX |
---|
[1304] | 372 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 373 | return write(self->b.p[ROAR_VIO_PIPE_S(self,vio)], buf, count); |
---|
| 374 | break; |
---|
[3909] | 375 | #endif |
---|
[1304] | 376 | } |
---|
| 377 | |
---|
| 378 | return -1; |
---|
| 379 | } |
---|
[1388] | 380 | #endif |
---|
[1304] | 381 | |
---|
[1300] | 382 | //ll |
---|