source: roaraudio/libroar/vio_cmd.c @ 1260:9d5611d31291

Last change on this file since 1260:9d5611d31291 was 1260:9d5611d31291, checked in by phi, 15 years ago

got vio cmd reader working, :)

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