1 | //vio_cmd.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012 |
---|
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 | calls->flags = ROAR_VIO_FLAGS_NONE; |
---|
57 | calls->refc = 1; |
---|
58 | memset(state, 0, sizeof(struct roar_vio_cmd_state)); |
---|
59 | |
---|
60 | // init reader and writer: |
---|
61 | state->reader.pid = -1; |
---|
62 | state->reader.in = -1; |
---|
63 | state->reader.out = -1; |
---|
64 | |
---|
65 | if ( reader != NULL ) |
---|
66 | state->reader.cmd = roar_mm_strdup(reader); |
---|
67 | |
---|
68 | state->writer.pid = -1; |
---|
69 | state->writer.in = -1; |
---|
70 | state->writer.out = -1; |
---|
71 | |
---|
72 | if ( writer != NULL ) |
---|
73 | state->writer.cmd = roar_mm_strdup(writer); |
---|
74 | |
---|
75 | // init state |
---|
76 | state->next = dst; |
---|
77 | state->options = options; |
---|
78 | state->state = ROAR_VIO_CMD_STATE_OPEN; |
---|
79 | |
---|
80 | // init calls |
---|
81 | calls->close = roar_vio_cmd_close; |
---|
82 | calls->read = roar_vio_cmd_read; |
---|
83 | calls->write = roar_vio_cmd_write; |
---|
84 | calls->sync = roar_vio_cmd_sync; |
---|
85 | calls->ctl = roar_vio_cmd_ctl; |
---|
86 | calls->inst = (void*) state; |
---|
87 | |
---|
88 | ROAR_DBG("roar_vio_open_cmd(*): var setup OK"); |
---|
89 | |
---|
90 | if ( !(options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) { |
---|
91 | if ( reader != NULL ) |
---|
92 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
93 | return roar_vio_cmd_close(calls); |
---|
94 | |
---|
95 | if ( writer != NULL ) |
---|
96 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
97 | return roar_vio_cmd_close(calls); |
---|
98 | } |
---|
99 | |
---|
100 | return 0; |
---|
101 | #else |
---|
102 | return -1; |
---|
103 | #endif |
---|
104 | } |
---|
105 | |
---|
106 | #ifndef ROAR_WITHOUT_VIO_CMD |
---|
107 | int roar_vio_cmd_close(struct roar_vio_calls * vio) { |
---|
108 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
109 | |
---|
110 | state->state = ROAR_VIO_CMD_STATE_CLOSING; |
---|
111 | |
---|
112 | if ( state->writer.opened ) { |
---|
113 | if ( state->writer.out != -1 ) { |
---|
114 | close(state->writer.out); |
---|
115 | state->writer.out = -1; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | roar_vio_cmd_sync(vio); |
---|
120 | |
---|
121 | if ( state->reader.opened ) |
---|
122 | roar_vio_cmd_wait(&(state->reader)); |
---|
123 | |
---|
124 | if ( state->writer.opened ) |
---|
125 | roar_vio_cmd_wait(&(state->writer)); |
---|
126 | |
---|
127 | if ( state->reader.cmd != NULL ) |
---|
128 | roar_mm_free(state->reader.cmd); |
---|
129 | |
---|
130 | if ( state->writer.cmd != NULL ) |
---|
131 | roar_mm_free(state->writer.cmd); |
---|
132 | |
---|
133 | roar_vio_close(state->next); |
---|
134 | |
---|
135 | // state->state = ROAR_VIO_CMD_STATE_CLOSED; |
---|
136 | roar_mm_free(state); |
---|
137 | |
---|
138 | return 0; |
---|
139 | } |
---|
140 | |
---|
141 | int roar_vio_cmd_fork(struct roar_vio_cmd_child * child) { |
---|
142 | int in[2], out[2]; |
---|
143 | |
---|
144 | if ( child == NULL ) |
---|
145 | return -1; |
---|
146 | |
---|
147 | if ( child->opened ) |
---|
148 | return 0; |
---|
149 | |
---|
150 | if ( child->cmd == NULL ) |
---|
151 | return -1; |
---|
152 | |
---|
153 | // open some pipes... |
---|
154 | if ( pipe(in) != 0 ) |
---|
155 | return -1; |
---|
156 | |
---|
157 | if ( pipe(out) != 0 ) { |
---|
158 | close(in[0]); |
---|
159 | close(in[1]); |
---|
160 | return -1; |
---|
161 | } |
---|
162 | |
---|
163 | child->pid = roar_fork(NULL); |
---|
164 | |
---|
165 | switch (child->pid) { |
---|
166 | case -1: |
---|
167 | close(in[0]); |
---|
168 | close(in[1]); |
---|
169 | close(out[0]); |
---|
170 | close(out[1]); |
---|
171 | return -1; |
---|
172 | break; |
---|
173 | case 0: |
---|
174 | close(in[0]); |
---|
175 | close(out[1]); |
---|
176 | close(ROAR_STDIN); |
---|
177 | close(ROAR_STDOUT); |
---|
178 | |
---|
179 | if ( dup2(out[0], ROAR_STDIN) == -1 ) |
---|
180 | _exit(1); |
---|
181 | |
---|
182 | if ( dup2(in[1], ROAR_STDOUT) == -1 ) |
---|
183 | _exit(1); |
---|
184 | |
---|
185 | execlp("/bin/sh", "/bin/sh", "-c", child->cmd, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
186 | |
---|
187 | _exit(1); |
---|
188 | break; |
---|
189 | } |
---|
190 | |
---|
191 | close(in[1]); |
---|
192 | close(out[0]); |
---|
193 | |
---|
194 | child->opened = 1; |
---|
195 | child->in = in[0]; |
---|
196 | child->out = out[1]; |
---|
197 | |
---|
198 | return 0; |
---|
199 | } |
---|
200 | |
---|
201 | int roar_vio_cmd_wait(struct roar_vio_cmd_child * child) { |
---|
202 | int status; |
---|
203 | |
---|
204 | if ( child == NULL ) |
---|
205 | return -1; |
---|
206 | |
---|
207 | if ( !child->opened ) |
---|
208 | return 0; |
---|
209 | |
---|
210 | if ( child->out != -1 ) |
---|
211 | close(child->out); |
---|
212 | |
---|
213 | if ( child->in != -1 ) |
---|
214 | close(child->in); |
---|
215 | |
---|
216 | waitpid(child->pid, &status, 0); |
---|
217 | |
---|
218 | return 0; |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | |
---|
223 | int roar_vio_open_2popen(struct roar_vio_calls * calls, char * command, int options) { |
---|
224 | #ifndef ROAR_WITHOUT_VIO_CMD |
---|
225 | struct roar_vio_2popen_state * state; |
---|
226 | |
---|
227 | if ( calls == NULL || command == NULL || options < 0 ) |
---|
228 | return -1; |
---|
229 | |
---|
230 | if ( (state = roar_mm_malloc(sizeof(struct roar_vio_2popen_state))) == NULL ) |
---|
231 | return -1; |
---|
232 | |
---|
233 | ROAR_DBG("roar_vio_open_2popen(*): pre reqs are OK"); |
---|
234 | |
---|
235 | // clear all |
---|
236 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
237 | calls->flags = ROAR_VIO_FLAGS_NONE; |
---|
238 | calls->refc = 1; |
---|
239 | memset(state, 0, sizeof(struct roar_vio_2popen_state)); |
---|
240 | |
---|
241 | // init reader and writer: |
---|
242 | state->child.pid = -1; |
---|
243 | state->child.in = -1; |
---|
244 | state->child.out = -1; |
---|
245 | |
---|
246 | state->child.cmd = roar_mm_strdup(command); |
---|
247 | |
---|
248 | // init state |
---|
249 | state->options = options; |
---|
250 | state->state = ROAR_VIO_CMD_STATE_OPEN; |
---|
251 | |
---|
252 | // init calls |
---|
253 | calls->close = roar_vio_2popen_close; |
---|
254 | /* |
---|
255 | calls->read = roar_vio_2popen_read; |
---|
256 | calls->write = roar_vio_2popen_write; |
---|
257 | calls->sync = roar_vio_2popen_sync; |
---|
258 | calls->ctl = roar_vio_2popen_ctl; |
---|
259 | */ |
---|
260 | calls->inst = (void*) state; |
---|
261 | |
---|
262 | ROAR_DBG("roar_vio_open_2popen(*): var setup OK"); |
---|
263 | |
---|
264 | if ( !(options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) { |
---|
265 | if ( roar_vio_cmd_fork(&(state->child)) == -1 ) |
---|
266 | return roar_vio_2popen_close(calls); |
---|
267 | } |
---|
268 | |
---|
269 | return 0; |
---|
270 | #else |
---|
271 | return -1; |
---|
272 | #endif |
---|
273 | } |
---|
274 | |
---|
275 | #ifndef ROAR_WITHOUT_VIO_CMD |
---|
276 | int roar_vio_2popen_close(struct roar_vio_calls * vio) { |
---|
277 | struct roar_vio_2popen_state * state = (struct roar_vio_2popen_state *)vio->inst; |
---|
278 | |
---|
279 | state->state = ROAR_VIO_CMD_STATE_CLOSING; |
---|
280 | |
---|
281 | if ( state->child.opened ) |
---|
282 | roar_vio_cmd_wait(&(state->child)); |
---|
283 | |
---|
284 | if ( state->child.cmd != NULL ) |
---|
285 | roar_mm_free(state->child.cmd); |
---|
286 | |
---|
287 | roar_mm_free(state); |
---|
288 | |
---|
289 | return 0; |
---|
290 | } |
---|
291 | #endif |
---|
292 | |
---|
293 | |
---|
294 | // VIOs: |
---|
295 | |
---|
296 | ssize_t roar_vio_cmd_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
297 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
298 | fd_set rfhs[1], wfhs[1]; |
---|
299 | struct timeval tv; |
---|
300 | size_t done = 0; |
---|
301 | int max_fh; |
---|
302 | int ret; |
---|
303 | char tbuf[ROAR_VIO_CMD_BUFSIZE]; |
---|
304 | char * tp = NULL; |
---|
305 | ssize_t tlen = 0; |
---|
306 | int nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
307 | int in, out; |
---|
308 | int eating = 1; |
---|
309 | |
---|
310 | ROAR_DBG("roar_vio_cmd_read(*) = ?"); |
---|
311 | |
---|
312 | if ( !state->reader.opened ) { |
---|
313 | if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no reader is forked :) */ |
---|
314 | return 0; |
---|
315 | |
---|
316 | if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no reader exists? -> err */ |
---|
317 | return -1; |
---|
318 | |
---|
319 | if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) |
---|
320 | return -1; |
---|
321 | } |
---|
322 | |
---|
323 | in = state->reader.in; |
---|
324 | out = state->reader.out; |
---|
325 | |
---|
326 | while (done < count || eating) { |
---|
327 | if ( nonblock ) { |
---|
328 | tv.tv_sec = 0; |
---|
329 | tv.tv_usec = 1; |
---|
330 | } else { |
---|
331 | tv.tv_sec = 3600; |
---|
332 | tv.tv_usec = 0; |
---|
333 | } |
---|
334 | |
---|
335 | FD_ZERO(rfhs); |
---|
336 | FD_ZERO(wfhs); |
---|
337 | |
---|
338 | FD_SET(in, rfhs); |
---|
339 | |
---|
340 | if ( out != -1 ) { |
---|
341 | FD_SET(out, wfhs); |
---|
342 | } |
---|
343 | |
---|
344 | #ifdef DEBUG |
---|
345 | if ( FD_ISSET(in, rfhs) ) { |
---|
346 | ROAR_DBG("roar_vio_cmd_read(*): reader set in fh group"); |
---|
347 | } |
---|
348 | #endif |
---|
349 | |
---|
350 | max_fh = in > out ? in : out; |
---|
351 | ROAR_DBG("roar_vio_cmd_read(*): max_fh=%i", max_fh); |
---|
352 | |
---|
353 | if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 ) |
---|
354 | return -1; |
---|
355 | |
---|
356 | ROAR_DBG("roar_vio_cmd_read(*): select(*) = %i", ret); |
---|
357 | ROAR_DBG("roar_vio_cmd_read(*): reader=%i, writer=%i", in, out); |
---|
358 | |
---|
359 | if ( ret > 0 ) { |
---|
360 | if ( FD_ISSET(in, rfhs) ) { |
---|
361 | ROAR_DBG("roar_vio_cmd_read(*): event on reader"); |
---|
362 | |
---|
363 | if ( (ret = read(in, buf+done, count-done)) == -1 ) |
---|
364 | break; |
---|
365 | |
---|
366 | if ( ret == 0 ) |
---|
367 | break; |
---|
368 | |
---|
369 | done += ret; |
---|
370 | } |
---|
371 | |
---|
372 | if ( out != -1 && FD_ISSET(out, wfhs) ) { |
---|
373 | ROAR_DBG("roar_vio_cmd_read(*): event on writer"); |
---|
374 | eating = 1; |
---|
375 | |
---|
376 | if ( !tlen ) { |
---|
377 | tp = tbuf; |
---|
378 | tlen = 0; |
---|
379 | if ( (tlen = roar_vio_read(state->next, tp, ROAR_VIO_CMD_BUFSIZE)) == -1 ) |
---|
380 | continue; |
---|
381 | } |
---|
382 | |
---|
383 | if ( tlen ) { |
---|
384 | if ( (ret = write(out, tp, tlen)) > 0 ) { |
---|
385 | tlen -= ret; |
---|
386 | tp += ret; |
---|
387 | } |
---|
388 | } else { |
---|
389 | close(out); |
---|
390 | state->reader.out = out = -1; |
---|
391 | } |
---|
392 | } else { |
---|
393 | eating = 0; |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | if ( nonblock ) |
---|
398 | break; |
---|
399 | } |
---|
400 | |
---|
401 | if ( tlen ) { /* we have some data to write to the child... */ |
---|
402 | // TODO: try to write it out... |
---|
403 | return -1; |
---|
404 | } |
---|
405 | |
---|
406 | return done; |
---|
407 | } |
---|
408 | |
---|
409 | ssize_t roar_vio_cmd_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
410 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
411 | fd_set rfhs[1], wfhs[1]; |
---|
412 | struct timeval tv; |
---|
413 | size_t done = 0; |
---|
414 | int max_fh; |
---|
415 | int ret; |
---|
416 | char tbuf[ROAR_VIO_CMD_BUFSIZE]; |
---|
417 | int nonblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
418 | int in, out; |
---|
419 | |
---|
420 | if ( !state->writer.opened ) { |
---|
421 | if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no writer is forked :) */ |
---|
422 | return 0; |
---|
423 | |
---|
424 | if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no writer exists? -> err */ |
---|
425 | return -1; |
---|
426 | |
---|
427 | if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) |
---|
428 | return -1; |
---|
429 | } |
---|
430 | |
---|
431 | in = state->writer.in; |
---|
432 | out = state->writer.out; |
---|
433 | |
---|
434 | if ( buf == NULL ) { // we are requested to sync |
---|
435 | if ( in != -1 ) { |
---|
436 | ret = 1; |
---|
437 | done = 0; |
---|
438 | while (ret > 0) { |
---|
439 | if ( state->state == ROAR_VIO_CMD_STATE_CLOSING ) { |
---|
440 | tv.tv_sec = 3600; |
---|
441 | tv.tv_usec = 0; |
---|
442 | } else { |
---|
443 | tv.tv_sec = 0; |
---|
444 | tv.tv_usec = done ? 1 : 50000; // 50ms |
---|
445 | } |
---|
446 | |
---|
447 | done++; |
---|
448 | |
---|
449 | FD_ZERO(rfhs); |
---|
450 | FD_SET(in, rfhs); |
---|
451 | |
---|
452 | if ( select(in+1, rfhs, NULL, NULL, &tv) < 1 ) |
---|
453 | break; |
---|
454 | |
---|
455 | ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE); |
---|
456 | |
---|
457 | if ( roar_vio_write(state->next, tbuf, ret) != ret ) |
---|
458 | return -1; |
---|
459 | } |
---|
460 | } |
---|
461 | |
---|
462 | return 0; |
---|
463 | } |
---|
464 | |
---|
465 | while (done < count) { |
---|
466 | if ( nonblock ) { |
---|
467 | tv.tv_sec = 0; |
---|
468 | tv.tv_usec = 1; |
---|
469 | } else { |
---|
470 | tv.tv_sec = 3600; |
---|
471 | tv.tv_usec = 0; |
---|
472 | } |
---|
473 | |
---|
474 | FD_ZERO(rfhs); |
---|
475 | FD_ZERO(wfhs); |
---|
476 | |
---|
477 | FD_SET(out, wfhs); |
---|
478 | |
---|
479 | if ( in != -1 ) { |
---|
480 | FD_SET(in, rfhs); |
---|
481 | } |
---|
482 | |
---|
483 | max_fh = in > out ? in : out; |
---|
484 | |
---|
485 | if ( (ret = select(max_fh + 1, rfhs, wfhs, NULL, &tv)) == -1 ) |
---|
486 | return -1; |
---|
487 | |
---|
488 | if ( ret > 0 ) { |
---|
489 | if ( FD_ISSET(out, wfhs) ) { |
---|
490 | |
---|
491 | if ( (ret = write(out, buf+done, count-done)) == -1 ) |
---|
492 | break; |
---|
493 | |
---|
494 | if ( ret == 0 ) |
---|
495 | break; |
---|
496 | |
---|
497 | done += ret; |
---|
498 | } |
---|
499 | if ( in != -1 && FD_ISSET(in, rfhs) ) { |
---|
500 | if ( (ret = read(in, tbuf, ROAR_VIO_CMD_BUFSIZE)) == -1 ) { /* error case: can not read on reader -> EOF */ |
---|
501 | close(in); |
---|
502 | state->writer.in = in = -1; |
---|
503 | break; |
---|
504 | } |
---|
505 | |
---|
506 | if ( roar_vio_write(state->next, tbuf, ret) != ret ) |
---|
507 | return -1; |
---|
508 | } |
---|
509 | } |
---|
510 | |
---|
511 | if ( nonblock ) |
---|
512 | break; |
---|
513 | } |
---|
514 | |
---|
515 | return done; |
---|
516 | } |
---|
517 | |
---|
518 | int roar_vio_cmd_sync (struct roar_vio_calls * vio) { |
---|
519 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
520 | int newblock = ROAR_SOCKET_BLOCK; |
---|
521 | int oldblock; |
---|
522 | int ret = 0; |
---|
523 | |
---|
524 | oldblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK ? ROAR_SOCKET_NONBLOCK : ROAR_SOCKET_BLOCK; |
---|
525 | |
---|
526 | if ( roar_vio_cmd_ctl(vio, ROAR_VIO_CTL_NONBLOCK, &newblock) == -1 ) |
---|
527 | return -1; |
---|
528 | |
---|
529 | if ( roar_vio_cmd_write(vio, NULL, 0) == -1 ) |
---|
530 | ret = -1; |
---|
531 | |
---|
532 | if ( roar_vio_cmd_read(vio, NULL, 0) == -1 ) |
---|
533 | ret = -1; |
---|
534 | |
---|
535 | if ( roar_vio_cmd_ctl(vio, ROAR_VIO_CTL_NONBLOCK, &oldblock) == -1 ) |
---|
536 | return -1; |
---|
537 | |
---|
538 | return ret; |
---|
539 | } |
---|
540 | |
---|
541 | int roar_vio_cmd_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
542 | struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; |
---|
543 | char buf[1]; |
---|
544 | |
---|
545 | ROAR_DBG("roar_vio_cmd_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data); |
---|
546 | |
---|
547 | switch (cmd) { |
---|
548 | case ROAR_VIO_CTL_GET_NAME: |
---|
549 | if ( data == NULL ) |
---|
550 | return -1; |
---|
551 | |
---|
552 | *(char**)data = "cmd"; |
---|
553 | return 0; |
---|
554 | break; |
---|
555 | case ROAR_VIO_CTL_GET_NEXT: |
---|
556 | *(struct roar_vio_calls **)data = state->next; |
---|
557 | return -1; |
---|
558 | break; |
---|
559 | case ROAR_VIO_CTL_GET_FH: |
---|
560 | return -1; |
---|
561 | break; |
---|
562 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
563 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
564 | // if ( !state->reader.opened ) { |
---|
565 | //for (i = 0; i < 128; i++) |
---|
566 | roar_vio_cmd_read(vio, buf, 0); |
---|
567 | // } |
---|
568 | if ( state->reader.opened ) { |
---|
569 | ROAR_DBG("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); |
---|
570 | *(int*)data = state->reader.in; |
---|
571 | return 0; |
---|
572 | } |
---|
573 | return -1; |
---|
574 | break; |
---|
575 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
576 | return -1; |
---|
577 | break; |
---|
578 | case ROAR_VIO_CTL_NONBLOCK: |
---|
579 | state->options |= ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
580 | |
---|
581 | if ( *(int*)data == ROAR_SOCKET_BLOCK ) |
---|
582 | state->options -= ROAR_VIO_CMD_OPTS_NONBLOCK; |
---|
583 | |
---|
584 | _LIBROAR_IGNORE_RET(roar_vio_ctl(state->next, cmd, data)); // this should help, but may not necessarily. |
---|
585 | break; |
---|
586 | default: |
---|
587 | return -1; |
---|
588 | } |
---|
589 | |
---|
590 | return 0; |
---|
591 | } |
---|
592 | #endif |
---|
593 | |
---|
594 | // MISC: |
---|
595 | int roar_vio_open_gzip(struct roar_vio_calls * calls, struct roar_vio_calls * dst, int level) { |
---|
596 | roar_debug_warn_obsolete("roar_vio_open_gzip", "roar_vio_open_zlib", NULL); |
---|
597 | |
---|
598 | #ifdef ROAR_HAVE_LIBZ |
---|
599 | return roar_vio_open_zlib(calls, dst, level, 1); |
---|
600 | #else |
---|
601 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
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 |
---|