From: Jeremy Fitzhardinge Date: Tue, 14 Oct 2003 22:13:28 +0000 (+0000) Subject: When creating a logfile name, add a sequence number to the name in case X-Git-Tag: svn/VALGRIND_2_1_0~132 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8f0884bbb618b1929af9c65113c802bb132939f2;p=thirdparty%2Fvalgrind.git When creating a logfile name, add a sequence number to the name in case a logfile for that pid already exists. This may happen for programs started during system boot which will tend to get the same pid each boot. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1928 --- diff --git a/coregrind/vg_errcontext.c b/coregrind/vg_errcontext.c index 96fe88a96c..3c76f05cbc 100644 --- a/coregrind/vg_errcontext.c +++ b/coregrind/vg_errcontext.c @@ -661,7 +661,7 @@ static void load_one_suppressions_file ( Char* filename ) Char* supp_name; fd = VG_(open)( filename, VKI_O_RDONLY, 0 ); - if (fd == -1) { + if (fd < 0) { VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file `%s'", filename ); VG_(exit)(1); diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c index 8680af0745..43a2315f00 100644 --- a/coregrind/vg_main.c +++ b/coregrind/vg_main.c @@ -1161,22 +1161,39 @@ static void process_cmd_line_options ( void ) case VgLogTo_File: { Char logfilename[1000]; + Int seq = 0; + Int pid = VG_(getpid)(); + vg_assert(VG_(clo_logfile_name) != NULL); vg_assert(VG_(strlen)(VG_(clo_logfile_name)) <= 900); /* paranoia */ - VG_(sprintf)(logfilename, "%s.pid%d", - VG_(clo_logfile_name), VG_(getpid)() ); - eventually_logfile_fd - = VG_(open)(logfilename, VKI_O_CREAT|VKI_O_WRONLY, - VKI_S_IRUSR|VKI_S_IWUSR); - if (eventually_logfile_fd != -1) { - VG_(clo_logfile_fd) = eventually_logfile_fd; - } else { - VG_(message)(Vg_UserMsg, - "Can't create/open log file `%s.pid%d'; giving up!", - VG_(clo_logfile_name), VG_(getpid)()); - VG_(bad_option)( - "--logfile= didn't work out for some reason."); - } + + for(;;) { + if (seq == 0) + VG_(sprintf)(logfilename, "%s.pid%d", + VG_(clo_logfile_name), pid ); + else + VG_(sprintf)(logfilename, "%s.pid%d.%d", + VG_(clo_logfile_name), pid, seq ); + seq++; + + eventually_logfile_fd + = VG_(open)(logfilename, + VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC, + VKI_S_IRUSR|VKI_S_IWUSR); + if (eventually_logfile_fd >= 0) { + VG_(clo_logfile_fd) = eventually_logfile_fd; + break; + } else { + if (eventually_logfile_fd != -VKI_EEXIST) { + VG_(message)(Vg_UserMsg, + "Can't create/open log file `%s.pid%d'; giving up!", + VG_(clo_logfile_name), pid); + VG_(bad_option)( + "--logfile= didn't work out for some reason."); + break; + } + } + } break; } diff --git a/coregrind/vg_mylibc.c b/coregrind/vg_mylibc.c index 811818f1a4..5aa4c095e9 100644 --- a/coregrind/vg_mylibc.c +++ b/coregrind/vg_mylibc.c @@ -1171,7 +1171,7 @@ Int VG_(open) ( const Char* pathname, Int flags, Int mode ) ok: */ fd = VG_(do_syscall)(__NR_open, (UInt)pathname, flags, mode); /* VG_(printf)("result = %d\n", fd); */ - if (VG_(is_kerror)(fd)) fd = -1; + /* return -ve error code */ return fd; } diff --git a/coregrind/vg_procselfmaps.c b/coregrind/vg_procselfmaps.c index 6350b564e1..33186f11fa 100644 --- a/coregrind/vg_procselfmaps.c +++ b/coregrind/vg_procselfmaps.c @@ -77,7 +77,7 @@ void VG_(read_procselfmaps)(void) /* Read the initial memory mapping from the /proc filesystem. */ fd = VG_(open) ( "/proc/self/maps", VKI_O_RDONLY, 0 ); - if (fd == -1) { + if (fd < 0) { VG_(message)(Vg_UserMsg, "FATAL: can't open /proc/self/maps"); VG_(exit)(1); } diff --git a/coregrind/vg_symtab2.c b/coregrind/vg_symtab2.c index 6af7713487..3a227e21b2 100644 --- a/coregrind/vg_symtab2.c +++ b/coregrind/vg_symtab2.c @@ -743,7 +743,7 @@ Bool vg_read_lib_symbols ( SegInfo* si ) n_oimage = stat_buf.st_size; fd = VG_(open)(si->filename, VKI_O_RDONLY, 0); - if (fd == -1) { + if (fd < 0) { VG_(symerr)("Can't open .so/.exe to read symbols?!"); return False; } diff --git a/include/vg_kerneliface.h b/include/vg_kerneliface.h index 9b281d260d..a12dfee4a9 100644 --- a/include/vg_kerneliface.h +++ b/include/vg_kerneliface.h @@ -359,6 +359,7 @@ struct vki_ucontext { #define VKI_EWOULDBLOCK VKI_EAGAIN /* Operation would block */ #define VKI_EAGAIN 11 /* Try again */ #define VKI_EFAULT 14 /* Bad address */ +#define VKI_EEXIST 17 /* File exists */ #define VKI_EINVAL 22 /* Invalid argument */ #define VKI_EMFILE 24 /* Too many open files */ #define VKI_ENOSYS 38 /* Function not implemented */