[PATCH] exit/kill ourself with straced child's exitcode/signal

Dmitry V. Levin ldv at altlinux.org
Thu Oct 16 17:11:42 UTC 2008


On Tue, Sep 30, 2008 at 02:21:23AM +0400, Dmitry V. Levin wrote:
> On Wed, Sep 24, 2008 at 06:58:05PM +0200, Denys Vlasenko wrote:
> > This patch is intended to implement this RFE:
> > 
> > https://bugzilla.redhat.com/show_bug.cgi?id=105371
> > 
> > There are some patches for this, but apparently discussion
> > on mailing list died out with no results. At least that bug
> > is still open. Moreover, the patches are somewhat buggy.
> > 
> > I propose this patch.
> > 
> > I think in real-world usage people do not check strace usage in scripts. They
> > just use it correctly, so that it doesn't exit(1) with usage info and whatnot.
> > 
> > But I know that when I try to debug something by replacing "cmd [args]"
> > with "strace cmd [args]" the fact that it's sometimes a problem
> > that parent does not see exit code of cmd but sees zero.
> > 
> > IOW: I do not think that there are users who will be adversely affected by this
> > change in behavior. Therefore I do not think adding a switch to enable this is
> > worth it. It will be just a case of featuritis.
> > 
> > Problems this patch fixes compared to previous patches:
> > 
> > * strace returns exit code of straced process, *never its children*.
> > * If child died from a signal, strace will (try to) die from the same signal.
> > * strace -p <pid> is not affected (will exit 0 as before).
> 
> I think your idea how to implement this RFE is better than mine, so
> I'm OK to install your patch with minor cleanup.
> 
> Objections?

Attached the edition of this patch I'm going to commit
if there are no objections.


-- 
ldv
-------------- next part --------------
--- strace.c
+++ strace.c
@@ -85,6 +85,9 @@ static int iflag = 0, interactive = 0, pflag_seen = 0, rflag = 0, tflag = 0;
 /* Sometimes we want to print only succeeding syscalls. */
 int not_failing_only = 0;
 
+static int exit_code = 0;
+static int strace_child = 0;
+
 static char *username = NULL;
 uid_t run_uid;
 gid_t run_gid;
@@ -523,7 +526,8 @@ startup_child (char **argv)
 			progname, filename);
 		exit(1);
 	}
-	switch (pid = fork()) {
+	strace_child = pid = fork();
+	switch (pid) {
 	case -1:
 		perror("strace: fork");
 		cleanup();
@@ -879,7 +883,17 @@ main(int argc, char *argv[])
 	if (trace() < 0)
 		exit(1);
 	cleanup();
-	exit(0);
+	fflush(NULL);
+	if (exit_code > 0xff) {
+		/* Child was killed by a signal, mimic that.  */
+		exit_code &= 0xff;
+		signal(exit_code, SIG_DFL);
+		raise(exit_code);
+		/* Paranoia - what if this signal is not fatal?
+		   Exit with 128 + signo then.  */
+		exit_code += 128;
+	}
+	exit(exit_code);
 }
 
 int
@@ -1783,7 +1797,7 @@ int pfd;
 	switch (fork()) {
 	case -1:
 		perror("fork");
-		_exit(0);
+		_exit(1);
 	case 0:
 		break;
 	default:
@@ -1807,7 +1821,7 @@ int pfd;
 
 	if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
 		perror("getrlimit(RLIMIT_NOFILE, ...)");
-		_exit(0);
+		_exit(1);
 	}
 	n = rl.rlim_cur;
 	for (i = 0; i < n; i++) {
@@ -2322,6 +2336,8 @@ Process %d attached (waiting for parent)\n",
 			continue;
 		}
 		if (WIFSIGNALED(status)) {
+			if (pid == strace_child)
+				exit_code = 0x100 | WTERMSIG(status);
 			if (!cflag
 			    && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
 				printleader(tcp);
@@ -2341,6 +2357,8 @@ Process %d attached (waiting for parent)\n",
 			continue;
 		}
 		if (WIFEXITED(status)) {
+			if (pid == strace_child)
+				exit_code = WEXITSTATUS(status);
 			if (debug)
 				fprintf(stderr, "pid %u exited\n", pid);
 			if ((tcp->flags & TCB_ATTACHED)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20081016/7fd75e5d/attachment.bin>


More information about the Strace-devel mailing list