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.41.2.13.4.6.2.9.2.2 2004/10/20 21:49:26 martinea 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));
60 int main(main_argc, main_argv)
69 char **datearg = NULL;
78 pid_t pid, driver_pid, reporter_pid;
82 sl_t *holding_list=NULL;
85 char date_string[100];
88 for(fd = 3; fd < FD_SETSIZE; fd++) {
90 * Make sure nobody spoofs us with a lot of extra open files
91 * that would cause an open we do to get a very high file
92 * descriptor, which in turn might be used as an index into
93 * an array (e.g. an fd_set).
100 set_pname("amflush");
102 signal(SIGPIPE, SIG_IGN);
104 erroutput_type = ERR_INTERACTIVE;
109 /* process arguments */
111 while((opt = getopt(main_argc, main_argv, "bfsD:")) != EOF) {
115 case 'f': foreground = 1;
117 case 's': redirect = 0;
119 case 'D': if (datearg == NULL)
120 datearg = alloc(21*sizeof(char *));
121 if(nb_datearg == 20) {
122 fprintf(stderr,"maximum of 20 -D arguments.\n");
125 datearg[nb_datearg++] = stralloc(optarg);
126 datearg[nb_datearg] = NULL;
130 if(!foreground && !redirect) {
131 fprintf(stderr,"Can't redirect to stdout/stderr if not in forground.\n");
135 main_argc -= optind, main_argv += optind;
138 error("Usage: amflush%s [-b] [-f] [-s] [-D date]* <confdir> [host [disk]* ]*", versionsuffix());
141 config_name = main_argv[0];
143 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
144 conffile = stralloc2(config_dir, CONFFILE_NAME);
145 if(read_conffile(conffile)) {
146 error("errors processing config file \"%s\"", conffile);
149 conf_diskfile = getconf_str(CNF_DISKFILE);
150 if (*conf_diskfile == '/') {
151 conf_diskfile = stralloc(conf_diskfile);
153 conf_diskfile = stralloc2(config_dir, conf_diskfile);
155 if((diskqp = read_diskfile(conf_diskfile)) == NULL) {
156 error("could not read disklist file \"%s\"", conf_diskfile);
158 match_disklist(diskqp, main_argc-1, main_argv+1);
159 amfree(conf_diskfile);
160 conf_tapelist = getconf_str(CNF_TAPELIST);
161 if (*conf_tapelist == '/') {
162 conf_tapelist = stralloc(conf_tapelist);
164 conf_tapelist = stralloc2(config_dir, conf_tapelist);
166 if(read_tapelist(conf_tapelist)) {
167 error("could not load tapelist \"%s\"", conf_tapelist);
169 amfree(conf_tapelist);
171 datestamp = construct_datestamp(NULL);
173 dumpuser = getconf_str(CNF_DUMPUSER);
174 if((pw = getpwnam(dumpuser)) == NULL)
175 error("dumpuser %s not found in password file", dumpuser);
176 if(pw->pw_uid != getuid())
177 error("must run amflush as user %s", dumpuser);
179 conf_logdir = getconf_str(CNF_LOGDIR);
180 if (*conf_logdir == '/') {
181 conf_logdir = stralloc(conf_logdir);
183 conf_logdir = stralloc2(config_dir, conf_logdir);
185 conf_logfile = vstralloc(conf_logdir, "/log", NULL);
186 if (access(conf_logfile, F_OK) == 0) {
187 error("%s exists: amdump or amflush is already running, or you must run amcleanup", conf_logfile);
189 amfree(conf_logfile);
191 driver_program = vstralloc(libexecdir, "/", "driver", versionsuffix(),
193 reporter_program = vstralloc(sbindir, "/", "amreport", versionsuffix(),
195 logroll_program = vstralloc(libexecdir, "/", "amlogroll", versionsuffix(),
199 sle_t *dir, *next_dir;
202 datestamp_list = pick_all_datestamp(1);
203 for(dir = datestamp_list->first; dir != NULL;) {
204 next_dir = dir->next;
206 for(i=0; i<nb_datearg && ok==0; i++) {
207 ok = match_datestamp(datearg[i], dir->name);
209 if(ok == 0) { /* remove dir */
210 remove_sl(datestamp_list, dir);
217 datestamp_list = pick_all_datestamp(1);
220 datestamp_list = pick_datestamp(1);
224 if(is_empty_sl(datestamp_list)) {
225 printf("Could not find any Amanda directories to flush.\n");
229 holding_list = get_flush(datestamp_list, NULL, 1, 0);
230 if(holding_list->first == NULL) {
231 printf("Could not find any valid dump image, check directory.\n");
235 if(!batch) confirm();
237 for(dp = diskqp->head; dp != NULL; dp = dp->next) {
239 log_add(L_DISK, "%s %s", dp->host->hostname, dp->name);
242 if(!foreground) { /* write it before redirecting stdout */
243 puts("Running in background, you can log off now.");
244 puts("You'll get mail when amflush is finished.");
247 if(redirect) redirect_stderr();
249 if(!foreground) detach();
251 erroutput_type = (ERR_AMANDALOG|ERR_INTERACTIVE);
252 set_logerror(logerror);
254 strftime(date_string, 100, "%a %b %e %H:%M:%S %Z %Y", localtime (&today));
255 fprintf(stderr, "amflush: start at %s\n", date_string);
256 fprintf(stderr, "amflush: datestamp %s\n", datestamp);
257 log_add(L_START, "date %s", datestamp);
260 if(pipe(driver_pipe) == -1) {
261 error("error [opening pipe to driver: %s]", strerror(errno));
263 if((driver_pid = fork()) == 0) {
265 * This is the child process.
267 dup2(driver_pipe[0], 0);
268 close(driver_pipe[1]);
269 execle(driver_program,
270 "driver", config_name, "nodump", (char *)0,
272 error("cannot exec %s: %s", driver_program, strerror(errno));
273 } else if(driver_pid == -1) {
274 error("cannot fork for %s: %s", driver_program, strerror(errno));
276 driver_stream = fdopen(driver_pipe[1], "w");
278 for(holding_file=holding_list->first; holding_file != NULL;
279 holding_file = holding_file->next) {
280 get_dumpfile(holding_file->name, &file);
282 dp = lookup_disk(file.name, file.disk);
283 if (dp->todo == 0) continue;
286 "FLUSH %s %s %s %d %s\n",
292 fprintf(driver_stream,
293 "FLUSH %s %s %s %d %s\n",
300 fprintf(stderr, "ENDFLUSH\n"); fflush(stderr);
301 fprintf(driver_stream, "ENDFLUSH\n"); fflush(driver_stream);
302 fclose(driver_stream);
306 if((pid = wait(&exitcode)) == -1) {
310 error("wait for %s: %s", driver_program, strerror(errno));
312 } else if (pid == driver_pid) {
317 free_sl(datestamp_list);
318 datestamp_list = NULL;
319 free_sl(holding_list);
322 if(redirect) { /* rename errfile */
323 char *errfile, *errfilex, *nerrfilex, number[100];
327 struct stat stat_buf;
329 errfile = vstralloc(conf_logdir, "/amflush", NULL);
332 tapecycle = getconf_int(CNF_TAPECYCLE);
333 maxdays = tapecycle + 2;
335 /* First, find out the last existing errfile, */
336 /* to avoid ``infinite'' loops if tapecycle is infinite */
338 ap_snprintf(number,100,"%d",days);
339 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
340 while ( days < maxdays && stat(errfilex,&stat_buf)==0) {
342 ap_snprintf(number,100,"%d",days);
343 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
345 ap_snprintf(number,100,"%d",days);
346 errfilex = newvstralloc(errfilex, errfile, ".", number, NULL);
350 nerrfilex = errfilex;
352 ap_snprintf(number,100,"%d",days);
353 errfilex = vstralloc(errfile, ".", number, NULL);
354 if (rename(errfilex, nerrfilex) != 0) {
355 error("cannot rename \"%s\" to \"%s\": %s",
356 errfilex, nerrfilex, strerror(errno));
359 errfilex = newvstralloc(errfilex, errfile, ".1", NULL);
360 if (rename(errfile,errfilex) != 0) {
361 error("cannot rename \"%s\" to \"%s\": %s",
362 errfilex, nerrfilex, strerror(errno));
370 * Have amreport generate report and send mail. Note that we do
371 * not bother checking the exit status. If it does not work, it
375 if((reporter_pid = fork()) == 0) {
377 * This is the child process.
379 execle(reporter_program,
380 "amreport", config_name, (char *)0,
382 error("cannot exec %s: %s", reporter_program, strerror(errno));
383 } else if(reporter_pid == -1) {
384 error("cannot fork for %s: %s", reporter_program, strerror(errno));
387 if((pid = wait(&exitcode)) == -1) {
391 error("wait for %s: %s", reporter_program, strerror(errno));
393 } else if (pid == reporter_pid) {
399 * Call amlogroll to rename the log file to its datestamped version.
400 * Since we exec at this point, our exit code will be that of amlogroll.
403 execle(logroll_program,
404 "amlogroll", config_name, (char *)0,
406 error("cannot exec %s: %s", logroll_program, strerror(errno));
407 return 0; /* keep the compiler happy */
411 int get_letter_from_user()
416 fflush(stdout); fflush(stderr);
417 while((ch = getchar()) != EOF && ch != '\n' && isspace(ch)) {}
420 } else if (ch != EOF) {
422 if(islower(r)) r = toupper(r);
423 while((ch = getchar()) != EOF && ch != '\n') {}
433 /* confirm before detaching and running */
441 printf("\nToday is: %s\n",datestamp);
442 printf("Flushing dumps in");
444 for(dir = datestamp_list->first; dir != NULL; dir = dir->next) {
445 printf("%s %s", extra, dir->name);
448 tpchanger = getconf_str(CNF_TPCHANGER);
449 if(*tpchanger != '\0') {
450 printf(" using tape changer \"%s\".\n", tpchanger);
452 printf(" to tape drive \"%s\".\n", getconf_str(CNF_TAPEDEV));
455 printf("Expecting ");
456 tp = lookup_last_reusable_tape(0);
457 if(tp != NULL) printf("tape %s or ", tp->label);
458 printf("a new tape.");
459 tp = lookup_tapepos(1);
460 if(tp != NULL) printf(" (The last dumps were to tape %s)", tp->label);
463 printf("\nAre you sure you want to do this [yN]? ");
464 if((ch = get_letter_from_user()) == 'Y') {
466 } else if (ch == 'N' || ch == '\0' || ch == EOF) {
474 printf("Ok, quitting. Run amflush again when you are ready.\n");
478 void redirect_stderr()
483 fflush(stdout); fflush(stderr);
484 errfile = vstralloc(conf_logdir, "/amflush", NULL);
485 if((fderr = open(errfile, O_WRONLY| O_CREAT | O_TRUNC, 0600)) == -1)
486 error("could not open %s: %s", errfile, strerror(errno));
497 fflush(stdout); fflush(stderr);
498 if((fd = open("/dev/null", O_RDWR, 0666)) == -1)
499 error("could not open /dev/null: %s", strerror(errno));
505 case -1: error("could not fork: %s", strerror(errno));