[1255] | 1 | //vio_cmd.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[1284] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
[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 |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #include "libroar.h" |
---|
| 36 | |
---|
| 37 | int roar_vio_open_cmd(struct roar_vio_calls * calls, struct roar_vio_calls * dst, |
---|
[1261] | 38 | char * reader, char * writer, int options) { |
---|
[1255] | 39 | struct roar_vio_cmd_state * state; |
---|
| 40 | |
---|
| 41 | if ( calls == NULL || dst == NULL ) |
---|
| 42 | return -1; |
---|
| 43 | |
---|
| 44 | if ( reader == NULL && writer == NULL ) |
---|
| 45 | return -1; |
---|
| 46 | |
---|
| 47 | if ( (state = malloc(sizeof(struct roar_vio_cmd_state))) == NULL ) |
---|
| 48 | return -1; |
---|
| 49 | |
---|
[1260] | 50 | ROAR_DBG("roar_vio_open_cmd(*): pre reqs are OK"); |
---|
| 51 | |
---|
[1255] | 52 | // clear all |
---|
| 53 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 54 | memset(state, 0, sizeof(struct roar_vio_cmd_state)); |
---|
| 55 | |
---|
| 56 | // init reader and writer: |
---|
| 57 | state->reader.pid = -1; |
---|
| 58 | state->reader.in = -1; |
---|
| 59 | state->reader.out = -1; |
---|
| 60 | |
---|
| 61 | if ( reader != NULL ) |
---|
| 62 | state->reader.cmd = strdup(reader); |
---|
| 63 | |
---|
| 64 | state->writer.pid = -1; |
---|
| 65 | state->writer.in = -1; |
---|
| 66 | state->writer.out = -1; |
---|
| 67 | |
---|
| 68 | if ( writer != NULL ) |
---|
| 69 | state->writer.cmd = strdup(writer); |
---|
| 70 | |
---|
| 71 | // init state |
---|
| 72 | state->next = dst; |
---|
| 73 | state->options = options; |
---|
[1273] | 74 | state->state = ROAR_VIO_CMD_STATE_OPEN; |
---|
[1255] | 75 | |
---|
| 76 | // init calls |
---|
[1260] | 77 | calls->close = roar_vio_cmd_close; |
---|
| 78 | calls->read = roar_vio_cmd_read; |
---|
| 79 | calls->write = roar_vio_cmd_write; |
---|
| 80 | calls->nonblock = roar_vio_cmd_nonblock; |
---|
| 81 | calls->sync = roar_vio_cmd_sync; |
---|
| 82 | calls->inst = (void*) state; |
---|
| 83 | |
---|
| 84 | ROAR_DBG("roar_vio_open_cmd(*): var setup OK"); |
---|
[1255] | 85 | |
---|
[1257] | 86 | if ( !(options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) { |
---|
| 87 | if ( reader != NULL ) |
---|
| 88 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
| 89 | return roar_vio_cmd_close(calls); |
---|
[1255] | 90 | |
---|
[1257] | 91 | if ( writer != NULL ) |
---|
| 92 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
| 93 | return roar_vio_cmd_close(calls); |
---|
| 94 | } |
---|
[1255] | 95 | |
---|
| 96 | return 0; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | int roar_vio_cmd_close(struct roar_vio_calls * vio) { |
---|
| 100 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
| 101 | |
---|
[1273] | 102 | state->state = ROAR_VIO_CMD_STATE_CLOSING; |
---|
| 103 | |
---|
[1271] | 104 | if ( state->writer.opened ) { |
---|
| 105 | if ( state->writer.out != -1 ) { |
---|
| 106 | close(state->writer.out); |
---|
| 107 | state->writer.out = -1; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | roar_vio_cmd_sync(vio); |
---|
| 112 | |
---|
[1255] | 113 | if ( state->reader.opened ) |
---|
| 114 | roar_vio_cmd_wait(&(state->reader)); |
---|
| 115 | |
---|
| 116 | if ( state->writer.opened ) |
---|
| 117 | roar_vio_cmd_wait(&(state->writer)); |
---|
| 118 | |
---|
| 119 | if ( state->reader.cmd != NULL ) |
---|
| 120 | free(state->reader.cmd); |
---|
| 121 | |
---|
| 122 | if ( state->writer.cmd != NULL ) |
---|
| 123 | free(state->writer.cmd); |
---|
| 124 | |
---|
| 125 | roar_vio_close(state->next); |
---|
| 126 | |
---|
[1273] | 127 | // state->state = ROAR_VIO_CMD_STATE_CLOSED; |
---|
[1255] | 128 | free(state); |
---|
| 129 | |
---|
| 130 | return 0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | int roar_vio_cmd_fork(struct roar_vio_cmd_child * child) { |
---|
[1256] | 134 | int in[2], out[2]; |
---|
| 135 | |
---|
| 136 | if ( child == NULL ) |
---|
| 137 | return -1; |
---|
| 138 | |
---|
| 139 | if ( child->opened ) |
---|
| 140 | return 0; |
---|
| 141 | |
---|
| 142 | if ( child->cmd == NULL ) |
---|
| 143 | return -1; |
---|
| 144 | |
---|
| 145 | // open some pipes... |
---|
| 146 | if ( pipe(in) != 0 ) |
---|
| 147 | return -1; |
---|
| 148 | |
---|
| 149 | if ( pipe(out) != 0 ) { |
---|
| 150 | close(in[0]); |
---|
| 151 | close(in[1]); |
---|
| 152 | return -1; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | child->pid = fork(); |
---|
| 156 | |
---|
| 157 | switch (child->pid) { |
---|
| 158 | case -1: |
---|
| 159 | close(in[0]); |
---|
| 160 | close(in[1]); |
---|
| 161 | close(out[0]); |
---|
| 162 | close(out[1]); |
---|
| 163 | return -1; |
---|
| 164 | break; |
---|
| 165 | case 0: |
---|
| 166 | close(in[0]); |
---|
| 167 | close(out[1]); |
---|
| 168 | close(ROAR_STDIN); |
---|
| 169 | close(ROAR_STDOUT); |
---|
| 170 | |
---|
| 171 | if ( dup2(out[0], ROAR_STDIN) == -1 ) |
---|
| 172 | _exit(1); |
---|
| 173 | |
---|
| 174 | if ( dup2(in[1], ROAR_STDOUT) == -1 ) |
---|
| 175 | _exit(1); |
---|
| 176 | |
---|
| 177 | execlp("/bin/sh", "/bin/sh", "-c", child->cmd, NULL); |
---|
| 178 | |
---|
| 179 | _exit(1); |
---|
| 180 | break; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | close(in[1]); |
---|
| 184 | close(out[0]); |
---|
| 185 | |
---|
| 186 | child->opened = 1; |
---|
| 187 | child->in = in[0]; |
---|
| 188 | child->out = out[1]; |
---|
| 189 | |
---|
| 190 | return 0; |
---|
[1255] | 191 | } |
---|
| 192 | |
---|
| 193 | int roar_vio_cmd_wait(struct roar_vio_cmd_child * child) { |
---|
| 194 | int status; |
---|
| 195 | |
---|
| 196 | if ( child == NULL ) |
---|
| 197 | return -1; |
---|
| 198 | |
---|
| 199 | if ( !child->opened ) |
---|
| 200 | return 0; |
---|
| 201 | |
---|
| 202 | if ( child->out != -1 ) |
---|
| 203 | close(child->out); |
---|
| 204 | |
---|
| 205 | if ( child->in != -1 ) |
---|
| 206 | close(child->in); |
---|
| 207 | |
---|
| 208 | waitpid(child->pid, &status, 0); |
---|
| 209 | |
---|
| 210 | return 0; |
---|
| 211 | } |
---|
| 212 | |
---|
[1256] | 213 | // VIOs: |
---|
| 214 | |
---|
| 215 | ssize_t roar_vio_cmd_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 216 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
[1257] | 217 | fd_set rfhs[1], wfhs[1]; |
---|
| 218 | struct timeval tv; |
---|
| 219 | size_t done = 0; |
---|
| 220 | int max_fh; |
---|
| 221 | int ret; |
---|
| 222 | char tbuf[ROAR_VIO_CMD_BUFSIZE]; |
---|
| 223 | char * tp = NULL; |
---|
| 224 | size_t tlen = 0; |
---|
[1271] | 225 | int nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 226 | int in, out; |
---|
[1256] | 227 | |
---|
[1260] | 228 | ROAR_DBG("roar_vio_cmd_read(*) = ?"); |
---|
| 229 | |
---|
[1256] | 230 | if ( !state->reader.opened ) { |
---|
| 231 | if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no reader is forked :) */ |
---|
| 232 | return 0; |
---|
| 233 | |
---|
| 234 | if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no reader exists? -> err */ |
---|
| 235 | return -1; |
---|
| 236 | |
---|
| 237 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
| 238 | return -1; |
---|
| 239 | } |
---|
| 240 | |
---|
[1271] | 241 | in = state->reader.in; |
---|
| 242 | out = state->reader.out; |
---|
| 243 | |
---|
[1257] | 244 | while (done < count) { |
---|
[1271] | 245 | if ( nonblock ) { |
---|
[1257] | 246 | tv.tv_sec = 0; |
---|
| 247 | tv.tv_usec = 1; |
---|
| 248 | } else { |
---|
| 249 | tv.tv_sec = 3600; |
---|
| 250 | tv.tv_usec = 0; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | FD_ZERO(rfhs); |
---|
| 254 | FD_ZERO(wfhs); |
---|
| 255 | |
---|
[1271] | 256 | FD_SET(in, rfhs); |
---|
[1260] | 257 | |
---|
[1271] | 258 | if ( out != -1 ) { |
---|
| 259 | FD_SET(out, wfhs); |
---|
[1260] | 260 | } |
---|
| 261 | |
---|
| 262 | #ifdef DEBUG |
---|
[1271] | 263 | if ( FD_ISSET(in, rfhs) ) { |
---|
[1260] | 264 | ROAR_DBG("roar_vio_cmd_read(*): reader set in fh group"); |
---|
| 265 | } |
---|
| 266 | #endif |
---|
[1257] | 267 | |
---|
[1271] | 268 | max_fh = in > out ? in : out; |
---|
[1260] | 269 | ROAR_DBG("roar_vio_cmd_read(*): max_fh=%i", max_fh); |
---|
[1257] | 270 | |
---|
[1260] | 271 | if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 ) |
---|
[1257] | 272 | return -1; |
---|
| 273 | |
---|
[1260] | 274 | ROAR_DBG("roar_vio_cmd_read(*): select(*) = %i", ret); |
---|
[1271] | 275 | ROAR_DBG("roar_vio_cmd_read(*): reader=%i, writer=%i", in, out); |
---|
[1260] | 276 | |
---|
[1257] | 277 | if ( ret > 0 ) { |
---|
[1271] | 278 | if ( FD_ISSET(in, rfhs) ) { |
---|
[1260] | 279 | ROAR_DBG("roar_vio_cmd_read(*): event on reader"); |
---|
| 280 | |
---|
[1271] | 281 | if ( (ret = read(in, buf+done, count-done)) == -1 ) |
---|
[1257] | 282 | break; |
---|
| 283 | |
---|
| 284 | if ( ret == 0 ) |
---|
| 285 | break; |
---|
| 286 | |
---|
| 287 | done += ret; |
---|
| 288 | } |
---|
| 289 | |
---|
[1271] | 290 | if ( out != -1 && FD_ISSET(out, wfhs) ) { |
---|
[1260] | 291 | ROAR_DBG("roar_vio_cmd_read(*): event on writer"); |
---|
| 292 | |
---|
[1257] | 293 | if ( !tlen ) { |
---|
[1260] | 294 | tp = tbuf; |
---|
| 295 | tlen = 0; |
---|
[1257] | 296 | if ( (tlen = roar_vio_read(state->next, tp, ROAR_VIO_CMD_BUFSIZE)) == -1 ) |
---|
[1260] | 297 | continue; |
---|
[1257] | 298 | } |
---|
| 299 | |
---|
| 300 | if ( tlen ) { |
---|
[1271] | 301 | if ( (ret = write(out, tp, tlen)) > 0 ) { |
---|
[1257] | 302 | tlen -= ret; |
---|
| 303 | tp += ret; |
---|
| 304 | } |
---|
[1260] | 305 | } else { |
---|
[1271] | 306 | close(out); |
---|
| 307 | state->reader.out = out = -1; |
---|
[1257] | 308 | } |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
[1271] | 312 | if ( nonblock ) |
---|
[1257] | 313 | break; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | if ( tlen ) { /* we have some data to write to the child... */ |
---|
| 317 | // TODO: try to write it out... |
---|
| 318 | return -1; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | return done; |
---|
[1256] | 322 | } |
---|
| 323 | |
---|
| 324 | ssize_t roar_vio_cmd_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 325 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
[1271] | 326 | fd_set rfhs[1], wfhs[1]; |
---|
| 327 | struct timeval tv; |
---|
| 328 | size_t done = 0; |
---|
| 329 | int max_fh; |
---|
| 330 | int ret; |
---|
| 331 | char tbuf[ROAR_VIO_CMD_BUFSIZE]; |
---|
| 332 | int nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 333 | int in, out; |
---|
[1256] | 334 | |
---|
| 335 | if ( !state->writer.opened ) { |
---|
| 336 | if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no writer is forked :) */ |
---|
| 337 | return 0; |
---|
| 338 | |
---|
| 339 | if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no writer exists? -> err */ |
---|
| 340 | return -1; |
---|
| 341 | |
---|
| 342 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
| 343 | return -1; |
---|
| 344 | } |
---|
| 345 | |
---|
[1271] | 346 | in = state->writer.in; |
---|
| 347 | out = state->writer.out; |
---|
| 348 | |
---|
| 349 | if ( buf == NULL ) { // we are requested to sync |
---|
| 350 | if ( in != -1 ) { |
---|
| 351 | ret = 1; |
---|
| 352 | done = 0; |
---|
| 353 | while (ret > 0) { |
---|
[1273] | 354 | if ( state->state == ROAR_VIO_CMD_STATE_CLOSING ) { |
---|
| 355 | tv.tv_sec = 3600; |
---|
| 356 | tv.tv_usec = 0; |
---|
| 357 | } else { |
---|
| 358 | tv.tv_sec = 0; |
---|
| 359 | tv.tv_usec = done ? 1 : 50000; // 50ms |
---|
| 360 | } |
---|
[1271] | 361 | |
---|
| 362 | done++; |
---|
| 363 | |
---|
| 364 | FD_ZERO(rfhs); |
---|
| 365 | FD_SET(in, rfhs); |
---|
| 366 | |
---|
| 367 | if ( select(in+1, rfhs, NULL, NULL, &tv) < 1 ) |
---|
| 368 | break; |
---|
| 369 | |
---|
| 370 | ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE); |
---|
| 371 | |
---|
| 372 | if ( roar_vio_write(state->next, tbuf, ret) != ret ) |
---|
| 373 | return -1; |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | return 0; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | while (done < count) { |
---|
| 381 | if ( nonblock ) { |
---|
| 382 | tv.tv_sec = 0; |
---|
| 383 | tv.tv_usec = 1; |
---|
| 384 | } else { |
---|
| 385 | tv.tv_sec = 3600; |
---|
| 386 | tv.tv_usec = 0; |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | FD_ZERO(rfhs); |
---|
| 390 | FD_ZERO(wfhs); |
---|
| 391 | |
---|
| 392 | FD_SET(out, wfhs); |
---|
| 393 | |
---|
| 394 | if ( in != -1 ) { |
---|
| 395 | FD_SET(in, rfhs); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | max_fh = in > out ? in : out; |
---|
| 399 | |
---|
| 400 | if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 ) |
---|
| 401 | return -1; |
---|
| 402 | |
---|
| 403 | if ( ret > 0 ) { |
---|
| 404 | if ( FD_ISSET(out, wfhs) ) { |
---|
| 405 | |
---|
| 406 | if ( (ret = write(out, buf+done, count-done)) == -1 ) |
---|
| 407 | break; |
---|
| 408 | |
---|
| 409 | if ( ret == 0 ) |
---|
| 410 | break; |
---|
| 411 | |
---|
| 412 | done += ret; |
---|
| 413 | } |
---|
| 414 | if ( in != -1 && FD_ISSET(in, wfhs) ) { |
---|
| 415 | if ( (ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE)) == -1 ) { /* error case: can not read on reader -> EOF */ |
---|
| 416 | close(in); |
---|
| 417 | state->writer.in = in = -1; |
---|
| 418 | break; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | if ( roar_vio_write(state->next, tbuf, ret) != ret ) |
---|
| 422 | return -1; |
---|
| 423 | } |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | if ( nonblock ) |
---|
| 427 | break; |
---|
| 428 | } |
---|
[1256] | 429 | |
---|
| 430 | return -1; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | int roar_vio_cmd_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 434 | struct roar_vio_cmd_state * self = (struct roar_vio_cmd_state *)vio->inst; |
---|
| 435 | |
---|
| 436 | self->options |= ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 437 | |
---|
| 438 | if ( state == ROAR_SOCKET_BLOCK ) |
---|
| 439 | self->options -= ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
| 440 | |
---|
[1257] | 441 | roar_vio_nonblock(self->next, state); // this should help, but may not nessessery. |
---|
| 442 | |
---|
[1256] | 443 | return 0; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | int roar_vio_cmd_sync (struct roar_vio_calls * vio) { |
---|
| 447 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
| 448 | int oldblock; |
---|
| 449 | int ret = 0; |
---|
| 450 | |
---|
| 451 | oldblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK ? ROAR_SOCKET_NONBLOCK : ROAR_SOCKET_BLOCK; |
---|
| 452 | |
---|
| 453 | if ( roar_vio_cmd_nonblock(vio, ROAR_SOCKET_BLOCK) == -1 ) |
---|
| 454 | return -1; |
---|
| 455 | |
---|
| 456 | if ( roar_vio_cmd_write(vio, NULL, 0) == -1 ) |
---|
| 457 | ret = -1; |
---|
| 458 | |
---|
| 459 | if ( roar_vio_cmd_read(vio, NULL, 0) == -1 ) |
---|
| 460 | ret = -1; |
---|
| 461 | |
---|
| 462 | if ( roar_vio_cmd_nonblock(vio, oldblock) == -1 ) |
---|
| 463 | return -1; |
---|
| 464 | |
---|
| 465 | return ret; |
---|
| 466 | } |
---|
| 467 | |
---|
[1264] | 468 | // MISC: |
---|
| 469 | int roar_vio_open_gzip(struct roar_vio_calls * calls, struct roar_vio_calls * dst, int level) { |
---|
| 470 | #ifdef ROAR_HAVE_BIN_GZIP |
---|
| 471 | char wbuf[80]; |
---|
| 472 | char * writer = ROAR_HAVE_BIN_GZIP " -c"; |
---|
| 473 | |
---|
| 474 | if ( level != -1 ) { |
---|
| 475 | snprintf(wbuf, 80, "%s -c%i", ROAR_HAVE_BIN_GZIP, level); |
---|
| 476 | writer = wbuf; |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | return roar_vio_open_cmd(calls, dst, ROAR_HAVE_BIN_GZIP " -dc", writer, ROAR_VIO_CMD_OPTS_ON_DEMAND); |
---|
| 480 | #else |
---|
| 481 | return -1; |
---|
| 482 | #endif |
---|
| 483 | } |
---|
| 484 | |
---|
[1269] | 485 | int roar_vio_open_gpg(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int wronly, char * opts, int options) { |
---|
[1266] | 486 | #ifdef ROAR_HAVE_BIN_GPG |
---|
| 487 | char command[1024]; |
---|
[1269] | 488 | char para[1024] = {0}; |
---|
[1266] | 489 | int pwpipe[2]; |
---|
| 490 | int ret; |
---|
| 491 | |
---|
[1269] | 492 | /* |
---|
| 493 | #define ROAR_VIO_PGP_OPTS_NONE 0x00 |
---|
| 494 | #define ROAR_VIO_PGP_OPTS_ASCII 0x01 |
---|
| 495 | #define ROAR_VIO_PGP_OPTS_SIGN 0x02 |
---|
| 496 | #define ROAR_VIO_PGP_OPTS_TEXTMODE 0x04 |
---|
| 497 | */ |
---|
| 498 | |
---|
| 499 | if ( options & ROAR_VIO_PGP_OPTS_ASCII ) |
---|
| 500 | strncat(para, "--armor ", 16); |
---|
| 501 | |
---|
| 502 | if ( options & ROAR_VIO_PGP_OPTS_SIGN ) |
---|
| 503 | strncat(para, "--sign ", 16); |
---|
| 504 | |
---|
| 505 | if ( options & ROAR_VIO_PGP_OPTS_TEXTMODE ) |
---|
| 506 | strncat(para, "--textmode ", 16); |
---|
| 507 | |
---|
[1266] | 508 | if ( pw != NULL ) { |
---|
| 509 | if ( pipe(pwpipe) == -1 ) |
---|
| 510 | return -1; |
---|
| 511 | |
---|
[1269] | 512 | 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] | 513 | |
---|
| 514 | write(pwpipe[1], pw, strlen(pw)); |
---|
| 515 | |
---|
| 516 | close(pwpipe[1]); |
---|
| 517 | } else { |
---|
[1278] | 518 | snprintf(command, 1024, "%s --no-verbose --quiet %s %s", ROAR_HAVE_BIN_GPG, para, opts); |
---|
[1266] | 519 | } |
---|
| 520 | |
---|
| 521 | if ( wronly ) { |
---|
| 522 | ret = roar_vio_open_cmd(calls, dst, NULL, command, 0); |
---|
| 523 | } else { |
---|
| 524 | ret = roar_vio_open_cmd(calls, dst, command, NULL, 0); |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | if ( pw != NULL ) |
---|
| 528 | close(pwpipe[0]); |
---|
| 529 | |
---|
| 530 | return ret; |
---|
| 531 | #else |
---|
| 532 | return -1; |
---|
| 533 | #endif |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | int roar_vio_open_pgp_decrypt(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw) { |
---|
[1269] | 537 | return roar_vio_open_gpg(calls, dst, pw, 0, "-d", ROAR_VIO_PGP_OPTS_NONE); |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | int roar_vio_open_pgp_store(struct roar_vio_calls * calls, struct roar_vio_calls * dst, int options) { |
---|
| 541 | return roar_vio_open_gpg(calls, dst, NULL, 1, "--store", options); |
---|
[1266] | 542 | } |
---|
| 543 | |
---|
[1269] | 544 | int roar_vio_open_pgp_encrypt_sym(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int options) { |
---|
| 545 | return roar_vio_open_gpg(calls, dst, pw, 1, "--symmetric", options); |
---|
| 546 | } |
---|
| 547 | int roar_vio_open_pgp_encrypt_pub(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int options, char * recipient) { |
---|
| 548 | char buf[1024]; |
---|
| 549 | |
---|
| 550 | snprintf(buf, 1024, "-e -r %s", recipient); |
---|
| 551 | return roar_vio_open_gpg(calls, dst, pw, 1, buf, options); |
---|
| 552 | } |
---|
[1266] | 553 | |
---|
[1255] | 554 | //ll |
---|