salvager: convert salvager and salvagerserver to libutil logging
[openafs.git] / src / vol / salvager.c
index 88d0ec1..532e7b8 100644 (file)
 #include <afs/procmgmt.h>
 #include <roken.h>
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <dirent.h>
-#include <sys/stat.h>
-#include <time.h>
-#include <errno.h>
+#ifdef HAVE_SYS_FILE_H
+#include <sys/file.h>
+#endif
+
 #ifdef AFS_NT40_ENV
-#include <io.h>
 #include <WINNT/afsevent.h>
-#else
-#include <sys/param.h>
-#include <sys/file.h>
-#ifndef ITIMER_REAL
-#include <sys/time.h>
-#endif /* ITIMER_REAL */
 #endif
+
 #ifndef WCOREDUMP
 #define WCOREDUMP(x)   ((x) & 0200)
 #endif
+
 #include <rx/xdr.h>
 #include <afs/afsint.h>
-#include <afs/afs_assert.h>
 #if !defined(AFS_SGI_ENV) && !defined(AFS_NT40_ENV)
 #if defined(AFS_VFSINCL_ENV)
 #include <sys/vnode.h>
@@ -63,7 +54,7 @@
 #ifdef AFS_OSF_ENV
 #include <ufs/inode.h>
 #else /* AFS_OSF_ENV */
-#if !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV) && !defined(AFS_ARM_DARWIN_ENV)
+#if !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV) && !defined(AFS_DARWIN_ENV)
 #include <sys/inode.h>
 #endif
 #endif
 #include <sys/lockf.h>
 #else
 #ifdef AFS_HPUX_ENV
-#include <unistd.h>
 #include <checklist.h>
 #else
 #if defined(AFS_SGI_ENV)
-#include <unistd.h>
-#include <fcntl.h>
 #include <mntent.h>
 #else
 #if    defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
 #ifdef   AFS_SUN5_ENV
-#include <unistd.h>
 #include <sys/mnttab.h>
 #include <sys/mntent.h>
 #else
@@ -95,7 +82,6 @@
 #endif /* AFS_HPUX_ENV */
 #endif
 #endif
-#include <fcntl.h>
 #ifndef AFS_NT40_ENV
 #include <afs/osi_inode.h>
 #endif
 #include <afs/dir.h>
 #include <afs/afsutil.h>
 #include <afs/fileutil.h>
-#ifndef AFS_NT40_ENV
-#include <syslog.h>
-#endif
+#include <rx/rx_queue.h>
 
 #include "nfs.h"
 #include "lwp.h"
 pthread_t main_thread;
 #endif
 
+extern char *ShowLogFilename;
+extern char cml_version_number[];
 static int get_salvage_lock = 0;
 
+struct CmdLine {
+   int argc;
+   char **argv;
+};
+
+#ifndef AFS_NT40_ENV
+static int
+TimeStampLogFile(char **logfile)
+{
+    char *stampSlvgLog;
+    struct tm *lt;
+    time_t now;
+
+    now = time(0);
+    lt = localtime(&now);
+    if (asprintf(&stampSlvgLog,
+                "%s.%04d-%02d-%02d.%02d:%02d:%02d",
+                AFSDIR_SERVER_SLVGLOG_FILEPATH,
+                lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour,
+                lt->tm_min, lt->tm_sec) < 0) {
+       return ENOMEM;
+    }
+    free(*logfile); /* free the default name */
+    *logfile = stampSlvgLog;
+    return 0;
+}
+#endif
+
 static int
 handleit(struct cmd_syndesc *as, void *arock)
 {
+    struct CmdLine *cmdline = (struct CmdLine*)arock;
     struct cmd_item *ti;
     char pname[100], *temp;
     afs_int32 seenpart = 0, seenvol = 0;
     VolumeId vid = 0;
     ProgramType pt;
+    char *logfile = strdup(AFSDIR_SERVER_SLVGLOG_FILEPATH);
 
 #ifdef FAST_RESTART
     afs_int32  seenany = 0;
@@ -164,14 +180,9 @@ handleit(struct cmd_syndesc *as, void *arock)
        }
     }
     if (!seenany) {
-       char *msg =
-           "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
-
-       if (useSyslog)
-           Log(msg);
-       else
-           printf("%s\n", msg);
-
+       printf
+           ("Exiting immediately without salvage. "
+            "Look into the FileLog to find volumes which really need to be salvaged!\n");
        Exit(0);
     }
 #endif /* FAST_RESTART */
@@ -181,17 +192,19 @@ handleit(struct cmd_syndesc *as, void *arock)
     }
     if ((ti = as->parms[1].items)) {   /* -volumeid */
        char *end;
+       unsigned long vid_l;
        if (!seenpart) {
            printf
                ("You must also specify '-partition' option with the '-volumeid' option\n");
            exit(-1);
        }
        seenvol = 1;
-       vid = strtoul(ti->data, &end, 10);
-       if (vid == ULONG_MAX || *end != '\0') {
+       vid_l = strtoul(ti->data, &end, 10);
+       if (vid_l >= MAX_AFS_UINT32 || vid_l == ULONG_MAX || *end != '\0') {
            Log("salvage: invalid volume id specified; salvage aborted\n");
            Exit(1);
        }
+       vid = (VolumeId)vid_l;
     }
     if (as->parms[2].items)    /* -debug */
        debug = 1;
@@ -199,7 +212,7 @@ handleit(struct cmd_syndesc *as, void *arock)
        Testing = 1;
     if (as->parms[4].items)    /* -inodes */
        ListInodeOption = 1;
-    if (as->parms[5].items || as->parms[20].items)     /* -force, -f */
+    if (as->parms[5].items || as->parms[21].items)     /* -force, -f */
        ForceSalvage = 1;
     if (as->parms[6].items)    /* -oktozap */
        OKToZap = 1;
@@ -241,7 +254,7 @@ handleit(struct cmd_syndesc *as, void *arock)
     }
     if ((ti = as->parms[12].items))    /* -showlog */
        ShowLog = 1;
-    if ((ti = as->parms[13].items)) {  /* -log */
+    if ((ti = as->parms[13].items)) {  /* -showsuid */
        Testing = 1;
        ShowSuid = 1;
        Showmode = 1;
@@ -263,46 +276,42 @@ handleit(struct cmd_syndesc *as, void *arock)
     }
 #ifndef AFS_NT40_ENV           /* ignore options on NT */
     if ((ti = as->parms[16].items)) {  /* -syslog */
-       useSyslog = 1;
-       ShowLog = 0;
+       if (ShowLog) {
+           fprintf(stderr, "Invalid options: -syslog and -showlog are exclusive.\n");
+           Exit(1);
+       }
+       serverLogSyslog = 1;
     }
     if ((ti = as->parms[17].items)) {  /* -syslogfacility */
-       useSyslogFacility = atoi(ti->data);
+       serverLogSyslogFacility = atoi(ti->data);
     }
 
     if ((ti = as->parms[18].items)) {  /* -datelogs */
-      TimeStampLogFile((char *)AFSDIR_SERVER_SLVGLOG_FILEPATH);
+       int code = TimeStampLogFile(&logfile);
+       if (code != 0) {
+           fprintf(stderr, "Failed to format log file name for -datelogs; code=%d\n", code);
+           Exit(code);
+       }
+       ShowLogFilename = logfile;
     }
 #endif
 
+    OpenLog(logfile);
+    SetupLogSignals();
+
+    Log("%s\n", cml_version_number);
+    LogCommandLine(cmdline->argc, cmdline->argv, "SALVAGER", SalvageVersion, "STARTING AFS", Log);
+
 #ifdef FAST_RESTART
     if (ti = as->parms[19].items) {    /* -DontSalvage */
        char *msg =
            "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
-
-       if (useSyslog)
-           Log(msg);
-       else
-           printf("%s\n", msg);
+       Log("%s\n", msg);
+       printf("%s\n", msg);
        Exit(0);
     }
-#elif defined(DEMAND_ATTACH_ENABLE)
-    if (seenvol && !as->parms[19].items) {
-       char * msg =
-           "The standalone salvager cannot be run concurrently with a Demand Attach Fileserver.  Please use 'salvageserver -client <partition> <volume id>' to manually schedule volume salvages with the salvageserver (new versions of 'bos salvage' automatically do this for you).  Or, if you insist on using the standalone salvager, add the -forceDAFS flag to your salvager command line.";
-
-       if (useSyslog)
-           Log(msg);
-       else
-           printf("%s\n", msg);
-       Exit(1);
-    }
 #endif
 
-    if (get_salvage_lock) {
-       ObtainSalvageLock();
-    }
-
     /* Note:  if seenvol we initialize this as a standard volume utility:  this has the
      * implication that the file server may be running; negotations have to be made with
      * the file server in this case to take the read write volume and associated read-only
@@ -329,6 +338,44 @@ handleit(struct cmd_syndesc *as, void *arock)
        Log("errors encountered initializing volume package; salvage aborted\n");
        Exit(1);
     }
+
+    /* defer lock until we init volume package */
+    if (get_salvage_lock) {
+       if (seenvol && AskDAFS()) /* support forceDAFS */
+           ObtainSharedSalvageLock();
+       else
+           ObtainSalvageLock();
+    }
+
+    /*
+     * Ok to defer this as Exit will clean up and no real work is done
+     * init'ing volume package
+     */
+    if (seenvol) {
+       char *msg = NULL;
+#ifdef AFS_DEMAND_ATTACH_FS
+       if (!AskDAFS()) {
+           msg =
+               "The DAFS dasalvager cannot be run with a non-DAFS fileserver.  Please use 'salvager'.";
+       }
+       if (!msg && !as->parms[20].items) {
+           msg =
+               "The standalone salvager cannot be run concurrently with a Demand Attach Fileserver.  Please use 'salvageserver -client <partition> <volume id>' to manually schedule volume salvages with the salvageserver (new versions of 'bos salvage' automatically do this for you).  Or, if you insist on using the standalone salvager, add the -forceDAFS flag to your salvager command line.";
+       }
+#else
+       if (AskDAFS()) {
+           msg =
+               "The non-DAFS salvager cannot be run with a Demand Attach Fileserver.  Please use 'salvageserver -client <partition> <volume id>' to manually schedule volume salvages with the salvageserver (new versions of 'bos salvage' automatically do this for you).  Or, if you insist on using the standalone salvager, run dasalvager with the -forceDAFS flag.";
+       }
+#endif
+
+       if (msg) {
+           Log("%s\n", msg);
+           printf("%s\n", msg);
+           Exit(1);
+       }
+    }
+
     DInit(10);
 #ifdef AFS_NT40_ENV
     if (myjob.cj_number != NOT_CHILD) {
@@ -367,11 +414,10 @@ handleit(struct cmd_syndesc *as, void *arock)
 int
 main(int argc, char **argv)
 {
+    struct CmdLine cmdline;
     struct cmd_syndesc *ts;
     int err = 0;
 
-    extern char cml_version_number[];
-
 #ifdef AFS_AIX32_ENV
     /*
      * The following signal action for AIX is necessary so that in case of a
@@ -410,19 +456,6 @@ main(int argc, char **argv)
            exit(3);
     } else {
 #endif
-       /* All entries to the log will be appended.  Useful if there are
-        * multiple salvagers appending to the log.
-        */
-
-       CheckLogFile((char *)AFSDIR_SERVER_SLVGLOG_FILEPATH);
-#ifndef AFS_NT40_ENV
-#ifdef AFS_LINUX20_ENV
-       fcntl(fileno(logFile), F_SETFL, O_APPEND);      /* Isn't this redundant? */
-#else
-       fcntl(fileno(logFile), F_SETFL, FAPPEND);       /* Isn't this redundant? */
-#endif
-#endif
-       setlinebuf(logFile);
 
 #ifndef AFS_NT40_ENV
        if (geteuid() != 0) {
@@ -432,12 +465,6 @@ main(int argc, char **argv)
        }
 #endif
 
-       /* bad for normal help flag processing, but can do nada */
-
-       fprintf(logFile, "%s\n", cml_version_number);
-       LogCommandLine(argc, argv, "SALVAGER", SalvageVersion, "STARTING AFS",
-                      Log);
-
        /* Get and hold a lock for the duration of the salvage to make sure
         * that no other salvage runs at the same time.  The routine
         * VInitVolumePackage2 (called below) makes sure that a file server or
@@ -448,7 +475,9 @@ main(int argc, char **argv)
     }
 #endif
 
-    ts = cmd_CreateSyntax("initcmd", handleit, NULL, "initialize the program");
+    cmdline.argc = argc;
+    cmdline.argv = argv;
+    ts = cmd_CreateSyntax("initcmd", handleit, &cmdline, 0, "initialize the program");
     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL,
                "Name of partition to salvage");
     cmd_AddParm(ts, "-volumeid", CMD_SINGLE, CMD_OPTIONAL,
@@ -492,11 +521,12 @@ main(int argc, char **argv)
 #ifdef FAST_RESTART
     cmd_AddParm(ts, "-DontSalvage", CMD_FLAG, CMD_OPTIONAL,
                "Don't salvage. This my be set in BosConfig to let the fileserver restart immediately after a crash. Bad volumes will be taken offline");
-#elif defined(DEMAND_ATTACH_ENABLE)
+#elif defined(AFS_DEMAND_ATTACH_FS)
+    cmd_Seek(ts, 20); /* skip DontSalvage */
     cmd_AddParm(ts, "-forceDAFS", CMD_FLAG, CMD_OPTIONAL,
                "For Demand Attach Fileserver, permit a manual volume salvage outside of the salvageserver");
 #endif /* FAST_RESTART */
-    cmd_Seek(ts, 20);
+    cmd_Seek(ts, 21); /* skip DontSalvage and forceDAFS if needed */
     cmd_AddParm(ts, "-f", CMD_FLAG, CMD_OPTIONAL, "Alias for -force");
     err = cmd_Dispatch(argc, argv);
     Exit(err);