source: roaraudio/libroar/vio_cmd.c @ 1257:0822da400485

Last change on this file since 1257:0822da400485 was 1257:0822da400485, checked in by phi, 15 years ago

wrote a reader for vio cmd, fixed a bug forking at open even with ROAR_VIO_CMD_OPTS_ON_DEMAND

File size: 7.5 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                      int flags, 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 ( flags == 0 )
45  return -1;
46
47 if ( reader == NULL && writer == NULL )
48  return -1;
49
50 if ( (state = malloc(sizeof(struct roar_vio_cmd_state))) == NULL )
51  return -1;
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->flags   = flags;
75 state->options = options;
76
77 // init calls
78 calls->inst = (void*) state;
79
80 if ( !(options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) {
81  if ( reader != NULL )
82   if ( roar_vio_cmd_fork(&(state->reader)) == -1 )
83    return roar_vio_cmd_close(calls);
84
85  if ( writer != NULL )
86   if ( roar_vio_cmd_fork(&(state->writer)) == -1 )
87    return roar_vio_cmd_close(calls);
88 }
89
90 return 0;
91}
92
93int roar_vio_cmd_close(struct roar_vio_calls * vio) {
94 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
95
96 if ( state->reader.opened )
97  roar_vio_cmd_wait(&(state->reader));
98
99 if ( state->writer.opened )
100  roar_vio_cmd_wait(&(state->writer));
101
102 if ( state->reader.cmd != NULL )
103  free(state->reader.cmd);
104
105 if ( state->writer.cmd != NULL )
106  free(state->writer.cmd);
107
108 roar_vio_close(state->next);
109
110 free(state);
111
112 return 0;
113}
114
115int roar_vio_cmd_fork(struct roar_vio_cmd_child * child) {
116 int in[2], out[2];
117
118 if ( child == NULL )
119  return -1;
120
121 if ( child->opened )
122  return 0;
123
124 if ( child->cmd == NULL )
125  return -1;
126
127 // open some pipes...
128 if ( pipe(in) != 0 )
129  return -1;
130
131 if ( pipe(out) != 0 ) {
132  close(in[0]);
133  close(in[1]);
134  return -1;
135 }
136
137 child->pid = fork();
138
139 switch (child->pid) {
140  case -1:
141    close(in[0]);
142    close(in[1]);
143    close(out[0]);
144    close(out[1]);
145    return -1;
146   break;
147  case 0:
148    close(in[0]);
149    close(out[1]);
150    close(ROAR_STDIN);
151    close(ROAR_STDOUT);
152
153    if ( dup2(out[0], ROAR_STDIN) == -1 )
154     _exit(1);
155
156    if ( dup2(in[1], ROAR_STDOUT) == -1 )
157     _exit(1);
158
159    execlp("/bin/sh", "/bin/sh", "-c", child->cmd, NULL);
160
161    _exit(1);
162   break;
163 }
164
165 close(in[1]);
166 close(out[0]);
167
168 child->opened = 1;
169 child->in     = in[0];
170 child->out    = out[1];
171
172 return 0;
173}
174
175int roar_vio_cmd_wait(struct roar_vio_cmd_child * child) {
176 int status;
177
178 if ( child == NULL )
179  return -1;
180
181 if ( !child->opened )
182  return 0;
183
184 if ( child->out != -1 )
185  close(child->out);
186
187 if ( child->in  != -1 )
188  close(child->in);
189
190 waitpid(child->pid, &status, 0);
191
192 return 0;
193}
194
195// VIOs:
196
197ssize_t roar_vio_cmd_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
198 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
199 fd_set rfhs[1], wfhs[1];
200 struct timeval tv;
201 size_t done = 0;
202 int max_fh;
203 int ret;
204 char   tbuf[ROAR_VIO_CMD_BUFSIZE];
205 char * tp   = NULL;
206 size_t tlen = 0;
207
208 if ( !state->reader.opened ) {
209  if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no reader is forked :) */
210   return 0;
211
212  if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no reader exists? -> err */
213   return -1;
214
215  if ( roar_vio_cmd_fork(&(state->reader)) == -1 )
216   return -1;
217 }
218
219 while (done < count) {
220  if ( state->options & ROAR_VIO_CMD_OPTS_NONBLOCK ) {
221   tv.tv_sec  =    0;
222   tv.tv_usec =    1;
223  } else {
224   tv.tv_sec  = 3600;
225   tv.tv_usec =    0;
226  }
227
228  FD_ZERO(rfhs);
229  FD_ZERO(wfhs);
230
231  FD_SET(state->reader.in,  rfhs);
232  FD_SET(state->reader.out, wfhs);
233
234  max_fh = state->reader.in > state->reader.out ? state->reader.in : state->reader.out;
235
236  if ( (ret = select(max_fh + 1, rfhs, wfhs, rfhs, &tv)) == -1 )
237   return -1;
238
239  if ( ret > 0 ) {
240   if ( FD_ISSET(state->reader.in, rfhs) ) {
241    if ( (ret = read(state->reader.in, buf+done, count-done)) == -1 )
242     break;
243
244    if ( ret == 0 )
245     break;
246
247    done += ret;
248   }
249
250   if ( FD_ISSET(state->reader.out, wfhs) ) {
251    if ( !tlen ) {
252     tp = tbuf;
253     if ( (tlen = roar_vio_read(state->next, tp, ROAR_VIO_CMD_BUFSIZE)) == -1 )
254      tlen = 0;
255    }
256
257    if ( tlen ) {
258     if ( (ret = write(state->reader.out, tp, tlen)) > 0 ) {
259      tlen -= ret;
260      tp   += ret;
261     }
262    }
263   }
264  }
265
266  if ( state->options & ROAR_VIO_CMD_OPTS_NONBLOCK )
267   break;
268 }
269
270 if ( tlen ) { /* we have some data to write to the child... */
271  // TODO: try to write it out...
272  return -1;
273 }
274
275 return done;
276}
277
278ssize_t roar_vio_cmd_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
279 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
280
281 if ( !state->writer.opened ) {
282  if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no writer is forked :) */
283   return 0;
284
285  if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no writer exists? -> err */
286   return -1;
287
288  if ( roar_vio_cmd_fork(&(state->writer)) == -1 )
289   return -1;
290 }
291
292
293 return -1;
294}
295
296int     roar_vio_cmd_nonblock(struct roar_vio_calls * vio, int state) {
297 struct roar_vio_cmd_state * self = (struct roar_vio_cmd_state *)vio->inst;
298
299 self->options |= ROAR_VIO_CMD_OPTS_NONBLOCK;
300
301 if ( state == ROAR_SOCKET_BLOCK )
302  self->options -= ROAR_VIO_CMD_OPTS_NONBLOCK;
303
304 roar_vio_nonblock(self->next, state); // this should help, but may not nessessery.
305
306 return 0;
307}
308
309int     roar_vio_cmd_sync    (struct roar_vio_calls * vio) {
310 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst;
311 int oldblock;
312 int ret = 0;
313
314 oldblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK ? ROAR_SOCKET_NONBLOCK : ROAR_SOCKET_BLOCK;
315
316 if ( roar_vio_cmd_nonblock(vio, ROAR_SOCKET_BLOCK) == -1 )
317  return -1;
318
319 if ( roar_vio_cmd_write(vio, NULL, 0) == -1 )
320  ret = -1;
321
322 if ( roar_vio_cmd_read(vio, NULL, 0) == -1 )
323  ret = -1;
324
325 if ( roar_vio_cmd_nonblock(vio, oldblock) == -1 )
326  return -1;
327
328 return ret;
329}
330
331//ll
Note: See TracBrowser for help on using the repository browser.