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);
89 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
90 error("taper pipe: %s", strerror(errno));
93 if(fd[0] < 0 || fd[0] >= (int)FD_SETSIZE) {
94 error("taper socketpair 0: descriptor %d out of range (0 .. %d)\n",
98 if(fd[1] < 0 || fd[1] >= (int)FD_SETSIZE) {
99 error("taper socketpair 1: descriptor %d out of range (0 .. %d)\n",
100 fd[1], FD_SETSIZE-1);
104 switch(taper_pid = fork()) {
106 error("fork taper: %s", strerror(errno));
109 case 0: /* child process */
111 if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
112 error("taper dup2: %s", strerror(errno));
113 execle(taper_program, "taper", config_name, (char *)0, safe_env());
114 error("exec %s: %s", taper_program, strerror(errno));
117 default: /* parent process */
120 taper_ev_read = NULL;
125 startup_dump_process(
127 char *dumper_program)
131 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
132 error("%s pipe: %s", dumper->name, strerror(errno));
136 switch(dumper->pid = fork()) {
138 error("fork %s: %s", dumper->name, strerror(errno));
141 case 0: /* child process */
143 if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
144 error("%s dup2: %s", dumper->name, strerror(errno));
145 execle(dumper_program,
146 dumper->name ? dumper->name : "dumper",
150 error("exec %s (%s): %s", dumper_program,
151 dumper->name, strerror(errno));
154 default: /* parent process */
157 dumper->ev_read = NULL;
158 dumper->busy = dumper->down = 0;
160 fprintf(stderr,"driver: started %s pid %u\n",
161 dumper->name, (unsigned)dumper->pid);
167 startup_dump_processes(
168 char *dumper_program,
174 char number[NUM_STR_SIZE];
176 for(dumper = dmptable, i = 0; i < inparallel; dumper++, i++) {
177 snprintf(number, SIZEOF(number), "%d", i);
178 dumper->name = stralloc2("dumper", number);
179 dumper->chunker = &chktable[i];
180 chktable[i].name = stralloc2("chunker", number);
181 chktable[i].dumper = dumper;
184 startup_dump_process(dumper, dumper_program);
185 dumper_cmd(dumper, START, (void *)timestamp);
190 startup_chunk_process(
192 char *chunker_program)
196 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
197 error("%s pipe: %s", chunker->name, strerror(errno));
201 switch(chunker->pid = fork()) {
203 error("fork %s: %s", chunker->name, strerror(errno));
206 case 0: /* child process */
208 if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) {
209 error("%s dup2: %s", chunker->name, strerror(errno));
212 execle(chunker_program,
213 chunker->name ? chunker->name : "chunker",
217 error("exec %s (%s): %s", chunker_program,
218 chunker->name, strerror(errno));
221 default: /* parent process */
225 chunker->ev_read = NULL;
226 fprintf(stderr,"driver: started %s pid %u\n",
227 chunker->name, (unsigned)chunker->pid);
244 if((line = areads(fd)) == NULL) {
246 error("reading result from %s: %s", childstr(fd), strerror(errno));
249 *result_argc = 0; /* EOF */
251 *result_argc = split(line, result_argv, max_arg, " ");
255 printf("driver: result time %s from %s:",
256 walltime_str(curclock()),
259 for(arg = 1; arg <= *result_argc; arg++) {
260 printf(" %s", result_argv[arg]);
271 printf("argc = %d\n", *result_argc);
272 for(arg = 0; arg < *result_argc; arg++)
273 printf("argv[%d] = \"%s\"\n", arg, result_argv[arg]);
276 if(*result_argc < 1) return BOGUS;
278 for(t = (cmd_t)(BOGUS+1); t < LAST_TOK; t++)
279 if(strcmp(result_argv[1], cmdstr[t]) == 0) return t;
293 char *cmdline = NULL;
294 char number[NUM_STR_SIZE];
295 char splitsize[NUM_STR_SIZE];
296 char fallback_splitsize[NUM_STR_SIZE];
297 char *diskbuffer = NULL;
305 cmdline = vstralloc(cmdstr[cmd], " ", (char *)ptr, "\n", NULL);
309 qname = quote_string(dp->name);
310 qdest = quote_string(destname);
311 snprintf(number, SIZEOF(number), "%d", level);
312 snprintf(splitsize, SIZEOF(splitsize), OFF_T_FMT,
313 (OFF_T_FMT_TYPE)dp->tape_splitsize);
314 features = am_feature_to_string(dp->host->features);
315 cmdline = vstralloc(cmdstr[cmd],
316 " ", disk2serial(dp),
318 " ", dp->host->hostname,
331 qname = quote_string(dp->name);
332 snprintf(number, SIZEOF(number), "%d", level);
335 If we haven't been given a place to buffer split dumps to disk,
336 make the argument something besides and empty string so's taper
339 if(!dp->split_diskbuffer || dp->split_diskbuffer[0] == '\0'){
342 diskbuffer = dp->split_diskbuffer;
344 snprintf(splitsize, SIZEOF(splitsize), OFF_T_FMT,
345 (OFF_T_FMT_TYPE)dp->tape_splitsize);
346 snprintf(fallback_splitsize, SIZEOF(fallback_splitsize), OFF_T_FMT,
347 (OFF_T_FMT_TYPE)dp->fallback_splitsize);
348 features = am_feature_to_string(dp->host->features);
349 cmdline = vstralloc(cmdstr[cmd],
350 " ", disk2serial(dp),
351 " ", dp->host->hostname,
358 " ", fallback_splitsize,
364 cmdline = stralloc2(cmdstr[cmd], "\n");
367 error("Don't know how to send %s command to taper", cmdstr[cmd]);
372 * Note: cmdline already has a '\n'.
374 printf("driver: send-cmd time %s to taper: %s",
375 walltime_str(curclock()), cmdline);
377 if ((fullwrite(taper, cmdline, strlen(cmdline))) < 0) {
378 printf("writing taper command '%s' failed: %s\n",
379 cmdline, strerror(errno));
384 if(cmd == QUIT) aclose(taper);
395 char *cmdline = NULL;
396 char number[NUM_STR_SIZE];
397 char numberport[NUM_STR_SIZE];
406 cmdline = vstralloc(cmdstr[cmd], " ", (char *)dp, "\n", NULL);
409 if(dp && dp->device) {
417 device = quote_string((dp->device) ? dp->device : "NODEVICE");
418 qname = quote_string(dp->name);
419 snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
420 snprintf(numberport, SIZEOF(numberport), "%d", dumper->output_port);
421 features = am_feature_to_string(dp->host->features);
422 o = optionstr(dp, dp->host->features, NULL);
424 error("problem with option string, check the dumptype definition.\n");
427 cmdline = vstralloc(cmdstr[cmd],
428 " ", disk2serial(dp),
430 " ", dp->host->hostname,
435 " ", sched(dp)->dumpdate,
437 " ", dp->amandad_path,
438 " ", dp->client_username,
447 error("PORT-DUMP without disk pointer\n");
454 qdest = quote_string(sched(dp)->destname);
455 cmdline = vstralloc(cmdstr[cmd],
460 cmdline = stralloc2(cmdstr[cmd], "\n");
464 error("Don't know how to send %s command to dumper", cmdstr[cmd]);
469 * Note: cmdline already has a '\n'.
472 printf("driver: send-cmd time %s ignored to down dumper %s: %s",
473 walltime_str(curclock()), dumper->name, cmdline);
475 printf("driver: send-cmd time %s to %s: %s",
476 walltime_str(curclock()), dumper->name, cmdline);
478 if (fullwrite(dumper->fd, cmdline, strlen(cmdline)) < 0) {
479 printf("writing %s command: %s\n", dumper->name, strerror(errno));
484 if (cmd == QUIT) aclose(dumper->fd);
496 char *cmdline = NULL;
497 char number[NUM_STR_SIZE];
498 char chunksize[NUM_STR_SIZE];
499 char use[NUM_STR_SIZE];
502 assignedhd_t **h=NULL;
509 cmdline = vstralloc(cmdstr[cmd], " ", (char *)dp, "\n", NULL);
512 if(dp && sched(dp) && sched(dp)->holdp) {
513 h = sched(dp)->holdp;
514 activehd = sched(dp)->activehd;
518 qname = quote_string(dp->name);
519 qdest = quote_string(sched(dp)->destname);
520 holdalloc(h[activehd]->disk)->allocated_dumpers++;
521 snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
522 snprintf(chunksize, SIZEOF(chunksize), OFF_T_FMT,
523 (OFF_T_FMT_TYPE)holdingdisk_get_chunksize(h[0]->disk));
524 snprintf(use, SIZEOF(use), OFF_T_FMT,
525 (OFF_T_FMT_TYPE)h[0]->reserved);
526 features = am_feature_to_string(dp->host->features);
527 o = optionstr(dp, dp->host->features, NULL);
529 error("problem with option string, check the dumptype definition.\n");
531 cmdline = vstralloc(cmdstr[cmd],
532 " ", disk2serial(dp),
534 " ", dp->host->hostname,
538 " ", sched(dp)->dumpdate,
549 error("Write command without disk and holding disk.\n",
555 if(dp && sched(dp) && sched(dp)->holdp) {
556 h = sched(dp)->holdp;
557 activehd = sched(dp)->activehd;
561 qname = quote_string(dp->name);
562 qdest = quote_string(h[activehd]->destname);
563 holdalloc(h[activehd]->disk)->allocated_dumpers++;
564 snprintf(chunksize, SIZEOF(chunksize), OFF_T_FMT,
565 (OFF_T_FMT_TYPE)holdingdisk_get_chunksize(h[activehd]->disk));
566 snprintf(use, SIZEOF(use), OFF_T_FMT,
567 (OFF_T_FMT_TYPE)(h[activehd]->reserved - h[activehd]->used));
568 cmdline = vstralloc(cmdstr[cmd],
569 " ", disk2serial(dp),
577 cmdline = stralloc2(cmdstr[cmd], "\n");
582 cmdline = stralloc2(cmdstr[cmd], "\n");
587 cmdline = vstralloc(cmdstr[cmd],
588 " ", disk2serial(dp),
591 cmdline = vstralloc(cmdstr[cmd], "\n");
595 error("Don't know how to send %s command to chunker", cmdstr[cmd]);
600 * Note: cmdline already has a '\n'.
602 printf("driver: send-cmd time %s to %s: %s",
603 walltime_str(curclock()), chunker->name, cmdline);
605 if (fullwrite(chunker->fd, cmdline, strlen(cmdline)) < 0) {
606 printf("writing %s command: %s\n", chunker->name, strerror(errno));
611 if (cmd == QUIT) aclose(chunker->fd);
616 #define MAX_SERIAL MAX_DUMPERS+1 /* one for the taper */
623 } stable[MAX_SERIAL];
632 rc = sscanf(str, "%d-%ld", &s, &gen);
634 error("error [serial2disk \"%s\" parse error]", str);
636 } else if (s < 0 || s >= MAX_SERIAL) {
637 error("error [serial out of range 0..%d: %d]", MAX_SERIAL, s);
640 if(gen != stable[s].gen)
641 printf("driver: serial2disk error time %s serial gen mismatch %s\n",
642 walltime_str(curclock()), str);
653 rc = sscanf(str, "%d-%ld", &s, &gen);
654 if(!(rc == 2 && s >= 0 && s < MAX_SERIAL)) {
655 /* nuke self to get core dump for Brett */
656 fprintf(stderr, "driver: free_serial: str \"%s\" rc %d s %d\n",
662 if(gen != stable[s].gen)
663 printf("driver: free_serial error time %s serial gen mismatch %s\n",
664 walltime_str(curclock()),str);
676 for(s = 0; s < MAX_SERIAL; s++) {
677 if(stable[s].dp == dp) {
684 printf("driver: error time %s serial not found\n",
685 walltime_str(curclock()));
690 check_unfree_serial(void)
694 /* find used serial number */
695 for(s = 0; s < MAX_SERIAL; s++) {
696 if(stable[s].gen != 0 || stable[s].dp != NULL) {
697 printf("driver: error time %s bug: serial in use: %02d-%05ld\n",
698 walltime_str(curclock()), s, stable[s].gen);
707 static char str[NUM_STR_SIZE];
709 for(s = 0; s < MAX_SERIAL; s++) {
710 if(stable[s].dp == dp) {
711 snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
716 /* find unused serial number */
717 for(s = 0; s < MAX_SERIAL; s++)
718 if(stable[s].gen == 0 && stable[s].dp == NULL)
720 if(s >= MAX_SERIAL) {
721 printf("driver: error time %s bug: out of serial numbers\n",
722 walltime_str(curclock()));
726 stable[s].gen = generation++;
729 snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
746 level = sched(dp)->level;
748 conf_infofile = getconf_str(CNF_INFOFILE);
749 if (*conf_infofile == '/') {
750 conf_infofile = stralloc(conf_infofile);
752 conf_infofile = stralloc2(config_dir, conf_infofile);
754 if (open_infofile(conf_infofile)) {
755 error("could not open info db \"%s\"", conf_infofile);
758 amfree(conf_infofile);
760 get_info(dp->host->hostname, dp->name, &info);
762 /* Clean up information about this and higher-level dumps. This
763 assumes that update_info_dumper() is always run before
764 update_info_taper(). */
765 for (i = level; i < DUMP_LEVELS; ++i) {
767 infp->size = (off_t)-1;
768 infp->csize = (off_t)-1;
769 infp->secs = (time_t)-1;
770 infp->date = (time_t)-1;
771 infp->label[0] = '\0';
775 /* now store information about this dump */
776 infp = &info.inf[level];
777 infp->size = origsize;
778 infp->csize = dumpsize;
779 infp->secs = dumptime;
780 infp->date = sched(dp)->timestamp;
782 if(level == 0) perfp = &info.full;
783 else perfp = &info.incr;
785 /* Update the stats, but only if the new values are meaningful */
786 if(dp->compress != COMP_NONE && origsize > (off_t)0) {
787 newperf(perfp->comp, (double)dumpsize/(double)origsize);
789 if(dumptime > (time_t)0) {
790 if((off_t)dumptime >= dumpsize)
791 newperf(perfp->rate, 1);
793 newperf(perfp->rate, (double)dumpsize/(double)dumptime);
796 if(getconf_int(CNF_RESERVE)<100) {
797 info.command = NO_COMMAND;
800 if(level == info.last_level)
801 info.consecutive_runs++;
803 info.last_level = level;
804 info.consecutive_runs = 1;
807 if(origsize >= (off_t)0 && dumpsize >= (off_t)0) {
808 for(i=NB_HISTORY-1;i>0;i--) {
809 info.history[i] = info.history[i-1];
812 info.history[0].level = level;
813 info.history[0].size = origsize;
814 info.history[0].csize = dumpsize;
815 info.history[0].date = sched(dp)->timestamp;
816 info.history[0].secs = dumptime;
819 if(put_info(dp->host->hostname, dp->name, &info)) {
820 error("infofile update failed (%s,'%s')\n", dp->host->hostname, dp->name);
838 rc = open_infofile(getconf_str(CNF_INFOFILE));
840 error("could not open infofile %s: %s (%d)", getconf_str(CNF_INFOFILE),
841 strerror(errno), rc);
845 get_info(dp->host->hostname, dp->name, &info);
847 infp = &info.inf[level];
848 /* XXX - should we record these two if no-record? */
849 strncpy(infp->label, label, SIZEOF(infp->label)-1);
850 infp->label[SIZEOF(infp->label)-1] = '\0';
851 infp->filenum = filenum;
853 info.command = NO_COMMAND;
855 if(put_info(dp->host->hostname, dp->name, &info)) {
856 error("infofile update failed (%s,'%s')\n", dp->host->hostname, dp->name);
862 /* Free an array of pointers to assignedhd_t after freeing the
863 * assignedhd_t themselves. The array must be NULL-terminated.
865 void free_assignedhd(
870 if( !ahd ) { return; }
872 for( i = 0; ahd[i]; i++ ) {
873 amfree(ahd[i]->destname);