fsck.c revision 1.1 1 1.1 christos /* $NetBSD: fsck.c,v 1.1 1996/09/11 20:27:14 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1996 Christos Zoulas. All rights reserved.
5 1.1 christos * Copyright (c) 1980, 1989, 1993, 1994
6 1.1 christos * The Regents of the University of California. All rights reserved.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos * 1. Redistributions of source code must retain the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer.
13 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer in the
15 1.1 christos * documentation and/or other materials provided with the distribution.
16 1.1 christos * 3. All advertising materials mentioning features or use of this software
17 1.1 christos * must display the following acknowledgement:
18 1.1 christos * This product includes software developed by the University of
19 1.1 christos * California, Berkeley and its contributors.
20 1.1 christos * 4. Neither the name of the University nor the names of its contributors
21 1.1 christos * may be used to endorse or promote products derived from this software
22 1.1 christos * without specific prior written permission.
23 1.1 christos *
24 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 christos * SUCH DAMAGE.
35 1.1 christos *
36 1.1 christos * From: @(#)mount.c 8.19 (Berkeley) 4/19/94
37 1.1 christos * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
38 1.1 christos *
39 1.1 christos */
40 1.1 christos
41 1.1 christos static char rcsid[] = "$NetBSD: fsck.c,v 1.1 1996/09/11 20:27:14 christos Exp $";
42 1.1 christos
43 1.1 christos #include <sys/param.h>
44 1.1 christos #include <sys/mount.h>
45 1.1 christos #include <sys/queue.h>
46 1.1 christos #include <sys/wait.h>
47 1.1 christos
48 1.1 christos #include <err.h>
49 1.1 christos #include <errno.h>
50 1.1 christos #include <fstab.h>
51 1.1 christos #include <signal.h>
52 1.1 christos #include <stdio.h>
53 1.1 christos #include <stdlib.h>
54 1.1 christos #include <string.h>
55 1.1 christos #include <unistd.h>
56 1.1 christos
57 1.1 christos #include "pathnames.h"
58 1.1 christos
59 1.1 christos static enum { IN_LIST, NOT_IN_LIST } which;
60 1.1 christos
61 1.1 christos TAILQ_HEAD(fstypelist, entry) head;
62 1.1 christos
63 1.1 christos struct entry {
64 1.1 christos char *type;
65 1.1 christos TAILQ_ENTRY(entry) entries;
66 1.1 christos };
67 1.1 christos
68 1.1 christos static int preen = 0, verbose = 0, debug = 0;
69 1.1 christos static struct fstypelist *typelist;
70 1.1 christos
71 1.1 christos static int checkfs __P((const char *, const char *, const char *));
72 1.1 christos static int selected __P((const char *));
73 1.1 christos static void addentry __P((const char *));
74 1.1 christos static void rementry __P((const char *));
75 1.1 christos static void unselect __P((const char *));
76 1.1 christos static void maketypelist __P((char *));
77 1.1 christos static char *catopt __P((char *, const char *));
78 1.1 christos static void mangle __P((char *, int *, const char **));
79 1.1 christos static void *emalloc __P((size_t));
80 1.1 christos static char *estrdup __P((const char *));
81 1.1 christos static void usage __P((void));
82 1.1 christos
83 1.1 christos
84 1.1 christos int
85 1.1 christos main(argc, argv)
86 1.1 christos int argc;
87 1.1 christos char * const argv[];
88 1.1 christos {
89 1.1 christos struct fstab *fs;
90 1.1 christos int i, rval = 0;
91 1.1 christos char *options = NULL;
92 1.1 christos char *vfstype = NULL;
93 1.1 christos
94 1.1 christos while ((i = getopt(argc, argv, "dvpnyt:o:")) != -1)
95 1.1 christos switch (i) {
96 1.1 christos case 'd':
97 1.1 christos debug++;
98 1.1 christos break;
99 1.1 christos
100 1.1 christos case 'v':
101 1.1 christos verbose++;
102 1.1 christos break;
103 1.1 christos
104 1.1 christos case 'y':
105 1.1 christos options = catopt(options, "-y");
106 1.1 christos break;
107 1.1 christos
108 1.1 christos case 'n':
109 1.1 christos options = catopt(options, "-n");
110 1.1 christos break;
111 1.1 christos
112 1.1 christos case 'o':
113 1.1 christos if (*optarg)
114 1.1 christos options = catopt(options, optarg);
115 1.1 christos break;
116 1.1 christos
117 1.1 christos case 't':
118 1.1 christos if (typelist != NULL)
119 1.1 christos errx(1, "only one -t option may be specified.");
120 1.1 christos maketypelist(optarg);
121 1.1 christos vfstype = optarg;
122 1.1 christos break;
123 1.1 christos
124 1.1 christos case 'p':
125 1.1 christos options = catopt(options, "-p");
126 1.1 christos preen++;
127 1.1 christos break;
128 1.1 christos
129 1.1 christos case '?':
130 1.1 christos default:
131 1.1 christos usage();
132 1.1 christos /* NOTREACHED */
133 1.1 christos }
134 1.1 christos
135 1.1 christos argc -= optind;
136 1.1 christos argv += optind;
137 1.1 christos
138 1.1 christos #define BADTYPE(type) \
139 1.1 christos (strcmp(type, FSTAB_RO) && \
140 1.1 christos strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
141 1.1 christos
142 1.1 christos if (argc == 0) {
143 1.1 christos while ((fs = getfsent()) != NULL) {
144 1.1 christos
145 1.1 christos if (fs->fs_passno == 0)
146 1.1 christos continue;
147 1.1 christos
148 1.1 christos if (BADTYPE(fs->fs_type))
149 1.1 christos continue;
150 1.1 christos
151 1.1 christos if (!selected(fs->fs_vfstype))
152 1.1 christos continue;
153 1.1 christos
154 1.1 christos if (preen) {
155 1.1 christos rval |= checkfs(fs->fs_vfstype, NULL, options);
156 1.1 christos unselect(fs->fs_vfstype);
157 1.1 christos }
158 1.1 christos else
159 1.1 christos rval |= checkfs(fs->fs_vfstype, fs->fs_spec,
160 1.1 christos options);
161 1.1 christos }
162 1.1 christos return rval;
163 1.1 christos }
164 1.1 christos
165 1.1 christos /*
166 1.1 christos * When we select specific filesystems, we cannot preen
167 1.1 christos */
168 1.1 christos if (preen)
169 1.1 christos usage();
170 1.1 christos
171 1.1 christos for (; argc--; argv++) {
172 1.1 christos char *spec, *type;
173 1.1 christos
174 1.1 christos if ((fs = getfsfile(*argv)) == NULL &&
175 1.1 christos (fs = getfsspec(*argv)) == NULL) {
176 1.1 christos if (vfstype == NULL)
177 1.1 christos errx(1,
178 1.1 christos "%s: unknown special file or file system.",
179 1.1 christos *argv);
180 1.1 christos spec = *argv;
181 1.1 christos type = vfstype;
182 1.1 christos }
183 1.1 christos else {
184 1.1 christos spec = fs->fs_spec;
185 1.1 christos type = fs->fs_vfstype;
186 1.1 christos if (BADTYPE(fs->fs_type))
187 1.1 christos errx(1, "%s has unknown file system type.",
188 1.1 christos *argv);
189 1.1 christos }
190 1.1 christos
191 1.1 christos rval |= checkfs(type, spec, options);
192 1.1 christos }
193 1.1 christos
194 1.1 christos return rval;
195 1.1 christos }
196 1.1 christos
197 1.1 christos static int
198 1.1 christos checkfs(vfstype, spec, options)
199 1.1 christos const char *vfstype, *spec, *options;
200 1.1 christos {
201 1.1 christos /* List of directories containing fsck_xxx subcommands. */
202 1.1 christos static const char *edirs[] = {
203 1.1 christos _PATH_SBIN,
204 1.1 christos _PATH_USRSBIN,
205 1.1 christos NULL
206 1.1 christos };
207 1.1 christos const char *argv[100], **edir;
208 1.1 christos pid_t pid;
209 1.1 christos int argc, i, status;
210 1.1 christos char *optbuf = NULL, execname[MAXPATHLEN + 1];
211 1.1 christos
212 1.1 christos #ifdef __GNUC__
213 1.1 christos /* Avoid vfork clobbering */
214 1.1 christos (void) &optbuf;
215 1.1 christos #endif
216 1.1 christos
217 1.1 christos argc = 0;
218 1.1 christos argv[argc++] = vfstype;
219 1.1 christos if (options)
220 1.1 christos mangle(optbuf = estrdup(options), &argc, argv);
221 1.1 christos if (spec)
222 1.1 christos argv[argc++] = spec;
223 1.1 christos argv[argc] = NULL;
224 1.1 christos
225 1.1 christos if (debug || verbose) {
226 1.1 christos (void)printf("fsck_%s", vfstype);
227 1.1 christos for (i = 1; i < argc; i++)
228 1.1 christos (void)printf(" %s", argv[i]);
229 1.1 christos (void)printf("\n");
230 1.1 christos if (debug)
231 1.1 christos return (0);
232 1.1 christos }
233 1.1 christos
234 1.1 christos switch (pid = vfork()) {
235 1.1 christos case -1: /* Error. */
236 1.1 christos warn("vfork");
237 1.1 christos if (optbuf)
238 1.1 christos free(optbuf);
239 1.1 christos return (1);
240 1.1 christos
241 1.1 christos case 0: /* Child. */
242 1.1 christos /* Go find an executable. */
243 1.1 christos edir = edirs;
244 1.1 christos do {
245 1.1 christos (void)snprintf(execname,
246 1.1 christos sizeof(execname), "%s/fsck_%s", *edir, vfstype);
247 1.1 christos execv(execname, (char * const *)argv);
248 1.1 christos if (errno != ENOENT)
249 1.1 christos if (spec)
250 1.1 christos warn("exec %s for %s", execname, spec);
251 1.1 christos else
252 1.1 christos warn("exec %s", execname);
253 1.1 christos } while (*++edir != NULL);
254 1.1 christos
255 1.1 christos if (errno == ENOENT)
256 1.1 christos if (spec)
257 1.1 christos warn("exec %s for %s", execname, spec);
258 1.1 christos else
259 1.1 christos warn("exec %s", execname);
260 1.1 christos exit(1);
261 1.1 christos /* NOTREACHED */
262 1.1 christos
263 1.1 christos default: /* Parent. */
264 1.1 christos if (optbuf)
265 1.1 christos free(optbuf);
266 1.1 christos
267 1.1 christos if (waitpid(pid, &status, 0) < 0) {
268 1.1 christos warn("waitpid");
269 1.1 christos return (1);
270 1.1 christos }
271 1.1 christos
272 1.1 christos if (WIFEXITED(status)) {
273 1.1 christos if (WEXITSTATUS(status) != 0)
274 1.1 christos return (WEXITSTATUS(status));
275 1.1 christos }
276 1.1 christos else if (WIFSIGNALED(status)) {
277 1.1 christos warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
278 1.1 christos return (1);
279 1.1 christos }
280 1.1 christos break;
281 1.1 christos }
282 1.1 christos
283 1.1 christos return (0);
284 1.1 christos }
285 1.1 christos
286 1.1 christos
287 1.1 christos static int
288 1.1 christos selected(type)
289 1.1 christos const char *type;
290 1.1 christos {
291 1.1 christos struct entry *e;
292 1.1 christos
293 1.1 christos /* If no type specified, it's always selected. */
294 1.1 christos if (typelist == NULL)
295 1.1 christos return (1);
296 1.1 christos
297 1.1 christos for (e = typelist->tqh_first; e != NULL; e = e->entries.tqe_next)
298 1.1 christos if (!strncmp(e->type, type, MFSNAMELEN))
299 1.1 christos return which == IN_LIST ? 1 : 0;
300 1.1 christos
301 1.1 christos return which == IN_LIST ? 0 : 1;
302 1.1 christos }
303 1.1 christos
304 1.1 christos
305 1.1 christos static void
306 1.1 christos addentry(type)
307 1.1 christos const char *type;
308 1.1 christos {
309 1.1 christos struct entry *e;
310 1.1 christos
311 1.1 christos e = emalloc(sizeof(struct entry));
312 1.1 christos e->type = estrdup(type);
313 1.1 christos
314 1.1 christos TAILQ_INSERT_TAIL(typelist, e, entries);
315 1.1 christos }
316 1.1 christos
317 1.1 christos
318 1.1 christos static void
319 1.1 christos rementry(type)
320 1.1 christos const char *type;
321 1.1 christos {
322 1.1 christos struct entry *e;
323 1.1 christos
324 1.1 christos for (e = typelist->tqh_first; e != NULL; e = e->entries.tqe_next)
325 1.1 christos if (!strncmp(e->type, type, MFSNAMELEN)) {
326 1.1 christos TAILQ_REMOVE(typelist, e, entries);
327 1.1 christos free(e->type);
328 1.1 christos free(e);
329 1.1 christos return;
330 1.1 christos }
331 1.1 christos }
332 1.1 christos
333 1.1 christos
334 1.1 christos static void
335 1.1 christos unselect(type)
336 1.1 christos const char *type;
337 1.1 christos {
338 1.1 christos if (typelist == NULL) {
339 1.1 christos TAILQ_INIT(&head);
340 1.1 christos typelist = &head;
341 1.1 christos which = NOT_IN_LIST;
342 1.1 christos addentry(type);
343 1.1 christos return;
344 1.1 christos }
345 1.1 christos if (which == IN_LIST)
346 1.1 christos rementry(type);
347 1.1 christos else
348 1.1 christos addentry(type);
349 1.1 christos }
350 1.1 christos
351 1.1 christos
352 1.1 christos static void
353 1.1 christos maketypelist(fslist)
354 1.1 christos char *fslist;
355 1.1 christos {
356 1.1 christos char *ptr;
357 1.1 christos
358 1.1 christos if (typelist == NULL) {
359 1.1 christos TAILQ_INIT(&head);
360 1.1 christos typelist = &head;
361 1.1 christos }
362 1.1 christos
363 1.1 christos if ((fslist == NULL) || (fslist[0] == '\0'))
364 1.1 christos errx(1, "empty type list");
365 1.1 christos
366 1.1 christos if (fslist[0] == 'n' && fslist[1] == 'o') {
367 1.1 christos fslist += 2;
368 1.1 christos which = NOT_IN_LIST;
369 1.1 christos }
370 1.1 christos else
371 1.1 christos which = IN_LIST;
372 1.1 christos
373 1.1 christos while ((ptr = strsep(&fslist, ",")) != NULL)
374 1.1 christos addentry(ptr);
375 1.1 christos
376 1.1 christos }
377 1.1 christos
378 1.1 christos
379 1.1 christos static char *
380 1.1 christos catopt(s0, s1)
381 1.1 christos char *s0;
382 1.1 christos const char *s1;
383 1.1 christos {
384 1.1 christos size_t i;
385 1.1 christos char *cp;
386 1.1 christos
387 1.1 christos if (s0 && *s0) {
388 1.1 christos i = strlen(s0) + strlen(s1) + 1 + 1;
389 1.1 christos cp = emalloc(i);
390 1.1 christos (void)snprintf(cp, i, "%s,%s", s0, s1);
391 1.1 christos }
392 1.1 christos else
393 1.1 christos cp = estrdup(s1);
394 1.1 christos
395 1.1 christos if (s0)
396 1.1 christos free(s0);
397 1.1 christos return (cp);
398 1.1 christos }
399 1.1 christos
400 1.1 christos
401 1.1 christos static void
402 1.1 christos mangle(options, argcp, argv)
403 1.1 christos char *options;
404 1.1 christos int *argcp;
405 1.1 christos const char **argv;
406 1.1 christos {
407 1.1 christos char *p, *s;
408 1.1 christos int argc;
409 1.1 christos
410 1.1 christos argc = *argcp;
411 1.1 christos for (s = options; (p = strsep(&s, ",")) != NULL;)
412 1.1 christos if (*p != '\0')
413 1.1 christos if (*p == '-') {
414 1.1 christos argv[argc++] = p;
415 1.1 christos p = strchr(p, '=');
416 1.1 christos if (p) {
417 1.1 christos *p = '\0';
418 1.1 christos argv[argc++] = p+1;
419 1.1 christos }
420 1.1 christos }
421 1.1 christos else {
422 1.1 christos argv[argc++] = "-o";
423 1.1 christos argv[argc++] = p;
424 1.1 christos }
425 1.1 christos
426 1.1 christos *argcp = argc;
427 1.1 christos }
428 1.1 christos
429 1.1 christos
430 1.1 christos static void *
431 1.1 christos emalloc(s)
432 1.1 christos size_t s;
433 1.1 christos {
434 1.1 christos void *p = malloc(s);
435 1.1 christos if (p == NULL)
436 1.1 christos err(1, "malloc failed");
437 1.1 christos return p;
438 1.1 christos }
439 1.1 christos
440 1.1 christos
441 1.1 christos static char *
442 1.1 christos estrdup(s)
443 1.1 christos const char *s;
444 1.1 christos {
445 1.1 christos char *p = strdup(s);
446 1.1 christos if (p == NULL)
447 1.1 christos err(1, "strdup failed");
448 1.1 christos return p;
449 1.1 christos }
450 1.1 christos
451 1.1 christos
452 1.1 christos static void
453 1.1 christos usage()
454 1.1 christos {
455 1.1 christos extern char *__progname;
456 1.1 christos static const char common[] = "dvYyNn] [-o fsoptions] [-t fstype]";
457 1.1 christos
458 1.1 christos (void)fprintf(stderr, "Usage: %s [-p%s\n\t%s [-%s [special|node]...\n",
459 1.1 christos __progname, common, __progname, common);
460 1.1 christos exit(1);
461 1.1 christos }
462