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