]> granicus.if.org Git - strace/commitdiff
Clean up pid2tcb usage
authorRoland McGrath <roland@redhat.com>
Wed, 15 Sep 2010 01:59:20 +0000 (18:59 -0700)
committerRoland McGrath <roland@redhat.com>
Wed, 15 Sep 2010 02:03:37 +0000 (19:03 -0700)
* strace.c (pid2tcb): Always match pid.  Fail for argument <= 0.
[USE_PROCFS] (first_used_tcb): New function.
[USE_PROCFS] (trace): Use that instead of pid2tcb(0).

strace.c

index 3cb3758c04c2884ddb0dbb1666ae0a5e36e1e776..497b8d15ca5fb09f5b4b1eb08e854f6a2ef1266e 100644 (file)
--- a/strace.c
+++ b/strace.c
@@ -1337,24 +1337,37 @@ proc_open(struct tcb *tcp, int attaching)
 #endif /* USE_PROCFS */
 
 struct tcb *
-pid2tcb(pid)
-int pid;
+pid2tcb(int pid)
 {
        int i;
-       struct tcb *tcp;
+
+       if (pid <= 0)
+               return NULL;
 
        for (i = 0; i < tcbtabsize; i++) {
-               tcp = tcbtab[i];
-               if (pid && tcp->pid != pid)
-                       continue;
-               if (tcp->flags & TCB_INUSE)
+               struct tcb *tcp = tcbtab[i];
+               if (tcp->pid == pid && (tcp->flags & TCB_INUSE))
                        return tcp;
        }
+
        return NULL;
 }
 
 #ifdef USE_PROCFS
 
+static struct tcb *
+first_used_tcb(void)
+{
+       int i;
+       struct tcb *tcp;
+       for (i = 0; i < tcbtabsize; i++) {
+               tcp = tcbtab[i];
+               if (tcp->flags & TCB_INUSE)
+                       return tcp;
+       }
+       return NULL;
+}
+
 static struct tcb *
 pfd2tcb(pfd)
 int pfd;
@@ -1992,7 +2005,7 @@ trace()
 #ifndef HAVE_POLLABLE_PROCFS
                        if (proc_poll_pipe[0] == -1) {
 #endif
-                               tcp = pid2tcb(0);
+                               tcp = first_used_tcb();
                                if (!tcp)
                                        continue;
                                pfd = tcp->pfd;