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 && dumper->chunker->fd == fd)
76 return (dumper->chunker->name);
78 g_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"),
96 fd[0], (int)FD_SETSIZE-1);
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], (int)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;
118 execve(taper_program, config_options, safe_env());
119 error("exec %s: %s", taper_program, strerror(errno));
122 default: /* parent process */
125 taper_ev_read = NULL;
130 startup_dump_process(
132 char *dumper_program)
135 char **config_options;
137 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
138 error(_("%s pipe: %s"), dumper->name, strerror(errno));
142 switch(dumper->pid = fork()) {
144 error(_("fork %s: %s"), dumper->name, strerror(errno));
147 case 0: /* child process */
149 if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1)
150 error(_("%s dup2: %s"), dumper->name, strerror(errno));
151 config_options = get_config_options(2);
152 config_options[0] = dumper->name ? dumper->name : "dumper",
153 config_options[1] = config_name;
155 execve(dumper_program, config_options, safe_env());
156 error(_("exec %s (%s): %s"), dumper_program,
157 dumper->name, strerror(errno));
160 default: /* parent process */
163 dumper->ev_read = NULL;
164 dumper->busy = dumper->down = 0;
166 g_fprintf(stderr,_("driver: started %s pid %u\n"),
167 dumper->name, (unsigned)dumper->pid);
173 startup_dump_processes(
174 char *dumper_program,
180 char number[NUM_STR_SIZE];
182 for(dumper = dmptable, i = 0; i < inparallel; dumper++, i++) {
183 g_snprintf(number, SIZEOF(number), "%d", i);
184 dumper->name = stralloc2("dumper", number);
185 dumper->chunker = &chktable[i];
186 chktable[i].name = stralloc2("chunker", number);
187 chktable[i].dumper = dumper;
190 startup_dump_process(dumper, dumper_program);
191 dumper_cmd(dumper, START, (void *)timestamp);
196 startup_chunk_process(
198 char *chunker_program)
201 char **config_options;
203 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
204 error(_("%s pipe: %s"), chunker->name, strerror(errno));
208 switch(chunker->pid = fork()) {
210 error(_("fork %s: %s"), chunker->name, strerror(errno));
213 case 0: /* child process */
215 if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) {
216 error(_("%s dup2: %s"), chunker->name, strerror(errno));
219 config_options = get_config_options(2);
220 config_options[0] = chunker->name ? chunker->name : "chunker",
221 config_options[1] = config_name;
223 execve(chunker_program, config_options, safe_env());
224 error(_("exec %s (%s): %s"), chunker_program,
225 chunker->name, strerror(errno));
228 default: /* parent process */
232 chunker->ev_read = NULL;
233 g_fprintf(stderr,_("driver: started %s pid %u\n"),
234 chunker->name, (unsigned)chunker->pid);
251 if((line = areads(fd)) == NULL) {
253 error(_("reading result from %s: %s"), childstr(fd), strerror(errno));
256 *result_argc = 0; /* EOF */
258 *result_argc = split(line, result_argv, max_arg, " ");
262 g_printf(_("driver: result time %s from %s:"),
263 walltime_str(curclock()),
266 for(arg = 1; arg <= *result_argc; arg++) {
267 g_printf(" %s", result_argv[arg]);
271 g_printf(" (eof)\n");
278 g_printf("argc = %d\n", *result_argc);
279 for(arg = 0; arg < *result_argc; arg++)
280 g_printf("argv[%d] = \"%s\"\n", arg, result_argv[arg]);
283 if(*result_argc < 1) return BOGUS;
285 for(t = (cmd_t)(BOGUS+1); t < LAST_TOK; t++)
286 if(strcmp(result_argv[1], cmdstr[t]) == 0) return t;
300 char *cmdline = NULL;
301 char number[NUM_STR_SIZE];
302 char splitsize[NUM_STR_SIZE];
303 char fallback_splitsize[NUM_STR_SIZE];
304 char *diskbuffer = NULL;
311 cmdline = vstralloc(cmdstr[cmd], " ", (char *)ptr, "\n", NULL);
315 qname = quote_string(dp->name);
316 qdest = quote_string(destname);
317 g_snprintf(number, SIZEOF(number), "%d", level);
318 g_snprintf(splitsize, SIZEOF(splitsize), "%lld",
319 (long long)dp->tape_splitsize * 1024);
320 cmdline = vstralloc(cmdstr[cmd],
321 " ", disk2serial(dp),
323 " ", dp->host->hostname,
334 qname = quote_string(dp->name);
335 g_snprintf(number, SIZEOF(number), "%d", level);
338 If we haven't been given a place to buffer split dumps to disk,
339 make the argument something besides and empty string so's taper
342 if(!dp->split_diskbuffer || dp->split_diskbuffer[0] == '\0'){
345 diskbuffer = dp->split_diskbuffer;
347 g_snprintf(splitsize, SIZEOF(splitsize), "%lld",
348 (long long)dp->tape_splitsize * 1024);
349 g_snprintf(fallback_splitsize, SIZEOF(fallback_splitsize), "%lld",
350 (long long)dp->fallback_splitsize * 1024);
351 cmdline = vstralloc(cmdstr[cmd],
352 " ", disk2serial(dp),
353 " ", dp->host->hostname,
359 " ", fallback_splitsize,
363 case DONE: /* handle */
364 case FAILED: /* handle */
366 cmdline = vstralloc(cmdstr[cmd],
367 " ", disk2serial(dp),
373 cmdline = stralloc2(cmdstr[cmd], "\n");
376 error(_("Don't know how to send %s command to taper"), cmdstr[cmd]);
381 * Note: cmdline already has a '\n'.
383 g_printf(_("driver: send-cmd time %s to taper: %s"),
384 walltime_str(curclock()), cmdline);
386 if ((fullwrite(taper, cmdline, strlen(cmdline))) < 0) {
387 g_printf(_("writing taper command '%s' failed: %s\n"),
388 cmdline, strerror(errno));
393 if(cmd == QUIT) aclose(taper);
404 char *cmdline = NULL;
405 char number[NUM_STR_SIZE];
406 char numberport[NUM_STR_SIZE];
415 cmdline = vstralloc(cmdstr[cmd], " ", (char *)dp, "\n", NULL);
418 if(dp && dp->device) {
426 device = quote_string((dp->device) ? dp->device : "NODEVICE");
427 qname = quote_string(dp->name);
428 g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
429 g_snprintf(numberport, SIZEOF(numberport), "%d", dumper->output_port);
430 features = am_feature_to_string(dp->host->features);
431 o = optionstr(dp, dp->host->features, NULL);
433 error(_("problem with option string, check the dumptype definition.\n"));
436 cmdline = vstralloc(cmdstr[cmd],
437 " ", disk2serial(dp),
439 " ", dp->host->hostname,
444 " ", sched(dp)->dumpdate,
446 " ", dp->amandad_path,
447 " ", dp->client_username,
456 error(_("PORT-DUMP without disk pointer\n"));
463 qdest = quote_string(sched(dp)->destname);
464 cmdline = vstralloc(cmdstr[cmd],
469 cmdline = stralloc2(cmdstr[cmd], "\n");
473 error(_("Don't know how to send %s command to dumper"), cmdstr[cmd]);
478 * Note: cmdline already has a '\n'.
481 g_printf(_("driver: send-cmd time %s ignored to down dumper %s: %s"),
482 walltime_str(curclock()), dumper->name, cmdline);
484 g_printf(_("driver: send-cmd time %s to %s: %s"),
485 walltime_str(curclock()), dumper->name, cmdline);
487 if (fullwrite(dumper->fd, cmdline, strlen(cmdline)) < 0) {
488 g_printf(_("writing %s command: %s\n"), dumper->name, strerror(errno));
493 if (cmd == QUIT) aclose(dumper->fd);
505 char *cmdline = NULL;
506 char number[NUM_STR_SIZE];
507 char chunksize[NUM_STR_SIZE];
508 char use[NUM_STR_SIZE];
511 assignedhd_t **h=NULL;
518 cmdline = vstralloc(cmdstr[cmd], " ", (char *)dp, "\n", NULL);
521 if(dp && sched(dp) && sched(dp)->holdp) {
522 h = sched(dp)->holdp;
523 activehd = sched(dp)->activehd;
527 qname = quote_string(dp->name);
528 qdest = quote_string(sched(dp)->destname);
529 h[activehd]->disk->allocated_dumpers++;
530 g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
531 g_snprintf(chunksize, SIZEOF(chunksize), "%lld",
532 (long long)holdingdisk_get_chunksize(h[0]->disk->hdisk));
533 g_snprintf(use, SIZEOF(use), "%lld",
534 (long long)h[0]->reserved);
535 features = am_feature_to_string(dp->host->features);
536 o = optionstr(dp, dp->host->features, NULL);
538 error(_("problem with option string, check the dumptype definition.\n"));
540 cmdline = vstralloc(cmdstr[cmd],
541 " ", disk2serial(dp),
543 " ", dp->host->hostname,
547 " ", sched(dp)->dumpdate,
558 error(_("%s command without disk and holding disk.\n"),
564 if(dp && sched(dp) && sched(dp)->holdp) {
565 h = sched(dp)->holdp;
566 activehd = sched(dp)->activehd;
570 qname = quote_string(dp->name);
571 qdest = quote_string(h[activehd]->destname);
572 h[activehd]->disk->allocated_dumpers++;
573 g_snprintf(chunksize, SIZEOF(chunksize), "%lld",
574 (long long)holdingdisk_get_chunksize(h[activehd]->disk->hdisk));
575 g_snprintf(use, SIZEOF(use), "%lld",
576 (long long)(h[activehd]->reserved - h[activehd]->used));
577 cmdline = vstralloc(cmdstr[cmd],
578 " ", disk2serial(dp),
586 cmdline = stralloc2(cmdstr[cmd], "\n");
591 cmdline = stralloc2(cmdstr[cmd], "\n");
596 cmdline = vstralloc(cmdstr[cmd],
597 " ", disk2serial(dp),
600 cmdline = vstralloc(cmdstr[cmd], "\n");
604 error(_("Don't know how to send %s command to chunker"), cmdstr[cmd]);
609 * Note: cmdline already has a '\n'.
611 g_printf(_("driver: send-cmd time %s to %s: %s"),
612 walltime_str(curclock()), chunker->name, cmdline);
614 if (fullwrite(chunker->fd, cmdline, strlen(cmdline)) < 0) {
615 g_printf(_("writing %s command: %s\n"), chunker->name, strerror(errno));
620 if (cmd == QUIT) aclose(chunker->fd);
625 #define MAX_SERIAL MAX_DUMPERS+1 /* one for the taper */
632 } stable[MAX_SERIAL];
641 rc = sscanf(str, "%d-%ld", &s, &gen);
643 error(_("error [serial2disk \"%s\" parse error]"), str);
645 } else if (s < 0 || s >= MAX_SERIAL) {
646 error(_("error [serial out of range 0..%d: %d]"), MAX_SERIAL, s);
649 if(gen != stable[s].gen)
650 g_printf(_("driver: serial2disk error time %s serial gen mismatch %s\n"),
651 walltime_str(curclock()), str);
662 rc = sscanf(str, _("%d-%ld"), &s, &gen);
663 if(!(rc == 2 && s >= 0 && s < MAX_SERIAL)) {
664 /* nuke self to get core dump for Brett */
665 g_fprintf(stderr, _("driver: free_serial: str \"%s\" rc %d s %d\n"),
671 if(gen != stable[s].gen)
672 g_printf(_("driver: free_serial error time %s serial gen mismatch %s\n"),
673 walltime_str(curclock()),str);
685 for(s = 0; s < MAX_SERIAL; s++) {
686 if(stable[s].dp == dp) {
693 g_printf(_("driver: error time %s serial not found\n"),
694 walltime_str(curclock()));
699 check_unfree_serial(void)
703 /* find used serial number */
704 for(s = 0; s < MAX_SERIAL; s++) {
705 if(stable[s].gen != 0 || stable[s].dp != NULL) {
706 g_printf(_("driver: error time %s bug: serial in use: %02d-%05ld\n"),
707 walltime_str(curclock()), s, stable[s].gen);
716 static char str[NUM_STR_SIZE];
718 for(s = 0; s < MAX_SERIAL; s++) {
719 if(stable[s].dp == dp) {
720 g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
725 /* find unused serial number */
726 for(s = 0; s < MAX_SERIAL; s++)
727 if(stable[s].gen == 0 && stable[s].dp == NULL)
729 if(s >= MAX_SERIAL) {
730 g_printf(_("driver: error time %s bug: out of serial numbers\n"),
731 walltime_str(curclock()));
735 stable[s].gen = generation++;
738 g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
755 level = sched(dp)->level;
757 conf_infofile = config_dir_relative(getconf_str(CNF_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);