]> granicus.if.org Git - strace/commitdiff
Make strace_fopen abort on error
authorDenys Vlasenko <dvlasenk@redhat.com>
Wed, 22 Jun 2011 11:17:16 +0000 (13:17 +0200)
committerDenys Vlasenko <dvlasenk@redhat.com>
Wed, 22 Jun 2011 11:17:16 +0000 (13:17 +0200)
Error from strace_fopen in main results in call to exit(1).
Error from strace_fopen in newoutf is propagated to newoutf
callers: startup_attach (where it results in exit(1))
and alloc_tcb (where error is ignored). In second case,
the behavior doesn't seem to be right: it means with -ff
on open error for new LOGFILE.PID the output will continue
to go into *the same file as the previous process* - which
would be confusing. Moreover, on droptcb outf may be closed
and the output of other, still running process outputting
to the same outf will be lost. I don't think this is sane.
IOW: in all cases, error in strace_fopen should be fatal.

* strace.c (strace_fopen): Abort on error instead of returning NULL.
  (newoutf): Change return type to void.
  (startup_attach): Remove error check on newoutf return value.
  (main): Remove error check on strace_fopen return value.

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

index d353eacffb0ca5471574fd5fc7add60e4dc32136..bbb7ac17272d13474e70cc9d16d65a6fa3dff30c 100644 (file)
--- a/strace.c
+++ b/strace.c
@@ -333,17 +333,16 @@ swap_uid(void)
 #endif
 
 static FILE *
-strace_fopen(const char *path, const char *mode)
+strace_fopen(const char *path)
 {
        FILE *fp;
 
        swap_uid();
-       if ((fp = fopen_for_output(path, mode)) == NULL)
-               fprintf(stderr, "%s: can't fopen '%s': %s\n",
-                       progname, path, strerror(errno));
+       fp = fopen_for_output(path, "w");
+       if (!fp)
+               perror_msg_and_die("Can't fopen '%s'", path);
        swap_uid();
-       if (fp)
-               set_cloexec_flag(fileno(fp));
+       set_cloexec_flag(fileno(fp));
        return fp;
 }
 
@@ -395,19 +394,14 @@ strace_popen(const char *command)
        return fp;
 }
 
-static int
+static void
 newoutf(struct tcb *tcp)
 {
        if (outfname && followfork > 1) {
                char name[520 + sizeof(int) * 3];
-               FILE *fp;
-
                sprintf(name, "%.512s.%u", outfname, tcp->pid);
-               if ((fp = strace_fopen(name, "w")) == NULL)
-                       return -1;
-               tcp->outf = fp;
+               tcp->outf = strace_fopen(name);
        }
-       return 0;
 }
 
 static void
@@ -455,8 +449,7 @@ startup_attach(void)
 #endif
                /* Reinitialize the output since it may have changed. */
                tcp->outf = outf;
-               if (newoutf(tcp) < 0)
-                       exit(1);
+               newoutf(tcp);
 
 #ifdef USE_PROCFS
                if (proc_open(tcp, 1) < 0) {
@@ -1129,9 +1122,8 @@ main(int argc, char *argv[])
                                error_msg_and_die("Piping the output and -ff are mutually exclusive");
                        outf = strace_popen(outfname + 1);
                }
-               else if (followfork <= 1 &&
-                        (outf = strace_fopen(outfname, "w")) == NULL)
-                       exit(1);
+               else if (followfork <= 1)
+                       outf = strace_fopen(outfname);
        }
 
        if (!outfname || outfname[0] == '|' || outfname[0] == '!')