source: roaraudio/libroar/vio_cmd.c @ 5298:a0d70da17a74

Last change on this file since 5298:a0d70da17a74 was 5278:b3e0dd3f3141, checked in by phi, 12 years ago

last parts of merging _nonblock into _ctl and fixed sizeof(cmd) of _ctls

File size: 15.4 KB
Line 
1//vio_cmd.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38int roar_vio_open_cmd(struct roar_vio_calls * calls, struct roar_vio_calls * dst,
39                      char * reader, char * writer, int options) {
40#ifndef ROAR_WITHOUT_VIO_CMD
41 struct roar_vio_cmd_state * state;
42
43 if ( calls == NULL || dst == NULL )
44  return -1;
45
46 if ( reader == NULL && writer == NULL )
47  return -1;
48
49 if ( (state = roar_mm_malloc(sizeof(struct roar_vio_cmd_state))) == NULL )
50  return -1;
51
52 ROAR_DBG("roar_vio_open_cmd(*): pre reqs are OK");
53
54 // clear all
55 memset(calls, 0, sizeof(struct roar_vio_calls));
56 memset(state, 0, sizeof(struct roar_vio_cmd_state));
57
58 // init reader and writer:
59 state->reader.pid = -1;
60 state->reader.in  = -1;
61 state->reader.out = -1;
62
63 if ( reader != NULL )
64  state->reader.cmd = roar_mm_strdup(reader);
65
66 state->writer.pid = -1;
67 state->writer.in  = -1;
68 state->writer.out = -1;
69
70 if ( writer != NULL )
71  state->writer.cmd = roar_mm_strdup(writer);
72
73 // init state
74 state->next    = dst;
75 state->options = options;
76 state->state   = ROAR_VIO_CMD_STATE_OPEN;
77
78 // init calls
79 calls->close    = roar_vio_cmd_close;
80 calls->read     = roar_vio_cmd_read;
81 calls->write    = roar_vio_cmd_write;
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  roar_mm_free(state->reader.cmd);
127
128 if ( state->writer.cmd != NULL )
129  roar_mm_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_2popen_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 = roar_mm_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->sync     = roar_vio_2popen_sync;
254 calls->ctl      = roar_vio_2popen_ctl;
255*/
256 calls->inst     = (void*) state;
257
258 ROAR_DBG("roar_vio_open_2popen(*): var setup OK");
259
260 if ( !(options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) {
261  if ( roar_vio_cmd_fork(&(state->child)) == -1 )
262   return roar_vio_2popen_close(calls);
263 }
264
265 return 0;
266#else
267 return -1;
268#endif
269}
270
271#ifndef ROAR_WITHOUT_VIO_CMD
272int roar_vio_2popen_close(struct roar_vio_calls * vio) {
273 struct roar_vio_2popen_state * state = (struct roar_vio_2popen_state *)vio->inst;
274
275 state->state = ROAR_VIO_CMD_STATE_CLOSING;
276
277 if ( state->child.opened )
278  roar_vio_cmd_wait(&(state->child));
279
280 if ( state->child.cmd != NULL )
281  roar_mm_free(state->child.cmd);
282
283 roar_mm_free(state);
284
285 return 0;
286}
287#endif
288
289
290// VIOs:
291
292ssize_t roar_vio_cmd_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
293 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
294 fd_set rfhs[1], wfhs[1];
295 struct timeval tv;
296 size_t done = 0;
297 int max_fh;
298 int ret;
299 char   tbuf[ROAR_VIO_CMD_BUFSIZE];
300 char * tp   = NULL;
301 size_t tlen = 0;
302 int    nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK;
303 int    in, out;
304 int    eating = 1;
305
306 ROAR_DBG("roar_vio_cmd_read(*) = ?");
307
308 if ( !state->reader.opened ) {
309  if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no reader is forked :) */
310   return 0;
311
312  if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no reader exists? -> err */
313   return -1;
314
315  if ( roar_vio_cmd_fork(&(state->reader)) == -1 )
316   return -1;
317 }
318
319 in  = state->reader.in;
320 out = state->reader.out;
321
322 while (done < count || eating) {
323  if ( nonblock ) {
324   tv.tv_sec  =    0;
325   tv.tv_usec =    1;
326  } else {
327   tv.tv_sec  = 3600;
328   tv.tv_usec =    0;
329  }
330
331  FD_ZERO(rfhs);
332  FD_ZERO(wfhs);
333
334  FD_SET(in,  rfhs);
335
336  if ( out != -1 ) {
337   FD_SET(out, wfhs);
338  }
339
340#ifdef DEBUG
341  if ( FD_ISSET(in, rfhs) ) {
342   ROAR_DBG("roar_vio_cmd_read(*): reader set in fh group");
343  }
344#endif
345
346  max_fh = in > out ? in : out;
347  ROAR_DBG("roar_vio_cmd_read(*): max_fh=%i", max_fh);
348
349  if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 )
350   return -1;
351
352  ROAR_DBG("roar_vio_cmd_read(*): select(*) = %i", ret);
353  ROAR_DBG("roar_vio_cmd_read(*): reader=%i, writer=%i", in, out);
354
355  if ( ret > 0 ) {
356   if ( FD_ISSET(in, rfhs) ) {
357    ROAR_DBG("roar_vio_cmd_read(*): event on reader");
358
359    if ( (ret = read(in, buf+done, count-done)) == -1 )
360     break;
361
362    if ( ret == 0 )
363     break;
364
365    done += ret;
366   }
367
368   if ( out != -1 && FD_ISSET(out, wfhs) ) {
369    ROAR_DBG("roar_vio_cmd_read(*): event on writer");
370    eating = 1;
371
372    if ( !tlen ) {
373     tp   = tbuf;
374     tlen = 0;
375     if ( (tlen = roar_vio_read(state->next, tp, ROAR_VIO_CMD_BUFSIZE)) == -1 )
376      continue;
377    }
378
379    if ( tlen ) {
380     if ( (ret = write(out, tp, tlen)) > 0 ) {
381      tlen -= ret;
382      tp   += ret;
383     }
384    } else {
385     close(out);
386     state->reader.out = out = -1;
387    }
388   } else {
389    eating = 0;
390   }
391  }
392
393  if ( nonblock )
394   break;
395 }
396
397 if ( tlen ) { /* we have some data to write to the child... */
398  // TODO: try to write it out...
399  return -1;
400 }
401
402 return done;
403}
404
405ssize_t roar_vio_cmd_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
406 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
407 fd_set rfhs[1], wfhs[1];
408 struct timeval tv;
409 size_t done = 0;
410 int max_fh;
411 int ret;
412 char   tbuf[ROAR_VIO_CMD_BUFSIZE];
413 int    nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK;
414 int    in, out;
415
416 if ( !state->writer.opened ) {
417  if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no writer is forked :) */
418   return 0;
419
420  if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no writer exists? -> err */
421   return -1;
422
423  if ( roar_vio_cmd_fork(&(state->writer)) == -1 )
424   return -1;
425 }
426
427 in  = state->writer.in;
428 out = state->writer.out;
429
430 if ( buf == NULL ) { // we are requested to sync
431  if ( in != -1 ) {
432   ret = 1;
433   done = 0;
434   while (ret > 0) {
435    if ( state->state == ROAR_VIO_CMD_STATE_CLOSING ) {
436     tv.tv_sec  = 3600;
437     tv.tv_usec = 0;
438    } else {
439     tv.tv_sec  = 0;
440     tv.tv_usec = done ? 1 : 50000; // 50ms
441    }
442
443    done++;
444
445    FD_ZERO(rfhs);
446    FD_SET(in, rfhs);
447
448    if ( select(in+1, rfhs, NULL, NULL, &tv) < 1 )
449     break;
450
451    ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE);
452
453    if ( roar_vio_write(state->next, tbuf, ret) != ret )
454     return -1;
455   }
456  }
457
458  return 0;
459 }
460
461 while (done < count) {
462  if ( nonblock ) {
463   tv.tv_sec  =    0;
464   tv.tv_usec =    1;
465  } else {
466   tv.tv_sec  = 3600;
467   tv.tv_usec =    0;
468  }
469
470  FD_ZERO(rfhs);
471  FD_ZERO(wfhs);
472
473  FD_SET(out, wfhs);
474
475  if ( in != -1 ) {
476   FD_SET(in,  rfhs);
477  }
478
479  max_fh = in > out ? in : out;
480
481  if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 )
482   return -1;
483
484  if ( ret > 0 ) {
485   if ( FD_ISSET(out, wfhs) ) {
486
487    if ( (ret = write(out, buf+done, count-done)) == -1 )
488     break;
489
490    if ( ret == 0 )
491     break;
492
493    done += ret;
494   }
495   if ( in != -1 && FD_ISSET(in, rfhs) ) {
496    if ( (ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE)) == -1 ) { /* error case: can not read on reader -> EOF */
497     close(in);
498     state->writer.in = in = -1;
499     break;
500    }
501
502    if ( roar_vio_write(state->next, tbuf, ret) != ret )
503     return -1;
504   }
505  }
506
507  if ( nonblock )
508   break;
509 }
510
511 return done;
512}
513
514int     roar_vio_cmd_sync    (struct roar_vio_calls * vio) {
515 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
516 int newblock = ROAR_SOCKET_BLOCK;
517 int oldblock;
518 int ret = 0;
519
520 oldblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK ? ROAR_SOCKET_NONBLOCK : ROAR_SOCKET_BLOCK;
521
522 if ( roar_vio_cmd_ctl(vio, ROAR_VIO_CTL_NONBLOCK, &newblock) == -1 )
523  return -1;
524
525 if ( roar_vio_cmd_write(vio, NULL, 0) == -1 )
526  ret = -1;
527
528 if ( roar_vio_cmd_read(vio, NULL, 0) == -1 )
529  ret = -1;
530
531 if ( roar_vio_cmd_ctl(vio, ROAR_VIO_CTL_NONBLOCK, &oldblock) == -1 )
532  return -1;
533
534 return ret;
535}
536
537int     roar_vio_cmd_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
538 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
539 char buf[1];
540
541 ROAR_WARN("roar_vio_cmd_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
542
543 switch (cmd) {
544  case ROAR_VIO_CTL_GET_NAME:
545    if ( data == NULL )
546     return -1;
547
548    *(char**)data = "cmd";
549    return 0;
550   break;
551  case ROAR_VIO_CTL_GET_NEXT:
552    *(struct roar_vio_calls **)data = state->next;
553    return -1;
554   break;
555  case ROAR_VIO_CTL_GET_FH:
556    return -1;
557   break;
558  case ROAR_VIO_CTL_GET_READ_FH:
559  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
560//    if ( !state->reader.opened ) {
561     //for (i = 0; i < 128; i++)
562      roar_vio_cmd_read(vio, buf, 0);
563//    }
564    if ( state->reader.opened ) {
565     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);
566     *(int*)data = state->reader.in;
567     return 0;
568    }
569    return -1;
570   break;
571  case ROAR_VIO_CTL_GET_WRITE_FH:
572    return -1;
573   break;
574  case ROAR_VIO_CTL_NONBLOCK:
575    state->options |= ROAR_VIO_CMD_OPTS_NONBLOCK;
576
577    if ( *(int*)data == ROAR_SOCKET_BLOCK )
578     state->options -= ROAR_VIO_CMD_OPTS_NONBLOCK;
579
580    _LIBROAR_IGNORE_RET(roar_vio_ctl(state->next, cmd, data)); // this should help, but may not necessarily.
581   break;
582  default:
583    return -1;
584 }
585
586 return 0;
587}
588#endif
589
590// MISC:
591int roar_vio_open_gzip(struct roar_vio_calls * calls, struct roar_vio_calls * dst, int level) {
592 roar_debug_warn_obsolete("roar_vio_open_gzip", "roar_vio_open_zlib", NULL);
593
594#ifdef ROAR_HAVE_LIBZ
595 return roar_vio_open_zlib(calls, dst, level, 1);
596#else
597 roar_err_set(ROAR_ERROR_NOSYS);
598 return -1;
599#endif
600}
601
602int roar_vio_open_gpg(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int wronly, char * opts, int options) {
603#if defined(ROAR_HAVE_BIN_GPG) && !defined(ROAR_WITHOUT_VIO_CMD)
604 char command[1024];
605 char para[1024] = {0};
606 int pwpipe[2];
607 int ret;
608
609/*
610#define ROAR_VIO_PGP_OPTS_NONE      0x00
611#define ROAR_VIO_PGP_OPTS_ASCII     0x01
612#define ROAR_VIO_PGP_OPTS_SIGN      0x02
613#define ROAR_VIO_PGP_OPTS_TEXTMODE  0x04
614*/
615
616 if ( options & ROAR_VIO_PGP_OPTS_ASCII )
617  strncat(para, "--armor ", 16);
618
619 if ( options & ROAR_VIO_PGP_OPTS_SIGN )
620  strncat(para, "--sign ", 16);
621
622 if ( options & ROAR_VIO_PGP_OPTS_TEXTMODE )
623  strncat(para, "--textmode ", 16);
624
625 if ( pw != NULL ) {
626  if ( pipe(pwpipe) == -1 )
627   return -1;
628
629  snprintf(command, 1024, "%s --batch --no-verbose --quiet --passphrase-repeat 0 --passphrase-fd %i %s %s", ROAR_HAVE_BIN_GPG, pwpipe[0], para, opts);
630
631  write(pwpipe[1], pw, strlen(pw));
632
633  close(pwpipe[1]);
634 } else {
635  snprintf(command, 1024, "%s --no-verbose --quiet %s %s", ROAR_HAVE_BIN_GPG, para, opts);
636 }
637
638 if ( wronly ) {
639  ret = roar_vio_open_cmd(calls, dst, NULL, command, 0);
640 } else {
641  ret = roar_vio_open_cmd(calls, dst, command, NULL, 0);
642 }
643
644 if ( pw != NULL )
645  close(pwpipe[0]);
646
647 return ret;
648#else
649 return -1;
650#endif
651}
652
653int roar_vio_open_pgp_decrypt(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw) {
654 return roar_vio_open_gpg(calls, dst, pw, 0, "-d", ROAR_VIO_PGP_OPTS_NONE);
655}
656
657int roar_vio_open_pgp_store(struct roar_vio_calls * calls, struct roar_vio_calls * dst, int options) {
658 return roar_vio_open_gpg(calls, dst, NULL, 1, "--store", options);
659}
660
661int roar_vio_open_pgp_encrypt_sym(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int options) {
662 return roar_vio_open_gpg(calls, dst, pw, 1, "--symmetric", options);
663}
664int roar_vio_open_pgp_encrypt_pub(struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * pw, int options, char * recipient) {
665 char buf[1024];
666
667 snprintf(buf, 1024, "-e -r %s", recipient);
668 return roar_vio_open_gpg(calls, dst, pw, 1, buf, options);
669}
670
671//ll
Note: See TracBrowser for help on using the repository browser.