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 | |
---|
37 | int 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 ( reader != NULL ) |
---|
81 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
82 | return roar_vio_cmd_close(calls); |
---|
83 | |
---|
84 | if ( writer != NULL ) |
---|
85 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
86 | return roar_vio_cmd_close(calls); |
---|
87 | |
---|
88 | return 0; |
---|
89 | } |
---|
90 | |
---|
91 | int roar_vio_cmd_close(struct roar_vio_calls * vio) { |
---|
92 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
93 | |
---|
94 | if ( state->reader.opened ) |
---|
95 | roar_vio_cmd_wait(&(state->reader)); |
---|
96 | |
---|
97 | if ( state->writer.opened ) |
---|
98 | roar_vio_cmd_wait(&(state->writer)); |
---|
99 | |
---|
100 | if ( state->reader.cmd != NULL ) |
---|
101 | free(state->reader.cmd); |
---|
102 | |
---|
103 | if ( state->writer.cmd != NULL ) |
---|
104 | free(state->writer.cmd); |
---|
105 | |
---|
106 | roar_vio_close(state->next); |
---|
107 | |
---|
108 | free(state); |
---|
109 | |
---|
110 | return 0; |
---|
111 | } |
---|
112 | |
---|
113 | int roar_vio_cmd_fork(struct roar_vio_cmd_child * child) { |
---|
114 | int in[2], out[2]; |
---|
115 | |
---|
116 | if ( child == NULL ) |
---|
117 | return -1; |
---|
118 | |
---|
119 | if ( child->opened ) |
---|
120 | return 0; |
---|
121 | |
---|
122 | if ( child->cmd == NULL ) |
---|
123 | return -1; |
---|
124 | |
---|
125 | // open some pipes... |
---|
126 | if ( pipe(in) != 0 ) |
---|
127 | return -1; |
---|
128 | |
---|
129 | if ( pipe(out) != 0 ) { |
---|
130 | close(in[0]); |
---|
131 | close(in[1]); |
---|
132 | return -1; |
---|
133 | } |
---|
134 | |
---|
135 | child->pid = fork(); |
---|
136 | |
---|
137 | switch (child->pid) { |
---|
138 | case -1: |
---|
139 | close(in[0]); |
---|
140 | close(in[1]); |
---|
141 | close(out[0]); |
---|
142 | close(out[1]); |
---|
143 | return -1; |
---|
144 | break; |
---|
145 | case 0: |
---|
146 | close(in[0]); |
---|
147 | close(out[1]); |
---|
148 | close(ROAR_STDIN); |
---|
149 | close(ROAR_STDOUT); |
---|
150 | |
---|
151 | if ( dup2(out[0], ROAR_STDIN) == -1 ) |
---|
152 | _exit(1); |
---|
153 | |
---|
154 | if ( dup2(in[1], ROAR_STDOUT) == -1 ) |
---|
155 | _exit(1); |
---|
156 | |
---|
157 | execlp("/bin/sh", "/bin/sh", "-c", child->cmd, NULL); |
---|
158 | |
---|
159 | _exit(1); |
---|
160 | break; |
---|
161 | } |
---|
162 | |
---|
163 | close(in[1]); |
---|
164 | close(out[0]); |
---|
165 | |
---|
166 | child->opened = 1; |
---|
167 | child->in = in[0]; |
---|
168 | child->out = out[1]; |
---|
169 | |
---|
170 | return 0; |
---|
171 | } |
---|
172 | |
---|
173 | int roar_vio_cmd_wait(struct roar_vio_cmd_child * child) { |
---|
174 | int status; |
---|
175 | |
---|
176 | if ( child == NULL ) |
---|
177 | return -1; |
---|
178 | |
---|
179 | if ( !child->opened ) |
---|
180 | return 0; |
---|
181 | |
---|
182 | if ( child->out != -1 ) |
---|
183 | close(child->out); |
---|
184 | |
---|
185 | if ( child->in != -1 ) |
---|
186 | close(child->in); |
---|
187 | |
---|
188 | waitpid(child->pid, &status, 0); |
---|
189 | |
---|
190 | return 0; |
---|
191 | } |
---|
192 | |
---|
193 | // VIOs: |
---|
194 | |
---|
195 | ssize_t roar_vio_cmd_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
196 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
197 | |
---|
198 | if ( !state->reader.opened ) { |
---|
199 | if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no reader is forked :) */ |
---|
200 | return 0; |
---|
201 | |
---|
202 | if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no reader exists? -> err */ |
---|
203 | return -1; |
---|
204 | |
---|
205 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
206 | return -1; |
---|
207 | } |
---|
208 | |
---|
209 | return -1; |
---|
210 | } |
---|
211 | |
---|
212 | ssize_t roar_vio_cmd_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
213 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
214 | |
---|
215 | if ( !state->writer.opened ) { |
---|
216 | if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no writer is forked :) */ |
---|
217 | return 0; |
---|
218 | |
---|
219 | if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no writer exists? -> err */ |
---|
220 | return -1; |
---|
221 | |
---|
222 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
223 | return -1; |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | return -1; |
---|
228 | } |
---|
229 | |
---|
230 | int roar_vio_cmd_nonblock(struct roar_vio_calls * vio, int state) { |
---|
231 | struct roar_vio_cmd_state * self = (struct roar_vio_cmd_state *)vio->inst; |
---|
232 | |
---|
233 | self->options |= ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
234 | |
---|
235 | if ( state == ROAR_SOCKET_BLOCK ) |
---|
236 | self->options -= ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
237 | |
---|
238 | return 0; |
---|
239 | } |
---|
240 | |
---|
241 | int roar_vio_cmd_sync (struct roar_vio_calls * vio) { |
---|
242 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
243 | int oldblock; |
---|
244 | int ret = 0; |
---|
245 | |
---|
246 | oldblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK ? ROAR_SOCKET_NONBLOCK : ROAR_SOCKET_BLOCK; |
---|
247 | |
---|
248 | if ( roar_vio_cmd_nonblock(vio, ROAR_SOCKET_BLOCK) == -1 ) |
---|
249 | return -1; |
---|
250 | |
---|
251 | if ( roar_vio_cmd_write(vio, NULL, 0) == -1 ) |
---|
252 | ret = -1; |
---|
253 | |
---|
254 | if ( roar_vio_cmd_read(vio, NULL, 0) == -1 ) |
---|
255 | ret = -1; |
---|
256 | |
---|
257 | if ( roar_vio_cmd_nonblock(vio, oldblock) == -1 ) |
---|
258 | return -1; |
---|
259 | |
---|
260 | return ret; |
---|
261 | } |
---|
262 | |
---|
263 | //ll |
---|