[1255] | 1 | //vio_cmd.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011 |
---|
[1255] | 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. |
---|
[1255] | 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_cmd(struct roar_vio_calls * calls, struct roar_vio_calls * dst, |
---|
[1261] | 39 | char * reader, char * writer, int options) { |
---|
[1366] | 40 | #ifndef ROAR_WITHOUT_VIO_CMD |
---|
[1255] | 41 | struct roar_vio_cmd_state * state; |
---|
| 42 | |
---|
| 43 | if ( calls == NULL || dst == NULL ) |
---|
| 44 | return -1; |
---|
| 45 | |
---|
| 46 | if ( reader == NULL && writer == NULL ) |
---|
| 47 | return -1; |
---|
| 48 | |
---|
[3063] | 49 | if ( (state = roar_mm_malloc(sizeof(struct roar_vio_cmd_state))) == NULL ) |
---|
[1255] | 50 | return -1; |
---|
| 51 | |
---|
[1260] | 52 | ROAR_DBG("roar_vio_open_cmd(*): pre reqs are OK"); |
---|
| 53 | |
---|
[1255] | 54 | // clear all |
---|
| 55 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 56 | memset(state, 0, sizeof(struct roar_vio_cmd_state)); |
---|
| 57 | |
---|
| 58 | // init reader and writer: |
---|
| 59 | state->reader.pid = -1; |
---|
| 60 | state->reader.in = -1; |
---|
| 61 | state->reader.out = -1; |
---|
| 62 | |
---|
| 63 | if ( reader != NULL ) |
---|
[4975] | 64 | state->reader.cmd = roar_mm_strdup(reader); |
---|
[1255] | 65 | |
---|
| 66 | state->writer.pid = -1; |
---|
| 67 | state->writer.in = -1; |
---|
| 68 | state->writer.out = -1; |
---|
| 69 | |
---|
| 70 | if ( writer != NULL ) |
---|
[4975] | 71 | state->writer.cmd = roar_mm_strdup(writer); |
---|
[1255] | 72 | |
---|
| 73 | // init state |
---|
| 74 | state->next = dst; |
---|
| 75 | state->options = options; |
---|
[1273] | 76 | state->state = ROAR_VIO_CMD_STATE_OPEN; |
---|
[1255] | 77 | |
---|
| 78 | // init calls |
---|
[1260] | 79 | calls->close = roar_vio_cmd_close; |
---|
| 80 | calls->read = roar_vio_cmd_read; |
---|
| 81 | calls->write = roar_vio_cmd_write; |
---|
| 82 | calls->nonblock = roar_vio_cmd_nonblock; |
---|
| 83 | calls->sync = roar_vio_cmd_sync; |
---|
[1612] | 84 | calls->ctl = roar_vio_cmd_ctl; |
---|
[1260] | 85 | calls->inst = (void*) state; |
---|
| 86 | |
---|
| 87 | ROAR_DBG("roar_vio_open_cmd(*): var setup OK"); |
---|
[1255] | 88 | |
---|
[1257] | 89 | if ( !(options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) { |
---|
| 90 | if ( reader != NULL ) |
---|
| 91 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
| 92 | return roar_vio_cmd_close(calls); |
---|
[1255] | 93 | |
---|
[1257] | 94 | if ( writer != NULL ) |
---|
| 95 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
| 96 | return roar_vio_cmd_close(calls); |
---|
| 97 | } |
---|
[1255] | 98 | |
---|
| 99 | return 0; |
---|
[1366] | 100 | #else |
---|
| 101 | return -1; |
---|
| 102 | #endif |
---|
[1255] | 103 | } |
---|
| 104 | |
---|
[1366] | 105 | #ifndef ROAR_WITHOUT_VIO_CMD |
---|
[1255] | 106 | int roar_vio_cmd_close(struct roar_vio_calls * vio) { |
---|
| 107 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
| 108 | |
---|
[1273] | 109 | state->state = ROAR_VIO_CMD_STATE_CLOSING; |
---|
| 110 | |
---|
[1271] | 111 | if ( state->writer.opened ) { |
---|
| 112 | if ( state->writer.out != -1 ) { |
---|
| 113 | close(state->writer.out); |
---|
| 114 | state->writer.out = -1; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | roar_vio_cmd_sync(vio); |
---|
| 119 | |
---|
[1255] | 120 | if ( state->reader.opened ) |
---|
| 121 | roar_vio_cmd_wait(&(state->reader)); |
---|
| 122 | |
---|
| 123 | if ( state->writer.opened ) |
---|
| 124 | roar_vio_cmd_wait(&(state->writer)); |
---|
| 125 | |
---|
| 126 | if ( state->reader.cmd != NULL ) |
---|
[4975] | 127 | roar_mm_free(state->reader.cmd); |
---|
[1255] | 128 | |
---|
| 129 | if ( state->writer.cmd != NULL ) |
---|
[4975] | 130 | roar_mm_free(state->writer.cmd); |
---|
[1255] | 131 | |
---|
| 132 | roar_vio_close(state->next); |
---|
| 133 | |
---|
[1273] | 134 | // state->state = ROAR_VIO_CMD_STATE_CLOSED; |
---|
[3063] | 135 | roar_mm_free(state); |
---|
[1255] | 136 | |
---|
| 137 | return 0; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | int roar_vio_cmd_fork(struct roar_vio_cmd_child * child) { |
---|
[1256] | 141 | int in[2], out[2]; |
---|
| 142 | |
---|
| 143 | if ( child == NULL ) |
---|
| 144 | return -1; |
---|
| 145 | |
---|
| 146 | if ( child->opened ) |
---|
| 147 | return 0; |
---|
| 148 | |
---|
| 149 | if ( child->cmd == NULL ) |
---|
| 150 | return -1; |
---|
| 151 | |
---|
| 152 | // open some pipes... |
---|
| 153 | if ( pipe(in) != 0 ) |
---|
| 154 | return -1; |
---|
| 155 | |
---|
| 156 | if ( pipe(out) != 0 ) { |
---|
| 157 | close(in[0]); |
---|
| 158 | close(in[1]); |
---|
| 159 | return -1; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | child->pid = fork(); |
---|
| 163 | |
---|
| 164 | switch (child->pid) { |
---|
| 165 | case -1: |
---|
| 166 | close(in[0]); |
---|
| 167 | close(in[1]); |
---|
| 168 | close(out[0]); |
---|
| 169 | close(out[1]); |
---|
| 170 | return -1; |
---|
| 171 | break; |
---|
| 172 | case 0: |
---|
| 173 | close(in[0]); |
---|
| 174 | close(out[1]); |
---|
| 175 | close(ROAR_STDIN); |
---|
| 176 | close(ROAR_STDOUT); |
---|
| 177 | |
---|
| 178 | if ( dup2(out[0], ROAR_STDIN) == -1 ) |
---|
| 179 | _exit(1); |
---|
| 180 | |
---|
| 181 | if ( dup2(in[1], ROAR_STDOUT) == -1 ) |
---|
| 182 | _exit(1); |
---|
| 183 | |
---|
[1652] | 184 | execlp("/bin/sh", "/bin/sh", "-c", child->cmd, (char*)NULL); |
---|
[1256] | 185 | |
---|
| 186 | _exit(1); |
---|
| 187 | break; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | close(in[1]); |
---|
| 191 | close(out[0]); |
---|
| 192 | |
---|
| 193 | child->opened = 1; |
---|
| 194 | child->in = in[0]; |
---|
| 195 | child->out = out[1]; |
---|
| 196 | |
---|
| 197 | return 0; |
---|
[1255] | 198 | } |
---|
| 199 | |
---|
| 200 | int roar_vio_cmd_wait(struct roar_vio_cmd_child * child) { |
---|
| 201 | int status; |
---|
| 202 | |
---|
| 203 | if ( child == NULL ) |
---|
| 204 | return -1; |
---|
| 205 | |
---|
| 206 | if ( !child->opened ) |
---|
| 207 | return 0; |
---|
| 208 | |
---|
| 209 | if ( child->out != -1 ) |
---|
| 210 | close(child->out); |
---|
| 211 | |
---|
| 212 | if ( child->in != -1 ) |
---|
| 213 | close(child->in); |
---|
| 214 | |
---|
| 215 | waitpid(child->pid, &status, 0); |
---|
| 216 | |
---|
| 217 | return 0; |
---|
| 218 | } |
---|
| 219 | |
---|
[1657] | 220 | |
---|
| 221 | |
---|
| 222 | int roar_vio_open_2popen(struct roar_vio_calls * calls, char * command, int options) { |
---|
| 223 | #ifndef ROAR_WITHOUT_VIO_CMD |
---|
| 224 | struct roar_vio_2popen_state * state; |
---|
| 225 | |
---|
| 226 | if ( calls == NULL || command == NULL || options < 0 ) |
---|
| 227 | return -1; |
---|
| 228 | |
---|
[3063] | 229 | if ( (state = roar_mm_malloc(sizeof(struct roar_vio_2popen_state))) == NULL ) |
---|
[1657] | 230 | return -1; |
---|
| 231 | |
---|
| 232 | ROAR_DBG("roar_vio_open_2popen(*): pre reqs are OK"); |
---|
| 233 | |
---|
| 234 | // clear all |
---|
| 235 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
[4617] | 236 | memset(state, 0, sizeof(struct roar_vio_2popen_state)); |
---|
[1657] | 237 | |
---|
| 238 | // init reader and writer: |
---|
| 239 | state->child.pid = -1; |
---|
| 240 | state->child.in = -1; |
---|
| 241 | state->child.out = -1; |
---|
| 242 | |
---|
[4975] | 243 | state->child.cmd = roar_mm_strdup(command); |
---|
[1657] | 244 | |
---|
| 245 | // init state |
---|
| 246 | state->options = options; |
---|
| 247 | state->state = ROAR_VIO_CMD_STATE_OPEN; |
---|
| 248 | |
---|
| 249 | // init calls |
---|
| 250 | calls->close = roar_vio_2popen_close; |
---|
| 251 | /* |
---|
| 252 | calls->read = roar_vio_2popen_read; |
---|
| 253 | calls->write = roar_vio_2popen_write; |
---|
| 254 | calls->nonblock = roar_vio_2popen_nonblock; |
---|
| 255 | calls->sync = roar_vio_2popen_sync; |
---|
| 256 | calls->ctl = roar_vio_2popen_ctl; |
---|
| 257 | */ |
---|
| 258 | calls->inst = (void*) state; |
---|
| 259 | |
---|
| 260 | ROAR_DBG("roar_vio_open_2popen(*): var setup OK"); |
---|
| 261 | |
---|
| 262 | if ( !(options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) { |
---|
| 263 | if ( roar_vio_cmd_fork(&(state->child)) == -1 ) |
---|
| 264 | return roar_vio_2popen_close(calls); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | return 0; |
---|
| 268 | #else |
---|
| 269 | return -1; |
---|
| 270 | #endif |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | #ifndef ROAR_WITHOUT_VIO_CMD |
---|
| 274 | int roar_vio_2popen_close(struct roar_vio_calls * vio) { |
---|
| 275 | struct roar_vio_2popen_state * state = (struct roar_vio_2popen_state *)vio->inst; |
---|
| 276 | |
---|
| 277 | state->state = ROAR_VIO_CMD_STATE_CLOSING; |
---|
| 278 | |
---|
| 279 | if ( state->child.opened ) |
---|
| 280 | roar_vio_cmd_wait(&(state->child)); |
---|
| 281 | |
---|
| 282 | if ( state->child.cmd != NULL ) |
---|
[4975] | 283 | roar_mm_free(state->child.cmd); |
---|
[1657] | 284 | |
---|
[3063] | 285 | roar_mm_free(state); |
---|
[1657] | 286 | |
---|
| 287 | return 0; |
---|
| 288 | } |
---|
| 289 | #endif |
---|
| 290 | |
---|
| 291 | |
---|
[1256] | 292 | // VIOs: |
---|
| 293 | |
---|
| 294 | ssize_t roar_vio_cmd_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 295 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
[1257] | 296 | fd_set rfhs[1], wfhs[1]; |
---|
| 297 | struct timeval tv; |
---|
| 298 | size_t done = 0; |
---|
| 299 | int max_fh; |
---|
| 300 | int ret; |
---|
| 301 | char tbuf[ROAR_VIO_CMD_BUFSIZE]; |
---|
| 302 | char * tp = NULL; |
---|
| 303 | size_t tlen = 0; |
---|
[1271] | 304 | int nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 305 | int in, out; |
---|
[1612] | 306 | int eating = 1; |
---|
[1256] | 307 | |
---|
[1260] | 308 | ROAR_DBG("roar_vio_cmd_read(*) = ?"); |
---|
| 309 | |
---|
[1256] | 310 | if ( !state->reader.opened ) { |
---|
| 311 | if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no reader is forked :) */ |
---|
| 312 | return 0; |
---|
| 313 | |
---|
| 314 | if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no reader exists? -> err */ |
---|
| 315 | return -1; |
---|
| 316 | |
---|
| 317 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
| 318 | return -1; |
---|
| 319 | } |
---|
| 320 | |
---|
[1271] | 321 | in = state->reader.in; |
---|
| 322 | out = state->reader.out; |
---|
| 323 | |
---|
[1612] | 324 | while (done < count || eating) { |
---|
[1271] | 325 | if ( nonblock ) { |
---|
[1257] | 326 | tv.tv_sec = 0; |
---|
| 327 | tv.tv_usec = 1; |
---|
| 328 | } else { |
---|
| 329 | tv.tv_sec = 3600; |
---|
| 330 | tv.tv_usec = 0; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | FD_ZERO(rfhs); |
---|
| 334 | FD_ZERO(wfhs); |
---|
| 335 | |
---|
[1271] | 336 | FD_SET(in, rfhs); |
---|
[1260] | 337 | |
---|
[1271] | 338 | if ( out != -1 ) { |
---|
| 339 | FD_SET(out, wfhs); |
---|
[1260] | 340 | } |
---|
| 341 | |
---|
| 342 | #ifdef DEBUG |
---|
[1271] | 343 | if ( FD_ISSET(in, rfhs) ) { |
---|
[1260] | 344 | ROAR_DBG("roar_vio_cmd_read(*): reader set in fh group"); |
---|
| 345 | } |
---|
| 346 | #endif |
---|
[1257] | 347 | |
---|
[1271] | 348 | max_fh = in > out ? in : out; |
---|
[1260] | 349 | ROAR_DBG("roar_vio_cmd_read(*): max_fh=%i", max_fh); |
---|
[1257] | 350 | |
---|
[1260] | 351 | if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 ) |
---|
[1257] | 352 | return -1; |
---|
| 353 | |
---|
[1260] | 354 | ROAR_DBG("roar_vio_cmd_read(*): select(*) = %i", ret); |
---|
[1271] | 355 | ROAR_DBG("roar_vio_cmd_read(*): reader=%i, writer=%i", in, out); |
---|
[1260] | 356 | |
---|
[1257] | 357 | if ( ret > 0 ) { |
---|
[1271] | 358 | if ( FD_ISSET(in, rfhs) ) { |
---|
[1260] | 359 | ROAR_DBG("roar_vio_cmd_read(*): event on reader"); |
---|
| 360 | |
---|
[1271] | 361 | if ( (ret = read(in, buf+done, count-done)) == -1 ) |
---|
[1257] | 362 | break; |
---|
| 363 | |
---|
| 364 | if ( ret == 0 ) |
---|
| 365 | break; |
---|
| 366 | |
---|
| 367 | done += ret; |
---|
| 368 | } |
---|
| 369 | |
---|
[1271] | 370 | if ( out != -1 && FD_ISSET(out, wfhs) ) { |
---|
[1260] | 371 | ROAR_DBG("roar_vio_cmd_read(*): event on writer"); |
---|
[1612] | 372 | eating = 1; |
---|
[1260] | 373 | |
---|
[1257] | 374 | if ( !tlen ) { |
---|
[1260] | 375 | tp = tbuf; |
---|
| 376 | tlen = 0; |
---|
[1257] | 377 | if ( (tlen = roar_vio_read(state->next, tp, ROAR_VIO_CMD_BUFSIZE)) == -1 ) |
---|
[1260] | 378 | continue; |
---|
[1257] | 379 | } |
---|
| 380 | |
---|
| 381 | if ( tlen ) { |
---|
[1271] | 382 | if ( (ret = write(out, tp, tlen)) > 0 ) { |
---|
[1257] | 383 | tlen -= ret; |
---|
| 384 | tp += ret; |
---|
| 385 | } |
---|
[1260] | 386 | } else { |
---|
[1271] | 387 | close(out); |
---|
| 388 | state->reader.out = out = -1; |
---|
[1257] | 389 | } |
---|
[1612] | 390 | } else { |
---|
| 391 | eating = 0; |
---|
[1257] | 392 | } |
---|
| 393 | } |
---|
| 394 | |
---|
[1271] | 395 | if ( nonblock ) |
---|
[1257] | 396 | break; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | if ( tlen ) { /* we have some data to write to the child... */ |
---|
| 400 | // TODO: try to write it out... |
---|
| 401 | return -1; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | return done; |
---|
[1256] | 405 | } |
---|
| 406 | |
---|
| 407 | ssize_t roar_vio_cmd_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 408 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
[1271] | 409 | fd_set rfhs[1], wfhs[1]; |
---|
| 410 | struct timeval tv; |
---|
| 411 | size_t done = 0; |
---|
| 412 | int max_fh; |
---|
| 413 | int ret; |
---|
| 414 | char tbuf[ROAR_VIO_CMD_BUFSIZE]; |
---|
| 415 | int nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 416 | int in, out; |
---|
[1256] | 417 | |
---|
| 418 | if ( !state->writer.opened ) { |
---|
| 419 | if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no writer is forked :) */ |
---|
| 420 | return 0; |
---|
| 421 | |
---|
| 422 | if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no writer exists? -> err */ |
---|
| 423 | return -1; |
---|
| 424 | |
---|
| 425 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
| 426 | return -1; |
---|
| 427 | } |
---|
| 428 | |
---|
[1271] | 429 | in = state->writer.in; |
---|
| 430 | out = state->writer.out; |
---|
| 431 | |
---|
| 432 | if ( buf == NULL ) { // we are requested to sync |
---|
| 433 | if ( in != -1 ) { |
---|
| 434 | ret = 1; |
---|
| 435 | done = 0; |
---|
| 436 | while (ret > 0) { |
---|
[1273] | 437 | if ( state->state == ROAR_VIO_CMD_STATE_CLOSING ) { |
---|
| 438 | tv.tv_sec = 3600; |
---|
| 439 | tv.tv_usec = 0; |
---|
| 440 | } else { |
---|
| 441 | tv.tv_sec = 0; |
---|
| 442 | tv.tv_usec = done ? 1 : 50000; // 50ms |
---|
| 443 | } |
---|
[1271] | 444 | |
---|
| 445 | done++; |
---|
| 446 | |
---|
| 447 | FD_ZERO(rfhs); |
---|
| 448 | FD_SET(in, rfhs); |
---|
| 449 | |
---|
| 450 | if ( select(in+1, rfhs, NULL, NULL, &tv) < 1 ) |
---|
| 451 | break; |
---|
| 452 | |
---|
| 453 | ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE); |
---|
| 454 | |
---|
| 455 | if ( roar_vio_write(state->next, tbuf, ret) != ret ) |
---|
| 456 | return -1; |
---|
| 457 | } |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | return 0; |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | while (done < count) { |
---|
| 464 | if ( nonblock ) { |
---|
| 465 | tv.tv_sec = 0; |
---|
| 466 | tv.tv_usec = 1; |
---|
| 467 | } else { |
---|
| 468 | tv.tv_sec = 3600; |
---|
| 469 | tv.tv_usec = 0; |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | FD_ZERO(rfhs); |
---|
| 473 | FD_ZERO(wfhs); |
---|
| 474 | |
---|
| 475 | FD_SET(out, wfhs); |
---|
| 476 | |
---|
| 477 | if ( in != -1 ) { |
---|
| 478 | FD_SET(in, rfhs); |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | max_fh = in > out ? in : out; |
---|
| 482 | |
---|
| 483 | if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 ) |
---|
| 484 | return -1; |
---|
| 485 | |
---|
| 486 | if ( ret > 0 ) { |
---|
| 487 | if ( FD_ISSET(out, wfhs) ) { |
---|
| 488 | |
---|
| 489 | if ( (ret = write(out, buf+done, count-done)) == -1 ) |
---|
| 490 | break; |
---|
| 491 | |
---|
| 492 | if ( ret == 0 ) |
---|
| 493 | break; |
---|
| 494 | |
---|
| 495 | done += ret; |
---|
| 496 | } |
---|
[1663] | 497 | if ( in != -1 && FD_ISSET(in, rfhs) ) { |
---|
[1271] | 498 | if ( (ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE)) == -1 ) { /* error case: can not read on reader -> EOF */ |
---|
| 499 | close(in); |
---|
| 500 | state->writer.in = in = -1; |
---|
| 501 | break; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | if ( roar_vio_write(state->next, tbuf, ret) != ret ) |
---|
| 505 | return -1; |
---|
| 506 | } |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | if ( nonblock ) |
---|
| 510 | break; |
---|
| 511 | } |
---|
[1256] | 512 | |
---|
[1663] | 513 | return done; |
---|
[1256] | 514 | } |
---|
| 515 | |
---|
| 516 | int roar_vio_cmd_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 517 | struct roar_vio_cmd_state * self = (struct roar_vio_cmd_state *)vio->inst; |
---|
| 518 | |
---|
| 519 | self->options |= ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 520 | |
---|
| 521 | if ( state == ROAR_SOCKET_BLOCK ) |
---|
| 522 | self->options -= ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 523 | |
---|
[5111] | 524 | _LIBROAR_IGNORE_RET(roar_vio_nonblock(self->next, state)); // this should help, but may not nessessery. |
---|
[1257] | 525 | |
---|
[1256] | 526 | return 0; |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | int roar_vio_cmd_sync (struct roar_vio_calls * vio) { |
---|
| 530 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
| 531 | int oldblock; |
---|
| 532 | int ret = 0; |
---|
| 533 | |
---|
| 534 | oldblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK ? ROAR_SOCKET_NONBLOCK : ROAR_SOCKET_BLOCK; |
---|
| 535 | |
---|
| 536 | if ( roar_vio_cmd_nonblock(vio, ROAR_SOCKET_BLOCK) == -1 ) |
---|
| 537 | return -1; |
---|
| 538 | |
---|
| 539 | if ( roar_vio_cmd_write(vio, NULL, 0) == -1 ) |
---|
| 540 | ret = -1; |
---|
| 541 | |
---|
| 542 | if ( roar_vio_cmd_read(vio, NULL, 0) == -1 ) |
---|
| 543 | ret = -1; |
---|
| 544 | |
---|
| 545 | if ( roar_vio_cmd_nonblock(vio, oldblock) == -1 ) |
---|
| 546 | return -1; |
---|
| 547 | |
---|
| 548 | return ret; |
---|
| 549 | } |
---|
[1612] | 550 | |
---|
| 551 | int roar_vio_cmd_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 552 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
| 553 | char buf[1]; |
---|
| 554 | |
---|
| 555 | ROAR_WARN("roar_vio_cmd_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data); |
---|
| 556 | |
---|
| 557 | switch (cmd) { |
---|
[3795] | 558 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 559 | if ( data == NULL ) |
---|
| 560 | return -1; |
---|
| 561 | |
---|
| 562 | *(char**)data = "cmd"; |
---|
| 563 | return 0; |
---|
| 564 | break; |
---|
[1612] | 565 | case ROAR_VIO_CTL_GET_NEXT: |
---|
| 566 | *(struct roar_vio_calls **)data = state->next; |
---|
| 567 | return -1; |
---|
| 568 | break; |
---|
| 569 | case ROAR_VIO_CTL_GET_FH: |
---|
| 570 | return -1; |
---|
| 571 | break; |
---|
| 572 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
[3437] | 573 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
[1612] | 574 | // if ( !state->reader.opened ) { |
---|
| 575 | //for (i = 0; i < 128; i++) |
---|
| 576 | roar_vio_cmd_read(vio, buf, 0); |
---|
| 577 | // } |
---|
| 578 | if ( state->reader.opened ) { |
---|
| 579 | ROAR_WARN("roar_vio_cmd_ctl(vio=%p, cmd=ROAR_VIO_CTL_GET_READ_FH(0x%.8x), data=%p) = 0 // fh=%i", vio, cmd, data, state->reader.in); |
---|
| 580 | *(int*)data = state->reader.in; |
---|
| 581 | return 0; |
---|
| 582 | } |
---|
| 583 | return -1; |
---|
| 584 | break; |
---|
| 585 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
| 586 | return -1; |
---|
| 587 | break; |
---|
| 588 | default: |
---|
| 589 | return -1; |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | return 0; |
---|
| 593 | } |
---|
[1366] | 594 | #endif |
---|
[1256] | 595 | |
---|
[1264] | 596 | // MISC: |
---|
| 597 | int roar_vio_open_gzip(struct roar_vio_calls * calls, struct roar_vio_calls * dst, int level) { |
---|
[4956] | 598 | roar_debug_warn_obsolete("roar_vio_open_gzip", "roar_vio_open_zlib", NULL); |
---|
[1264] | 599 | |
---|
[4984] | 600 | #ifdef ROAR_HAVE_LIBZ |
---|
[4956] | 601 | return roar_vio_open_zlib(calls, dst, level, 1); |
---|
[4984] | 602 | #else |
---|
| 603 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
| 604 | return -1; |
---|
| 605 | #endif |
---|
[1264] | 606 | } |
---|
| 607 | |
---|
[1269] | 608 | int roar_vio_open_gpg(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int wronly, char * opts, int options) { |
---|
[1400] | 609 | #if defined(ROAR_HAVE_BIN_GPG) && !defined(ROAR_WITHOUT_VIO_CMD) |
---|
[1266] | 610 | char command[1024]; |
---|
[1269] | 611 | char para[1024] = {0}; |
---|
[1266] | 612 | int pwpipe[2]; |
---|
| 613 | int ret; |
---|
| 614 | |
---|
[1269] | 615 | /* |
---|
| 616 | #define ROAR_VIO_PGP_OPTS_NONE 0x00 |
---|
| 617 | #define ROAR_VIO_PGP_OPTS_ASCII 0x01 |
---|
| 618 | #define ROAR_VIO_PGP_OPTS_SIGN 0x02 |
---|
| 619 | #define ROAR_VIO_PGP_OPTS_TEXTMODE 0x04 |
---|
| 620 | */ |
---|
| 621 | |
---|
| 622 | if ( options & ROAR_VIO_PGP_OPTS_ASCII ) |
---|
| 623 | strncat(para, "--armor ", 16); |
---|
| 624 | |
---|
| 625 | if ( options & ROAR_VIO_PGP_OPTS_SIGN ) |
---|
| 626 | strncat(para, "--sign ", 16); |
---|
| 627 | |
---|
| 628 | if ( options & ROAR_VIO_PGP_OPTS_TEXTMODE ) |
---|
| 629 | strncat(para, "--textmode ", 16); |
---|
| 630 | |
---|
[1266] | 631 | if ( pw != NULL ) { |
---|
| 632 | if ( pipe(pwpipe) == -1 ) |
---|
| 633 | return -1; |
---|
| 634 | |
---|
[1269] | 635 | snprintf(command, 1024, "%s --batch --no-verbose --quiet --passphrase-repeat 0 --passphrase-fd %i %s %s", ROAR_HAVE_BIN_GPG, pwpipe[0], para, opts); |
---|
[1266] | 636 | |
---|
| 637 | write(pwpipe[1], pw, strlen(pw)); |
---|
| 638 | |
---|
| 639 | close(pwpipe[1]); |
---|
| 640 | } else { |
---|
[1278] | 641 | snprintf(command, 1024, "%s --no-verbose --quiet %s %s", ROAR_HAVE_BIN_GPG, para, opts); |
---|
[1266] | 642 | } |
---|
| 643 | |
---|
| 644 | if ( wronly ) { |
---|
| 645 | ret = roar_vio_open_cmd(calls, dst, NULL, command, 0); |
---|
| 646 | } else { |
---|
| 647 | ret = roar_vio_open_cmd(calls, dst, command, NULL, 0); |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | if ( pw != NULL ) |
---|
| 651 | close(pwpipe[0]); |
---|
| 652 | |
---|
| 653 | return ret; |
---|
| 654 | #else |
---|
| 655 | return -1; |
---|
| 656 | #endif |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | int roar_vio_open_pgp_decrypt(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw) { |
---|
[1269] | 660 | return roar_vio_open_gpg(calls, dst, pw, 0, "-d", ROAR_VIO_PGP_OPTS_NONE); |
---|
| 661 | } |
---|
| 662 | |
---|
| 663 | int roar_vio_open_pgp_store(struct roar_vio_calls * calls, struct roar_vio_calls * dst, int options) { |
---|
| 664 | return roar_vio_open_gpg(calls, dst, NULL, 1, "--store", options); |
---|
[1266] | 665 | } |
---|
| 666 | |
---|
[1269] | 667 | int roar_vio_open_pgp_encrypt_sym(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int options) { |
---|
| 668 | return roar_vio_open_gpg(calls, dst, pw, 1, "--symmetric", options); |
---|
| 669 | } |
---|
| 670 | int roar_vio_open_pgp_encrypt_pub(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int options, char * recipient) { |
---|
| 671 | char buf[1024]; |
---|
| 672 | |
---|
| 673 | snprintf(buf, 1024, "-e -r %s", recipient); |
---|
| 674 | return roar_vio_open_gpg(calls, dst, pw, 1, buf, options); |
---|
| 675 | } |
---|
[1266] | 676 | |
---|
[1255] | 677 | //ll |
---|