Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sbin/fsck/fsck.c
39475 views
1
/* $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $ */
2
3
/*-
4
* SPDX-License-Identifier: BSD-3-Clause
5
*
6
* Copyright (c) 1996 Christos Zoulas. All rights reserved.
7
* Copyright (c) 1980, 1989, 1993, 1994
8
* The Regents of the University of California. All rights reserved.
9
*
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
12
* are met:
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
* 3. Neither the name of the University nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
21
*
22
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
* SUCH DAMAGE.
33
* From: $NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
34
* $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $
35
*/
36
37
#include <sys/param.h>
38
#include <sys/mount.h>
39
#include <sys/queue.h>
40
#include <sys/wait.h>
41
#include <sys/disk.h>
42
#include <sys/ioctl.h>
43
44
#include <ctype.h>
45
#include <err.h>
46
#include <fstab.h>
47
#include <fcntl.h>
48
#include <mntopts.h>
49
#include <paths.h>
50
#include <signal.h>
51
#include <stdio.h>
52
#include <stdlib.h>
53
#include <string.h>
54
#include <unistd.h>
55
56
#include "fsutil.h"
57
58
static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
59
60
static TAILQ_HEAD(fstypelist, entry) opthead, selhead;
61
62
struct entry {
63
char *type;
64
char *options;
65
TAILQ_ENTRY(entry) entries;
66
};
67
68
static char *options = NULL;
69
static int flags = 0;
70
static int forceflag = 0;
71
72
static int checkfs(const char *, const char *, const char *, const char *, pid_t *);
73
static int selected(const char *);
74
static void addoption(char *);
75
static const char *getoptions(const char *);
76
static void addentry(struct fstypelist *, const char *, const char *);
77
static void maketypelist(char *);
78
static void catopt(char **, const char *);
79
static void mangle(char *, int *, const char ** volatile *, int *);
80
static const char *getfstype(const char *);
81
static void usage(void) __dead2;
82
static int isok(struct fstab *);
83
84
static struct {
85
const char *ptype;
86
const char *name;
87
} ptype_map[] = {
88
{ "ufs", "ffs" },
89
{ "ffs", "ffs" },
90
{ "fat", "msdosfs" },
91
{ "efi", "msdosfs" },
92
{ NULL, NULL },
93
};
94
95
int
96
main(int argc, char *argv[])
97
{
98
struct fstab *fs;
99
int i, rval = 0;
100
const char *vfstype = NULL;
101
char globopt[3];
102
const char *etc_fstab;
103
104
globopt[0] = '-';
105
globopt[2] = '\0';
106
107
TAILQ_INIT(&selhead);
108
TAILQ_INIT(&opthead);
109
110
etc_fstab = NULL;
111
while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1)
112
switch (i) {
113
case 'B':
114
if (flags & CHECK_BACKGRD)
115
errx(1, "Cannot specify -B and -F.");
116
flags |= DO_BACKGRD;
117
break;
118
119
case 'd':
120
flags |= CHECK_DEBUG;
121
break;
122
123
case 'v':
124
flags |= CHECK_VERBOSE;
125
break;
126
127
case 'F':
128
if (flags & DO_BACKGRD)
129
errx(1, "Cannot specify -B and -F.");
130
flags |= CHECK_BACKGRD;
131
break;
132
133
case 'p':
134
flags |= CHECK_PREEN;
135
/*FALLTHROUGH*/
136
case 'C':
137
flags |= CHECK_CLEAN;
138
/*FALLTHROUGH*/
139
case 'n':
140
case 'y':
141
globopt[1] = i;
142
catopt(&options, globopt);
143
break;
144
145
case 'f':
146
forceflag = 1;
147
globopt[1] = i;
148
catopt(&options, globopt);
149
break;
150
151
case 'l':
152
warnx("Ignoring obsolete -l option\n");
153
break;
154
155
case 'T':
156
if (*optarg)
157
addoption(optarg);
158
break;
159
160
case 't':
161
if (!TAILQ_EMPTY(&selhead))
162
errx(1, "only one -t option may be specified.");
163
164
maketypelist(optarg);
165
vfstype = optarg;
166
break;
167
168
case 'c':
169
etc_fstab = optarg;
170
break;
171
172
case '?':
173
default:
174
usage();
175
/* NOTREACHED */
176
}
177
178
argc -= optind;
179
argv += optind;
180
181
if (etc_fstab != NULL)
182
setfstab(etc_fstab);
183
184
if (argc == 0)
185
return checkfstab(flags, isok, checkfs);
186
187
#define BADTYPE(type) \
188
(strcmp(type, FSTAB_RO) && \
189
strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
190
191
192
for (; argc--; argv++) {
193
const char *spec, *mntpt, *type, *cp;
194
char device[MAXPATHLEN];
195
struct statfs *mntp;
196
197
mntpt = NULL;
198
spec = *argv;
199
cp = strrchr(spec, '/');
200
if (cp == NULL) {
201
(void)snprintf(device, sizeof(device), "%s%s",
202
_PATH_DEV, spec);
203
spec = device;
204
}
205
mntp = getmntpoint(spec);
206
if (mntp != NULL) {
207
spec = mntp->f_mntfromname;
208
mntpt = mntp->f_mntonname;
209
}
210
if ((fs = getfsfile(spec)) == NULL &&
211
(fs = getfsspec(spec)) == NULL) {
212
if (vfstype == NULL)
213
vfstype = getfstype(spec);
214
if (vfstype == NULL)
215
vfstype = "ufs";
216
type = vfstype;
217
devcheck(spec);
218
} else {
219
spec = fs->fs_spec;
220
type = fs->fs_vfstype;
221
mntpt = fs->fs_file;
222
if (BADTYPE(fs->fs_type))
223
errx(1, "%s has unknown file system type.",
224
spec);
225
}
226
if ((flags & CHECK_BACKGRD) &&
227
checkfs(type, spec, mntpt, "-F", NULL) == 0) {
228
printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
229
continue;
230
}
231
if ((flags & DO_BACKGRD) && forceflag == 0 &&
232
checkfs(type, spec, mntpt, "-F", NULL) != 0)
233
continue;
234
235
rval |= checkfs(type, spec, mntpt, NULL, NULL);
236
}
237
238
return rval;
239
}
240
241
242
static int
243
isok(struct fstab *fs)
244
{
245
int i;
246
247
if (fs->fs_passno == 0)
248
return (0);
249
if (BADTYPE(fs->fs_type))
250
return (0);
251
if (!selected(fs->fs_vfstype))
252
return (0);
253
/* If failok, always check now */
254
if (getfsopt(fs, "failok"))
255
return (1);
256
/*
257
* If the -B flag has been given, then process the needed
258
* background checks. Background checks cannot be run on
259
* file systems that will be mounted read-only or that were
260
* not mounted at boot time (typically those marked `noauto').
261
* If these basic tests are passed, check with the file system
262
* itself to see if it is willing to do background checking
263
* by invoking its check program with the -F flag.
264
*/
265
if (flags & DO_BACKGRD) {
266
if (!strcmp(fs->fs_type, FSTAB_RO))
267
return (0);
268
if (getmntpoint(fs->fs_spec) == NULL)
269
return (0);
270
if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
271
return (0);
272
return (1);
273
}
274
/*
275
* If the -F flag has been given, then consider deferring the
276
* check to background. Background checks cannot be run on
277
* file systems that will be mounted read-only or that will
278
* not be mounted at boot time (e.g., marked `noauto'). If
279
* these basic tests are passed, check with the file system
280
* itself to see if it is willing to defer to background
281
* checking by invoking its check program with the -F flag.
282
*/
283
if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
284
return (1);
285
for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
286
if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
287
break;
288
if (i >= 0)
289
return (1);
290
if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
291
return (1);
292
printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
293
return (0);
294
}
295
296
297
static int
298
checkfs(const char *pvfstype, const char *spec, const char *mntpt,
299
const char *auxopt, pid_t *pidp)
300
{
301
const char ** volatile argv;
302
pid_t pid;
303
int argc, i, status, maxargc;
304
char *optbuf, execbase[MAXPATHLEN];
305
char *vfstype = NULL;
306
const char *extra = NULL;
307
308
#ifdef __GNUC__
309
/* Avoid vfork clobbering */
310
(void) &optbuf;
311
(void) &vfstype;
312
#endif
313
/*
314
* We convert the vfstype to lowercase and any spaces to underscores
315
* to not confuse the issue
316
*
317
* XXX This is a kludge to make automatic filesystem type guessing
318
* from the disklabel work for "4.2BSD" filesystems. It does a
319
* very limited subset of transliteration to a normalised form of
320
* filesystem name, and we do not seem to enforce a filesystem
321
* name character set.
322
*/
323
vfstype = strdup(pvfstype);
324
if (vfstype == NULL)
325
perr("strdup(pvfstype)");
326
for (i = 0; i < (int)strlen(vfstype); i++) {
327
vfstype[i] = tolower(vfstype[i]);
328
if (vfstype[i] == ' ')
329
vfstype[i] = '_';
330
}
331
332
extra = getoptions(vfstype);
333
optbuf = NULL;
334
if (options)
335
catopt(&optbuf, options);
336
if (extra)
337
catopt(&optbuf, extra);
338
if (auxopt)
339
catopt(&optbuf, auxopt);
340
else if (flags & DO_BACKGRD)
341
catopt(&optbuf, "-B");
342
343
maxargc = 64;
344
argv = emalloc(sizeof(char *) * maxargc);
345
346
(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
347
argc = 0;
348
argv[argc++] = execbase;
349
if (optbuf)
350
mangle(optbuf, &argc, &argv, &maxargc);
351
argv[argc++] = spec;
352
argv[argc] = NULL;
353
354
if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
355
(void)printf("start %s %swait", mntpt,
356
pidp ? "no" : "");
357
for (i = 0; i < argc; i++)
358
(void)printf(" %s", argv[i]);
359
(void)printf("\n");
360
}
361
362
switch (pid = vfork()) {
363
case -1: /* Error. */
364
warn("vfork");
365
if (optbuf)
366
free(optbuf);
367
free(vfstype);
368
return (1);
369
370
case 0: /* Child. */
371
if ((flags & CHECK_DEBUG) && auxopt == NULL)
372
_exit(0);
373
374
/* Go find an executable. */
375
execvP(execbase, _PATH_SYSPATH, __DECONST(char * const *, argv));
376
if (spec)
377
warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
378
else
379
warn("exec %s in %s", execbase, _PATH_SYSPATH);
380
_exit(1);
381
/* NOTREACHED */
382
383
default: /* Parent. */
384
if (optbuf)
385
free(optbuf);
386
387
free(vfstype);
388
389
if (pidp) {
390
*pidp = pid;
391
return 0;
392
}
393
394
if (waitpid(pid, &status, 0) < 0) {
395
warn("waitpid");
396
return (1);
397
}
398
399
if (WIFEXITED(status)) {
400
if (WEXITSTATUS(status) != 0)
401
return (WEXITSTATUS(status));
402
}
403
else if (WIFSIGNALED(status)) {
404
warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
405
return (1);
406
}
407
break;
408
}
409
410
return (0);
411
}
412
413
414
static int
415
selected(const char *type)
416
{
417
struct entry *e;
418
419
/* If no type specified, it's always selected. */
420
TAILQ_FOREACH(e, &selhead, entries)
421
if (!strncmp(e->type, type, MFSNAMELEN))
422
return which == IN_LIST ? 1 : 0;
423
424
return which == IN_LIST ? 0 : 1;
425
}
426
427
428
static const char *
429
getoptions(const char *type)
430
{
431
struct entry *e;
432
433
TAILQ_FOREACH(e, &opthead, entries)
434
if (!strncmp(e->type, type, MFSNAMELEN))
435
return e->options;
436
return "";
437
}
438
439
440
static void
441
addoption(char *optstr)
442
{
443
char *newoptions;
444
struct entry *e;
445
446
if ((newoptions = strchr(optstr, ':')) == NULL)
447
errx(1, "Invalid option string");
448
449
*newoptions++ = '\0';
450
451
TAILQ_FOREACH(e, &opthead, entries)
452
if (!strncmp(e->type, optstr, MFSNAMELEN)) {
453
catopt(&e->options, newoptions);
454
return;
455
}
456
addentry(&opthead, optstr, newoptions);
457
}
458
459
460
static void
461
addentry(struct fstypelist *list, const char *type, const char *opts)
462
{
463
struct entry *e;
464
465
e = emalloc(sizeof(struct entry));
466
e->type = estrdup(type);
467
e->options = estrdup(opts);
468
TAILQ_INSERT_TAIL(list, e, entries);
469
}
470
471
472
static void
473
maketypelist(char *fslist)
474
{
475
char *ptr;
476
477
if ((fslist == NULL) || (fslist[0] == '\0'))
478
errx(1, "empty type list");
479
480
if (fslist[0] == 'n' && fslist[1] == 'o') {
481
fslist += 2;
482
which = NOT_IN_LIST;
483
}
484
else
485
which = IN_LIST;
486
487
while ((ptr = strsep(&fslist, ",")) != NULL)
488
addentry(&selhead, ptr, "");
489
490
}
491
492
493
static void
494
catopt(char **sp, const char *o)
495
{
496
char *s;
497
size_t i, j;
498
499
s = *sp;
500
if (s) {
501
i = strlen(s);
502
j = i + 1 + strlen(o) + 1;
503
s = erealloc(s, j);
504
(void)snprintf(s + i, j, ",%s", o);
505
} else
506
s = estrdup(o);
507
*sp = s;
508
}
509
510
511
static void
512
mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp)
513
{
514
char *p, *s;
515
int argc, maxargc;
516
const char **argv;
517
518
argc = *argcp;
519
argv = *argvp;
520
maxargc = *maxargcp;
521
522
for (s = opts; (p = strsep(&s, ",")) != NULL;) {
523
/* Always leave space for one more argument and the NULL. */
524
if (argc >= maxargc - 3) {
525
maxargc <<= 1;
526
argv = erealloc(argv, maxargc * sizeof(char *));
527
}
528
if (*p != '\0') {
529
if (*p == '-') {
530
argv[argc++] = p;
531
p = strchr(p, '=');
532
if (p) {
533
*p = '\0';
534
argv[argc++] = p+1;
535
}
536
} else {
537
argv[argc++] = "-o";
538
argv[argc++] = p;
539
}
540
}
541
}
542
543
*argcp = argc;
544
*argvp = argv;
545
*maxargcp = maxargc;
546
}
547
548
static const char *
549
getfstype(const char *str)
550
{
551
struct diocgattr_arg attr;
552
int fd, i;
553
554
if ((fd = open(str, O_RDONLY)) == -1)
555
err(1, "cannot open `%s'", str);
556
557
strncpy(attr.name, "PART::type", sizeof(attr.name));
558
memset(&attr.value, 0, sizeof(attr.value));
559
attr.len = sizeof(attr.value);
560
if (ioctl(fd, DIOCGATTR, &attr) == -1) {
561
(void) close(fd);
562
return(NULL);
563
}
564
(void) close(fd);
565
for (i = 0; ptype_map[i].ptype != NULL; i++)
566
if (strstr(attr.value.str, ptype_map[i].ptype) != NULL)
567
return (ptype_map[i].name);
568
return (NULL);
569
}
570
571
572
static void
573
usage(void)
574
{
575
static const char common[] =
576
"[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]";
577
578
(void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
579
getprogname(), common);
580
exit(1);
581
}
582
583