[1300] | 1 | //vio_pipe.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2013 |
---|
[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) { |
---|
[5276] | 121 | int nonblock = ROAR_SOCKET_NONBLOCK; |
---|
| 122 | |
---|
[1304] | 123 | if ( s == NULL || self == NULL ) |
---|
| 124 | return -1; |
---|
| 125 | |
---|
| 126 | memset(s, 0, sizeof(struct roar_vio_calls)); |
---|
[5388] | 127 | s->flags = ROAR_VIO_FLAGS_NONE; |
---|
| 128 | s->refc = 1; |
---|
[1304] | 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 ) { |
---|
[5276] | 138 | roar_vio_pipe_ctl(s, ROAR_VIO_CTL_NONBLOCK, &nonblock); |
---|
[1304] | 139 | } |
---|
| 140 | |
---|
[1301] | 141 | return 0; |
---|
| 142 | } |
---|
| 143 | |
---|
[1304] | 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]); |
---|
[1309] | 164 | close(self->b.p[3]); |
---|
[1304] | 165 | self->b.p[0] = -1; |
---|
[1309] | 166 | self->b.p[3] = -1; |
---|
[1304] | 167 | break; |
---|
| 168 | case 1: |
---|
[1309] | 169 | close(self->b.p[1]); |
---|
[1304] | 170 | close(self->b.p[2]); |
---|
[1309] | 171 | self->b.p[1] = -1; |
---|
[1304] | 172 | self->b.p[2] = -1; |
---|
| 173 | break; |
---|
| 174 | } |
---|
| 175 | break; |
---|
[3909] | 176 | #ifdef ROAR_HAVE_UNIX |
---|
[1304] | 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; |
---|
[3909] | 181 | #endif |
---|
[1304] | 182 | } |
---|
| 183 | |
---|
| 184 | if ( ! self->refcount ) { |
---|
[3063] | 185 | roar_mm_free(self); |
---|
[1304] | 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. |
---|
[5270] | 195 | (void)vio; |
---|
[1304] | 196 | return 0; |
---|
| 197 | } |
---|
| 198 | |
---|
[5278] | 199 | int roar_vio_pipe_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
[1505] | 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) { |
---|
[3795] | 209 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 210 | if ( data == NULL ) |
---|
| 211 | return -1; |
---|
| 212 | |
---|
| 213 | *(char**)data = "pipe"; |
---|
| 214 | return 0; |
---|
| 215 | break; |
---|
[1505] | 216 | case ROAR_VIO_CTL_GET_FH: |
---|
[3437] | 217 | case ROAR_VIO_CTL_GET_SELECT_FH: |
---|
[3909] | 218 | #ifdef ROAR_HAVE_UNIX |
---|
[1505] | 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 | } |
---|
[3909] | 225 | #else |
---|
| 226 | return -1; |
---|
| 227 | #endif |
---|
[1505] | 228 | break; |
---|
| 229 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
[3437] | 230 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
[1505] | 231 | switch (self->type) { |
---|
[3909] | 232 | #ifdef ROAR_HAVE_UNIX |
---|
[1505] | 233 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 234 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
| 235 | return 0; |
---|
| 236 | break; |
---|
[3909] | 237 | #endif |
---|
[1505] | 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: |
---|
[3437] | 244 | case ROAR_VIO_CTL_GET_SELECT_WRITE_FH: |
---|
[1505] | 245 | switch (self->type) { |
---|
[3909] | 246 | #ifdef ROAR_HAVE_UNIX |
---|
[1505] | 247 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 248 | *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)]; |
---|
| 249 | return 0; |
---|
| 250 | break; |
---|
[3909] | 251 | #endif |
---|
[1505] | 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; |
---|
[5276] | 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; |
---|
[1505] | 272 | } |
---|
| 273 | |
---|
| 274 | return -1; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | |
---|
[1304] | 278 | ssize_t roar_vio_pipe_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 279 | struct roar_vio_pipe * self; |
---|
[1311] | 280 | int idx; |
---|
[1304] | 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: |
---|
[1311] | 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; |
---|
[1304] | 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; |
---|
[3909] | 308 | #ifdef ROAR_HAVE_UNIX |
---|
[1304] | 309 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 310 | return read(self->b.p[ROAR_VIO_PIPE_S(self,vio)], buf, count); |
---|
| 311 | break; |
---|
[3909] | 312 | #endif |
---|
[1304] | 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; |
---|
[1311] | 320 | struct roar_buffer * next; |
---|
| 321 | void * data; |
---|
| 322 | int idx; |
---|
[1304] | 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: |
---|
[1312] | 332 | if ( self->refcount < 2 ) { |
---|
| 333 | raise(SIGPIPE); |
---|
| 334 | return -1; |
---|
| 335 | } |
---|
| 336 | |
---|
[1311] | 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 | |
---|
[3764] | 344 | if ( roar_buffer_new_data(&next, count, &data) == -1 ) |
---|
[1311] | 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 { |
---|
[5300] | 352 | if ( roar_buffer_moveinto(self->b.b[idx], &next) == -1 ) { |
---|
[1311] | 353 | roar_buffer_free(next); |
---|
| 354 | return -1; |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | return count; |
---|
[1304] | 359 | break; |
---|
| 360 | case ROAR_VIO_PIPE_TYPE_PIPE: |
---|
[1309] | 361 | return write(self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1], buf, count); |
---|
[1304] | 362 | break; |
---|
[3909] | 363 | #ifdef ROAR_HAVE_UNIX |
---|
[1304] | 364 | case ROAR_VIO_PIPE_TYPE_SOCKET: |
---|
| 365 | return write(self->b.p[ROAR_VIO_PIPE_S(self,vio)], buf, count); |
---|
| 366 | break; |
---|
[3909] | 367 | #endif |
---|
[1304] | 368 | } |
---|
| 369 | |
---|
| 370 | return -1; |
---|
| 371 | } |
---|
[1388] | 372 | #endif |
---|
[1304] | 373 | |
---|
[1300] | 374 | //ll |
---|