]> git.wh0rd.org - dump.git/blobdiff - dump/tape.c
Fix a bug in dump where waiting too much when changing tapes causes the volume to...
[dump.git] / dump / tape.c
index f4133942dd332a771d667c211c7c6c6a46bdea54..2db739cf91b19e5e0df7ae845e62618358cf8e5a 100644 (file)
@@ -2,7 +2,8 @@
  *     Ported to Linux's Second Extended File System as part of the
  *     dump and restore backup suit
  *     Remy Card <card@Linux.EU.Org>, 1994-1997
- *     Stelian Pop <pop@cybercable.fr>, 1999-2000
+ *     Stelian Pop <pop@noos.fr>, 1999-2000
+ *     Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
  */
 
 /*-
 
 #ifndef lint
 static const char rcsid[] =
-       "$Id: tape.c,v 1.13 2000/02/26 01:35:48 stelian Exp $";
+       "$Id: tape.c,v 1.30 2001/02/16 13:38:47 stelian Exp $";
 #endif /* not lint */
 
+#include <config.h>
 #ifdef __linux__
 #include <sys/types.h>
 #include <linux/types.h>
@@ -73,6 +75,7 @@ static const char rcsid[] =
 #include <setjmp.h>
 #include <signal.h>
 #include <stdio.h>
+#include <compaterr.h>
 #ifdef __STDC__
 #include <stdlib.h>
 #include <string.h>
@@ -97,6 +100,7 @@ extern       int cartridge;
 extern char *host;
 char   *nexttape;
 extern  pid_t rshpid;
+int    eot_code = 1;
 
 static ssize_t atomic_read __P((int, void *, size_t));
 static ssize_t atomic_write __P((int, const void *, size_t));
@@ -105,6 +109,7 @@ static      void enslave __P((void));
 static void flushtape __P((void));
 static void killall __P((void));
 static void rollforward __P((void));
+static int system_command __P((const char *, const char *, int));
 
 /*
  * Concurrent dump mods (Caltech) - disk block reading and tape writing
@@ -231,24 +236,27 @@ dumpblock(daddr_t blkno, int size)
 int    nogripe = 0;
 
 static void
-tperror(int signo)
+tperror(int errnum)
 {
 
        if (pipeout) {
-               msg("write error on %s\n", tape);
+               msg("write error on %s: %s\n", tape, strerror(errnum));
                quit("Cannot recover\n");
                /* NOTREACHED */
        }
-       msg("write error %d blocks into volume %d\n", blocksthisvol, tapeno);
+       msg("write error %d blocks into volume %d: %s\n", blocksthisvol, tapeno, strerror(errnum));
        broadcast("DUMP WRITE ERROR!\n");
-       if (!query("Do you want to restart?"))
-               dumpabort(0);
-       msg("Closing this volume.  Prepare to restart with new media;\n");
-       msg("this dump volume will be rewritten.\n");
-       killall();
-       nogripe = 1;
-       close_rewind();
-       Exit(X_REWRITE);
+       if (query("Do you want to rewrite this volume?")) {
+               msg("Closing this volume.  Prepare to restart with new media;\n");
+               msg("this dump volume will be rewritten.\n");
+               killall();
+               nogripe = 1;
+               close_rewind();
+               Exit(X_REWRITE);
+       }
+       if (query("Do you want to start the next tape?"))
+               return;
+       dumpabort(0);
 }
 
 static void
@@ -281,6 +289,8 @@ do_stats(void)
 #else
                                          ctime(&tnow));
 #endif
+       msg("Volume %d: %ld tape blocks (%.2fMB)\n", tapeno, 
+               blocks, ((double)blocks * TP_BSIZE / 1048576));
        if (ttaken > 0) {
                msg("Volume %d took %d:%02d:%02d\n", tapeno,
                        ttaken / 3600, (ttaken % 3600) / 60, ttaken % 60);
@@ -291,36 +301,55 @@ do_stats(void)
        return(tnow);
 }
 
+char *
+mktimeest(time_t tnow)
+{
+       static char msgbuf[128];
+       time_t deltat;
+
+       msgbuf[0] = '\0';
+
+       if (blockswritten < 500)
+               return NULL;
+       if (blockswritten > tapesize)
+               tapesize = blockswritten;
+       deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
+               / blockswritten * tapesize;
+       if (tnow > tstart_volume)
+               (void)snprintf(msgbuf, sizeof(msgbuf),
+                       "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
+                       (blockswritten * 100.0) / tapesize,
+                       (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume),
+                       (int)(deltat / 3600), (int)((deltat % 3600) / 60));
+       else
+               (void)snprintf(msgbuf, sizeof(msgbuf),
+                       "%3.2f%% done, finished in %d:%02d\n",
+                       (blockswritten * 100.0) / tapesize,
+                       (int)(deltat / 3600), (int)((deltat % 3600) / 60));
+
+       return msgbuf;
+}
+
 #if defined(SIGINFO)
 /*
  * statussig --
  *     information message upon receipt of SIGINFO
- *     (derived from optr.c::timeest())
  */
 void
 statussig(int notused)
 {
-       time_t  tnow, deltat;
-       char    msgbuf[128];
+       time_t tnow;
        int save_errno = errno;
+       char *buf;
 
-       if (blockswritten < 500)
-               return;
 #ifdef __linux__
        (void) time4(&tnow);
 #else
        (void) time((time_t *) &tnow);
 #endif
-       if (blockswritten > tapesize)
-               tapesize = blockswritten;
-       deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
-               / blockswritten * tapesize;
-       (void)snprintf(msgbuf, sizeof(msgbuf),
-               "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
-               (blockswritten * 100.0) / tapesize,
-               (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume),
-               (int)(deltat / 3600), (int)((deltat % 3600) / 60));
-       write(STDERR_FILENO, msgbuf, strlen(msgbuf));
+       buf = mktimeest(tnow);
+       if (buf)
+               write(STDERR_FILENO, buf, strlen(buf));
        errno = save_errno;
 }
 #endif
@@ -353,6 +382,10 @@ flushtape(void)
                }
                slp->sent = 0;
 
+               /* Check for errors */
+               if (got < 0)
+                       tperror(-got);
+               
                /* Check for end of tape */
                if (got < writesize) {
                        msg("End of tape detected\n");
@@ -402,7 +435,49 @@ flushtape(void)
        timeest();
 }
 
-void
+/*
+ * Executes the command in a shell.
+ * Returns -1 if an error occured, the exit status of
+ * the command on success.
+ */
+int system_command(const char *command, const char *device, int volnum) {
+       int pid, status;
+       char commandstr[4096];
+
+       pid = fork();
+       if (pid == -1) {
+               perror("  DUMP: unable to fork");
+               return -1;
+       }
+       if (pid == 0) {
+               setuid(getuid());
+               setgid(getgid());
+#if OLD_STYLE_FSCRIPT
+               snprintf(commandstr, sizeof(commandstr), "%s", command);
+#else
+               snprintf(commandstr, sizeof(commandstr), "%s %s %d", command, device, volnum);
+#endif
+               commandstr[sizeof(commandstr) - 1] = '\0';
+               execl("/bin/sh", "sh", "-c", commandstr, NULL);
+               perror("  DUMP: unable to execute shell");
+               exit(-1);
+       }
+       do {
+               if (waitpid(pid, &status, 0) == -1) {
+                       if (errno != EINTR) {
+                               perror("  DUMP: waitpid error");
+                               return -1;
+                       }
+               } else {
+                       if (WIFEXITED(status))
+                               return WEXITSTATUS(status);
+                       else
+                               return -1;
+               }
+       } while(1);
+}
+
+time_t
 trewind(void)
 {
        int f;
@@ -424,6 +499,10 @@ trewind(void)
                                dumpabort(0);
                        }
                        slaves[f].sent = 0;
+
+                       if (got < 0)
+                               tperror(-got);
+
                        if (got != writesize) {
                                msg("EOT detected in last 2 tape records!\n");
                                msg("Use a longer tape, decrease the size estimate\n");
@@ -435,32 +514,44 @@ trewind(void)
        while (wait((int *)NULL) >= 0)  /* wait for any signals from slaves */
                /* void */;
 
-       if (pipeout)
-               return;
+       if (!pipeout) {
 
-       msg("Closing %s\n", tape);
+               msg("Closing %s\n", tape);
 
 #ifdef RDUMP
-       if (host) {
-               rmtclose();
-               while (rmtopen(tape, 0) < 0)
-                       sleep(10);
-               rmtclose();
-               return;
-       }
+               if (host) {
+                       rmtclose();
+                       while (rmtopen(tape, 0) < 0)
+                               sleep(10);
+                       rmtclose();
+               }
+               else 
 #endif
-       (void) close(tapefd);
-       while ((f = open(tape, 0)) < 0)
-               sleep (10);
-       (void) close(f);
+               {
+                       (void) close(tapefd);
+                       while ((f = open(tape, 0)) < 0)
+                               sleep (10);
+                       (void) close(f);
+               }
+               eot_code = 1;
+               if (eot_script && spcl.c_type != TS_END) {
+                       msg("Launching %s\n", eot_script);
+                       eot_code = system_command(eot_script, tape, tapeno);
+               }
+               if (eot_code != 0 && eot_code != 1) {
+                       msg("Dump aborted by the end of tape script\n");
+                       dumpabort(0);
+               }
+       }
+       return do_stats();
 }
 
+               
 void
 close_rewind(void)
 {
-       trewind();
-       (void)do_stats();
-       if (nexttape || Mflag)
+       (void)trewind();
+       if (nexttape || Mflag || (eot_code == 0) )
                return;
        if (!nogripe) {
                msg("Change Volumes: Mount volume #%d\n", tapeno+1);
@@ -564,12 +655,13 @@ rollforward(void)
        }
        slp->req[0] = *q;
        nextblock = slp->tblock;
-       if (q->dblk == 0)
+       if (q->dblk == 0) {
 #ifdef __linux__
-               *(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)tslp->tblock;
-#else
-               nextblock++;
+       /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
+               *(union u_spcl *)(*nextblock) = *(union u_spcl *)tslp->tblock;
 #endif
+               nextblock++;
+       }
        trecno = 1;
 
        /*
@@ -584,6 +676,9 @@ rollforward(void)
                }
                slp->sent = 0;
 
+               if (got < 0)
+                       tperror(-got);
+
                if (got != writesize) {
                        quit("EOT detected at start of the tape!\n");
                }
@@ -623,17 +718,21 @@ startnewtape(int top)
        int     status;
        int     waitpid;
        char    *p;
+
 #ifdef __linux__
-       void    (*interrupt_save)  __P((int signo));
+       sigset_t sigs;
+       sigemptyset(&sigs);
+       sigaddset(&sigs, SIGINT);
+       sigprocmask(SIG_BLOCK, &sigs, NULL);
 #else  /* __linux__ */
 #ifdef sunos
        void    (*interrupt_save)();
 #else
        sig_t   interrupt_save;
 #endif
+       interrupt_save = signal(SIGINT, SIG_IGN);
 #endif /* __linux__ */
 
-       interrupt_save = signal(SIGINT, SIG_IGN);
        parentpid = getpid();
        tapea_volume = spcl.c_tapea;
 #ifdef __linux__
@@ -643,7 +742,11 @@ startnewtape(int top)
 #endif
 
 restore_check_point:
+#ifdef __linux__
+       sigprocmask(SIG_UNBLOCK, &sigs, NULL);
+#else
        (void)signal(SIGINT, interrupt_save);
+#endif
        /*
         *      All signals are inherited...
         */
@@ -659,7 +762,11 @@ restore_check_point:
                 *      until the child doing all of the work returns.
                 *      don't catch the interrupt
                 */
+#ifdef __linux__
+               sigprocmask(SIG_BLOCK, &sigs, NULL);
+#else
                signal(SIGINT, SIG_IGN);
+#endif
 #ifdef TDEBUG
                msg("Tape: %d; parent process: %d child process %d\n",
                        tapeno+1, parentpid, childpid);
@@ -715,8 +822,8 @@ restore_check_point:
                 */
                tapeno++;               /* current tape sequence */
                if (Mflag) {
-                       snprintf(tape, NAME_MAX, "%s%03d", tapeprefix, tapeno);
-                       tape[NAME_MAX - 1] = '\0';
+                       snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno);
+                       tape[MAXPATHLEN - 1] = '\0';
                        msg("Dumping volume %d on %s\n", tapeno, tape);
                }
                else if (nexttape || strchr(tapeprefix, ',')) {
@@ -727,8 +834,8 @@ restore_check_point:
                                nexttape = p + 1;
                        } else
                                nexttape = NULL;
-                       strncpy(tape, tapeprefix, NAME_MAX);
-                       tape[NAME_MAX - 1] = '\0';
+                       strncpy(tape, tapeprefix, MAXPATHLEN);
+                       tape[MAXPATHLEN - 1] = '\0';
                        msg("Dumping volume %d on %s\n", tapeno, tape);
                }
 #ifdef RDUMP
@@ -807,7 +914,6 @@ Exit(int status)
 static void
 proceed(int signo)
 {
-
        if (ready)
                siglongjmp(jmpbuf, 1);
        caught++;
@@ -825,10 +931,17 @@ enslave(void)
 
        master = getpid();
 
-       signal(SIGTERM, dumpabort);  /* Slave sends SIGTERM on dumpabort() */
-       signal(SIGPIPE, sigpipe);
-       signal(SIGUSR1, tperror);    /* Slave sends SIGUSR1 on tape errors */
-       signal(SIGUSR2, proceed);    /* Slave sends SIGUSR2 to next slave */
+    {  struct sigaction sa;
+       memset(&sa, 0, sizeof sa);
+       sigemptyset(&sa.sa_mask);
+       sa.sa_handler = dumpabort;
+       sigaction(SIGTERM, &sa, NULL); /* Slave sends SIGTERM on dumpabort() */
+       sa.sa_handler = sigpipe;
+       sigaction(SIGPIPE, &sa, NULL);
+       sa.sa_handler = proceed;
+       sa.sa_flags = SA_RESTART;
+       sigaction(SIGUSR2, &sa, NULL); /* Slave sends SIGUSR2 to next slave */
+   }
 
        for (i = 0; i < SLAVES; i++) {
                if (i == slp - &slaves[0]) {
@@ -845,12 +958,15 @@ enslave(void)
                slaves[i].fd = cmd[1];
                slaves[i].sent = 0;
                if (slaves[i].pid == 0) {           /* Slave starts up here */
+                       sigset_t sigs;
                        for (j = 0; j <= i; j++)
                                (void) close(slaves[j].fd);
-                       signal(SIGINT, SIG_IGN);    /* Master handles this */
+                       sigemptyset(&sigs);
+                       sigaddset(&sigs, SIGINT);  /* Master handles this */
 #if defined(SIGINFO)
-                       signal(SIGINFO, SIG_IGN);
+                       sigaddset(&sigs, SIGINFO);
 #endif
+                       sigprocmask(SIG_BLOCK, &sigs, NULL);
 
 #ifdef LINUX_FORK_BUG
                        if (atomic_write( cmd[0], (char *) &i, sizeof i)
@@ -906,7 +1022,6 @@ doslave(int cmd, int slave_number)
        register int nread;
        int nextslave, size, eot_count;
        volatile int wrote = 0;
-       sigset_t sigset;
 #ifdef __linux__
        errcode_t retval;
 #endif
@@ -950,7 +1065,7 @@ doslave(int cmd, int slave_number)
                                       quit("master/slave protocol botched.\n");
                        }
                }
-               if (setjmp(jmpbuf) == 0) {
+               if (sigsetjmp(jmpbuf, 1) == 0) {
                        ready = 1;
                        if (!caught)
                                (void) pause();
@@ -999,18 +1114,17 @@ doslave(int cmd, int slave_number)
                if (eot_count > 0)
                        size = 0;
 
-               if (wrote < 0) {
-                       (void) kill(master, SIGUSR1);
-                       sigemptyset(&sigset);
-                       for (;;)
-                               sigsuspend(&sigset);
-               } else {
-                       /*
-                        * pass size of write back to master
-                        * (for EOT handling)
-                        */
-                       (void) atomic_write( cmd, (char *)&size, sizeof size);
-               }
+               /*
+                * pass errno back to master for special handling
+                */
+               if (wrote < 0)
+                       size = -errno;
+
+               /*
+                * pass size of write back to master
+                * (for EOT handling)
+                */
+               (void) atomic_write( cmd, (char *)&size, sizeof size);
 
                /*
                 * If partial write, don't want next slave to go.