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"
40 #include "timestamp.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] = get_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] = get_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, NULL, (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] = get_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);
249 if((line = areads(fd)) == NULL) {
251 error(_("reading result from %s: %s"), childstr(fd), strerror(errno));
255 *result_argc = 0; /* EOF */
257 *result_argv = split_quoted_strings(line);
258 *result_argc = g_strv_length(*result_argv);
262 g_printf(_("driver: result time %s from %s:"),
263 walltime_str(curclock()),
266 g_printf(" %s", line);
269 g_printf(" (eof)\n");
275 if(*result_argc < 1) return BOGUS;
277 for(t = (cmd_t)(BOGUS+1); t < LAST_TOK; t++)
278 if(strcmp((*result_argv)[0], cmdstr[t]) == 0) return t;
292 char *cmdline = NULL;
293 char number[NUM_STR_SIZE];
294 char splitsize[NUM_STR_SIZE];
295 char fallback_splitsize[NUM_STR_SIZE];
296 char orig_kb[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 g_snprintf(number, SIZEOF(number), "%d", level);
312 g_snprintf(splitsize, SIZEOF(splitsize), "%lld",
313 (long long)dp->tape_splitsize * 1024);
314 g_snprintf(orig_kb, SIZEOF(orig_kb), "%jd",
315 (intmax_t)sched(dp)->origsize);
316 cmdline = vstralloc(cmdstr[cmd],
317 " ", disk2serial(dp),
319 " ", dp->host->hostname,
331 qname = quote_string(dp->name);
332 g_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 g_snprintf(splitsize, SIZEOF(splitsize), "%lld",
345 (long long)dp->tape_splitsize * 1024);
346 g_snprintf(fallback_splitsize, SIZEOF(fallback_splitsize), "%lld",
347 (long long)dp->fallback_splitsize * 1024);
348 cmdline = vstralloc(cmdstr[cmd],
349 " ", disk2serial(dp),
350 " ", dp->host->hostname,
356 " ", fallback_splitsize,
360 case DONE: /* handle */
362 g_snprintf(number, SIZEOF(number), "%jd",
363 (intmax_t)(sched(dp)->origsize));
364 cmdline = vstralloc(cmdstr[cmd],
365 " ", disk2serial(dp),
369 case FAILED: /* handle */
371 cmdline = vstralloc(cmdstr[cmd],
372 " ", disk2serial(dp),
376 q = quote_string((char *)ptr);
377 cmdline = vstralloc(cmdstr[cmd],
384 cmdline = stralloc2(cmdstr[cmd], "\n");
387 error(_("Don't know how to send %s command to taper"), cmdstr[cmd]);
392 * Note: cmdline already has a '\n'.
394 g_printf(_("driver: send-cmd time %s to taper: %s"),
395 walltime_str(curclock()), cmdline);
397 if ((full_write(taper, cmdline, strlen(cmdline))) < strlen(cmdline)) {
398 g_printf(_("writing taper command '%s' failed: %s\n"),
399 cmdline, strerror(errno));
404 if(cmd == QUIT) aclose(taper);
416 char *cmdline = NULL;
417 char number[NUM_STR_SIZE];
418 char numberport[NUM_STR_SIZE];
427 cmdline = vstralloc(cmdstr[cmd], " ", mesg, "\n", NULL);
430 if(dp && dp->device) {
438 application_t *application = NULL;
442 char *qclient_username;
446 if (dp->application != NULL) {
447 application = lookup_application(dp->application);
448 g_assert(application != NULL);
451 device = quote_string((dp->device) ? dp->device : "NODEVICE");
452 qname = quote_string(dp->name);
453 g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
454 g_snprintf(numberport, SIZEOF(numberport), "%d", dumper->output_port);
455 features = am_feature_to_string(dp->host->features);
456 if (am_has_feature(dp->host->features, fe_req_xml)) {
457 o = xml_optionstr(dp, 1);
460 xml_app = xml_application(dp, application,
462 vstrextend(&o, xml_app, NULL);
470 g_assert(dp->program);
471 if (0 == strcmp(dp->program, "APPLICATION")) {
472 g_assert(application != NULL);
473 plugin = application_get_plugin(application);
475 plugin = dp->program;
477 qplugin = quote_string(plugin);
478 qamandad_path = quote_string(dp->amandad_path);
479 qclient_username = quote_string(dp->client_username);
480 qclient_port = quote_string(dp->client_port);
481 qssh_keys = quote_string(dp->ssh_keys);
482 dbprintf("security_driver %s\n", dp->auth);
484 cmdline = vstralloc(cmdstr[cmd],
485 " ", disk2serial(dp),
487 " ", dp->host->hostname,
492 " ", sched(dp)->dumpdate,
495 " ", qclient_username,
499 " ", data_path_to_string(dp->data_path),
500 " ", dp->dataport_list,
504 amfree(qamandad_path);
505 amfree(qclient_username);
506 amfree(qclient_port);
513 error(_("PORT-DUMP without disk pointer\n"));
519 qmesg = quote_string(mesg);
520 cmdline = vstralloc(cmdstr[cmd], " ", qmesg, "\n", NULL );
524 error(_("Don't know how to send %s command to dumper"), cmdstr[cmd]);
529 * Note: cmdline already has a '\n'.
532 g_printf(_("driver: send-cmd time %s ignored to down dumper %s: %s"),
533 walltime_str(curclock()), dumper->name, cmdline);
535 g_printf(_("driver: send-cmd time %s to %s: %s"),
536 walltime_str(curclock()), dumper->name, cmdline);
538 if (full_write(dumper->fd, cmdline, strlen(cmdline)) < strlen(cmdline)) {
539 g_printf(_("writing %s command: %s\n"), dumper->name, strerror(errno));
544 if (cmd == QUIT) aclose(dumper->fd);
557 char *cmdline = NULL;
558 char number[NUM_STR_SIZE];
559 char chunksize[NUM_STR_SIZE];
560 char use[NUM_STR_SIZE];
563 assignedhd_t **h=NULL;
570 cmdline = vstralloc(cmdstr[cmd], " ", mesg, "\n", NULL);
573 if(dp && sched(dp) && sched(dp)->holdp) {
574 h = sched(dp)->holdp;
575 activehd = sched(dp)->activehd;
579 qname = quote_string(dp->name);
580 qdest = quote_string(sched(dp)->destname);
581 h[activehd]->disk->allocated_dumpers++;
582 g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
583 g_snprintf(chunksize, SIZEOF(chunksize), "%lld",
584 (long long)holdingdisk_get_chunksize(h[0]->disk->hdisk));
585 g_snprintf(use, SIZEOF(use), "%lld",
586 (long long)h[0]->reserved);
587 features = am_feature_to_string(dp->host->features);
589 cmdline = vstralloc(cmdstr[cmd],
590 " ", disk2serial(dp),
592 " ", dp->host->hostname,
596 " ", sched(dp)->dumpdate,
607 error(_("%s command without disk and holding disk.\n"),
613 if(dp && sched(dp) && sched(dp)->holdp) {
614 h = sched(dp)->holdp;
615 activehd = sched(dp)->activehd;
619 qname = quote_string(dp->name);
620 qdest = quote_string(h[activehd]->destname);
621 h[activehd]->disk->allocated_dumpers++;
622 g_snprintf(chunksize, SIZEOF(chunksize), "%lld",
623 (long long)holdingdisk_get_chunksize(h[activehd]->disk->hdisk));
624 g_snprintf(use, SIZEOF(use), "%lld",
625 (long long)(h[activehd]->reserved - h[activehd]->used));
626 cmdline = vstralloc(cmdstr[cmd],
627 " ", disk2serial(dp),
635 cmdline = stralloc2(cmdstr[cmd], "\n");
641 char *q = quote_string(mesg);
642 cmdline = vstralloc(cmdstr[cmd], " ", q, "\n", NULL);
649 cmdline = vstralloc(cmdstr[cmd],
650 " ", disk2serial(dp),
653 cmdline = vstralloc(cmdstr[cmd], "\n");
657 error(_("Don't know how to send %s command to chunker"), cmdstr[cmd]);
662 * Note: cmdline already has a '\n'.
664 g_printf(_("driver: send-cmd time %s to %s: %s"),
665 walltime_str(curclock()), chunker->name, cmdline);
667 if (full_write(chunker->fd, cmdline, strlen(cmdline)) < strlen(cmdline)) {
668 g_printf(_("writing %s command: %s\n"), chunker->name, strerror(errno));
673 if (cmd == QUIT) aclose(chunker->fd);
678 #define MAX_SERIAL MAX_DUMPERS+1 /* one for the taper */
685 } stable[MAX_SERIAL];
694 rc = sscanf(str, "%d-%ld", &s, &gen);
696 error(_("error [serial2disk \"%s\" parse error]"), str);
698 } else if (s < 0 || s >= MAX_SERIAL) {
699 error(_("error [serial out of range 0..%d: %d]"), MAX_SERIAL, s);
702 if(gen != stable[s].gen)
703 g_printf(_("driver: serial2disk error time %s serial gen mismatch %s\n"),
704 walltime_str(curclock()), str);
715 rc = sscanf(str, _("%d-%ld"), &s, &gen);
716 if(!(rc == 2 && s >= 0 && s < MAX_SERIAL)) {
717 /* nuke self to get core dump for Brett */
718 g_fprintf(stderr, _("driver: free_serial: str \"%s\" rc %d s %d\n"),
724 if(gen != stable[s].gen)
725 g_printf(_("driver: free_serial error time %s serial gen mismatch %s\n"),
726 walltime_str(curclock()),str);
738 for(s = 0; s < MAX_SERIAL; s++) {
739 if(stable[s].dp == dp) {
746 g_printf(_("driver: error time %s serial not found\n"),
747 walltime_str(curclock()));
752 check_unfree_serial(void)
756 /* find used serial number */
757 for(s = 0; s < MAX_SERIAL; s++) {
758 if(stable[s].gen != 0 || stable[s].dp != NULL) {
759 g_printf(_("driver: error time %s bug: serial in use: %02d-%05ld\n"),
760 walltime_str(curclock()), s, stable[s].gen);
769 static char str[NUM_STR_SIZE];
771 for(s = 0; s < MAX_SERIAL; s++) {
772 if(stable[s].dp == dp) {
773 g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
778 /* find unused serial number */
779 for(s = 0; s < MAX_SERIAL; s++)
780 if(stable[s].gen == 0 && stable[s].dp == NULL)
782 if(s >= MAX_SERIAL) {
783 g_printf(_("driver: error time %s bug: out of serial numbers\n"),
784 walltime_str(curclock()));
788 stable[s].gen = generation++;
791 g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
808 level = sched(dp)->level;
810 conf_infofile = config_dir_relative(getconf_str(CNF_INFOFILE));
811 if (open_infofile(conf_infofile)) {
812 error(_("could not open info db \"%s\""), conf_infofile);
815 amfree(conf_infofile);
817 get_info(dp->host->hostname, dp->name, &info);
819 /* Clean up information about this and higher-level dumps. This
820 assumes that update_info_dumper() is always run before
821 update_info_taper(). */
822 for (i = level; i < DUMP_LEVELS; ++i) {
824 infp->size = (off_t)-1;
825 infp->csize = (off_t)-1;
826 infp->secs = (time_t)-1;
827 infp->date = (time_t)-1;
828 infp->label[0] = '\0';
832 /* now store information about this dump */
833 infp = &info.inf[level];
834 infp->size = origsize;
835 infp->csize = dumpsize;
836 infp->secs = dumptime;
837 infp->date = get_time_from_timestamp(sched(dp)->datestamp);
839 if(level == 0) perfp = &info.full;
840 else perfp = &info.incr;
842 /* Update the stats, but only if the new values are meaningful */
843 if(dp->compress != COMP_NONE && origsize > (off_t)0) {
844 newperf(perfp->comp, (double)dumpsize/(double)origsize);
846 if(dumptime > (time_t)0) {
847 if((off_t)dumptime >= dumpsize)
848 newperf(perfp->rate, 1);
850 newperf(perfp->rate, (double)dumpsize/(double)dumptime);
853 if(origsize >= (off_t)0 && getconf_int(CNF_RESERVE)<100) {
854 info.command = NO_COMMAND;
857 if (origsize >= (off_t)0 && level == info.last_level) {
858 info.consecutive_runs++;
859 } else if (origsize >= (off_t)0 || level < info.last_level) {
860 info.last_level = level;
861 info.consecutive_runs = 1;
864 if(origsize >= (off_t)0 && dumpsize >= (off_t)0) {
865 for(i=NB_HISTORY-1;i>0;i--) {
866 info.history[i] = info.history[i-1];
869 info.history[0].level = level;
870 info.history[0].size = origsize;
871 info.history[0].csize = dumpsize;
872 info.history[0].date = get_time_from_timestamp(sched(dp)->datestamp);
873 info.history[0].secs = dumptime;
876 if (put_info(dp->host->hostname, dp->name, &info)) {
877 int save_errno = errno;
878 g_fprintf(stderr, _("infofile update failed (%s,'%s'): %s\n"),
879 dp->host->hostname, dp->name, strerror(save_errno));
880 log_add(L_ERROR, _("infofile update failed (%s,'%s'): %s\n"),
881 dp->host->hostname, dp->name, strerror(save_errno));
882 error(_("infofile update failed (%s,'%s'): %s\n"),
883 dp->host->hostname, dp->name, strerror(save_errno));
901 rc = open_infofile(getconf_str(CNF_INFOFILE));
903 error(_("could not open infofile %s: %s (%d)"), getconf_str(CNF_INFOFILE),
904 strerror(errno), rc);
908 get_info(dp->host->hostname, dp->name, &info);
910 infp = &info.inf[level];
911 /* XXX - should we record these two if no-record? */
912 strncpy(infp->label, label, SIZEOF(infp->label)-1);
913 infp->label[SIZEOF(infp->label)-1] = '\0';
914 infp->filenum = filenum;
916 info.command = NO_COMMAND;
918 if (put_info(dp->host->hostname, dp->name, &info)) {
919 int save_errno = errno;
920 g_fprintf(stderr, _("infofile update failed (%s,'%s'): %s\n"),
921 dp->host->hostname, dp->name, strerror(save_errno));
922 log_add(L_ERROR, _("infofile update failed (%s,'%s'): %s\n"),
923 dp->host->hostname, dp->name, strerror(save_errno));
924 error(_("infofile update failed (%s,'%s'): %s\n"),
925 dp->host->hostname, dp->name, strerror(save_errno));
931 /* Free an array of pointers to assignedhd_t after freeing the
932 * assignedhd_t themselves. The array must be NULL-terminated.
934 void free_assignedhd(
939 if( !ahd ) { return; }
941 for( i = 0; ahd[i]; i++ ) {
942 amfree(ahd[i]->destname);