]> granicus.if.org Git - strace/blobdiff - wait.c
nlattr: add UID/GID netlink attribute decoders
[strace] / wait.c
diff --git a/wait.c b/wait.c
index 09341c52e47564a619b897253c07805fc5237853..c1d1dff24952b6be91feeb39232a9e3ccf0be834 100644 (file)
--- a/wait.c
+++ b/wait.c
@@ -1,4 +1,40 @@
+/*
+ * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
+ * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
+ * Copyright (c) 1993-1996 Rick Sladkey <jrs@world.std.com>
+ * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
+ * Copyright (c) 2002-2004 Roland McGrath <roland@redhat.com>
+ * Copyright (c) 2004 Ulrich Drepper <drepper@redhat.com>
+ * Copyright (c) 2009-2013 Denys Vlasenko <dvlasenk@redhat.com>
+ * Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
+ * Copyright (c) 2014-2018 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 #include "defs.h"
+#include "ptrace.h"
 
 #include <sys/wait.h>
 
@@ -23,7 +59,6 @@
 # define W_CONTINUED 0xffff
 #endif
 
-#include "ptrace.h"
 #include "xlat/ptrace_events.h"
 
 static int
@@ -42,14 +77,12 @@ printstatus(int status)
                        signame(sig & 0x7f),
                        sig & 0x80 ? " | 0x80" : "");
                status &= ~W_STOPCODE(sig);
-       }
-       else if (WIFSIGNALED(status)) {
+       } else if (WIFSIGNALED(status)) {
                tprintf("[{WIFSIGNALED(s) && WTERMSIG(s) == %s%s}",
                        signame(WTERMSIG(status)),
                        WCOREDUMP(status) ? " && WCOREDUMP(s)" : "");
                status &= ~(W_EXITCODE(0, WTERMSIG(status)) | WCOREFLAG);
-       }
-       else if (WIFEXITED(status)) {
+       } else if (WIFEXITED(status)) {
                tprintf("[{WIFEXITED(s) && WEXITSTATUS(s) == %d}",
                        WEXITSTATUS(status));
                exited = 1;
@@ -83,10 +116,9 @@ printstatus(int status)
 }
 
 static int
-printwaitn(struct tcb *tcp, int n, int bitness)
+printwaitn(struct tcb *const tcp,
+          void (*const print_rusage)(struct tcb *, kernel_ulong_t))
 {
-       int status;
-
        if (entering(tcp)) {
                /* On Linux, kernel-side pid_t is typedef'ed to int
                 * on all arches. Also, glibc-2.8 truncates wait3 and wait4
@@ -97,33 +129,23 @@ printwaitn(struct tcb *tcp, int n, int bitness)
                int pid = tcp->u_arg[0];
                tprintf("%d, ", pid);
        } else {
+               int status;
+
                /* status */
-               if (!tcp->u_arg[1])
-                       tprints("NULL");
-               else if (syserror(tcp) || tcp->u_rval == 0)
-                       tprintf("%#lx", tcp->u_arg[1]);
-               else if (umove(tcp, tcp->u_arg[1], &status) < 0)
-                       tprints("[?]");
-               else
+               if (tcp->u_rval == 0)
+                       printaddr(tcp->u_arg[1]);
+               else if (!umove_or_printaddr(tcp, tcp->u_arg[1], &status))
                        printstatus(status);
                /* options */
                tprints(", ");
                printflags(wait4_options, tcp->u_arg[2], "W???");
-               if (n == 4) {
-                       tprints(", ");
+               if (print_rusage) {
                        /* usage */
-                       if (!tcp->u_arg[3])
-                               tprints("NULL");
-                       else if (tcp->u_rval > 0) {
-#ifdef ALPHA
-                               if (bitness)
-                                       printrusage32(tcp, tcp->u_arg[3]);
-                               else
-#endif
-                                       printrusage(tcp, tcp->u_arg[3]);
-                       }
+                       tprints(", ");
+                       if (tcp->u_rval > 0)
+                               print_rusage(tcp, tcp->u_arg[3]);
                        else
-                               tprintf("%#lx", tcp->u_arg[3]);
+                               printaddr(tcp->u_arg[3]);
                }
        }
        return 0;
@@ -131,18 +153,18 @@ printwaitn(struct tcb *tcp, int n, int bitness)
 
 SYS_FUNC(waitpid)
 {
-       return printwaitn(tcp, 3, 0);
+       return printwaitn(tcp, NULL);
 }
 
 SYS_FUNC(wait4)
 {
-       return printwaitn(tcp, 4, 0);
+       return printwaitn(tcp, printrusage);
 }
 
 #ifdef ALPHA
 SYS_FUNC(osf_wait4)
 {
-       return printwaitn(tcp, 4, 1);
+       return printwaitn(tcp, printrusage32);
 }
 #endif
 
@@ -152,24 +174,17 @@ SYS_FUNC(waitid)
 {
        if (entering(tcp)) {
                printxval(waitid_types, tcp->u_arg[0], "P_???");
-               tprintf(", %ld, ", tcp->u_arg[1]);
-       }
-       else {
+               int pid = tcp->u_arg[1];
+               tprintf(", %d, ", pid);
+       else {
                /* siginfo */
                printsiginfo_at(tcp, tcp->u_arg[2]);
                /* options */
                tprints(", ");
                printflags(wait4_options, tcp->u_arg[3], "W???");
-               if (tcp->s_ent->nargs > 4) {
-                       /* usage */
-                       tprints(", ");
-                       if (!tcp->u_arg[4])
-                               tprints("NULL");
-                       else if (tcp->u_error)
-                               tprintf("%#lx", tcp->u_arg[4]);
-                       else
-                               printrusage(tcp, tcp->u_arg[4]);
-               }
+               /* usage */
+               tprints(", ");
+               printrusage(tcp, tcp->u_arg[4]);
        }
        return 0;
 }