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;
205 * Configure program for internationalization:
206 * 1) Only set the message locale for now.
207 * 2) Set textdomain for all amanda related programs to "amanda"
208 * We don't want to be forced to support dozens of message catalogs.
210 setlocale(LC_MESSAGES, "C");
211 textdomain("amanda");
213 safe_fd(DATA_FD_OFFSET, DATA_FD_COUNT*2);
217 set_pname("sendbackup");
219 /* Don't die when child closes pipe */
220 signal(SIGPIPE, SIG_IGN);
222 /* Don't die when interrupt received */
223 signal(SIGINT, SIG_IGN);
225 if(argc > 1 && strcmp(argv[1],"-t") == 0) {
233 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
234 dbopen(DBG_SUBDIR_CLIENT);
236 dbprintf(_("Version %s\n"), version());
238 if(argc > 2 && strcmp(argv[1], "amandad") == 0) {
239 amandad_auth = stralloc(argv[2]);
242 our_features = am_init_feature_set();
243 our_feature_string = am_feature_to_string(our_features);
245 config_init(CONFIG_INIT_CLIENT, NULL);
247 check_running_as(RUNNING_AS_CLIENT_LOGIN);
251 * In interactive (debug) mode, the backup data is sent to
252 * /dev/null and none of the network connections back to driver
253 * programs on the tape host are set up. The index service is
254 * run and goes to stdout.
256 g_fprintf(stderr, _("%s: running in interactive test mode\n"), get_pname());
266 program_is_backup_api=0;
268 for(; (line = agets(stdin)) != NULL; free(line)) {
272 g_fprintf(stderr, "%s> ", get_pname());
275 if(strncmp_const(line, "OPTIONS ") == 0) {
276 g_options = parse_g_options(line+8, 1);
277 if(!g_options->hostname) {
278 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
279 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
280 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
283 if (g_options->config) {
284 /* overlay this configuration on the existing (nameless) configuration */
285 config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
288 dbrename(config_name, DBG_SUBDIR_CLIENT);
294 err_extra = _("multiple requests");
298 dbprintf(_(" sendbackup req: <%s>\n"), line);
302 skip_whitespace(s, ch); /* find the program name */
304 err_extra = _("no program name");
305 goto err; /* no program name */
308 skip_non_whitespace(s, ch);
311 if(strcmp(prog,"BACKUP")==0) {
312 program_is_backup_api=1;
313 skip_whitespace(s, ch); /* find dumper name */
315 goto err; /* no program */
318 skip_non_whitespace(s, ch);
321 prog = stralloc(prog);
323 skip_whitespace(s, ch); /* find the disk name */
325 err_extra = _("no disk name");
326 goto err; /* no disk name */
333 skip_quoted_string(s, ch);
335 qdisk = stralloc(qdisk);
336 disk = unquote_string(qdisk);
338 skip_whitespace(s, ch); /* find the device or level */
340 err_extra = _("bad level");
344 if(!isdigit((int)s[-1])) {
349 skip_quoted_string(s, ch);
351 qamdevice = stralloc(qamdevice);
352 amdevice = unquote_string(qamdevice);
353 skip_whitespace(s, ch); /* find level number */
356 amdevice = stralloc(disk);
357 qamdevice = stralloc(qdisk);
359 /* find the level number */
360 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
361 err_extra = _("bad level");
362 goto err; /* bad level */
366 skip_whitespace(s, ch); /* find the dump date */
368 err_extra = _("no dumpdate");
369 goto err; /* no dumpdate */
373 skip_non_whitespace(s, ch);
375 dumpdate = stralloc(dumpdate);
377 skip_whitespace(s, ch); /* find the options keyword */
379 err_extra = _("no options");
380 goto err; /* no options */
382 if(strncmp_const_skip(s - 1, "OPTIONS ", s, ch) != 0) {
383 err_extra = _("no OPTIONS keyword");
384 goto err; /* no options */
386 skip_whitespace(s, ch); /* find the options string */
388 err_extra = _("bad options string");
389 goto err; /* no options */
392 stroptions = stralloc(s - 1);
395 if (g_options == NULL) {
396 printf(_("ERROR [Missing OPTIONS line in sendbackup input]\n"));
397 error(_("Missing OPTIONS line in sendbackup input\n"));
405 stroptions == NULL) {
406 err_extra = _("no valid sendbackup request");
410 dbprintf(_(" Parsed request as: program `%s'\n"), prog);
411 dbprintf(_(" disk `%s'\n"), qdisk);
412 dbprintf(_(" device `%s'\n"), qamdevice);
413 dbprintf(_(" level %d\n"), level);
414 dbprintf(_(" since %s\n"), dumpdate);
415 dbprintf(_(" options `%s'\n"), stroptions);
417 if(program_is_backup_api==1) {
418 /* check that the backup_api exist */
421 for(i = 0; programs[i]; i++) {
422 if (strcmp(programs[i]->name, prog) == 0) {
426 if (programs[i] == NULL) {
427 dbprintf(_("ERROR [%s: unknown program %s]\n"), get_pname(), prog);
428 error(_("ERROR [%s: unknown program %s]"), get_pname(), prog);
431 program = programs[i];
434 options = parse_options(stroptions, disk, amdevice, g_options->features, 0);
437 datafd = DATA_FD_OFFSET + 0;
438 mesgfd = DATA_FD_OFFSET + 2;
439 indexfd = DATA_FD_OFFSET + 4;
441 if (!options->createindex)
444 if(options->auth && amandad_auth) {
445 if(strcasecmp(options->auth, amandad_auth) != 0) {
446 g_printf(_("ERROR [client configured for auth=%s while server requested '%s']\n"),
447 amandad_auth, options->auth);
452 if (options->kencrypt) {
453 g_printf("KENCRYPT\n");
456 g_printf(_("CONNECT DATA %d MESG %d INDEX %d\n"),
457 DATA_FD_OFFSET, DATA_FD_OFFSET+1,
458 indexfd == -1 ? -1 : DATA_FD_OFFSET+2);
459 g_printf(_("OPTIONS "));
460 if(am_has_feature(g_options->features, fe_rep_options_features)) {
461 g_printf("features=%s;", our_feature_string);
463 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
464 g_printf("hostname=%s;", g_options->hostname);
466 if(am_has_feature(g_options->features, fe_rep_options_sendbackup_options)) {
467 g_printf("%s", optionstr(options));
471 if (freopen("/dev/null", "w", stdout) == NULL) {
472 dbprintf(_("Error redirecting stdout to /dev/null: %s\n"),
478 if((datafd = open("/dev/null", O_RDWR)) < 0) {
479 error(_("ERROR [open of /dev/null for debug data stream: %s]\n"),
488 if(datafd == -1 || mesgfd == -1 || (options->createindex && indexfd == -1)) {
494 if(program_is_backup_api==1) {
495 pid_t backup_api_pid;
500 int property_pipe[2];
501 backup_support_option_t *bsu;
503 if (pipe(property_pipe) < 0) {
504 error(_("Can't create pipe: %s"),strerror(errno));
507 bsu = backup_support_option(prog, g_options, disk, amdevice);
509 switch(backup_api_pid=fork()) {
511 aclose(property_pipe[1]);
512 if(dup2(property_pipe[0], 0) == -1) {
513 error(_("Can't dup2: %s"),strerror(errno));
516 if(dup2(datafd, 1) == -1) {
517 error(_("Can't dup2: %s"),strerror(errno));
520 if(dup2(mesgfd, 2) == -1) {
521 error(_("Can't dup2: %s"),strerror(errno));
525 if(dup2(indexfd, 3) == -1) {
526 error(_("Can't dup2: %s"),strerror(errno));
529 fcntl(indexfd, F_SETFD, 0);
530 fcntl(3, F_SETFD, 0);
535 cmd = vstralloc(DUMPER_DIR, "/", prog, NULL);
537 argvchild[i++] = prog;
538 argvchild[i++] = "backup";
539 if (bsu->message_line == 1) {
540 argvchild[i++] = "--message";
541 argvchild[i++] = "line";
543 if (g_options->config && bsu->config == 1) {
544 argvchild[i++] = "--config";
545 argvchild[i++] = g_options->config;
547 if (g_options->hostname && bsu->host == 1) {
548 argvchild[i++] = "--host";
549 argvchild[i++] = g_options->hostname;
551 if (disk && bsu->disk == 1) {
552 argvchild[i++] = "--disk";
553 argvchild[i++] = disk;
555 argvchild[i++] = "--device";
556 argvchild[i++] = amdevice;
557 if (level <= bsu->max_level) {
558 argvchild[i++] = "--level";
559 g_snprintf(levelstr,19,"%d",level);
560 argvchild[i++] = levelstr;
562 if (indexfd != 0 && bsu->index_line == 1) {
563 argvchild[i++] = "--index";
564 argvchild[i++] = "line";
566 if (!options->no_record && bsu->record == 1) {
567 argvchild[i++] = "--record";
570 dbprintf(_("%s: running \"%s"), get_pname(), cmd);
571 for(j=1;j<i;j++) dbprintf(" %s",argvchild[j]);
573 backup_api_info_tapeheader(mesgfd, prog, options);
574 execve(cmd, argvchild, safe_env());
579 aclose(property_pipe[0]);
580 toolin = fdopen(property_pipe[1],"w");
582 error(_("Can't fdopen: %s"), strerror(errno));
585 output_tool_property(toolin, options);
590 error(_("%s: fork returned: %s"), get_pname(), strerror(errno));
593 if (waitpid(backup_api_pid, &status, 0) < 0) {
594 if (!WIFEXITED(status)) {
595 dbprintf(_("Tool exited with signal %d"), WTERMSIG(status));
596 } else if (WEXITSTATUS(status) != 0) {
597 dbprintf(_("Tool exited with status %d"), WEXITSTATUS(status));
599 dbprintf(_("waitpid returned negative value"));
605 /* redirect stderr */
606 if(dup2(mesgfd, 2) == -1) {
607 dbprintf(_("Error redirecting stderr to fd %d: %s\n"),
608 mesgfd, strerror(errno));
614 if(pipe(mesgpipe) == -1) {
616 dbprintf(_("error [opening mesg pipe: %s]\n"), s);
617 error(_("error [opening mesg pipe: %s]"), s);
620 program->start_backup(g_options->hostname, disk, amdevice, level,
621 dumpdate, datafd, mesgpipe[1], indexfd);
622 dbprintf(_("Started backup\n"));
623 parse_backup_messages(mesgpipe[0]);
624 dbprintf(_("Parsed backup messages\n"));
634 amfree(our_feature_string);
635 am_release_feature_set(our_features);
637 free_g_options(g_options);
644 g_printf(_("FORMAT ERROR IN REQUEST PACKET\n"));
645 dbprintf(_("REQ packet is bogus%s%s\n"),
646 err_extra ? ": " : "",
647 err_extra ? err_extra : "");
654 * Returns a string for a child process. Checks the saved dump and
655 * compress pids to see which it is.
662 if(pid == dumppid) return program->backup_name;
663 if(pid == comppid) return "compress";
664 if(pid == encpid) return "encrypt";
665 if(pid == indexpid) return "index";
671 * Determine if the child return status really indicates an error.
672 * If so, add the error message to the error string; more than one
673 * child can have an error.
681 char *thiserr = NULL;
689 rc = sig = WTERMSIG(w);
692 rc = ret = WEXITSTATUS(w);
695 if(pid == indexpid) {
697 * Treat an index failure (other than signal) as a "STRANGE"
698 * rather than an error so the dump goes ahead and gets processed
699 * but the failure is noted.
702 g_fprintf(stderr, _("? index %s returned %d\n"), str, ret);
707 } else if(pid == comppid) {
709 * compress returns 2 sometimes, but it is ok.
718 } else if(pid == dumppid && tarpid == -1) {
720 * Ultrix dump returns 1 sometimes, but it is ok.
722 #ifdef DUMP_RETURNS_1
729 } else if(pid == tarpid) {
734 * tar bitches about active filesystems, but we do not care.
736 #ifdef IGNORE_TAR_ERRORS
741 dumppid = tarpid = -1;
748 return 0; /* normal exit */
751 snprintf(numberpid, SIZEOF(number), "%d", (int)pid);
753 thiserr = vstrallocf(_("%s (%d) %s got signal %d"), strX, (int)pid, str,
756 thiserr = vstrallocf(_("%s (%d) %s returned %d"), strX, (int)pid, str, ret);
760 errorstr = newvstrallocf(errorstr, "%s, %s", errorstr, thiserr);
771 *Send header info to the message file.
774 info_tapeheader(void)
776 g_fprintf(stderr, "%s: info BACKUP=%s\n", get_pname(), program->backup_name);
778 g_fprintf(stderr, "%s: info RECOVER_CMD=", get_pname());
779 if (options->compress == COMP_FAST || options->compress == COMP_BEST)
780 g_fprintf(stderr, "%s %s |", UNCOMPRESS_PATH,
781 #ifdef UNCOMPRESS_OPT
788 g_fprintf(stderr, "%s -xpGf - ...\n", program->restore_name);
790 if (options->compress == COMP_FAST || options->compress == COMP_BEST)
791 g_fprintf(stderr, "%s: info COMPRESS_SUFFIX=%s\n",
792 get_pname(), COMPRESS_SUFFIX);
794 g_fprintf(stderr, "%s: info end\n", get_pname());
798 backup_api_info_tapeheader(
805 g_snprintf(line, 1024, "%s: info BACKUP=DUMPER\n", get_pname());
806 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
807 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
811 g_snprintf(line, 1024, "%s: info DUMPER=%s\n", get_pname(), prog);
812 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
813 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
817 g_snprintf(line, 1024, "%s: info RECOVER_CMD=", get_pname());
818 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
819 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
823 if (options->compress) {
824 g_snprintf(line, 1024, "%s %s |", UNCOMPRESS_PATH,
825 #ifdef UNCOMPRESS_OPT
831 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
832 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
836 g_snprintf(line, 1024, "%s -f... -\n", prog);
837 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
838 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
842 if (options->compress) {
843 g_snprintf(line, 1024, "%s: info COMPRESS_SUFFIX=%s\n",
844 get_pname(), COMPRESS_SUFFIX);
845 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
846 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
851 g_snprintf(line, 1024, "%s: info end\n", get_pname());
852 if (fullwrite(mesgfd, line, strlen(line)) != (ssize_t)strlen(line)) {
853 dbprintf(_("error writing to mesgfd socket: %s"), strerror(errno));
869 dbprintf(_("Forking function %s in pipeline\n"), fname);
871 if(pipe(inpipe) == -1) {
872 error(_("error [open pipe to %s: %s]"), fname, strerror(errno));
876 switch(pid = fork()) {
878 error(_("error [fork %s: %s]"), fname, strerror(errno));
880 default: /* parent process */
881 aclose(inpipe[0]); /* close input side of pipe */
882 *stdinfd = inpipe[1];
884 case 0: /* child process */
885 aclose(inpipe[1]); /* close output side of pipe */
887 if(dup2(inpipe[0], 0) == -1) {
888 error(_("error [fork %s: dup2(%d, in): %s]"),
889 fname, inpipe[0], strerror(errno));
892 if(dup2(stdoutfd, 1) == -1) {
893 error(_("error [fork %s: dup2(%d, out): %s]"),
894 fname, stdoutfd, strerror(errno));
897 if(dup2(stderrfd, 2) == -1) {
898 error(_("error [fork %s: dup2(%d, err): %s]"),
899 fname, stderrfd, strerror(errno));
911 parse_backup_messages(
922 for(; (line = areads(mesgin)) != NULL; free(line)) {
923 process_dumpline(line);
927 error(_("error [read mesg pipe: %s]"), strerror(errno));
931 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
932 if(check_status(wpid, retstat)) goterror = 1;
937 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
938 if(check_status(wpid, retstat)) goterror = 1;
942 dbprintf(_("Sending SIGHUP to dump process %d\n"),
945 if(kill(dumppid, SIGHUP) == -1) {
946 dbprintf(_("Can't send SIGHUP to %d: %s\n"),
952 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
953 if(check_status(wpid, retstat)) goterror = 1;
957 dbprintf(_("Sending SIGKILL to dump process %d\n"),
960 if(kill(dumppid, SIGKILL) == -1) {
961 dbprintf(_("Can't send SIGKILL to %d: %s\n"),
967 while((wpid = waitpid((pid_t)-1, &retstat, WNOHANG)) > 0) {
968 if(check_status(wpid, retstat)) goterror = 1;
973 error(_("error [%s]"), errorstr);
975 } else if(dump_size == -1) {
976 error(_("error [no backup size line]"));
980 program->end_backup(goterror);
982 g_fprintf(stderr, _("%s: size %ld\n"), get_pname(), dump_size);
983 g_fprintf(stderr, _("%s: end\n"), get_pname());
988 * Returns the value of the first integer in a string.
1002 while(ch && !isdigit(ch)) ch = *str++;
1003 if (pos == 1) break;
1005 while(ch && (isdigit(ch) || ch == '.')) ch = *str++;
1008 while(isdigit(ch) || ch == '.') ch = *str++;
1024 for(rp = program->re_table; rp->regex != NULL; rp++) {
1025 if(match(rp->regex, str)) {
1029 if(rp->typ == DMP_SIZE) {
1030 dump_size = (long)((the_num(str, rp->field)* rp->scale+1023.0)/1024.0);
1051 * Should never get here.
1057 dbprintf("%3d: %7s(%c): %s\n",
1062 g_fprintf(stderr, "%c %s\n", startchr, str);
1067 * start_index. Creates an index file from the output of dump/tar.
1068 * It arranges that input is the fd to be written by the dump process.
1069 * If createindex is not enabled, it does nothing. If it is not, a
1070 * new process will be created that tees input both to a pipe whose
1071 * read fd is dup2'ed input and to a program that outputs an index
1074 * make sure that the chat from restore doesn't go to stderr cause
1075 * this goes back to amanda which doesn't expect to see it
1076 * (2>/dev/null should do it)
1078 * Originally by Alan M. McIvor, 13 April 1996
1080 * Adapted by Alexandre Oliva, 1 May 1997
1082 * This program owes a lot to tee.c from GNU sh-utils and dumptee.c
1083 * from the DeeJay backup package.
1093 while (*fd >= 0 && *fd < min) {
1094 int newfd = dup(*fd);
1096 dbprintf(_("Unable to save file descriptor [%s]\n"), strerror(errno));
1100 dbprintf(_("Dupped file descriptor %i to %i\n"), origfd, *fd);
1118 if (pipe(pipefd) != 0) {
1119 error(_("creating index pipe: %s"), strerror(errno));
1123 switch(indexpid = fork()) {
1125 error(_("forking index tee process: %s"), strerror(errno));
1130 if (dup2(pipefd[1], input) == -1) {
1131 error(_("dup'ping index tee output: %s"), strerror(errno));
1141 /* now in a child process */
1142 save_fd(&pipefd[0], 4);
1150 for(index = 4; index < (int)FD_SETSIZE; index++) {
1151 if (index != dbfd()) {
1156 if ((pipe_fp = popen(cmd, "w")) == NULL) {
1157 error(_("couldn't start index creator [%s]"), strerror(errno));
1161 dbprintf(_("Started index creator: \"%s\"\n"), cmd);
1163 char buffer[BUFSIZ], *ptr;
1165 size_t bytes_written;
1166 ssize_t just_written;
1169 bytes_read = read(0, buffer, SIZEOF(buffer));
1170 } while ((bytes_read < 0) && ((errno == EINTR) || (errno == EAGAIN)));
1172 if (bytes_read < 0) {
1173 error(_("index tee cannot read [%s]"), strerror(errno));
1177 if (bytes_read == 0)
1178 break; /* finished */
1180 /* write the stuff to the subprocess */
1183 just_written = fullwrite(fileno(pipe_fp), ptr, (size_t)bytes_read);
1184 if (just_written < 0) {
1186 * just as we waited for write() to complete.
1188 if (errno != EPIPE) {
1189 dbprintf(_("Index tee cannot write to index creator [%s]\n"),
1193 bytes_written += just_written;
1194 ptr += just_written;
1197 /* write the stuff to stdout, ensuring none lost when interrupt
1201 just_written = fullwrite(3, ptr, (size_t)bytes_read);
1202 if (just_written < 0) {
1203 error(_("index tee cannot write [%s]"), strerror(errno));
1206 bytes_written += just_written;
1207 ptr += just_written;
1214 /* check the exit code of the pipe and moan if not 0 */
1215 if ((exitcode = pclose(pipe_fp)) != 0) {
1216 char *exitstr = str_exit_status("Index pipe", exitcode);
1217 dbprintf("%s\n", exitstr);
1220 dbprintf(_("Index created successfully\n"));
1227 extern backup_program_t dump_program, gnutar_program;
1229 backup_program_t *programs[] = {
1230 &dump_program, &gnutar_program, NULL