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.
33 #include "sendbackup.h"
35 #include "pipespawn.h"
36 #include "amfeatures.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 char *errorstr = NULL;
63 g_option_t *g_options = NULL;
67 backup_program_t *program = NULL;
69 static am_feature_t *our_features = NULL;
70 static char *our_feature_string = NULL;
71 static char *amandad_auth = NULL;
74 int main(int argc, char **argv);
75 char *optionstr(option_t *options);
76 char *childstr(pid_t pid);
77 int check_status(pid_t pid, amwait_t w);
79 pid_t pipefork(void (*func)(void), char *fname, int *stdinfd,
80 int stdoutfd, int stderrfd);
81 void parse_backup_messages(int mesgin);
82 static void process_dumpline(char *str);
83 static void save_fd(int *, int);
84 void backup_api_info_tapeheader(int mesgfd, char *prog, option_t *options);
86 double the_num(char *str, int pos);
93 static char *optstr = NULL;
97 char *record_opt = "";
100 char *exclude_file_opt;
101 char *exclude_list_opt;
105 if(options->compress == COMP_BEST)
106 compress_opt = stralloc("compress-best;");
107 else if(options->compress == COMP_FAST)
108 compress_opt = stralloc("compress-fast;");
109 else if(options->compress == COMP_SERVER_BEST)
110 compress_opt = stralloc("srvcomp-best;");
111 else if(options->compress == COMP_SERVER_FAST)
112 compress_opt = stralloc("srvcomp-fast;");
113 else if(options->compress == COMP_SERVER_CUST)
114 compress_opt = vstralloc("srvcomp-cust=", options->srvcompprog, ";", NULL);
115 else if(options->compress == COMP_CUST)
116 compress_opt = vstralloc("comp-cust=", options->clntcompprog, ";", NULL);
118 compress_opt = stralloc("");
120 if(options->encrypt == ENCRYPT_CUST) {
121 encrypt_opt = vstralloc("encrypt-cust=", options->clnt_encrypt, ";", NULL);
122 if (options->clnt_decrypt_opt)
123 decrypt_opt = vstralloc("client-decrypt-option=", options->clnt_decrypt_opt, ";", NULL);
125 decrypt_opt = stralloc("");
127 else if(options->encrypt == ENCRYPT_SERV_CUST) {
128 encrypt_opt = vstralloc("encrypt-serv-cust=", options->srv_encrypt, ";", NULL);
129 if(options->srv_decrypt_opt)
130 decrypt_opt = vstralloc("server-decrypt-option=", options->srv_decrypt_opt, ";", NULL);
132 decrypt_opt = stralloc("");
135 encrypt_opt = stralloc("");
136 decrypt_opt = stralloc("");
139 if(options->no_record) record_opt = "no-record;";
140 if(options->auth) auth_opt = vstralloc("auth=", options->auth, ";", NULL);
141 else auth_opt = stralloc("");
142 if(options->createindex) index_opt = "index;";
144 exclude_file_opt = stralloc("");
145 if(options->exclude_file) {
146 for(excl = options->exclude_file->first; excl != NULL; excl=excl->next){
147 exc = newvstralloc(exc, "exclude-file=", excl->name, ";", NULL);
148 strappend(exclude_file_opt, exc);
151 exclude_list_opt = stralloc("");
152 if(options->exclude_list) {
153 for(excl = options->exclude_list->first; excl != NULL; excl=excl->next){
154 exc = newvstralloc(exc, "exclude-list=", excl->name, ";", NULL);
155 strappend(exclude_list_opt, exc);
159 optstr = newvstralloc(optstr,
169 amfree(compress_opt);
173 amfree(exclude_file_opt);
174 amfree(exclude_list_opt);
187 char *prog, *dumpdate, *stroptions;
188 int program_is_backup_api;
191 char *amdevice = NULL;
192 char *qamdevice = NULL;
194 char *err_extra = NULL;
204 * Configure program for internationalization:
205 * 1) Only set the message locale for now.
206 * 2) Set textdomain for all amanda related programs to "amanda"
207 * We don't want to be forced to support dozens of message catalogs.
209 setlocale(LC_MESSAGES, "C");
210 textdomain("amanda");
212 safe_fd(DATA_FD_OFFSET, DATA_FD_COUNT*2);
216 set_pname("sendbackup");
218 /* Don't die when child closes pipe */
219 signal(SIGPIPE, SIG_IGN);
221 /* Don't die when interrupt received */
222 signal(SIGINT, SIG_IGN);
224 if(argc > 1 && strcmp(argv[1],"-t") == 0) {
232 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
233 dbopen(DBG_SUBDIR_CLIENT);
235 dbprintf(_("Version %s\n"), version());
237 if(argc > 2 && strcmp(argv[1], "amandad") == 0) {
238 amandad_auth = stralloc(argv[2]);
241 our_features = am_init_feature_set();
242 our_feature_string = am_feature_to_string(our_features);
244 config_init(CONFIG_INIT_CLIENT, NULL);
246 check_running_as(RUNNING_AS_CLIENT_LOGIN);
250 * In interactive (debug) mode, the backup data is sent to
251 * /dev/null and none of the network connections back to driver
252 * programs on the tape host are set up. The index service is
253 * run and goes to stdout.
255 g_fprintf(stderr, _("%s: running in interactive test mode\n"), get_pname());
265 program_is_backup_api=0;
267 for(; (line = agets(stdin)) != NULL; free(line)) {
271 g_fprintf(stderr, "%s> ", get_pname());
274 if(strncmp_const(line, "OPTIONS ") == 0) {
275 g_options = parse_g_options(line+8, 1);
276 if(!g_options->hostname) {
277 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
278 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
279 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
282 if (g_options->config) {
283 /* overlay this configuration on the existing (nameless) configuration */
284 config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
287 dbrename(config_name, DBG_SUBDIR_CLIENT);
293 err_extra = _("multiple requests");
297 dbprintf(_(" sendbackup req: <%s>\n"), line);
301 skip_whitespace(s, ch); /* find the program name */
303 err_extra = _("no program name");
304 goto err; /* no program name */
307 skip_non_whitespace(s, ch);
310 if(strcmp(prog,"BACKUP")==0) {
311 program_is_backup_api=1;
312 skip_whitespace(s, ch); /* find dumper name */
314 goto err; /* no program */
317 skip_non_whitespace(s, ch);
320 prog = stralloc(prog);
322 skip_whitespace(s, ch); /* find the disk name */
324 err_extra = _("no disk name");
325 goto err; /* no disk name */
332 skip_quoted_string(s, ch);
334 qdisk = stralloc(qdisk);
335 disk = unquote_string(qdisk);
337 skip_whitespace(s, ch); /* find the device or level */
339 err_extra = _("bad level");
343 if(!isdigit((int)s[-1])) {
348 skip_quoted_string(s, ch);
350 qamdevice = stralloc(qamdevice);
351 amdevice = unquote_string(qamdevice);
352 skip_whitespace(s, ch); /* find level number */
355 amdevice = stralloc(disk);
356 qamdevice = stralloc(qdisk);
358 /* find the level number */
359 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
360 err_extra = _("bad level");
361 goto err; /* bad level */
365 skip_whitespace(s, ch); /* find the dump date */
367 err_extra = _("no dumpdate");
368 goto err; /* no dumpdate */
372 skip_non_whitespace(s, ch);
374 dumpdate = stralloc(dumpdate);
376 skip_whitespace(s, ch); /* find the options keyword */
378 err_extra = _("no options");
379 goto err; /* no options */
381 if(strncmp_const_skip(s - 1, "OPTIONS ", s, ch) != 0) {
382 err_extra = _("no OPTIONS keyword");
383 goto err; /* no options */
385 skip_whitespace(s, ch); /* find the options string */
387 err_extra = _("bad options string");
388 goto err; /* no options */
391 stroptions = stralloc(s - 1);
394 if (g_options == NULL) {
395 printf(_("ERROR [Missing OPTIONS line in sendbackup input]\n"));
396 error(_("Missing OPTIONS line in sendbackup input\n"));
404 stroptions == NULL) {
405 err_extra = _("no valid sendbackup request");
409 dbprintf(_(" Parsed request as: program `%s'\n"), prog);
410 dbprintf(_(" disk `%s'\n"), qdisk);
411 dbprintf(_(" device `%s'\n"), qamdevice);
412 dbprintf(_(" level %d\n"), level);
413 dbprintf(_(" since %s\n"), dumpdate);
414 dbprintf(_(" options `%s'\n"), stroptions);
416 if(program_is_backup_api==1) {
417 /* check that the backup_api exist */
420 for(i = 0; programs[i]; i++) {
421 if (strcmp(programs[i]->name, prog) == 0) {
425 if (programs[i] == NULL) {
426 dbprintf(_("ERROR [%s: unknown program %s]\n"), get_pname(), prog);
427 error(_("ERROR [%s: unknown program %s]"), get_pname(), prog);
430 program = programs[i];
433 options = parse_options(stroptions, disk, amdevice, g_options->features, 0);
436 datafd = DATA_FD_OFFSET + 0;
437 mesgfd = DATA_FD_OFFSET + 2;
438 indexfd = DATA_FD_OFFSET + 4;
440 if (!options->createindex)
443 if(options->auth && amandad_auth) {
444 if(strcasecmp(options->auth, amandad_auth) != 0) {
445 g_printf(_("ERROR [client configured for auth=%s while server requested '%s']\n"),
446 amandad_auth, options->auth);
451 if (options->kencrypt) {
452 g_printf("KENCRYPT\n");
455 g_printf(_("CONNECT DATA %d MESG %d INDEX %d\n"),
456 DATA_FD_OFFSET, DATA_FD_OFFSET+1,
457 indexfd == -1 ? -1 : DATA_FD_OFFSET+2);
458 g_printf(_("OPTIONS "));
459 if(am_has_feature(g_options->features, fe_rep_options_features)) {
460 g_printf("features=%s;", our_feature_string);
462 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
463 g_printf("hostname=%s;", g_options->hostname);
465 if(am_has_feature(g_options->features, fe_rep_options_sendbackup_options)) {
466 g_printf("%s", optionstr(options));
470 if (freopen("/dev/null", "w", stdout) == NULL) {
471 dbprintf(_("Error redirecting stdout to /dev/null: %s\n"),
477 if((datafd = open("/dev/null", O_RDWR)) < 0) {
478 error(_("ERROR [open of /dev/null for debug data stream: %s]\n"),
487 if(datafd == -1 || mesgfd == -1 || (options->createindex && indexfd == -1)) {
493 if(program_is_backup_api==1) {
494 pid_t backup_api_pid;
499 int property_pipe[2];
500 backup_support_option_t *bsu;
502 if (pipe(property_pipe) < 0) {
503 error(_("Can't create pipe: %s"),strerror(errno));
506 bsu = backup_support_option(prog, g_options, disk, amdevice);
508 switch(backup_api_pid=fork()) {
510 aclose(property_pipe[1]);
511 if(dup2(property_pipe[0], 0) == -1) {
512 error(_("Can't dup2: %s"),strerror(errno));
515 if(dup2(datafd, 1) == -1) {
516 error(_("Can't dup2: %s"),strerror(errno));
519 if(dup2(mesgfd, 2) == -1) {
520 error(_("Can't dup2: %s"),strerror(errno));
524 if(dup2(indexfd, 3) == -1) {
525 error(_("Can't dup2: %s"),strerror(errno));
528 fcntl(indexfd, F_SETFD, 0);
529 fcntl(3, F_SETFD, 0);
534 cmd = vstralloc(DUMPER_DIR, "/", prog, NULL);
536 argvchild[i++] = prog;
537 argvchild[i++] = "backup";
538 if (bsu->message_line == 1) {
539 argvchild[i++] = "--message";
540 argvchild[i++] = "line";
542 if (g_options->config && bsu->config == 1) {
543 argvchild[i++] = "--config";
544 argvchild[i++] = g_options->config;
546 if (g_options->hostname && bsu->host == 1) {
547 argvchild[i++] = "--host";
548 argvchild[i++] = g_options->hostname;
550 if (disk && bsu->disk == 1) {
551 argvchild[i++] = "--disk";
552 argvchild[i++] = disk;
554 argvchild[i++] = "--device";
555 argvchild[i++] = amdevice;
556 if (level <= bsu->max_level) {
557 argvchild[i++] = "--level";
558 g_snprintf(levelstr,19,"%d",level);
559 argvchild[i++] = levelstr;
561 if (indexfd != 0 && bsu->index_line == 1) {
562 argvchild[i++] = "--index";
563 argvchild[i++] = "line";
565 if (!options->no_record && bsu->record == 1) {
566 argvchild[i++] = "--record";
569 dbprintf(_("%s: running \"%s"), get_pname(), cmd);
570 for(j=1;j<i;j++) dbprintf(" %s",argvchild[j]);
572 backup_api_info_tapeheader(mesgfd, prog, options);
573 execve(cmd, argvchild, safe_env());
578 aclose(property_pipe[0]);
579 toolin = fdopen(property_pipe[1],"w");
581 error(_("Can't fdopen: %s"), strerror(errno));
584 output_tool_property(toolin, options);
589 error(_("%s: fork returned: %s"), get_pname(), strerror(errno));
592 if (waitpid(backup_api_pid, &status, 0) < 0) {
593 if (!WIFEXITED(status)) {
594 dbprintf(_("Tool exited with signal %d"), WTERMSIG(status));
595 } else if (WEXITSTATUS(status) != 0) {
596 dbprintf(_("Tool exited with status %d"), WEXITSTATUS(status));
598 dbprintf(_("waitpid returned negative value"));
604 /* redirect stderr */
605 if(dup2(mesgfd, 2) == -1) {
606 dbprintf(_("Error redirecting stderr to fd %d: %s\n"),
607 mesgfd, strerror(errno));
613 if(pipe(mesgpipe) == -1) {
615 dbprintf(_("error [opening mesg pipe: %s]\n"), s);
616 error(_("error [opening mesg pipe: %s]"), s);
619 program->start_backup(g_options->hostname, disk, amdevice, level,
620 dumpdate, datafd, mesgpipe[1], indexfd);
621 dbprintf(_("Started backup\n"));
622 parse_backup_messages(mesgpipe[0]);
623 dbprintf(_("Parsed backup messages\n"));
633 amfree(our_feature_string);
634 am_release_feature_set(our_features);
636 free_g_options(g_options);
643 g_printf(_("FORMAT ERROR IN REQUEST PACKET\n"));
644 dbprintf(_("REQ packet is bogus%s%s\n"),
645 err_extra ? ": " : "",
646 err_extra ? err_extra : "");
653 * Returns a string for a child process. Checks the saved dump and
654 * compress pids to see which it is.
661 if(pid == dumppid) return program->backup_name;
662 if(pid == comppid) return "compress";
663 if(pid == encpid) return "encrypt";
664 if(pid == indexpid) return "index";
670 * Determine if the child return status really indicates an error.
671 * If so, add the error message to the error string; more than one
672 * child can have an error.
680 char *thiserr = NULL;
688 rc = sig = WTERMSIG(w);
691 rc = ret = WEXITSTATUS(w);
694 if(pid == indexpid) {
696 * Treat an index failure (other than signal) as a "STRANGE"
697 * rather than an error so the dump goes ahead and gets processed
698 * but the failure is noted.
701 g_fprintf(stderr, _("? index %s returned %d\n"), str, ret);
706 } else if(pid == comppid) {
708 * compress returns 2 sometimes, but it is ok.
717 } else if(pid == dumppid && tarpid == -1) {
719 * Ultrix dump returns 1 sometimes, but it is ok.
721 #ifdef DUMP_RETURNS_1
728 } else if(pid == tarpid) {
733 * tar bitches about active filesystems, but we do not care.
735 #ifdef IGNORE_TAR_ERRORS
740 dumppid = tarpid = -1;
747 return 0; /* normal exit */
751 thiserr = vstrallocf(_("%s (%d) %s got signal %d"), strX, (int)pid, str,
754 thiserr = vstrallocf(_("%s (%d) %s returned %d"), strX, (int)pid, str, ret);
758 errorstr = newvstrallocf(errorstr, "%s, %s", errorstr, thiserr);
769 *Send header info to the message file.
772 info_tapeheader(void)
774 g_fprintf(stderr, "%s: info BACKUP=%s\n", get_pname(), program->backup_name);
776 g_fprintf(stderr, "%s: info RECOVER_CMD=", get_pname());
777 if (options->compress == COMP_FAST || options->compress == COMP_BEST)
778 g_fprintf(stderr, "%s %s |", UNCOMPRESS_PATH,
779 #ifdef UNCOMPRESS_OPT
786 g_fprintf(stderr, "%s -xpGf - ...\n", program->restore_name);
788 if (options->compress == COMP_FAST || options->compress == COMP_BEST)
789 g_fprintf(stderr, "%s: info COMPRESS_SUFFIX=%s\n",
790 get_pname(), COMPRESS_SUFFIX);
792 g_fprintf(stderr, "%s: info end\n", get_pname());
796 backup_api_info_tapeheader(
803 g_snprintf(line, 1024, "%s: info BACKUP=DUMPER\n", get_pname());
804 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
805 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
809 g_snprintf(line, 1024, "%s: info DUMPER=%s\n", get_pname(), prog);
810 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
811 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
815 g_snprintf(line, 1024, "%s: info RECOVER_CMD=", get_pname());
816 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
817 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
821 if (options->compress) {
822 g_snprintf(line, 1024, "%s %s |", UNCOMPRESS_PATH,
823 #ifdef UNCOMPRESS_OPT
829 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
830 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
834 g_snprintf(line, 1024, "%s -f... -\n", prog);
835 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
836 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
840 if (options->compress) {
841 g_snprintf(line, 1024, "%s: info COMPRESS_SUFFIX=%s\n",
842 get_pname(), COMPRESS_SUFFIX);
843 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
844 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
849 g_snprintf(line, 1024, "%s: info end\n", get_pname());
850 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
851 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
867 dbprintf(_("Forking function %s in pipeline\n"), fname);
869 if(pipe(inpipe) == -1) {
870 error(_("error [open pipe to %s: %s]"), fname, strerror(errno));
874 switch(pid = fork()) {
876 error(_("error [fork %s: %s]"), fname, strerror(errno));
878 default: /* parent process */
879 aclose(inpipe[0]); /* close input side of pipe */
880 *stdinfd = inpipe[1];
882 case 0: /* child process */
883 aclose(inpipe[1]); /* close output side of pipe */
885 if(dup2(inpipe[0], 0) == -1) {
886 error(_("error [fork %s: dup2(%d, in): %s]"),
887 fname, inpipe[0], strerror(errno));
890 if(dup2(stdoutfd, 1) == -1) {
891 error(_("error [fork %s: dup2(%d, out): %s]"),
892 fname, stdoutfd, strerror(errno));
895 if(dup2(stderrfd, 2) == -1) {
896 error(_("error [fork %s: dup2(%d, err): %s]"),
897 fname, stderrfd, strerror(errno));
909 parse_backup_messages(
920 for(; (line = areads(mesgin)) != NULL; free(line)) {
921 process_dumpline(line);
925 error(_("error [read mesg pipe: %s]"), strerror(errno));
929 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
930 if(check_status(wpid, retstat)) goterror = 1;
935 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
936 if(check_status(wpid, retstat)) goterror = 1;
940 dbprintf(_("Sending SIGHUP to dump process %d\n"),
943 if(kill(dumppid, SIGHUP) == -1) {
944 dbprintf(_("Can't send SIGHUP to %d: %s\n"),
950 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
951 if(check_status(wpid, retstat)) goterror = 1;
955 dbprintf(_("Sending SIGKILL to dump process %d\n"),
958 if(kill(dumppid, SIGKILL) == -1) {
959 dbprintf(_("Can't send SIGKILL to %d: %s\n"),
965 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
966 if(check_status(wpid, retstat)) goterror = 1;
971 error(_("error [%s]"), errorstr);
973 } else if(dump_size == -1) {
974 error(_("error [no backup size line]"));
978 program->end_backup(goterror);
980 g_fprintf(stderr, _("%s: size %ld\n"), get_pname(), dump_size);
981 g_fprintf(stderr, _("%s: end\n"), get_pname());
986 * Returns the value of the first integer in a string.
1000 while(ch && !isdigit(ch)) ch = *str++;
1001 if (pos == 1) break;
1003 while(ch && (isdigit(ch) || ch == '.')) ch = *str++;
1006 while(isdigit(ch) || ch == '.') ch = *str++;
1022 for(rp = program->re_table; rp->regex != NULL; rp++) {
1023 if(match(rp->regex, str)) {
1027 if(rp->typ == DMP_SIZE) {
1028 dump_size = (long)((the_num(str, rp->field)* rp->scale+1023.0)/1024.0);
1049 * Should never get here.
1055 dbprintf("%3d: %7s(%c): %s\n",
1060 g_fprintf(stderr, "%c %s\n", startchr, str);
1065 * start_index. Creates an index file from the output of dump/tar.
1066 * It arranges that input is the fd to be written by the dump process.
1067 * If createindex is not enabled, it does nothing. If it is not, a
1068 * new process will be created that tees input both to a pipe whose
1069 * read fd is dup2'ed input and to a program that outputs an index
1072 * make sure that the chat from restore doesn't go to stderr cause
1073 * this goes back to amanda which doesn't expect to see it
1074 * (2>/dev/null should do it)
1076 * Originally by Alan M. McIvor, 13 April 1996
1078 * Adapted by Alexandre Oliva, 1 May 1997
1080 * This program owes a lot to tee.c from GNU sh-utils and dumptee.c
1081 * from the DeeJay backup package.
1091 while (*fd >= 0 && *fd < min) {
1092 int newfd = dup(*fd);
1094 dbprintf(_("Unable to save file descriptor [%s]\n"), strerror(errno));
1098 dbprintf(_("Dupped file descriptor %i to %i\n"), origfd, *fd);
1116 if (pipe(pipefd) != 0) {
1117 error(_("creating index pipe: %s"), strerror(errno));
1121 switch(indexpid = fork()) {
1123 error(_("forking index tee process: %s"), strerror(errno));
1128 if (dup2(pipefd[1], input) == -1) {
1129 error(_("dup'ping index tee output: %s"), strerror(errno));
1139 /* now in a child process */
1140 save_fd(&pipefd[0], 4);
1148 for(index = 4; index < (int)FD_SETSIZE; index++) {
1149 if (index != dbfd()) {
1154 if ((pipe_fp = popen(cmd, "w")) == NULL) {
1155 error(_("couldn't start index creator [%s]"), strerror(errno));
1159 dbprintf(_("Started index creator: \"%s\"\n"), cmd);
1161 char buffer[BUFSIZ], *ptr;
1163 size_t bytes_written;
1164 ssize_t just_written;
1167 bytes_read = read(0, buffer, SIZEOF(buffer));
1168 } while ((bytes_read < 0) && ((errno == EINTR) || (errno == EAGAIN)));
1170 if (bytes_read < 0) {
1171 error(_("index tee cannot read [%s]"), strerror(errno));
1175 if (bytes_read == 0)
1176 break; /* finished */
1178 /* write the stuff to the subprocess */
1181 just_written = fullwrite(fileno(pipe_fp), ptr, (size_t)bytes_read);
1182 if (just_written < 0) {
1184 * just as we waited for write() to complete.
1186 if (errno != EPIPE) {
1187 dbprintf(_("Index tee cannot write to index creator [%s]\n"),
1191 bytes_written += just_written;
1192 ptr += just_written;
1195 /* write the stuff to stdout, ensuring none lost when interrupt
1199 just_written = fullwrite(3, ptr, (size_t)bytes_read);
1200 if (just_written < 0) {
1201 error(_("index tee cannot write [%s]"), strerror(errno));
1204 bytes_written += just_written;
1205 ptr += just_written;
1212 /* check the exit code of the pipe and moan if not 0 */
1213 if ((exitcode = pclose(pipe_fp)) != 0) {
1214 char *exitstr = str_exit_status("Index pipe", exitcode);
1215 dbprintf("%s\n", exitstr);
1218 dbprintf(_("Index created successfully\n"));
1225 extern backup_program_t dump_program, gnutar_program;
1227 backup_program_t *programs[] = {
1228 &dump_program, &gnutar_program, NULL