2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1999 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 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: sendbackup.c,v 1.88 2006/07/25 18:27:56 martinea Exp $
29 * common code for the sendbackup-* programs.
34 #include "sendbackup.h"
36 #include "pipespawn.h"
37 #include "amfeatures.h"
41 #include "amandates.h"
43 #define sendbackup_debug(i, ...) do { \
44 if ((i) <= debug_sendbackup) { \
45 dbprintf(__VA_LIST__); \
51 pid_t comppid = (pid_t)-1;
52 pid_t dumppid = (pid_t)-1;
53 pid_t tarpid = (pid_t)-1;
54 pid_t encpid = (pid_t)-1;
55 pid_t indexpid = (pid_t)-1;
56 pid_t application_api_pid = (pid_t)-1;
57 char *errorstr = NULL;
63 g_option_t *g_options = NULL;
67 backup_program_t *program = NULL;
70 static am_feature_t *our_features = NULL;
71 static char *our_feature_string = NULL;
72 static char *amandad_auth = NULL;
75 int main(int argc, char **argv);
76 char *childstr(pid_t pid);
77 int check_status(pid_t pid, amwait_t w, int mesgfd);
79 pid_t pipefork(void (*func)(void), char *fname, int *stdinfd,
80 int stdoutfd, int stderrfd);
81 int check_result(int mesgfd);
82 void parse_backup_messages(dle_t *dle, int mesgin);
83 static void process_dumpline(char *str);
84 static void save_fd(int *, int);
85 void application_api_info_tapeheader(int mesgfd, char *prog, dle_t *dle);
87 int fdprintf(int fd, char *format, ...) G_GNUC_PRINTF(2, 3);
99 arglist_start(argp, format);
100 s = g_strdup_vprintf(format, argp);
103 r = full_write(fd, s, strlen(s));
117 char *dumpdate, *stroptions;
119 char *qamdevice = NULL;
121 char *err_extra = NULL;
131 * Configure program for internationalization:
132 * 1) Only set the message locale for now.
133 * 2) Set textdomain for all amanda related programs to "amanda"
134 * We don't want to be forced to support dozens of message catalogs.
136 setlocale(LC_MESSAGES, "C");
137 textdomain("amanda");
139 safe_fd(DATA_FD_OFFSET, DATA_FD_COUNT*2);
144 set_pname("sendbackup");
146 /* Don't die when child closes pipe */
147 signal(SIGPIPE, SIG_IGN);
149 /* Don't die when interrupt received */
150 signal(SIGINT, SIG_IGN);
152 if(argc > 1 && strcmp(argv[1],"-t") == 0) {
160 add_amanda_log_handler(amanda_log_stderr);
161 add_amanda_log_handler(amanda_log_syslog);
162 dbopen(DBG_SUBDIR_CLIENT);
164 dbprintf(_("Version %s\n"), VERSION);
166 if(argc > 2 && strcmp(argv[1], "amandad") == 0) {
167 amandad_auth = stralloc(argv[2]);
170 our_features = am_init_feature_set();
171 our_feature_string = am_feature_to_string(our_features);
173 config_init(CONFIG_INIT_CLIENT, NULL);
174 /* (check for config errors comes later) */
176 check_running_as(RUNNING_AS_CLIENT_LOGIN);
180 * In interactive (debug) mode, the backup data is sent to
181 * /dev/null and none of the network connections back to driver
182 * programs on the tape host are set up. The index service is
183 * run and goes to stdout.
185 g_fprintf(stderr, _("%s: running in interactive test mode\n"), get_pname());
193 for(; (line = agets(stdin)) != NULL; free(line)) {
197 g_fprintf(stderr, "%s> ", get_pname());
200 if(strncmp_const(line, "OPTIONS ") == 0) {
201 g_options = parse_g_options(line+8, 1);
202 if(!g_options->hostname) {
203 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
204 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
205 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
208 if (g_options->config) {
209 /* overlay this configuration on the existing (nameless) configuration */
210 config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
213 dbrename(get_config_name(), DBG_SUBDIR_CLIENT);
216 /* check for any config errors now */
217 if (config_errors(&errlist) >= CFGERR_ERRORS) {
218 char *errstr = config_errors_to_error_string(errlist);
219 g_printf("%s\n", errstr);
224 if (am_has_feature(g_options->features, fe_req_xml)) {
230 if (dle && dle->program != NULL) {
231 err_extra = _("multiple requests");
235 dbprintf(_(" sendbackup req: <%s>\n"), line);
241 skip_whitespace(s, ch); /* find the program name */
243 err_extra = _("no program name");
244 goto err; /* no program name */
246 dle->program = s - 1;
247 skip_non_whitespace(s, ch);
250 if (strcmp(dle->program, "APPLICATION")==0) {
251 dle->program_is_application_api=1;
252 skip_whitespace(s, ch); /* find dumper name */
254 goto err; /* no program */
256 dle->program = s - 1;
257 skip_non_whitespace(s, ch);
260 dle->program = stralloc(dle->program);
262 skip_whitespace(s, ch); /* find the disk name */
264 err_extra = _("no disk name");
265 goto err; /* no disk name */
271 skip_quoted_string(s, ch);
273 qdisk = stralloc(qdisk);
274 dle->disk = unquote_string(qdisk);
276 skip_whitespace(s, ch); /* find the device or level */
278 err_extra = _("bad level");
282 if(!isdigit((int)s[-1])) {
286 skip_quoted_string(s, ch);
288 qamdevice = stralloc(qamdevice);
289 dle->device = unquote_string(qamdevice);
290 skip_whitespace(s, ch); /* find level number */
293 dle->device = stralloc(dle->disk);
294 qamdevice = stralloc(qdisk);
296 /* find the level number */
297 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
298 err_extra = _("bad level");
299 goto err; /* bad level */
302 alevel = g_new0(level_t, 1);
303 alevel->level = level;
304 dle->levellist = g_slist_append(dle->levellist, alevel);
306 skip_whitespace(s, ch); /* find the dump date */
308 err_extra = _("no dumpdate");
309 goto err; /* no dumpdate */
313 skip_non_whitespace(s, ch);
315 dumpdate = stralloc(dumpdate);
317 skip_whitespace(s, ch); /* find the options keyword */
319 err_extra = _("no options");
320 goto err; /* no options */
322 if(strncmp_const_skip(s - 1, "OPTIONS ", s, ch) != 0) {
323 err_extra = _("no OPTIONS keyword");
324 goto err; /* no options */
326 skip_whitespace(s, ch); /* find the options string */
328 err_extra = _("bad options string");
329 goto err; /* no options */
332 stroptions = stralloc(s - 1);
335 if (g_options == NULL) {
336 g_printf(_("ERROR [Missing OPTIONS line in sendbackup input]\n"));
337 error(_("Missing OPTIONS line in sendbackup input\n"));
341 if (am_has_feature(g_options->features, fe_req_xml)) {
344 dle = amxml_parse_node_FILE(stdin, &errmsg);
350 err_extra = _("One DLE required");
352 } else if (dle->next) {
353 err_extra = _("Only one DLE allowed");
357 qdisk = quote_string(dle->disk);
358 if (dle->device == NULL)
359 dle->device = stralloc(dle->disk);
360 qamdevice = quote_string(dle->device);
361 dumpdate = stralloc("NODATE");
362 stroptions = stralloc("");
364 parse_options(stroptions, dle, g_options->features, 0);
368 if (dle->program == NULL ||
370 dle->device == NULL ||
371 dle->levellist == NULL ||
373 err_extra = _("no valid sendbackup request");
377 if (g_slist_length(dle->levellist) != 1) {
378 err_extra = _("Too many level");
382 alevel = (level_t *)dle->levellist->data;
383 level = alevel->level;
384 dbprintf(_(" Parsed request as: program `%s'\n"), dle->program);
385 dbprintf(_(" disk `%s'\n"), qdisk);
386 dbprintf(_(" device `%s'\n"), qamdevice);
387 dbprintf(_(" level %d\n"), level);
388 dbprintf(_(" since %s\n"), dumpdate);
389 dbprintf(_(" options `%s'\n"), stroptions);
390 dbprintf(_(" datapath `%s'\n"),
391 data_path_to_string(dle->data_path));
393 if (dle->program_is_application_api==1) {
394 /* check that the application_api exist */
396 for(i = 0; programs[i]; i++) {
397 if (strcmp(programs[i]->name, dle->program) == 0) {
401 if (programs[i] == NULL) {
402 dbprintf(_("ERROR [%s: unknown program %s]\n"), get_pname(),
404 error(_("ERROR [%s: unknown program %s]"), get_pname(),
408 program = programs[i];
412 datafd = DATA_FD_OFFSET + 0;
413 mesgfd = DATA_FD_OFFSET + 2;
414 indexfd = DATA_FD_OFFSET + 4;
416 if (!dle->create_index)
419 if (dle->auth && amandad_auth) {
420 if(strcasecmp(dle->auth, amandad_auth) != 0) {
421 g_printf(_("ERROR [client configured for auth=%s while server requested '%s']\n"),
422 amandad_auth, dle->auth);
428 g_printf("KENCRYPT\n");
431 g_printf(_("CONNECT DATA %d MESG %d INDEX %d\n"),
432 DATA_FD_OFFSET, DATA_FD_OFFSET+1,
433 indexfd == -1 ? -1 : DATA_FD_OFFSET+2);
434 g_printf(_("OPTIONS "));
435 if(am_has_feature(g_options->features, fe_rep_options_features)) {
436 g_printf("features=%s;", our_feature_string);
438 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
439 g_printf("hostname=%s;", g_options->hostname);
441 if (!am_has_feature(g_options->features, fe_rep_options_features) &&
442 !am_has_feature(g_options->features, fe_rep_options_hostname)) {
447 if (freopen("/dev/null", "w", stdout) == NULL) {
448 dbprintf(_("Error redirecting stdout to /dev/null: %s\n"),
454 if((datafd = open("/dev/null", O_RDWR)) < 0) {
455 error(_("ERROR [open of /dev/null for debug data stream: %s]\n"),
464 if(datafd == -1 || mesgfd == -1 || (dle->create_index && indexfd == -1)) {
470 mesgstream = fdopen(mesgfd,"w");
471 run_client_scripts(EXECUTE_ON_PRE_DLE_BACKUP, g_options, dle, mesgstream);
474 if (dle->program_is_application_api==1) {
479 backup_support_option_t *bsu;
480 char *compopt = NULL;
481 char *encryptopt = skip_argument;
482 int compout, dumpout;
491 /* apply client-side encryption here */
492 if ( dle->encrypt == ENCRYPT_CUST ) {
493 encpid = pipespawn(dle->clnt_encrypt, STDIN_PIPE, 0,
494 &compout, &datafd, &mesgfd,
495 dle->clnt_encrypt, encryptopt, NULL);
496 dbprintf(_("encrypt: pid %ld: %s\n"), (long)encpid, dle->clnt_encrypt);
502 /* now do the client-side compression */
503 if(dle->compress == COMP_FAST || dle->compress == COMP_BEST) {
504 compopt = skip_argument;
505 #if defined(COMPRESS_BEST_OPT) && defined(COMPRESS_FAST_OPT)
506 if(dle->compress == COMP_BEST) {
507 compopt = COMPRESS_BEST_OPT;
509 compopt = COMPRESS_FAST_OPT;
512 comppid = pipespawn(COMPRESS_PATH, STDIN_PIPE, 0,
513 &dumpout, &compout, &mesgfd,
514 COMPRESS_PATH, compopt, NULL);
515 if(compopt != skip_argument) {
516 dbprintf(_("compress pid %ld: %s %s\n"),
517 (long)comppid, COMPRESS_PATH, compopt);
519 dbprintf(_("compress pid %ld: %s\n"), (long)comppid, COMPRESS_PATH);
521 } else if (dle->compress == COMP_CUST) {
522 compopt = skip_argument;
523 comppid = pipespawn(dle->compprog, STDIN_PIPE, 0,
524 &dumpout, &compout, &mesgfd,
525 dle->compprog, compopt, NULL);
526 if(compopt != skip_argument) {
527 dbprintf(_("pid %ld: %s %s\n"),
528 (long)comppid, dle->compprog, compopt);
530 dbprintf(_("pid %ld: %s\n"), (long)comppid, dle->compprog);
537 cur_dumptime = time(0);
538 bsu = backup_support_option(dle->program, g_options, dle->disk,
539 dle->device, &errarray);
544 for (i=0; i < errarray->len; i++) {
545 errmsg = g_ptr_array_index(errarray, i);
546 qerrmsg = quote_string(errmsg);
548 _("sendbackup: error [Application '%s': %s]\n"),
549 dle->program, errmsg);
550 dbprintf("aa: %s\n",qerrmsg);
553 if (i == 0) { /* no errarray */
554 errmsg = vstrallocf(_("Can't execute application '%s'"),
556 qerrmsg = quote_string(errmsg);
557 fdprintf(mesgfd, _("sendbackup: error [%s]\n"), errmsg);
558 dbprintf(_("ERROR %s\n"), qerrmsg);
565 if (pipe(errfd) < 0) {
568 errmsg = vstrallocf(_("Application '%s': can't create pipe"),
570 qerrmsg = quote_string(errmsg);
571 fdprintf(mesgfd, _("sendbackup: error [%s]\n"), errmsg);
572 dbprintf(_("ERROR %s\n"), qerrmsg);
578 switch(application_api_pid=fork()) {
580 argv_ptr = g_ptr_array_new();
581 cmd = vstralloc(APPLICATION_DIR, "/", dle->program, NULL);
582 g_ptr_array_add(argv_ptr, stralloc(dle->program));
583 g_ptr_array_add(argv_ptr, stralloc("backup"));
584 if (bsu->message_line == 1) {
585 g_ptr_array_add(argv_ptr, stralloc("--message"));
586 g_ptr_array_add(argv_ptr, stralloc("line"));
588 if (g_options->config && bsu->config == 1) {
589 g_ptr_array_add(argv_ptr, stralloc("--config"));
590 g_ptr_array_add(argv_ptr, stralloc(g_options->config));
592 if (g_options->hostname && bsu->host == 1) {
593 g_ptr_array_add(argv_ptr, stralloc("--host"));
594 g_ptr_array_add(argv_ptr, stralloc(g_options->hostname));
596 if (dle->disk && bsu->disk == 1) {
597 g_ptr_array_add(argv_ptr, stralloc("--disk"));
598 g_ptr_array_add(argv_ptr, stralloc(dle->disk));
600 g_ptr_array_add(argv_ptr, stralloc("--device"));
601 g_ptr_array_add(argv_ptr, stralloc(dle->device));
602 if (level <= bsu->max_level) {
603 g_ptr_array_add(argv_ptr, stralloc("--level"));
604 g_snprintf(levelstr,19,"%d",level);
605 g_ptr_array_add(argv_ptr, stralloc(levelstr));
607 if (indexfd != -1 && bsu->index_line == 1) {
608 g_ptr_array_add(argv_ptr, stralloc("--index"));
609 g_ptr_array_add(argv_ptr, stralloc("line"));
611 if (dle->record && bsu->record == 1) {
612 g_ptr_array_add(argv_ptr, stralloc("--record"));
614 application_property_add_to_argv(argv_ptr, dle, bsu,
615 g_options->features);
617 for (scriptlist = dle->scriptlist; scriptlist != NULL;
618 scriptlist = scriptlist->next) {
619 script = (script_t *)scriptlist->data;
620 if (script->result && script->result->proplist) {
621 property_add_to_argv(argv_ptr, script->result->proplist);
625 g_ptr_array_add(argv_ptr, NULL);
626 dbprintf(_("%s: running \"%s\n"), get_pname(), cmd);
627 for (j = 1; j < argv_ptr->len - 1; j++)
628 dbprintf(" %s\n", (char *)g_ptr_array_index(argv_ptr,j));
630 if(dup2(dumpout, 1) == -1) {
631 error(_("Can't dup2: %s"),strerror(errno));
634 if (dup2(errfd[1], 2) == -1) {
635 error(_("Can't dup2: %s"),strerror(errno));
638 if(dup2(mesgfd, 3) == -1) {
639 error(_("Can't dup2: %s"),strerror(errno));
643 if(dup2(indexfd, 4) == -1) {
644 error(_("Can't dup2: %s"),strerror(errno));
647 fcntl(indexfd, F_SETFD, 0);
649 application_api_info_tapeheader(mesgfd, dle->program, dle);
655 execve(cmd, (char **)argv_ptr->pdata, safe_env());
662 error(_("%s: fork returned: %s"), get_pname(), strerror(errno));
666 dumperr = fdopen(errfd[0],"r");
668 error(_("Can't fdopen: %s"), strerror(errno));
673 while ((line = agets(dumperr)) != NULL) {
674 if (strlen(line) > 0) {
675 fdprintf(mesgfd, "sendbackup: error [%s]\n", line);
676 dbprintf("error: %s\n", line);
682 result |= check_result(mesgfd);
684 char *amandates_file;
686 amandates_file = getconf_str(CNF_AMANDATES);
687 if(start_amandates(amandates_file, 1)) {
688 amandates_updateone(dle->disk, level, cur_dumptime);
692 if (GPOINTER_TO_INT(dle->estimatelist->data) == ES_CALCSIZE &&
694 error(_("error [opening %s for writing: %s]"),
695 amandates_file, strerror(errno));
697 g_debug(_("non-fatal error opening '%s' for writing: %s]"),
698 amandates_file, strerror(errno));
705 /* redirect stderr */
706 if(dup2(mesgfd, 2) == -1) {
707 dbprintf(_("Error redirecting stderr to fd %d: %s\n"),
708 mesgfd, strerror(errno));
714 if(pipe(mesgpipe) == -1) {
716 dbprintf(_("error [opening mesg pipe: %s]\n"), s);
717 error(_("error [opening mesg pipe: %s]"), s);
720 program->start_backup(dle, g_options->hostname,
721 datafd, mesgpipe[1], indexfd);
722 dbprintf(_("Started backup\n"));
723 parse_backup_messages(dle, mesgpipe[0]);
724 dbprintf(_("Parsed backup messages\n"));
727 run_client_scripts(EXECUTE_ON_POST_DLE_BACKUP, g_options, dle, mesgstream);
734 amfree(our_feature_string);
735 am_release_feature_set(our_features);
737 free_g_options(g_options);
745 g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET '%s'\n"), err_extra);
746 dbprintf(_("REQ packet is bogus: %s\n"), err_extra);
748 g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET\n"));
749 dbprintf(_("REQ packet is bogus\n"));
756 amfree(our_feature_string);
764 * Returns a string for a child process. Checks the saved dump and
765 * compress pids to see which it is.
772 if(pid == dumppid) return program->backup_name;
773 if(pid == comppid) return "compress";
774 if(pid == encpid) return "encrypt";
775 if(pid == indexpid) return "index";
776 if(pid == application_api_pid) {
778 dbprintf("gdle == NULL\n");
779 return "gdle == NULL";
781 return gdle->program;
788 * Determine if the child return status really indicates an error.
789 * If so, add the error message to the error string; more than one
790 * child can have an error.
799 char *thiserr = NULL;
807 rc = sig = WTERMSIG(w);
810 rc = ret = WEXITSTATUS(w);
813 if(pid == indexpid) {
815 * Treat an index failure (other than signal) as a "STRANGE"
816 * rather than an error so the dump goes ahead and gets processed
817 * but the failure is noted.
820 fdprintf(mesgfd, _("? index %s returned %d\n"), str, ret);
825 } else if(pid == comppid) {
827 * compress returns 2 sometimes, but it is ok.
836 } else if(pid == dumppid && tarpid == -1) {
838 * Ultrix dump returns 1 sometimes, but it is ok.
840 #ifdef DUMP_RETURNS_1
847 } else if(pid == tarpid) {
852 * tar bitches about active filesystems, but we do not care.
854 #ifdef IGNORE_TAR_ERRORS
859 dumppid = tarpid = -1;
861 } else if(pid == application_api_pid) {
862 strX = "Application";
868 return 0; /* normal exit */
872 thiserr = vstrallocf(_("%s (%d) %s got signal %d"), strX, (int)pid, str,
875 thiserr = vstrallocf(_("%s (%d) %s returned %d"), strX, (int)pid, str, ret);
878 fdprintf(mesgfd, "? %s\n", thiserr);
881 errorstr = newvstrallocf(errorstr, "%s, %s", errorstr, thiserr);
892 *Send header info to the message file.
898 g_fprintf(stderr, "%s: info BACKUP=%s\n", get_pname(), program->backup_name);
900 g_fprintf(stderr, "%s: info RECOVER_CMD=", get_pname());
901 if (dle->compress == COMP_FAST || dle->compress == COMP_BEST)
902 g_fprintf(stderr, "%s %s |", UNCOMPRESS_PATH,
903 #ifdef UNCOMPRESS_OPT
910 g_fprintf(stderr, "%s -xpGf - ...\n", program->restore_name);
912 if (dle->compress == COMP_FAST || dle->compress == COMP_BEST)
913 g_fprintf(stderr, "%s: info COMPRESS_SUFFIX=%s\n",
914 get_pname(), COMPRESS_SUFFIX);
916 g_fprintf(stderr, "%s: info end\n", get_pname());
920 application_api_info_tapeheader(
927 g_snprintf(line, 1024, "%s: info BACKUP=APPLICATION\n", get_pname());
928 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
929 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
933 g_snprintf(line, 1024, "%s: info APPLICATION=%s\n", get_pname(), prog);
934 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
935 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
939 g_snprintf(line, 1024, "%s: info RECOVER_CMD=", get_pname());
940 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
941 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
945 if (dle->compress == COMP_FAST || dle->compress == COMP_BEST) {
946 g_snprintf(line, 1024, "%s %s |", UNCOMPRESS_PATH,
947 #ifdef UNCOMPRESS_OPT
953 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
954 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
958 g_snprintf(line, 1024, "%s/%s restore [./file-to-restore]+\n",
959 APPLICATION_DIR, prog);
960 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
961 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
966 g_snprintf(line, 1024, "%s: info COMPRESS_SUFFIX=%s\n",
967 get_pname(), COMPRESS_SUFFIX);
968 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
969 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
974 g_snprintf(line, 1024, "%s: info end\n", get_pname());
975 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
976 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
992 dbprintf(_("Forking function %s in pipeline\n"), fname);
994 if(pipe(inpipe) == -1) {
995 error(_("error [open pipe to %s: %s]"), fname, strerror(errno));
999 switch(pid = fork()) {
1001 error(_("error [fork %s: %s]"), fname, strerror(errno));
1003 default: /* parent process */
1004 aclose(inpipe[0]); /* close input side of pipe */
1005 *stdinfd = inpipe[1];
1007 case 0: /* child process */
1008 aclose(inpipe[1]); /* close output side of pipe */
1010 if(dup2(inpipe[0], 0) == -1) {
1011 error(_("error [fork %s: dup2(%d, in): %s]"),
1012 fname, inpipe[0], strerror(errno));
1015 if(dup2(stdoutfd, 1) == -1) {
1016 error(_("error [fork %s: dup2(%d, out): %s]"),
1017 fname, stdoutfd, strerror(errno));
1020 if(dup2(stderrfd, 2) == -1) {
1021 error(_("error [fork %s: dup2(%d, err): %s]"),
1022 fname, stderrfd, strerror(errno));
1044 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
1045 if(check_status(wpid, retstat, mesgfd)) goterror = 1;
1048 if (dumppid != -1) {
1050 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
1051 if(check_status(wpid, retstat, mesgfd)) goterror = 1;
1054 if (dumppid != -1) {
1055 dbprintf(_("Sending SIGHUP to dump process %d\n"),
1058 if(kill(dumppid, SIGHUP) == -1) {
1059 dbprintf(_("Can't send SIGHUP to %d: %s\n"),
1065 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
1066 if(check_status(wpid, retstat, mesgfd)) goterror = 1;
1069 if (dumppid != -1) {
1070 dbprintf(_("Sending SIGKILL to dump process %d\n"),
1073 if(kill(dumppid, SIGKILL) == -1) {
1074 dbprintf(_("Can't send SIGKILL to %d: %s\n"),
1080 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
1081 if(check_status(wpid, retstat, mesgfd)) goterror = 1;
1089 parse_backup_messages(
1098 for(; (line = areads(mesgin)) != NULL; free(line)) {
1099 process_dumpline(line);
1103 error(_("error [read mesg pipe: %s]"), strerror(errno));
1107 goterror = check_result(mesgfd);
1110 error(_("error [%s]"), errorstr);
1112 } else if(dump_size == -1) {
1113 error(_("error [no backup size line]"));
1117 program->end_backup(dle, goterror);
1119 fdprintf(mesgfd, _("%s: size %ld\n"), get_pname(), dump_size);
1120 fdprintf(mesgfd, _("%s: end\n"), get_pname());
1132 for(rp = program->re_table; rp->regex != NULL; rp++) {
1133 if(match(rp->regex, str)) {
1137 if(rp->typ == DMP_SIZE) {
1138 dump_size = (long)((the_num(str, rp->field)* rp->scale+1023.0)/1024.0);
1159 * Should never get here.
1165 dbprintf("%3d: %7s(%c): %s\n",
1170 fdprintf(mesgfd, "%c %s\n", startchr, str);
1175 * start_index. Creates an index file from the output of dump/tar.
1176 * It arranges that input is the fd to be written by the dump process.
1177 * If createindex is not enabled, it does nothing. If it is not, a
1178 * new process will be created that tees input both to a pipe whose
1179 * read fd is dup2'ed input and to a program that outputs an index
1182 * make sure that the chat from restore doesn't go to stderr cause
1183 * this goes back to amanda which doesn't expect to see it
1184 * (2>/dev/null should do it)
1186 * Originally by Alan M. McIvor, 13 April 1996
1188 * Adapted by Alexandre Oliva, 1 May 1997
1190 * This program owes a lot to tee.c from GNU sh-utils and dumptee.c
1191 * from the DeeJay backup package.
1201 while (*fd >= 0 && *fd < min) {
1202 int newfd = dup(*fd);
1204 dbprintf(_("Unable to save file descriptor [%s]\n"), strerror(errno));
1208 dbprintf(_("Dupped file descriptor %i to %i\n"), origfd, *fd);
1226 if (pipe(pipefd) != 0) {
1227 error(_("creating index pipe: %s"), strerror(errno));
1231 switch(indexpid = fork()) {
1233 error(_("forking index tee process: %s"), strerror(errno));
1238 if (dup2(pipefd[1], input) == -1) {
1239 error(_("dup'ping index tee output: %s"), strerror(errno));
1249 /* now in a child process */
1250 save_fd(&pipefd[0], 4);
1258 for(index = 4; index < (int)FD_SETSIZE; index++) {
1259 if (index != dbfd()) {
1264 if ((pipe_fp = popen(cmd, "w")) == NULL) {
1265 error(_("couldn't start index creator [%s]"), strerror(errno));
1269 dbprintf(_("Started index creator: \"%s\"\n"), cmd);
1271 char buffer[BUFSIZ], *ptr;
1273 size_t bytes_written;
1274 size_t just_written;
1277 bytes_read = read(0, buffer, SIZEOF(buffer));
1278 } while ((bytes_read < 0) && ((errno == EINTR) || (errno == EAGAIN)));
1280 if (bytes_read < 0) {
1281 error(_("index tee cannot read [%s]"), strerror(errno));
1285 if (bytes_read == 0)
1286 break; /* finished */
1288 /* write the stuff to the subprocess */
1291 just_written = full_write(fileno(pipe_fp), ptr, (size_t)bytes_read);
1292 if (just_written < (size_t)bytes_read) {
1294 * just as we waited for write() to complete.
1296 if (errno != EPIPE) {
1297 dbprintf(_("Index tee cannot write to index creator [%s]\n"),
1301 bytes_written += just_written;
1302 ptr += just_written;
1305 /* write the stuff to stdout, ensuring none lost when interrupt
1309 just_written = full_write(3, ptr, bytes_read);
1310 if (just_written < (size_t)bytes_read) {
1311 error(_("index tee cannot write [%s]"), strerror(errno));
1314 bytes_written += just_written;
1315 ptr += just_written;
1322 /* check the exit code of the pipe and moan if not 0 */
1323 if ((exitcode = pclose(pipe_fp)) != 0) {
1324 char *exitstr = str_exit_status("Index pipe", exitcode);
1325 dbprintf("%s\n", exitstr);
1328 dbprintf(_("Index created successfully\n"));
1335 extern backup_program_t dump_program, gnutar_program;
1337 backup_program_t *programs[] = {
1338 &dump_program, &gnutar_program, NULL