fsck.c revision 1.17 1 /* $NetBSD: fsck.c,v 1.17 1998/07/26 12:03:09 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.17 1998/07/26 12:03:09 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 void catopt __P((char **, const char *));
91 static void mangle __P((char *, int *, const char ***, int *));
92 static const 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 const 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 catopt(&options, globopt);
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;
168 const char *type;
169
170 if ((fs = getfsfile(*argv)) == NULL &&
171 (fs = getfsspec(*argv)) == NULL) {
172 if (vfstype == NULL)
173 vfstype = getfslab(*argv);
174 spec = *argv;
175 type = vfstype;
176 }
177 else {
178 spec = fs->fs_spec;
179 type = fs->fs_vfstype;
180 if (BADTYPE(fs->fs_type))
181 errx(1, "%s has unknown file system type.",
182 *argv);
183 }
184
185 rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
186 }
187
188 return rval;
189 }
190
191
192 static void *
193 isok(fs)
194 struct fstab *fs;
195 {
196 if (fs->fs_passno == 0)
197 return NULL;
198
199 if (BADTYPE(fs->fs_type))
200 return NULL;
201
202 if (!selected(fs->fs_vfstype))
203 return NULL;
204
205 return fs;
206 }
207
208
209 static int
210 checkfs(vfstype, spec, mntpt, auxarg, pidp)
211 const char *vfstype, *spec, *mntpt;
212 void *auxarg;
213 pid_t *pidp;
214 {
215 /* List of directories containing fsck_xxx subcommands. */
216 static const char *edirs[] = {
217 _PATH_SBIN,
218 _PATH_USRSBIN,
219 NULL
220 };
221 const char **argv, **edir;
222 pid_t pid;
223 int argc, i, status, maxargc;
224 char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
225 const char *extra = getoptions(vfstype);
226
227 #ifdef __GNUC__
228 /* Avoid vfork clobbering */
229 (void) &optbuf;
230 (void) &vfstype;
231 #endif
232
233 if (!strcmp(vfstype, "ufs"))
234 vfstype = MOUNT_UFS;
235
236 optbuf = NULL;
237 if (options)
238 catopt(&optbuf, options);
239 if (extra)
240 catopt(&optbuf, extra);
241
242 maxargc = 64;
243 argv = emalloc(sizeof(char *) * maxargc);
244
245 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
246 argc = 0;
247 argv[argc++] = execbase;
248 if (optbuf)
249 mangle(optbuf, &argc, &argv, &maxargc);
250 argv[argc++] = spec;
251 argv[argc] = NULL;
252
253 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
254 (void)printf("start %s %swait", mntpt,
255 pidp ? "no" : "");
256 for (i = 0; i < argc; i++)
257 (void)printf(" %s", argv[i]);
258 (void)printf("\n");
259 }
260
261 switch (pid = vfork()) {
262 case -1: /* Error. */
263 warn("vfork");
264 if (optbuf)
265 free(optbuf);
266 return (1);
267
268 case 0: /* Child. */
269 if (flags & CHECK_DEBUG)
270 _exit(0);
271
272 /* Go find an executable. */
273 edir = edirs;
274 do {
275 (void)snprintf(execname,
276 sizeof(execname), "%s/%s", *edir, execbase);
277 execv(execname, (char * const *)argv);
278 if (errno != ENOENT)
279 if (spec)
280 warn("exec %s for %s", execname, spec);
281 else
282 warn("exec %s", execname);
283 } while (*++edir != NULL);
284
285 if (errno == ENOENT)
286 if (spec)
287 warn("exec %s for %s", execname, spec);
288 else
289 warn("exec %s", execname);
290 _exit(1);
291 /* NOTREACHED */
292
293 default: /* Parent. */
294 if (optbuf)
295 free(optbuf);
296
297 if (pidp) {
298 *pidp = pid;
299 return 0;
300 }
301
302 if (waitpid(pid, &status, 0) < 0) {
303 warn("waitpid");
304 return (1);
305 }
306
307 if (WIFEXITED(status)) {
308 if (WEXITSTATUS(status) != 0)
309 return (WEXITSTATUS(status));
310 }
311 else if (WIFSIGNALED(status)) {
312 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
313 return (1);
314 }
315 break;
316 }
317
318 return (0);
319 }
320
321
322 static int
323 selected(type)
324 const char *type;
325 {
326 struct entry *e;
327
328 /* If no type specified, it's always selected. */
329 for (e = selhead.tqh_first; e != NULL; e = e->entries.tqe_next)
330 if (!strncmp(e->type, type, MFSNAMELEN))
331 return which == IN_LIST ? 1 : 0;
332
333 return which == IN_LIST ? 0 : 1;
334 }
335
336
337 static const char *
338 getoptions(type)
339 const char *type;
340 {
341 struct entry *e;
342
343 for (e = opthead.tqh_first; e != NULL; e = e->entries.tqe_next)
344 if (!strncmp(e->type, type, MFSNAMELEN))
345 return e->options;
346 return "";
347 }
348
349
350 static void
351 addoption(optstr)
352 char *optstr;
353 {
354 char *newoptions;
355 struct entry *e;
356
357 if ((newoptions = strchr(optstr, ':')) == NULL)
358 errx(1, "Invalid option string");
359
360 *newoptions++ = '\0';
361
362 for (e = opthead.tqh_first; e != NULL; e = e->entries.tqe_next)
363 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
364 catopt(&e->options, newoptions);
365 return;
366 }
367 addentry(&opthead, optstr, newoptions);
368 }
369
370
371 static void
372 addentry(list, type, opts)
373 struct fstypelist *list;
374 const char *type;
375 const char *opts;
376 {
377 struct entry *e;
378
379 e = emalloc(sizeof(struct entry));
380 e->type = estrdup(type);
381 e->options = estrdup(opts);
382 TAILQ_INSERT_TAIL(list, e, entries);
383 }
384
385
386 static void
387 maketypelist(fslist)
388 char *fslist;
389 {
390 char *ptr;
391
392 if ((fslist == NULL) || (fslist[0] == '\0'))
393 errx(1, "empty type list");
394
395 if (fslist[0] == 'n' && fslist[1] == 'o') {
396 fslist += 2;
397 which = NOT_IN_LIST;
398 }
399 else
400 which = IN_LIST;
401
402 while ((ptr = strsep(&fslist, ",")) != NULL)
403 addentry(&selhead, ptr, "");
404
405 }
406
407
408 static void
409 catopt(sp, o)
410 char **sp;
411 const char *o;
412 {
413 char *s;
414 size_t i, j;
415
416 s = *sp;
417 if (s) {
418 i = strlen(s);
419 j = i + 1 + strlen(o) + 1;
420 s = erealloc(s, j);
421 (void)snprintf(s + i, j, ",%s", o);
422 } else
423 s = estrdup(o);
424 *sp = s;
425 }
426
427
428 static void
429 mangle(options, argcp, argvp, maxargcp)
430 char *options;
431 int *argcp, *maxargcp;
432 const char ***argvp;
433 {
434 char *p, *s;
435 int argc, maxargc;
436 const char **argv;
437
438 argc = *argcp;
439 argv = *argvp;
440 maxargc = *maxargcp;
441
442 for (s = options; (p = strsep(&s, ",")) != NULL;) {
443 /* Always leave space for one more argument and the NULL. */
444 if (argc >= maxargc - 3) {
445 maxargc <<= 1;
446 argv = erealloc(argv, maxargc * sizeof(char *));
447 }
448 if (*p != '\0')
449 if (*p == '-') {
450 argv[argc++] = p;
451 p = strchr(p, '=');
452 if (p) {
453 *p = '\0';
454 argv[argc++] = p+1;
455 }
456 } else {
457 argv[argc++] = "-o";
458 argv[argc++] = p;
459 }
460 }
461
462 *argcp = argc;
463 *argvp = argv;
464 *maxargcp = maxargc;
465 }
466
467
468 const static char *
469 getfslab(str)
470 const char *str;
471 {
472 struct disklabel dl;
473 int fd;
474 char p;
475 const char *vfstype;
476 u_char t;
477
478 /* deduce the filesystem type from the disk label */
479 if ((fd = open(str, O_RDONLY)) == -1)
480 err(1, "cannot open `%s'", str);
481
482 if (ioctl(fd, DIOCGDINFO, &dl) == -1)
483 err(1, "cannot get disklabel for `%s'", str);
484
485 (void) close(fd);
486
487 p = str[strlen(str) - 1];
488
489 if ((p - 'a') >= dl.d_npartitions)
490 errx(1, "partition `%s' is not defined on disk", str);
491
492 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
493 errx(1, "partition `%s' is not of a legal vfstype",
494 str);
495
496 if ((vfstype = fscknames[t]) == NULL)
497 errx(1, "vfstype `%s' on partition `%s' is not supported",
498 fstypenames[t], str);
499
500 return vfstype;
501 }
502
503
504 static void
505 usage()
506 {
507 extern char *__progname;
508 static const char common[] =
509 "[-dpvlyn] [-T fstype:fsoptions] [-t fstype]";
510
511 (void)fprintf(stderr, "Usage: %s %s [special|node]...\n",
512 __progname, common);
513 exit(1);
514 }
515