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