summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <dvlasenk@redhat.com>2013-07-02 11:31:24 +0200
committerDenys Vlasenko <dvlasenk@redhat.com>2013-07-02 11:31:24 +0200
commit519af5ad34fa19fb48e2bc6539135fe0852210c3 (patch)
treeaae46d4b30d6950c075c8e253875680ca15eb527
parentd0ffdf494ac722f3d121c6e807f105000409e6a6 (diff)
downloadstrace-519af5ad34fa19fb48e2bc6539135fe0852210c3.tar.gz
strace-519af5ad34fa19fb48e2bc6539135fe0852210c3.tar.bz2
strace-519af5ad34fa19fb48e2bc6539135fe0852210c3.tar.xz
Replace suspicious popen_pid assignment with an obviously correct one
popen_pid = vfork() does work correctly, but for a subtle reason that wrong assignment of 0 happens in the child _first_, and _then_ correct value overwrites it in the parent. (And in a hyphothetical system where vfork = fork, popen_pid wouldn't be shared, so it will also be ok.) However, it's not necessary to be difficult. This change makes it so that assignment is done only in parent. Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
-rw-r--r--strace.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/strace.c b/strace.c
index 4c70b98..2741201 100644
--- a/strace.c
+++ b/strace.c
@@ -485,6 +485,7 @@ static FILE *
strace_popen(const char *command)
{
FILE *fp;
+ int pid;
int fds[2];
swap_uid();
@@ -493,11 +494,11 @@ strace_popen(const char *command)
set_cloexec_flag(fds[1]); /* never fails */
- popen_pid = vfork();
- if (popen_pid == -1)
+ pid = vfork();
+ if (pid < 0)
perror_msg_and_die("vfork");
- if (popen_pid == 0) {
+ if (pid == 0) {
/* child */
close(fds[1]);
if (fds[0] != 0) {
@@ -510,6 +511,7 @@ strace_popen(const char *command)
}
/* parent */
+ popen_pid = pid;
close(fds[0]);
swap_uid();
fp = fdopen(fds[1], "w");