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 * Author: James da Silva, Systems Design and Analysis Group
24 * Computer Science Department
25 * University of Maryland at College Park
28 * $Id: driverio.c,v 1.92 2006/08/24 01:57:16 paddy_s Exp $
30 * I/O-related functions for driver program
35 #include "server_util.h"
42 #define GLOBAL /* the global variables defined here */
47 static const char *childstr(int);
56 for(dumper = dmptable; dumper < dmptable + MAX_DUMPERS; dumper++) {
66 static char buf[NUM_STR_SIZE + 32];
72 for (dumper = dmptable; dumper < dmptable + MAX_DUMPERS; dumper++) {
74 return (dumper->name);
75 if (dumper->chunker->fd == fd)
76 return (dumper->chunker->name);
78 snprintf(buf, SIZEOF(buf), "unknown child (fd %d)", fd);
88 char **config_options;
90 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
91 error("taper pipe: %s", strerror(errno));
94 if(fd[0] < 0 || fd[0] >= (int)FD_SETSIZE) {
95 error("taper socketpair 0: descriptor %d out of range (0 .. %d)\n",
99 if(fd[1] < 0 || fd[1] >= (int)FD_SETSIZE) {
100 error("taper socketpair 1: descriptor %d out of range (0 .. %d)\n",
101 fd[1], FD_SETSIZE-1);
105 switch(taper_pid = fork()) {
107 error("fork taper: %s", strerror(errno));
110 case 0: /* child process */
112 if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
113 error("taper dup2: %s", strerror(errno));
114 config_options = get_config_options(2);
115 config_options[0] = "taper";
116 config_options[1] = config_name;
117 execve(taper_program, config_options, safe_env());
118 error("exec %s: %s", taper_program, strerror(errno));
121 default: /* parent process */
124 taper_ev_read = NULL;
129 startup_dump_process(
131 char *dumper_program)
134 char **config_options;
136 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
137 error("%s pipe: %s", dumper->name, strerror(errno));
141 switch(dumper->pid = fork()) {
143 error("fork %s: %s", dumper->name, strerror(errno));
146 case 0: /* child process */
148 if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
149 error("%s dup2: %s", dumper->name, strerror(errno));
150 config_options = get_config_options(2);
151 config_options[0] = dumper->name ? dumper->name : "dumper",
152 config_options[1] = config_name;
153 execve(dumper_program, config_options, safe_env());
154 error("exec %s (%s): %s", dumper_program,
155 dumper->name, strerror(errno));
158 default: /* parent process */
161 dumper->ev_read = NULL;
162 dumper->busy = dumper->down = 0;
164 fprintf(stderr,"driver: started %s pid %u\n",
165 dumper->name, (unsigned)dumper->pid);
171 startup_dump_processes(
172 char *dumper_program,
178 char number[NUM_STR_SIZE];
180 for(dumper = dmptable, i = 0; i < inparallel; dumper++, i++) {
181 snprintf(number, SIZEOF(number), "%d", i);
182 dumper->name = stralloc2("dumper", number);
183 dumper->chunker = &chktable[i];
184 chktable[i].name = stralloc2("chunker", number);
185 chktable[i].dumper = dumper;
188 startup_dump_process(dumper, dumper_program);
189 dumper_cmd(dumper, START, (void *)timestamp);
194 startup_chunk_process(
196 char *chunker_program)
199 char **config_options;
201 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
202 error("%s pipe: %s", chunker->name, strerror(errno));
206 switch(chunker->pid = fork()) {
208 error("fork %s: %s", chunker->name, strerror(errno));
211 case 0: /* child process */
213 if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) {
214 error("%s dup2: %s", chunker->name, strerror(errno));
217 config_options = get_config_options(2);
218 config_options[0] = chunker->name ? chunker->name : "chunker",
219 config_options[1] = config_name;
220 execve(chunker_program, config_options, safe_env());
221 error("exec %s (%s): %s", chunker_program,
222 chunker->name, strerror(errno));
225 default: /* parent process */
229 chunker->ev_read = NULL;
230 fprintf(stderr,"driver: started %s pid %u\n",
231 chunker->name, (unsigned)chunker->pid);
248 if((line = areads(fd)) == NULL) {
250 error("reading result from %s: %s", childstr(fd), strerror(errno));
253 *result_argc = 0; /* EOF */
255 *result_argc = split(line, result_argv, max_arg, " ");
259 printf("driver: result time %s from %s:",
260 walltime_str(curclock()),
263 for(arg = 1; arg <= *result_argc; arg++) {
264 printf(" %s", result_argv[arg]);
275 printf("argc = %d\n", *result_argc);
276 for(arg = 0; arg < *result_argc; arg++)
277 printf("argv[%d] = \"%s\"\n", arg, result_argv[arg]);
280 if(*result_argc < 1) return BOGUS;
282 for(t = (cmd_t)(BOGUS+1); t < LAST_TOK; t++)
283 if(strcmp(result_argv[1], cmdstr[t]) == 0) return t;
297 char *cmdline = NULL;
298 char number[NUM_STR_SIZE];
299 char splitsize[NUM_STR_SIZE];
300 char fallback_splitsize[NUM_STR_SIZE];
301 char *diskbuffer = NULL;
309 cmdline = vstralloc(cmdstr[cmd], " ", (char *)ptr, "\n", NULL);
313 qname = quote_string(dp->name);
314 qdest = quote_string(destname);
315 snprintf(number, SIZEOF(number), "%d", level);
316 snprintf(splitsize, SIZEOF(splitsize), OFF_T_FMT,
317 (OFF_T_FMT_TYPE)dp->tape_splitsize);
318 features = am_feature_to_string(dp->host->features);
319 cmdline = vstralloc(cmdstr[cmd],
320 " ", disk2serial(dp),
322 " ", dp->host->hostname,
335 qname = quote_string(dp->name);
336 snprintf(number, SIZEOF(number), "%d", level);
339 If we haven't been given a place to buffer split dumps to disk,
340 make the argument something besides and empty string so's taper
343 if(!dp->split_diskbuffer || dp->split_diskbuffer[0] == '\0'){
346 diskbuffer = dp->split_diskbuffer;
348 snprintf(splitsize, SIZEOF(splitsize), OFF_T_FMT,
349 (OFF_T_FMT_TYPE)dp->tape_splitsize);
350 snprintf(fallback_splitsize, SIZEOF(fallback_splitsize), OFF_T_FMT,
351 (OFF_T_FMT_TYPE)dp->fallback_splitsize);
352 features = am_feature_to_string(dp->host->features);
353 cmdline = vstralloc(cmdstr[cmd],
354 " ", disk2serial(dp),
355 " ", dp->host->hostname,
362 " ", fallback_splitsize,
368 cmdline = stralloc2(cmdstr[cmd], "\n");
371 error("Don't know how to send %s command to taper", cmdstr[cmd]);
376 * Note: cmdline already has a '\n'.
378 printf("driver: send-cmd time %s to taper: %s",
379 walltime_str(curclock()), cmdline);
381 if ((fullwrite(taper, cmdline, strlen(cmdline))) < 0) {
382 printf("writing taper command '%s' failed: %s\n",
383 cmdline, strerror(errno));
388 if(cmd == QUIT) aclose(taper);
399 char *cmdline = NULL;
400 char number[NUM_STR_SIZE];
401 char numberport[NUM_STR_SIZE];
410 cmdline = vstralloc(cmdstr[cmd], " ", (char *)dp, "\n", NULL);
413 if(dp && dp->device) {
421 device = quote_string((dp->device) ? dp->device : "NODEVICE");
422 qname = quote_string(dp->name);
423 snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
424 snprintf(numberport, SIZEOF(numberport), "%d", dumper->output_port);
425 features = am_feature_to_string(dp->host->features);
426 o = optionstr(dp, dp->host->features, NULL);
428 error("problem with option string, check the dumptype definition.\n");
431 cmdline = vstralloc(cmdstr[cmd],
432 " ", disk2serial(dp),
434 " ", dp->host->hostname,
439 " ", sched(dp)->dumpdate,
441 " ", dp->amandad_path,
442 " ", dp->client_username,
451 error("PORT-DUMP without disk pointer\n");
458 qdest = quote_string(sched(dp)->destname);
459 cmdline = vstralloc(cmdstr[cmd],
464 cmdline = stralloc2(cmdstr[cmd], "\n");
468 error("Don't know how to send %s command to dumper", cmdstr[cmd]);
473 * Note: cmdline already has a '\n'.
476 printf("driver: send-cmd time %s ignored to down dumper %s: %s",
477 walltime_str(curclock()), dumper->name, cmdline);
479 printf("driver: send-cmd time %s to %s: %s",
480 walltime_str(curclock()), dumper->name, cmdline);
482 if (fullwrite(dumper->fd, cmdline, strlen(cmdline)) < 0) {
483 printf("writing %s command: %s\n", dumper->name, strerror(errno));
488 if (cmd == QUIT) aclose(dumper->fd);
500 char *cmdline = NULL;
501 char number[NUM_STR_SIZE];
502 char chunksize[NUM_STR_SIZE];
503 char use[NUM_STR_SIZE];
506 assignedhd_t **h=NULL;
513 cmdline = vstralloc(cmdstr[cmd], " ", (char *)dp, "\n", NULL);
516 if(dp && sched(dp) && sched(dp)->holdp) {
517 h = sched(dp)->holdp;
518 activehd = sched(dp)->activehd;
522 qname = quote_string(dp->name);
523 qdest = quote_string(sched(dp)->destname);
524 holdalloc(h[activehd]->disk)->allocated_dumpers++;
525 snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
526 snprintf(chunksize, SIZEOF(chunksize), OFF_T_FMT,
527 (OFF_T_FMT_TYPE)holdingdisk_get_chunksize(h[0]->disk));
528 snprintf(use, SIZEOF(use), OFF_T_FMT,
529 (OFF_T_FMT_TYPE)h[0]->reserved);
530 features = am_feature_to_string(dp->host->features);
531 o = optionstr(dp, dp->host->features, NULL);
533 error("problem with option string, check the dumptype definition.\n");
535 cmdline = vstralloc(cmdstr[cmd],
536 " ", disk2serial(dp),
538 " ", dp->host->hostname,
542 " ", sched(dp)->dumpdate,
553 error("%s command without disk and holding disk.\n",
559 if(dp && sched(dp) && sched(dp)->holdp) {
560 h = sched(dp)->holdp;
561 activehd = sched(dp)->activehd;
565 qname = quote_string(dp->name);
566 qdest = quote_string(h[activehd]->destname);
567 holdalloc(h[activehd]->disk)->allocated_dumpers++;
568 snprintf(chunksize, SIZEOF(chunksize), OFF_T_FMT,
569 (OFF_T_FMT_TYPE)holdingdisk_get_chunksize(h[activehd]->disk));
570 snprintf(use, SIZEOF(use), OFF_T_FMT,
571 (OFF_T_FMT_TYPE)(h[activehd]->reserved - h[activehd]->used));
572 cmdline = vstralloc(cmdstr[cmd],
573 " ", disk2serial(dp),
581 cmdline = stralloc2(cmdstr[cmd], "\n");
586 cmdline = stralloc2(cmdstr[cmd], "\n");
591 cmdline = vstralloc(cmdstr[cmd],
592 " ", disk2serial(dp),
595 cmdline = vstralloc(cmdstr[cmd], "\n");
599 error("Don't know how to send %s command to chunker", cmdstr[cmd]);
604 * Note: cmdline already has a '\n'.
606 printf("driver: send-cmd time %s to %s: %s",
607 walltime_str(curclock()), chunker->name, cmdline);
609 if (fullwrite(chunker->fd, cmdline, strlen(cmdline)) < 0) {
610 printf("writing %s command: %s\n", chunker->name, strerror(errno));
615 if (cmd == QUIT) aclose(chunker->fd);
620 #define MAX_SERIAL MAX_DUMPERS+1 /* one for the taper */
627 } stable[MAX_SERIAL];
636 rc = sscanf(str, "%d-%ld", &s, &gen);
638 error("error [serial2disk \"%s\" parse error]", str);
640 } else if (s < 0 || s >= MAX_SERIAL) {
641 error("error [serial out of range 0..%d: %d]", MAX_SERIAL, s);
644 if(gen != stable[s].gen)
645 printf("driver: serial2disk error time %s serial gen mismatch %s\n",
646 walltime_str(curclock()), str);
657 rc = sscanf(str, "%d-%ld", &s, &gen);
658 if(!(rc == 2 && s >= 0 && s < MAX_SERIAL)) {
659 /* nuke self to get core dump for Brett */
660 fprintf(stderr, "driver: free_serial: str \"%s\" rc %d s %d\n",
666 if(gen != stable[s].gen)
667 printf("driver: free_serial error time %s serial gen mismatch %s\n",
668 walltime_str(curclock()),str);
680 for(s = 0; s < MAX_SERIAL; s++) {
681 if(stable[s].dp == dp) {
688 printf("driver: error time %s serial not found\n",
689 walltime_str(curclock()));
694 check_unfree_serial(void)
698 /* find used serial number */
699 for(s = 0; s < MAX_SERIAL; s++) {
700 if(stable[s].gen != 0 || stable[s].dp != NULL) {
701 printf("driver: error time %s bug: serial in use: %02d-%05ld\n",
702 walltime_str(curclock()), s, stable[s].gen);
711 static char str[NUM_STR_SIZE];
713 for(s = 0; s < MAX_SERIAL; s++) {
714 if(stable[s].dp == dp) {
715 snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
720 /* find unused serial number */
721 for(s = 0; s < MAX_SERIAL; s++)
722 if(stable[s].gen == 0 && stable[s].dp == NULL)
724 if(s >= MAX_SERIAL) {
725 printf("driver: error time %s bug: out of serial numbers\n",
726 walltime_str(curclock()));
730 stable[s].gen = generation++;
733 snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
750 level = sched(dp)->level;
752 conf_infofile = getconf_str(CNF_INFOFILE);
753 if (*conf_infofile == '/') {
754 conf_infofile = stralloc(conf_infofile);
756 conf_infofile = stralloc2(config_dir, conf_infofile);
758 if (open_infofile(conf_infofile)) {
759 error("could not open info db \"%s\"", conf_infofile);
762 amfree(conf_infofile);
764 get_info(dp->host->hostname, dp->name, &info);
766 /* Clean up information about this and higher-level dumps. This
767 assumes that update_info_dumper() is always run before
768 update_info_taper(). */
769 for (i = level; i < DUMP_LEVELS; ++i) {
771 infp->size = (off_t)-1;
772 infp->csize = (off_t)-1;
773 infp->secs = (time_t)-1;
774 infp->date = (time_t)-1;
775 infp->label[0] = '\0';
779 /* now store information about this dump */
780 infp = &info.inf[level];
781 infp->size = origsize;
782 infp->csize = dumpsize;
783 infp->secs = dumptime;
784 infp->date = sched(dp)->timestamp;
786 if(level == 0) perfp = &info.full;
787 else perfp = &info.incr;
789 /* Update the stats, but only if the new values are meaningful */
790 if(dp->compress != COMP_NONE && origsize > (off_t)0) {
791 newperf(perfp->comp, (double)dumpsize/(double)origsize);
793 if(dumptime > (time_t)0) {
794 if((off_t)dumptime >= dumpsize)
795 newperf(perfp->rate, 1);
797 newperf(perfp->rate, (double)dumpsize/(double)dumptime);
800 if(getconf_int(CNF_RESERVE)<100) {
801 info.command = NO_COMMAND;
804 if(level == info.last_level)
805 info.consecutive_runs++;
807 info.last_level = level;
808 info.consecutive_runs = 1;
811 if(origsize >= (off_t)0 && dumpsize >= (off_t)0) {
812 for(i=NB_HISTORY-1;i>0;i--) {
813 info.history[i] = info.history[i-1];
816 info.history[0].level = level;
817 info.history[0].size = origsize;
818 info.history[0].csize = dumpsize;
819 info.history[0].date = sched(dp)->timestamp;
820 info.history[0].secs = dumptime;
823 if(put_info(dp->host->hostname, dp->name, &info)) {
824 error("infofile update failed (%s,'%s')\n", dp->host->hostname, dp->name);
842 rc = open_infofile(getconf_str(CNF_INFOFILE));
844 error("could not open infofile %s: %s (%d)", getconf_str(CNF_INFOFILE),
845 strerror(errno), rc);
849 get_info(dp->host->hostname, dp->name, &info);
851 infp = &info.inf[level];
852 /* XXX - should we record these two if no-record? */
853 strncpy(infp->label, label, SIZEOF(infp->label)-1);
854 infp->label[SIZEOF(infp->label)-1] = '\0';
855 infp->filenum = filenum;
857 info.command = NO_COMMAND;
859 if(put_info(dp->host->hostname, dp->name, &info)) {
860 error("infofile update failed (%s,'%s')\n", dp->host->hostname, dp->name);
866 /* Free an array of pointers to assignedhd_t after freeing the
867 * assignedhd_t themselves. The array must be NULL-terminated.
869 void free_assignedhd(
874 if( !ahd ) { return; }
876 for( i = 0; ahd[i]; i++ ) {
877 amfree(ahd[i]->destname);