source: roaraudio/libroar/vio_cmd.c @ 3437:4289f31e687f

Last change on this file since 3437:4289f31e687f was 3437:4289f31e687f, checked in by phi, 14 years ago

added support for ROAR_VIO_CTL_GET_SELECT_*FH to some less basic objects

File size: 15.4 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 = roar_mm_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 roar_mm_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
220
221int roar_vio_open_2popen(struct roar_vio_calls * calls, char * command, int options) {
222#ifndef ROAR_WITHOUT_VIO_CMD
223 struct roar_vio_2popen_state * state;
224
225 if ( calls == NULL || command == NULL || options < 0 )
226  return -1;
227
228 if ( (state = roar_mm_malloc(sizeof(struct roar_vio_2popen_state))) == NULL )
229  return -1;
230
231 ROAR_DBG("roar_vio_open_2popen(*): pre reqs are OK");
232
233 // clear all
234 memset(calls, 0, sizeof(struct roar_vio_calls));
235 memset(state, 0, sizeof(struct roar_vio_cmd_state));
236
237 // init reader and writer:
238 state->child.pid = -1;
239 state->child.in  = -1;
240 state->child.out = -1;
241
242 state->child.cmd = strdup(command);
243
244 // init state
245 state->options = options;
246 state->state   = ROAR_VIO_CMD_STATE_OPEN;
247
248 // init calls
249 calls->close    = roar_vio_2popen_close;
250/*
251 calls->read     = roar_vio_2popen_read;
252 calls->write    = roar_vio_2popen_write;
253 calls->nonblock = roar_vio_2popen_nonblock;
254 calls->sync     = roar_vio_2popen_sync;
255 calls->ctl      = roar_vio_2popen_ctl;
256*/
257 calls->inst     = (void*) state;
258
259 ROAR_DBG("roar_vio_open_2popen(*): var setup OK");
260
261 if ( !(options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) {
262  if ( roar_vio_cmd_fork(&(state->child)) == -1 )
263   return roar_vio_2popen_close(calls);
264 }
265
266 return 0;
267#else
268 return -1;
269#endif
270}
271
272#ifndef ROAR_WITHOUT_VIO_CMD
273int roar_vio_2popen_close(struct roar_vio_calls * vio) {
274 struct roar_vio_2popen_state * state = (struct roar_vio_2popen_state *)vio->inst;
275
276 state->state = ROAR_VIO_CMD_STATE_CLOSING;
277
278 if ( state->child.opened )
279  roar_vio_cmd_wait(&(state->child));
280
281 if ( state->child.cmd != NULL )
282  free(state->child.cmd);
283
284 roar_mm_free(state);
285
286 return 0;
287}
288#endif
289
290
291// VIOs:
292
293ssize_t roar_vio_cmd_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
294 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
295 fd_set rfhs[1], wfhs[1];
296 struct timeval tv;
297 size_t done = 0;
298 int max_fh;
299 int ret;
300 char   tbuf[ROAR_VIO_CMD_BUFSIZE];
301 char * tp   = NULL;
302 size_t tlen = 0;
303 int    nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK;
304 int    in, out;
305 int    eating = 1;
306
307 ROAR_DBG("roar_vio_cmd_read(*) = ?");
308
309 if ( !state->reader.opened ) {
310  if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no reader is forked :) */
311   return 0;
312
313  if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no reader exists? -> err */
314   return -1;
315
316  if ( roar_vio_cmd_fork(&(state->reader)) == -1 )
317   return -1;
318 }
319
320 in  = state->reader.in;
321 out = state->reader.out;
322
323 while (done < count || eating) {
324  if ( nonblock ) {
325   tv.tv_sec  =    0;
326   tv.tv_usec =    1;
327  } else {
328   tv.tv_sec  = 3600;
329   tv.tv_usec =    0;
330  }
331
332  FD_ZERO(rfhs);
333  FD_ZERO(wfhs);
334
335  FD_SET(in,  rfhs);
336
337  if ( out != -1 ) {
338   FD_SET(out, wfhs);
339  }
340
341#ifdef DEBUG
342  if ( FD_ISSET(in, rfhs) ) {
343   ROAR_DBG("roar_vio_cmd_read(*): reader set in fh group");
344  }
345#endif
346
347  max_fh = in > out ? in : out;
348  ROAR_DBG("roar_vio_cmd_read(*): max_fh=%i", max_fh);
349
350  if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 )
351   return -1;
352
353  ROAR_DBG("roar_vio_cmd_read(*): select(*) = %i", ret);
354  ROAR_DBG("roar_vio_cmd_read(*): reader=%i, writer=%i", in, out);
355
356  if ( ret > 0 ) {
357   if ( FD_ISSET(in, rfhs) ) {
358    ROAR_DBG("roar_vio_cmd_read(*): event on reader");
359
360    if ( (ret = read(in, buf+done, count-done)) == -1 )
361     break;
362
363    if ( ret == 0 )
364     break;
365
366    done += ret;
367   }
368
369   if ( out != -1 && FD_ISSET(out, wfhs) ) {
370    ROAR_DBG("roar_vio_cmd_read(*): event on writer");
371    eating = 1;
372
373    if ( !tlen ) {
374     tp   = tbuf;
375     tlen = 0;
376     if ( (tlen = roar_vio_read(state->next, tp, ROAR_VIO_CMD_BUFSIZE)) == -1 )
377      continue;
378    }
379
380    if ( tlen ) {
381     if ( (ret = write(out, tp, tlen)) > 0 ) {
382      tlen -= ret;
383      tp   += ret;
384     }
385    } else {
386     close(out);
387     state->reader.out = out = -1;
388    }
389   } else {
390    eating = 0;
391   }
392  }
393
394  if ( nonblock )
395   break;
396 }
397
398 if ( tlen ) { /* we have some data to write to the child... */
399  // TODO: try to write it out...
400  return -1;
401 }
402
403 return done;
404}
405
406ssize_t roar_vio_cmd_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
407 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
408 fd_set rfhs[1], wfhs[1];
409 struct timeval tv;
410 size_t done = 0;
411 int max_fh;
412 int ret;
413 char   tbuf[ROAR_VIO_CMD_BUFSIZE];
414 int    nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK;
415 int    in, out;
416
417 if ( !state->writer.opened ) {
418  if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no writer is forked :) */
419   return 0;
420
421  if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no writer exists? -> err */
422   return -1;
423
424  if ( roar_vio_cmd_fork(&(state->writer)) == -1 )
425   return -1;
426 }
427
428 in  = state->writer.in;
429 out = state->writer.out;
430
431 if ( buf == NULL ) { // we are requested to sync
432  if ( in != -1 ) {
433   ret = 1;
434   done = 0;
435   while (ret > 0) {
436    if ( state->state == ROAR_VIO_CMD_STATE_CLOSING ) {
437     tv.tv_sec  = 3600;
438     tv.tv_usec = 0;
439    } else {
440     tv.tv_sec  = 0;
441     tv.tv_usec = done ? 1 : 50000; // 50ms
442    }
443
444    done++;
445
446    FD_ZERO(rfhs);
447    FD_SET(in, rfhs);
448
449    if ( select(in+1, rfhs, NULL, NULL, &tv) < 1 )
450     break;
451
452    ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE);
453
454    if ( roar_vio_write(state->next, tbuf, ret) != ret )
455     return -1;
456   }
457  }
458
459  return 0;
460 }
461
462 while (done < count) {
463  if ( nonblock ) {
464   tv.tv_sec  =    0;
465   tv.tv_usec =    1;
466  } else {
467   tv.tv_sec  = 3600;
468   tv.tv_usec =    0;
469  }
470
471  FD_ZERO(rfhs);
472  FD_ZERO(wfhs);
473
474  FD_SET(out, wfhs);
475
476  if ( in != -1 ) {
477   FD_SET(in,  rfhs);
478  }
479
480  max_fh = in > out ? in : out;
481
482  if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 )
483   return -1;
484
485  if ( ret > 0 ) {
486   if ( FD_ISSET(out, wfhs) ) {
487
488    if ( (ret = write(out, buf+done, count-done)) == -1 )
489     break;
490
491    if ( ret == 0 )
492     break;
493
494    done += ret;
495   }
496   if ( in != -1 && FD_ISSET(in, rfhs) ) {
497    if ( (ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE)) == -1 ) { /* error case: can not read on reader -> EOF */
498     close(in);
499     state->writer.in = in = -1;
500     break;
501    }
502
503    if ( roar_vio_write(state->next, tbuf, ret) != ret )
504     return -1;
505   }
506  }
507
508  if ( nonblock )
509   break;
510 }
511
512 return done;
513}
514
515int     roar_vio_cmd_nonblock(struct roar_vio_calls * vio, int state) {
516 struct roar_vio_cmd_state * self = (struct roar_vio_cmd_state *)vio->inst;
517
518 self->options |= ROAR_VIO_CMD_OPTS_NONBLOCK;
519
520 if ( state == ROAR_SOCKET_BLOCK )
521  self->options -= ROAR_VIO_CMD_OPTS_NONBLOCK;
522
523 roar_vio_nonblock(self->next, state); // this should help, but may not nessessery.
524
525 return 0;
526}
527
528int     roar_vio_cmd_sync    (struct roar_vio_calls * vio) {
529 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
530 int oldblock;
531 int ret = 0;
532
533 oldblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK ? ROAR_SOCKET_NONBLOCK : ROAR_SOCKET_BLOCK;
534
535 if ( roar_vio_cmd_nonblock(vio, ROAR_SOCKET_BLOCK) == -1 )
536  return -1;
537
538 if ( roar_vio_cmd_write(vio, NULL, 0) == -1 )
539  ret = -1;
540
541 if ( roar_vio_cmd_read(vio, NULL, 0) == -1 )
542  ret = -1;
543
544 if ( roar_vio_cmd_nonblock(vio, oldblock) == -1 )
545  return -1;
546
547 return ret;
548}
549
550int     roar_vio_cmd_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
551 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
552 char buf[1];
553
554 ROAR_WARN("roar_vio_cmd_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
555
556 switch (cmd) {
557  case ROAR_VIO_CTL_GET_NEXT:
558    *(struct roar_vio_calls **)data = state->next;
559    return -1;
560   break;
561  case ROAR_VIO_CTL_GET_FH:
562    return -1;
563   break;
564  case ROAR_VIO_CTL_GET_READ_FH:
565  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
566//    if ( !state->reader.opened ) {
567     //for (i = 0; i < 128; i++)
568      roar_vio_cmd_read(vio, buf, 0);
569//    }
570    if ( state->reader.opened ) {
571     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);
572     *(int*)data = state->reader.in;
573     return 0;
574    }
575    return -1;
576   break;
577  case ROAR_VIO_CTL_GET_WRITE_FH:
578    return -1;
579   break;
580  default:
581    return -1;
582 }
583
584 return 0;
585}
586#endif
587
588// MISC:
589int roar_vio_open_gzip(struct roar_vio_calls * calls, struct roar_vio_calls * dst, int level) {
590#if defined(ROAR_HAVE_BIN_GZIP) || !defined(ROAR_WITHOUT_VIO_CMD)
591 char   wbuf[80];
592 char * writer = ROAR_HAVE_BIN_GZIP " -c";
593
594 if ( level != -1 ) {
595  snprintf(wbuf, 80, "%s -c%i", ROAR_HAVE_BIN_GZIP, level);
596  writer = wbuf;
597 }
598
599 return roar_vio_open_cmd(calls, dst, ROAR_HAVE_BIN_GZIP " -dc", writer, ROAR_VIO_CMD_OPTS_ON_DEMAND);
600#else
601 return -1;
602#endif
603}
604
605int roar_vio_open_gpg(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int wronly, char * opts, int options) {
606#if defined(ROAR_HAVE_BIN_GPG) && !defined(ROAR_WITHOUT_VIO_CMD)
607 char command[1024];
608 char para[1024] = {0};
609 int pwpipe[2];
610 int ret;
611
612/*
613#define ROAR_VIO_PGP_OPTS_NONE      0x00
614#define ROAR_VIO_PGP_OPTS_ASCII     0x01
615#define ROAR_VIO_PGP_OPTS_SIGN      0x02
616#define ROAR_VIO_PGP_OPTS_TEXTMODE  0x04
617*/
618
619 if ( options & ROAR_VIO_PGP_OPTS_ASCII )
620  strncat(para, "--armor ", 16);
621
622 if ( options & ROAR_VIO_PGP_OPTS_SIGN )
623  strncat(para, "--sign ", 16);
624
625 if ( options & ROAR_VIO_PGP_OPTS_TEXTMODE )
626  strncat(para, "--textmode ", 16);
627
628 if ( pw != NULL ) {
629  if ( pipe(pwpipe) == -1 )
630   return -1;
631
632  snprintf(command, 1024, "%s --batch --no-verbose --quiet --passphrase-repeat 0 --passphrase-fd %i %s %s", ROAR_HAVE_BIN_GPG, pwpipe[0], para, opts);
633
634  write(pwpipe[1], pw, strlen(pw));
635
636  close(pwpipe[1]);
637 } else {
638  snprintf(command, 1024, "%s --no-verbose --quiet %s %s", ROAR_HAVE_BIN_GPG, para, opts);
639 }
640
641 if ( wronly ) {
642  ret = roar_vio_open_cmd(calls, dst, NULL, command, 0);
643 } else {
644  ret = roar_vio_open_cmd(calls, dst, command, NULL, 0);
645 }
646
647 if ( pw != NULL )
648  close(pwpipe[0]);
649
650 return ret;
651#else
652 return -1;
653#endif
654}
655
656int roar_vio_open_pgp_decrypt(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw) {
657 return roar_vio_open_gpg(calls, dst, pw, 0, "-d", ROAR_VIO_PGP_OPTS_NONE);
658}
659
660int roar_vio_open_pgp_store(struct roar_vio_calls * calls, struct roar_vio_calls * dst, int options) {
661 return roar_vio_open_gpg(calls, dst, NULL, 1, "--store", options);
662}
663
664int roar_vio_open_pgp_encrypt_sym(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int options) {
665 return roar_vio_open_gpg(calls, dst, pw, 1, "--symmetric", options);
666}
667int roar_vio_open_pgp_encrypt_pub(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int options, char * recipient) {
668 char buf[1024];
669
670 snprintf(buf, 1024, "-e -r %s", recipient);
671 return roar_vio_open_gpg(calls, dst, pw, 1, buf, options);
672}
673
674//ll
Note: See TracBrowser for help on using the repository browser.