Imported Upstream version 2.5.1
[debian/amanda] / server-src / amflush.c
index 1cd9749056612f0d29476a1e2e2a80c303cb137f..32bfe1cafa3b38a798878cccd712288ed93397c8 100644 (file)
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: amflush.c,v 1.80 2006/01/14 04:37:19 paddy_s Exp $
+ * $Id: amflush.c,v 1.95 2006/07/25 21:41:24 martinea Exp $
  *
  * write files from work directory onto tape
  */
@@ -46,20 +46,23 @@ char *driver_program;
 char *reporter_program;
 char *logroll_program;
 char *datestamp;
+char *amflush_timestamp;
+char *amflush_datestamp;
 sl_t *datestamp_list;
 
 /* local functions */
-int main P((int main_argc, char **main_argv));
-void flush_holdingdisk P((char *diskdir, char *datestamp));
-void confirm P((void));
-void redirect_stderr P((void));
-void detach P((void));
-void run_dumps P((void));
-static int get_letter_from_user P((void));
-
-int main(main_argc, main_argv)
-int main_argc;
-char **main_argv;
+int main(int main_argc, char **main_argv);
+void flush_holdingdisk(char *diskdir, char *datestamp);
+void confirm(void);
+void redirect_stderr(void);
+void detach(void);
+void run_dumps(void);
+static int get_letter_from_user(void);
+
+int
+main(
+    int                main_argc,
+    char **    main_argv)
 {
     int foreground;
     int batch;
@@ -72,6 +75,7 @@ char **main_argv;
     char *conf_diskfile;
     char *conf_tapelist;
     char *conf_logfile;
+    int conf_usetimestamps;
     disklist_t diskq;
     disk_t *dp;
     pid_t pid;
@@ -84,12 +88,18 @@ char **main_argv;
     int driver_pipe[2];
     char date_string[100];
     time_t today;
+    int    new_argc,   my_argc;
+    char **new_argv, **my_argv;
+    char *errstr;
+    struct tm *tm;
 
     safe_fd(-1, 0);
     safe_cd();
 
     set_pname("amflush");
 
+    dbopen(DBG_SUBDIR_SERVER);
+
     /* Don't die when child closes pipe */
     signal(SIGPIPE, SIG_IGN);
 
@@ -100,7 +110,11 @@ char **main_argv;
 
     /* process arguments */
 
-    while((opt = getopt(main_argc, main_argv, "bfsD:")) != EOF) {
+    parse_server_conf(main_argc, main_argv, &new_argc, &new_argv);
+    my_argc = new_argc;
+    my_argv = new_argv;
+
+    while((opt = getopt(my_argc, my_argv, "bfsD:")) != EOF) {
        switch(opt) {
        case 'b': batch = 1;
                  break;
@@ -109,7 +123,7 @@ char **main_argv;
        case 's': redirect = 0;
                  break;
        case 'D': if (datearg == NULL)
-                     datearg = alloc(21*sizeof(char *));
+                     datearg = alloc(21*SIZEOF(char *));
                  if(nb_datearg == 20) {
                      fprintf(stderr,"maximum of 20 -D arguments.\n");
                      exit(1);
@@ -124,29 +138,42 @@ char **main_argv;
        exit(1);
     }
 
-    main_argc -= optind, main_argv += optind;
+    my_argc -= optind, my_argv += optind;
 
-    if(main_argc < 1) {
-       error("Usage: amflush%s [-b] [-f] [-s] [-D date]* <confdir> [host [disk]* ]*", versionsuffix());
+    if(my_argc < 1) {
+       error("Usage: amflush%s [-b] [-f] [-s] [-D date]* <confdir> [host [disk]* ]* [-o configoption]*", versionsuffix());
+       /*NOTREACHED*/
     }
 
-    config_name = main_argv[0];
+    config_name = my_argv[0];
     config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
 
     conffile = stralloc2(config_dir, CONFFILE_NAME);
-    if(read_conffile(conffile))
+    if(read_conffile(conffile)) {
        error("errors processing config file \"%s\"", conffile);
+       /*NOTREACHED*/
+    }
     amfree(conffile);
 
+    dbrename(config_name, DBG_SUBDIR_SERVER);
+
+    report_bad_conf_arg();
+
     conf_diskfile = getconf_str(CNF_DISKFILE);
     if (*conf_diskfile == '/') {
        conf_diskfile = stralloc(conf_diskfile);
     } else {
        conf_diskfile = stralloc2(config_dir, conf_diskfile);
     }
-    if (read_diskfile(conf_diskfile, &diskq) < 0)
+    if (read_diskfile(conf_diskfile, &diskq) < 0) {
        error("could not read disklist file \"%s\"", conf_diskfile);
-    match_disklist(&diskq, main_argc-1, main_argv+1);
+       /*NOTREACHED*/
+    }
+    errstr = match_disklist(&diskq, my_argc-1, my_argv+1);
+    if (errstr) {
+       printf("%s",errstr);
+       amfree(errstr);
+    }
     amfree(conf_diskfile);
 
     conf_tapelist = getconf_str(CNF_TAPELIST);
@@ -155,17 +182,31 @@ char **main_argv;
     } else {
        conf_tapelist = stralloc2(config_dir, conf_tapelist);
     }
-    if(read_tapelist(conf_tapelist))
+    if(read_tapelist(conf_tapelist)) {
        error("could not load tapelist \"%s\"", conf_tapelist);
+       /*NOTREACHED*/
+    }
     amfree(conf_tapelist);
 
-    datestamp = construct_datestamp(NULL);
+    conf_usetimestamps = getconf_boolean(CNF_USETIMESTAMPS);
+
+    amflush_datestamp = construct_datestamp(NULL);
+    if(conf_usetimestamps == 0) {
+       amflush_timestamp = stralloc(amflush_datestamp);
+    }
+    else {
+       amflush_timestamp = construct_timestamp(NULL);
+    }
 
     dumpuser = getconf_str(CNF_DUMPUSER);
-    if((pw = getpwnam(dumpuser)) == NULL)
+    if((pw = getpwnam(dumpuser)) == NULL) {
        error("dumpuser %s not found in password file", dumpuser);
-    if(pw->pw_uid != getuid())
+       /*NOTREACHED*/
+    }
+    if(pw->pw_uid != getuid()) {
        error("must run amflush as user %s", dumpuser);
+       /*NOTREACHED*/
+    }
 
     conf_logdir = getconf_str(CNF_LOGDIR);
     if (*conf_logdir == '/') {
@@ -174,8 +215,10 @@ char **main_argv;
        conf_logdir = stralloc2(config_dir, conf_logdir);
     }
     conf_logfile = vstralloc(conf_logdir, "/log", NULL);
-    if (access(conf_logfile, F_OK) == 0)
+    if (access(conf_logfile, F_OK) == 0) {
        error("%s exists: amdump or amflush is already running, or you must run amcleanup", conf_logfile);
+       /*NOTREACHED*/
+    }
     amfree(conf_logfile);
 
     driver_program = vstralloc(libexecdir, "/", "driver", versionsuffix(),
@@ -241,14 +284,19 @@ char **main_argv;
     erroutput_type = (ERR_AMANDALOG|ERR_INTERACTIVE);
     set_logerror(logerror);
     today = time(NULL);
-    strftime(date_string, 100, "%a %b %e %H:%M:%S %Z %Y", localtime (&today));
+    tm = localtime(&today);
+    if (tm)
+       strftime(date_string, 100, "%a %b %e %H:%M:%S %Z %Y", tm);
+    else
+       error("BAD DATE"); /* should never happen */
     fprintf(stderr, "amflush: start at %s\n", date_string);
-    fprintf(stderr, "amflush: datestamp %s\n", datestamp);
-    log_add(L_START, "date %s", datestamp);
+    fprintf(stderr, "amflush: datestamp %s\n", amflush_timestamp);
+    log_add(L_START, "date %s", amflush_timestamp);
 
     /* START DRIVER */
     if(pipe(driver_pipe) == -1) {
        error("error [opening pipe to driver: %s]", strerror(errno));
+       /*NOTREACHED*/
     }
     if((driver_pid = fork()) == 0) {
        /*
@@ -260,16 +308,27 @@ char **main_argv;
               "driver", config_name, "nodump", (char *)0,
               safe_env());
        error("cannot exec %s: %s", driver_program, strerror(errno));
+       /*NOTREACHED*/
     } else if(driver_pid == -1) {
        error("cannot fork for %s: %s", driver_program, strerror(errno));
+       /*NOTREACHED*/
     }
     driver_stream = fdopen(driver_pipe[1], "w");
+    if (!driver_stream) {
+       error("Can't fdopen: %s", strerror(errno));
+       /*NOTREACHED*/
+    }
 
+    fprintf(driver_stream, "DATE %s\n", amflush_timestamp);
     for(holding_file=holding_list->first; holding_file != NULL;
                                   holding_file = holding_file->next) {
        get_dumpfile(holding_file->name, &file);
 
        dp = lookup_disk(file.name, file.disk);
+       if (!dp) {
+           error("dp == NULL");
+           /*NOTREACHED*/
+       }
        if (dp->todo == 0) continue;
 
        fprintf(stderr,
@@ -298,6 +357,7 @@ char **main_argv;
                continue;
            } else {
                error("wait for %s: %s", driver_program, strerror(errno));
+               /*NOTREACHED*/
            }
        } else if (pid == driver_pid) {
            break;
@@ -344,12 +404,14 @@ char **main_argv;
            if (rename(errfilex, nerrfilex) != 0) {
                error("cannot rename \"%s\" to \"%s\": %s",
                      errfilex, nerrfilex, strerror(errno));
+               /*NOTREACHED*/
            }
        }
        errfilex = newvstralloc(errfilex, errfile, ".1", NULL);
        if (rename(errfile,errfilex) != 0) {
            error("cannot rename \"%s\" to \"%s\": %s",
                  errfilex, nerrfilex, strerror(errno));
+           /*NOTREACHED*/
        }
        amfree(errfile);
        amfree(errfilex);
@@ -370,8 +432,10 @@ char **main_argv;
               "amreport", config_name, (char *)0,
               safe_env());
        error("cannot exec %s: %s", reporter_program, strerror(errno));
+       /*NOTREACHED*/
     } else if(reporter_pid == -1) {
        error("cannot fork for %s: %s", reporter_program, strerror(errno));
+       /*NOTREACHED*/
     }
     while(1) {
        if((pid = wait(&exitcode)) == -1) {
@@ -379,6 +443,7 @@ char **main_argv;
                continue;
            } else {
                error("wait for %s: %s", reporter_program, strerror(errno));
+               /*NOTREACHED*/
            }
        } else if (pid == reporter_pid) {
            break;
@@ -389,27 +454,32 @@ char **main_argv;
      * Call amlogroll to rename the log file to its datestamped version.
      * Since we exec at this point, our exit code will be that of amlogroll.
      */
-
     execle(logroll_program,
           "amlogroll", config_name, (char *)0,
           safe_env());
     error("cannot exec %s: %s", logroll_program, strerror(errno));
+    /*NOTREACHED*/
     return 0;                          /* keep the compiler happy */
 }
 
 
-static int get_letter_from_user()
+static int
+get_letter_from_user(void)
 {
     int r, ch;
 
     fflush(stdout); fflush(stderr);
-    while((ch = getchar()) != EOF && ch != '\n' && isspace(ch)) {}
+    while((ch = getchar()) != EOF && ch != '\n' && isspace(ch)) {
+       (void)ch; /* Quite lint */
+    }
     if(ch == '\n') {
        r = '\0';
     } else if (ch != EOF) {
        r = ch;
        if(islower(r)) r = toupper(r);
-       while((ch = getchar()) != EOF && ch != '\n') {}
+       while((ch = getchar()) != EOF && ch != '\n') { 
+           (void)ch; /* Quite lint */
+       }
     } else {
        r = ch;
        clearerr(stdin);
@@ -418,8 +488,12 @@ static int get_letter_from_user()
 }
 
 
-void confirm()
-/* confirm before detaching and running */
+/*
+ * confirm before detaching and running
+ */
+
+void
+confirm(void)
 {
     tape_t *tp;
     char *tpchanger;
@@ -427,7 +501,7 @@ void confirm()
     int ch;
     char *extra;
 
-    printf("\nToday is: %s\n",datestamp);
+    printf("\nToday is: %s\n",amflush_datestamp);
     printf("Flushing dumps in");
     extra = "";
     for(dir = datestamp_list->first; dir != NULL; dir = dir->next) {
@@ -464,34 +538,43 @@ void confirm()
     exit(1);
 }
 
-void redirect_stderr()
+void
+redirect_stderr(void)
 {
     int fderr;
     char *errfile;
 
     fflush(stdout); fflush(stderr);
     errfile = vstralloc(conf_logdir, "/amflush", NULL);
-    if((fderr = open(errfile, O_WRONLY| O_CREAT | O_TRUNC, 0600)) == -1)
+    if((fderr = open(errfile, O_WRONLY| O_CREAT | O_TRUNC, 0600)) == -1) {
        error("could not open %s: %s", errfile, strerror(errno));
+       /*NOTREACHED*/
+    }
     dup2(fderr,1);
     dup2(fderr,2);
     aclose(fderr);
     amfree(errfile);
 }
 
-void detach()
+void
+detach(void)
 {
     int fd;
 
     fflush(stdout); fflush(stderr);
-    if((fd = open("/dev/null", O_RDWR, 0666)) == -1)
+    if((fd = open("/dev/null", O_RDWR, 0666)) == -1) {
        error("could not open /dev/null: %s", strerror(errno));
+       /*NOTREACHED*/
+    }
 
     dup2(fd,0);
     aclose(fd);
 
     switch(fork()) {
-    case -1: error("could not fork: %s", strerror(errno));
+    case -1:
+       error("could not fork: %s", strerror(errno));
+       /*NOTREACHED*/
+
     case 0:
        setsid();
        return;
@@ -499,5 +582,3 @@ void detach()
 
     exit(0);
 }
-
-