source: roaraudio/libroar/vio_cmd.c @ 1652:940ffafab455

Last change on this file since 1652:940ffafab455 was 1652:940ffafab455, checked in by phi, 15 years ago

do a better casting on exec()s

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