source: roaraudio/libroar/vio_cmd.c @ 1366:2dc3d99579da

Last change on this file since 1366:2dc3d99579da was 1366:2dc3d99579da, checked in by phi, 15 years ago

make vio cmd optional

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