aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2017-09-27 14:13:02 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2017-09-27 14:13:02 +0300
commit0145de1db25c3840346b9cd9ea44e571812e5516 (patch)
tree92c29f344764a54ce07d99d487321896bec53162
parent80134405a63517bda8964f7993d096dc1df247c1 (diff)
downloadruncap-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.c45
-rw-r--r--t/pipe.at2
-rw-r--r--t/stdin.at2
3 files changed, 19 insertions, 30 deletions
diff --git a/runcap.c b/runcap.c
index f1e9410..26654f8 100644
--- a/runcap.c
+++ b/runcap.c
@@ -26,9 +26,14 @@
26 26
27#include "runcap.h" 27#include "runcap.h"
28 28
29static int 29int
30stream_capture_alloc(struct stream_capture *cap, size_t size) 30stream_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
48int
49stream_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
62static void 53static void
63stream_capture_reset(struct stream_capture *cap) 54stream_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
diff --git a/t/pipe.at b/t/pipe.at
index 3b31f3f..974afa7 100644
--- a/t/pipe.at
+++ b/t/pipe.at
@@ -16,7 +16,7 @@
16 16
17AT_SETUP([pipe stdin]) 17AT_SETUP([pipe stdin])
18AT_KEYWORDS([pipe]) 18AT_KEYWORDS([pipe])
19AT_CHECK([rt -n all -i -f $INPUT -- genout -l 337 -o - -s 628 -l 734 -e $INPUT 19AT_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
diff --git a/t/stdin.at b/t/stdin.at
index 36f684c..4556145 100644
--- a/t/stdin.at
+++ b/t/stdin.at
@@ -16,7 +16,7 @@
16 16
17AT_SETUP([supply stdin]) 17AT_SETUP([supply stdin])
18AT_KEYWORDS([stdin]) 18AT_KEYWORDS([stdin])
19AT_CHECK([rt -n all -f $INPUT -- genout -l 337 -o - -s 628 -l 734 -e $INPUT 19AT_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

Return to:

Send suggestions and report system problems to the System administrator.