salvager: redd up showlog global flag
[openafs.git] / src / vol / vol-salvage.c
index f961d9c..4a7b2eb 100644 (file)
@@ -281,6 +281,8 @@ char *tmpdir = NULL;
 
 
 /* Forward declarations */
+static void QuietExit(int) AFS_NORETURN;
+static void SalvageShowLog(void);
 static int IsVnodeOrphaned(struct SalvInfo *salvinfo, VnodeId vnode);
 static int AskVolumeSummary(struct SalvInfo *salvinfo,
                             VolumeId singleVolumeNumber);
@@ -608,7 +610,7 @@ SalvageFileSysParallel(struct DiskPartition64 *partP)
            } else {
                int fd;
 
-               ShowLog = 0;
+               ShowLog = 0; /* Child processes do not display. */
                for (fd = 0; fd < 16; fd++)
                    close(fd);
                open(OS_DIRSEP, 0);
@@ -663,8 +665,7 @@ SalvageFileSys(struct DiskPartition64 *partP, VolumeId singleVolumeNumber)
     if (!canfork || debug || Fork() == 0) {
        SalvageFileSys1(partP, singleVolumeNumber);
        if (canfork && !debug) {
-           ShowLog = 0;
-           Exit(0);
+           QuietExit(0);
        }
     } else
        Wait("SalvageFileSys");
@@ -1304,8 +1305,7 @@ GetInodeSummary(struct SalvInfo *salvinfo, FD_t inodeFile, VolumeId singleVolume
            goto error;
        }
        if (canfork && !debug) {
-           ShowLog = 0;
-           Exit(0);
+           QuietExit(0);
        }
     } else {
        if (Wait("Inode summary") == -1) {
@@ -2192,8 +2192,7 @@ DoSalvageVolumeGroup(struct SalvInfo *salvinfo, struct InodeSummary *isp, int nV
     IH_RELEASE(salvinfo->VGLinkH);
 
     if (canfork && !debug) {
-       ShowLog = 0;
-       Exit(0);
+       QuietExit(0);
     }
 }
 
@@ -4763,12 +4762,9 @@ Fork(void)
     return f;
 }
 
-void
-Exit(int code)
+static void
+QuietExit(int code)
 {
-    if (ShowLog)
-       showlog();
-
 #ifdef AFS_DEMAND_ATTACH_FS
     if (programType == salvageServer) {
        /* release all volume locks before closing down our SYNC channels.
@@ -4802,6 +4798,14 @@ Exit(int code)
 #endif
 }
 
+void
+Exit(int code)
+{
+    SalvageShowLog();
+    QuietExit(code);
+}
+
+
 int
 Wait(char *prog)
 {
@@ -4832,24 +4836,23 @@ TimeStamp(time_t clock, int precision)
 void
 CheckLogFile(char * log_path)
 {
-    char oldSlvgLog[AFSDIR_PATH_MAX];
+    char *oldSlvgLog;
 
 #ifndef AFS_NT40_ENV
     if (useSyslog) {
-       ShowLog = 0;
        return;
     }
 #endif
 
-    strcpy(oldSlvgLog, log_path);
-    strcat(oldSlvgLog, ".old");
     if (!logFile) {
-       rk_rename(log_path, oldSlvgLog);
+       if (asprintf(&oldSlvgLog, "%s.old", log_path) >= 0) {
+           rk_rename(log_path, oldSlvgLog);
+           free(oldSlvgLog);
+       }
        logFile = afs_fopen(log_path, "a");
 
        if (!logFile) {         /* still nothing, use stdout */
            logFile = stdout;
-           ShowLog = 0;
        }
 #ifndef AFS_NAMEI_ENV
        AFS_DEBUG_IOPS_LOG(logFile);
@@ -4861,29 +4864,34 @@ CheckLogFile(char * log_path)
 void
 TimeStampLogFile(char * log_path)
 {
-    char stampSlvgLog[AFSDIR_PATH_MAX];
+    char *stampSlvgLog;
     struct tm *lt;
     time_t now;
 
     now = time(0);
     lt = localtime(&now);
-    snprintf(stampSlvgLog, sizeof stampSlvgLog,
-            "%s.%04d-%02d-%02d.%02d:%02d:%02d", log_path,
-            lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour,
-            lt->tm_min, lt->tm_sec);
-
-    /* try to link the logfile to a timestamped filename */
-    /* if it fails, oh well, nothing we can do */
-    if (link(log_path, stampSlvgLog))
-       ; /* oh well */
+    if (asprintf(&stampSlvgLog,
+                "%s.%04d-%02d-%02d.%02d:%02d:%02d", log_path,
+                lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour,
+                lt->tm_min, lt->tm_sec) >= 0) {
+       /* try to link the logfile to a timestamped filename */
+       /* if it fails, oh well, nothing we can do */
+       if (link(log_path, stampSlvgLog))
+           ; /* oh well */
+       free(stampSlvgLog);
+    }
 }
 #endif
 
-void
-showlog(void)
+static void
+SalvageShowLog(void)
 {
     char line[256];
 
+    if (ShowLog == 0 || logFile == stdout || logFile == stderr) {
+       return;
+    }
+
 #ifndef AFS_NT40_ENV
     if (useSyslog) {
        printf("Can't show log since using syslog.\n");
@@ -4948,13 +4956,12 @@ Abort(const char *format, ...)
        if (logFile) {
            fprintf(logFile, "%s", tmp);
            fflush(logFile);
-           if (ShowLog)
-               showlog();
+           SalvageShowLog();
        }
 
     if (debug)
        abort();
-    Exit(1);
+    QuietExit(1);
 }
 
 char *
@@ -5039,7 +5046,7 @@ int
 nt_SetupPartitionSalvage(void *datap, int len)
 {
     childJob_t *jobp = (childJob_t *) datap;
-    char logname[AFSDIR_PATH_MAX];
+    char *logname;
 
     if (len != sizeof(childJob_t))
        return -1;
@@ -5048,9 +5055,11 @@ nt_SetupPartitionSalvage(void *datap, int len)
     myjob = *jobp;
 
     /* Open logFile */
-    (void)sprintf(logname, "%s.%d", AFSDIR_SERVER_SLVGLOG_FILEPATH,
-                 myjob.cj_number);
+    if (asprintf(&logname, "%s.%d", AFSDIR_SERVER_SLVGLOG_FILEPATH,
+                myjob.cj_number) < 0)
+       return -1;
     logFile = afs_fopen(logname, "w");
+    free(logname);
     if (!logFile)
        logFile = stdout;