2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-2000 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: amcheck.c,v 1.50.2.19.2.7.2.20.2.12 2005/10/11 14:50:00 martinea Exp $
29 * checks for common problems in server and clients
44 #include "pipespawn.h"
45 #include "amfeatures.h"
48 * If we don't have the new-style wait access functions, use our own,
49 * compatible with old-style BSD systems at least. Note that we don't
50 * care about the case w_stopval == WSTOPPED since we don't ask to see
51 * stopped processes, so should never get them from wait.
54 # define WEXITSTATUS(r) (((union wait *) &(r))->w_retcode)
55 # define WTERMSIG(r) (((union wait *) &(r))->w_termsig)
58 # define WIFSIGNALED(r) (((union wait *) &(r))->w_termsig != 0)
61 #define BUFFER_SIZE 32768
63 static int conf_ctimeout;
67 static disklist_t *origqp;
69 static uid_t uid_dumpuser;
74 int start_client_checks P((int fd));
75 int start_server_check P((int fd, int do_localchk, int do_tapechk));
76 int main P((int argc, char **argv));
77 int scan_init P((int rc, int ns, int bk));
78 int taperscan_slot P((int rc, char *slotstr, char *device));
79 char *taper_scan P((void));
80 int test_server_pgm P((FILE *outf, char *dir, char *pgm,
81 int suid, uid_t dumpuid));
85 error("Usage: amcheck%s [-M <username>] [-mawsclt] <conf> [host [disk]* ]*", versionsuffix());
88 static unsigned long malloc_hist_1, malloc_size_1;
89 static unsigned long malloc_hist_2, malloc_size_2;
91 static am_feature_t *our_features = NULL;
92 static char *our_feature_string = NULL;
93 static char *displayunit;
94 static long int unitdivisor;
100 char buffer[BUFFER_SIZE];
101 char *version_string;
102 char *mainfname = NULL;
103 char pid_str[NUM_STR_SIZE];
104 int do_clientchk, clientchk_pid, client_probs;
105 int do_localchk, do_tapechk, serverchk_pid, server_probs;
107 int opt, size, result_port, tempfd, mainfd;
116 char *tempfname = NULL;
126 set_pname("amcheck");
129 malloc_size_1 = malloc_inuse(&malloc_hist_1);
131 ap_snprintf(pid_str, sizeof(pid_str), "%ld", (long)getpid());
133 erroutput_type = ERR_INTERACTIVE;
135 our_features = am_init_feature_set();
136 our_feature_string = am_feature_to_string(our_features);
138 /* set up dgram port first thing */
142 if(dgram_bind(msg, &result_port) == -1)
143 error("could not bind result datagram port: %s", strerror(errno));
146 /* set both real and effective uid's to real uid, likewise for gid */
152 alwaysmail = mailout = overwrite = 0;
153 do_localchk = do_tapechk = do_clientchk = 0;
155 server_probs = client_probs = 0;
156 tempfd = mainfd = -1;
158 /* process arguments */
160 while((opt = getopt(argc, argv, "M:mawsclt")) != EOF) {
162 case 'M': mailto=stralloc(optarg);
167 printf("You can't use -%c because configure didn't find a mailer.\n",
177 printf("You can't use -%c because configure didn't find a mailer.\n",
182 case 's': do_localchk = 1; do_tapechk = 1;
185 case 'c': do_clientchk = 1;
188 case 'l': do_localchk = 1;
191 case 'w': do_tapechk = 1; overwrite = 1;
194 case 't': do_tapechk = 1;
202 argc -= optind, argv += optind;
204 do_localchk = do_clientchk = do_tapechk = 1;
207 if(argc < 1) usage();
209 config_name = stralloc(*argv);
211 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
212 conffile = stralloc2(config_dir, CONFFILE_NAME);
213 if(read_conffile(conffile)) {
214 error("errors processing config file \"%s\"", conffile);
217 conf_ctimeout = getconf_int(CNF_CTIMEOUT);
218 conf_diskfile = getconf_str(CNF_DISKFILE);
219 if (*conf_diskfile == '/') {
220 conf_diskfile = stralloc(conf_diskfile);
222 conf_diskfile = stralloc2(config_dir, conf_diskfile);
224 if((origqp = read_diskfile(conf_diskfile)) == NULL) {
225 error("could not load disklist %s", conf_diskfile);
227 match_disklist(origqp, argc-1, argv+1);
228 amfree(conf_diskfile);
231 * Make sure we are running as the dump user.
233 dumpuser = getconf_str(CNF_DUMPUSER);
234 if ((pw = getpwnam(dumpuser)) == NULL) {
235 error("cannot look up dump user \"%s\"", dumpuser);
237 uid_dumpuser = pw->pw_uid;
238 if ((pw = getpwuid(uid_me)) == NULL) {
239 error("cannot look up my own uid (%ld)", (long)uid_me);
241 if (uid_me != uid_dumpuser) {
242 error("running as user \"%s\" instead of \"%s\"",
247 displayunit = getconf_str(CNF_DISPLAYUNIT);
248 unitdivisor = getconf_unit_divisor();
251 * If both server and client side checks are being done, the server
252 * check output goes to the main output, while the client check output
253 * goes to a temporary file and is copied to the main output when done.
255 * If the output is to be mailed, the main output is also a disk file,
256 * otherwise it is stdout.
258 if(do_clientchk && (do_localchk || do_tapechk)) {
259 /* we need the temp file */
260 tempfname = vstralloc(AMANDA_TMPDIR, "/amcheck.temp.", pid_str, NULL);
261 if((tempfd = open(tempfname, O_RDWR|O_CREAT|O_TRUNC, 0600)) == -1)
262 error("could not open %s: %s", tempfname, strerror(errno));
263 unlink(tempfname); /* so it goes away on close */
268 /* the main fd is a file too */
269 mainfname = vstralloc(AMANDA_TMPDIR, "/amcheck.main.", pid_str, NULL);
270 if((mainfd = open(mainfname, O_RDWR|O_CREAT|O_TRUNC, 0600)) == -1)
271 error("could not open %s: %s", mainfname, strerror(errno));
272 unlink(mainfname); /* so it goes away on close */
276 /* just use stdout */
279 /* start server side checks */
281 if(do_localchk || do_tapechk) {
282 serverchk_pid = start_server_check(mainfd, do_localchk, do_tapechk);
287 /* start client side checks */
290 clientchk_pid = start_client_checks((do_localchk || do_tapechk) ? tempfd : mainfd);
295 /* wait for child processes and note any problems */
298 if((pid = wait(&retstat)) == -1) {
299 if(errno == EINTR) continue;
301 } else if(pid == clientchk_pid) {
302 client_probs = WIFSIGNALED(retstat) || WEXITSTATUS(retstat);
304 } else if(pid == serverchk_pid) {
305 server_probs = WIFSIGNALED(retstat) || WEXITSTATUS(retstat);
308 char number[NUM_STR_SIZE];
309 char *wait_msg = NULL;
311 ap_snprintf(number, sizeof(number), "%ld", (long)pid);
312 wait_msg = vstralloc("parent: reaped bogus pid ", number, "\n",
314 for(l = 0, n = strlen(wait_msg); l < n; l += s) {
315 if((s = write(mainfd, wait_msg + l, n - l)) < 0) {
316 error("write main file: %s", strerror(errno));
324 /* copy temp output to main output and write tagline */
326 if(do_clientchk && (do_localchk || do_tapechk)) {
327 if(lseek(tempfd, 0, 0) == -1)
328 error("seek temp file: %s", strerror(errno));
330 while((size=read(tempfd, buffer, sizeof(buffer))) > 0) {
331 for(l = 0; l < size; l += s) {
332 if((s = write(mainfd, buffer + l, size - l)) < 0) {
333 error("write main file: %s", strerror(errno));
338 error("read temp file: %s", strerror(errno));
342 version_string = vstralloc("\n",
343 "(brought to you by Amanda ", version(), ")\n",
345 for(l = 0, n = strlen(version_string); l < n; l += s) {
346 if((s = write(mainfd, version_string + l, n - l)) < 0) {
347 error("write main file: %s", strerror(errno));
350 amfree(version_string);
353 amfree(our_feature_string);
354 am_release_feature_set(our_features);
357 malloc_size_2 = malloc_inuse(&malloc_hist_2);
359 if(malloc_size_1 != malloc_size_2) {
360 malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
363 /* send mail if requested, but only if there were problems */
366 #define MAILTO_LIMIT 10
368 if((server_probs || client_probs || alwaysmail) && mailout) {
381 char *extra_info = NULL;
386 char number[NUM_STR_SIZE];
389 if(lseek(mainfd, (off_t)0, SEEK_SET) == -1) {
390 error("lseek main file: %s", strerror(errno));
392 if(alwaysmail && !(server_probs || client_probs)) {
393 subject = stralloc2(getconf_str(CNF_ORG),
394 " AMCHECK REPORT: NO PROBLEMS FOUND");
396 subject = stralloc2(getconf_str(CNF_ORG),
397 " AMANDA PROBLEM: FIX BEFORE RUN, IF POSSIBLE");
400 * Variable arg lists are hard to deal with when we do not know
401 * ourself how many args are involved. Split the address list
402 * and hope there are not more than 9 entries.
404 * Remember that split() returns the original input string in
405 * argv[0], so we have to skip over that.
407 a = (char **) alloc((MAILTO_LIMIT + 1) * sizeof(char *));
408 memset(a, 0, (MAILTO_LIMIT + 1) * sizeof(char *));
413 n = split(getconf_str(CNF_MAILTO), a, MAILTO_LIMIT, " ");
416 if((nullfd = open("/dev/null", O_RDWR)) < 0) {
417 error("nullfd: /dev/null: %s", strerror(errno));
419 mailpid = pipespawn(MAILER, STDIN_PIPE | STDERR_PIPE,
420 &mailfd, &nullfd, &errfd,
423 a[1], a[2], a[3], a[4],
424 a[5], a[6], a[7], a[8], a[9],
428 * There is the potential for a deadlock here since we are writing
429 * to the process and then reading stderr, but in the normal case,
430 * nothing should be coming back to us, and hopefully in error
431 * cases, the pipe will break and we will exit out of the loop.
433 signal(SIGPIPE, SIG_IGN);
434 while((r = fullread(mainfd, buffer, sizeof(buffer))) > 0) {
435 if((w = fullwrite(mailfd, buffer, r)) != r) {
436 if(w < 0 && errno == EPIPE) {
437 strappend(extra_info, "EPIPE writing to mail process\n");
440 error("mailfd write: %s", strerror(errno));
442 error("mailfd write: wrote %d instead of %d", w, r);
447 ferr = fdopen(errfd, "r");
448 for(; (line = agets(ferr)) != NULL; free(line)) {
449 strappend(extra_info, line);
450 strappend(extra_info, "\n");
455 while ((wpid = wait(&retstat)) != -1) {
456 if (WIFSIGNALED(retstat)) {
458 rc = sig = WTERMSIG(retstat);
461 rc = ret = WEXITSTATUS(retstat);
465 strappend(err, "got signal ");
468 strappend(err, "returned ");
470 ap_snprintf(number, sizeof(number), "%d", ret);
471 strappend(err, number);
476 fputs(extra_info, stderr);
479 error("error running mailer %s: %s", MAILER, err);
484 return (server_probs || client_probs);
487 /* --------------------------------------------------- */
489 int nslots, backwards, found, got_match, tapedays;
491 char *first_match_label = NULL, *first_match = NULL, *found_device = NULL;
493 char *searchlabel, *labelstr;
497 int scan_init(rc, ns, bk)
501 error("could not get changer info: %s", changer_resultstr);
509 int taperscan_slot(rc, slotstr, device)
517 fprintf(errf, "%s: fatal slot %s: %s\n",
518 get_pname(), slotstr, changer_resultstr);
522 fprintf(errf, "%s: slot %s: %s\n",
523 get_pname(), slotstr, changer_resultstr);
527 if((errstr = tape_rdlabel(device, &datestamp, &label)) != NULL) {
528 fprintf(errf, "%s: slot %s: %s\n", get_pname(), slotstr, errstr);
530 /* got an amanda tape */
531 fprintf(errf, "%s: slot %s: date %-8s label %s",
532 get_pname(), slotstr, datestamp, label);
533 if(searchlabel != NULL
534 && (strcmp(label, FAKE_LABEL) == 0
535 || strcmp(label, searchlabel) == 0)) {
536 /* it's the one we are looking for, stop here */
537 fprintf(errf, " (exact label match)\n");
538 found_device = newstralloc(found_device, device);
542 else if(!match(labelstr, label))
543 fprintf(errf, " (no match)\n");
545 /* not an exact label match, but a labelstr match */
546 /* check against tape list */
547 tp = lookup_tapelabel(label);
549 fprintf(errf, " (Not in tapelist)\n");
550 else if(!reusable_tape(tp))
551 fprintf(errf, " (active tape)\n");
552 else if(got_match == 0 && tp->datestamp == 0) {
554 first_match = newstralloc(first_match, slotstr);
555 first_match_label = newstralloc(first_match_label, label);
556 fprintf(errf, " (new tape)\n");
558 found_device = newstralloc(found_device, device);
562 fprintf(errf, " (labelstr match)\n");
565 first_match = newstralloc(first_match, slotstr);
566 first_match_label = newstralloc(first_match_label, label);
567 fprintf(errf, " (first labelstr match)\n");
568 if(!backwards || !searchlabel) {
570 found_device = newstralloc(found_device, device);
582 char *outslot = NULL;
584 if((tp = lookup_last_reusable_tape(0)) == NULL)
587 searchlabel = tp->label;
592 changer_find(scan_init, taperscan_slot, searchlabel);
594 if(found == 2 || found == 3)
595 searchlabel = first_match_label;
596 else if(!found && got_match) {
597 searchlabel = first_match_label;
598 amfree(found_device);
599 if(changer_loadslot(first_match, &outslot, &found_device) == 0) {
604 changer_resultstr = newvstralloc(changer_resultstr,
605 "label ", searchlabel,
606 " or new tape not found in rack",
609 changer_resultstr = newstralloc(changer_resultstr,
610 "new tape not found in rack");
615 return found ? found_device : NULL;
618 int test_server_pgm(outf, dir, pgm, suid, dumpuid)
628 pgm = vstralloc(dir, "/", pgm, versionsuffix(), NULL);
629 if(stat(pgm, &statbuf) == -1) {
630 fprintf(outf, "ERROR: program %s: does not exist\n",
633 } else if (!S_ISREG(statbuf.st_mode)) {
634 fprintf(outf, "ERROR: program %s: not a file\n",
637 } else if (access(pgm, X_OK) == -1) {
638 fprintf(outf, "ERROR: program %s: not executable\n",
643 && (statbuf.st_uid != 0 || (statbuf.st_mode & 04000) == 0)) {
644 fprintf(outf, "WARNING: program %s: not setuid-root\n",
651 int start_server_check(fd, do_localchk, do_tapechk)
653 int do_localchk, do_tapechk;
655 char *errstr, *tapename;
656 generic_fs_stats_t fs;
661 int confbad = 0, tapebad = 0, disklow = 0, logbad = 0;
662 int userbad = 0, infobad = 0, indexbad = 0, pgmbad = 0;
663 int testtape = do_tapechk;
665 switch(pid = fork()) {
666 case -1: error("could not fork server check: %s", strerror(errno));
675 set_pname("amcheck-server");
681 if((outf = fdopen(fd, "w")) == NULL)
682 error("fdopen %d: %s", fd, strerror(errno));
685 fprintf(outf, "Amanda Tape Server Host Check\n");
686 fprintf(outf, "-----------------------------\n");
689 * Check various server side config file settings.
697 ColumnSpec = getconf_str(CNF_COLUMNSPEC);
698 if(SetColumDataFromString(ColumnData, ColumnSpec, &errstr) < 0) {
699 fprintf(outf, "ERROR: %s\n", errstr);
703 tp = lookup_tapetype(getconf_str(CNF_TAPETYPE));
704 lbl_templ = tp->lbl_templ;
705 if(strcmp(lbl_templ, "") != 0) {
706 if(strchr(lbl_templ, '/') == NULL) {
707 lbl_templ = stralloc2(config_dir, lbl_templ);
709 lbl_templ = stralloc(lbl_templ);
711 if(access(lbl_templ, R_OK) == -1) {
713 "ERROR: cannot access lbl_templ file %s: %s\n",
719 fprintf(outf, "ERROR: lbl_templ set but no LPRCMD defined, you should reconfigure amanda\n and make sure it find a lpr or lp command.\n");
726 * Look up the programs used on the server side.
729 if(access(libexecdir, X_OK) == -1) {
730 fprintf(outf, "ERROR: program dir %s: not accessible\n",
735 || test_server_pgm(outf, libexecdir, "planner",
738 || test_server_pgm(outf, libexecdir, "dumper",
741 || test_server_pgm(outf, libexecdir, "driver",
744 || test_server_pgm(outf, libexecdir, "taper",
747 || test_server_pgm(outf, libexecdir, "amtrmidx",
750 || test_server_pgm(outf, libexecdir, "amlogroll",
753 if(access(sbindir, X_OK) == -1) {
754 fprintf(outf, "ERROR: program dir %s: not accessible\n",
759 || test_server_pgm(outf, sbindir, "amgetconf",
762 || test_server_pgm(outf, sbindir, "amcheck",
765 || test_server_pgm(outf, sbindir, "amdump",
768 || test_server_pgm(outf, sbindir, "amreport",
771 if(access(COMPRESS_PATH, X_OK) == -1) {
772 fprintf(outf, "WARNING: %s is not executable, server-compression and indexing will not work\n",
778 * Check that the directory for the tapelist file is writable, as well
779 * as the tapelist file itself (if it already exists). Also, check for
780 * a "hold" file (just because it is convenient to do it here) and warn
781 * if tapedev is set to the null device.
784 if(do_localchk || do_tapechk) {
792 conf_tapelist=getconf_str(CNF_TAPELIST);
793 if (*conf_tapelist == '/') {
794 tapefile = stralloc(conf_tapelist);
796 tapefile = stralloc2(config_dir, conf_tapelist);
799 * XXX There Really Ought to be some error-checking here... dhw
801 tape_dir = stralloc(tapefile);
802 if ((lastslash = strrchr((const char *)tape_dir, '/')) != NULL) {
805 * else whine Really Loudly about a path with no slashes??!?
808 if(access(tape_dir, W_OK) == -1) {
809 fprintf(outf, "ERROR: tapelist dir %s: not writable.\n", tape_dir);
812 else if(stat(tapefile, &statbuf) == -1) {
813 fprintf(outf, "ERROR: tapefile %s: %s, you must create an empty file.\n",
814 tapefile, strerror(errno));
817 else if(access(tapefile, F_OK) != 0) {
818 fprintf(outf, "ERROR: can't access tape list %s\n", tapefile);
820 } else if(access(tapefile, F_OK) == 0 && access(tapefile, W_OK) != 0) {
821 fprintf(outf, "ERROR: tape list %s: not writable\n", tapefile);
823 } else if(read_tapelist(tapefile)) {
824 fprintf(outf, "ERROR: tape list %s: parse error\n", tapefile);
827 holdfile = vstralloc(config_dir, "/", "hold", NULL);
828 if(access(holdfile, F_OK) != -1) {
829 fprintf(outf, "WARNING: hold file %s exists\n", holdfile);
834 tapename = getconf_str(CNF_TAPEDEV);
835 if (strncmp(tapename, "null:", 5) == 0) {
837 "WARNING: tapedev is %s, dumps will be thrown away\n",
844 /* check available disk space */
847 for(hdp = holdingdisks; hdp != NULL; hdp = hdp->next) {
848 if(get_fs_stats(hdp->diskdir, &fs) == -1) {
849 fprintf(outf, "ERROR: holding dir %s: %s, you must create a directory.\n",
850 hdp->diskdir, strerror(errno));
853 else if(access(hdp->diskdir, W_OK) == -1) {
854 fprintf(outf, "ERROR: holding disk %s: not writable: %s.\n",
855 hdp->diskdir, strerror(errno));
858 else if(fs.avail == -1) {
860 "WARNING: holding disk %s: available space unknown (%ld KB requested)\n",
861 hdp->diskdir, (long)hdp->disksize);
864 else if(hdp->disksize > 0) {
865 if(fs.avail < hdp->disksize) {
867 "WARNING: holding disk %s: only %ld %sB free (%ld %sB requested)\n",
868 hdp->diskdir, (long)fs.avail/unitdivisor, displayunit,
869 (long)hdp->disksize/unitdivisor, displayunit);
874 "Holding disk %s: %ld %sB disk space available, that's plenty\n",
875 hdp->diskdir, fs.avail/unitdivisor, displayunit);
878 if(fs.avail < -hdp->disksize) {
880 "WARNING: holding disk %s: only %ld %sB free, using nothing\n",
881 hdp->diskdir, fs.avail/unitdivisor, displayunit);
886 "Holding disk %s: %ld %sB disk space available, using %ld %sB\n",
887 hdp->diskdir, fs.avail/unitdivisor, displayunit,
888 (fs.avail + hdp->disksize)/unitdivisor, displayunit);
893 /* check that the log file is writable if it already exists */
899 struct stat stat_old;
902 conf_logdir = getconf_str(CNF_LOGDIR);
903 if (*conf_logdir == '/') {
904 conf_logdir = stralloc(conf_logdir);
906 conf_logdir = stralloc2(config_dir, conf_logdir);
908 logfile = vstralloc(conf_logdir, "/log", NULL);
910 if(stat(conf_logdir, &statbuf) == -1) {
911 fprintf(outf, "ERROR: logdir %s: %s, you must create a directory.\n",
912 conf_logdir, strerror(errno));
915 else if(access(conf_logdir, W_OK) == -1) {
916 fprintf(outf, "ERROR: log dir %s: not writable\n", conf_logdir);
920 if(access(logfile, F_OK) == 0) {
923 if(access(logfile, W_OK) != 0)
924 fprintf(outf, "ERROR: log file %s: not writable\n",
928 olddir = vstralloc(conf_logdir, "/oldlog", NULL);
929 if (stat(olddir,&stat_old) == 0) { /* oldlog exist */
930 if(!(S_ISDIR(stat_old.st_mode))) {
931 fprintf(outf, "ERROR: oldlog directory \"%s\" is not a directory\n", olddir);
933 if(access(olddir, W_OK) == -1) {
934 fprintf(outf, "ERROR: oldlog dir %s: not writable\n", olddir);
937 else if(lstat(olddir,&stat_old) == 0) {
938 fprintf(outf, "ERROR: oldlog directory \"%s\" is not a directory\n", olddir);
942 logfile = newvstralloc(logfile, conf_logdir, "/amdump", NULL);
943 if (access(logfile, F_OK) == 0) {
954 /* check that the tape is a valid amanda tape */
956 tapedays = getconf_int(CNF_TAPECYCLE);
957 labelstr = getconf_str(CNF_LABELSTR);
958 tapename = getconf_str(CNF_TAPEDEV);
960 if (!getconf_seen(CNF_TPCHANGER) && getconf_int(CNF_RUNTAPES) != 1) {
962 "WARNING: if a tape changer is not available, runtapes must be set to 1\n");
965 if(changer_init() && (tapename = taper_scan()) == NULL) {
966 fprintf(outf, "ERROR: %s\n", changer_resultstr);
968 } else if(tape_access(tapename,F_OK|R_OK|W_OK) == -1) {
969 fprintf(outf, "ERROR: %s: %s\n", tapename, strerror(errno));
971 } else if((errstr = tape_rdlabel(tapename, &datestamp, &label)) != NULL) {
972 fprintf(outf, "ERROR: %s: %s\n", tapename, errstr);
974 } else if(strcmp(label, FAKE_LABEL) != 0) {
975 if(!match(labelstr, label)) {
976 fprintf(outf, "ERROR: label %s doesn't match labelstr \"%s\"\n",
981 tp = lookup_tapelabel(label);
983 fprintf(outf, "ERROR: label %s match labelstr but it not listed in the tapelist file.\n", label);
986 else if(tp != NULL && !reusable_tape(tp)) {
987 fprintf(outf, "ERROR: cannot overwrite active tape %s\n",
996 tape_t *exptape = lookup_last_reusable_tape(0);
997 fprintf(outf, " (expecting ");
998 if(exptape != NULL) fprintf(outf, "tape %s or ", exptape->label);
999 fprintf(outf, "a new tape)\n");
1002 if(!tapebad && overwrite) {
1003 if((errstr = tape_writable(tapename)) != NULL) {
1005 "ERROR: tape %s label ok, but is not writable\n",
1009 else fprintf(outf, "Tape %s is writable\n", label);
1011 else fprintf(outf, "NOTE: skipping tape-writable test\n");
1014 fprintf(outf, "Tape %s label ok\n", label);
1015 } else if (do_tapechk) {
1016 fprintf(outf, "WARNING: skipping tape test because amdump or amflush seem to be running\n");
1017 fprintf(outf, "WARNING: if they are not, you must run amcleanup\n");
1019 fprintf(outf, "NOTE: skipping tape checks\n");
1023 * See if the information file and index directory for each client
1024 * and disk is OK. Since we may be seeing clients and/or disks for
1025 * the first time, these are just warnings, not errors.
1028 char *conf_infofile;
1029 char *conf_indexdir;
1030 char *hostinfodir = NULL;
1031 char *hostindexdir = NULL;
1032 char *diskdir = NULL;
1033 char *infofile = NULL;
1034 struct stat statbuf;
1037 int indexdir_checked = 0;
1038 int hostindexdir_checked = 0;
1041 int conf_tapecycle, conf_runspercycle;
1043 conf_tapecycle = getconf_int(CNF_TAPECYCLE);
1044 conf_runspercycle = getconf_int(CNF_RUNSPERCYCLE);
1046 if(conf_tapecycle <= conf_runspercycle) {
1047 fprintf(outf, "WARNING: tapecycle (%d) <= runspercycle (%d).\n",
1048 conf_tapecycle, conf_runspercycle);
1051 conf_infofile = stralloc(getconf_str(CNF_INFOFILE));
1052 if (*conf_infofile != '/') {
1053 char *ci = stralloc2(config_dir, conf_infofile);
1054 amfree(conf_infofile);
1057 conf_indexdir = stralloc(getconf_str(CNF_INDEXDIR));
1058 if (*conf_indexdir != '/') {
1059 char *ci = stralloc2(config_dir, conf_indexdir);
1060 amfree(conf_indexdir);
1064 if(stat(conf_infofile, &statbuf) == -1) {
1065 fprintf(outf, "NOTE: info dir %s: does not exist\n", conf_infofile);
1066 fprintf(outf, "NOTE: it will be created on the next run.\n");
1067 amfree(conf_infofile);
1068 } else if (!S_ISDIR(statbuf.st_mode)) {
1069 fprintf(outf, "ERROR: info dir %s: not a directory\n", conf_infofile);
1070 amfree(conf_infofile);
1072 } else if (access(conf_infofile, W_OK) == -1) {
1073 fprintf(outf, "ERROR: info dir %s: not writable\n", conf_infofile);
1074 amfree(conf_infofile);
1077 strappend(conf_infofile, "/");
1080 while(!empty(*origqp)) {
1081 hostp = origqp->head->host;
1082 host = sanitise_filename(hostp->hostname);
1085 hostinfodir = newstralloc2(hostinfodir, conf_infofile, host);
1086 if(stat(hostinfodir, &statbuf) == -1) {
1087 fprintf(outf, "NOTE: info dir %s: does not exist\n",
1089 fprintf(outf, "NOTE: it will be created on the next run.\n");
1090 amfree(hostinfodir);
1091 } else if (!S_ISDIR(statbuf.st_mode)) {
1092 fprintf(outf, "ERROR: info dir %s: not a directory\n",
1094 amfree(hostinfodir);
1096 } else if (access(hostinfodir, W_OK) == -1) {
1097 fprintf(outf, "ERROR: info dir %s: not writable\n",
1099 amfree(hostinfodir);
1102 strappend(hostinfodir, "/");
1106 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1107 disk = sanitise_filename(dp->name);
1110 diskdir = newstralloc2(diskdir, hostinfodir, disk);
1111 infofile = vstralloc(diskdir, "/", "info", NULL);
1112 if(stat(diskdir, &statbuf) == -1) {
1113 fprintf(outf, "NOTE: info dir %s: does not exist\n",
1115 fprintf(outf, "NOTE: it will be created on the next run.\n");
1116 } else if (!S_ISDIR(statbuf.st_mode)) {
1117 fprintf(outf, "ERROR: info dir %s: not a directory\n",
1120 } else if (access(diskdir, W_OK) == -1) {
1121 fprintf(outf, "ERROR: info dir %s: not writable\n",
1124 } else if(stat(infofile, &statbuf) == -1) {
1125 fprintf(outf, "WARNING: info file %s: does not exist\n",
1127 fprintf(outf, "NOTE: it will be created on the next run.\n");
1128 } else if (!S_ISREG(statbuf.st_mode)) {
1129 fprintf(outf, "ERROR: info file %s: not a file\n",
1132 } else if (access(infofile, R_OK) == -1) {
1133 fprintf(outf, "ERROR: info file %s: not readable\n",
1141 if(! indexdir_checked) {
1142 if(stat(conf_indexdir, &statbuf) == -1) {
1143 fprintf(outf, "NOTE: index dir %s: does not exist\n",
1145 fprintf(outf, "NOTE: it will be created on the next run.\n");
1146 amfree(conf_indexdir);
1147 } else if (!S_ISDIR(statbuf.st_mode)) {
1148 fprintf(outf, "ERROR: index dir %s: not a directory\n",
1150 amfree(conf_indexdir);
1152 } else if (access(conf_indexdir, W_OK) == -1) {
1153 fprintf(outf, "ERROR: index dir %s: not writable\n",
1155 amfree(conf_indexdir);
1158 strappend(conf_indexdir, "/");
1160 indexdir_checked = 1;
1163 if(! hostindexdir_checked) {
1164 hostindexdir = stralloc2(conf_indexdir, host);
1165 if(stat(hostindexdir, &statbuf) == -1) {
1166 fprintf(outf, "NOTE: index dir %s: does not exist\n",
1168 fprintf(outf, "NOTE: it will be created on the next run.\n");
1169 amfree(hostindexdir);
1170 } else if (!S_ISDIR(statbuf.st_mode)) {
1171 fprintf(outf, "ERROR: index dir %s: not a directory\n",
1173 amfree(hostindexdir);
1175 } else if (access(hostindexdir, W_OK) == -1) {
1176 fprintf(outf, "ERROR: index dir %s: not writable\n",
1178 amfree(hostindexdir);
1181 strappend(hostindexdir, "/");
1183 hostindexdir_checked = 1;
1186 diskdir = newstralloc2(diskdir, hostindexdir, disk);
1187 if(stat(diskdir, &statbuf) == -1) {
1188 fprintf(outf, "NOTE: index dir %s: does not exist\n",
1190 fprintf(outf, "NOTE: it will be created on the next run.\n");
1191 } else if (!S_ISDIR(statbuf.st_mode)) {
1192 fprintf(outf, "ERROR: index dir %s: not a directory\n",
1195 } else if (access(diskdir, W_OK) == -1) {
1196 fprintf(outf, "ERROR: index dir %s: is not writable\n",
1204 remove_disk(origqp, dp);
1207 amfree(hostindexdir);
1208 hostindexdir_checked = 0;
1211 amfree(hostinfodir);
1212 amfree(conf_infofile);
1213 amfree(conf_indexdir);
1219 amfree(config_name);
1221 fprintf(outf, "Server check took %s seconds\n", walltime_str(curclock()));
1225 malloc_size_2 = malloc_inuse(&malloc_hist_2);
1227 if(malloc_size_1 != malloc_size_2) {
1228 malloc_list(fd, malloc_hist_1, malloc_hist_2);
1243 /* --------------------------------------------------- */
1249 #ifdef KRB4_SECURITY
1253 static void handle_response P((proto_t *p, pkt_t *pkt));
1255 #define HOST_READY ((void *)0) /* must be 0 */
1256 #define HOST_ACTIVE ((void *)1)
1257 #define HOST_DONE ((void *)2)
1259 #define DISK_READY ((void *)0) /* must be 0 */
1260 #define DISK_ACTIVE ((void *)1)
1261 #define DISK_DONE ((void *)2)
1263 int start_host(hostp)
1271 char number[NUM_STR_SIZE];
1273 if(hostp->up != HOST_READY) {
1277 if (strncmp (hostp->hostname,"localhost",9) == 0) {
1279 "WARNING: Usage of fully qualified hostname recommended for Client %s.\n",
1284 * The first time through here we send a "noop" request. This will
1285 * return the feature list from the client if it supports that.
1286 * If it does not, handle_result() will set the feature list to an
1287 * empty structure. In either case, we do the disks on the second
1288 * (and subsequent) pass(es).
1291 if(hostp->features != NULL) { /* selfcheck service */
1292 int has_features = am_has_feature(hostp->features,
1293 fe_req_options_features);
1294 int has_hostname = am_has_feature(hostp->features,
1295 fe_req_options_hostname);
1296 int has_maxdumps = am_has_feature(hostp->features,
1297 fe_req_options_maxdumps);
1299 if(!am_has_feature(hostp->features, fe_selfcheck_req) &&
1300 !am_has_feature(hostp->features, fe_selfcheck_req_device)) {
1302 "ERROR: Client %s does not support selfcheck REQ packet.\n",
1305 if(!am_has_feature(hostp->features, fe_selfcheck_rep)) {
1307 "ERROR: Client %s does not support selfcheck REP packet.\n",
1310 if(!am_has_feature(hostp->features, fe_sendsize_req_options) &&
1311 !am_has_feature(hostp->features, fe_sendsize_req_no_options) &&
1312 !am_has_feature(hostp->features, fe_sendsize_req_device)) {
1314 "ERROR: Client %s does not support sendsize REQ packet.\n",
1317 if(!am_has_feature(hostp->features, fe_sendsize_rep)) {
1319 "ERROR: Client %s does not support sendsize REP packet.\n",
1322 if(!am_has_feature(hostp->features, fe_sendbackup_req) &&
1323 !am_has_feature(hostp->features, fe_sendbackup_req_device)) {
1325 "ERROR: Client %s does not support sendbackup REQ packet.\n",
1328 if(!am_has_feature(hostp->features, fe_sendbackup_rep)) {
1330 "ERROR: Client %s does not support sendbackup REP packet.\n",
1334 ap_snprintf(number, sizeof(number), "%d", hostp->maxdumps);
1335 req = vstralloc("SERVICE ", "selfcheck", "\n",
1337 has_features ? "features=" : "",
1338 has_features ? our_feature_string : "",
1339 has_features ? ";" : "",
1340 has_maxdumps ? "maxdumps=" : "",
1341 has_maxdumps ? number : "",
1342 has_maxdumps ? ";" : "",
1343 has_hostname ? "hostname=" : "",
1344 has_hostname ? hostp->hostname : "",
1345 has_hostname ? ";" : "",
1349 req_len = strlen(req);
1350 req_len += 128; /* room for SECURITY ... */
1351 req_len += 256; /* room for non-disk answers */
1352 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1358 if(dp->todo == 0) continue;
1360 if(dp->up != DISK_READY) {
1363 o = optionstr(dp, hostp->features, outf);
1366 if(!am_has_feature(hostp->features, fe_selfcheck_req_device)) {
1368 "ERROR: %s:%s (%s): selfcheck does not support device.\n",
1369 hostp->hostname, dp->name, dp->device);
1371 if(!am_has_feature(hostp->features, fe_sendsize_req_device)) {
1373 "ERROR: %s:%s (%s): sendsize does not support device.\n",
1374 hostp->hostname, dp->name, dp->device);
1376 if(!am_has_feature(hostp->features, fe_sendbackup_req_device)) {
1378 "ERROR: %s:%s (%s): sendbackup does not support device.\n",
1379 hostp->hostname, dp->name, dp->device);
1382 if(strcmp(dp->program, "DUMP") == 0 &&
1383 !am_has_feature(hostp->features, fe_program_dump)) {
1384 fprintf(outf, "ERROR: %s:%s does not support DUMP.\n",
1385 hostp->hostname, dp->name);
1387 if(strcmp(dp->program, "GNUTAR") == 0 &&
1388 !am_has_feature(hostp->features, fe_program_gnutar)) {
1389 fprintf(outf, "ERROR: %s:%s does not support GNUTAR.\n",
1390 hostp->hostname, dp->name);
1392 if(dp->estimate == ES_CALCSIZE &&
1393 !am_has_feature(hostp->features, fe_calcsize_estimate)) {
1394 fprintf(outf, "ERROR: %s:%s does not support CALCSIZE for estimate, using CLIENT.\n",
1395 hostp->hostname, dp->name);
1396 dp->estimate = ES_CLIENT;
1399 if(dp->estimate == ES_CALCSIZE &&
1400 am_has_feature(hostp->features, fe_selfcheck_calcsize))
1401 calcsize = "CALCSIZE ";
1405 l = vstralloc(calcsize,
1408 dp->device ? dp->device : "",
1416 * Allow 2X for error response in return packet.
1418 if(req_len + l_len > MAX_DGRAM / 2) {
1425 dp->up = DISK_ACTIVE;
1430 else { /* noop service */
1431 req = vstralloc("SERVICE ", "noop", "\n",
1433 "features=", our_feature_string, ";",
1436 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1437 if(dp->todo == 0) continue;
1439 if(dp->up != DISK_READY) {
1445 if(disk_count == 0) {
1447 hostp->up = HOST_DONE;
1451 #ifdef KRB4_SECURITY
1452 if(hostp->disks->auth == AUTH_KRB4)
1453 rc = make_krb_request(hostp->hostname, kamanda_port, req,
1454 hostp, conf_ctimeout, handle_response);
1457 rc = make_request(hostp->hostname, amanda_port, req,
1458 hostp, conf_ctimeout, handle_response);
1460 req = NULL; /* do not own this any more */
1463 /* couldn't resolve hostname */
1465 "ERROR: %s: could not resolve hostname\n", hostp->hostname);
1467 hostp->up = HOST_DONE;
1469 hostp->up = HOST_ACTIVE;
1474 int start_client_checks(fd)
1480 struct servent *amandad;
1483 switch(pid = fork()) {
1484 case -1: error("could not fork client check: %s", strerror(errno));
1493 set_pname("amcheck-clients");
1497 if((outf = fdopen(fd, "w")) == NULL)
1498 error("fdopen %d: %s", fd, strerror(errno));
1501 fprintf(outf, "\nAmanda Backup Client Hosts Check\n");
1502 fprintf(outf, "--------------------------------\n");
1504 #ifdef KRB4_SECURITY
1505 kerberos_service_init();
1508 proto_init(msg->socket, time(0), 1024);
1510 /* get remote service port */
1511 if((amandad = getservbyname(AMANDA_SERVICE_NAME, "udp")) == NULL)
1512 amanda_port = AMANDA_SERVICE_DEFAULT;
1514 amanda_port = ntohs(amandad->s_port);
1516 #ifdef KRB4_SECURITY
1517 if((amandad = getservbyname(KAMANDA_SERVICE_NAME, "udp")) == NULL)
1518 kamanda_port = KAMANDA_SERVICE_DEFAULT;
1520 kamanda_port = ntohs(amandad->s_port);
1523 hostcount = remote_errors = 0;
1525 for(dp = origqp->head; dp != NULL; dp = dp->next) {
1527 if(hostp->up == HOST_READY) {
1528 if(start_host(hostp) == 1) {
1539 "Client check: %d host%s checked in %s seconds, %d problem%s found\n",
1540 hostcount, (hostcount == 1) ? "" : "s",
1541 walltime_str(curclock()),
1542 remote_errors, (remote_errors == 1) ? "" : "s");
1546 amfree(config_name);
1548 malloc_size_2 = malloc_inuse(&malloc_hist_2);
1550 if(malloc_size_1 != malloc_size_2) {
1551 malloc_list(fd, malloc_hist_1, malloc_hist_2);
1554 exit(userbad || remote_errors > 0);
1559 static void handle_response(p, pkt)
1571 hostp = (am_host_t *) p->datap;
1572 hostp->up = HOST_READY;
1574 if(p->state == S_FAILED && pkt == NULL) {
1575 if(p->prevstate == S_REPWAIT) {
1577 "WARNING: %s: selfcheck reply timed out.\n",
1582 "WARNING: %s: selfcheck request timed out. Host down?\n",
1586 hostp->up = HOST_DONE;
1590 #ifdef KRB4_SECURITY
1591 if(hostp->disks->auth == AUTH_KRB4 &&
1592 !check_mutual_authenticator(host2key(hostp->hostname), pkt, p)) {
1593 fprintf(outf, "ERROR: %s [mutual-authentication failed]\n",
1596 hostp->up = HOST_DONE;
1602 fprintf(errf, "got %sresponse from %s:\n----\n%s----\n\n",
1603 (p->state == S_FAILED) ? "NAK " : "", hostp->hostname, pkt->body);
1611 if (s[-2] == '\n') {
1615 #define sc "OPTIONS "
1616 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
1619 #define sc "features="
1620 t = strstr(line, sc);
1621 if(t != NULL && (isspace((int)t[-1]) || t[-1] == ';')) {
1624 am_release_feature_set(hostp->features);
1625 if((hostp->features = am_string_to_feature(t)) == NULL) {
1626 fprintf(outf, "ERROR: %s: bad features value: %s\n",
1627 hostp->hostname, line);
1635 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
1641 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
1642 t = line + sizeof(sc)-1;
1646 skip_whitespace(t, tch);
1648 * If the "error" is that the "noop" service is unknown, it
1649 * just means the client is "old" (does not support the service).
1650 * We can ignore this.
1652 if(hostp->features == NULL
1653 && p->state == S_FAILED
1654 && (strcmp(t - 1, "unknown service: noop") == 0
1655 || strcmp(t - 1, "noop: invalid service") == 0)) {
1657 fprintf(outf, "ERROR: %s%s: %s\n",
1658 (p->state == S_FAILED) ? "NAK " : "",
1662 hostp->up = HOST_DONE;
1667 fprintf(outf, "ERROR: %s: unknown response: %s\n",
1668 hostp->hostname, line);
1670 hostp->up = HOST_DONE;
1672 if(hostp->up == HOST_READY && hostp->features == NULL) {
1674 * The client does not support the features list, so give it an
1677 dbprintf(("%s: no feature set from host %s\n",
1678 debug_prefix_time(NULL), hostp->hostname));
1679 hostp->features = am_set_default_feature_set();
1681 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1682 if(dp->up == DISK_ACTIVE) {