]>
Commit | Line | Data |
---|---|---|
1227625a SP |
1 | /* |
2 | * Ported to Linux's Second Extended File System as part of the | |
3 | * dump and restore backup suit | |
b45f51d6 | 4 | * Remy Card <card@Linux.EU.Org>, 1994-1997 |
ebcbe7f6 | 5 | * Stelian Pop <pop@cybercable.fr>, 1999-2000 |
1227625a SP |
6 | */ |
7 | ||
8 | /*- | |
9 | * Copyright (c) 1980, 1991, 1993 | |
10 | * The Regents of the University of California. All rights reserved. | |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or without | |
13 | * modification, are permitted provided that the following conditions | |
14 | * are met: | |
15 | * 1. Redistributions of source code must retain the above copyright | |
16 | * notice, this list of conditions and the following disclaimer. | |
17 | * 2. Redistributions in binary form must reproduce the above copyright | |
18 | * notice, this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * 3. All advertising materials mentioning features or use of this software | |
21 | * must display the following acknowledgement: | |
22 | * This product includes software developed by the University of | |
23 | * California, Berkeley and its contributors. | |
24 | * 4. Neither the name of the University nor the names of its contributors | |
25 | * may be used to endorse or promote products derived from this software | |
26 | * without specific prior written permission. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
38 | * SUCH DAMAGE. | |
39 | */ | |
40 | ||
df9ae507 SP |
41 | #ifndef lint |
42 | static const char rcsid[] = | |
1099d48c | 43 | "$Id: tape.c,v 1.22 2000/08/21 10:45:40 stelian Exp $"; |
df9ae507 SP |
44 | #endif /* not lint */ |
45 | ||
b45f51d6 SP |
46 | #ifdef __linux__ |
47 | #include <sys/types.h> | |
48 | #include <linux/types.h> | |
49 | #endif | |
1227625a SP |
50 | #include <sys/param.h> |
51 | #include <sys/socket.h> | |
52 | #include <sys/time.h> | |
53 | #include <sys/wait.h> | |
54 | #ifdef __linux__ | |
55 | #include <linux/ext2_fs.h> | |
56 | #include <bsdcompat.h> | |
57 | #else /* __linux__ */ | |
58 | #ifdef sunos | |
59 | #include <sys/vnode.h> | |
60 | ||
61 | #include <ufs/fs.h> | |
62 | #include <ufs/inode.h> | |
63 | #else | |
64 | #include <ufs/ufs/dinode.h> | |
65 | #include <ufs/ffs/fs.h> | |
66 | #endif | |
67 | #endif /* __linux__ */ | |
68 | ||
69 | #include <protocols/dumprestore.h> | |
70 | ||
71 | #include <errno.h> | |
72 | #include <fcntl.h> | |
73 | #include <setjmp.h> | |
74 | #include <signal.h> | |
75 | #include <stdio.h> | |
0d7af9c5 | 76 | #include <compaterr.h> |
1227625a SP |
77 | #ifdef __STDC__ |
78 | #include <stdlib.h> | |
79 | #include <string.h> | |
80 | #include <unistd.h> | |
81 | #else | |
82 | int write(), read(); | |
83 | #endif | |
84 | ||
c930abff | 85 | #ifdef __linux__ |
1227625a SP |
86 | #include <ext2fs/ext2fs.h> |
87 | #endif | |
c930abff | 88 | |
1227625a | 89 | #include "dump.h" |
1227625a SP |
90 | |
91 | int writesize; /* size of malloc()ed buffer for tape */ | |
92 | long lastspclrec = -1; /* tape block number of last written header */ | |
93 | int trecno = 0; /* next record to write in current block */ | |
94 | extern long blocksperfile; /* number of blocks per output file */ | |
95 | long blocksthisvol; /* number of blocks on current output file */ | |
96 | extern int ntrec; /* blocking factor on tape */ | |
97 | extern int cartridge; | |
98 | extern char *host; | |
99 | char *nexttape; | |
f34aca37 | 100 | extern pid_t rshpid; |
0d7af9c5 | 101 | int eot_code = 1; |
1227625a | 102 | |
ddd2ef55 SP |
103 | static ssize_t atomic_read __P((int, void *, size_t)); |
104 | static ssize_t atomic_write __P((int, const void *, size_t)); | |
1227625a SP |
105 | static void doslave __P((int, int)); |
106 | static void enslave __P((void)); | |
107 | static void flushtape __P((void)); | |
108 | static void killall __P((void)); | |
109 | static void rollforward __P((void)); | |
365a7c7c | 110 | static int system_command __P((const char *, const char *, int)); |
1227625a SP |
111 | |
112 | /* | |
113 | * Concurrent dump mods (Caltech) - disk block reading and tape writing | |
114 | * are exported to several slave processes. While one slave writes the | |
115 | * tape, the others read disk blocks; they pass control of the tape in | |
116 | * a ring via signals. The parent process traverses the filesystem and | |
117 | * sends writeheader()'s and lists of daddr's to the slaves via pipes. | |
118 | * The following structure defines the instruction packets sent to slaves. | |
119 | */ | |
120 | struct req { | |
121 | daddr_t dblk; | |
122 | int count; | |
123 | }; | |
124 | int reqsiz; | |
125 | ||
126 | #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */ | |
127 | struct slave { | |
128 | int tapea; /* header number at start of this chunk */ | |
129 | int count; /* count to next header (used for TS_TAPE */ | |
130 | /* after EOT) */ | |
131 | int inode; /* inode that we are currently dealing with */ | |
132 | int fd; /* FD for this slave */ | |
133 | int pid; /* PID for this slave */ | |
134 | int sent; /* 1 == we've sent this slave requests */ | |
135 | int firstrec; /* record number of this block */ | |
136 | char (*tblock)[TP_BSIZE]; /* buffer for data blocks */ | |
137 | struct req *req; /* buffer for requests */ | |
138 | } slaves[SLAVES+1]; | |
139 | struct slave *slp; | |
140 | ||
141 | char (*nextblock)[TP_BSIZE]; | |
142 | ||
ddd2ef55 SP |
143 | static time_t tstart_volume; /* time of volume start */ |
144 | static int tapea_volume; /* value of spcl.c_tapea at volume start */ | |
145 | ||
1227625a SP |
146 | int master; /* pid of master, for sending error signals */ |
147 | int tenths; /* length of tape used per block written */ | |
148 | static int caught; /* have we caught the signal to proceed? */ | |
149 | static int ready; /* have we reached the lock point without having */ | |
150 | /* received the SIGUSR2 signal from the prev slave? */ | |
ddd2ef55 | 151 | static sigjmp_buf jmpbuf; /* where to jump to if we are ready when the */ |
1227625a SP |
152 | /* SIGUSR2 arrives from the previous slave */ |
153 | ||
154 | int | |
ddd2ef55 | 155 | alloctape(void) |
1227625a SP |
156 | { |
157 | int pgoff = getpagesize() - 1; | |
158 | char *buf; | |
159 | int i; | |
160 | ||
161 | writesize = ntrec * TP_BSIZE; | |
162 | reqsiz = (ntrec + 1) * sizeof(struct req); | |
163 | /* | |
164 | * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode | |
165 | * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require | |
166 | * repositioning after stopping, i.e, streaming mode, where the gap is | |
167 | * variable, 0.30" to 0.45". The gap is maximal when the tape stops. | |
168 | */ | |
b45f51d6 | 169 | if (blocksperfile == 0 && !unlimited) |
1227625a SP |
170 | tenths = writesize / density + |
171 | (cartridge ? 16 : density == 625 ? 5 : 8); | |
172 | /* | |
173 | * Allocate tape buffer contiguous with the array of instruction | |
174 | * packets, so flushtape() can write them together with one write(). | |
175 | * Align tape buffer on page boundary to speed up tape write(). | |
176 | */ | |
177 | for (i = 0; i <= SLAVES; i++) { | |
178 | buf = (char *) | |
179 | malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE)); | |
180 | if (buf == NULL) | |
181 | return(0); | |
182 | slaves[i].tblock = (char (*)[TP_BSIZE]) | |
183 | #ifdef __linux__ | |
184 | (((long)&buf[reqsiz] + pgoff) &~ pgoff); | |
185 | #else | |
186 | (((long)&buf[ntrec + 1] + pgoff) &~ pgoff); | |
187 | #endif | |
188 | slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1; | |
189 | } | |
190 | slp = &slaves[0]; | |
191 | slp->count = 1; | |
192 | slp->tapea = 0; | |
193 | slp->firstrec = 0; | |
194 | nextblock = slp->tblock; | |
195 | return(1); | |
196 | } | |
197 | ||
198 | void | |
ddd2ef55 | 199 | writerec(const void *dp, int isspcl) |
1227625a SP |
200 | { |
201 | ||
202 | slp->req[trecno].dblk = (daddr_t)0; | |
203 | slp->req[trecno].count = 1; | |
ddd2ef55 SP |
204 | /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */ |
205 | *(union u_spcl *)(*(nextblock)) = *(union u_spcl *)dp; | |
206 | nextblock++; | |
1227625a SP |
207 | if (isspcl) |
208 | lastspclrec = spcl.c_tapea; | |
209 | trecno++; | |
210 | spcl.c_tapea++; | |
211 | if (trecno >= ntrec) | |
212 | flushtape(); | |
213 | } | |
214 | ||
215 | void | |
ddd2ef55 | 216 | dumpblock(daddr_t blkno, int size) |
1227625a SP |
217 | { |
218 | int avail, tpblks, dblkno; | |
219 | ||
220 | dblkno = fsbtodb(sblock, blkno); | |
221 | tpblks = size >> tp_bshift; | |
222 | while ((avail = MIN(tpblks, ntrec - trecno)) > 0) { | |
223 | slp->req[trecno].dblk = dblkno; | |
224 | slp->req[trecno].count = avail; | |
225 | trecno += avail; | |
226 | spcl.c_tapea += avail; | |
227 | if (trecno >= ntrec) | |
228 | flushtape(); | |
229 | dblkno += avail << (tp_bshift - dev_bshift); | |
230 | tpblks -= avail; | |
231 | } | |
232 | } | |
233 | ||
234 | int nogripe = 0; | |
235 | ||
ddd2ef55 SP |
236 | static void |
237 | tperror(int signo) | |
1227625a SP |
238 | { |
239 | ||
240 | if (pipeout) { | |
241 | msg("write error on %s\n", tape); | |
242 | quit("Cannot recover\n"); | |
243 | /* NOTREACHED */ | |
244 | } | |
245 | msg("write error %d blocks into volume %d\n", blocksthisvol, tapeno); | |
246 | broadcast("DUMP WRITE ERROR!\n"); | |
247 | if (!query("Do you want to restart?")) | |
248 | dumpabort(0); | |
249 | msg("Closing this volume. Prepare to restart with new media;\n"); | |
250 | msg("this dump volume will be rewritten.\n"); | |
251 | killall(); | |
252 | nogripe = 1; | |
253 | close_rewind(); | |
254 | Exit(X_REWRITE); | |
255 | } | |
256 | ||
ddd2ef55 SP |
257 | static void |
258 | sigpipe(int signo) | |
1227625a SP |
259 | { |
260 | ||
261 | quit("Broken pipe\n"); | |
262 | } | |
263 | ||
ddd2ef55 SP |
264 | /* |
265 | * do_stats -- | |
266 | * Update xferrate stats | |
267 | */ | |
268 | time_t | |
269 | do_stats(void) | |
270 | { | |
271 | time_t tnow, ttaken; | |
272 | int blocks; | |
273 | ||
8d4197bb SP |
274 | #ifdef __linux__ |
275 | (void)time4(&tnow); | |
276 | #else | |
ddd2ef55 | 277 | (void)time(&tnow); |
8d4197bb | 278 | #endif |
ddd2ef55 SP |
279 | ttaken = tnow - tstart_volume; |
280 | blocks = spcl.c_tapea - tapea_volume; | |
8d4197bb SP |
281 | msg("Volume %d completed at: %s", tapeno, |
282 | #ifdef __linux__ | |
283 | ctime4(&tnow)); | |
284 | #else | |
285 | ctime(&tnow)); | |
286 | #endif | |
ddd2ef55 SP |
287 | if (ttaken > 0) { |
288 | msg("Volume %d took %d:%02d:%02d\n", tapeno, | |
289 | ttaken / 3600, (ttaken % 3600) / 60, ttaken % 60); | |
290 | msg("Volume %d transfer rate: %ld KB/s\n", tapeno, | |
291 | blocks / ttaken); | |
292 | xferrate += blocks / ttaken; | |
293 | } | |
294 | return(tnow); | |
295 | } | |
296 | ||
297 | #if defined(SIGINFO) | |
298 | /* | |
299 | * statussig -- | |
300 | * information message upon receipt of SIGINFO | |
301 | * (derived from optr.c::timeest()) | |
302 | */ | |
303 | void | |
304 | statussig(int notused) | |
305 | { | |
306 | time_t tnow, deltat; | |
307 | char msgbuf[128]; | |
308 | int save_errno = errno; | |
309 | ||
310 | if (blockswritten < 500) | |
311 | return; | |
8d4197bb SP |
312 | #ifdef __linux__ |
313 | (void) time4(&tnow); | |
314 | #else | |
ddd2ef55 | 315 | (void) time((time_t *) &tnow); |
8d4197bb | 316 | #endif |
8eb05297 SP |
317 | if (blockswritten > tapesize) |
318 | tapesize = blockswritten; | |
ddd2ef55 SP |
319 | deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing)) |
320 | / blockswritten * tapesize; | |
321 | (void)snprintf(msgbuf, sizeof(msgbuf), | |
322 | "%3.2f%% done at %ld KB/s, finished in %d:%02d\n", | |
323 | (blockswritten * 100.0) / tapesize, | |
324 | (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume), | |
325 | (int)(deltat / 3600), (int)((deltat % 3600) / 60)); | |
326 | write(STDERR_FILENO, msgbuf, strlen(msgbuf)); | |
327 | errno = save_errno; | |
328 | } | |
329 | #endif | |
330 | ||
1227625a | 331 | static void |
ddd2ef55 | 332 | flushtape(void) |
1227625a SP |
333 | { |
334 | int i, blks, got; | |
335 | long lastfirstrec; | |
336 | ||
337 | int siz = (char *)nextblock - (char *)slp->req; | |
338 | ||
339 | slp->req[trecno].count = 0; /* Sentinel */ | |
340 | ||
ddd2ef55 | 341 | if (atomic_write( slp->fd, (char *)slp->req, siz) != siz) |
1227625a SP |
342 | quit("error writing command pipe: %s\n", strerror(errno)); |
343 | slp->sent = 1; /* we sent a request, read the response later */ | |
344 | ||
345 | lastfirstrec = slp->firstrec; | |
346 | ||
347 | if (++slp >= &slaves[SLAVES]) | |
348 | slp = &slaves[0]; | |
349 | ||
350 | /* Read results back from next slave */ | |
351 | if (slp->sent) { | |
ddd2ef55 | 352 | if (atomic_read( slp->fd, (char *)&got, sizeof got) |
1227625a SP |
353 | != sizeof got) { |
354 | perror(" DUMP: error reading command pipe in master"); | |
355 | dumpabort(0); | |
356 | } | |
357 | slp->sent = 0; | |
358 | ||
359 | /* Check for end of tape */ | |
360 | if (got < writesize) { | |
361 | msg("End of tape detected\n"); | |
362 | ||
363 | /* | |
364 | * Drain the results, don't care what the values were. | |
365 | * If we read them here then trewind won't... | |
366 | */ | |
367 | for (i = 0; i < SLAVES; i++) { | |
368 | if (slaves[i].sent) { | |
ddd2ef55 | 369 | if (atomic_read( slaves[i].fd, |
1227625a SP |
370 | (char *)&got, sizeof got) |
371 | != sizeof got) { | |
372 | perror(" DUMP: error reading command pipe in master"); | |
373 | dumpabort(0); | |
374 | } | |
375 | slaves[i].sent = 0; | |
376 | } | |
377 | } | |
378 | ||
379 | close_rewind(); | |
380 | rollforward(); | |
381 | return; | |
382 | } | |
383 | } | |
384 | ||
385 | blks = 0; | |
386 | if (spcl.c_type != TS_END) { | |
387 | for (i = 0; i < spcl.c_count; i++) | |
388 | if (spcl.c_addr[i] != 0) | |
389 | blks++; | |
390 | } | |
391 | slp->count = lastspclrec + blks + 1 - spcl.c_tapea; | |
392 | slp->tapea = spcl.c_tapea; | |
393 | slp->firstrec = lastfirstrec + ntrec; | |
394 | slp->inode = curino; | |
395 | nextblock = slp->tblock; | |
396 | trecno = 0; | |
397 | asize += tenths; | |
398 | blockswritten += ntrec; | |
399 | blocksthisvol += ntrec; | |
b45f51d6 | 400 | if (!pipeout && !unlimited && (blocksperfile ? |
1227625a SP |
401 | (blocksthisvol >= blocksperfile) : (asize > tsize))) { |
402 | close_rewind(); | |
403 | startnewtape(0); | |
404 | } | |
405 | timeest(); | |
406 | } | |
407 | ||
0d7af9c5 SP |
408 | /* |
409 | * Executes the command in a shell. | |
410 | * Returns -1 if an error occured, the exit status of | |
411 | * the command on success. | |
412 | */ | |
365a7c7c | 413 | int system_command(const char *command, const char *device, int volnum) { |
0d7af9c5 | 414 | int pid, status; |
365a7c7c | 415 | char commandstr[4096]; |
0d7af9c5 SP |
416 | |
417 | pid = fork(); | |
418 | if (pid == -1) { | |
419 | perror(" DUMP: unable to fork"); | |
420 | return -1; | |
421 | } | |
422 | if (pid == 0) { | |
423 | setuid(getuid()); | |
424 | setgid(getgid()); | |
d8574d45 SP |
425 | #if OLD_STYLE_FSCRIPT |
426 | snprintf(commandstr, sizeof(commandstr), "%s", command); | |
427 | #else | |
365a7c7c | 428 | snprintf(commandstr, sizeof(commandstr), "%s %s %d", command, device, volnum); |
d8574d45 | 429 | #endif |
365a7c7c SP |
430 | commandstr[sizeof(commandstr) - 1] = '\0'; |
431 | execl("/bin/sh", "sh", "-c", commandstr, NULL); | |
0d7af9c5 SP |
432 | perror(" DUMP: unable to execute shell"); |
433 | exit(-1); | |
434 | } | |
435 | do { | |
436 | if (waitpid(pid, &status, 0) == -1) { | |
437 | if (errno != EINTR) { | |
438 | perror(" DUMP: waitpid error"); | |
439 | return -1; | |
440 | } | |
441 | } else { | |
442 | if (WIFEXITED(status)) | |
443 | return WEXITSTATUS(status); | |
444 | else | |
445 | return -1; | |
446 | } | |
447 | } while(1); | |
448 | } | |
449 | ||
450 | time_t | |
ddd2ef55 | 451 | trewind(void) |
1227625a SP |
452 | { |
453 | int f; | |
454 | int got; | |
455 | ||
456 | for (f = 0; f < SLAVES; f++) { | |
457 | /* | |
b45f51d6 SP |
458 | * Drain the results, but unlike EOT we DO (or should) care |
459 | * what the return values were, since if we detect EOT after | |
460 | * we think we've written the last blocks to the tape anyway, | |
1227625a SP |
461 | * we have to replay those blocks with rollforward. |
462 | * | |
b45f51d6 | 463 | * fixme: punt for now. |
1227625a SP |
464 | */ |
465 | if (slaves[f].sent) { | |
ddd2ef55 | 466 | if (atomic_read( slaves[f].fd, (char *)&got, sizeof got) |
1227625a SP |
467 | != sizeof got) { |
468 | perror(" DUMP: error reading command pipe in master"); | |
469 | dumpabort(0); | |
470 | } | |
471 | slaves[f].sent = 0; | |
472 | if (got != writesize) { | |
473 | msg("EOT detected in last 2 tape records!\n"); | |
474 | msg("Use a longer tape, decrease the size estimate\n"); | |
475 | quit("or use no size estimate at all.\n"); | |
476 | } | |
477 | } | |
478 | (void) close(slaves[f].fd); | |
479 | } | |
480 | while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */ | |
481 | /* void */; | |
482 | ||
0d7af9c5 | 483 | if (!pipeout) { |
1227625a | 484 | |
0d7af9c5 | 485 | msg("Closing %s\n", tape); |
1227625a SP |
486 | |
487 | #ifdef RDUMP | |
0d7af9c5 SP |
488 | if (host) { |
489 | rmtclose(); | |
490 | while (rmtopen(tape, 0) < 0) | |
491 | sleep(10); | |
492 | rmtclose(); | |
493 | } | |
494 | else | |
24ea1fb9 | 495 | #endif |
0d7af9c5 SP |
496 | { |
497 | (void) close(tapefd); | |
498 | while ((f = open(tape, 0)) < 0) | |
499 | sleep (10); | |
500 | (void) close(f); | |
501 | } | |
0d7af9c5 | 502 | eot_code = 1; |
30a43325 | 503 | if (eot_script && spcl.c_type != TS_END) { |
0d7af9c5 | 504 | msg("Launching %s\n", eot_script); |
365a7c7c | 505 | eot_code = system_command(eot_script, tape, tapeno); |
0d7af9c5 SP |
506 | } |
507 | if (eot_code != 0 && eot_code != 1) { | |
508 | msg("Dump aborted by the end of tape script\n"); | |
509 | dumpabort(0); | |
510 | } | |
511 | } | |
512 | return do_stats(); | |
1227625a SP |
513 | } |
514 | ||
0d7af9c5 | 515 | |
1227625a | 516 | void |
ddd2ef55 | 517 | close_rewind(void) |
1227625a | 518 | { |
0d7af9c5 SP |
519 | (void)trewind(); |
520 | if (nexttape || Mflag || (eot_code == 0) ) | |
1227625a SP |
521 | return; |
522 | if (!nogripe) { | |
523 | msg("Change Volumes: Mount volume #%d\n", tapeno+1); | |
524 | broadcast("CHANGE DUMP VOLUMES!\7\7\n"); | |
525 | } | |
526 | while (!query("Is the new volume mounted and ready to go?")) | |
527 | if (query("Do you want to abort?")) { | |
528 | dumpabort(0); | |
529 | /*NOTREACHED*/ | |
530 | } | |
531 | } | |
532 | ||
533 | void | |
ddd2ef55 | 534 | rollforward(void) |
1227625a SP |
535 | { |
536 | register struct req *p, *q, *prev; | |
537 | register struct slave *tslp; | |
538 | int i, size, savedtapea, got; | |
539 | union u_spcl *ntb, *otb; | |
b45f51d6 SP |
540 | #ifdef __linux__ |
541 | int blks; | |
542 | long lastfirstrec; | |
543 | #endif | |
1227625a SP |
544 | tslp = &slaves[SLAVES]; |
545 | ntb = (union u_spcl *)tslp->tblock[1]; | |
546 | ||
547 | /* | |
b45f51d6 SP |
548 | * Each of the N slaves should have requests that need to |
549 | * be replayed on the next tape. Use the extra slave buffers | |
550 | * (slaves[SLAVES]) to construct request lists to be sent to | |
1227625a SP |
551 | * each slave in turn. |
552 | */ | |
553 | for (i = 0; i < SLAVES; i++) { | |
554 | q = &tslp->req[1]; | |
555 | otb = (union u_spcl *)slp->tblock; | |
556 | ||
557 | /* | |
b45f51d6 | 558 | * For each request in the current slave, copy it to tslp. |
1227625a SP |
559 | */ |
560 | ||
561 | prev = NULL; | |
562 | for (p = slp->req; p->count > 0; p += p->count) { | |
563 | *q = *p; | |
564 | if (p->dblk == 0) | |
565 | *ntb++ = *otb++; /* copy the datablock also */ | |
566 | prev = q; | |
567 | q += q->count; | |
568 | } | |
569 | if (prev == NULL) | |
570 | quit("rollforward: protocol botch"); | |
571 | if (prev->dblk != 0) | |
572 | prev->count -= 1; | |
573 | else | |
574 | ntb--; | |
575 | q -= 1; | |
576 | q->count = 0; | |
577 | q = &tslp->req[0]; | |
578 | if (i == 0) { | |
579 | q->dblk = 0; | |
580 | q->count = 1; | |
581 | trecno = 0; | |
582 | nextblock = tslp->tblock; | |
583 | savedtapea = spcl.c_tapea; | |
584 | spcl.c_tapea = slp->tapea; | |
585 | startnewtape(0); | |
586 | spcl.c_tapea = savedtapea; | |
587 | lastspclrec = savedtapea - 1; | |
588 | } | |
589 | size = (char *)ntb - (char *)q; | |
ddd2ef55 | 590 | if (atomic_write( slp->fd, (char *)q, size) != size) { |
1227625a SP |
591 | perror(" DUMP: error writing command pipe"); |
592 | dumpabort(0); | |
593 | } | |
594 | slp->sent = 1; | |
b45f51d6 SP |
595 | #ifdef __linux__ |
596 | lastfirstrec = slp->firstrec; | |
597 | #endif | |
1227625a SP |
598 | if (++slp >= &slaves[SLAVES]) |
599 | slp = &slaves[0]; | |
600 | ||
601 | q->count = 1; | |
602 | ||
603 | if (prev->dblk != 0) { | |
604 | /* | |
b45f51d6 SP |
605 | * If the last one was a disk block, make the |
606 | * first of this one be the last bit of that disk | |
1227625a SP |
607 | * block... |
608 | */ | |
609 | q->dblk = prev->dblk + | |
610 | prev->count * (TP_BSIZE / DEV_BSIZE); | |
611 | ntb = (union u_spcl *)tslp->tblock; | |
612 | } else { | |
613 | /* | |
b45f51d6 | 614 | * It wasn't a disk block. Copy the data to its |
1227625a SP |
615 | * new location in the buffer. |
616 | */ | |
617 | q->dblk = 0; | |
618 | *((union u_spcl *)tslp->tblock) = *ntb; | |
619 | ntb = (union u_spcl *)tslp->tblock[1]; | |
620 | } | |
621 | } | |
622 | slp->req[0] = *q; | |
623 | nextblock = slp->tblock; | |
30a43325 | 624 | if (q->dblk == 0) { |
b45f51d6 | 625 | #ifdef __linux__ |
30a43325 SP |
626 | /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */ |
627 | *(union u_spcl *)(*nextblock) = *(union u_spcl *)tslp->tblock; | |
b45f51d6 | 628 | #endif |
30a43325 SP |
629 | nextblock++; |
630 | } | |
1227625a SP |
631 | trecno = 1; |
632 | ||
633 | /* | |
634 | * Clear the first slaves' response. One hopes that it | |
635 | * worked ok, otherwise the tape is much too short! | |
636 | */ | |
637 | if (slp->sent) { | |
ddd2ef55 | 638 | if (atomic_read( slp->fd, (char *)&got, sizeof got) |
1227625a SP |
639 | != sizeof got) { |
640 | perror(" DUMP: error reading command pipe in master"); | |
641 | dumpabort(0); | |
642 | } | |
643 | slp->sent = 0; | |
644 | ||
645 | if (got != writesize) { | |
646 | quit("EOT detected at start of the tape!\n"); | |
647 | } | |
648 | } | |
b45f51d6 SP |
649 | |
650 | #ifdef __linux__ | |
651 | blks = 0; | |
652 | if (spcl.c_type != TS_END) { | |
653 | for (i = 0; i < spcl.c_count; i++) | |
654 | if (spcl.c_addr[i] != 0) | |
655 | blks++; | |
656 | } | |
657 | ||
658 | slp->firstrec = lastfirstrec + ntrec; | |
659 | slp->count = lastspclrec + blks + 1 - spcl.c_tapea; | |
660 | slp->inode = curino; | |
661 | asize += tenths; | |
662 | blockswritten += ntrec; | |
663 | blocksthisvol += ntrec; | |
664 | #endif | |
1227625a SP |
665 | } |
666 | ||
667 | /* | |
668 | * We implement taking and restoring checkpoints on the tape level. | |
669 | * When each tape is opened, a new process is created by forking; this | |
670 | * saves all of the necessary context in the parent. The child | |
671 | * continues the dump; the parent waits around, saving the context. | |
672 | * If the child returns X_REWRITE, then it had problems writing that tape; | |
673 | * this causes the parent to fork again, duplicating the context, and | |
674 | * everything continues as if nothing had happened. | |
675 | */ | |
676 | void | |
ddd2ef55 | 677 | startnewtape(int top) |
1227625a SP |
678 | { |
679 | int parentpid; | |
680 | int childpid; | |
681 | int status; | |
682 | int waitpid; | |
683 | char *p; | |
30a43325 | 684 | |
1227625a | 685 | #ifdef __linux__ |
30a43325 SP |
686 | sigset_t sigs; |
687 | sigemptyset(&sigs); | |
688 | sigaddset(&sigs, SIGINT); | |
689 | sigprocmask(SIG_BLOCK, &sigs, NULL); | |
1227625a SP |
690 | #else /* __linux__ */ |
691 | #ifdef sunos | |
692 | void (*interrupt_save)(); | |
693 | #else | |
694 | sig_t interrupt_save; | |
695 | #endif | |
30a43325 | 696 | interrupt_save = signal(SIGINT, SIG_IGN); |
1227625a SP |
697 | #endif /* __linux__ */ |
698 | ||
1227625a | 699 | parentpid = getpid(); |
ddd2ef55 | 700 | tapea_volume = spcl.c_tapea; |
8d4197bb SP |
701 | #ifdef __linux__ |
702 | (void)time4(&tstart_volume); | |
703 | #else | |
704 | (void)time((&tstart_volume); | |
705 | #endif | |
1227625a SP |
706 | |
707 | restore_check_point: | |
30a43325 SP |
708 | #ifdef __linux__ |
709 | sigprocmask(SIG_UNBLOCK, &sigs, NULL); | |
710 | #else | |
1227625a | 711 | (void)signal(SIGINT, interrupt_save); |
30a43325 | 712 | #endif |
1227625a SP |
713 | /* |
714 | * All signals are inherited... | |
715 | */ | |
716 | childpid = fork(); | |
717 | if (childpid < 0) { | |
718 | msg("Context save fork fails in parent %d\n", parentpid); | |
719 | Exit(X_ABORT); | |
720 | } | |
721 | if (childpid != 0) { | |
722 | /* | |
723 | * PARENT: | |
724 | * save the context by waiting | |
725 | * until the child doing all of the work returns. | |
726 | * don't catch the interrupt | |
727 | */ | |
30a43325 SP |
728 | #ifdef __linux__ |
729 | sigprocmask(SIG_BLOCK, &sigs, NULL); | |
730 | #else | |
1227625a | 731 | signal(SIGINT, SIG_IGN); |
30a43325 | 732 | #endif |
1227625a SP |
733 | #ifdef TDEBUG |
734 | msg("Tape: %d; parent process: %d child process %d\n", | |
735 | tapeno+1, parentpid, childpid); | |
736 | #endif /* TDEBUG */ | |
737 | while ((waitpid = wait(&status)) != childpid) | |
f34aca37 SP |
738 | if (waitpid != rshpid) |
739 | msg("Parent %d waiting for child %d has another child %d return\n", | |
1227625a SP |
740 | parentpid, childpid, waitpid); |
741 | if (status & 0xFF) { | |
742 | msg("Child %d returns LOB status %o\n", | |
743 | childpid, status&0xFF); | |
744 | } | |
745 | status = (status >> 8) & 0xFF; | |
746 | #ifdef TDEBUG | |
747 | switch(status) { | |
748 | case X_FINOK: | |
749 | msg("Child %d finishes X_FINOK\n", childpid); | |
750 | break; | |
b45f51d6 | 751 | case X_ABORT: |
1227625a SP |
752 | msg("Child %d finishes X_ABORT\n", childpid); |
753 | break; | |
754 | case X_REWRITE: | |
755 | msg("Child %d finishes X_REWRITE\n", childpid); | |
756 | break; | |
757 | default: | |
758 | msg("Child %d finishes unknown %d\n", | |
759 | childpid, status); | |
760 | break; | |
761 | } | |
762 | #endif /* TDEBUG */ | |
763 | switch(status) { | |
764 | case X_FINOK: | |
765 | Exit(X_FINOK); | |
766 | case X_ABORT: | |
767 | Exit(X_ABORT); | |
768 | case X_REWRITE: | |
769 | goto restore_check_point; | |
770 | default: | |
771 | msg("Bad return code from dump: %d\n", status); | |
772 | Exit(X_ABORT); | |
773 | } | |
774 | /*NOTREACHED*/ | |
775 | } else { /* we are the child; just continue */ | |
776 | #ifdef TDEBUG | |
777 | sleep(4); /* allow time for parent's message to get out */ | |
778 | msg("Child on Tape %d has parent %d, my pid = %d\n", | |
779 | tapeno+1, parentpid, getpid()); | |
780 | #endif /* TDEBUG */ | |
781 | /* | |
782 | * If we have a name like "/dev/rmt0,/dev/rmt1", | |
783 | * use the name before the comma first, and save | |
784 | * the remaining names for subsequent volumes. | |
785 | */ | |
786 | tapeno++; /* current tape sequence */ | |
d1c73b9a | 787 | if (Mflag) { |
92a9bf12 SP |
788 | snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno); |
789 | tape[MAXPATHLEN - 1] = '\0'; | |
d1c73b9a SP |
790 | msg("Dumping volume %d on %s\n", tapeno, tape); |
791 | } | |
792 | else if (nexttape || strchr(tapeprefix, ',')) { | |
1227625a | 793 | if (nexttape && *nexttape) |
d1c73b9a SP |
794 | tapeprefix = nexttape; |
795 | if ((p = strchr(tapeprefix, ',')) != NULL) { | |
1227625a SP |
796 | *p = '\0'; |
797 | nexttape = p + 1; | |
798 | } else | |
799 | nexttape = NULL; | |
92a9bf12 SP |
800 | strncpy(tape, tapeprefix, MAXPATHLEN); |
801 | tape[MAXPATHLEN - 1] = '\0'; | |
1227625a SP |
802 | msg("Dumping volume %d on %s\n", tapeno, tape); |
803 | } | |
804 | #ifdef RDUMP | |
d3393043 SP |
805 | while ((tapefd = (host ? rmtopen(tape, 2) : pipeout ? |
806 | fileno(stdout) : | |
807 | open(tape, O_WRONLY|O_CREAT, 0666))) < 0) | |
1227625a | 808 | #else |
d3393043 | 809 | while ((tapefd = (pipeout ? fileno(stdout) : |
1227625a SP |
810 | open(tape, O_WRONLY|O_CREAT, 0666))) < 0) |
811 | #endif | |
812 | { | |
813 | msg("Cannot open output \"%s\".\n", tape); | |
814 | if (!query("Do you want to retry the open?")) | |
815 | dumpabort(0); | |
816 | } | |
817 | ||
818 | enslave(); /* Share open tape file descriptor with slaves */ | |
819 | ||
820 | asize = 0; | |
821 | blocksthisvol = 0; | |
822 | if (top) | |
823 | newtape++; /* new tape signal */ | |
b45f51d6 | 824 | spcl.c_count = slp->count; |
1227625a SP |
825 | /* |
826 | * measure firstrec in TP_BSIZE units since restore doesn't | |
827 | * know the correct ntrec value... | |
828 | */ | |
829 | spcl.c_firstrec = slp->firstrec; | |
830 | spcl.c_volume++; | |
831 | spcl.c_type = TS_TAPE; | |
832 | spcl.c_flags |= DR_NEWHEADER; | |
833 | writeheader((ino_t)slp->inode); | |
834 | spcl.c_flags &=~ DR_NEWHEADER; | |
8d4197bb SP |
835 | msg("Volume %d started at: %s", tapeno, |
836 | #ifdef __linux__ | |
837 | ctime4(&tstart_volume)); | |
838 | #else | |
839 | ctime(&tstart_volume)); | |
840 | #endif | |
1227625a SP |
841 | if (tapeno > 1) |
842 | msg("Volume %d begins with blocks from inode %d\n", | |
843 | tapeno, slp->inode); | |
844 | } | |
845 | } | |
846 | ||
847 | void | |
ddd2ef55 | 848 | dumpabort(int signo) |
1227625a SP |
849 | { |
850 | ||
851 | if (master != 0 && master != getpid()) | |
852 | /* Signals master to call dumpabort */ | |
853 | (void) kill(master, SIGTERM); | |
854 | else { | |
855 | killall(); | |
856 | msg("The ENTIRE dump is aborted.\n"); | |
857 | } | |
858 | #ifdef RDUMP | |
859 | rmtclose(); | |
860 | #endif | |
861 | Exit(X_ABORT); | |
862 | } | |
863 | ||
b45f51d6 | 864 | void |
ddd2ef55 | 865 | Exit(int status) |
1227625a SP |
866 | { |
867 | ||
868 | #ifdef TDEBUG | |
869 | msg("pid = %d exits with status %d\n", getpid(), status); | |
870 | #endif /* TDEBUG */ | |
871 | exit(status); | |
872 | } | |
873 | ||
874 | /* | |
875 | * proceed - handler for SIGUSR2, used to synchronize IO between the slaves. | |
876 | */ | |
ddd2ef55 SP |
877 | static void |
878 | proceed(int signo) | |
1227625a | 879 | { |
1227625a | 880 | if (ready) |
ddd2ef55 | 881 | siglongjmp(jmpbuf, 1); |
f1d86882 | 882 | caught++; |
1227625a SP |
883 | } |
884 | ||
885 | void | |
ddd2ef55 | 886 | enslave(void) |
1227625a SP |
887 | { |
888 | int cmd[2]; | |
889 | #ifdef LINUX_FORK_BUG | |
890 | int i, j; | |
891 | #else | |
892 | register int i, j; | |
893 | #endif | |
894 | ||
895 | master = getpid(); | |
896 | ||
30a43325 | 897 | { struct sigaction sa; |
1099d48c | 898 | #if HAVE_SIGACTION_SA_SIGACTION |
f1d86882 | 899 | sa.sa_sigaction = NULL; |
1099d48c | 900 | #endif |
f1d86882 SP |
901 | sigemptyset(&sa.sa_mask); |
902 | sa.sa_flags = 0; | |
30a43325 SP |
903 | sa.sa_handler = dumpabort; |
904 | sigaction(SIGTERM, &sa, NULL); /* Slave sends SIGTERM on dumpabort() */ | |
905 | sa.sa_handler = sigpipe; | |
906 | sigaction(SIGPIPE, &sa, NULL); | |
907 | sa.sa_handler = tperror; | |
908 | sigaction(SIGUSR1, &sa, NULL); /* Slave sends SIGUSR1 on tape errors */ | |
909 | sa.sa_handler = proceed; | |
910 | sa.sa_flags = SA_RESTART; | |
911 | sigaction(SIGUSR2, &sa, NULL); /* Slave sends SIGUSR2 to next slave */ | |
912 | } | |
1227625a SP |
913 | |
914 | for (i = 0; i < SLAVES; i++) { | |
915 | if (i == slp - &slaves[0]) { | |
916 | caught = 1; | |
917 | } else { | |
918 | caught = 0; | |
919 | } | |
920 | ||
921 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 || | |
922 | (slaves[i].pid = fork()) < 0) | |
923 | quit("too many slaves, %d (recompile smaller): %s\n", | |
924 | i, strerror(errno)); | |
925 | ||
926 | slaves[i].fd = cmd[1]; | |
927 | slaves[i].sent = 0; | |
928 | if (slaves[i].pid == 0) { /* Slave starts up here */ | |
30a43325 | 929 | sigset_t sigs; |
1227625a SP |
930 | for (j = 0; j <= i; j++) |
931 | (void) close(slaves[j].fd); | |
30a43325 SP |
932 | sigemptyset(&sigs); |
933 | sigaddset(&sigs, SIGINT); /* Master handles this */ | |
ddd2ef55 | 934 | #if defined(SIGINFO) |
30a43325 | 935 | sigaddset(&sigs, SIGINFO); |
ddd2ef55 | 936 | #endif |
30a43325 | 937 | sigprocmask(SIG_BLOCK, &sigs, NULL); |
ddd2ef55 | 938 | |
1227625a | 939 | #ifdef LINUX_FORK_BUG |
ddd2ef55 | 940 | if (atomic_write( cmd[0], (char *) &i, sizeof i) |
1227625a SP |
941 | != sizeof i) |
942 | quit("master/slave protocol botched 3\n"); | |
943 | #endif | |
944 | doslave(cmd[0], i); | |
945 | Exit(X_FINOK); | |
946 | } | |
947 | } | |
948 | ||
949 | #ifdef LINUX_FORK_BUG | |
950 | /* | |
951 | * Wait for all slaves to _actually_ start to circumvent a bug in | |
952 | * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't | |
953 | * returned from fork() causes a SEGV in the child process | |
954 | */ | |
955 | for (i = 0; i < SLAVES; i++) | |
ddd2ef55 | 956 | if (atomic_read( slaves[i].fd, (char *) &j, sizeof j) != sizeof j) |
1227625a SP |
957 | quit("master/slave protocol botched 4\n"); |
958 | #endif | |
959 | ||
1227625a | 960 | for (i = 0; i < SLAVES; i++) |
ddd2ef55 | 961 | (void) atomic_write( slaves[i].fd, |
1227625a SP |
962 | (char *) &slaves[(i + 1) % SLAVES].pid, |
963 | sizeof slaves[0].pid); | |
964 | ||
965 | master = 0; | |
966 | } | |
967 | ||
968 | void | |
ddd2ef55 | 969 | killall(void) |
1227625a SP |
970 | { |
971 | register int i; | |
972 | ||
973 | for (i = 0; i < SLAVES; i++) | |
b45f51d6 | 974 | if (slaves[i].pid > 0) { |
1227625a | 975 | (void) kill(slaves[i].pid, SIGKILL); |
b45f51d6 SP |
976 | slaves[i].sent = 0; |
977 | } | |
1227625a SP |
978 | } |
979 | ||
980 | /* | |
981 | * Synchronization - each process has a lockfile, and shares file | |
982 | * descriptors to the following process's lockfile. When our write | |
983 | * completes, we release our lock on the following process's lock- | |
984 | * file, allowing the following process to lock it and proceed. We | |
985 | * get the lock back for the next cycle by swapping descriptors. | |
986 | */ | |
987 | static void | |
ddd2ef55 | 988 | doslave(int cmd, int slave_number) |
1227625a SP |
989 | { |
990 | register int nread; | |
ddd2ef55 SP |
991 | int nextslave, size, eot_count; |
992 | volatile int wrote = 0; | |
993 | sigset_t sigset; | |
1227625a SP |
994 | #ifdef __linux__ |
995 | errcode_t retval; | |
996 | #endif | |
997 | ||
1227625a SP |
998 | /* |
999 | * Need our own seek pointer. | |
1000 | */ | |
1001 | (void) close(diskfd); | |
1002 | if ((diskfd = open(disk, O_RDONLY)) < 0) | |
1003 | quit("slave couldn't reopen disk: %s\n", strerror(errno)); | |
1004 | #ifdef __linux__ | |
1005 | ext2fs_close(fs); | |
c930abff | 1006 | retval = dump_fs_open(disk, &fs); |
1227625a | 1007 | if (retval) |
c930abff | 1008 | quit("slave couldn't reopen disk: %s\n", error_message(retval)); |
1227625a SP |
1009 | #endif /* __linux__ */ |
1010 | ||
1011 | /* | |
1012 | * Need the pid of the next slave in the loop... | |
1013 | */ | |
ddd2ef55 | 1014 | if ((nread = atomic_read( cmd, (char *)&nextslave, sizeof nextslave)) |
1227625a SP |
1015 | != sizeof nextslave) { |
1016 | quit("master/slave protocol botched - didn't get pid of next slave.\n"); | |
1017 | } | |
1018 | ||
1019 | /* | |
1020 | * Get list of blocks to dump, read the blocks into tape buffer | |
1021 | */ | |
ddd2ef55 | 1022 | while ((nread = atomic_read( cmd, (char *)slp->req, reqsiz)) == reqsiz) { |
1227625a SP |
1023 | register struct req *p = slp->req; |
1024 | ||
1025 | for (trecno = 0; trecno < ntrec; | |
1026 | trecno += p->count, p += p->count) { | |
1027 | if (p->dblk) { | |
1028 | bread(p->dblk, slp->tblock[trecno], | |
1029 | p->count * TP_BSIZE); | |
1030 | } else { | |
ddd2ef55 | 1031 | if (p->count != 1 || atomic_read( cmd, |
b45f51d6 | 1032 | (char *)slp->tblock[trecno], |
1227625a SP |
1033 | TP_BSIZE) != TP_BSIZE) |
1034 | quit("master/slave protocol botched.\n"); | |
1035 | } | |
1036 | } | |
30a43325 | 1037 | if (sigsetjmp(jmpbuf, 1) == 0) { |
1227625a SP |
1038 | ready = 1; |
1039 | if (!caught) | |
1040 | (void) pause(); | |
1041 | } | |
1042 | ready = 0; | |
1043 | caught = 0; | |
1044 | ||
1045 | /* Try to write the data... */ | |
b45f51d6 | 1046 | wrote = 0; |
1227625a SP |
1047 | eot_count = 0; |
1048 | size = 0; | |
1049 | ||
1050 | while (eot_count < 10 && size < writesize) { | |
1051 | #ifdef RDUMP | |
1052 | if (host) | |
1053 | wrote = rmtwrite(slp->tblock[0]+size, | |
1054 | writesize-size); | |
1055 | else | |
1056 | #endif | |
1057 | wrote = write(tapefd, slp->tblock[0]+size, | |
1058 | writesize-size); | |
1059 | #ifdef WRITEDEBUG | |
b45f51d6 | 1060 | printf("slave %d wrote %d\n", slave_number, wrote); |
1227625a | 1061 | #endif |
b45f51d6 | 1062 | if (wrote < 0) |
1227625a SP |
1063 | break; |
1064 | if (wrote == 0) | |
1065 | eot_count++; | |
1066 | size += wrote; | |
1067 | } | |
1068 | ||
1069 | #ifdef WRITEDEBUG | |
b45f51d6 SP |
1070 | if (size != writesize) |
1071 | printf("slave %d only wrote %d out of %d bytes and gave up.\n", | |
1227625a SP |
1072 | slave_number, size, writesize); |
1073 | #endif | |
1074 | ||
b45f51d6 SP |
1075 | /* |
1076 | * Handle ENOSPC as an EOT condition. | |
1077 | */ | |
1078 | if (wrote < 0 && errno == ENOSPC) { | |
1079 | wrote = 0; | |
1080 | eot_count++; | |
1081 | } | |
1082 | ||
1227625a SP |
1083 | if (eot_count > 0) |
1084 | size = 0; | |
1085 | ||
b45f51d6 | 1086 | if (wrote < 0) { |
1227625a | 1087 | (void) kill(master, SIGUSR1); |
ddd2ef55 | 1088 | sigemptyset(&sigset); |
f1d86882 | 1089 | sigaddset(&sigset, SIGINT); |
1227625a | 1090 | for (;;) |
ddd2ef55 | 1091 | sigsuspend(&sigset); |
1227625a SP |
1092 | } else { |
1093 | /* | |
1094 | * pass size of write back to master | |
1095 | * (for EOT handling) | |
1096 | */ | |
ddd2ef55 | 1097 | (void) atomic_write( cmd, (char *)&size, sizeof size); |
b45f51d6 | 1098 | } |
1227625a SP |
1099 | |
1100 | /* | |
1101 | * If partial write, don't want next slave to go. | |
1102 | * Also jolts him awake. | |
1103 | */ | |
1104 | (void) kill(nextslave, SIGUSR2); | |
1105 | } | |
1106 | if (nread != 0) | |
1107 | quit("error reading command pipe: %s\n", strerror(errno)); | |
1108 | } | |
1109 | ||
1110 | /* | |
1111 | * Since a read from a pipe may not return all we asked for, | |
1112 | * or a write may not write all we ask if we get a signal, | |
1113 | * loop until the count is satisfied (or error). | |
1114 | */ | |
ddd2ef55 SP |
1115 | static ssize_t |
1116 | atomic_read(int fd, void *buf, size_t count) | |
1117 | { | |
1118 | int got, need = count; | |
1119 | ||
2e682895 SP |
1120 | do { |
1121 | while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0) | |
1122 | (char *)buf += got; | |
1123 | } while (got == -1 && errno == EINTR); | |
ddd2ef55 SP |
1124 | return (got < 0 ? got : count - need); |
1125 | } | |
1126 | ||
1127 | /* | |
1128 | * Since a read from a pipe may not return all we asked for, | |
1129 | * or a write may not write all we ask if we get a signal, | |
1130 | * loop until the count is satisfied (or error). | |
1131 | */ | |
1132 | static ssize_t | |
1133 | atomic_write(int fd, const void *buf, size_t count) | |
1227625a SP |
1134 | { |
1135 | int got, need = count; | |
1136 | ||
2e682895 SP |
1137 | do { |
1138 | while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0) | |
1139 | (char *)buf += got; | |
1140 | } while (got == -1 && errno == EINTR); | |
1227625a SP |
1141 | return (got < 0 ? got : count - need); |
1142 | } |