1 /* $OpenBSD: main.c,v 1.66 2024/05/09 08:35:40 florian Exp $ */
2 /* $NetBSD: main.c,v 1.14 1997/06/05 11:13:24 lukem Exp $ */
3
4 /*-
5 * Copyright (c) 1980, 1991, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/param.h> /* MAXBSIZE DEV_BSIZE roundup */
34 #include <sys/mount.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/ioctl.h>
38 #include <sys/disklabel.h>
39 #include <sys/dkio.h>
40 #include <ufs/ffs/fs.h>
41 #include <ufs/ufs/dinode.h>
42
43 #include <protocols/dumprestore.h>
44
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <fstab.h>
50 #include <paths.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57 #include <limits.h>
58 #include <util.h>
59
60 #include "dump.h"
61 #include "pathnames.h"
62
63 int mapsize; /* size of the state maps */
64 char *usedinomap; /* map of allocated inodes */
65 char *dumpdirmap; /* map of directories to be dumped */
66 char *dumpinomap; /* map of files to be dumped */
67 char *disk; /* name of the disk file */
68 char *tape; /* name of the tape file */
69 char level; /* dump level of this dump */
70 int uflag; /* update flag */
71 int diskfd; /* disk file descriptor */
72 int pipeout; /* true => output to standard output */
73 int density = 0; /* density in bytes/0.1" */
74 int64_t tapesize; /* estimated tape size, blocks */
75 int64_t tsize; /* tape size in 0.1" units */
76 int etapes; /* estimated number of tapes */
77 int nonodump; /* if set, do not honor UF_NODUMP user flags */
78 int unlimited; /* if set, write to end of medium */
79 int notify = 0; /* notify operator flag */
80 int64_t blockswritten = 0; /* number of blocks written on current tape */
81 int tapeno = 0; /* current tape number */
82 int ntrec = NTREC; /* # tape blocks in each tape record */
83 int64_t blocksperfile; /* output blocks per file */
84 int cartridge = 0; /* Assume non-cartridge tape */
85 char *host = NULL; /* remote host (if any) */
86 time_t tstart_writing; /* when started writing the first tape block */
87 long xferrate; /* averaged transfer rate of all volumes */
88 struct fs *sblock; /* the file system super block */
89 char sblock_buf[MAXBSIZE];
90 int tp_bshift; /* log2(TP_BSIZE) */
91 char *duid; /* duid of the disk being dumped */
92 int maxbsize = 64*1024; /* XXX MAXBSIZE from sys/param.h */
93
94 struct disklabel lab;
95
96 /*
97 * Possible superblock locations ordered from most to least likely.
98 */
99 static int sblock_try[] = SBLOCKSEARCH;
100
101 static long long numarg(char *, long long, long long);
102 static void obsolete(int *, char **[]);
103 static void usage(void);
104
105 int
main(int argc,char * argv[])106 main(int argc, char *argv[])
107 {
108 ino_t ino;
109 int dirty;
110 union dinode *dp;
111 struct fstab *dt;
112 char *map;
113 int ch, mode;
114 struct tm then;
115 struct statfs fsbuf;
116 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
117 ino_t maxino;
118 time_t t;
119 int dirlist;
120 char *toplevel, *str, *mount_point = NULL, *realpath, *ct;
121 int just_estimate = 0;
122 u_int64_t zero_uid = 0;
123
124 spcl.c_date = (int64_t)time(NULL);
125
126 tsize = 0; /* Default later, based on 'c' option for cart tapes */
127 if ((tape = getenv("TAPE")) == NULL)
128 tape = _PATH_DEFTAPE;
129 dumpdates = _PATH_DUMPDATES;
130 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
131 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
132 level = '0';
133
134 if (argc < 2)
135 usage();
136
137 obsolete(&argc, &argv);
138 while ((ch = getopt(argc, argv, "0123456789aB:b:cd:f:h:ns:ST:uWw")) != -1)
139 switch (ch) {
140 /* dump level */
141 case '0': case '1': case '2': case '3': case '4':
142 case '5': case '6': case '7': case '8': case '9':
143 level = ch;
144 break;
145
146 case 'B': /* blocks per output file */
147 blocksperfile = numarg("blocks per file", 1, 0);
148 break;
149
150 case 'b': /* blocks per tape write */
151 ntrec = numarg("blocks per write", 1, 1000);
152 if (ntrec > maxbsize/1024) {
153 msg("Please choose a blocksize <= %dKB\n",
154 maxbsize/1024);
155 exit(X_STARTUP);
156 }
157 bflag = 1;
158 break;
159
160 case 'c': /* Tape is cart. not 9-track */
161 cartridge = 1;
162 break;
163
164 case 'd': /* density, in bits per inch */
165 density = numarg("density", 10, 327670) / 10;
166 if (density >= 625 && !bflag)
167 ntrec = HIGHDENSITYTREC;
168 break;
169
170 case 'f': /* output file */
171 tape = optarg;
172 break;
173
174 case 'h':
175 honorlevel = numarg("honor level", 0, 10);
176 break;
177
178 case 'n': /* notify operators */
179 notify = 1;
180 break;
181
182 case 's': /* tape size, feet */
183 tsize = numarg("tape size", 1, 0) * 12 * 10;
184 break;
185
186 case 'S': /* estimate blocks and # of tapes */
187 just_estimate = 1;
188 break;
189
190 case 'T': /* time of last dump */
191 str = strptime(optarg, "%a %b %e %H:%M:%S %Y", &then);
192 then.tm_isdst = -1;
193 if (str == NULL || (*str != '\n' && *str != '\0'))
194 spcl.c_ddate = -1;
195 else
196 spcl.c_ddate = (int64_t)mktime(&then);
197 if (spcl.c_ddate < 0) {
198 (void)fprintf(stderr, "bad time \"%s\"\n",
199 optarg);
200 exit(X_STARTUP);
201 }
202 Tflag = 1;
203 lastlevel = '?';
204 break;
205
206 case 'u': /* update /etc/dumpdates */
207 uflag = 1;
208 break;
209
210 case 'W': /* what to do */
211 case 'w':
212 lastdump(ch);
213 exit(X_FINOK); /* do nothing else */
214 break;
215
216 case 'a': /* `auto-size', Write to EOM. */
217 unlimited = 1;
218 break;
219
220 default:
221 usage();
222 }
223 argc -= optind;
224 argv += optind;
225
226 if (argc < 1) {
227 (void)fprintf(stderr, "Must specify disk or filesystem\n");
228 exit(X_STARTUP);
229 }
230
231 /*
232 * determine if disk is a subdirectory, and setup appropriately
233 */
234 dirlist = 0;
235 toplevel = NULL;
236 for (i = 0; i < argc; i++) {
237 struct stat sb;
238
239 /* Convert potential duid into a device name */
240 if ((diskfd = opendev(argv[i], O_RDONLY | O_NOFOLLOW, 0,
241 &realpath)) >= 0) {
242 argv[i] = strdup(realpath);
243 if (argv[i] == NULL) {
244 msg("Cannot malloc realpath\n");
245 exit(X_STARTUP);
246 }
247 (void)close(diskfd);
248 }
249 if (lstat(argv[i], &sb) == -1) {
250 msg("Cannot lstat %s: %s\n", argv[i], strerror(errno));
251 exit(X_STARTUP);
252 }
253 if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
254 break;
255 if (statfs(argv[i], &fsbuf) == -1) {
256 msg("Cannot statfs %s: %s\n", argv[i], strerror(errno));
257 exit(X_STARTUP);
258 }
259 if (strcmp(argv[i], fsbuf.f_mntonname) == 0) {
260 if (dirlist != 0) {
261 msg("Can't dump a mountpoint and a filelist\n");
262 exit(X_STARTUP);
263 }
264 break; /* exit if sole mountpoint */
265 }
266 if (!disk) {
267 if ((toplevel = strdup(fsbuf.f_mntonname)) == NULL) {
268 msg("Cannot malloc diskname\n");
269 exit(X_STARTUP);
270 }
271 disk = toplevel;
272 if (uflag) {
273 msg("Ignoring u flag for subdir dump\n");
274 uflag = 0;
275 }
276 if (level > '0') {
277 msg("Subdir dump is done at level 0\n");
278 level = '0';
279 }
280 msg("Dumping sub files/directories from %s\n", disk);
281 } else {
282 if (strcmp(disk, fsbuf.f_mntonname) != 0) {
283 msg("%s is not on %s\n", argv[i], disk);
284 exit(X_STARTUP);
285 }
286 }
287 msg("Dumping file/directory %s\n", argv[i]);
288 dirlist++;
289 }
290 if (dirlist == 0) {
291 disk = *argv++;
292 if (argc != 1) {
293 (void)fputs("Excess arguments to dump:", stderr);
294 while (--argc) {
295 (void)putc(' ', stderr);
296 (void)fputs(*argv++, stderr);
297 }
298 (void)putc('\n', stderr);
299 exit(X_STARTUP);
300 }
301 }
302 if (Tflag && uflag) {
303 (void)fprintf(stderr,
304 "You cannot use the T and u flags together.\n");
305 exit(X_STARTUP);
306 }
307 if (strcmp(tape, "-") == 0) {
308 pipeout++;
309 tape = "standard output";
310 }
311
312 if (blocksperfile)
313 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
314 else if (!unlimited) {
315 /*
316 * Determine how to default tape size and density
317 *
318 * density tape size
319 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
320 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
321 * cartridge 8000 bpi (800 bytes/.1") 1700 ft.
322 * (450*4 - slop)
323 */
324 if (density == 0)
325 density = cartridge ? 100 : 160;
326 if (tsize == 0)
327 tsize = cartridge ? 1700L*120L : 2300L*120L;
328 }
329
330 if (strchr(tape, ':')) {
331 host = tape;
332 tape = strchr(host, ':');
333 *tape++ = '\0';
334 #ifdef RDUMP
335 if (rmthost(host) == 0)
336 exit(X_STARTUP);
337 #else
338 (void)fprintf(stderr, "remote dump not enabled\n");
339 exit(X_STARTUP);
340 #endif
341 }
342
343 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
344 signal(SIGHUP, sig);
345 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
346 signal(SIGTERM, sig);
347 if (signal(SIGINT, interrupt) == SIG_IGN)
348 signal(SIGINT, SIG_IGN);
349
350 getfstab(); /* /etc/fstab snarfed */
351
352 /*
353 * disk can be either the full special file name,
354 * the suffix of the special file name,
355 * the special name missing the leading '/',
356 * the file system name with or without the leading '/'.
357 */
358 if (!statfs(disk, &fsbuf) && !strcmp(fsbuf.f_mntonname, disk)) {
359 /* mounted disk? */
360 disk = rawname(fsbuf.f_mntfromname);
361 if (!disk) {
362 (void)fprintf(stderr, "cannot get raw name for %s\n",
363 fsbuf.f_mntfromname);
364 exit(X_STARTUP);
365 }
366 mount_point = fsbuf.f_mntonname;
367 (void)strlcpy(spcl.c_dev, fsbuf.f_mntfromname,
368 sizeof(spcl.c_dev));
369 if (dirlist != 0) {
370 (void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
371 "a subset of %s", mount_point);
372 } else {
373 (void)strlcpy(spcl.c_filesys, mount_point,
374 sizeof(spcl.c_filesys));
375 }
376 } else if ((dt = fstabsearch(disk)) != NULL) {
377 /* in fstab? */
378 if (strchr(dt->fs_spec, '/')) {
379 /* fs_spec is a /dev/something */
380 disk = rawname(dt->fs_spec);
381 } else {
382 /* fs_spec is a DUID */
383 disk = rawname(disk);
384 }
385 mount_point = dt->fs_file;
386 (void)strlcpy(spcl.c_dev, dt->fs_spec, sizeof(spcl.c_dev));
387 if (dirlist != 0) {
388 (void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
389 "a subset of %s", mount_point);
390 } else {
391 (void)strlcpy(spcl.c_filesys, mount_point,
392 sizeof(spcl.c_filesys));
393 }
394 } else {
395 /* must be a device */
396 (void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev));
397 (void)strlcpy(spcl.c_filesys, "an unlisted file system",
398 sizeof(spcl.c_filesys));
399 }
400 (void)strlcpy(spcl.c_label, "none", sizeof(spcl.c_label));
401 (void)gethostname(spcl.c_host, sizeof(spcl.c_host));
402 spcl.c_level = level - '0';
403 spcl.c_type = TS_TAPE;
404
405 if ((diskfd = open(disk, O_RDONLY)) == -1) {
406 msg("Cannot open %s\n", disk);
407 exit(X_STARTUP);
408 }
409 if (ioctl(diskfd, DIOCGDINFO, (char *)&lab) == -1)
410 err(1, "ioctl (DIOCGDINFO)");
411
412 if (memcmp(lab.d_uid, &zero_uid, sizeof(lab.d_uid)) != 0) {
413 if (asprintf(&duid,
414 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c",
415 lab.d_uid[0], lab.d_uid[1], lab.d_uid[2], lab.d_uid[3],
416 lab.d_uid[4], lab.d_uid[5], lab.d_uid[6], lab.d_uid[7],
417 disk[strlen(disk)-1]) == -1) {
418 msg("Cannot malloc duid\n");
419 exit(X_STARTUP);
420 }
421 }
422 if (!Tflag)
423 getdumptime(); /* /etc/dumpdates snarfed */
424
425 t = (time_t)spcl.c_date;
426 ct = ctime(&t);
427 msg("Date of this level %c dump: %s", level,
428 t == 0 ? "the epoch\n" : ct ? ct : "?\n");
429 t = (time_t)spcl.c_ddate;
430 ct = ctime(&t);
431 msg("Date of last level %c dump: %s", lastlevel,
432 t == 0 ? "the epoch\n" : ct ? ct : "?\n");
433 msg("Dumping %s ", disk);
434 if (mount_point != NULL)
435 msgtail("(%s) ", mount_point);
436 if (host)
437 msgtail("to %s on host %s\n", tape, host);
438 else
439 msgtail("to %s\n", tape);
440
441 if (ioctl(diskfd, DIOCGPDINFO, (char *)&lab) == -1)
442 err(1, "ioctl (DIOCGPDINFO)");
443 sync();
444 sblock = (struct fs *)sblock_buf;
445 for (i = 0; sblock_try[i] != -1; i++) {
446 ssize_t n = pread(diskfd, sblock, SBLOCKSIZE,
447 (off_t)sblock_try[i]);
448 if (n == SBLOCKSIZE && (sblock->fs_magic == FS_UFS1_MAGIC ||
449 (sblock->fs_magic == FS_UFS2_MAGIC &&
450 sblock->fs_sblockloc == sblock_try[i])) &&
451 sblock->fs_bsize <= MAXBSIZE &&
452 sblock->fs_bsize >= sizeof(struct fs))
453 break;
454 }
455 if (sblock_try[i] == -1)
456 quit("Cannot find filesystem superblock\n");
457 tp_bshift = ffs(TP_BSIZE) - 1;
458 if (TP_BSIZE != (1 << tp_bshift))
459 quit("TP_BSIZE (%d) is not a power of 2\n", TP_BSIZE);
460 if (sblock->fs_magic == FS_UFS2_MAGIC ||
461 sblock->fs_inodefmt >= FS_44INODEFMT)
462 spcl.c_flags |= DR_NEWINODEFMT;
463 maxino = (ino_t)sblock->fs_ipg * sblock->fs_ncg;
464 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
465 usedinomap = calloc((unsigned) mapsize, sizeof(char));
466 dumpdirmap = calloc((unsigned) mapsize, sizeof(char));
467 dumpinomap = calloc((unsigned) mapsize, sizeof(char));
468 if (usedinomap == NULL || dumpdirmap == NULL || dumpinomap == NULL)
469 quit("Failed to allocate tables");
470
471 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
472
473 nonodump = spcl.c_level < honorlevel;
474
475 (void)signal(SIGINFO, statussig);
476
477 msg("mapping (Pass I) [regular files]\n");
478 anydirskipped = mapfiles(maxino, &tapesize, toplevel,
479 (dirlist ? argv : NULL));
480
481 msg("mapping (Pass II) [directories]\n");
482 while (anydirskipped) {
483 anydirskipped = mapdirs(maxino, &tapesize);
484 }
485
486 if (pipeout || unlimited) {
487 tapesize += 10; /* 10 trailer blocks */
488 msg("estimated %lld tape blocks.\n", tapesize);
489 } else {
490 double fetapes;
491
492 if (blocksperfile)
493 fetapes = (double) tapesize / blocksperfile;
494 else if (cartridge) {
495 /* Estimate number of tapes, assuming streaming stops at
496 the end of each block written, and not in mid-block.
497 Assume no erroneous blocks; this can be compensated
498 for with an artificially low tape size. */
499 fetapes =
500 ( tapesize /* blocks */
501 * TP_BSIZE /* bytes/block */
502 * (1.0/density) /* 0.1" / byte */
503 +
504 tapesize /* blocks */
505 * (1.0/ntrec) /* streaming-stops per block */
506 * 15.48 /* 0.1" / streaming-stop */
507 ) * (1.0 / tsize ); /* tape / 0.1" */
508 } else {
509 /* Estimate number of tapes, for old fashioned 9-track
510 tape */
511 int tenthsperirg = (density == 625) ? 3 : 7;
512 fetapes =
513 ( tapesize /* blocks */
514 * TP_BSIZE /* bytes / block */
515 * (1.0/density) /* 0.1" / byte */
516 +
517 tapesize /* blocks */
518 * (1.0/ntrec) /* IRG's / block */
519 * tenthsperirg /* 0.1" / IRG */
520 ) * (1.0 / tsize ); /* tape / 0.1" */
521 }
522 etapes = fetapes; /* truncating assignment */
523 etapes++;
524 /* count the dumped inodes map on each additional tape */
525 tapesize += (etapes - 1) *
526 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
527 tapesize += etapes + 10; /* headers + 10 trailer blks */
528 msg("estimated %lld tape blocks on %3.2f tape(s).\n",
529 tapesize, fetapes);
530 }
531
532 /*
533 * Exit if user wants an estimate of blocks and # of tapes only.
534 */
535 if (just_estimate)
536 exit(X_FINOK);
537
538 /*
539 * Allocate tape buffer.
540 */
541 if (!alloctape())
542 quit("can't allocate tape buffers - try a smaller blocking factor.\n");
543
544 startnewtape(1);
545 (void)time(&tstart_writing);
546 xferrate = 0;
547 dumpmap(usedinomap, TS_CLRI, maxino - 1);
548
549 msg("dumping (Pass III) [directories]\n");
550 dirty = 0; /* XXX just to get gcc to shut up */
551 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
552 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
553 dirty = *map++;
554 else
555 dirty >>= 1;
556 if ((dirty & 1) == 0)
557 continue;
558 /*
559 * Skip directory inodes deleted and maybe reallocated
560 */
561 dp = getino(ino, &mode);
562 if (mode != IFDIR)
563 continue;
564 (void)dumpino(dp, ino);
565 }
566
567 msg("dumping (Pass IV) [regular files]\n");
568 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
569 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
570 dirty = *map++;
571 else
572 dirty >>= 1;
573 if ((dirty & 1) == 0)
574 continue;
575 /*
576 * Skip inodes deleted and reallocated as directories.
577 */
578 dp = getino(ino, &mode);
579 if (mode == IFDIR)
580 continue;
581 (void)dumpino(dp, ino);
582 }
583
584 spcl.c_type = TS_END;
585 for (i = 0; i < ntrec; i++)
586 writeheader(maxino - 1);
587 if (pipeout)
588 msg("%lld tape blocks\n", spcl.c_tapea);
589 else
590 msg("%lld tape blocks on %d volume%s\n",
591 spcl.c_tapea, spcl.c_volume,
592 (spcl.c_volume == 1) ? "" : "s");
593 t = (time_t)spcl.c_date;
594 ct = ctime(&t);
595 msg("Date of this level %c dump: %s", level,
596 t == 0 ? "the epoch\n" : ct ? ct : "?\n");
597 t = do_stats();
598 ct = ctime(&t);
599 msg("Date this dump completed: %s", ct ? ct : "?\n");
600 msg("Average transfer rate: %ld KB/s\n", xferrate / tapeno);
601 putdumptime();
602 trewind();
603 broadcast("DUMP IS DONE!\7\7\n");
604 msg("DUMP IS DONE\n");
605 Exit(X_FINOK);
606 /* NOTREACHED */
607 }
608
609 static void
usage(void)610 usage(void)
611 {
612 extern char *__progname;
613
614 (void)fprintf(stderr, "usage: %s [-0123456789acnSuWw] [-B records] "
615 "[-b blocksize] [-d density]\n"
616 "\t[-f file] [-h level] [-s feet] "
617 "[-T date] files-to-dump\n",
618 __progname);
619 exit(X_STARTUP);
620 }
621
622 /*
623 * Pick up a numeric argument. It must be nonnegative and in the given
624 * range (except that a vmax of 0 means unlimited).
625 */
626 static long long
numarg(char * meaning,long long vmin,long long vmax)627 numarg(char *meaning, long long vmin, long long vmax)
628 {
629 long long val;
630 const char *errstr;
631
632 if (vmax == 0)
633 vmax = LLONG_MAX;
634 val = strtonum(optarg, vmin, vmax, &errstr);
635 if (errstr)
636 errx(X_STARTUP, "%s is %s [%lld - %lld]",
637 meaning, errstr, vmin, vmax);
638
639 return (val);
640 }
641
642 void
sig(int signo)643 sig(int signo)
644 {
645 switch(signo) {
646 case SIGALRM:
647 case SIGHUP:
648 case SIGTERM:
649 /* XXX signal race */
650 if (pipeout)
651 quit("Signal on pipe: cannot recover\n");
652 msg("Rewriting attempted as response to unknown signal.\n");
653 (void)fflush(stderr);
654 (void)fflush(stdout);
655 close_rewind();
656 exit(X_REWRITE);
657 /* NOTREACHED */
658 }
659 }
660
661 char *
rawname(char * cp)662 rawname(char *cp)
663 {
664 static char rawbuf[PATH_MAX];
665 char *dp = strrchr(cp, '/');
666 char *prefix;
667
668 if (dp == NULL)
669 return (NULL);
670 prefix = dp[1] == 'r' ? "" : "r";
671 *dp = '\0';
672 (void)snprintf(rawbuf, sizeof(rawbuf), "%s/%s%s", cp, prefix, dp + 1);
673 *dp = '/';
674 return (rawbuf);
675 }
676
677 char *
getduid(char * path)678 getduid(char *path)
679 {
680 int fd;
681 struct disklabel lab;
682 u_int64_t zero_uid = 0;
683 char *duid;
684
685 if ((fd = opendev(path, O_RDONLY | O_NOFOLLOW, 0, NULL)) >= 0) {
686 if (ioctl(fd, DIOCGDINFO, (char *)&lab) == -1) {
687 close(fd);
688 warn("ioctl(DIOCGDINFO)");
689 return (NULL);
690 }
691 close(fd);
692
693 if (memcmp(lab.d_uid, &zero_uid, sizeof(lab.d_uid)) != 0) {
694 if (asprintf(&duid,
695 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c",
696 lab.d_uid[0], lab.d_uid[1], lab.d_uid[2],
697 lab.d_uid[3], lab.d_uid[4], lab.d_uid[5],
698 lab.d_uid[6], lab.d_uid[7],
699 path[strlen(path)-1]) == -1) {
700 warn("Cannot malloc duid");
701 return (NULL);
702 }
703 return (duid);
704 }
705 }
706
707 return (NULL);
708 }
709
710 /*
711 * obsolete --
712 * Change set of key letters and ordered arguments into something
713 * getopt(3) will like.
714 */
715 static void
obsolete(int * argcp,char ** argvp[])716 obsolete(int *argcp, char **argvp[])
717 {
718 int argc, flags;
719 char *ap, **argv, *flagsp, **nargv, *p;
720 size_t len;
721
722 /* Setup. */
723 argv = *argvp;
724 argc = *argcp;
725
726 /* Return if no args or first argument has leading dash or a slash. */
727 ap = argv[1];
728 if (argc == 1 || *ap == '-' || strchr(ap, '/') != NULL)
729 return;
730
731 /* Allocate space for new arguments. */
732 if ((*argvp = nargv = calloc(argc + 1, sizeof(char *))) == NULL ||
733 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
734 err(1, NULL);
735
736 *nargv++ = *argv;
737 argv += 2;
738
739 for (flags = 0; *ap; ++ap) {
740 switch (*ap) {
741 case 'B':
742 case 'b':
743 case 'd':
744 case 'f':
745 case 'h':
746 case 's':
747 case 'T':
748 if (*argv == NULL) {
749 warnx("option requires an argument -- %c", *ap);
750 usage();
751 }
752 len = 2 + strlen(*argv) + 1;
753 if ((nargv[0] = malloc(len)) == NULL)
754 err(1, NULL);
755 nargv[0][0] = '-';
756 nargv[0][1] = *ap;
757 (void)strlcpy(&nargv[0][2], *argv, len - 2);
758 ++argv;
759 ++nargv;
760 break;
761 default:
762 if (!flags) {
763 *p++ = '-';
764 flags = 1;
765 }
766 *p++ = *ap;
767 break;
768 }
769 }
770
771 /* Terminate flags, or toss the buffer we did not use. */
772 if (flags) {
773 *p = '\0';
774 *nargv++ = flagsp;
775 } else
776 free(flagsp);
777
778 /* Copy remaining arguments. */
779 while ((*nargv++ = *argv++))
780 continue;
781
782 /* Update argument count. */
783 *argcp = nargv - *argvp - 1;
784 }
785