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"
44 #define sendbackup_debug(i, ...) do { \
45 if ((i) <= debug_sendbackup) { \
46 dbprintf(__VA_LIST__); \
52 pid_t comppid = (pid_t)-1;
53 pid_t dumppid = (pid_t)-1;
54 pid_t tarpid = (pid_t)-1;
55 pid_t encpid = (pid_t)-1;
56 pid_t indexpid = (pid_t)-1;
57 pid_t application_api_pid = (pid_t)-1;
58 char *errorstr = NULL;
64 g_option_t *g_options = NULL;
68 backup_program_t *program = NULL;
71 static am_feature_t *our_features = NULL;
72 static char *our_feature_string = NULL;
73 static char *amandad_auth = NULL;
76 int main(int argc, char **argv);
77 char *childstr(pid_t pid);
78 int check_status(pid_t pid, amwait_t w, int mesgfd);
80 pid_t pipefork(void (*func)(void), char *fname, int *stdinfd,
81 int stdoutfd, int stderrfd);
82 int check_result(int mesgfd);
83 void parse_backup_messages(dle_t *dle, int mesgin);
84 static void process_dumpline(char *str);
85 static void save_fd(int *, int);
86 void application_api_info_tapeheader(int mesgfd, char *prog, dle_t *dle);
88 int fdprintf(int fd, char *format, ...) G_GNUC_PRINTF(2, 3);
100 arglist_start(argp, format);
101 s = g_strdup_vprintf(format, argp);
104 r = full_write(fd, s, strlen(s));
118 char *dumpdate, *stroptions;
120 char *qamdevice = NULL;
122 char *err_extra = NULL;
132 * Configure program for internationalization:
133 * 1) Only set the message locale for now.
134 * 2) Set textdomain for all amanda related programs to "amanda"
135 * We don't want to be forced to support dozens of message catalogs.
137 setlocale(LC_MESSAGES, "C");
138 textdomain("amanda");
140 safe_fd(DATA_FD_OFFSET, DATA_FD_COUNT*2);
145 set_pname("sendbackup");
147 /* Don't die when child closes pipe */
148 signal(SIGPIPE, SIG_IGN);
150 /* Don't die when interrupt received */
151 signal(SIGINT, SIG_IGN);
153 if(argc > 1 && strcmp(argv[1],"-t") == 0) {
161 add_amanda_log_handler(amanda_log_stderr);
162 add_amanda_log_handler(amanda_log_syslog);
163 dbopen(DBG_SUBDIR_CLIENT);
165 dbprintf(_("Version %s\n"), VERSION);
167 if(argc > 2 && strcmp(argv[1], "amandad") == 0) {
168 amandad_auth = stralloc(argv[2]);
171 our_features = am_init_feature_set();
172 our_feature_string = am_feature_to_string(our_features);
174 config_init(CONFIG_INIT_CLIENT, NULL);
175 /* (check for config errors comes later) */
177 check_running_as(RUNNING_AS_CLIENT_LOGIN);
181 * In interactive (debug) mode, the backup data is sent to
182 * /dev/null and none of the network connections back to driver
183 * programs on the tape host are set up. The index service is
184 * run and goes to stdout.
186 g_fprintf(stderr, _("%s: running in interactive test mode\n"), get_pname());
194 for(; (line = agets(stdin)) != NULL; free(line)) {
198 g_fprintf(stderr, "%s> ", get_pname());
201 if(strncmp_const(line, "OPTIONS ") == 0) {
202 g_options = parse_g_options(line+8, 1);
203 if(!g_options->hostname) {
204 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
205 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
206 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
209 if (g_options->config) {
210 /* overlay this configuration on the existing (nameless) configuration */
211 config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
214 dbrename(get_config_name(), DBG_SUBDIR_CLIENT);
217 /* check for any config errors now */
218 if (config_errors(&errlist) >= CFGERR_ERRORS) {
219 char *errstr = config_errors_to_error_string(errlist);
220 g_printf("%s\n", errstr);
225 if (am_has_feature(g_options->features, fe_req_xml)) {
231 if (dle && dle->program != NULL) {
232 err_extra = _("multiple requests");
236 dbprintf(_(" sendbackup req: <%s>\n"), line);
242 skip_whitespace(s, ch); /* find the program name */
244 err_extra = _("no program name");
245 goto err; /* no program name */
247 dle->program = s - 1;
248 skip_non_whitespace(s, ch);
251 if (strcmp(dle->program, "APPLICATION")==0) {
252 dle->program_is_application_api=1;
253 skip_whitespace(s, ch); /* find dumper name */
255 goto err; /* no program */
257 dle->program = s - 1;
258 skip_non_whitespace(s, ch);
261 dle->program = stralloc(dle->program);
263 skip_whitespace(s, ch); /* find the disk name */
265 err_extra = _("no disk name");
266 goto err; /* no disk name */
272 skip_quoted_string(s, ch);
274 qdisk = stralloc(qdisk);
275 dle->disk = unquote_string(qdisk);
277 skip_whitespace(s, ch); /* find the device or level */
279 err_extra = _("bad level");
283 if(!isdigit((int)s[-1])) {
287 skip_quoted_string(s, ch);
289 qamdevice = stralloc(qamdevice);
290 dle->device = unquote_string(qamdevice);
291 skip_whitespace(s, ch); /* find level number */
294 dle->device = stralloc(dle->disk);
295 qamdevice = stralloc(qdisk);
297 /* find the level number */
298 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
299 err_extra = _("bad level");
300 goto err; /* bad level */
303 alevel = g_new0(level_t, 1);
304 alevel->level = level;
305 dle->levellist = g_slist_append(dle->levellist, alevel);
307 skip_whitespace(s, ch); /* find the dump date */
309 err_extra = _("no dumpdate");
310 goto err; /* no dumpdate */
314 skip_non_whitespace(s, ch);
316 dumpdate = stralloc(dumpdate);
318 skip_whitespace(s, ch); /* find the options keyword */
320 err_extra = _("no options");
321 goto err; /* no options */
323 if(strncmp_const_skip(s - 1, "OPTIONS ", s, ch) != 0) {
324 err_extra = _("no OPTIONS keyword");
325 goto err; /* no options */
327 skip_whitespace(s, ch); /* find the options string */
329 err_extra = _("bad options string");
330 goto err; /* no options */
333 stroptions = stralloc(s - 1);
336 if (g_options == NULL) {
337 g_printf(_("ERROR [Missing OPTIONS line in sendbackup input]\n"));
338 error(_("Missing OPTIONS line in sendbackup input\n"));
342 if (am_has_feature(g_options->features, fe_req_xml)) {
345 dle = amxml_parse_node_FILE(stdin, &errmsg);
351 err_extra = _("One DLE required");
353 } else if (dle->next) {
354 err_extra = _("Only one DLE allowed");
358 qdisk = quote_string(dle->disk);
359 if (dle->device == NULL)
360 dle->device = stralloc(dle->disk);
361 qamdevice = quote_string(dle->device);
362 dumpdate = stralloc("NODATE");
363 stroptions = stralloc("");
365 parse_options(stroptions, dle, g_options->features, 0);
369 if (dle->program == NULL ||
371 dle->device == NULL ||
372 dle->levellist == NULL ||
374 err_extra = _("no valid sendbackup request");
378 if (g_slist_length(dle->levellist) != 1) {
379 err_extra = _("Too many level");
383 alevel = (level_t *)dle->levellist->data;
384 level = alevel->level;
385 dbprintf(_(" Parsed request as: program `%s'\n"), dle->program);
386 dbprintf(_(" disk `%s'\n"), qdisk);
387 dbprintf(_(" device `%s'\n"), qamdevice);
388 dbprintf(_(" level %d\n"), level);
389 dbprintf(_(" since %s\n"), dumpdate);
390 dbprintf(_(" options `%s'\n"), stroptions);
391 dbprintf(_(" datapath `%s'\n"),
392 data_path_to_string(dle->data_path));
394 if (dle->program_is_application_api==1) {
395 /* check that the application_api exist */
397 for(i = 0; programs[i]; i++) {
398 if (strcmp(programs[i]->name, dle->program) == 0) {
402 if (programs[i] == NULL) {
403 dbprintf(_("ERROR [%s: unknown program %s]\n"), get_pname(),
405 error(_("ERROR [%s: unknown program %s]"), get_pname(),
409 program = programs[i];
413 datafd = DATA_FD_OFFSET + 0;
414 mesgfd = DATA_FD_OFFSET + 2;
415 indexfd = DATA_FD_OFFSET + 4;
417 if (!dle->create_index)
420 if (dle->auth && amandad_auth) {
421 if(strcasecmp(dle->auth, amandad_auth) != 0) {
422 g_printf(_("ERROR [client configured for auth=%s while server requested '%s']\n"),
423 amandad_auth, dle->auth);
429 g_printf("KENCRYPT\n");
432 g_printf(_("CONNECT DATA %d MESG %d INDEX %d\n"),
433 DATA_FD_OFFSET, DATA_FD_OFFSET+1,
434 indexfd == -1 ? -1 : DATA_FD_OFFSET+2);
435 g_printf(_("OPTIONS "));
436 if(am_has_feature(g_options->features, fe_rep_options_features)) {
437 g_printf("features=%s;", our_feature_string);
439 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
440 g_printf("hostname=%s;", g_options->hostname);
442 if (!am_has_feature(g_options->features, fe_rep_options_features) &&
443 !am_has_feature(g_options->features, fe_rep_options_hostname)) {
448 if (freopen("/dev/null", "w", stdout) == NULL) {
449 dbprintf(_("Error redirecting stdout to /dev/null: %s\n"),
455 if((datafd = open("/dev/null", O_RDWR)) < 0) {
456 error(_("ERROR [open of /dev/null for debug data stream: %s]\n"),
465 if(datafd == -1 || mesgfd == -1 || (dle->create_index && indexfd == -1)) {
471 mesgstream = fdopen(mesgfd,"w");
472 run_client_scripts(EXECUTE_ON_PRE_DLE_BACKUP, g_options, dle, mesgstream);
475 if (dle->program_is_application_api==1) {
480 backup_support_option_t *bsu;
481 char *compopt = NULL;
482 char *encryptopt = skip_argument;
483 int compout, dumpout;
492 /* apply client-side encryption here */
493 if ( dle->encrypt == ENCRYPT_CUST ) {
494 encpid = pipespawn(dle->clnt_encrypt, STDIN_PIPE, 0,
495 &compout, &datafd, &mesgfd,
496 dle->clnt_encrypt, encryptopt, NULL);
497 dbprintf(_("encrypt: pid %ld: %s\n"), (long)encpid, dle->clnt_encrypt);
503 /* now do the client-side compression */
504 if(dle->compress == COMP_FAST || dle->compress == COMP_BEST) {
505 compopt = skip_argument;
506 #if defined(COMPRESS_BEST_OPT) && defined(COMPRESS_FAST_OPT)
507 if(dle->compress == COMP_BEST) {
508 compopt = COMPRESS_BEST_OPT;
510 compopt = COMPRESS_FAST_OPT;
513 comppid = pipespawn(COMPRESS_PATH, STDIN_PIPE, 0,
514 &dumpout, &compout, &mesgfd,
515 COMPRESS_PATH, compopt, NULL);
516 if(compopt != skip_argument) {
517 dbprintf(_("compress pid %ld: %s %s\n"),
518 (long)comppid, COMPRESS_PATH, compopt);
520 dbprintf(_("compress pid %ld: %s\n"), (long)comppid, COMPRESS_PATH);
522 } else if (dle->compress == COMP_CUST) {
523 compopt = skip_argument;
524 comppid = pipespawn(dle->compprog, STDIN_PIPE, 0,
525 &dumpout, &compout, &mesgfd,
526 dle->compprog, compopt, NULL);
527 if(compopt != skip_argument) {
528 dbprintf(_("pid %ld: %s %s\n"),
529 (long)comppid, dle->compprog, compopt);
531 dbprintf(_("pid %ld: %s\n"), (long)comppid, dle->compprog);
538 cur_dumptime = time(0);
539 bsu = backup_support_option(dle->program, g_options, dle->disk,
540 dle->device, &errarray);
545 for (i=0; i < errarray->len; i++) {
546 errmsg = g_ptr_array_index(errarray, i);
547 qerrmsg = quote_string(errmsg);
549 _("sendbackup: error [Application '%s': %s]\n"),
550 dle->program, errmsg);
551 dbprintf("aa: %s\n",qerrmsg);
554 if (i == 0) { /* no errarray */
555 errmsg = vstrallocf(_("Can't execute application '%s'"),
557 qerrmsg = quote_string(errmsg);
558 fdprintf(mesgfd, _("sendbackup: error [%s]\n"), errmsg);
559 dbprintf(_("ERROR %s\n"), qerrmsg);
566 if (pipe(errfd) < 0) {
569 errmsg = vstrallocf(_("Application '%s': can't create pipe"),
571 qerrmsg = quote_string(errmsg);
572 fdprintf(mesgfd, _("sendbackup: error [%s]\n"), errmsg);
573 dbprintf(_("ERROR %s\n"), qerrmsg);
579 switch(application_api_pid=fork()) {
581 application_api_info_tapeheader(mesgfd, dle->program, dle);
583 /* find directt-tcp address from indirect direct-tcp */
584 if (dle->data_path == DATA_PATH_DIRECTTCP &&
585 bsu->data_path_set & DATA_PATH_DIRECTTCP &&
586 strncmp(dle->directtcp_list->data, "255.255.255.255:", 16) == 0) {
595 indirect_tcp = stralloc(dle->directtcp_list->data);
596 g_slist_free(dle->directtcp_list);
597 dle->directtcp_list = NULL;
598 str_port = strchr(indirect_tcp, ':');
600 port = atoi(str_port);
601 fd = stream_client("localhost", port, 32768, 32768, NULL, 0);
603 g_debug("Failed to connect to indirect-direct-tcp port: %s",
607 size = full_read(fd, buffer, 32768);
609 g_debug("Failed to read from indirect-direct-tcp port: %s",
613 buffer[size++] = ' ';
616 while ((s = strchr(s1, ' ')) != NULL) {
618 dle->directtcp_list = g_slist_append(dle->directtcp_list, stralloc(s1));
621 amfree(indirect_tcp);
624 argv_ptr = g_ptr_array_new();
625 cmd = vstralloc(APPLICATION_DIR, "/", dle->program, NULL);
626 g_ptr_array_add(argv_ptr, stralloc(dle->program));
627 g_ptr_array_add(argv_ptr, stralloc("backup"));
628 if (bsu->message_line == 1) {
629 g_ptr_array_add(argv_ptr, stralloc("--message"));
630 g_ptr_array_add(argv_ptr, stralloc("line"));
632 if (g_options->config && bsu->config == 1) {
633 g_ptr_array_add(argv_ptr, stralloc("--config"));
634 g_ptr_array_add(argv_ptr, stralloc(g_options->config));
636 if (g_options->hostname && bsu->host == 1) {
637 g_ptr_array_add(argv_ptr, stralloc("--host"));
638 g_ptr_array_add(argv_ptr, stralloc(g_options->hostname));
640 if (dle->disk && bsu->disk == 1) {
641 g_ptr_array_add(argv_ptr, stralloc("--disk"));
642 g_ptr_array_add(argv_ptr, stralloc(dle->disk));
644 g_ptr_array_add(argv_ptr, stralloc("--device"));
645 g_ptr_array_add(argv_ptr, stralloc(dle->device));
646 if (level <= bsu->max_level) {
647 g_ptr_array_add(argv_ptr, stralloc("--level"));
648 g_snprintf(levelstr,19,"%d",level);
649 g_ptr_array_add(argv_ptr, stralloc(levelstr));
651 if (indexfd != -1 && bsu->index_line == 1) {
652 g_ptr_array_add(argv_ptr, stralloc("--index"));
653 g_ptr_array_add(argv_ptr, stralloc("line"));
655 if (dle->record && bsu->record == 1) {
656 g_ptr_array_add(argv_ptr, stralloc("--record"));
658 application_property_add_to_argv(argv_ptr, dle, bsu,
659 g_options->features);
661 for (scriptlist = dle->scriptlist; scriptlist != NULL;
662 scriptlist = scriptlist->next) {
663 script = (script_t *)scriptlist->data;
664 if (script->result && script->result->proplist) {
665 property_add_to_argv(argv_ptr, script->result->proplist);
669 g_ptr_array_add(argv_ptr, NULL);
670 dbprintf(_("%s: running \"%s\n"), get_pname(), cmd);
671 for (j = 1; j < argv_ptr->len - 1; j++)
672 dbprintf(" %s\n", (char *)g_ptr_array_index(argv_ptr,j));
674 if(dup2(dumpout, 1) == -1) {
675 error(_("Can't dup2: %s"),strerror(errno));
678 if (dup2(errfd[1], 2) == -1) {
679 error(_("Can't dup2: %s"),strerror(errno));
682 if(dup2(mesgfd, 3) == -1) {
683 error(_("Can't dup2: %s"),strerror(errno));
687 if(dup2(indexfd, 4) == -1) {
688 error(_("Can't dup2: %s"),strerror(errno));
691 fcntl(indexfd, F_SETFD, 0);
698 execve(cmd, (char **)argv_ptr->pdata, safe_env());
705 error(_("%s: fork returned: %s"), get_pname(), strerror(errno));
709 dumperr = fdopen(errfd[0],"r");
711 error(_("Can't fdopen: %s"), strerror(errno));
716 while ((line = agets(dumperr)) != NULL) {
717 if (strlen(line) > 0) {
718 fdprintf(mesgfd, "sendbackup: error [%s]\n", line);
719 dbprintf("error: %s\n", line);
725 result |= check_result(mesgfd);
727 char *amandates_file;
729 amandates_file = getconf_str(CNF_AMANDATES);
730 if(start_amandates(amandates_file, 1)) {
731 amandates_updateone(dle->disk, level, cur_dumptime);
735 if (GPOINTER_TO_INT(dle->estimatelist->data) == ES_CALCSIZE &&
737 error(_("error [opening %s for writing: %s]"),
738 amandates_file, strerror(errno));
740 g_debug(_("non-fatal error opening '%s' for writing: %s]"),
741 amandates_file, strerror(errno));
748 /* redirect stderr */
749 if(dup2(mesgfd, 2) == -1) {
750 dbprintf(_("Error redirecting stderr to fd %d: %s\n"),
751 mesgfd, strerror(errno));
757 if(pipe(mesgpipe) == -1) {
759 dbprintf(_("error [opening mesg pipe: %s]\n"), s);
760 error(_("error [opening mesg pipe: %s]"), s);
763 program->start_backup(dle, g_options->hostname,
764 datafd, mesgpipe[1], indexfd);
765 dbprintf(_("Started backup\n"));
766 parse_backup_messages(dle, mesgpipe[0]);
767 dbprintf(_("Parsed backup messages\n"));
770 run_client_scripts(EXECUTE_ON_POST_DLE_BACKUP, g_options, dle, mesgstream);
777 amfree(our_feature_string);
778 am_release_feature_set(our_features);
780 free_g_options(g_options);
788 g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET '%s'\n"), err_extra);
789 dbprintf(_("REQ packet is bogus: %s\n"), err_extra);
791 g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET\n"));
792 dbprintf(_("REQ packet is bogus\n"));
799 amfree(our_feature_string);
807 * Returns a string for a child process. Checks the saved dump and
808 * compress pids to see which it is.
815 if(pid == dumppid) return program->backup_name;
816 if(pid == comppid) return "compress";
817 if(pid == encpid) return "encrypt";
818 if(pid == indexpid) return "index";
819 if(pid == application_api_pid) {
821 dbprintf("gdle == NULL\n");
822 return "gdle == NULL";
824 return gdle->program;
831 * Determine if the child return status really indicates an error.
832 * If so, add the error message to the error string; more than one
833 * child can have an error.
842 char *thiserr = NULL;
850 rc = sig = WTERMSIG(w);
853 rc = ret = WEXITSTATUS(w);
856 if(pid == indexpid) {
858 * Treat an index failure (other than signal) as a "STRANGE"
859 * rather than an error so the dump goes ahead and gets processed
860 * but the failure is noted.
863 fdprintf(mesgfd, _("? index %s returned %d\n"), str, ret);
868 } else if(pid == comppid) {
870 * compress returns 2 sometimes, but it is ok.
879 } else if(pid == dumppid && tarpid == -1) {
881 * Ultrix dump returns 1 sometimes, but it is ok.
883 #ifdef DUMP_RETURNS_1
890 } else if(pid == tarpid) {
895 * tar bitches about active filesystems, but we do not care.
897 #ifdef IGNORE_TAR_ERRORS
902 dumppid = tarpid = -1;
904 } else if(pid == application_api_pid) {
905 strX = "Application";
911 return 0; /* normal exit */
915 thiserr = vstrallocf(_("%s (%d) %s got signal %d"), strX, (int)pid, str,
918 thiserr = vstrallocf(_("%s (%d) %s returned %d"), strX, (int)pid, str, ret);
921 fdprintf(mesgfd, "? %s\n", thiserr);
924 errorstr = newvstrallocf(errorstr, "%s, %s", errorstr, thiserr);
935 *Send header info to the message file.
941 g_fprintf(stderr, "%s: info BACKUP=%s\n", get_pname(), program->backup_name);
943 g_fprintf(stderr, "%s: info RECOVER_CMD=", get_pname());
944 if (dle->compress == COMP_FAST || dle->compress == COMP_BEST)
945 g_fprintf(stderr, "%s %s |", UNCOMPRESS_PATH,
946 #ifdef UNCOMPRESS_OPT
953 g_fprintf(stderr, "%s -xpGf - ...\n", program->restore_name);
955 if (dle->compress == COMP_FAST || dle->compress == COMP_BEST)
956 g_fprintf(stderr, "%s: info COMPRESS_SUFFIX=%s\n",
957 get_pname(), COMPRESS_SUFFIX);
959 g_fprintf(stderr, "%s: info end\n", get_pname());
963 application_api_info_tapeheader(
970 g_snprintf(line, 1024, "%s: info BACKUP=APPLICATION\n", get_pname());
971 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
972 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
976 g_snprintf(line, 1024, "%s: info APPLICATION=%s\n", get_pname(), prog);
977 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
978 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
982 g_snprintf(line, 1024, "%s: info RECOVER_CMD=", get_pname());
983 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
984 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
988 if (dle->compress == COMP_FAST || dle->compress == COMP_BEST) {
989 g_snprintf(line, 1024, "%s %s |", UNCOMPRESS_PATH,
990 #ifdef UNCOMPRESS_OPT
996 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
997 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
1001 g_snprintf(line, 1024, "%s/%s restore [./file-to-restore]+\n",
1002 APPLICATION_DIR, prog);
1003 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
1004 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
1008 if (dle->compress) {
1009 g_snprintf(line, 1024, "%s: info COMPRESS_SUFFIX=%s\n",
1010 get_pname(), COMPRESS_SUFFIX);
1011 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
1012 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
1017 g_snprintf(line, 1024, "%s: info end\n", get_pname());
1018 if (full_write(mesgfd, line, strlen(line)) != strlen(line)) {
1019 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
1035 dbprintf(_("Forking function %s in pipeline\n"), fname);
1037 if(pipe(inpipe) == -1) {
1038 error(_("error [open pipe to %s: %s]"), fname, strerror(errno));
1042 switch(pid = fork()) {
1044 error(_("error [fork %s: %s]"), fname, strerror(errno));
1046 default: /* parent process */
1047 aclose(inpipe[0]); /* close input side of pipe */
1048 *stdinfd = inpipe[1];
1050 case 0: /* child process */
1051 aclose(inpipe[1]); /* close output side of pipe */
1053 if(dup2(inpipe[0], 0) == -1) {
1054 error(_("error [fork %s: dup2(%d, in): %s]"),
1055 fname, inpipe[0], strerror(errno));
1058 if(dup2(stdoutfd, 1) == -1) {
1059 error(_("error [fork %s: dup2(%d, out): %s]"),
1060 fname, stdoutfd, strerror(errno));
1063 if(dup2(stderrfd, 2) == -1) {
1064 error(_("error [fork %s: dup2(%d, err): %s]"),
1065 fname, stderrfd, strerror(errno));
1087 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
1088 if(check_status(wpid, retstat, mesgfd)) goterror = 1;
1091 if (dumppid != -1) {
1093 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
1094 if(check_status(wpid, retstat, mesgfd)) goterror = 1;
1097 if (dumppid != -1) {
1098 dbprintf(_("Sending SIGHUP to dump process %d\n"),
1101 if(kill(dumppid, SIGHUP) == -1) {
1102 dbprintf(_("Can't send SIGHUP to %d: %s\n"),
1108 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
1109 if(check_status(wpid, retstat, mesgfd)) goterror = 1;
1112 if (dumppid != -1) {
1113 dbprintf(_("Sending SIGKILL to dump process %d\n"),
1116 if(kill(dumppid, SIGKILL) == -1) {
1117 dbprintf(_("Can't send SIGKILL to %d: %s\n"),
1123 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
1124 if(check_status(wpid, retstat, mesgfd)) goterror = 1;
1132 parse_backup_messages(
1141 for(; (line = areads(mesgin)) != NULL; free(line)) {
1142 process_dumpline(line);
1146 error(_("error [read mesg pipe: %s]"), strerror(errno));
1150 goterror = check_result(mesgfd);
1153 error(_("error [%s]"), errorstr);
1155 } else if(dump_size == -1) {
1156 error(_("error [no backup size line]"));
1160 program->end_backup(dle, goterror);
1162 fdprintf(mesgfd, _("%s: size %ld\n"), get_pname(), dump_size);
1163 fdprintf(mesgfd, _("%s: end\n"), get_pname());
1175 for(rp = program->re_table; rp->regex != NULL; rp++) {
1176 if(match(rp->regex, str)) {
1180 if(rp->typ == DMP_SIZE) {
1181 dump_size = (long)((the_num(str, rp->field)* rp->scale+1023.0)/1024.0);
1202 * Should never get here.
1208 dbprintf("%3d: %7s(%c): %s\n",
1213 fdprintf(mesgfd, "%c %s\n", startchr, str);
1218 * start_index. Creates an index file from the output of dump/tar.
1219 * It arranges that input is the fd to be written by the dump process.
1220 * If createindex is not enabled, it does nothing. If it is not, a
1221 * new process will be created that tees input both to a pipe whose
1222 * read fd is dup2'ed input and to a program that outputs an index
1225 * make sure that the chat from restore doesn't go to stderr cause
1226 * this goes back to amanda which doesn't expect to see it
1227 * (2>/dev/null should do it)
1229 * Originally by Alan M. McIvor, 13 April 1996
1231 * Adapted by Alexandre Oliva, 1 May 1997
1233 * This program owes a lot to tee.c from GNU sh-utils and dumptee.c
1234 * from the DeeJay backup package.
1244 while (*fd >= 0 && *fd < min) {
1245 int newfd = dup(*fd);
1247 dbprintf(_("Unable to save file descriptor [%s]\n"), strerror(errno));
1251 dbprintf(_("Dupped file descriptor %i to %i\n"), origfd, *fd);
1269 if (pipe(pipefd) != 0) {
1270 error(_("creating index pipe: %s"), strerror(errno));
1274 switch(indexpid = fork()) {
1276 error(_("forking index tee process: %s"), strerror(errno));
1281 if (dup2(pipefd[1], input) == -1) {
1282 error(_("dup'ping index tee output: %s"), strerror(errno));
1292 /* now in a child process */
1293 save_fd(&pipefd[0], 4);
1301 for(index = 4; index < (int)FD_SETSIZE; index++) {
1302 if (index != dbfd()) {
1307 if ((pipe_fp = popen(cmd, "w")) == NULL) {
1308 error(_("couldn't start index creator [%s]"), strerror(errno));
1312 dbprintf(_("Started index creator: \"%s\"\n"), cmd);
1314 char buffer[BUFSIZ], *ptr;
1316 size_t bytes_written;
1317 size_t just_written;
1320 bytes_read = read(0, buffer, SIZEOF(buffer));
1321 } while ((bytes_read < 0) && ((errno == EINTR) || (errno == EAGAIN)));
1323 if (bytes_read < 0) {
1324 error(_("index tee cannot read [%s]"), strerror(errno));
1328 if (bytes_read == 0)
1329 break; /* finished */
1331 /* write the stuff to the subprocess */
1334 just_written = full_write(fileno(pipe_fp), ptr, (size_t)bytes_read);
1335 if (just_written < (size_t)bytes_read) {
1337 * just as we waited for write() to complete.
1339 if (errno != EPIPE) {
1340 dbprintf(_("Index tee cannot write to index creator [%s]\n"),
1344 bytes_written += just_written;
1345 ptr += just_written;
1348 /* write the stuff to stdout, ensuring none lost when interrupt
1352 just_written = full_write(3, ptr, bytes_read);
1353 if (just_written < (size_t)bytes_read) {
1354 error(_("index tee cannot write [%s]"), strerror(errno));
1357 bytes_written += just_written;
1358 ptr += just_written;
1365 /* check the exit code of the pipe and moan if not 0 */
1366 if ((exitcode = pclose(pipe_fp)) != 0) {
1367 char *exitstr = str_exit_status("Index pipe", exitcode);
1368 dbprintf("%s\n", exitstr);
1371 dbprintf(_("Index created successfully\n"));
1378 extern backup_program_t dump_program, gnutar_program;
1380 backup_program_t *programs[] = {
1381 &dump_program, &gnutar_program, NULL