2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: amflush.c,v 1.80 2006/01/14 04:37:19 paddy_s Exp $
29 * write files from work directory onto tape
41 #include "server_util.h"
43 static char *conf_logdir;
46 char *reporter_program;
47 char *logroll_program;
52 int main P((int main_argc, char **main_argv));
53 void flush_holdingdisk P((char *diskdir, char *datestamp));
54 void confirm P((void));
55 void redirect_stderr P((void));
56 void detach P((void));
57 void run_dumps P((void));
58 static int get_letter_from_user P((void));
60 int main(main_argc, main_argv)
69 char **datearg = NULL;
78 pid_t driver_pid, reporter_pid;
82 sl_t *holding_list=NULL;
85 char date_string[100];
93 /* Don't die when child closes pipe */
94 signal(SIGPIPE, SIG_IGN);
96 erroutput_type = ERR_INTERACTIVE;
101 /* process arguments */
103 while((opt = getopt(main_argc, main_argv, "bfsD:")) != EOF) {
107 case 'f': foreground = 1;
109 case 's': redirect = 0;
111 case 'D': if (datearg == NULL)
112 datearg = alloc(21*sizeof(char *));
113 if(nb_datearg == 20) {
114 fprintf(stderr,"maximum of 20 -D arguments.\n");
117 datearg[nb_datearg++] = stralloc(optarg);
118 datearg[nb_datearg] = NULL;
122 if(!foreground && !redirect) {
123 fprintf(stderr,"Can't redirect to stdout/stderr if not in forground.\n");
127 main_argc -= optind, main_argv += optind;
130 error("Usage: amflush%s [-b] [-f] [-s] [-D date]* <confdir> [host [disk]* ]*", versionsuffix());
133 config_name = main_argv[0];
134 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
136 conffile = stralloc2(config_dir, CONFFILE_NAME);
137 if(read_conffile(conffile))
138 error("errors processing config file \"%s\"", conffile);
141 conf_diskfile = getconf_str(CNF_DISKFILE);
142 if (*conf_diskfile == '/') {
143 conf_diskfile = stralloc(conf_diskfile);
145 conf_diskfile = stralloc2(config_dir, conf_diskfile);
147 if (read_diskfile(conf_diskfile, &diskq) < 0)
148 error("could not read disklist file \"%s\"", conf_diskfile);
149 match_disklist(&diskq, main_argc-1, main_argv+1);
150 amfree(conf_diskfile);
152 conf_tapelist = getconf_str(CNF_TAPELIST);
153 if (*conf_tapelist == '/') {
154 conf_tapelist = stralloc(conf_tapelist);
156 conf_tapelist = stralloc2(config_dir, conf_tapelist);
158 if(read_tapelist(conf_tapelist))
159 error("could not load tapelist \"%s\"", conf_tapelist);
160 amfree(conf_tapelist);
162 datestamp = construct_datestamp(NULL);
164 dumpuser = getconf_str(CNF_DUMPUSER);
165 if((pw = getpwnam(dumpuser)) == NULL)
166 error("dumpuser %s not found in password file", dumpuser);
167 if(pw->pw_uid != getuid())
168 error("must run amflush as user %s", dumpuser);
170 conf_logdir = getconf_str(CNF_LOGDIR);
171 if (*conf_logdir == '/') {
172 conf_logdir = stralloc(conf_logdir);
174 conf_logdir = stralloc2(config_dir, conf_logdir);
176 conf_logfile = vstralloc(conf_logdir, "/log", NULL);
177 if (access(conf_logfile, F_OK) == 0)
178 error("%s exists: amdump or amflush is already running, or you must run amcleanup", conf_logfile);
179 amfree(conf_logfile);
181 driver_program = vstralloc(libexecdir, "/", "driver", versionsuffix(),
183 reporter_program = vstralloc(sbindir, "/", "amreport", versionsuffix(),
185 logroll_program = vstralloc(libexecdir, "/", "amlogroll", versionsuffix(),
189 sle_t *dir, *next_dir;
192 datestamp_list = pick_all_datestamp(1);
193 for(dir = datestamp_list->first; dir != NULL;) {
194 next_dir = dir->next;
196 for(i=0; i<nb_datearg && ok==0; i++) {
197 ok = match_datestamp(datearg[i], dir->name);
199 if(ok == 0) { /* remove dir */
200 remove_sl(datestamp_list, dir);
207 datestamp_list = pick_all_datestamp(1);
210 datestamp_list = pick_datestamp(1);
214 if(is_empty_sl(datestamp_list)) {
215 printf("Could not find any Amanda directories to flush.\n");
219 holding_list = get_flush(datestamp_list, NULL, 1, 0);
220 if(holding_list->first == NULL) {
221 printf("Could not find any valid dump image, check directory.\n");
225 if(!batch) confirm();
227 for(dp = diskq.head; dp != NULL; dp = dp->next) {
229 log_add(L_DISK, "%s %s", dp->host->hostname, dp->name);
232 if(!foreground) { /* write it before redirecting stdout */
233 puts("Running in background, you can log off now.");
234 puts("You'll get mail when amflush is finished.");
237 if(redirect) redirect_stderr();
239 if(!foreground) detach();
241 erroutput_type = (ERR_AMANDALOG|ERR_INTERACTIVE);
242 set_logerror(logerror);
244 strftime(date_string, 100, "%a %b %e %H:%M:%S %Z %Y", localtime (&today));
245 fprintf(stderr, "amflush: start at %s\n", date_string);
246 fprintf(stderr, "amflush: datestamp %s\n", datestamp);
247 log_add(L_START, "date %s", datestamp);
250 if(pipe(driver_pipe) == -1) {
251 error("error [opening pipe to driver: %s]", strerror(errno));
253 if((driver_pid = fork()) == 0) {
255 * This is the child process.
257 dup2(driver_pipe[0], 0);
258 close(driver_pipe[1]);
259 execle(driver_program,
260 "driver", config_name, "nodump", (char *)0,
262 error("cannot exec %s: %s", driver_program, strerror(errno));
263 } else if(driver_pid == -1) {
264 error("cannot fork for %s: %s", driver_program, strerror(errno));
266 driver_stream = fdopen(driver_pipe[1], "w");
268 for(holding_file=holding_list->first; holding_file != NULL;
269 holding_file = holding_file->next) {
270 get_dumpfile(holding_file->name, &file);
272 dp = lookup_disk(file.name, file.disk);
273 if (dp->todo == 0) continue;
276 "FLUSH %s %s %s %d %s\n",
282 fprintf(driver_stream,
283 "FLUSH %s %s %s %d %s\n",
290 fprintf(stderr, "ENDFLUSH\n"); fflush(stderr);
291 fprintf(driver_stream, "ENDFLUSH\n"); fflush(driver_stream);
292 fclose(driver_stream);
296 if((pid = wait(&exitcode)) == -1) {
300 error("wait for %s: %s", driver_program, strerror(errno));
302 } else if (pid == driver_pid) {
307 free_sl(datestamp_list);
308 datestamp_list = NULL;
309 free_sl(holding_list);
312 if(redirect) { /* rename errfile */
313 char *errfile, *errfilex, *nerrfilex, number[100];
317 struct stat stat_buf;
319 errfile = vstralloc(conf_logdir, "/amflush", NULL);
322 tapecycle = getconf_int(CNF_TAPECYCLE);
323 maxdays = tapecycle + 2;
325 /* First, find out the last existing errfile, */
326 /* to avoid ``infinite'' loops if tapecycle is infinite */
328 snprintf(number,100,"%d",days);
329 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
330 while ( days < maxdays && stat(errfilex,&stat_buf)==0) {
332 snprintf(number,100,"%d",days);
333 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
335 snprintf(number,100,"%d",days);
336 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
340 nerrfilex = errfilex;
342 snprintf(number,100,"%d",days);
343 errfilex = vstralloc(errfile, ".", number, NULL);
344 if (rename(errfilex, nerrfilex) != 0) {
345 error("cannot rename \"%s\" to \"%s\": %s",
346 errfilex, nerrfilex, strerror(errno));
349 errfilex = newvstralloc(errfilex, errfile, ".1", NULL);
350 if (rename(errfile,errfilex) != 0) {
351 error("cannot rename \"%s\" to \"%s\": %s",
352 errfilex, nerrfilex, strerror(errno));
360 * Have amreport generate report and send mail. Note that we do
361 * not bother checking the exit status. If it does not work, it
365 if((reporter_pid = fork()) == 0) {
367 * This is the child process.
369 execle(reporter_program,
370 "amreport", config_name, (char *)0,
372 error("cannot exec %s: %s", reporter_program, strerror(errno));
373 } else if(reporter_pid == -1) {
374 error("cannot fork for %s: %s", reporter_program, strerror(errno));
377 if((pid = wait(&exitcode)) == -1) {
381 error("wait for %s: %s", reporter_program, strerror(errno));
383 } else if (pid == reporter_pid) {
389 * Call amlogroll to rename the log file to its datestamped version.
390 * Since we exec at this point, our exit code will be that of amlogroll.
393 execle(logroll_program,
394 "amlogroll", config_name, (char *)0,
396 error("cannot exec %s: %s", logroll_program, strerror(errno));
397 return 0; /* keep the compiler happy */
401 static int get_letter_from_user()
405 fflush(stdout); fflush(stderr);
406 while((ch = getchar()) != EOF && ch != '\n' && isspace(ch)) {}
409 } else if (ch != EOF) {
411 if(islower(r)) r = toupper(r);
412 while((ch = getchar()) != EOF && ch != '\n') {}
422 /* confirm before detaching and running */
430 printf("\nToday is: %s\n",datestamp);
431 printf("Flushing dumps in");
433 for(dir = datestamp_list->first; dir != NULL; dir = dir->next) {
434 printf("%s %s", extra, dir->name);
437 tpchanger = getconf_str(CNF_TPCHANGER);
438 if(*tpchanger != '\0') {
439 printf(" using tape changer \"%s\".\n", tpchanger);
441 printf(" to tape drive \"%s\".\n", getconf_str(CNF_TAPEDEV));
444 printf("Expecting ");
445 tp = lookup_last_reusable_tape(0);
446 if(tp != NULL) printf("tape %s or ", tp->label);
447 printf("a new tape.");
448 tp = lookup_tapepos(1);
449 if(tp != NULL) printf(" (The last dumps were to tape %s)", tp->label);
452 printf("\nAre you sure you want to do this [yN]? ");
453 if((ch = get_letter_from_user()) == 'Y') {
455 } else if (ch == 'N' || ch == '\0' || ch == EOF) {
463 printf("Ok, quitting. Run amflush again when you are ready.\n");
467 void redirect_stderr()
472 fflush(stdout); fflush(stderr);
473 errfile = vstralloc(conf_logdir, "/amflush", NULL);
474 if((fderr = open(errfile, O_WRONLY| O_CREAT | O_TRUNC, 0600)) == -1)
475 error("could not open %s: %s", errfile, strerror(errno));
486 fflush(stdout); fflush(stderr);
487 if((fd = open("/dev/null", O_RDWR, 0666)) == -1)
488 error("could not open /dev/null: %s", strerror(errno));
494 case -1: error("could not fork: %s", strerror(errno));