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"
41 #define GLOBAL /* the global variables defined here */
46 static const char *childstr(int);
55 for(dumper = dmptable; dumper < dmptable + MAX_DUMPERS; dumper++) {
65 static char buf[NUM_STR_SIZE + 32];
71 for (dumper = dmptable; dumper < dmptable + MAX_DUMPERS; dumper++) {
73 return (dumper->name);
74 if (dumper->chunker && dumper->chunker->fd == fd)
75 return (dumper->chunker->name);
77 g_snprintf(buf, SIZEOF(buf), _("unknown child (fd %d)"), fd);
87 char **config_options;
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"),
95 fd[0], (int)FD_SETSIZE-1);
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], (int)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 config_options = get_config_options(2);
114 config_options[0] = "taper";
115 config_options[1] = get_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] = get_config_name();
154 execve(dumper_program, config_options, safe_env());
155 error(_("exec %s (%s): %s"), dumper_program,
156 dumper->name, strerror(errno));
159 default: /* parent process */
162 dumper->ev_read = NULL;
163 dumper->busy = dumper->down = 0;
165 g_fprintf(stderr,_("driver: started %s pid %u\n"),
166 dumper->name, (unsigned)dumper->pid);
172 startup_dump_processes(
173 char *dumper_program,
179 char number[NUM_STR_SIZE];
181 for(dumper = dmptable, i = 0; i < inparallel; dumper++, i++) {
182 g_snprintf(number, SIZEOF(number), "%d", i);
183 dumper->name = stralloc2("dumper", number);
184 dumper->chunker = &chktable[i];
185 chktable[i].name = stralloc2("chunker", number);
186 chktable[i].dumper = dumper;
189 startup_dump_process(dumper, dumper_program);
190 dumper_cmd(dumper, START, NULL, (void *)timestamp);
195 startup_chunk_process(
197 char *chunker_program)
200 char **config_options;
202 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) {
203 error(_("%s pipe: %s"), chunker->name, strerror(errno));
207 switch(chunker->pid = fork()) {
209 error(_("fork %s: %s"), chunker->name, strerror(errno));
212 case 0: /* child process */
214 if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) {
215 error(_("%s dup2: %s"), chunker->name, strerror(errno));
218 config_options = get_config_options(2);
219 config_options[0] = chunker->name ? chunker->name : "chunker",
220 config_options[1] = get_config_name();
222 execve(chunker_program, config_options, safe_env());
223 error(_("exec %s (%s): %s"), chunker_program,
224 chunker->name, strerror(errno));
227 default: /* parent process */
231 chunker->ev_read = NULL;
232 g_fprintf(stderr,_("driver: started %s pid %u\n"),
233 chunker->name, (unsigned)chunker->pid);
248 if((line = areads(fd)) == NULL) {
250 error(_("reading result from %s: %s"), childstr(fd), strerror(errno));
254 *result_argc = 0; /* EOF */
256 *result_argv = split_quoted_strings(line);
257 *result_argc = g_strv_length(*result_argv);
261 g_printf(_("driver: result time %s from %s:"),
262 walltime_str(curclock()),
265 g_printf(" %s", line);
268 g_printf(" (eof)\n");
274 if(*result_argc < 1) return BOGUS;
276 for(t = (cmd_t)(BOGUS+1); t < LAST_TOK; t++)
277 if(strcmp((*result_argv)[0], cmdstr[t]) == 0) return t;
291 char *cmdline = NULL;
292 char number[NUM_STR_SIZE];
293 char splitsize[NUM_STR_SIZE];
294 char fallback_splitsize[NUM_STR_SIZE];
295 char *diskbuffer = NULL;
303 cmdline = vstralloc(cmdstr[cmd], " ", (char *)ptr, "\n", NULL);
307 qname = quote_string(dp->name);
308 qdest = quote_string(destname);
309 g_snprintf(number, SIZEOF(number), "%d", level);
310 g_snprintf(splitsize, SIZEOF(splitsize), "%lld",
311 (long long)dp->tape_splitsize * 1024);
312 cmdline = vstralloc(cmdstr[cmd],
313 " ", disk2serial(dp),
315 " ", dp->host->hostname,
326 qname = quote_string(dp->name);
327 g_snprintf(number, SIZEOF(number), "%d", level);
330 If we haven't been given a place to buffer split dumps to disk,
331 make the argument something besides and empty string so's taper
334 if(!dp->split_diskbuffer || dp->split_diskbuffer[0] == '\0'){
337 diskbuffer = dp->split_diskbuffer;
339 g_snprintf(splitsize, SIZEOF(splitsize), "%lld",
340 (long long)dp->tape_splitsize * 1024);
341 g_snprintf(fallback_splitsize, SIZEOF(fallback_splitsize), "%lld",
342 (long long)dp->fallback_splitsize * 1024);
343 cmdline = vstralloc(cmdstr[cmd],
344 " ", disk2serial(dp),
345 " ", dp->host->hostname,
351 " ", fallback_splitsize,
355 case DONE: /* handle */
356 case FAILED: /* handle */
358 cmdline = vstralloc(cmdstr[cmd],
359 " ", disk2serial(dp),
363 q = quote_string((char *)ptr);
364 cmdline = vstralloc(cmdstr[cmd],
371 cmdline = stralloc2(cmdstr[cmd], "\n");
374 error(_("Don't know how to send %s command to taper"), cmdstr[cmd]);
379 * Note: cmdline already has a '\n'.
381 g_printf(_("driver: send-cmd time %s to taper: %s"),
382 walltime_str(curclock()), cmdline);
384 if ((full_write(taper, cmdline, strlen(cmdline))) < strlen(cmdline)) {
385 g_printf(_("writing taper command '%s' failed: %s\n"),
386 cmdline, strerror(errno));
391 if(cmd == QUIT) aclose(taper);
403 char *cmdline = NULL;
404 char number[NUM_STR_SIZE];
405 char numberport[NUM_STR_SIZE];
414 cmdline = vstralloc(cmdstr[cmd], " ", mesg, "\n", NULL);
417 if(dp && dp->device) {
425 device = quote_string((dp->device) ? dp->device : "NODEVICE");
426 qname = quote_string(dp->name);
427 g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
428 g_snprintf(numberport, SIZEOF(numberport), "%d", dumper->output_port);
429 features = am_feature_to_string(dp->host->features);
430 if (am_has_feature(dp->host->features, fe_req_xml)) {
431 o = xml_optionstr(dp, dp->host->features, NULL, 1);
432 if (dp->application) {
433 char *app = xml_application(dp->application,
435 vstrextend(&o, app, NULL);
440 o = optionstr(dp, dp->host->features, NULL);
443 error(_("problem with option string, check the dumptype definition.\n"));
446 dbprintf("security_driver %s\n", dp->security_driver);
447 cmdline = vstralloc(cmdstr[cmd],
448 " ", disk2serial(dp),
450 " ", dp->host->hostname,
455 " ", sched(dp)->dumpdate,
456 " ", dp->program && strcmp(dp->program,"APPLICATION")!=0 ? dp->program: application_get_plugin(dp->application),
457 " ", dp->amandad_path,
458 " ", dp->client_username,
460 " ", dp->security_driver,
468 error(_("PORT-DUMP without disk pointer\n"));
474 qmesg = quote_string(mesg);
475 cmdline = vstralloc(cmdstr[cmd], " ", qmesg, "\n", NULL );
479 error(_("Don't know how to send %s command to dumper"), cmdstr[cmd]);
484 * Note: cmdline already has a '\n'.
487 g_printf(_("driver: send-cmd time %s ignored to down dumper %s: %s"),
488 walltime_str(curclock()), dumper->name, cmdline);
490 g_printf(_("driver: send-cmd time %s to %s: %s"),
491 walltime_str(curclock()), dumper->name, cmdline);
493 if (full_write(dumper->fd, cmdline, strlen(cmdline)) < strlen(cmdline)) {
494 g_printf(_("writing %s command: %s\n"), dumper->name, strerror(errno));
499 if (cmd == QUIT) aclose(dumper->fd);
512 char *cmdline = NULL;
513 char number[NUM_STR_SIZE];
514 char chunksize[NUM_STR_SIZE];
515 char use[NUM_STR_SIZE];
518 assignedhd_t **h=NULL;
525 cmdline = vstralloc(cmdstr[cmd], " ", mesg, "\n", NULL);
528 if(dp && sched(dp) && sched(dp)->holdp) {
529 h = sched(dp)->holdp;
530 activehd = sched(dp)->activehd;
534 qname = quote_string(dp->name);
535 qdest = quote_string(sched(dp)->destname);
536 h[activehd]->disk->allocated_dumpers++;
537 g_snprintf(number, SIZEOF(number), "%d", sched(dp)->level);
538 g_snprintf(chunksize, SIZEOF(chunksize), "%lld",
539 (long long)holdingdisk_get_chunksize(h[0]->disk->hdisk));
540 g_snprintf(use, SIZEOF(use), "%lld",
541 (long long)h[0]->reserved);
542 features = am_feature_to_string(dp->host->features);
543 o = optionstr(dp, dp->host->features, NULL);
545 error(_("problem with option string, check the dumptype definition.\n"));
547 cmdline = vstralloc(cmdstr[cmd],
548 " ", disk2serial(dp),
550 " ", dp->host->hostname,
554 " ", sched(dp)->dumpdate,
565 error(_("%s command without disk and holding disk.\n"),
571 if(dp && sched(dp) && sched(dp)->holdp) {
572 h = sched(dp)->holdp;
573 activehd = sched(dp)->activehd;
577 qname = quote_string(dp->name);
578 qdest = quote_string(h[activehd]->destname);
579 h[activehd]->disk->allocated_dumpers++;
580 g_snprintf(chunksize, SIZEOF(chunksize), "%lld",
581 (long long)holdingdisk_get_chunksize(h[activehd]->disk->hdisk));
582 g_snprintf(use, SIZEOF(use), "%lld",
583 (long long)(h[activehd]->reserved - h[activehd]->used));
584 cmdline = vstralloc(cmdstr[cmd],
585 " ", disk2serial(dp),
593 cmdline = stralloc2(cmdstr[cmd], "\n");
599 char *q = quote_string(mesg);
600 cmdline = vstralloc(cmdstr[cmd], " ", q, "\n", NULL);
607 cmdline = vstralloc(cmdstr[cmd],
608 " ", disk2serial(dp),
611 cmdline = vstralloc(cmdstr[cmd], "\n");
615 error(_("Don't know how to send %s command to chunker"), cmdstr[cmd]);
620 * Note: cmdline already has a '\n'.
622 g_printf(_("driver: send-cmd time %s to %s: %s"),
623 walltime_str(curclock()), chunker->name, cmdline);
625 if (full_write(chunker->fd, cmdline, strlen(cmdline)) < strlen(cmdline)) {
626 g_printf(_("writing %s command: %s\n"), chunker->name, strerror(errno));
631 if (cmd == QUIT) aclose(chunker->fd);
636 #define MAX_SERIAL MAX_DUMPERS+1 /* one for the taper */
643 } stable[MAX_SERIAL];
652 rc = sscanf(str, "%d-%ld", &s, &gen);
654 error(_("error [serial2disk \"%s\" parse error]"), str);
656 } else if (s < 0 || s >= MAX_SERIAL) {
657 error(_("error [serial out of range 0..%d: %d]"), MAX_SERIAL, s);
660 if(gen != stable[s].gen)
661 g_printf(_("driver: serial2disk error time %s serial gen mismatch %s\n"),
662 walltime_str(curclock()), str);
673 rc = sscanf(str, _("%d-%ld"), &s, &gen);
674 if(!(rc == 2 && s >= 0 && s < MAX_SERIAL)) {
675 /* nuke self to get core dump for Brett */
676 g_fprintf(stderr, _("driver: free_serial: str \"%s\" rc %d s %d\n"),
682 if(gen != stable[s].gen)
683 g_printf(_("driver: free_serial error time %s serial gen mismatch %s\n"),
684 walltime_str(curclock()),str);
696 for(s = 0; s < MAX_SERIAL; s++) {
697 if(stable[s].dp == dp) {
704 g_printf(_("driver: error time %s serial not found\n"),
705 walltime_str(curclock()));
710 check_unfree_serial(void)
714 /* find used serial number */
715 for(s = 0; s < MAX_SERIAL; s++) {
716 if(stable[s].gen != 0 || stable[s].dp != NULL) {
717 g_printf(_("driver: error time %s bug: serial in use: %02d-%05ld\n"),
718 walltime_str(curclock()), s, stable[s].gen);
727 static char str[NUM_STR_SIZE];
729 for(s = 0; s < MAX_SERIAL; s++) {
730 if(stable[s].dp == dp) {
731 g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
736 /* find unused serial number */
737 for(s = 0; s < MAX_SERIAL; s++)
738 if(stable[s].gen == 0 && stable[s].dp == NULL)
740 if(s >= MAX_SERIAL) {
741 g_printf(_("driver: error time %s bug: out of serial numbers\n"),
742 walltime_str(curclock()));
746 stable[s].gen = generation++;
749 g_snprintf(str, SIZEOF(str), "%02d-%05ld", s, stable[s].gen);
766 level = sched(dp)->level;
768 conf_infofile = config_dir_relative(getconf_str(CNF_INFOFILE));
769 if (open_infofile(conf_infofile)) {
770 error(_("could not open info db \"%s\""), conf_infofile);
773 amfree(conf_infofile);
775 get_info(dp->host->hostname, dp->name, &info);
777 /* Clean up information about this and higher-level dumps. This
778 assumes that update_info_dumper() is always run before
779 update_info_taper(). */
780 for (i = level; i < DUMP_LEVELS; ++i) {
782 infp->size = (off_t)-1;
783 infp->csize = (off_t)-1;
784 infp->secs = (time_t)-1;
785 infp->date = (time_t)-1;
786 infp->label[0] = '\0';
790 /* now store information about this dump */
791 infp = &info.inf[level];
792 infp->size = origsize;
793 infp->csize = dumpsize;
794 infp->secs = dumptime;
795 infp->date = sched(dp)->timestamp;
797 if(level == 0) perfp = &info.full;
798 else perfp = &info.incr;
800 /* Update the stats, but only if the new values are meaningful */
801 if(dp->compress != COMP_NONE && origsize > (off_t)0) {
802 newperf(perfp->comp, (double)dumpsize/(double)origsize);
804 if(dumptime > (time_t)0) {
805 if((off_t)dumptime >= dumpsize)
806 newperf(perfp->rate, 1);
808 newperf(perfp->rate, (double)dumpsize/(double)dumptime);
811 if(origsize >= (off_t)0 && getconf_int(CNF_RESERVE)<100) {
812 info.command = NO_COMMAND;
815 if (origsize >= (off_t)0 && level == info.last_level) {
816 info.consecutive_runs++;
817 } else if (origsize >= (off_t)0 || level < info.last_level) {
818 info.last_level = level;
819 info.consecutive_runs = 1;
822 if(origsize >= (off_t)0 && dumpsize >= (off_t)0) {
823 for(i=NB_HISTORY-1;i>0;i--) {
824 info.history[i] = info.history[i-1];
827 info.history[0].level = level;
828 info.history[0].size = origsize;
829 info.history[0].csize = dumpsize;
830 info.history[0].date = sched(dp)->timestamp;
831 info.history[0].secs = dumptime;
834 if(put_info(dp->host->hostname, dp->name, &info)) {
835 error(_("infofile update failed (%s,'%s')\n"), dp->host->hostname, dp->name);
853 rc = open_infofile(getconf_str(CNF_INFOFILE));
855 error(_("could not open infofile %s: %s (%d)"), getconf_str(CNF_INFOFILE),
856 strerror(errno), rc);
860 get_info(dp->host->hostname, dp->name, &info);
862 infp = &info.inf[level];
863 /* XXX - should we record these two if no-record? */
864 strncpy(infp->label, label, SIZEOF(infp->label)-1);
865 infp->label[SIZEOF(infp->label)-1] = '\0';
866 infp->filenum = filenum;
868 info.command = NO_COMMAND;
870 if(put_info(dp->host->hostname, dp->name, &info)) {
871 error(_("infofile update failed (%s,'%s')\n"), dp->host->hostname, dp->name);
877 /* Free an array of pointers to assignedhd_t after freeing the
878 * assignedhd_t themselves. The array must be NULL-terminated.
880 void free_assignedhd(
885 if( !ahd ) { return; }
887 for( i = 0; ahd[i]; i++ ) {
888 amfree(ahd[i]->destname);