VM: munmap used by VM for itself is no longer used
[minix.git] / sbin / fsck / fsck.c
blobe57ec3065706f5ce5c8d8404f6e62a0969ccdabb
1 /* $NetBSD: fsck.c,v 1.49 2010/02/24 13:56:07 hannken Exp $ */
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.
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.
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.
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
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: fsck.c,v 1.49 2010/02/24 13:56:07 hannken Exp $");
40 #endif /* not lint */
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>
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>
64 #include "pathnames.h"
65 #include "fsutil.h"
66 #include "exitvalues.h"
68 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
70 TAILQ_HEAD(fstypelist, entry) opthead, selhead, omhead;
72 struct entry {
73 char *type;
74 char *options;
75 TAILQ_ENTRY(entry) entries;
78 static int maxrun = 0;
79 static char *options = NULL;
80 static int flags = 0;
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 static void usage(void);
93 static void *isok(struct fstab *);
95 int
96 main(int argc, char *argv[])
98 struct fstab *fs;
99 int i, rval;
100 const char *vfstype = NULL;
101 char globopt[3];
102 int ret = FSCK_EXIT_OK;
104 globopt[0] = '-';
105 globopt[2] = '\0';
107 TAILQ_INIT(&selhead);
108 TAILQ_INIT(&opthead);
109 TAILQ_INIT(&omhead);
111 while ((i = getopt(argc, argv, "dfl:nPpqT:t:vx:y")) != -1) {
112 switch (i) {
113 case 'd':
114 flags |= CHECK_DEBUG;
115 continue;
117 case 'f':
118 flags |= CHECK_FORCE;
119 break;
121 case 'n':
122 flags |= CHECK_NOFIX;
123 break;
125 case 'p':
126 flags |= CHECK_PREEN;
127 break;
129 case 'P':
130 flags |= CHECK_PROGRESS;
131 break;
133 case 'q':
134 break;
136 case 'l':
137 maxrun = atoi(optarg);
138 continue;
140 case 'T':
141 if (*optarg)
142 addoption(optarg);
143 continue;
145 case 't':
146 if (TAILQ_FIRST(&selhead) != NULL)
147 errx(1, "only one -t option may be specified.");
149 maketypelist(optarg);
150 vfstype = optarg;
151 continue;
153 case 'v':
154 flags |= CHECK_VERBOSE;
155 continue;
157 case 'x':
158 addentry(&omhead, optarg, "");
159 continue;
161 case 'y':
162 break;
164 case '?':
165 default:
166 usage();
167 /* NOTREACHED */
170 /* Pass option to fsck_xxxfs */
171 globopt[1] = i;
172 catopt(&options, globopt);
175 /* Don't do progress meters if we're debugging. */
176 if (flags & CHECK_DEBUG)
177 flags &= ~CHECK_PROGRESS;
180 * If progress meters are being used, force max parallel to 1
181 * so the progress meter outputs don't interfere with one another.
183 if (flags & CHECK_PROGRESS)
184 maxrun = 1;
186 #ifdef __minix
187 /* parallel checking heuristic doesn't work for minix currently */
188 maxrun = 1;
189 #endif
191 argc -= optind;
192 argv += optind;
194 if (argc == 0)
195 return checkfstab(flags, maxrun, isok, checkfs);
197 #define BADTYPE(type) \
198 (strcmp(type, FSTAB_RO) && \
199 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
202 for (; argc--; argv++) {
203 const char *spec, *type, *cp;
204 char device[MAXPATHLEN];
206 spec = *argv;
207 cp = strrchr(spec, '/');
208 if (cp == 0) {
209 (void)snprintf(device, sizeof(device), "%s%s",
210 _PATH_DEV, spec);
211 spec = device;
213 if ((fs = getfsfile(spec)) == NULL &&
214 (fs = getfsspec(spec)) == NULL) {
215 if (vfstype == NULL)
216 vfstype = getfslab(spec);
217 type = vfstype;
219 else {
220 spec = fs->fs_spec;
221 type = fs->fs_vfstype;
222 if (BADTYPE(fs->fs_type))
223 errx(FSCK_EXIT_CHECK_FAILED,
224 "%s has unknown file system type.",
225 spec);
228 rval = checkfs(type, blockcheck(spec), *argv, NULL, NULL);
229 if (rval > ret)
230 ret = rval;
233 return ret;
237 static void *
238 isok(struct fstab *fs)
241 if (fs->fs_passno == 0)
242 return NULL;
244 if (BADTYPE(fs->fs_type))
245 return NULL;
247 if (!selected(fs->fs_vfstype))
248 return NULL;
250 if (omitted(fs->fs_file))
251 return NULL;
253 return fs;
257 static int
258 checkfs(const char *vfst, const char *spec, const char *mntpt, void *auxarg,
259 pid_t *pidp)
261 /* List of directories containing fsck_xxx subcommands. */
262 static const char *edirs[] = {
263 #ifdef RESCUEDIR
264 RESCUEDIR,
265 #endif
266 _PATH_SBIN,
267 _PATH_USRSBIN,
268 #ifdef __minix
269 "/usr/pkg/sbin/",
270 #endif
271 NULL
273 const char ** volatile argv, **edir;
274 const char * volatile vfstype = vfst;
275 pid_t pid;
276 int argc, i, status, maxargc;
277 char *optb;
278 char *volatile optbuf;
279 char execname[MAXPATHLEN + 1], execbase1[MAXPATHLEN], execbase2[MAXPATHLEN];
280 const char *extra = getoptions(vfstype);
282 if (!strcmp(vfstype, "ufs"))
283 vfstype = MOUNT_UFS;
285 optb = NULL;
286 if (options)
287 catopt(&optb, options);
288 if (extra)
289 catopt(&optb, extra);
290 optbuf = optb;
292 maxargc = 64;
293 argv = emalloc(sizeof(char *) * maxargc);
295 (void) snprintf(execbase1, sizeof(execbase1), "fsck_%s", vfstype);
296 (void) snprintf(execbase2, sizeof(execbase2), "fsck.%s", vfstype);
297 argc = 0;
298 argv[argc++] = execbase1;
299 if (optbuf)
300 mangle(optbuf, &argc, &argv, &maxargc);
301 argv[argc++] = spec;
302 argv[argc] = NULL;
304 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
305 (void)printf("start %s %swait", mntpt,
306 pidp ? "no" : "");
307 for (i = 0; i < argc; i++)
308 (void)printf(" %s", argv[i]);
309 (void)printf("\n");
312 switch (pid = vfork()) {
313 case -1: /* Error. */
314 warn("vfork");
315 if (optbuf)
316 free(optbuf);
317 free(argv);
318 return FSCK_EXIT_CHECK_FAILED;
320 case 0: /* Child. */
321 #ifndef __minix
322 if ((flags & CHECK_FORCE) == 0) {
323 struct statvfs sfs;
326 * if mntpt is a mountpoint of a mounted file
327 * system and it's mounted read-write, skip it
328 * unless -f is given.
330 if ((statvfs(mntpt, &sfs) == 0) &&
331 (strcmp(mntpt, sfs.f_mntonname) == 0) &&
332 ((sfs.f_flag & MNT_RDONLY) == 0)) {
333 printf(
334 "%s: file system is mounted read-write on %s; not checking\n",
335 spec, mntpt);
336 if ((flags & CHECK_PREEN) && auxarg != NULL)
337 _exit(FSCK_EXIT_OK); /* fsck -p */
338 else
339 _exit(FSCK_EXIT_CHECK_FAILED); /* fsck [[-p] ...] */
342 #endif
344 if (flags & CHECK_DEBUG)
345 _exit(FSCK_EXIT_OK);
347 /* Go find an executable. */
348 edir = edirs;
349 do {
350 (void)snprintf(execname,
351 sizeof(execname), "%s/%s", *edir, execbase1);
352 execv(execname, (char * const *)__UNCONST(argv));
353 if (errno != ENOENT) {
354 if (spec)
355 warn("exec %s for %s", execname, spec);
356 else
357 warn("exec %s", execname);
359 (void)snprintf(execname,
360 sizeof(execname), "%s/%s", *edir, execbase2);
361 execv(execname, (char * const *)__UNCONST(argv));
362 if (errno != ENOENT) {
363 if (spec)
364 warn("exec %s for %s", execname, spec);
365 else
366 warn("exec %s", execname);
368 } while (*++edir != NULL);
370 if (errno == ENOENT) {
371 if (spec)
372 warn("exec %s for %s", execname, spec);
373 else
374 warn("exec %s", execname);
376 _exit(FSCK_EXIT_CHECK_FAILED);
377 /* NOTREACHED */
379 default: /* Parent. */
380 if (optbuf)
381 free(optbuf);
382 free(argv);
384 if (pidp) {
385 *pidp = pid;
386 return FSCK_EXIT_OK;
389 if (waitpid(pid, &status, 0) < 0) {
390 warn("waitpid");
391 return FSCK_EXIT_CHECK_FAILED;
394 if (WIFEXITED(status)) {
395 if (WEXITSTATUS(status) != 0)
396 return WEXITSTATUS(status);
398 else if (WIFSIGNALED(status)) {
399 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
400 return FSCK_EXIT_CHECK_FAILED;
402 break;
405 return FSCK_EXIT_OK;
409 static int
410 selected(const char *type)
412 struct entry *e;
414 /* If no type specified, it's always selected. */
415 TAILQ_FOREACH(e, &selhead, entries)
416 if (!strcmp(e->type, type))
417 return which == IN_LIST ? 1 : 0;
419 return which == IN_LIST ? 0 : 1;
423 static int
424 omitted(const char *mountedon)
426 struct entry *e;
428 /* If no type specified, it's always selected. */
429 TAILQ_FOREACH(e, &omhead, entries)
430 if (!strcmp(e->type, mountedon))
431 return 1;
433 return 0;
437 static const char *
438 getoptions(const char *type)
440 struct entry *e;
442 TAILQ_FOREACH(e, &opthead, entries)
443 if (!strcmp(e->type, type))
444 return e->options;
445 return "";
449 static void
450 addoption(char *optstr)
452 char *newoptions;
453 struct entry *e;
455 if ((newoptions = strchr(optstr, ':')) == NULL)
456 errx(1, "Invalid option string");
458 *newoptions++ = '\0';
460 TAILQ_FOREACH(e, &opthead, entries)
461 if (!strcmp(e->type, optstr)) {
462 catopt(&e->options, newoptions);
463 return;
465 addentry(&opthead, optstr, newoptions);
469 static void
470 addentry(struct fstypelist *list, const char *type, const char *opts)
472 struct entry *e;
474 e = emalloc(sizeof(struct entry));
475 e->type = estrdup(type);
476 e->options = estrdup(opts);
477 TAILQ_INSERT_TAIL(list, e, entries);
481 static void
482 maketypelist(char *fslist)
484 char *ptr;
486 if ((fslist == NULL) || (fslist[0] == '\0'))
487 errx(1, "empty type list");
489 if (fslist[0] == 'n' && fslist[1] == 'o') {
490 fslist += 2;
491 which = NOT_IN_LIST;
493 else
494 which = IN_LIST;
496 while ((ptr = strsep(&fslist, ",")) != NULL)
497 addentry(&selhead, ptr, "");
502 static void
503 catopt(char **sp, const char *o)
505 char *s;
506 size_t i, j;
508 s = *sp;
509 if (s) {
510 i = strlen(s);
511 j = i + 1 + strlen(o) + 1;
512 s = erealloc(s, j);
513 (void)snprintf(s + i, j, ",%s", o);
514 } else
515 s = estrdup(o);
516 *sp = s;
520 static void
521 mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp)
523 char *p, *s;
524 int argc, maxargc;
525 const char **argv;
527 argc = *argcp;
528 argv = *argvp;
529 maxargc = *maxargcp;
531 for (s = opts; (p = strsep(&s, ",")) != NULL;) {
532 /* Always leave space for one more argument and the NULL. */
533 if (argc >= maxargc - 3) {
534 maxargc <<= 1;
535 argv = erealloc(argv, maxargc * sizeof(char *));
537 if (*p != '\0') {
538 if (*p == '-') {
539 argv[argc++] = p;
540 p = strchr(p, '=');
541 if (p) {
542 *p = '\0';
543 argv[argc++] = p+1;
545 } else {
546 argv[argc++] = "-o";
547 argv[argc++] = p;
552 *argcp = argc;
553 *argvp = argv;
554 *maxargcp = maxargc;
557 static const char *
558 getfslab(const char *str)
560 #ifdef __minix
561 errx(1, "cannot determine vfstype under minix");
562 #else
563 static struct dkwedge_info dkw;
564 struct disklabel dl;
565 int fd;
566 char p;
567 const char *vfstype;
568 u_char t;
570 /* deduce the file system type from the disk label */
571 if ((fd = open(str, O_RDONLY)) == -1)
572 err(1, "cannot open `%s'", str);
574 /* First check to see if it's a wedge. */
575 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
576 /* Yup, this is easy. */
577 (void) close(fd);
578 return (dkw.dkw_ptype);
581 if (ioctl(fd, DIOCGDINFO, &dl) == -1)
582 err(1, "cannot get disklabel for `%s'", str);
584 (void) close(fd);
586 p = str[strlen(str) - 1];
588 if ((p - 'a') >= dl.d_npartitions)
589 errx(1, "partition `%s' is not defined on disk", str);
591 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
592 errx(1, "partition `%s' is not of a legal vfstype",
593 str);
595 if ((vfstype = fscknames[t]) == NULL)
596 errx(1, "vfstype `%s' on partition `%s' is not supported",
597 fstypenames[t], str);
599 return vfstype;
600 #endif
604 static void
605 usage(void)
607 static const char common[] =
608 "[-dfnPpqvy] [-x excludemount] [-l maxparallel] [-T fstype:fsoptions]\n\t\t[-t fstype]";
610 (void)fprintf(stderr, "usage: %s %s [special|node]...\n",
611 getprogname(), common);
612 exit(FSCK_EXIT_USAGE);