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