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