]> granicus.if.org Git - strace/blobdiff - pathtrace.c
mips n32: fix preadv/pwritev offset decoding
[strace] / pathtrace.c
index 95643c673ae8ab2526b0d08fe4de915833aa95eb..28fc7c9ad8773c82c16bcc206a4514da922b6ec3 100644 (file)
 
 #include "defs.h"
 #include <sys/param.h>
-#ifdef HAVE_POLL_H
+#if defined HAVE_POLL_H
 # include <poll.h>
-#endif
-#ifdef HAVE_SYS_POLL_H
+#elif defined HAVE_SYS_POLL_H
 # include <sys/poll.h>
 #endif
 
@@ -73,14 +72,15 @@ upathmatch(struct tcb *tcp, unsigned long upath)
 static int
 fdmatch(struct tcb *tcp, int fd)
 {
-       const char *path = getfdpath(tcp, fd);
+       char path[PATH_MAX + 1];
+       int n = getfdpath(tcp, fd, path, sizeof(path));
 
-       return path && pathmatch(path);
+       return n >= 0 && pathmatch(path);
 }
 
 /*
  * Add a path to the set we're tracing.
- * Secifying NULL will delete all paths.
+ * Specifying NULL will delete all paths.
  */
 static void
 storepath(const char *path)
@@ -100,22 +100,24 @@ storepath(const char *path)
 /*
  * Get path associated with fd.
  */
-const char *
-getfdpath(struct tcb *tcp, int fd)
+int
+getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
 {
-       static char path[PATH_MAX+1];
        char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
        ssize_t n;
 
        if (fd < 0)
-               return NULL;
+               return -1;
 
        sprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
-       n = readlink(linkpath, path, (sizeof path) - 1);
-       if (n <= 0)
-               return NULL;
-       path[n] = '\0';
-       return path;
+       n = readlink(linkpath, buf, bufsize - 1);
+       /*
+        * NB: if buf is too small, readlink doesn't fail,
+        * it returns truncated result (IOW: n == bufsize - 1).
+        */
+       if (n >= 0)
+               buf[n] = '\0';
+       return n;
 }
 
 /*
@@ -156,7 +158,7 @@ pathtrace_match(struct tcb *tcp)
 
        s = tcp->s_ent;
 
-       if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC)))
+       if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC | TRACE_NETWORK)))
                return 0;
 
        /*
@@ -179,7 +181,6 @@ pathtrace_match(struct tcb *tcp)
            s->sys_func == sys_faccessat ||
            s->sys_func == sys_fchmodat ||
            s->sys_func == sys_futimesat ||
-           s->sys_func == sys_mkdirat ||
            s->sys_func == sys_unlinkat ||
            s->sys_func == sys_newfstatat ||
            s->sys_func == sys_mknodat ||
@@ -209,6 +210,7 @@ pathtrace_match(struct tcb *tcp)
        }
 
        if (s->sys_func == sys_renameat ||
+           s->sys_func == sys_renameat2 ||
            s->sys_func == sys_linkat)
        {
                /* fd, path, fd, path */
@@ -249,16 +251,23 @@ pathtrace_match(struct tcb *tcp)
                return fdmatch(tcp, tcp->u_arg[2]);
        }
 
+       if (s->sys_func == sys_fanotify_mark) {
+               /* x, x, x, fd, path */
+               return fdmatch(tcp, tcp->u_arg[3]) ||
+                       upathmatch(tcp, tcp->u_arg[4]);
+       }
+
        if (s->sys_func == sys_select ||
            s->sys_func == sys_oldselect ||
            s->sys_func == sys_pselect6)
        {
                int     i, j;
-               unsigned nfds;
+               int     nfds;
                long   *args, oldargs[5];
                unsigned fdsize;
                fd_set *fds;
 
+               args = tcp->u_arg;
                if (s->sys_func == sys_oldselect) {
                        if (umoven(tcp, tcp->u_arg[0], sizeof oldargs,
                                   (char*) oldargs) < 0)
@@ -267,17 +276,17 @@ pathtrace_match(struct tcb *tcp)
                                return 0;
                        }
                        args = oldargs;
-               } else
-                       args = tcp->u_arg;
+               }
 
-               nfds = args[0];
+               /* Kernel truncates arg[0] to int, we do the same. */
+               nfds = (int) args[0];
+               /* Kernel rejects negative nfds, so we don't parse it either. */
+               if (nfds <= 0)
+                       return 0;
                /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
-               if (args[0] > 1024*1024)
+               if (nfds > 1024*1024)
                        nfds = 1024*1024;
-               if (args[0] < 0)
-                       nfds = 0;
-               fdsize = ((((nfds + 7) / 8) + sizeof(long) - 1)
-                         & -sizeof(long));
+               fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
                fds = malloc(fdsize);
                if (!fds)
                        die_out_of_memory();
@@ -285,17 +294,19 @@ pathtrace_match(struct tcb *tcp)
                for (i = 1; i <= 3; ++i) {
                        if (args[i] == 0)
                                continue;
-
                        if (umoven(tcp, args[i], fdsize, (char *) fds) < 0) {
                                fprintf(stderr, "umoven() failed\n");
                                continue;
                        }
-
-                       for (j = 0; j < nfds; ++j)
-                               if (FD_ISSET(j, fds) && fdmatch(tcp, j)) {
+                       for (j = 0;; j++) {
+                               j = next_set_bit(fds, j, nfds);
+                               if (j < 0)
+                                       break;
+                               if (fdmatch(tcp, j)) {
                                        free(fds);
                                        return 1;
                                }
+                       }
                }
                free(fds);
                return 0;
@@ -334,11 +345,13 @@ pathtrace_match(struct tcb *tcp)
            s->sys_func == sys_timerfd_settime ||
            s->sys_func == sys_timerfd_gettime ||
            s->sys_func == sys_epoll_create ||
-           strcmp(s->sys_name, "fanotify_init") == 0)
+           s->sys_func == sys_socket ||
+           s->sys_func == sys_socketpair ||
+           s->sys_func == sys_fanotify_init)
        {
                /*
-                * These have TRACE_FILE or TRACE_DESCRIPTOR set, but they
-                * don't have any file descriptor or path args to test.
+                * These have TRACE_FILE or TRACE_DESCRIPTOR or TRACE_NETWORK set,
+                * but they don't have any file descriptor or path args to test.
                 */
                return 0;
        }
@@ -351,7 +364,7 @@ pathtrace_match(struct tcb *tcp)
        if (s->sys_flags & TRACE_FILE)
                return upathmatch(tcp, tcp->u_arg[0]);
 
-       if (s->sys_flags & TRACE_DESC)
+       if (s->sys_flags & (TRACE_DESC | TRACE_NETWORK))
                return fdmatch(tcp, tcp->u_arg[0]);
 
        return 0;