fsck.c revision 1.51 1 /* $NetBSD: fsck.c,v 1.51 2012/04/07 04:52:20 christos Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christos Zoulas. All rights reserved.
5 * Copyright (c) 1980, 1989, 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 * From: @(#)mount.c 8.19 (Berkeley) 4/19/94
33 * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
34 *
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: fsck.c,v 1.51 2012/04/07 04:52:20 christos Exp $");
40 #endif /* not lint */
41
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/queue.h>
45 #include <sys/wait.h>
46 #define FSTYPENAMES
47 #define FSCKNAMES
48 #include <sys/disk.h>
49 #include <sys/disklabel.h>
50 #include <sys/ioctl.h>
51
52 #include <err.h>
53 #include <errno.h>
54 #include <fstab.h>
55 #include <fcntl.h>
56 #include <paths.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <util.h>
63
64 #include "pathnames.h"
65 #include "fsutil.h"
66 #include "exitvalues.h"
67
68 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
69
70 TAILQ_HEAD(fstypelist, entry) opthead, selhead, omhead;
71
72 struct entry {
73 char *type;
74 char *options;
75 TAILQ_ENTRY(entry) entries;
76 };
77
78 static int maxrun = 0;
79 static char *options = NULL;
80 static int flags = 0;
81
82 static int checkfs(const char *, const char *, const char *, void *, pid_t *);
83 static int selected(const char *);
84 static int omitted(const char *);
85 static void addoption(char *);
86 static const char *getoptions(const char *);
87 static void addentry(struct fstypelist *, const char *, const char *);
88 static void maketypelist(char *);
89 static void catopt(char **, const char *);
90 static void mangle(char *, int *, const char ** volatile *, int *);
91 static const char *getfslab(const char *);
92 __dead static void usage(void);
93 static void *isok(struct fstab *);
94
95 int
96 main(int argc, char *argv[])
97 {
98 struct fstab *fs;
99 int i, rval;
100 const char *vfstype = NULL;
101 char globopt[3];
102 int ret = FSCK_EXIT_OK;
103 char buf[MAXPATHLEN];
104
105 globopt[0] = '-';
106 globopt[2] = '\0';
107
108 TAILQ_INIT(&selhead);
109 TAILQ_INIT(&opthead);
110 TAILQ_INIT(&omhead);
111
112 while ((i = getopt(argc, argv, "dfl:nPpqT:t:vx:y")) != -1) {
113 switch (i) {
114 case 'd':
115 flags |= CHECK_DEBUG;
116 continue;
117
118 case 'f':
119 flags |= CHECK_FORCE;
120 break;
121
122 case 'n':
123 flags |= CHECK_NOFIX;
124 break;
125
126 case 'p':
127 flags |= CHECK_PREEN;
128 break;
129
130 case 'P':
131 flags |= CHECK_PROGRESS;
132 break;
133
134 case 'q':
135 break;
136
137 case 'l':
138 maxrun = atoi(optarg);
139 continue;
140
141 case 'T':
142 if (*optarg)
143 addoption(optarg);
144 continue;
145
146 case 't':
147 if (TAILQ_FIRST(&selhead) != NULL)
148 errx(1, "only one -t option may be specified.");
149
150 maketypelist(optarg);
151 vfstype = optarg;
152 continue;
153
154 case 'v':
155 flags |= CHECK_VERBOSE;
156 continue;
157
158 case 'x':
159 addentry(&omhead, optarg, "");
160 continue;
161
162 case 'y':
163 break;
164
165 case '?':
166 default:
167 usage();
168 /* NOTREACHED */
169 }
170
171 /* Pass option to fsck_xxxfs */
172 globopt[1] = i;
173 catopt(&options, globopt);
174 }
175
176 /* Don't do progress meters if we're debugging. */
177 if (flags & CHECK_DEBUG)
178 flags &= ~CHECK_PROGRESS;
179
180 /*
181 * If progress meters are being used, force max parallel to 1
182 * so the progress meter outputs don't interfere with one another.
183 */
184 if (flags & CHECK_PROGRESS)
185 maxrun = 1;
186
187 argc -= optind;
188 argv += optind;
189
190 if (argc == 0)
191 return checkfstab(flags, maxrun, isok, checkfs);
192
193 #define BADTYPE(type) \
194 (strcmp(type, FSTAB_RO) && \
195 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
196
197
198 for (; argc--; argv++) {
199 const char *spec, *type, *cp;
200 char device[MAXPATHLEN];
201
202 spec = *argv;
203 cp = strrchr(spec, '/');
204 if (cp == 0) {
205 (void)snprintf(device, sizeof(device), "%s%s",
206 _PATH_DEV, spec);
207 spec = device;
208 }
209 if ((fs = getfsfile(spec)) == NULL &&
210 (fs = getfsspec(spec)) == NULL) {
211 if (vfstype == NULL)
212 vfstype = getfslab(spec);
213 type = vfstype;
214 }
215 else {
216 spec = getfsspecname(buf, sizeof(buf), fs->fs_spec);
217 if (spec == NULL)
218 err(FSCK_EXIT_CHECK_FAILED, "%s", buf);
219 type = fs->fs_vfstype;
220 if (BADTYPE(fs->fs_type))
221 errx(FSCK_EXIT_CHECK_FAILED,
222 "%s has unknown file system type.",
223 spec);
224 }
225
226 rval = checkfs(type, blockcheck(spec), *argv, NULL, NULL);
227 if (rval > ret)
228 ret = rval;
229 }
230
231 return ret;
232 }
233
234
235 static void *
236 isok(struct fstab *fs)
237 {
238
239 if (fs->fs_passno == 0)
240 return NULL;
241
242 if (BADTYPE(fs->fs_type))
243 return NULL;
244
245 if (!selected(fs->fs_vfstype))
246 return NULL;
247
248 if (omitted(fs->fs_file))
249 return NULL;
250
251 return fs;
252 }
253
254
255 static int
256 checkfs(const char *vfst, const char *spec, const char *mntpt, void *auxarg,
257 pid_t *pidp)
258 {
259 /* List of directories containing fsck_xxx subcommands. */
260 static const char *edirs[] = {
261 #ifdef RESCUEDIR
262 RESCUEDIR,
263 #endif
264 _PATH_SBIN,
265 _PATH_USRSBIN,
266 NULL
267 };
268 const char ** volatile argv, **edir;
269 const char * volatile vfstype = vfst;
270 pid_t pid;
271 int argc, i, status, maxargc;
272 char *optb;
273 char *volatile optbuf;
274 char execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
275 const char *extra = getoptions(vfstype);
276
277 if (!strcmp(vfstype, "ufs"))
278 vfstype = MOUNT_UFS;
279
280 optb = NULL;
281 if (options)
282 catopt(&optb, options);
283 if (extra)
284 catopt(&optb, extra);
285 optbuf = optb;
286
287 maxargc = 64;
288 argv = emalloc(sizeof(char *) * maxargc);
289
290 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
291 argc = 0;
292 argv[argc++] = execbase;
293 if (optbuf)
294 mangle(optbuf, &argc, &argv, &maxargc);
295 argv[argc++] = spec;
296 argv[argc] = NULL;
297
298 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
299 (void)printf("start %s %swait", mntpt,
300 pidp ? "no" : "");
301 for (i = 0; i < argc; i++)
302 (void)printf(" %s", argv[i]);
303 (void)printf("\n");
304 }
305
306 switch (pid = vfork()) {
307 case -1: /* Error. */
308 warn("vfork");
309 if (optbuf)
310 free(optbuf);
311 free(argv);
312 return FSCK_EXIT_CHECK_FAILED;
313
314 case 0: /* Child. */
315 if ((flags & CHECK_FORCE) == 0) {
316 struct statvfs sfs;
317
318 /*
319 * if mntpt is a mountpoint of a mounted file
320 * system and it's mounted read-write, skip it
321 * unless -f is given.
322 */
323 if ((statvfs(mntpt, &sfs) == 0) &&
324 (strcmp(mntpt, sfs.f_mntonname) == 0) &&
325 ((sfs.f_flag & MNT_RDONLY) == 0)) {
326 printf(
327 "%s: file system is mounted read-write on %s; not checking\n",
328 spec, mntpt);
329 if ((flags & CHECK_PREEN) && auxarg != NULL)
330 _exit(FSCK_EXIT_OK); /* fsck -p */
331 else
332 _exit(FSCK_EXIT_CHECK_FAILED); /* fsck [[-p] ...] */
333 }
334 }
335
336 if (flags & CHECK_DEBUG)
337 _exit(FSCK_EXIT_OK);
338
339 /* Go find an executable. */
340 edir = edirs;
341 do {
342 (void)snprintf(execname,
343 sizeof(execname), "%s/%s", *edir, execbase);
344 execv(execname, (char * const *)__UNCONST(argv));
345 if (errno != ENOENT) {
346 if (spec)
347 warn("exec %s for %s", execname, spec);
348 else
349 warn("exec %s", execname);
350 }
351 } while (*++edir != NULL);
352
353 if (errno == ENOENT) {
354 if (spec)
355 warn("exec %s for %s", execname, spec);
356 else
357 warn("exec %s", execname);
358 }
359 _exit(FSCK_EXIT_CHECK_FAILED);
360 /* NOTREACHED */
361
362 default: /* Parent. */
363 if (optbuf)
364 free(optbuf);
365 free(argv);
366
367 if (pidp) {
368 *pidp = pid;
369 return FSCK_EXIT_OK;
370 }
371
372 if (waitpid(pid, &status, 0) < 0) {
373 warn("waitpid");
374 return FSCK_EXIT_CHECK_FAILED;
375 }
376
377 if (WIFEXITED(status)) {
378 if (WEXITSTATUS(status) != 0)
379 return WEXITSTATUS(status);
380 }
381 else if (WIFSIGNALED(status)) {
382 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
383 return FSCK_EXIT_CHECK_FAILED;
384 }
385 break;
386 }
387
388 return FSCK_EXIT_OK;
389 }
390
391
392 static int
393 selected(const char *type)
394 {
395 struct entry *e;
396
397 /* If no type specified, it's always selected. */
398 TAILQ_FOREACH(e, &selhead, entries)
399 if (!strcmp(e->type, type))
400 return which == IN_LIST ? 1 : 0;
401
402 return which == IN_LIST ? 0 : 1;
403 }
404
405
406 static int
407 omitted(const char *mountedon)
408 {
409 struct entry *e;
410
411 /* If no type specified, it's always selected. */
412 TAILQ_FOREACH(e, &omhead, entries)
413 if (!strcmp(e->type, mountedon))
414 return 1;
415
416 return 0;
417 }
418
419
420 static const char *
421 getoptions(const char *type)
422 {
423 struct entry *e;
424
425 TAILQ_FOREACH(e, &opthead, entries)
426 if (!strcmp(e->type, type))
427 return e->options;
428 return "";
429 }
430
431
432 static void
433 addoption(char *optstr)
434 {
435 char *newoptions;
436 struct entry *e;
437
438 if ((newoptions = strchr(optstr, ':')) == NULL)
439 errx(1, "Invalid option string");
440
441 *newoptions++ = '\0';
442
443 TAILQ_FOREACH(e, &opthead, entries)
444 if (!strcmp(e->type, optstr)) {
445 catopt(&e->options, newoptions);
446 return;
447 }
448 addentry(&opthead, optstr, newoptions);
449 }
450
451
452 static void
453 addentry(struct fstypelist *list, const char *type, const char *opts)
454 {
455 struct entry *e;
456
457 e = emalloc(sizeof(struct entry));
458 e->type = estrdup(type);
459 e->options = estrdup(opts);
460 TAILQ_INSERT_TAIL(list, e, entries);
461 }
462
463
464 static void
465 maketypelist(char *fslist)
466 {
467 char *ptr;
468
469 if ((fslist == NULL) || (fslist[0] == '\0'))
470 errx(1, "empty type list");
471
472 if (fslist[0] == 'n' && fslist[1] == 'o') {
473 fslist += 2;
474 which = NOT_IN_LIST;
475 }
476 else
477 which = IN_LIST;
478
479 while ((ptr = strsep(&fslist, ",")) != NULL)
480 addentry(&selhead, ptr, "");
481
482 }
483
484
485 static void
486 catopt(char **sp, const char *o)
487 {
488 char *s;
489 size_t i, j;
490
491 s = *sp;
492 if (s) {
493 i = strlen(s);
494 j = i + 1 + strlen(o) + 1;
495 s = erealloc(s, j);
496 (void)snprintf(s + i, j, ",%s", o);
497 } else
498 s = estrdup(o);
499 *sp = s;
500 }
501
502
503 static void
504 mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp)
505 {
506 char *p, *s;
507 int argc, maxargc;
508 const char **argv;
509
510 argc = *argcp;
511 argv = *argvp;
512 maxargc = *maxargcp;
513
514 for (s = opts; (p = strsep(&s, ",")) != NULL;) {
515 /* Always leave space for one more argument and the NULL. */
516 if (argc >= maxargc - 3) {
517 maxargc <<= 1;
518 argv = erealloc(argv, maxargc * sizeof(char *));
519 }
520 if (*p != '\0') {
521 if (*p == '-') {
522 argv[argc++] = p;
523 p = strchr(p, '=');
524 if (p) {
525 *p = '\0';
526 argv[argc++] = p+1;
527 }
528 } else {
529 argv[argc++] = "-o";
530 argv[argc++] = p;
531 }
532 }
533 }
534
535 *argcp = argc;
536 *argvp = argv;
537 *maxargcp = maxargc;
538 }
539
540 static const char *
541 getfslab(const char *str)
542 {
543 static struct dkwedge_info dkw;
544 struct disklabel dl;
545 int fd;
546 char p;
547 const char *vfstype;
548 u_char t;
549
550 /* deduce the file system type from the disk label */
551 if ((fd = open(str, O_RDONLY)) == -1)
552 err(1, "cannot open `%s'", str);
553
554 /* First check to see if it's a wedge. */
555 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
556 /* Yup, this is easy. */
557 (void) close(fd);
558 return (dkw.dkw_ptype);
559 }
560
561 if (ioctl(fd, DIOCGDINFO, &dl) == -1)
562 err(1, "cannot get disklabel for `%s'", str);
563
564 (void) close(fd);
565
566 p = str[strlen(str) - 1];
567
568 if ((p - 'a') >= dl.d_npartitions)
569 errx(1, "partition `%s' is not defined on disk", str);
570
571 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
572 errx(1, "partition `%s' is not of a legal vfstype",
573 str);
574
575 if ((vfstype = fscknames[t]) == NULL)
576 errx(1, "vfstype `%s' on partition `%s' is not supported",
577 fstypenames[t], str);
578
579 return vfstype;
580 }
581
582
583 static void
584 usage(void)
585 {
586 static const char common[] =
587 "[-dfnPpqvy] [-x excludemount] [-l maxparallel] [-T fstype:fsoptions]\n\t\t[-t fstype]";
588
589 (void)fprintf(stderr, "usage: %s %s [special|node]...\n",
590 getprogname(), common);
591 exit(FSCK_EXIT_USAGE);
592 }
593