[1255] | 1 | //vio_cmd.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012 |
---|
[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)); |
---|
[5388] | 56 | calls->flags = ROAR_VIO_FLAGS_NONE; |
---|
| 57 | calls->refc = 1; |
---|
[1255] | 58 | memset(state, 0, sizeof(struct roar_vio_cmd_state)); |
---|
| 59 | |
---|
| 60 | // init reader and writer: |
---|
| 61 | state->reader.pid = -1; |
---|
| 62 | state->reader.in = -1; |
---|
| 63 | state->reader.out = -1; |
---|
| 64 | |
---|
| 65 | if ( reader != NULL ) |
---|
[4975] | 66 | state->reader.cmd = roar_mm_strdup(reader); |
---|
[1255] | 67 | |
---|
| 68 | state->writer.pid = -1; |
---|
| 69 | state->writer.in = -1; |
---|
| 70 | state->writer.out = -1; |
---|
| 71 | |
---|
| 72 | if ( writer != NULL ) |
---|
[4975] | 73 | state->writer.cmd = roar_mm_strdup(writer); |
---|
[1255] | 74 | |
---|
| 75 | // init state |
---|
| 76 | state->next = dst; |
---|
| 77 | state->options = options; |
---|
[1273] | 78 | state->state = ROAR_VIO_CMD_STATE_OPEN; |
---|
[1255] | 79 | |
---|
| 80 | // init calls |
---|
[1260] | 81 | calls->close = roar_vio_cmd_close; |
---|
| 82 | calls->read = roar_vio_cmd_read; |
---|
| 83 | calls->write = roar_vio_cmd_write; |
---|
| 84 | calls->sync = roar_vio_cmd_sync; |
---|
[1612] | 85 | calls->ctl = roar_vio_cmd_ctl; |
---|
[1260] | 86 | calls->inst = (void*) state; |
---|
| 87 | |
---|
| 88 | ROAR_DBG("roar_vio_open_cmd(*): var setup OK"); |
---|
[1255] | 89 | |
---|
[1257] | 90 | if ( !(options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) { |
---|
| 91 | if ( reader != NULL ) |
---|
| 92 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
| 93 | return roar_vio_cmd_close(calls); |
---|
[1255] | 94 | |
---|
[1257] | 95 | if ( writer != NULL ) |
---|
| 96 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
| 97 | return roar_vio_cmd_close(calls); |
---|
| 98 | } |
---|
[1255] | 99 | |
---|
| 100 | return 0; |
---|
[1366] | 101 | #else |
---|
| 102 | return -1; |
---|
| 103 | #endif |
---|
[1255] | 104 | } |
---|
| 105 | |
---|
[1366] | 106 | #ifndef ROAR_WITHOUT_VIO_CMD |
---|
[1255] | 107 | int roar_vio_cmd_close(struct roar_vio_calls * vio) { |
---|
| 108 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
| 109 | |
---|
[1273] | 110 | state->state = ROAR_VIO_CMD_STATE_CLOSING; |
---|
| 111 | |
---|
[1271] | 112 | if ( state->writer.opened ) { |
---|
| 113 | if ( state->writer.out != -1 ) { |
---|
| 114 | close(state->writer.out); |
---|
| 115 | state->writer.out = -1; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | roar_vio_cmd_sync(vio); |
---|
| 120 | |
---|
[1255] | 121 | if ( state->reader.opened ) |
---|
| 122 | roar_vio_cmd_wait(&(state->reader)); |
---|
| 123 | |
---|
| 124 | if ( state->writer.opened ) |
---|
| 125 | roar_vio_cmd_wait(&(state->writer)); |
---|
| 126 | |
---|
| 127 | if ( state->reader.cmd != NULL ) |
---|
[4975] | 128 | roar_mm_free(state->reader.cmd); |
---|
[1255] | 129 | |
---|
| 130 | if ( state->writer.cmd != NULL ) |
---|
[4975] | 131 | roar_mm_free(state->writer.cmd); |
---|
[1255] | 132 | |
---|
| 133 | roar_vio_close(state->next); |
---|
| 134 | |
---|
[1273] | 135 | // state->state = ROAR_VIO_CMD_STATE_CLOSED; |
---|
[3063] | 136 | roar_mm_free(state); |
---|
[1255] | 137 | |
---|
| 138 | return 0; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | int roar_vio_cmd_fork(struct roar_vio_cmd_child * child) { |
---|
[1256] | 142 | int in[2], out[2]; |
---|
| 143 | |
---|
| 144 | if ( child == NULL ) |
---|
| 145 | return -1; |
---|
| 146 | |
---|
| 147 | if ( child->opened ) |
---|
| 148 | return 0; |
---|
| 149 | |
---|
| 150 | if ( child->cmd == NULL ) |
---|
| 151 | return -1; |
---|
| 152 | |
---|
| 153 | // open some pipes... |
---|
| 154 | if ( pipe(in) != 0 ) |
---|
| 155 | return -1; |
---|
| 156 | |
---|
| 157 | if ( pipe(out) != 0 ) { |
---|
| 158 | close(in[0]); |
---|
| 159 | close(in[1]); |
---|
| 160 | return -1; |
---|
| 161 | } |
---|
| 162 | |
---|
[5373] | 163 | child->pid = roar_fork(NULL); |
---|
[1256] | 164 | |
---|
| 165 | switch (child->pid) { |
---|
| 166 | case -1: |
---|
| 167 | close(in[0]); |
---|
| 168 | close(in[1]); |
---|
| 169 | close(out[0]); |
---|
| 170 | close(out[1]); |
---|
| 171 | return -1; |
---|
| 172 | break; |
---|
| 173 | case 0: |
---|
[5619] | 174 | roar_watchdog_stop(); |
---|
| 175 | |
---|
[1256] | 176 | close(in[0]); |
---|
| 177 | close(out[1]); |
---|
| 178 | close(ROAR_STDIN); |
---|
| 179 | close(ROAR_STDOUT); |
---|
| 180 | |
---|
| 181 | if ( dup2(out[0], ROAR_STDIN) == -1 ) |
---|
[5619] | 182 | ROAR_U_EXIT(1); |
---|
[1256] | 183 | |
---|
| 184 | if ( dup2(in[1], ROAR_STDOUT) == -1 ) |
---|
[5619] | 185 | ROAR_U_EXIT(1); |
---|
[1256] | 186 | |
---|
[5364] | 187 | execlp("/bin/sh", "/bin/sh", "-c", child->cmd, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
[1256] | 188 | |
---|
[5619] | 189 | ROAR_U_EXIT(1); |
---|
[1256] | 190 | break; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | close(in[1]); |
---|
| 194 | close(out[0]); |
---|
| 195 | |
---|
| 196 | child->opened = 1; |
---|
| 197 | child->in = in[0]; |
---|
| 198 | child->out = out[1]; |
---|
| 199 | |
---|
| 200 | return 0; |
---|
[1255] | 201 | } |
---|
| 202 | |
---|
| 203 | int roar_vio_cmd_wait(struct roar_vio_cmd_child * child) { |
---|
| 204 | int status; |
---|
| 205 | |
---|
| 206 | if ( child == NULL ) |
---|
| 207 | return -1; |
---|
| 208 | |
---|
| 209 | if ( !child->opened ) |
---|
| 210 | return 0; |
---|
| 211 | |
---|
| 212 | if ( child->out != -1 ) |
---|
| 213 | close(child->out); |
---|
| 214 | |
---|
| 215 | if ( child->in != -1 ) |
---|
| 216 | close(child->in); |
---|
| 217 | |
---|
| 218 | waitpid(child->pid, &status, 0); |
---|
| 219 | |
---|
| 220 | return 0; |
---|
| 221 | } |
---|
| 222 | |
---|
[1657] | 223 | |
---|
| 224 | |
---|
| 225 | int roar_vio_open_2popen(struct roar_vio_calls * calls, char * command, int options) { |
---|
| 226 | #ifndef ROAR_WITHOUT_VIO_CMD |
---|
| 227 | struct roar_vio_2popen_state * state; |
---|
| 228 | |
---|
| 229 | if ( calls == NULL || command == NULL || options < 0 ) |
---|
| 230 | return -1; |
---|
| 231 | |
---|
[3063] | 232 | if ( (state = roar_mm_malloc(sizeof(struct roar_vio_2popen_state))) == NULL ) |
---|
[1657] | 233 | return -1; |
---|
| 234 | |
---|
| 235 | ROAR_DBG("roar_vio_open_2popen(*): pre reqs are OK"); |
---|
| 236 | |
---|
| 237 | // clear all |
---|
| 238 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
[5388] | 239 | calls->flags = ROAR_VIO_FLAGS_NONE; |
---|
| 240 | calls->refc = 1; |
---|
[4617] | 241 | memset(state, 0, sizeof(struct roar_vio_2popen_state)); |
---|
[1657] | 242 | |
---|
| 243 | // init reader and writer: |
---|
| 244 | state->child.pid = -1; |
---|
| 245 | state->child.in = -1; |
---|
| 246 | state->child.out = -1; |
---|
| 247 | |
---|
[4975] | 248 | state->child.cmd = roar_mm_strdup(command); |
---|
[1657] | 249 | |
---|
| 250 | // init state |
---|
| 251 | state->options = options; |
---|
| 252 | state->state = ROAR_VIO_CMD_STATE_OPEN; |
---|
| 253 | |
---|
| 254 | // init calls |
---|
| 255 | calls->close = roar_vio_2popen_close; |
---|
| 256 | /* |
---|
| 257 | calls->read = roar_vio_2popen_read; |
---|
| 258 | calls->write = roar_vio_2popen_write; |
---|
| 259 | calls->sync = roar_vio_2popen_sync; |
---|
| 260 | calls->ctl = roar_vio_2popen_ctl; |
---|
| 261 | */ |
---|
| 262 | calls->inst = (void*) state; |
---|
| 263 | |
---|
| 264 | ROAR_DBG("roar_vio_open_2popen(*): var setup OK"); |
---|
| 265 | |
---|
| 266 | if ( !(options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) { |
---|
| 267 | if ( roar_vio_cmd_fork(&(state->child)) == -1 ) |
---|
| 268 | return roar_vio_2popen_close(calls); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | return 0; |
---|
| 272 | #else |
---|
| 273 | return -1; |
---|
| 274 | #endif |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | #ifndef ROAR_WITHOUT_VIO_CMD |
---|
| 278 | int roar_vio_2popen_close(struct roar_vio_calls * vio) { |
---|
| 279 | struct roar_vio_2popen_state * state = (struct roar_vio_2popen_state *)vio->inst; |
---|
| 280 | |
---|
| 281 | state->state = ROAR_VIO_CMD_STATE_CLOSING; |
---|
| 282 | |
---|
| 283 | if ( state->child.opened ) |
---|
| 284 | roar_vio_cmd_wait(&(state->child)); |
---|
| 285 | |
---|
| 286 | if ( state->child.cmd != NULL ) |
---|
[4975] | 287 | roar_mm_free(state->child.cmd); |
---|
[1657] | 288 | |
---|
[3063] | 289 | roar_mm_free(state); |
---|
[1657] | 290 | |
---|
| 291 | return 0; |
---|
| 292 | } |
---|
| 293 | #endif |
---|
| 294 | |
---|
| 295 | |
---|
[1256] | 296 | // VIOs: |
---|
| 297 | |
---|
| 298 | ssize_t roar_vio_cmd_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 299 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
[1257] | 300 | fd_set rfhs[1], wfhs[1]; |
---|
| 301 | struct timeval tv; |
---|
| 302 | size_t done = 0; |
---|
| 303 | int max_fh; |
---|
| 304 | int ret; |
---|
[5357] | 305 | char tbuf[ROAR_VIO_CMD_BUFSIZE]; |
---|
| 306 | char * tp = NULL; |
---|
| 307 | ssize_t tlen = 0; |
---|
| 308 | int nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 309 | int in, out; |
---|
| 310 | int eating = 1; |
---|
[1256] | 311 | |
---|
[1260] | 312 | ROAR_DBG("roar_vio_cmd_read(*) = ?"); |
---|
| 313 | |
---|
[1256] | 314 | if ( !state->reader.opened ) { |
---|
| 315 | if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no reader is forked :) */ |
---|
| 316 | return 0; |
---|
| 317 | |
---|
| 318 | if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no reader exists? -> err */ |
---|
| 319 | return -1; |
---|
| 320 | |
---|
| 321 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
| 322 | return -1; |
---|
| 323 | } |
---|
| 324 | |
---|
[1271] | 325 | in = state->reader.in; |
---|
| 326 | out = state->reader.out; |
---|
| 327 | |
---|
[1612] | 328 | while (done < count || eating) { |
---|
[1271] | 329 | if ( nonblock ) { |
---|
[1257] | 330 | tv.tv_sec = 0; |
---|
| 331 | tv.tv_usec = 1; |
---|
| 332 | } else { |
---|
| 333 | tv.tv_sec = 3600; |
---|
| 334 | tv.tv_usec = 0; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | FD_ZERO(rfhs); |
---|
| 338 | FD_ZERO(wfhs); |
---|
| 339 | |
---|
[1271] | 340 | FD_SET(in, rfhs); |
---|
[1260] | 341 | |
---|
[1271] | 342 | if ( out != -1 ) { |
---|
| 343 | FD_SET(out, wfhs); |
---|
[1260] | 344 | } |
---|
| 345 | |
---|
| 346 | #ifdef DEBUG |
---|
[1271] | 347 | if ( FD_ISSET(in, rfhs) ) { |
---|
[1260] | 348 | ROAR_DBG("roar_vio_cmd_read(*): reader set in fh group"); |
---|
| 349 | } |
---|
| 350 | #endif |
---|
[1257] | 351 | |
---|
[1271] | 352 | max_fh = in > out ? in : out; |
---|
[1260] | 353 | ROAR_DBG("roar_vio_cmd_read(*): max_fh=%i", max_fh); |
---|
[1257] | 354 | |
---|
[1260] | 355 | if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 ) |
---|
[1257] | 356 | return -1; |
---|
| 357 | |
---|
[1260] | 358 | ROAR_DBG("roar_vio_cmd_read(*): select(*) = %i", ret); |
---|
[1271] | 359 | ROAR_DBG("roar_vio_cmd_read(*): reader=%i, writer=%i", in, out); |
---|
[1260] | 360 | |
---|
[1257] | 361 | if ( ret > 0 ) { |
---|
[1271] | 362 | if ( FD_ISSET(in, rfhs) ) { |
---|
[1260] | 363 | ROAR_DBG("roar_vio_cmd_read(*): event on reader"); |
---|
| 364 | |
---|
[1271] | 365 | if ( (ret = read(in, buf+done, count-done)) == -1 ) |
---|
[1257] | 366 | break; |
---|
| 367 | |
---|
| 368 | if ( ret == 0 ) |
---|
| 369 | break; |
---|
| 370 | |
---|
| 371 | done += ret; |
---|
| 372 | } |
---|
| 373 | |
---|
[1271] | 374 | if ( out != -1 && FD_ISSET(out, wfhs) ) { |
---|
[1260] | 375 | ROAR_DBG("roar_vio_cmd_read(*): event on writer"); |
---|
[1612] | 376 | eating = 1; |
---|
[1260] | 377 | |
---|
[1257] | 378 | if ( !tlen ) { |
---|
[1260] | 379 | tp = tbuf; |
---|
| 380 | tlen = 0; |
---|
[1257] | 381 | if ( (tlen = roar_vio_read(state->next, tp, ROAR_VIO_CMD_BUFSIZE)) == -1 ) |
---|
[1260] | 382 | continue; |
---|
[1257] | 383 | } |
---|
| 384 | |
---|
| 385 | if ( tlen ) { |
---|
[1271] | 386 | if ( (ret = write(out, tp, tlen)) > 0 ) { |
---|
[1257] | 387 | tlen -= ret; |
---|
| 388 | tp += ret; |
---|
| 389 | } |
---|
[1260] | 390 | } else { |
---|
[1271] | 391 | close(out); |
---|
| 392 | state->reader.out = out = -1; |
---|
[1257] | 393 | } |
---|
[1612] | 394 | } else { |
---|
| 395 | eating = 0; |
---|
[1257] | 396 | } |
---|
| 397 | } |
---|
| 398 | |
---|
[1271] | 399 | if ( nonblock ) |
---|
[1257] | 400 | break; |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | if ( tlen ) { /* we have some data to write to the child... */ |
---|
| 404 | // TODO: try to write it out... |
---|
| 405 | return -1; |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | return done; |
---|
[1256] | 409 | } |
---|
| 410 | |
---|
| 411 | ssize_t roar_vio_cmd_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 412 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
[1271] | 413 | fd_set rfhs[1], wfhs[1]; |
---|
| 414 | struct timeval tv; |
---|
| 415 | size_t done = 0; |
---|
| 416 | int max_fh; |
---|
| 417 | int ret; |
---|
| 418 | char tbuf[ROAR_VIO_CMD_BUFSIZE]; |
---|
| 419 | int nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 420 | int in, out; |
---|
[1256] | 421 | |
---|
| 422 | if ( !state->writer.opened ) { |
---|
| 423 | if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no writer is forked :) */ |
---|
| 424 | return 0; |
---|
| 425 | |
---|
| 426 | if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no writer exists? -> err */ |
---|
| 427 | return -1; |
---|
| 428 | |
---|
| 429 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
| 430 | return -1; |
---|
| 431 | } |
---|
| 432 | |
---|
[1271] | 433 | in = state->writer.in; |
---|
| 434 | out = state->writer.out; |
---|
| 435 | |
---|
| 436 | if ( buf == NULL ) { // we are requested to sync |
---|
| 437 | if ( in != -1 ) { |
---|
| 438 | ret = 1; |
---|
| 439 | done = 0; |
---|
| 440 | while (ret > 0) { |
---|
[1273] | 441 | if ( state->state == ROAR_VIO_CMD_STATE_CLOSING ) { |
---|
| 442 | tv.tv_sec = 3600; |
---|
| 443 | tv.tv_usec = 0; |
---|
| 444 | } else { |
---|
| 445 | tv.tv_sec = 0; |
---|
| 446 | tv.tv_usec = done ? 1 : 50000; // 50ms |
---|
| 447 | } |
---|
[1271] | 448 | |
---|
| 449 | done++; |
---|
| 450 | |
---|
| 451 | FD_ZERO(rfhs); |
---|
| 452 | FD_SET(in, rfhs); |
---|
| 453 | |
---|
| 454 | if ( select(in+1, rfhs, NULL, NULL, &tv) < 1 ) |
---|
| 455 | break; |
---|
| 456 | |
---|
| 457 | ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE); |
---|
| 458 | |
---|
| 459 | if ( roar_vio_write(state->next, tbuf, ret) != ret ) |
---|
| 460 | return -1; |
---|
| 461 | } |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | return 0; |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | while (done < count) { |
---|
| 468 | if ( nonblock ) { |
---|
| 469 | tv.tv_sec = 0; |
---|
| 470 | tv.tv_usec = 1; |
---|
| 471 | } else { |
---|
| 472 | tv.tv_sec = 3600; |
---|
| 473 | tv.tv_usec = 0; |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | FD_ZERO(rfhs); |
---|
| 477 | FD_ZERO(wfhs); |
---|
| 478 | |
---|
| 479 | FD_SET(out, wfhs); |
---|
| 480 | |
---|
| 481 | if ( in != -1 ) { |
---|
| 482 | FD_SET(in, rfhs); |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | max_fh = in > out ? in : out; |
---|
| 486 | |
---|
| 487 | if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 ) |
---|
| 488 | return -1; |
---|
| 489 | |
---|
| 490 | if ( ret > 0 ) { |
---|
| 491 | if ( FD_ISSET(out, wfhs) ) { |
---|
| 492 | |
---|
| 493 | if ( (ret = write(out, buf+done, count-done)) == -1 ) |
---|
| 494 | break; |
---|
| 495 | |
---|
| 496 | if ( ret == 0 ) |
---|
| 497 | break; |
---|
| 498 | |
---|
| 499 | done += ret; |
---|
| 500 | } |
---|
[1663] | 501 | if ( in != -1 && FD_ISSET(in, rfhs) ) { |
---|
[1271] | 502 | if ( (ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE)) == -1 ) { /* error case: can not read on reader -> EOF */ |
---|
| 503 | close(in); |
---|
| 504 | state->writer.in = in = -1; |
---|
| 505 | break; |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | if ( roar_vio_write(state->next, tbuf, ret) != ret ) |
---|
| 509 | return -1; |
---|
| 510 | } |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | if ( nonblock ) |
---|
| 514 | break; |
---|
| 515 | } |
---|
[1256] | 516 | |
---|
[1663] | 517 | return done; |
---|
[1256] | 518 | } |
---|
| 519 | |
---|
| 520 | int roar_vio_cmd_sync (struct roar_vio_calls * vio) { |
---|
| 521 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
[5276] | 522 | int newblock = ROAR_SOCKET_BLOCK; |
---|
[1256] | 523 | int oldblock; |
---|
| 524 | int ret = 0; |
---|
| 525 | |
---|
| 526 | oldblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK ? ROAR_SOCKET_NONBLOCK : ROAR_SOCKET_BLOCK; |
---|
| 527 | |
---|
[5276] | 528 | if ( roar_vio_cmd_ctl(vio, ROAR_VIO_CTL_NONBLOCK, &newblock) == -1 ) |
---|
[1256] | 529 | return -1; |
---|
| 530 | |
---|
| 531 | if ( roar_vio_cmd_write(vio, NULL, 0) == -1 ) |
---|
| 532 | ret = -1; |
---|
| 533 | |
---|
| 534 | if ( roar_vio_cmd_read(vio, NULL, 0) == -1 ) |
---|
| 535 | ret = -1; |
---|
| 536 | |
---|
[5276] | 537 | if ( roar_vio_cmd_ctl(vio, ROAR_VIO_CTL_NONBLOCK, &oldblock) == -1 ) |
---|
[1256] | 538 | return -1; |
---|
| 539 | |
---|
| 540 | return ret; |
---|
| 541 | } |
---|
[1612] | 542 | |
---|
[5278] | 543 | int roar_vio_cmd_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
[1612] | 544 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
| 545 | char buf[1]; |
---|
| 546 | |
---|
[5405] | 547 | ROAR_DBG("roar_vio_cmd_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data); |
---|
[1612] | 548 | |
---|
| 549 | switch (cmd) { |
---|
[3795] | 550 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 551 | if ( data == NULL ) |
---|
| 552 | return -1; |
---|
| 553 | |
---|
| 554 | *(char**)data = "cmd"; |
---|
| 555 | return 0; |
---|
| 556 | break; |
---|
[1612] | 557 | case ROAR_VIO_CTL_GET_NEXT: |
---|
| 558 | *(struct roar_vio_calls **)data = state->next; |
---|
| 559 | return -1; |
---|
| 560 | break; |
---|
| 561 | case ROAR_VIO_CTL_GET_FH: |
---|
| 562 | return -1; |
---|
| 563 | break; |
---|
| 564 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
[3437] | 565 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
[1612] | 566 | // if ( !state->reader.opened ) { |
---|
| 567 | //for (i = 0; i < 128; i++) |
---|
| 568 | roar_vio_cmd_read(vio, buf, 0); |
---|
| 569 | // } |
---|
| 570 | if ( state->reader.opened ) { |
---|
[5405] | 571 | ROAR_DBG("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); |
---|
[1612] | 572 | *(int*)data = state->reader.in; |
---|
| 573 | return 0; |
---|
| 574 | } |
---|
| 575 | return -1; |
---|
| 576 | break; |
---|
| 577 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
| 578 | return -1; |
---|
| 579 | break; |
---|
[5276] | 580 | case ROAR_VIO_CTL_NONBLOCK: |
---|
| 581 | state->options |= ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 582 | |
---|
| 583 | if ( *(int*)data == ROAR_SOCKET_BLOCK ) |
---|
| 584 | state->options -= ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 585 | |
---|
| 586 | _LIBROAR_IGNORE_RET(roar_vio_ctl(state->next, cmd, data)); // this should help, but may not necessarily. |
---|
| 587 | break; |
---|
[1612] | 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 |
---|