fsck.c revision 1.15 1 /* $NetBSD: fsck.c,v 1.15 1997/10/31 09:11:53 mycroft 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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * From: @(#)mount.c 8.19 (Berkeley) 4/19/94
37 * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
38 *
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: fsck.c,v 1.15 1997/10/31 09:11:53 mycroft Exp $");
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/mount.h>
48 #include <sys/queue.h>
49 #include <sys/wait.h>
50 #define DKTYPENAMES
51 #include <sys/disklabel.h>
52 #include <sys/ioctl.h>
53
54 #include <err.h>
55 #include <errno.h>
56 #include <fstab.h>
57 #include <fcntl.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #include "pathnames.h"
65 #include "fsutil.h"
66
67 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
68
69 TAILQ_HEAD(fstypelist, entry) opthead, selhead;
70
71 struct entry {
72 char *type;
73 char *options;
74 TAILQ_ENTRY(entry) entries;
75 };
76
77 static int maxrun = 0;
78 static char *options = NULL;
79 static int flags = 0;
80
81 int main __P((int, char *[]));
82
83 static int checkfs __P((const char *, const char *, const char *, void *,
84 pid_t *));
85 static int selected __P((const char *));
86 static void addoption __P((char *));
87 static const char *getoptions __P((const char *));
88 static void addentry __P((struct fstypelist *, const char *, const char *));
89 static void maketypelist __P((char *));
90 static char *catopt __P((char *, const char *, int));
91 static void mangle __P((char *, int *, const char ***, int *));
92 static char *getfslab __P((const char *));
93 static void usage __P((void));
94 static void *isok __P((struct fstab *));
95
96 int
97 main(argc, argv)
98 int argc;
99 char *argv[];
100 {
101 struct fstab *fs;
102 int i, rval = 0;
103 char *vfstype = NULL;
104 char globopt[3];
105
106 globopt[0] = '-';
107 globopt[2] = '\0';
108
109 TAILQ_INIT(&selhead);
110 TAILQ_INIT(&opthead);
111
112 while ((i = getopt(argc, argv, "dvpfnyl:t:T:")) != -1)
113 switch (i) {
114 case 'd':
115 flags |= CHECK_DEBUG;
116 break;
117
118 case 'v':
119 flags |= CHECK_VERBOSE;
120 break;
121
122 case 'p':
123 flags |= CHECK_PREEN;
124 /*FALLTHROUGH*/
125 case 'n':
126 case 'f':
127 case 'y':
128 globopt[1] = i;
129 options = catopt(options, globopt, 1);
130 break;
131
132 case 'l':
133 maxrun = atoi(optarg);
134 break;
135
136 case 'T':
137 if (*optarg)
138 addoption(optarg);
139 break;
140
141 case 't':
142 if (selhead.tqh_first != NULL)
143 errx(1, "only one -t option may be specified.");
144
145 maketypelist(optarg);
146 vfstype = optarg;
147 break;
148
149 case '?':
150 default:
151 usage();
152 /* NOTREACHED */
153 }
154
155 argc -= optind;
156 argv += optind;
157
158 if (argc == 0)
159 return checkfstab(flags, maxrun, isok, checkfs);
160
161 #define BADTYPE(type) \
162 (strcmp(type, FSTAB_RO) && \
163 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
164
165
166 for (; argc--; argv++) {
167 char *spec, *type;
168
169 if ((fs = getfsfile(*argv)) == NULL &&
170 (fs = getfsspec(*argv)) == NULL) {
171 if (vfstype == NULL)
172 vfstype = getfslab(*argv);
173 spec = *argv;
174 type = vfstype;
175 }
176 else {
177 spec = fs->fs_spec;
178 type = fs->fs_vfstype;
179 if (BADTYPE(fs->fs_type))
180 errx(1, "%s has unknown file system type.",
181 *argv);
182 }
183
184 rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
185 }
186
187 return rval;
188 }
189
190
191 static void *
192 isok(fs)
193 struct fstab *fs;
194 {
195 if (fs->fs_passno == 0)
196 return NULL;
197
198 if (BADTYPE(fs->fs_type))
199 return NULL;
200
201 if (!selected(fs->fs_vfstype))
202 return NULL;
203
204 return fs;
205 }
206
207
208 static int
209 checkfs(vfstype, spec, mntpt, auxarg, pidp)
210 const char *vfstype, *spec, *mntpt;
211 void *auxarg;
212 pid_t *pidp;
213 {
214 /* List of directories containing fsck_xxx subcommands. */
215 static const char *edirs[] = {
216 _PATH_SBIN,
217 _PATH_USRSBIN,
218 NULL
219 };
220 const char **argv, **edir;
221 pid_t pid;
222 int argc, i, status, maxargc;
223 char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
224 const char *extra = getoptions(vfstype);
225
226 #ifdef __GNUC__
227 /* Avoid vfork clobbering */
228 (void) &optbuf;
229 (void) &vfstype;
230 #endif
231
232 if (!strcmp(vfstype, "ufs"))
233 vfstype = MOUNT_UFS;
234
235 if (options) {
236 if (extra)
237 optbuf = catopt(options, extra, 0);
238 else
239 optbuf = estrdup(options);
240 } else {
241 if (extra)
242 optbuf = estrdup(extra);
243 else
244 optbuf = NULL;
245 }
246
247 maxargc = 64;
248 argv = emalloc(sizeof(char *) * maxargc);
249
250 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
251 argc = 0;
252 argv[argc++] = execbase;
253 if (optbuf)
254 mangle(optbuf, &argc, &argv, &maxargc);
255 argv[argc++] = spec;
256 argv[argc] = NULL;
257
258 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
259 (void)printf("start %s %swait", mntpt,
260 pidp ? "no" : "");
261 for (i = 0; i < argc; i++)
262 (void)printf(" %s", argv[i]);
263 (void)printf("\n");
264 }
265
266 switch (pid = vfork()) {
267 case -1: /* Error. */
268 warn("vfork");
269 if (optbuf)
270 free(optbuf);
271 return (1);
272
273 case 0: /* Child. */
274 if (flags & CHECK_DEBUG)
275 _exit(0);
276
277 /* Go find an executable. */
278 edir = edirs;
279 do {
280 (void)snprintf(execname,
281 sizeof(execname), "%s/%s", *edir, execbase);
282 execv(execname, (char * const *)argv);
283 if (errno != ENOENT)
284 if (spec)
285 warn("exec %s for %s", execname, spec);
286 else
287 warn("exec %s", execname);
288 } while (*++edir != NULL);
289
290 if (errno == ENOENT)
291 if (spec)
292 warn("exec %s for %s", execname, spec);
293 else
294 warn("exec %s", execname);
295 _exit(1);
296 /* NOTREACHED */
297
298 default: /* Parent. */
299 if (optbuf)
300 free(optbuf);
301
302 if (pidp) {
303 *pidp = pid;
304 return 0;
305 }
306
307 if (waitpid(pid, &status, 0) < 0) {
308 warn("waitpid");
309 return (1);
310 }
311
312 if (WIFEXITED(status)) {
313 if (WEXITSTATUS(status) != 0)
314 return (WEXITSTATUS(status));
315 }
316 else if (WIFSIGNALED(status)) {
317 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
318 return (1);
319 }
320 break;
321 }
322
323 return (0);
324 }
325
326
327 static int
328 selected(type)
329 const char *type;
330 {
331 struct entry *e;
332
333 /* If no type specified, it's always selected. */
334 for (e = selhead.tqh_first; e != NULL; e = e->entries.tqe_next)
335 if (!strncmp(e->type, type, MFSNAMELEN))
336 return which == IN_LIST ? 1 : 0;
337
338 return which == IN_LIST ? 0 : 1;
339 }
340
341
342 static const char *
343 getoptions(type)
344 const char *type;
345 {
346 struct entry *e;
347
348 for (e = opthead.tqh_first; e != NULL; e = e->entries.tqe_next)
349 if (!strncmp(e->type, type, MFSNAMELEN))
350 return e->options;
351 return "";
352 }
353
354
355 static void
356 addoption(optstr)
357 char *optstr;
358 {
359 char *newoptions;
360 struct entry *e;
361
362 if ((newoptions = strchr(optstr, ':')) == NULL)
363 errx(1, "Invalid option string");
364
365 *newoptions++ = '\0';
366
367 for (e = opthead.tqh_first; e != NULL; e = e->entries.tqe_next)
368 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
369 e->options = catopt(e->options, newoptions, 1);
370 return;
371 }
372 addentry(&opthead, optstr, newoptions);
373 }
374
375
376 static void
377 addentry(list, type, opts)
378 struct fstypelist *list;
379 const char *type;
380 const char *opts;
381 {
382 struct entry *e;
383
384 e = emalloc(sizeof(struct entry));
385 e->type = estrdup(type);
386 e->options = estrdup(opts);
387 TAILQ_INSERT_TAIL(list, e, entries);
388 }
389
390
391 static void
392 maketypelist(fslist)
393 char *fslist;
394 {
395 char *ptr;
396
397 if ((fslist == NULL) || (fslist[0] == '\0'))
398 errx(1, "empty type list");
399
400 if (fslist[0] == 'n' && fslist[1] == 'o') {
401 fslist += 2;
402 which = NOT_IN_LIST;
403 }
404 else
405 which = IN_LIST;
406
407 while ((ptr = strsep(&fslist, ",")) != NULL)
408 addentry(&selhead, ptr, "");
409
410 }
411
412
413 static char *
414 catopt(s0, s1, fr)
415 char *s0;
416 const char *s1;
417 int fr;
418 {
419 size_t i;
420 char *cp;
421
422 if (s0 && *s0) {
423 i = strlen(s0) + strlen(s1) + 1 + 1;
424 cp = emalloc(i);
425 (void)snprintf(cp, i, "%s,%s", s0, s1);
426 } else
427 cp = estrdup(s1);
428
429 if (s0 && fr)
430 free(s0);
431 return (cp);
432 }
433
434
435 static void
436 mangle(options, argcp, argvp, maxargcp)
437 char *options;
438 int *argcp, *maxargcp;
439 const char ***argvp;
440 {
441 char *p, *s;
442 int argc, maxargc;
443 const char **argv;
444
445 argc = *argcp;
446 argv = *argvp;
447 maxargc = *maxargcp;
448
449 for (s = options; (p = strsep(&s, ",")) != NULL;) {
450 /* Always leave space for one more argument and the NULL. */
451 if (argc >= maxargc - 3) {
452 maxargc <<= 1;
453 argv = erealloc(argv, maxargc * sizeof(char *));
454 }
455 if (*p != '\0')
456 if (*p == '-') {
457 argv[argc++] = p;
458 p = strchr(p, '=');
459 if (p) {
460 *p = '\0';
461 argv[argc++] = p+1;
462 }
463 } else {
464 argv[argc++] = "-o";
465 argv[argc++] = p;
466 }
467 }
468
469 *argcp = argc;
470 *argvp = argv;
471 *maxargcp = maxargc;
472 }
473
474
475 static char *
476 getfslab(str)
477 const char *str;
478 {
479 struct disklabel dl;
480 int fd;
481 char p, *vfstype;
482 u_char t;
483
484 /* deduce the filesystem type from the disk label */
485 if ((fd = open(str, O_RDONLY)) == -1)
486 err(1, "cannot open `%s'", str);
487
488 if (ioctl(fd, DIOCGDINFO, &dl) == -1)
489 err(1, "cannot get disklabel for `%s'", str);
490
491 (void) close(fd);
492
493 p = str[strlen(str) - 1];
494
495 if ((p - 'a') >= dl.d_npartitions)
496 errx(1, "partition `%s' is not defined on disk", str);
497
498 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
499 errx(1, "partition `%s' is not of a legal vfstype",
500 str);
501
502 if ((vfstype = fscknames[t]) == NULL)
503 errx(1, "vfstype `%s' on partition `%s' is not supported",
504 fstypenames[t], str);
505
506 return vfstype;
507 }
508
509
510 static void
511 usage()
512 {
513 extern char *__progname;
514 static const char common[] =
515 "[-dpvlyn] [-T fstype:fsoptions] [-t fstype]";
516
517 (void)fprintf(stderr, "Usage: %s %s [special|node]...\n",
518 __progname, common);
519 exit(1);
520 }
521