util: fix log file renaming of mrafs-style logs 20/12220/4
authorMichael Meffie <mmeffie@sinenomine.net>
Sun, 13 Mar 2016 21:27:59 +0000 (17:27 -0400)
committerBenjamin Kaduk <kaduk@mit.edu>
Mon, 25 Apr 2016 04:05:06 +0000 (00:05 -0400)
Do not make timestamped log files with an invalid number of seconds when
renaming old mrsafs-style log files, i.e., more than 59 seconds in the
seconds field.

Replace the goto used in the mrafs-style make file name retries with a
regular, bounded loop.

Change-Id: I16d032197e4b1e227b1f005fbc395a013e099561
Reviewed-on: https://gerrit.openafs.org/12220
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
Tested-by: BuildBot <buildbot@rampaginggeek.com>

src/util/serverLog.c

index e9ad82e..90dd452 100644 (file)
@@ -332,8 +332,6 @@ OpenLog(const char *fileName)
     int tempfd, isfifo = 0;
     int code;
     char *nextName = NULL;
-    struct timeval Start;
-    struct tm *TimeFields;
 
 #ifndef AFS_NT40_ENV
     struct stat statbuf;
@@ -362,24 +360,27 @@ OpenLog(const char *fileName)
     if (mrafsStyleLogs) {
         time_t t;
        struct stat buf;
-       gettimeofday(&Start, NULL);
-        t = Start.tv_sec;
-       TimeFields = localtime(&t);
-    makefilename:
-       code = asprintf(&nextName, "%s.%d%02d%02d%02d%02d%02d",
-                fileName, TimeFields->tm_year + 1900,
-                TimeFields->tm_mon + 1, TimeFields->tm_mday,
-                TimeFields->tm_hour, TimeFields->tm_min,
-                TimeFields->tm_sec);
-       if (code < 0) {
-           nextName = NULL;
-       } else if (lstat(nextName, &buf) == 0) {
-           /* avoid clobbering a log */
-           TimeFields->tm_sec++;
-           free(nextName);
-           nextName = NULL;
-           goto makefilename;
-       }
+       int tries;
+       struct tm *timeFields;
+
+       time(&t);
+       for (tries = 0; nextName == NULL && tries < 100; t++, tries++) {
+           timeFields = localtime(&t);
+           code = asprintf(&nextName, "%s.%d%02d%02d%02d%02d%02d",
+                    fileName, timeFields->tm_year + 1900,
+                    timeFields->tm_mon + 1, timeFields->tm_mday,
+                    timeFields->tm_hour, timeFields->tm_min,
+                    timeFields->tm_sec);
+           if (code < 0) {
+               nextName = NULL;
+               break;
+           }
+           if (lstat(nextName, &buf) == 0) {
+               /* Avoid clobbering a log. */
+               free(nextName);
+               nextName = NULL;
+           }
+        }
     } else {
        code = asprintf(&nextName, "%s.old", fileName);
        if (code < 0) {