salvageserver: segfault in DoSalvageVolume
[openafs.git] / src / vol / salvaged.c
index e053e62..f513ce3 100644 (file)
 #include <pthread.h>
 #endif
 
+extern int ClientMode;
 
 #if !defined(AFS_DEMAND_ATTACH_FS)
 #error "online salvager only supported for demand attach fileserver"
@@ -137,13 +138,13 @@ static pthread_cond_t worker_cv;
 static void * SalvageChildReaperThread(void *);
 static int DoSalvageVolume(struct SalvageQueueNode * node, int slot);
 
-static void SalvageServer(int argc, char **argv);
+static void SalvageServer(int argc, char **argv, struct logOptions *logopts);
 static void SalvageClient(VolumeId vid, char * pname);
 
 static int Reap_Child(char * prog, int * pid, int * status);
 
 static void * SalvageLogCleanupThread(void *);
-static int SalvageLogCleanup(int pid);
+static void SalvageLogCleanup(int pid);
 
 static void * SalvageLogScanningThread(void *);
 static void ScanLogs(struct rx_queue *log_watch_queue);
@@ -166,14 +167,37 @@ struct {
 
 #define DEFAULT_PARALLELISM 4 /* allow 4 parallel salvage workers by default */
 
+enum optionsList {
+    OPT_partition,
+    OPT_volumeid,
+    OPT_debug,
+    OPT_nowrite,
+    OPT_inodes,
+    OPT_oktozap,
+    OPT_rootinodes,
+    OPT_salvagedirs,
+    OPT_blockreads,
+    OPT_parallel,
+    OPT_tmpdir,
+    OPT_orphans,
+    OPT_syslog,
+    OPT_syslogfacility,
+    OPT_logfile,
+    OPT_client,
+    OPT_transarc_logs
+};
+
 static int
-handleit(struct cmd_syndesc *as, void *arock)
+handleit(struct cmd_syndesc *opts, void *arock)
 {
-    struct cmd_item *ti;
-    char pname[100], *temp;
+    char pname[100];
     afs_int32 seenpart = 0, seenvol = 0;
     VolumeId vid = 0;
     struct cmdline_rock *rock = (struct cmdline_rock *)arock;
+    char *optstring = NULL;
+    struct logOptions logopts;
+
+    memset(&logopts, 0, sizeof(logopts));
 
 #ifdef AFS_SGI_VNODE_GLUE
     if (afs_init_kernel_config(-1) < 0) {
@@ -183,26 +207,18 @@ handleit(struct cmd_syndesc *as, void *arock)
     }
 #endif
 
-    if (as->parms[2].items)    /* -debug */
-       debug = 1;
-    if (as->parms[3].items)    /* -nowrite */
-       Testing = 1;
-    if (as->parms[4].items)    /* -inodes */
-       ListInodeOption = 1;
-    if (as->parms[5].items)    /* -oktozap */
-       OKToZap = 1;
-    if (as->parms[6].items)    /* -rootinodes */
-       ShowRootFiles = 1;
-    if (as->parms[8].items)    /* -ForceReads */
-       forceR = 1;
-    if ((ti = as->parms[9].items)) {   /* -Parallel # */
-       temp = ti->data;
-       if (strncmp(temp, "all", 3) == 0) {
+    cmd_OptionAsFlag(opts, OPT_debug, &debug);
+    cmd_OptionAsFlag(opts, OPT_nowrite, &Testing);
+    cmd_OptionAsFlag(opts, OPT_inodes, &ListInodeOption);
+    cmd_OptionAsFlag(opts, OPT_oktozap, &OKToZap);
+    cmd_OptionAsFlag(opts, OPT_rootinodes, &ShowRootFiles);
+    cmd_OptionAsFlag(opts, OPT_blockreads, &forceR);
+    if (cmd_OptionAsString(opts, OPT_parallel, &optstring) == 0) {
+       if (strncmp(optstring, "all", 3) == 0) {
            PartsPerDisk = 1;
-           temp += 3;
        }
-       if (strlen(temp) != 0) {
-           Parallel = atoi(temp);
+       if (strlen(optstring) != 0) {
+           Parallel = atoi(optstring);
            if (Parallel < 1)
                Parallel = 1;
            if (Parallel > MAXPARALLEL) {
@@ -211,58 +227,77 @@ handleit(struct cmd_syndesc *as, void *arock)
                Parallel = MAXPARALLEL;
            }
        }
+       free(optstring);
+       optstring = NULL;
     } else {
        Parallel = min(DEFAULT_PARALLELISM, MAXPARALLEL);
     }
-    if ((ti = as->parms[10].items)) {  /* -tmpdir */
+    if (cmd_OptionAsString(opts, OPT_tmpdir, &optstring) == 0) {
        DIR *dirp;
-
-       tmpdir = ti->data;
-       dirp = opendir(tmpdir);
+       dirp = opendir(optstring);
        if (!dirp) {
            printf
                ("Can't open temporary placeholder dir %s; using current partition \n",
-                tmpdir);
+                optstring);
            tmpdir = NULL;
        } else
            closedir(dirp);
+       free(optstring);
+       optstring = NULL;
     }
-    if ((ti = as->parms[11].items))    /* -showlog */
-       ShowLog = 1;
-    if ((ti = as->parms[12].items)) {  /* -orphans */
+    if (cmd_OptionAsString(opts, OPT_orphans, &optstring) == 0) {
        if (Testing)
            orphans = ORPH_IGNORE;
-       else if (strcmp(ti->data, "remove") == 0
-                || strcmp(ti->data, "r") == 0)
+       else if (strcmp(optstring, "remove") == 0
+                || strcmp(optstring, "r") == 0)
            orphans = ORPH_REMOVE;
-       else if (strcmp(ti->data, "attach") == 0
-                || strcmp(ti->data, "a") == 0)
+       else if (strcmp(optstring, "attach") == 0
+                || strcmp(optstring, "a") == 0)
            orphans = ORPH_ATTACH;
-    }
-#ifndef AFS_NT40_ENV           /* ignore options on NT */
-    if ((ti = as->parms[13].items)) {  /* -syslog */
-       useSyslog = 1;
-       ShowLog = 0;
-    }
-    if ((ti = as->parms[14].items)) {  /* -syslogfacility */
-       useSyslogFacility = atoi(ti->data);
+       free(optstring);
+       optstring = NULL;
     }
 
-    if ((ti = as->parms[15].items)) {  /* -datelogs */
-       TimeStampLogFile((char *)AFSDIR_SERVER_SALSRVLOG_FILEPATH);
-    }
+#ifdef HAVE_SYSLOG
+    if (cmd_OptionPresent(opts, OPT_syslog)) {
+       if (cmd_OptionPresent(opts, OPT_logfile)) {
+           fprintf(stderr, "Invalid options: -syslog and -logfile are exclusive.\n");
+           return -1;
+       }
+       if (cmd_OptionPresent(opts, OPT_transarc_logs)) {
+           fprintf(stderr, "Invalid options: -syslog and -transarc-logs are exclusive.\n");
+           return -1;
+       }
+       logopts.lopt_dest = logDest_syslog;
+       logopts.lopt_facility = LOG_DAEMON;
+       logopts.lopt_tag = "salvageserver";
+       cmd_OptionAsInt(opts, OPT_syslogfacility, &logopts.lopt_facility);
+    } else
 #endif
+    {
+       logopts.lopt_dest = logDest_file;
+       if (cmd_OptionPresent(opts, OPT_transarc_logs)) {
+           logopts.lopt_rotateOnOpen = 1;
+           logopts.lopt_rotateStyle = logRotate_old;
+       }
+       if (cmd_OptionPresent(opts, OPT_logfile))
+           cmd_OptionAsString(opts, OPT_logfile, (char**)&logopts.lopt_filename);
+       else
+           logopts.lopt_filename = AFSDIR_SERVER_SALSRVLOG_FILEPATH;
+    }
 
-    if ((ti = as->parms[16].items)) {   /* -client */
-       if ((ti = as->parms[0].items)) {        /* -partition */
+    if (cmd_OptionPresent(opts, OPT_client)) {
+       if (cmd_OptionAsString(opts, OPT_partition, &optstring) == 0) {
            seenpart = 1;
-           strlcpy(pname, ti->data, sizeof(pname));
+           strlcpy(pname, optstring, sizeof(pname));
+           free(optstring);
+           optstring = NULL;
        }
-       if ((ti = as->parms[1].items)) {        /* -volumeid */
+       if (cmd_OptionAsString(opts, OPT_volumeid, &optstring) == 0) {
            char *end;
            unsigned long vid_l;
            seenvol = 1;
-           vid_l = strtoul(ti->data, &end, 10);
+           vid_l = strtoul(optstring, &end, 10);
            if (vid_l >= MAX_AFS_UINT32 || vid_l == ULONG_MAX || *end != '\0') {
                printf("Invalid volume id specified; salvage aborted\n");
                exit(-1);
@@ -270,11 +305,6 @@ handleit(struct cmd_syndesc *as, void *arock)
            vid = (VolumeId)vid_l;
        }
 
-       if (ShowLog) {
-           printf("-showlog does not work with -client\n");
-           exit(-1);
-       }
-
        if (!seenpart || !seenvol) {
            printf("You must specify '-partition' and '-volumeid' with the '-client' option\n");
            exit(-1);
@@ -283,7 +313,7 @@ handleit(struct cmd_syndesc *as, void *arock)
        SalvageClient(vid, pname);
 
     } else {  /* salvageserver mode */
-       SalvageServer(rock->argc, rock->argv);
+       SalvageServer(rock->argc, rock->argv, &logopts);
     }
     return (0);
 }
@@ -362,46 +392,49 @@ main(int argc, char **argv)
     arock.argc = argc;
     arock.argv = argv;
 
-    ts = cmd_CreateSyntax("initcmd", handleit, &arock, "initialize the program");
-    cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL,
-               "Name of partition to salvage");
-    cmd_AddParm(ts, "-volumeid", CMD_SINGLE, CMD_OPTIONAL,
-               "Volume Id to salvage");
-    cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
-               "Run in Debugging mode");
-    cmd_AddParm(ts, "-nowrite", CMD_FLAG, CMD_OPTIONAL,
-               "Run readonly/test mode");
-    cmd_AddParm(ts, "-inodes", CMD_FLAG, CMD_OPTIONAL,
-               "Just list affected afs inodes - debugging flag");
-    cmd_AddParm(ts, "-oktozap", CMD_FLAG, CMD_OPTIONAL,
-               "Give permission to destroy bogus inodes/volumes - debugging flag");
-    cmd_AddParm(ts, "-rootinodes", CMD_FLAG, CMD_OPTIONAL,
-               "Show inodes owned by root - debugging flag");
-    cmd_AddParm(ts, "-salvagedirs", CMD_FLAG, CMD_OPTIONAL,
-               "Force rebuild/salvage of all directories");
-    cmd_AddParm(ts, "-blockreads", CMD_FLAG, CMD_OPTIONAL,
-               "Read smaller blocks to handle IO/bad blocks");
-    cmd_AddParm(ts, "-parallel", CMD_SINGLE, CMD_OPTIONAL,
-               "# of max parallel partition salvaging");
-    cmd_AddParm(ts, "-tmpdir", CMD_SINGLE, CMD_OPTIONAL,
-               "Name of dir to place tmp files ");
-    cmd_AddParm(ts, "-showlog", CMD_FLAG, CMD_OPTIONAL,
-               "Show log file upon completion");
-    cmd_AddParm(ts, "-orphans", CMD_SINGLE, CMD_OPTIONAL,
-               "ignore | remove | attach");
-
-    /* note - syslog isn't avail on NT, but if we make it conditional, have
-     * to deal with screwy offsets for cmd params */
-    cmd_AddParm(ts, "-syslog", CMD_FLAG, CMD_OPTIONAL,
-               "Write salvage log to syslogs");
-    cmd_AddParm(ts, "-syslogfacility", CMD_SINGLE, CMD_OPTIONAL,
-               "Syslog facility number to use");
-    cmd_AddParm(ts, "-datelogs", CMD_FLAG, CMD_OPTIONAL,
-               "Include timestamp in logfile filename");
-
-    cmd_AddParm(ts, "-client", CMD_FLAG, CMD_OPTIONAL,
+
+    ts = cmd_CreateSyntax("initcmd", handleit, &arock, 0, "initialize the program");
+    cmd_AddParmAtOffset(ts, OPT_partition, "-partition", CMD_SINGLE,
+           CMD_OPTIONAL, "Name of partition to salvage");
+    cmd_AddParmAtOffset(ts, OPT_volumeid, "-volumeid", CMD_SINGLE, CMD_OPTIONAL,
+           "Volume Id to salvage");
+    cmd_AddParmAtOffset(ts, OPT_debug, "-debug", CMD_FLAG, CMD_OPTIONAL,
+           "Run in Debugging mode");
+    cmd_AddParmAtOffset(ts, OPT_nowrite, "-nowrite", CMD_FLAG, CMD_OPTIONAL,
+           "Run readonly/test mode");
+    cmd_AddParmAtOffset(ts, OPT_inodes, "-inodes", CMD_FLAG, CMD_OPTIONAL,
+           "Just list affected afs inodes - debugging flag");
+    cmd_AddParmAtOffset(ts, OPT_oktozap, "-oktozap", CMD_FLAG, CMD_OPTIONAL,
+           "Give permission to destroy bogus inodes/volumes - debugging flag");
+    cmd_AddParmAtOffset(ts, OPT_rootinodes, "-rootinodes", CMD_FLAG,
+           CMD_OPTIONAL, "Show inodes owned by root - debugging flag");
+    cmd_AddParmAtOffset(ts, OPT_salvagedirs, "-salvagedirs", CMD_FLAG,
+           CMD_OPTIONAL, "Force rebuild/salvage of all directories");
+    cmd_AddParmAtOffset(ts, OPT_blockreads, "-blockreads", CMD_FLAG,
+           CMD_OPTIONAL, "Read smaller blocks to handle IO/bad blocks");
+    cmd_AddParmAtOffset(ts, OPT_parallel, "-parallel", CMD_SINGLE, CMD_OPTIONAL,
+           "# of max parallel partition salvaging");
+    cmd_AddParmAtOffset(ts, OPT_tmpdir, "-tmpdir", CMD_SINGLE, CMD_OPTIONAL,
+           "Name of dir to place tmp files ");
+    cmd_AddParmAtOffset(ts, OPT_orphans, "-orphans", CMD_SINGLE, CMD_OPTIONAL,
+           "ignore | remove | attach");
+
+#ifdef HAVE_SYSLOG
+    cmd_AddParmAtOffset(ts, OPT_syslog, "-syslog", CMD_FLAG, CMD_OPTIONAL,
+           "Write salvage log to syslogs");
+    cmd_AddParmAtOffset(ts, OPT_syslogfacility, "-syslogfacility", CMD_SINGLE,
+           CMD_OPTIONAL, "Syslog facility number to use");
+#endif
+
+    cmd_AddParmAtOffset(ts, OPT_client, "-client", CMD_FLAG, CMD_OPTIONAL,
                "Use SALVSYNC to ask salvageserver to salvage a volume");
 
+    cmd_AddParmAtOffset(ts, OPT_logfile, "-logfile", CMD_SINGLE, CMD_OPTIONAL,
+           "Location of log file ");
+
+    cmd_AddParmAtOffset(ts, OPT_transarc_logs, "-transarc-logs", CMD_FLAG,
+                       CMD_OPTIONAL, "enable Transarc style logging");
+
     err = cmd_Dispatch(argc, argv);
     Exit(err);
     return 0; /* not reached */
@@ -416,6 +449,9 @@ SalvageClient(VolumeId vid, char * pname)
     SALVSYNC_response_hdr sres;
     VolumePackageOptions opts;
 
+    /* Send Log() messages to stderr in client mode. */
+    ClientMode = 1;
+
     VOptDefaults(volumeUtility, &opts);
     if (VInitVolumePackage2(volumeUtility, &opts)) {
        /* VInitVolumePackage2 can fail on e.g. partition attachment errors,
@@ -467,7 +503,7 @@ SalvageClient(VolumeId vid, char * pname)
 static int * child_slot;
 
 static void
-SalvageServer(int argc, char **argv)
+SalvageServer(int argc, char **argv, struct logOptions *logopts)
 {
     int pid, ret;
     struct SalvageQueueNode * node;
@@ -479,18 +515,10 @@ SalvageServer(int argc, char **argv)
     /* All entries to the log will be appended.  Useful if there are
      * multiple salvagers appending to the log.
      */
+    OpenLog(logopts);
+    SetupLogSignals();
 
-    CheckLogFile((char *)AFSDIR_SERVER_SALSRVLOG_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);
-
-    fprintf(logFile, "%s\n", cml_version_number);
+    Log("%s\n", cml_version_number);
     LogCommandLine(argc, argv, "Online Salvage Server",
                   SalvageVersion, "Starting OpenAFS", Log);
     /* Get and hold a lock for the duration of the salvage to make sure
@@ -581,24 +609,29 @@ SalvageServer(int argc, char **argv)
 static int
 DoSalvageVolume(struct SalvageQueueNode * node, int slot)
 {
-    char childLog[AFSDIR_PATH_MAX];
+    char *filename = NULL;
+    struct logOptions logopts;
     struct DiskPartition64 * partP;
 
     /* do not allow further forking inside salvager */
     canfork = 0;
 
-    /* do not attempt to close parent's logFile handle as
-     * another thread may have held the lock on the FILE
-     * structure when fork was called! */
-
-    snprintf(childLog, sizeof(childLog), "%s.%d",
-            AFSDIR_SERVER_SLVGLOG_FILEPATH, getpid());
-
-    logFile = afs_fopen(childLog, "a");
-    if (!logFile) {            /* still nothing, use stdout */
-       logFile = stdout;
-       ShowLog = 0;
+    /*
+     * Do not attempt to close parent's log file handle as
+     * another thread may have held the lock when fork was
+     * called!
+     */
+    memset(&logopts, 0, sizeof(logopts));
+    logopts.lopt_dest = logDest_file;
+    logopts.lopt_rotateStyle = logRotate_none;
+    if (asprintf(&filename, "%s.%d",
+                AFSDIR_SERVER_SLVGLOG_FILEPATH, getpid()) < 0) {
+       fprintf(stderr, "out of memory\n");
+       return ENOMEM;
     }
+    logopts.lopt_filename = filename;
+    OpenLog(&logopts);
+    free(filename);
 
     if (node->command.sop.parent <= 0) {
        Log("salvageServer: invalid volume id specified; salvage aborted\n");
@@ -620,7 +653,7 @@ DoSalvageVolume(struct SalvageQueueNode * node, int slot)
     /* Salvage individual volume; don't notify fs */
     SalvageFileSys1(partP, node->command.sop.parent);
 
-    fclose(logFile);
+    CloseLog();
     return 0;
 }
 
@@ -728,31 +761,40 @@ SalvageLogCleanupThread(void * arg)
 }
 
 #define LOG_XFER_BUF_SIZE 65536
-static int
+static void
 SalvageLogCleanup(int pid)
 {
     int pidlog, len;
-    char fn[AFSDIR_PATH_MAX];
-    static char buf[LOG_XFER_BUF_SIZE];
+    char *fn = NULL;
+    char *buf = NULL;
 
-    snprintf(fn, sizeof(fn), "%s.%d",
-            AFSDIR_SERVER_SLVGLOG_FILEPATH, pid);
+    if (asprintf(&fn, "%s.%d", AFSDIR_SERVER_SLVGLOG_FILEPATH, pid) < 0) {
+       Log("Unable to write child log: out of memory\n");
+       goto done;
+    }
 
+    buf = calloc(1, LOG_XFER_BUF_SIZE);
+    if (buf != NULL) {
+       Log("Unable to write child log: out of memory\n");
+       goto done;
+    }
 
     pidlog = open(fn, O_RDONLY);
     unlink(fn);
     if (pidlog < 0)
-       return 1;
+       goto done;
 
     len = read(pidlog, buf, LOG_XFER_BUF_SIZE);
     while (len) {
-       fwrite(buf, len, 1, logFile);
+       WriteLogBuffer(buf, len);
        len = read(pidlog, buf, LOG_XFER_BUF_SIZE);
     }
 
     close(pidlog);
 
-    return 0;
+ done:
+    free(fn);
+    free(buf);
 }
 
 /* wake up every five minutes to see if a non-child salvage has finished */
@@ -772,17 +814,15 @@ static void *
 SalvageLogScanningThread(void * arg)
 {
     struct rx_queue log_watch_queue;
+    char *prefix;
+    int prefix_len;
 
     queue_Init(&log_watch_queue);
 
-    {
+    prefix_len = asprintf(&prefix, "%s.", AFSDIR_SLVGLOG_FILE);
+    if (prefix_len >= 0) {
        DIR *dp;
        struct dirent *dirp;
-       char prefix[AFSDIR_PATH_MAX];
-       size_t prefix_len;
-
-       snprintf(prefix, sizeof(prefix), "%s.", AFSDIR_SLVGLOG_FILE);
-       prefix_len = strlen(prefix);
 
        dp = opendir(AFSDIR_LOGS_DIR);
        opr_Assert(dp);
@@ -824,7 +864,7 @@ SalvageLogScanningThread(void * arg)
 
            queue_Append(&log_watch_queue, cleanup);
        }
-
+       free(prefix);
        closedir(dp);
     }