[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. |
---|
| 217 | return 0; |
---|
| 218 | } |
---|
| 219 | |
---|
[1505] | 220 | int roar_vio_pipe_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 221 | struct roar_vio_pipe * self; |
---|
| 222 | |
---|
| 223 | if (vio == NULL || cmd == -1) |
---|
| 224 | return -1; |
---|
| 225 | |
---|
| 226 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
| 227 | return -1; |
---|
| 228 | |
---|
| 229 | switch (cmd) { |
---|
[3795] | 230 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 231 | if ( data == NULL ) |
---|
| 232 | return -1; |
---|
| 233 | |
---|
| 234 | *(char**)data = "pipe"; |
---|
| 235 | return 0; |
---|
| 236 | break; |
---|
[1505] | 237 | case ROAR_VIO_CTL_GET_FH: |
---|
[3437] | 238 | case ROAR_VIO_CTL_GET_SELECT_FH: |
---|
[3909] | 239 | #ifdef ROAR_HAVE_UNIX |
---|
[1505] | 240 | if ( self->type == ROAR_VIO_PIPE_TYPE_SOCKET ) { |
---|
| 241 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
| 242 | return 0; |
---|
| 243 | } else { |
---|
| 244 | return -1; |
---|
| 245 | } |
---|
[3909] | 246 | #else |
---|
| 247 | return -1; |
---|
| 248 | #endif |
---|
[1505] | 249 | break; |
---|
| 250 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
[3437] | 251 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
[1505] | 252 | switch (self->type) { |
---|
[3909] | 253 | #ifdef ROAR_HAVE_UNIX |
---|
[1505] | 254 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 255 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
| 256 | return 0; |
---|
| 257 | break; |
---|
[3909] | 258 | #endif |
---|
[1505] | 259 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
| 260 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)*2]; |
---|
| 261 | return 0; |
---|
| 262 | break; |
---|
| 263 | } |
---|
| 264 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
[3437] | 265 | case ROAR_VIO_CTL_GET_SELECT_WRITE_FH: |
---|
[1505] | 266 | switch (self->type) { |
---|
[3909] | 267 | #ifdef ROAR_HAVE_UNIX |
---|
[1505] | 268 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 269 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
| 270 | return 0; |
---|
| 271 | break; |
---|
[3909] | 272 | #endif |
---|
[1505] | 273 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
| 274 | *(int*)data = self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1]; |
---|
| 275 | return 0; |
---|
| 276 | break; |
---|
| 277 | } |
---|
| 278 | break; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | return -1; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | |
---|
[1304] | 285 | ssize_t roar_vio_pipe_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 286 | struct roar_vio_pipe * self; |
---|
[1311] | 287 | int idx; |
---|
[1304] | 288 | |
---|
| 289 | if ( vio == NULL ) |
---|
| 290 | return -1; |
---|
| 291 | |
---|
| 292 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
| 293 | return -1; |
---|
| 294 | |
---|
| 295 | switch (self->type) { |
---|
| 296 | case ROAR_VIO_PIPE_TYPE_BUFFER: |
---|
[1311] | 297 | idx = ROAR_VIO_PIPE_S(self,vio); |
---|
| 298 | |
---|
| 299 | if ( (idx == 0 ? O_WRONLY : O_RDONLY) == (self->flags & (O_RDONLY|O_WRONLY|O_RDWR)) ) { |
---|
| 300 | raise(SIGPIPE); |
---|
| 301 | return -1; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | if ( self->b.b[idx] == NULL ) |
---|
| 305 | return 0; |
---|
| 306 | |
---|
| 307 | if ( roar_buffer_shift_out(&(self->b.b[idx]), buf, &count) == -1 ) |
---|
| 308 | return -1; |
---|
| 309 | |
---|
| 310 | return count; |
---|
[1304] | 311 | break; |
---|
| 312 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
| 313 | return read(self->b.p[ROAR_VIO_PIPE_S(self,vio)*2], buf, count); |
---|
| 314 | break; |
---|
[3909] | 315 | #ifdef ROAR_HAVE_UNIX |
---|
[1304] | 316 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 317 | return read(self->b.p[ROAR_VIO_PIPE_S(self,vio)], buf, count); |
---|
| 318 | break; |
---|
[3909] | 319 | #endif |
---|
[1304] | 320 | } |
---|
| 321 | |
---|
| 322 | return -1; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | ssize_t roar_vio_pipe_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 326 | struct roar_vio_pipe * self; |
---|
[1311] | 327 | struct roar_buffer * next; |
---|
| 328 | void * data; |
---|
| 329 | int idx; |
---|
[1304] | 330 | |
---|
| 331 | if ( vio == NULL ) |
---|
| 332 | return -1; |
---|
| 333 | |
---|
| 334 | if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL ) |
---|
| 335 | return -1; |
---|
| 336 | |
---|
| 337 | switch (self->type) { |
---|
| 338 | case ROAR_VIO_PIPE_TYPE_BUFFER: |
---|
[1312] | 339 | if ( self->refcount < 2 ) { |
---|
| 340 | raise(SIGPIPE); |
---|
| 341 | return -1; |
---|
| 342 | } |
---|
| 343 | |
---|
[1311] | 344 | idx = ROAR_VIO_PIPE_SR(self,vio); |
---|
| 345 | |
---|
| 346 | if ( (idx == 0 ? O_WRONLY : O_RDONLY) == (self->flags & (O_RDONLY|O_WRONLY|O_RDWR)) ) { |
---|
| 347 | raise(SIGPIPE); |
---|
| 348 | return -1; |
---|
| 349 | } |
---|
| 350 | |
---|
[3764] | 351 | if ( roar_buffer_new_data(&next, count, &data) == -1 ) |
---|
[1311] | 352 | return -1; |
---|
| 353 | |
---|
| 354 | memcpy(data, buf, count); |
---|
| 355 | |
---|
| 356 | if ( self->b.b[idx] == NULL ) { |
---|
| 357 | self->b.b[idx] = next; |
---|
| 358 | } else { |
---|
| 359 | if ( roar_buffer_add(self->b.b[idx], next) == -1 ) { |
---|
| 360 | roar_buffer_free(next); |
---|
| 361 | return -1; |
---|
| 362 | } |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | return count; |
---|
[1304] | 366 | break; |
---|
| 367 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
[1309] | 368 | return write(self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1], buf, count); |
---|
[1304] | 369 | break; |
---|
[3909] | 370 | #ifdef ROAR_HAVE_UNIX |
---|
[1304] | 371 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 372 | return write(self->b.p[ROAR_VIO_PIPE_S(self,vio)], buf, count); |
---|
| 373 | break; |
---|
[3909] | 374 | #endif |
---|
[1304] | 375 | } |
---|
| 376 | |
---|
| 377 | return -1; |
---|
| 378 | } |
---|
[1388] | 379 | #endif |
---|
[1304] | 380 | |
---|
[1300] | 381 | //ll |
---|