2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
10 * Copyright (c) 1980, 1991, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static const char rcsid[] =
44 "$Id: tape.c,v 1.28 2000/12/21 15:01:54 stelian Exp $";
49 #include <sys/types.h>
50 #include <linux/types.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
57 #include <linux/ext2_fs.h>
58 #include <bsdcompat.h>
61 #include <sys/vnode.h>
64 #include <ufs/inode.h>
66 #include <ufs/ufs/dinode.h>
67 #include <ufs/ffs/fs.h>
69 #endif /* __linux__ */
71 #include <protocols/dumprestore.h>
78 #include <compaterr.h>
88 #include <ext2fs/ext2fs.h>
93 int writesize; /* size of malloc()ed buffer for tape */
94 long lastspclrec = -1; /* tape block number of last written header */
95 int trecno = 0; /* next record to write in current block */
96 extern long blocksperfile; /* number of blocks per output file */
97 long blocksthisvol; /* number of blocks on current output file */
98 extern int ntrec; /* blocking factor on tape */
105 static ssize_t atomic_read __P((int, void *, size_t));
106 static ssize_t atomic_write __P((int, const void *, size_t));
107 static void doslave __P((int, int));
108 static void enslave __P((void));
109 static void flushtape __P((void));
110 static void killall __P((void));
111 static void rollforward __P((void));
112 static int system_command __P((const char *, const char *, int));
115 * Concurrent dump mods (Caltech) - disk block reading and tape writing
116 * are exported to several slave processes. While one slave writes the
117 * tape, the others read disk blocks; they pass control of the tape in
118 * a ring via signals. The parent process traverses the filesystem and
119 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
120 * The following structure defines the instruction packets sent to slaves.
128 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
130 int tapea; /* header number at start of this chunk */
131 int count; /* count to next header (used for TS_TAPE */
133 int inode; /* inode that we are currently dealing with */
134 int fd; /* FD for this slave */
135 int pid; /* PID for this slave */
136 int sent; /* 1 == we've sent this slave requests */
137 int firstrec; /* record number of this block */
138 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
139 struct req *req; /* buffer for requests */
143 char (*nextblock)[TP_BSIZE];
145 static time_t tstart_volume; /* time of volume start */
146 static int tapea_volume; /* value of spcl.c_tapea at volume start */
148 int master; /* pid of master, for sending error signals */
149 int tenths; /* length of tape used per block written */
150 static int caught; /* have we caught the signal to proceed? */
151 static int ready; /* have we reached the lock point without having */
152 /* received the SIGUSR2 signal from the prev slave? */
153 static sigjmp_buf jmpbuf; /* where to jump to if we are ready when the */
154 /* SIGUSR2 arrives from the previous slave */
159 int pgoff = getpagesize() - 1;
163 writesize = ntrec * TP_BSIZE;
164 reqsiz = (ntrec + 1) * sizeof(struct req);
166 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
167 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
168 * repositioning after stopping, i.e, streaming mode, where the gap is
169 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
171 if (blocksperfile == 0 && !unlimited)
172 tenths = writesize / density +
173 (cartridge ? 16 : density == 625 ? 5 : 8);
175 * Allocate tape buffer contiguous with the array of instruction
176 * packets, so flushtape() can write them together with one write().
177 * Align tape buffer on page boundary to speed up tape write().
179 for (i = 0; i <= SLAVES; i++) {
181 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
184 slaves[i].tblock = (char (*)[TP_BSIZE])
186 (((long)&buf[reqsiz] + pgoff) &~ pgoff);
188 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
190 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
196 nextblock = slp->tblock;
201 writerec(const void *dp, int isspcl)
204 slp->req[trecno].dblk = (daddr_t)0;
205 slp->req[trecno].count = 1;
206 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
207 *(union u_spcl *)(*(nextblock)) = *(union u_spcl *)dp;
210 lastspclrec = spcl.c_tapea;
218 dumpblock(daddr_t blkno, int size)
220 int avail, tpblks, dblkno;
222 dblkno = fsbtodb(sblock, blkno);
223 tpblks = size >> tp_bshift;
224 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
225 slp->req[trecno].dblk = dblkno;
226 slp->req[trecno].count = avail;
228 spcl.c_tapea += avail;
231 dblkno += avail << (tp_bshift - dev_bshift);
243 msg("write error on %s: %s\n", tape, strerror(errnum));
244 quit("Cannot recover\n");
247 msg("write error %d blocks into volume %d: %s\n", blocksthisvol, tapeno, strerror(errnum));
248 broadcast("DUMP WRITE ERROR!\n");
249 if (query("Do you want to rewrite this volume?")) {
250 msg("Closing this volume. Prepare to restart with new media;\n");
251 msg("this dump volume will be rewritten.\n");
257 if (query("Do you want to start the next tape?"))
266 quit("Broken pipe\n");
271 * Update xferrate stats
284 ttaken = tnow - tstart_volume;
285 blocks = spcl.c_tapea - tapea_volume;
286 msg("Volume %d completed at: %s", tapeno,
293 msg("Volume %d took %d:%02d:%02d\n", tapeno,
294 ttaken / 3600, (ttaken % 3600) / 60, ttaken % 60);
295 msg("Volume %d transfer rate: %ld KB/s\n", tapeno,
297 xferrate += blocks / ttaken;
303 mktimeest(time_t tnow)
305 static char msgbuf[128];
310 if (blockswritten < 500)
312 if (blockswritten > tapesize)
313 tapesize = blockswritten;
314 deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
315 / blockswritten * tapesize;
316 (void)snprintf(msgbuf, sizeof(msgbuf),
317 "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
318 (blockswritten * 100.0) / tapesize,
319 (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume),
320 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
328 * information message upon receipt of SIGINFO
331 statussig(int notused)
334 int save_errno = errno;
340 (void) time((time_t *) &tnow);
342 buf = mktimeest(tnow);
344 write(STDERR_FILENO, buf, strlen(buf));
355 int siz = (char *)nextblock - (char *)slp->req;
357 slp->req[trecno].count = 0; /* Sentinel */
359 if (atomic_write( slp->fd, (char *)slp->req, siz) != siz)
360 quit("error writing command pipe: %s\n", strerror(errno));
361 slp->sent = 1; /* we sent a request, read the response later */
363 lastfirstrec = slp->firstrec;
365 if (++slp >= &slaves[SLAVES])
368 /* Read results back from next slave */
370 if (atomic_read( slp->fd, (char *)&got, sizeof got)
372 perror(" DUMP: error reading command pipe in master");
377 /* Check for errors */
381 /* Check for end of tape */
382 if (got < writesize) {
383 msg("End of tape detected\n");
386 * Drain the results, don't care what the values were.
387 * If we read them here then trewind won't...
389 for (i = 0; i < SLAVES; i++) {
390 if (slaves[i].sent) {
391 if (atomic_read( slaves[i].fd,
392 (char *)&got, sizeof got)
394 perror(" DUMP: error reading command pipe in master");
408 if (spcl.c_type != TS_END) {
409 for (i = 0; i < spcl.c_count; i++)
410 if (spcl.c_addr[i] != 0)
413 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
414 slp->tapea = spcl.c_tapea;
415 slp->firstrec = lastfirstrec + ntrec;
417 nextblock = slp->tblock;
420 blockswritten += ntrec;
421 blocksthisvol += ntrec;
422 if (!pipeout && !unlimited && (blocksperfile ?
423 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
431 * Executes the command in a shell.
432 * Returns -1 if an error occured, the exit status of
433 * the command on success.
435 int system_command(const char *command, const char *device, int volnum) {
437 char commandstr[4096];
441 perror(" DUMP: unable to fork");
447 #if OLD_STYLE_FSCRIPT
448 snprintf(commandstr, sizeof(commandstr), "%s", command);
450 snprintf(commandstr, sizeof(commandstr), "%s %s %d", command, device, volnum);
452 commandstr[sizeof(commandstr) - 1] = '\0';
453 execl("/bin/sh", "sh", "-c", commandstr, NULL);
454 perror(" DUMP: unable to execute shell");
458 if (waitpid(pid, &status, 0) == -1) {
459 if (errno != EINTR) {
460 perror(" DUMP: waitpid error");
464 if (WIFEXITED(status))
465 return WEXITSTATUS(status);
478 for (f = 0; f < SLAVES; f++) {
480 * Drain the results, but unlike EOT we DO (or should) care
481 * what the return values were, since if we detect EOT after
482 * we think we've written the last blocks to the tape anyway,
483 * we have to replay those blocks with rollforward.
485 * fixme: punt for now.
487 if (slaves[f].sent) {
488 if (atomic_read( slaves[f].fd, (char *)&got, sizeof got)
490 perror(" DUMP: error reading command pipe in master");
498 if (got != writesize) {
499 msg("EOT detected in last 2 tape records!\n");
500 msg("Use a longer tape, decrease the size estimate\n");
501 quit("or use no size estimate at all.\n");
504 (void) close(slaves[f].fd);
506 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
511 msg("Closing %s\n", tape);
516 while (rmtopen(tape, 0) < 0)
523 (void) close(tapefd);
524 while ((f = open(tape, 0)) < 0)
529 if (eot_script && spcl.c_type != TS_END) {
530 msg("Launching %s\n", eot_script);
531 eot_code = system_command(eot_script, tape, tapeno);
533 if (eot_code != 0 && eot_code != 1) {
534 msg("Dump aborted by the end of tape script\n");
546 if (nexttape || Mflag || (eot_code == 0) )
549 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
550 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
552 while (!query("Is the new volume mounted and ready to go?"))
553 if (query("Do you want to abort?")) {
562 register struct req *p, *q, *prev;
563 register struct slave *tslp;
564 int i, size, savedtapea, got;
565 union u_spcl *ntb, *otb;
570 tslp = &slaves[SLAVES];
571 ntb = (union u_spcl *)tslp->tblock[1];
574 * Each of the N slaves should have requests that need to
575 * be replayed on the next tape. Use the extra slave buffers
576 * (slaves[SLAVES]) to construct request lists to be sent to
577 * each slave in turn.
579 for (i = 0; i < SLAVES; i++) {
581 otb = (union u_spcl *)slp->tblock;
584 * For each request in the current slave, copy it to tslp.
588 for (p = slp->req; p->count > 0; p += p->count) {
591 *ntb++ = *otb++; /* copy the datablock also */
596 quit("rollforward: protocol botch");
608 nextblock = tslp->tblock;
609 savedtapea = spcl.c_tapea;
610 spcl.c_tapea = slp->tapea;
612 spcl.c_tapea = savedtapea;
613 lastspclrec = savedtapea - 1;
615 size = (char *)ntb - (char *)q;
616 if (atomic_write( slp->fd, (char *)q, size) != size) {
617 perror(" DUMP: error writing command pipe");
622 lastfirstrec = slp->firstrec;
624 if (++slp >= &slaves[SLAVES])
629 if (prev->dblk != 0) {
631 * If the last one was a disk block, make the
632 * first of this one be the last bit of that disk
635 q->dblk = prev->dblk +
636 prev->count * (TP_BSIZE / DEV_BSIZE);
637 ntb = (union u_spcl *)tslp->tblock;
640 * It wasn't a disk block. Copy the data to its
641 * new location in the buffer.
644 *((union u_spcl *)tslp->tblock) = *ntb;
645 ntb = (union u_spcl *)tslp->tblock[1];
649 nextblock = slp->tblock;
652 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
653 *(union u_spcl *)(*nextblock) = *(union u_spcl *)tslp->tblock;
660 * Clear the first slaves' response. One hopes that it
661 * worked ok, otherwise the tape is much too short!
664 if (atomic_read( slp->fd, (char *)&got, sizeof got)
666 perror(" DUMP: error reading command pipe in master");
674 if (got != writesize) {
675 quit("EOT detected at start of the tape!\n");
681 if (spcl.c_type != TS_END) {
682 for (i = 0; i < spcl.c_count; i++)
683 if (spcl.c_addr[i] != 0)
687 slp->firstrec = lastfirstrec + ntrec;
688 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
691 blockswritten += ntrec;
692 blocksthisvol += ntrec;
697 * We implement taking and restoring checkpoints on the tape level.
698 * When each tape is opened, a new process is created by forking; this
699 * saves all of the necessary context in the parent. The child
700 * continues the dump; the parent waits around, saving the context.
701 * If the child returns X_REWRITE, then it had problems writing that tape;
702 * this causes the parent to fork again, duplicating the context, and
703 * everything continues as if nothing had happened.
706 startnewtape(int top)
717 sigaddset(&sigs, SIGINT);
718 sigprocmask(SIG_BLOCK, &sigs, NULL);
719 #else /* __linux__ */
721 void (*interrupt_save)();
723 sig_t interrupt_save;
725 interrupt_save = signal(SIGINT, SIG_IGN);
726 #endif /* __linux__ */
728 parentpid = getpid();
729 tapea_volume = spcl.c_tapea;
731 (void)time4(&tstart_volume);
733 (void)time((&tstart_volume);
738 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
740 (void)signal(SIGINT, interrupt_save);
743 * All signals are inherited...
747 msg("Context save fork fails in parent %d\n", parentpid);
753 * save the context by waiting
754 * until the child doing all of the work returns.
755 * don't catch the interrupt
758 sigprocmask(SIG_BLOCK, &sigs, NULL);
760 signal(SIGINT, SIG_IGN);
763 msg("Tape: %d; parent process: %d child process %d\n",
764 tapeno+1, parentpid, childpid);
766 while ((waitpid = wait(&status)) != childpid)
767 if (waitpid != rshpid)
768 msg("Parent %d waiting for child %d has another child %d return\n",
769 parentpid, childpid, waitpid);
771 msg("Child %d returns LOB status %o\n",
772 childpid, status&0xFF);
774 status = (status >> 8) & 0xFF;
778 msg("Child %d finishes X_FINOK\n", childpid);
781 msg("Child %d finishes X_ABORT\n", childpid);
784 msg("Child %d finishes X_REWRITE\n", childpid);
787 msg("Child %d finishes unknown %d\n",
798 goto restore_check_point;
800 msg("Bad return code from dump: %d\n", status);
804 } else { /* we are the child; just continue */
806 sleep(4); /* allow time for parent's message to get out */
807 msg("Child on Tape %d has parent %d, my pid = %d\n",
808 tapeno+1, parentpid, getpid());
811 * If we have a name like "/dev/rmt0,/dev/rmt1",
812 * use the name before the comma first, and save
813 * the remaining names for subsequent volumes.
815 tapeno++; /* current tape sequence */
817 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno);
818 tape[MAXPATHLEN - 1] = '\0';
819 msg("Dumping volume %d on %s\n", tapeno, tape);
821 else if (nexttape || strchr(tapeprefix, ',')) {
822 if (nexttape && *nexttape)
823 tapeprefix = nexttape;
824 if ((p = strchr(tapeprefix, ',')) != NULL) {
829 strncpy(tape, tapeprefix, MAXPATHLEN);
830 tape[MAXPATHLEN - 1] = '\0';
831 msg("Dumping volume %d on %s\n", tapeno, tape);
834 while ((tapefd = (host ? rmtopen(tape, 2) : pipeout ?
836 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
838 while ((tapefd = (pipeout ? fileno(stdout) :
839 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
842 msg("Cannot open output \"%s\".\n", tape);
843 if (!query("Do you want to retry the open?"))
847 enslave(); /* Share open tape file descriptor with slaves */
852 newtape++; /* new tape signal */
853 spcl.c_count = slp->count;
855 * measure firstrec in TP_BSIZE units since restore doesn't
856 * know the correct ntrec value...
858 spcl.c_firstrec = slp->firstrec;
860 spcl.c_type = TS_TAPE;
861 spcl.c_flags |= DR_NEWHEADER;
862 writeheader((ino_t)slp->inode);
863 spcl.c_flags &=~ DR_NEWHEADER;
864 msg("Volume %d started at: %s", tapeno,
866 ctime4(&tstart_volume));
868 ctime(&tstart_volume));
871 msg("Volume %d begins with blocks from inode %d\n",
880 if (master != 0 && master != getpid())
881 /* Signals master to call dumpabort */
882 (void) kill(master, SIGTERM);
885 msg("The ENTIRE dump is aborted.\n");
898 msg("pid = %d exits with status %d\n", getpid(), status);
904 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
910 siglongjmp(jmpbuf, 1);
918 #ifdef LINUX_FORK_BUG
926 { struct sigaction sa;
927 memset(&sa, 0, sizeof sa);
928 sigemptyset(&sa.sa_mask);
929 sa.sa_handler = dumpabort;
930 sigaction(SIGTERM, &sa, NULL); /* Slave sends SIGTERM on dumpabort() */
931 sa.sa_handler = sigpipe;
932 sigaction(SIGPIPE, &sa, NULL);
933 sa.sa_handler = proceed;
934 sa.sa_flags = SA_RESTART;
935 sigaction(SIGUSR2, &sa, NULL); /* Slave sends SIGUSR2 to next slave */
938 for (i = 0; i < SLAVES; i++) {
939 if (i == slp - &slaves[0]) {
945 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
946 (slaves[i].pid = fork()) < 0)
947 quit("too many slaves, %d (recompile smaller): %s\n",
950 slaves[i].fd = cmd[1];
952 if (slaves[i].pid == 0) { /* Slave starts up here */
954 for (j = 0; j <= i; j++)
955 (void) close(slaves[j].fd);
957 sigaddset(&sigs, SIGINT); /* Master handles this */
959 sigaddset(&sigs, SIGINFO);
961 sigprocmask(SIG_BLOCK, &sigs, NULL);
963 #ifdef LINUX_FORK_BUG
964 if (atomic_write( cmd[0], (char *) &i, sizeof i)
966 quit("master/slave protocol botched 3\n");
973 #ifdef LINUX_FORK_BUG
975 * Wait for all slaves to _actually_ start to circumvent a bug in
976 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
977 * returned from fork() causes a SEGV in the child process
979 for (i = 0; i < SLAVES; i++)
980 if (atomic_read( slaves[i].fd, (char *) &j, sizeof j) != sizeof j)
981 quit("master/slave protocol botched 4\n");
984 for (i = 0; i < SLAVES; i++)
985 (void) atomic_write( slaves[i].fd,
986 (char *) &slaves[(i + 1) % SLAVES].pid,
987 sizeof slaves[0].pid);
997 for (i = 0; i < SLAVES; i++)
998 if (slaves[i].pid > 0) {
999 (void) kill(slaves[i].pid, SIGKILL);
1005 * Synchronization - each process has a lockfile, and shares file
1006 * descriptors to the following process's lockfile. When our write
1007 * completes, we release our lock on the following process's lock-
1008 * file, allowing the following process to lock it and proceed. We
1009 * get the lock back for the next cycle by swapping descriptors.
1012 doslave(int cmd, int slave_number)
1015 int nextslave, size, eot_count;
1016 volatile int wrote = 0;
1022 * Need our own seek pointer.
1024 (void) close(diskfd);
1025 if ((diskfd = open(disk, O_RDONLY)) < 0)
1026 quit("slave couldn't reopen disk: %s\n", strerror(errno));
1029 retval = dump_fs_open(disk, &fs);
1031 quit("slave couldn't reopen disk: %s\n", error_message(retval));
1032 #endif /* __linux__ */
1035 * Need the pid of the next slave in the loop...
1037 if ((nread = atomic_read( cmd, (char *)&nextslave, sizeof nextslave))
1038 != sizeof nextslave) {
1039 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1043 * Get list of blocks to dump, read the blocks into tape buffer
1045 while ((nread = atomic_read( cmd, (char *)slp->req, reqsiz)) == reqsiz) {
1046 register struct req *p = slp->req;
1048 for (trecno = 0; trecno < ntrec;
1049 trecno += p->count, p += p->count) {
1051 bread(p->dblk, slp->tblock[trecno],
1052 p->count * TP_BSIZE);
1054 if (p->count != 1 || atomic_read( cmd,
1055 (char *)slp->tblock[trecno],
1056 TP_BSIZE) != TP_BSIZE)
1057 quit("master/slave protocol botched.\n");
1060 if (sigsetjmp(jmpbuf, 1) == 0) {
1068 /* Try to write the data... */
1073 while (eot_count < 10 && size < writesize) {
1076 wrote = rmtwrite(slp->tblock[0]+size,
1080 wrote = write(tapefd, slp->tblock[0]+size,
1083 printf("slave %d wrote %d\n", slave_number, wrote);
1093 if (size != writesize)
1094 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1095 slave_number, size, writesize);
1099 * Handle ENOSPC as an EOT condition.
1101 if (wrote < 0 && errno == ENOSPC) {
1110 * pass errno back to master for special handling
1116 * pass size of write back to master
1117 * (for EOT handling)
1119 (void) atomic_write( cmd, (char *)&size, sizeof size);
1122 * If partial write, don't want next slave to go.
1123 * Also jolts him awake.
1125 (void) kill(nextslave, SIGUSR2);
1128 quit("error reading command pipe: %s\n", strerror(errno));
1132 * Since a read from a pipe may not return all we asked for,
1133 * or a write may not write all we ask if we get a signal,
1134 * loop until the count is satisfied (or error).
1137 atomic_read(int fd, void *buf, size_t count)
1139 int got, need = count;
1142 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
1144 } while (got == -1 && errno == EINTR);
1145 return (got < 0 ? got : count - need);
1149 * Since a read from a pipe may not return all we asked for,
1150 * or a write may not write all we ask if we get a signal,
1151 * loop until the count is satisfied (or error).
1154 atomic_write(int fd, const void *buf, size_t count)
1156 int got, need = count;
1159 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
1161 } while (got == -1 && errno == EINTR);
1162 return (got < 0 ? got : count - need);