Deleted Added
sdiff udiff text old ( 241806 ) new ( 241807 )
full compact
1/* $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc 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 * $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sbin/fsck/fsck.c 241806 2012-10-21 12:01:11Z uqs $");
39
40#include <sys/param.h>
41#include <sys/mount.h>
42#include <sys/queue.h>
43#include <sys/wait.h>
44#define FSTYPENAMES
45#include <sys/disklabel.h>
46#include <sys/ioctl.h>
47
48#include <ctype.h>
49#include <err.h>
50#include <errno.h>
51#include <fstab.h>
52#include <fcntl.h>
53#include <paths.h>
54#include <signal.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
59
60#include "fsutil.h"
61
62static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
63
64static TAILQ_HEAD(fstypelist, entry) opthead, selhead;
65
66struct entry {
67 char *type;
68 char *options;
69 TAILQ_ENTRY(entry) entries;
70};
71
72static char *options = NULL;
73static int flags = 0;
74static int forceflag = 0;
75
76static int checkfs(const char *, const char *, const char *, char *, pid_t *);
77static int selected(const char *);
78static void addoption(char *);
79static const char *getoptions(const char *);
80static void addentry(struct fstypelist *, const char *, const char *);
81static void maketypelist(char *);
82static void catopt(char **, const char *);
83static void mangle(char *, int *, const char ***, int *);
84static const char *getfslab(const char *);
85static void usage(void) __dead2;
86static int isok(struct fstab *);
87
88int
89main(int argc, char *argv[])
90{
91 struct fstab *fs;
92 int i, rval = 0;
93 const char *vfstype = NULL;
94 char globopt[3];
95 const char *etc_fstab;
96
97 globopt[0] = '-';
98 globopt[2] = '\0';
99
100 TAILQ_INIT(&selhead);
101 TAILQ_INIT(&opthead);
102
103 etc_fstab = NULL;
104 while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1)
105 switch (i) {
106 case 'B':
107 if (flags & CHECK_BACKGRD)
108 errx(1, "Cannot specify -B and -F.");
109 flags |= DO_BACKGRD;
110 break;
111
112 case 'd':
113 flags |= CHECK_DEBUG;
114 break;
115
116 case 'v':
117 flags |= CHECK_VERBOSE;
118 break;
119
120 case 'F':
121 if (flags & DO_BACKGRD)
122 errx(1, "Cannot specify -B and -F.");
123 flags |= CHECK_BACKGRD;
124 break;
125
126 case 'p':
127 flags |= CHECK_PREEN;
128 /*FALLTHROUGH*/
129 case 'C':
130 flags |= CHECK_CLEAN;
131 /*FALLTHROUGH*/
132 case 'n':
133 case 'y':
134 globopt[1] = i;
135 catopt(&options, globopt);
136 break;
137
138 case 'f':
139 forceflag = 1;
140 globopt[1] = i;
141 catopt(&options, globopt);
142 break;
143
144 case 'l':
145 warnx("Ignoring obsolete -l option\n");
146 break;
147
148 case 'T':
149 if (*optarg)
150 addoption(optarg);
151 break;
152
153 case 't':
154 if (!TAILQ_EMPTY(&selhead))
155 errx(1, "only one -t option may be specified.");
156
157 maketypelist(optarg);
158 vfstype = optarg;
159 break;
160
161 case 'c':
162 etc_fstab = optarg;
163 break;
164
165 case '?':
166 default:
167 usage();
168 /* NOTREACHED */
169 }
170
171 argc -= optind;
172 argv += optind;
173
174 if (etc_fstab != NULL)
175 setfstab(etc_fstab);
176
177 if (argc == 0)
178 return checkfstab(flags, isok, checkfs);
179
180#define BADTYPE(type) \
181 (strcmp(type, FSTAB_RO) && \
182 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
183
184
185 for (; argc--; argv++) {
186 const char *spec, *mntpt, *type, *cp;
187 char device[MAXPATHLEN];
188 struct statfs *mntp;
189
190 spec = *argv;
191 cp = strrchr(spec, '/');
192 if (cp == 0) {
193 (void)snprintf(device, sizeof(device), "%s%s",
194 _PATH_DEV, spec);
195 spec = device;
196 }
197 mntp = getmntpt(spec);
198 if (mntp != NULL) {
199 spec = mntp->f_mntfromname;
200 mntpt = mntp->f_mntonname;
201 }
202 if ((fs = getfsfile(spec)) == NULL &&
203 (fs = getfsspec(spec)) == NULL) {
204 if (vfstype == NULL)
205 vfstype = getfslab(spec);
206 if (vfstype == NULL)
207 errx(1, "Could not determine filesystem type");
208 type = vfstype;
209 devcheck(spec);
210 } else {
211 spec = fs->fs_spec;
212 type = fs->fs_vfstype;
213 mntpt = fs->fs_file;
214 if (BADTYPE(fs->fs_type))
215 errx(1, "%s has unknown file system type.",
216 spec);
217 }
218 if ((flags & CHECK_BACKGRD) &&
219 checkfs(type, spec, mntpt, "-F", NULL) == 0) {
220 printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
221 continue;
222 }
223 if ((flags & DO_BACKGRD) && forceflag == 0 &&
224 checkfs(type, spec, mntpt, "-F", NULL) != 0)
225 continue;
226
227 rval |= checkfs(type, spec, mntpt, NULL, NULL);
228 }
229
230 return rval;
231}
232
233
234static int
235isok(struct fstab *fs)
236{
237 int i;
238
239 if (fs->fs_passno == 0)
240 return (0);
241 if (BADTYPE(fs->fs_type))
242 return (0);
243 if (!selected(fs->fs_vfstype))
244 return (0);
245 /*
246 * If the -B flag has been given, then process the needed
247 * background checks. Background checks cannot be run on
248 * file systems that will be mounted read-only or that were
249 * not mounted at boot time (typically those marked `noauto').
250 * If these basic tests are passed, check with the file system
251 * itself to see if it is willing to do background checking
252 * by invoking its check program with the -F flag.
253 */
254 if (flags & DO_BACKGRD) {
255 if (!strcmp(fs->fs_type, FSTAB_RO))
256 return (0);
257 if (getmntpt(fs->fs_spec) == NULL)
258 return (0);
259 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
260 return (0);
261 return (1);
262 }
263 /*
264 * If the -F flag has been given, then consider deferring the
265 * check to background. Background checks cannot be run on
266 * file systems that will be mounted read-only or that will
267 * not be mounted at boot time (e.g., marked `noauto'). If
268 * these basic tests are passed, check with the file system
269 * itself to see if it is willing to defer to background
270 * checking by invoking its check program with the -F flag.
271 */
272 if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
273 return (1);
274 for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
275 if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
276 break;
277 if (i >= 0)
278 return (1);
279 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
280 return (1);
281 printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
282 return (0);
283}
284
285
286static int
287checkfs(const char *pvfstype, const char *spec, const char *mntpt,
288 char *auxopt, pid_t *pidp)
289{
290 const char **argv;
291 pid_t pid;
292 int argc, i, status, maxargc;
293 char *optbuf, execbase[MAXPATHLEN];
294 char *vfstype = NULL;
295 const char *extra = NULL;
296
297#ifdef __GNUC__
298 /* Avoid vfork clobbering */
299 (void) &optbuf;
300 (void) &vfstype;
301#endif
302 /*
303 * We convert the vfstype to lowercase and any spaces to underscores
304 * to not confuse the issue
305 *
306 * XXX This is a kludge to make automatic filesystem type guessing
307 * from the disklabel work for "4.2BSD" filesystems. It does a
308 * very limited subset of transliteration to a normalised form of
309 * filesystem name, and we do not seem to enforce a filesystem
310 * name character set.
311 */
312 vfstype = strdup(pvfstype);
313 if (vfstype == NULL)
314 perr("strdup(pvfstype)");
315 for (i = 0; i < strlen(vfstype); i++) {
316 vfstype[i] = tolower(vfstype[i]);
317 if (vfstype[i] == ' ')
318 vfstype[i] = '_';
319 }
320
321 extra = getoptions(vfstype);
322 optbuf = NULL;
323 if (options)
324 catopt(&optbuf, options);
325 if (extra)
326 catopt(&optbuf, extra);
327 if (auxopt)
328 catopt(&optbuf, auxopt);
329 else if (flags & DO_BACKGRD)
330 catopt(&optbuf, "-B");
331
332 maxargc = 64;
333 argv = emalloc(sizeof(char *) * maxargc);
334
335 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
336 argc = 0;
337 argv[argc++] = execbase;
338 if (optbuf)
339 mangle(optbuf, &argc, &argv, &maxargc);
340 argv[argc++] = spec;
341 argv[argc] = NULL;
342
343 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
344 (void)printf("start %s %swait", mntpt,
345 pidp ? "no" : "");
346 for (i = 0; i < argc; i++)
347 (void)printf(" %s", argv[i]);
348 (void)printf("\n");
349 }
350
351 switch (pid = vfork()) {
352 case -1: /* Error. */
353 warn("vfork");
354 if (optbuf)
355 free(optbuf);
356 free(vfstype);
357 return (1);
358
359 case 0: /* Child. */
360 if ((flags & CHECK_DEBUG) && auxopt == NULL)
361 _exit(0);
362
363 /* Go find an executable. */
364 execvP(execbase, _PATH_SYSPATH, (char * const *)argv);
365 if (spec)
366 warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
367 else
368 warn("exec %s in %s", execbase, _PATH_SYSPATH);
369 _exit(1);
370 /* NOTREACHED */
371
372 default: /* Parent. */
373 if (optbuf)
374 free(optbuf);
375
376 free(vfstype);
377
378 if (pidp) {
379 *pidp = pid;
380 return 0;
381 }
382
383 if (waitpid(pid, &status, 0) < 0) {
384 warn("waitpid");
385 return (1);
386 }
387
388 if (WIFEXITED(status)) {
389 if (WEXITSTATUS(status) != 0)
390 return (WEXITSTATUS(status));
391 }
392 else if (WIFSIGNALED(status)) {
393 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
394 return (1);
395 }
396 break;
397 }
398
399 return (0);
400}
401
402
403static int
404selected(const char *type)
405{
406 struct entry *e;
407
408 /* If no type specified, it's always selected. */
409 TAILQ_FOREACH(e, &selhead, entries)
410 if (!strncmp(e->type, type, MFSNAMELEN))
411 return which == IN_LIST ? 1 : 0;
412
413 return which == IN_LIST ? 0 : 1;
414}
415
416
417static const char *
418getoptions(const char *type)
419{
420 struct entry *e;
421
422 TAILQ_FOREACH(e, &opthead, entries)
423 if (!strncmp(e->type, type, MFSNAMELEN))
424 return e->options;
425 return "";
426}
427
428
429static void
430addoption(char *optstr)
431{
432 char *newoptions;
433 struct entry *e;
434
435 if ((newoptions = strchr(optstr, ':')) == NULL)
436 errx(1, "Invalid option string");
437
438 *newoptions++ = '\0';
439
440 TAILQ_FOREACH(e, &opthead, entries)
441 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
442 catopt(&e->options, newoptions);
443 return;
444 }
445 addentry(&opthead, optstr, newoptions);
446}
447
448
449static void
450addentry(struct fstypelist *list, const char *type, const char *opts)
451{
452 struct entry *e;
453
454 e = emalloc(sizeof(struct entry));
455 e->type = estrdup(type);
456 e->options = estrdup(opts);
457 TAILQ_INSERT_TAIL(list, e, entries);
458}
459
460
461static void
462maketypelist(char *fslist)
463{
464 char *ptr;
465
466 if ((fslist == NULL) || (fslist[0] == '\0'))
467 errx(1, "empty type list");
468
469 if (fslist[0] == 'n' && fslist[1] == 'o') {
470 fslist += 2;
471 which = NOT_IN_LIST;
472 }
473 else
474 which = IN_LIST;
475
476 while ((ptr = strsep(&fslist, ",")) != NULL)
477 addentry(&selhead, ptr, "");
478
479}
480
481
482static void
483catopt(char **sp, const char *o)
484{
485 char *s;
486 size_t i, j;
487
488 s = *sp;
489 if (s) {
490 i = strlen(s);
491 j = i + 1 + strlen(o) + 1;
492 s = erealloc(s, j);
493 (void)snprintf(s + i, j, ",%s", o);
494 } else
495 s = estrdup(o);
496 *sp = s;
497}
498
499
500static void
501mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp)
502{
503 char *p, *s;
504 int argc, maxargc;
505 const char **argv;
506
507 argc = *argcp;
508 argv = *argvp;
509 maxargc = *maxargcp;
510
511 for (s = opts; (p = strsep(&s, ",")) != NULL;) {
512 /* Always leave space for one more argument and the NULL. */
513 if (argc >= maxargc - 3) {
514 maxargc <<= 1;
515 argv = erealloc(argv, maxargc * sizeof(char *));
516 }
517 if (*p != '\0') {
518 if (*p == '-') {
519 argv[argc++] = p;
520 p = strchr(p, '=');
521 if (p) {
522 *p = '\0';
523 argv[argc++] = p+1;
524 }
525 } else {
526 argv[argc++] = "-o";
527 argv[argc++] = p;
528 }
529 }
530 }
531
532 *argcp = argc;
533 *argvp = argv;
534 *maxargcp = maxargc;
535}
536
537
538const static char *
539getfslab(const char *str)
540{
541 struct disklabel dl;
542 int fd;
543 char p;
544 const char *vfstype;
545 u_char t;
546
547 /* deduce the file system type from the disk label */
548 if ((fd = open(str, O_RDONLY)) == -1)
549 err(1, "cannot open `%s'", str);
550
551 if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
552 (void) close(fd);
553 return(NULL);
554 }
555
556 (void) close(fd);
557
558 p = str[strlen(str) - 1];
559
560 if ((p - 'a') >= dl.d_npartitions)
561 errx(1, "partition `%s' is not defined on disk", str);
562
563 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
564 errx(1, "partition `%s' is not of a legal vfstype",
565 str);
566
567 if ((vfstype = fstypenames[t]) == NULL)
568 errx(1, "vfstype `%s' on partition `%s' is not supported",
569 fstypenames[t], str);
570
571 return vfstype;
572}
573
574
575static void
576usage(void)
577{
578 static const char common[] =
579 "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]";
580
581 (void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
582 getprogname(), common);
583 exit(1);
584}