]> granicus.if.org Git - strace/commitdiff
Make set_cloexec_flag abort on error
authorDenys Vlasenko <dvlasenk@redhat.com>
Wed, 22 Jun 2011 11:11:23 +0000 (13:11 +0200)
committerDenys Vlasenko <dvlasenk@redhat.com>
Wed, 22 Jun 2011 11:11:23 +0000 (13:11 +0200)
set_cloexec_flag() may fail only if we pass it a bad fd,
such as -1 or non-opened one. If we do, we have a bug
in the caller. It makes no sense to try to continue
running when we detect such a blatant bug in our own code.

* strace (set_cloexec_flag): Abort instead of returning error
  indicator. Change function to return void.
  (strace_fopen): Remove error check on set_cloexec_flag return value.
  (proc_open): Likewise.
  (proc_poll_open): Likewise.

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
strace.c

index 4c91e51984895ce99decfdfe8e4359ae28e8a33e..d353eacffb0ca5471574fd5fc7add60e4dc32136 100644 (file)
--- a/strace.c
+++ b/strace.c
@@ -285,30 +285,26 @@ foobar()
 # define fork()         vfork()
 #endif
 
-static int
+static void
 set_cloexec_flag(int fd)
 {
-       int     flags, newflags;
-
-       if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
-       {
-               fprintf(stderr, "%s: fcntl F_GETFD: %s\n",
-                       progname, strerror(errno));
-               return -1;
+       int flags, newflags;
+
+       flags = fcntl(fd, F_GETFD);
+       if (flags < 0) {
+               /* Can happen only if fd is bad.
+                * Should never happen: if it does, we have a bug
+                * in the caller. Therefore we just abort
+                * instead of propagating the error.
+                */
+               perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
        }
 
        newflags = flags | FD_CLOEXEC;
        if (flags == newflags)
-               return 0;
-
-       if (fcntl(fd, F_SETFD, newflags) < 0)
-       {
-               fprintf(stderr, "%s: fcntl F_SETFD: %s\n",
-                       progname, strerror(errno));
-               return -1;
-       }
+               return;
 
-       return 0;
+       fcntl(fd, F_SETFD, newflags); /* never fails */
 }
 
 /*
@@ -346,11 +342,8 @@ strace_fopen(const char *path, const char *mode)
                fprintf(stderr, "%s: can't fopen '%s': %s\n",
                        progname, path, strerror(errno));
        swap_uid();
-       if (fp && set_cloexec_flag(fileno(fp)) < 0)
-       {
-               fclose(fp);
-               fp = NULL;
-       }
+       if (fp)
+               set_cloexec_flag(fileno(fp));
        return fp;
 }
 
@@ -1287,25 +1280,19 @@ proc_open(struct tcb *tcp, int attaching)
                perror("strace: open(\"/proc/...\", ...)");
                return -1;
        }
-       if (set_cloexec_flag(tcp->pfd) < 0) {
-               return -1;
-       }
+       set_cloexec_flag(tcp->pfd);
        sprintf(proc, "/proc/%d/status", tcp->pid);
        if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
                perror("strace: open(\"/proc/...\", ...)");
                return -1;
        }
-       if (set_cloexec_flag(tcp->pfd_stat) < 0) {
-               return -1;
-       }
+       set_cloexec_flag(tcp->pfd_stat);
        sprintf(proc, "/proc/%d/as", tcp->pid);
        if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
                perror("strace: open(\"/proc/...\", ...)");
                return -1;
        }
-       if (set_cloexec_flag(tcp->pfd_as) < 0) {
-               return -1;
-       }
+       set_cloexec_flag(tcp->pfd_as);
 #else
        /* Open the process pseudo-file in /proc. */
 #ifndef FREEBSD
@@ -1319,9 +1306,7 @@ proc_open(struct tcb *tcp, int attaching)
                perror("strace: open(\"/proc/...\", ...)");
                return -1;
        }
-       if (set_cloexec_flag(tcp->pfd) < 0) {
-               return -1;
-       }
+       set_cloexec_flag(tcp->pfd);
 #endif
 #ifdef FREEBSD
        sprintf(proc, "/proc/%d/regs", tcp->pid);
@@ -1931,9 +1916,7 @@ proc_poll_open(void)
                exit(1);
        }
        for (i = 0; i < 2; i++) {
-               if (set_cloexec_flag(proc_poll_pipe[i]) < 0) {
-                       exit(1);
-               }
+               set_cloexec_flag(proc_poll_pipe[i]);
        }
 }