source: roaraudio/libroar/vio_cmd.c @ 1271:7ff82047e706

Last change on this file since 1271:7ff82047e706 was 1271:7ff82047e706, checked in by phi, 15 years ago

wrote a working writer, done things for the sync() code, still need to do some work

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