from shannon
[unix-history] / usr / src / sbin / dump / main.c
CommitLineData
1ddebffe 1static char *sccsid = "@(#)main.c 1.11 (Berkeley) %G%";
0393c389
BJ
2#include "dump.h"
3
4int notify = 0; /* notify operator flag */
5int blockswritten = 0; /* number of blocks written on current tape */
6int tapeno = 0; /* current tape number */
1ddebffe
SL
7int density = 0; /* density in bytes/0.1" */
8int ntrec = NTREC; /* # tape blocks in each tape record */
9int cartridge = 0; /* Assume non-cartridge tape */
c47275e3
SL
10#ifdef RDUMP
11char *host;
12#endif
0393c389
BJ
13
14main(argc, argv)
15 int argc;
16 char *argv[];
17{
18 char *arg;
b6407c9d 19 int i;
0393c389
BJ
20 float fetapes;
21 register struct fstab *dt;
22
23 time(&(spcl.c_date));
24
1ddebffe 25 tsize = 0; /* Default later, based on 'c' option for cart tapes */
0393c389
BJ
26 tape = TAPE;
27 disk = DISK;
28 increm = NINCREM;
b6407c9d
KM
29 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) {
30 msg("TP_BSIZE must be a multiple of DEV_BSIZE\n");
31 dumpabort();
32 }
0393c389
BJ
33 incno = '9';
34 uflag = 0;
35 arg = "u";
36 if(argc > 1) {
37 argv++;
38 argc--;
39 arg = *argv;
40 if (*arg == '-')
41 argc++;
42 }
43 while(*arg)
44 switch (*arg++) {
77db894d
BJ
45 case 'w':
46 lastdump('w'); /* tell us only what has to be done */
47 exit(0);
48 break;
0393c389 49 case 'W': /* what to do */
77db894d 50 lastdump('W'); /* tell us the current state of what has been done */
0393c389
BJ
51 exit(0); /* do nothing else */
52 break;
53
54 case 'J': /* update old to new */
55 o_nconvert();
56 exit(0); /* do nothing else */
57 break;
58
59 case 'f': /* output file */
60 if(argc > 1) {
61 argv++;
62 argc--;
63 tape = *argv;
64 }
65 break;
66
67 case 'd': /* density, in bits per inch */
68 if (argc > 1) {
69 argv++;
70 argc--;
71 density = atoi(*argv) / 10;
72 }
73 break;
74
75 case 's': /* tape size, feet */
76 if(argc > 1) {
77 argv++;
78 argc--;
79 tsize = atol(*argv);
80 tsize *= 12L*10L;
81 }
82 break;
83
1ddebffe
SL
84 case 'b': /* blocks per tape write */
85 if(argc > 1) {
86 argv++;
87 argc--;
88 ntrec = atol(*argv);
89 }
90 break;
91
92 case 'c': /* Tape is cart. not 9-track */
93 cartridge++;
94 break;
95
0393c389
BJ
96 case '0': /* dump level */
97 case '1':
98 case '2':
99 case '3':
100 case '4':
101 case '5':
102 case '6':
103 case '7':
104 case '8':
105 case '9':
106 incno = arg[-1];
107 break;
108
109 case 'u': /* update /etc/dumpdates */
110 uflag++;
111 break;
112
113 case 'n': /* notify operators */
114 notify++;
115 break;
116
117 default:
118 printf("bad key '%c%'\n", arg[-1]);
119 Exit(X_ABORT);
120 }
121 if(argc > 1) {
122 argv++;
123 argc--;
124 disk = *argv;
125 }
1ddebffe
SL
126
127 /*
128 * Determine how to default tape size and density
129 *
130 * density tape size
131 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
132 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
133 * cartridge 8000 bpi (100 bytes/.1") 4000 ft. (450*9 - slop)
134 */
135 if (density == 0)
136 density = cartridge ? 100 : 160;
137 if (tsize == 0)
138 tsize = cartridge ? 4000L*120L : 2300L*120L;
139
c29a1d14
SL
140#ifdef RDUMP
141 { char *index();
142 host = tape;
143 tape = index(host, ':');
144 if (tape == 0) {
145 msg("need keyletter ``f'' and device ``host:tape''");
146 exit(1);
147 }
148 *tape++ = 0;
149 if (rmthost(host) == 0)
150 exit(X_ABORT);
151 }
152#endif
0393c389
BJ
153 if (signal(SIGHUP, sighup) == SIG_IGN)
154 signal(SIGHUP, SIG_IGN);
155 if (signal(SIGTRAP, sigtrap) == SIG_IGN)
156 signal(SIGTRAP, SIG_IGN);
157 if (signal(SIGFPE, sigfpe) == SIG_IGN)
158 signal(SIGFPE, SIG_IGN);
159 if (signal(SIGBUS, sigbus) == SIG_IGN)
160 signal(SIGBUS, SIG_IGN);
161 if (signal(SIGSEGV, sigsegv) == SIG_IGN)
162 signal(SIGSEGV, SIG_IGN);
163 if (signal(SIGTERM, sigterm) == SIG_IGN)
164 signal(SIGTERM, SIG_IGN);
165
166
167 if (signal(SIGINT, interrupt) == SIG_IGN)
168 signal(SIGINT, SIG_IGN);
169
170 set_operators(); /* /etc/group snarfed */
171 getfstab(); /* /etc/fstab snarfed */
172 /*
173 * disk can be either the full special file name,
174 * the suffix of the special file name,
175 * the special name missing the leading '/',
176 * the file system name with or without the leading '/'.
177 */
178 dt = fstabsearch(disk);
179 if (dt != 0)
180 disk = rawname(dt->fs_spec);
181 getitime(); /* /etc/dumpdates snarfed */
182
183 msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
184 msg("Date of last level %c dump: %s\n", incno, prdate(spcl.c_ddate));
185 msg("Dumping %s ", disk);
186 if (dt != 0)
187 msgtail("(%s) ", dt->fs_file);
c29a1d14 188#ifdef RDUMP
b17ae412
KM
189 msgtail("to %s on host %s\n", tape, host);
190#else
191 msgtail("to %s\n", tape);
c47275e3 192#endif
0393c389
BJ
193
194 fi = open(disk, 0);
195 if (fi < 0) {
196 msg("Cannot open %s\n", disk);
197 Exit(X_ABORT);
198 }
0393c389 199 esize = 0;
b6407c9d
KM
200 sblock = (struct fs *)buf;
201 sync();
012ccbb8 202 bread(SBLOCK, sblock, SBSIZE);
b6407c9d
KM
203 if (sblock->fs_magic != FS_MAGIC) {
204 msg("bad sblock magic number\n");
205 dumpabort();
206 }
207 msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
208 TP_BSIZE);
209 clrmap = (char *)calloc(msiz, sizeof(char));
210 dirmap = (char *)calloc(msiz, sizeof(char));
211 nodmap = (char *)calloc(msiz, sizeof(char));
0393c389
BJ
212
213 msg("mapping (Pass I) [regular files]\n");
b6407c9d 214 pass(mark, (char *)NULL); /* mark updates esize */
0393c389
BJ
215
216 do {
217 msg("mapping (Pass II) [directories]\n");
218 nadded = 0;
219 pass(add, dirmap);
220 } while(nadded);
221
222 bmapest(clrmap);
223 bmapest(nodmap);
224
1ddebffe
SL
225 if (cartridge) {
226 /* Estimate number of tapes, assuming streaming stops at
227 the end of each block written, and not in mid-block.
228 Assume no erroneous blocks; this can be compensated for
229 with an artificially low tape size. */
230 fetapes =
231 ( esize /* blocks */
232 * TP_BSIZE /* bytes/block */
233 * (1.0/density) /* 0.1" / byte */
234 +
235 esize /* blocks */
236 * (1.0/ntrec) /* streaming-stops per block */
237 * 15.48 /* 0.1" / streaming-stop */
238 ) * (1.0 / tsize ); /* tape / 0.1" */
239 } else {
240 /* Estimate number of tapes, for old fashioned 9-track tape */
241 int tenthsperirg = (density == 625) ? 3 : 7;
242 fetapes =
b6407c9d
KM
243 ( esize /* blocks */
244 * TP_BSIZE /* bytes / block */
245 * (1.0/density) /* 0.1" / byte */
0393c389 246 +
b6407c9d 247 esize /* blocks */
1ddebffe
SL
248 * (1.0/ntrec) /* IRG's / block */
249 * tenthsperirg /* 0.1" / IRG */
b6407c9d 250 * 7 /* 0.1" / IRG */
1ddebffe
SL
251 ) * (1.0 / tsize ); /* tape / 0.1" */
252 }
0393c389
BJ
253 etapes = fetapes; /* truncating assignment */
254 etapes++;
b6407c9d
KM
255 /* count the nodemap on each additional tape */
256 for (i = 1; i < etapes; i++)
257 bmapest(nodmap);
258 esize += i + 10; /* headers + 10 trailer blocks */
0393c389
BJ
259 msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
260
1ddebffe
SL
261 alloctape(); /* Allocate tape buffer */
262
0393c389
BJ
263 otape(); /* bitmap is the first to tape write */
264 time(&(tstart_writing));
265 bitmap(clrmap, TS_CLRI);
266
267 msg("dumping (Pass III) [directories]\n");
268 pass(dump, dirmap);
269
270 msg("dumping (Pass IV) [regular files]\n");
271 pass(dump, nodmap);
272
273 spcl.c_type = TS_END;
c47275e3 274#ifndef RDUMP
1ddebffe 275 for(i=0; i<ntrec; i++)
0393c389 276 spclrec();
c47275e3 277#endif
0393c389
BJ
278 msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
279 msg("DUMP IS DONE\n");
280
281 putitime();
c47275e3 282#ifndef RDUMP
0393c389 283 close(to);
c47275e3
SL
284#else
285 tflush(1);
286#endif
0393c389
BJ
287 rewind();
288 broadcast("DUMP IS DONE!\7\7\n");
289 Exit(X_FINOK);
290}
291
292int sighup(){ msg("SIGHUP() try rewriting\n"); sigAbort();}
293int sigtrap(){ msg("SIGTRAP() try rewriting\n"); sigAbort();}
294int sigfpe(){ msg("SIGFPE() try rewriting\n"); sigAbort();}
295int sigbus(){ msg("SIGBUS() try rewriting\n"); sigAbort();}
296int sigsegv(){ msg("SIGSEGV() ABORTING!\n"); abort();}
297int sigalrm(){ msg("SIGALRM() try rewriting\n"); sigAbort();}
298int sigterm(){ msg("SIGTERM() try rewriting\n"); sigAbort();}
299
300sigAbort()
301{
302 msg("Rewriting attempted as response to unknown signal.\n");
303 fflush(stderr);
304 fflush(stdout);
305 close_rewind();
306 exit(X_REWRITE);
307}
308
309char *rawname(cp)
310 char *cp;
311{
312 static char rawbuf[32];
1d070b31 313 char *rindex();
0393c389
BJ
314 char *dp = rindex(cp, '/');
315
316 if (dp == 0)
317 return (0);
318 *dp = 0;
319 strcpy(rawbuf, cp);
320 *dp = '/';
321 strcat(rawbuf, "/r");
322 strcat(rawbuf, dp+1);
323 return (rawbuf);
324}