diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-09-27 14:13:02 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-09-27 14:13:02 +0300 |
commit | 0145de1db25c3840346b9cd9ea44e571812e5516 (patch) | |
tree | 92c29f344764a54ce07d99d487321896bec53162 | |
parent | 80134405a63517bda8964f7993d096dc1df247c1 (diff) | |
download | runcap-0145de1db25c3840346b9cd9ea44e571812e5516.tar.gz runcap-0145de1db25c3840346b9cd9ea44e571812e5516.tar.bz2 |
Avoid closing stdin.
When given the RCF_STDIN flag, runcap_init failed to initialize
rc_cap[RUNCAP_STDIN].sc_storfd. It remained initialized to 0 which
eventually led to stdin being inadvertenlty closed by runcap_free.
* runcap.c (stream_capture_alloc): Merge with stream_capture_init.
All callers updated.
(runcap_init): Initialize rc_cap[RUNCAP_STDIN].sc_storfd to -1
to prevent it from being closed.
* t/pipe.at: Set 5 seconds timeout.
* t/stdin.at: Likewise.
-rw-r--r-- | runcap.c | 45 | ||||
-rw-r--r-- | t/pipe.at | 2 | ||||
-rw-r--r-- | t/stdin.at | 2 |
3 files changed, 19 insertions, 30 deletions
@@ -26,9 +26,14 @@ | |||
26 | 26 | ||
27 | #include "runcap.h" | 27 | #include "runcap.h" |
28 | 28 | ||
29 | static int | 29 | int |
30 | stream_capture_alloc(struct stream_capture *cap, size_t size) | 30 | stream_capture_init(struct stream_capture *cap, size_t size) |
31 | { | 31 | { |
32 | if (!cap) { | ||
33 | errno = EINVAL; | ||
34 | return -1; | ||
35 | } | ||
36 | |||
32 | if (size) { | 37 | if (size) { |
33 | cap->sc_base = malloc(size); | 38 | cap->sc_base = malloc(size); |
34 | if (!cap->sc_base) | 39 | if (!cap->sc_base) |
@@ -45,20 +50,6 @@ stream_capture_alloc(struct stream_capture *cap, size_t size) | |||
45 | return 0; | 50 | return 0; |
46 | } | 51 | } |
47 | 52 | ||
48 | int | ||
49 | stream_capture_init(struct stream_capture *cap, size_t size) | ||
50 | { | ||
51 | if (!cap) { | ||
52 | errno = EINVAL; | ||
53 | return -1; | ||
54 | } | ||
55 | |||
56 | if (stream_capture_alloc(cap, size)) | ||
57 | return -1; | ||
58 | |||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | static void | 53 | static void |
63 | stream_capture_reset(struct stream_capture *cap) | 54 | stream_capture_reset(struct stream_capture *cap) |
64 | { | 55 | { |
@@ -434,6 +425,7 @@ runcap_init(struct runcap *rc, int flags) | |||
434 | rc->rc_program = NULL; | 425 | rc->rc_program = NULL; |
435 | if (!(flags & RCF_TIMEOUT)) | 426 | if (!(flags & RCF_TIMEOUT)) |
436 | rc->rc_timeout = 0; | 427 | rc->rc_timeout = 0; |
428 | |||
437 | if (flags & RCF_STDIN) { | 429 | if (flags & RCF_STDIN) { |
438 | if (rc->rc_cap[RUNCAP_STDIN].sc_size > 0 | 430 | if (rc->rc_cap[RUNCAP_STDIN].sc_size > 0 |
439 | && rc->rc_cap[RUNCAP_STDIN].sc_fd != -1) { | 431 | && rc->rc_cap[RUNCAP_STDIN].sc_fd != -1) { |
@@ -443,15 +435,14 @@ runcap_init(struct runcap *rc, int flags) | |||
443 | rc->rc_cap[RUNCAP_STDIN].sc_level = | 435 | rc->rc_cap[RUNCAP_STDIN].sc_level = |
444 | rc->rc_cap[RUNCAP_STDIN].sc_size; | 436 | rc->rc_cap[RUNCAP_STDIN].sc_size; |
445 | rc->rc_cap[RUNCAP_STDIN].sc_cur = 0; | 437 | rc->rc_cap[RUNCAP_STDIN].sc_cur = 0; |
438 | rc->rc_cap[RUNCAP_STDIN].sc_storfd = -1; | ||
446 | } else if (stream_capture_init(&rc->rc_cap[RUNCAP_STDIN], 0)) | 439 | } else if (stream_capture_init(&rc->rc_cap[RUNCAP_STDIN], 0)) |
447 | return -1; | 440 | return -1; |
448 | 441 | ||
449 | if (flags & RCF_STDOUT_SIZE) | 442 | res = stream_capture_init(&rc->rc_cap[RUNCAP_STDOUT], |
450 | res = stream_capture_alloc(&rc->rc_cap[RUNCAP_STDOUT], | 443 | (flags & RCF_STDOUT_SIZE) |
451 | rc->rc_cap[RUNCAP_STDOUT].sc_size); | 444 | ? rc->rc_cap[RUNCAP_STDOUT].sc_size |
452 | else | 445 | : STRCAP_BUFSIZE); |
453 | res = stream_capture_init(&rc->rc_cap[RUNCAP_STDOUT], | ||
454 | STRCAP_BUFSIZE); | ||
455 | if (res) | 446 | if (res) |
456 | return res; | 447 | return res; |
457 | 448 | ||
@@ -460,12 +451,10 @@ runcap_init(struct runcap *rc, int flags) | |||
460 | rc->rc_cap[RUNCAP_STDOUT].sc_monarg = NULL; | 451 | rc->rc_cap[RUNCAP_STDOUT].sc_monarg = NULL; |
461 | } | 452 | } |
462 | 453 | ||
463 | if (flags & RCF_STDERR_SIZE) | 454 | res = stream_capture_init(&rc->rc_cap[RUNCAP_STDERR], |
464 | res = stream_capture_alloc(&rc->rc_cap[RUNCAP_STDERR], | 455 | (flags & RCF_STDERR_SIZE) |
465 | rc->rc_cap[RUNCAP_STDERR].sc_size); | 456 | ? rc->rc_cap[RUNCAP_STDERR].sc_size |
466 | else | 457 | : STRCAP_BUFSIZE); |
467 | res = stream_capture_init(&rc->rc_cap[RUNCAP_STDERR], | ||
468 | STRCAP_BUFSIZE); | ||
469 | if (res) | 458 | if (res) |
470 | return res; | 459 | return res; |
471 | 460 | ||
@@ -16,7 +16,7 @@ | |||
16 | 16 | ||
17 | AT_SETUP([pipe stdin]) | 17 | AT_SETUP([pipe stdin]) |
18 | AT_KEYWORDS([pipe]) | 18 | AT_KEYWORDS([pipe]) |
19 | AT_CHECK([rt -n all -i -f $INPUT -- genout -l 337 -o - -s 628 -l 734 -e $INPUT | 19 | AT_CHECK([rt -n all -t 5 -i -f $INPUT -- genout -l 337 -o - -s 628 -l 734 -e $INPUT |
20 | ], | 20 | ], |
21 | [0], | 21 | [0], |
22 | [res=0 | 22 | [res=0 |
@@ -16,7 +16,7 @@ | |||
16 | 16 | ||
17 | AT_SETUP([supply stdin]) | 17 | AT_SETUP([supply stdin]) |
18 | AT_KEYWORDS([stdin]) | 18 | AT_KEYWORDS([stdin]) |
19 | AT_CHECK([rt -n all -f $INPUT -- genout -l 337 -o - -s 628 -l 734 -e $INPUT | 19 | AT_CHECK([rt -n all -t 5 -f $INPUT -- genout -l 337 -o - -s 628 -l 734 -e $INPUT |
20 | ], | 20 | ], |
21 | [0], | 21 | [0], |
22 | [res=0 | 22 | [res=0 |