⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fsck.c

📁 busybox最新版的源码:学习和应用的好东东,多的不说了,大家看后再说吧
💻 C
📖 第 1 页 / 共 2 页
字号:
/* vi: set sw=4 ts=4: *//* * pfsck --- A generic, parallelizing front-end for the fsck program. * It will automatically try to run fsck programs in parallel if the * devices are on separate spindles.  It is based on the same ideas as * the generic front end for fsck by David Engel and Fred van Kempen, * but it has been completely rewritten from scratch to support * parallel execution. * * Written by Theodore Ts'o, <tytso@mit.edu> * * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994: *   o Changed -t fstype to behave like with mount when -A (all file *     systems) or -M (like mount) is specified. *   o fsck looks if it can find the fsck.type program to decide *     if it should ignore the fs type. This way more fsck programs *     can be added without changing this front-end. *   o -R flag skip root file system. * * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, *      2001, 2002, 2003, 2004, 2005 by  Theodore Ts'o. * * %Begin-Header% * This file may be redistributed under the terms of the GNU Public * License. * %End-Header% */#include <sys/types.h>#include <sys/wait.h>#include <sys/stat.h>#include <limits.h>#include <stdio.h>#include <ctype.h>#include <string.h>#include <time.h>#include <stdlib.h>#include <errno.h>#include <paths.h>#include <unistd.h>#include <errno.h>#include <signal.h>#include "fsck.h"#include "blkid/blkid.h"#include "e2fsbb.h"#include "libbb.h"#ifndef _PATH_MNTTAB#define _PATH_MNTTAB    "/etc/fstab"#endif/* * fsck.h */#ifndef DEFAULT_FSTYPE#define DEFAULT_FSTYPE	"ext2"#endif#define MAX_DEVICES 32#define MAX_ARGS 32/* * Internal structure for mount tabel entries. */struct fs_info {	char  *device;	char  *mountpt;	char  *type;	char  *opts;	int   freq;	int   passno;	int   flags;	struct fs_info *next;};#define FLAG_DONE 1#define FLAG_PROGRESS 2/* * Structure to allow exit codes to be stored */struct fsck_instance {	int	pid;	int	flags;	int	exit_status;	time_t	start_time;	char *	prog;	char *	type;	char *	device;	char *	base_device;	struct fsck_instance *next;};/* * base_device.c * * Return the "base device" given a particular device; this is used to * assure that we only fsck one partition on a particular drive at any * one time.  Otherwise, the disk heads will be seeking all over the * place.  If the base device cannot be determined, return NULL. * * The base_device() function returns an allocated string which must * be freed. * */#ifdef CONFIG_FEATURE_DEVFS/* * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3 * pathames. */static const char *const devfs_hier[] = {	"host", "bus", "target", "lun", 0};#endifstatic char *base_device(const char *device){	char *str, *cp;#ifdef CONFIG_FEATURE_DEVFS	const char *const *hier;	const char *disk;	int len;#endif	cp = str = xstrdup(device);	/* Skip over /dev/; if it's not present, give up. */	if (strncmp(cp, "/dev/", 5) != 0)		goto errout;	cp += 5;	/*	 * For md devices, we treat them all as if they were all	 * on one disk, since we don't know how to parallelize them.	 */	if (cp[0] == 'm' && cp[1] == 'd') {		*(cp+2) = 0;		return str;	}	/* Handle DAC 960 devices */	if (strncmp(cp, "rd/", 3) == 0) {		cp += 3;		if (cp[0] != 'c' || cp[2] != 'd' ||		    !isdigit(cp[1]) || !isdigit(cp[3]))			goto errout;		*(cp+4) = 0;		return str;	}	/* Now let's handle /dev/hd* and /dev/sd* devices.... */	if ((cp[0] == 'h' || cp[0] == 's') && (cp[1] == 'd')) {		cp += 2;		/* If there's a single number after /dev/hd, skip it */		if (isdigit(*cp))			cp++;		/* What follows must be an alpha char, or give up */		if (!isalpha(*cp))			goto errout;		*(cp + 1) = 0;		return str;	}#ifdef CONFIG_FEATURE_DEVFS	/* Now let's handle devfs (ugh) names */	len = 0;	if (strncmp(cp, "ide/", 4) == 0)		len = 4;	if (strncmp(cp, "scsi/", 5) == 0)		len = 5;	if (len) {		cp += len;		/*		 * Now we proceed down the expected devfs hierarchy.		 * i.e., .../host1/bus2/target3/lun4/...		 * If we don't find the expected token, followed by		 * some number of digits at each level, abort.		 */		for (hier = devfs_hier; *hier; hier++) {			len = strlen(*hier);			if (strncmp(cp, *hier, len) != 0)				goto errout;			cp += len;			while (*cp != '/' && *cp != 0) {				if (!isdigit(*cp))					goto errout;				cp++;			}			cp++;		}		*(cp - 1) = 0;		return str;	}	/* Now handle devfs /dev/disc or /dev/disk names */	disk = 0;	if (strncmp(cp, "discs/", 6) == 0)		disk = "disc";	else if (strncmp(cp, "disks/", 6) == 0)		disk = "disk";	if (disk) {		cp += 6;		if (strncmp(cp, disk, 4) != 0)			goto errout;		cp += 4;		while (*cp != '/' && *cp != 0) {			if (!isdigit(*cp))				goto errout;			cp++;		}		*cp = 0;		return str;	}#endiferrout:	free(str);	return NULL;}static const char *const ignored_types[] = {	"ignore",	"iso9660",	"nfs",	"proc",	"sw",	"swap",	"tmpfs",	"devpts",	NULL};static const char *const really_wanted[] = {	"minix",	"ext2",	"ext3",	"jfs",	"reiserfs",	"xiafs",	"xfs",	NULL};#define BASE_MD "/dev/md"/* * Global variables for options */static char *devices[MAX_DEVICES];static char *args[MAX_ARGS];static int num_devices, num_args;static int verbose;static int doall;static int noexecute;static int serialize;static int skip_root;static int like_mount;static int notitle;static int parallel_root;static int progress;static int progress_fd;static int force_all_parallel;static int num_running;static int max_running;static volatile int cancel_requested;static int kill_sent;static char *fstype;static struct fs_info *filesys_info, *filesys_last;static struct fsck_instance *instance_list;static char *fsck_path;static blkid_cache cache;static char *string_copy(const char *s){	char    *ret;	if (!s)		return 0;	ret = strdup(s);	return ret;}static int string_to_int(const char *s){	long l;	char *p;	l = strtol(s, &p, 0);	if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)		return -1;	else		return (int) l;}static char *skip_over_blank(char *cp){	while (*cp && isspace(*cp))		cp++;	return cp;}static char *skip_over_word(char *cp){	while (*cp && !isspace(*cp))		cp++;	return cp;}static void strip_line(char *line){	char    *p;	while (*line) {		p = line + strlen(line) - 1;		if ((*p == '\n') || (*p == '\r'))			*p = 0;		else			break;	}}static char *parse_word(char **buf){	char *word, *next;	word = *buf;	if (*word == 0)		return 0;	word = skip_over_blank(word);	next = skip_over_word(word);	if (*next)		*next++ = 0;	*buf = next;	return word;}static void parse_escape(char *word){	char    *q, c;	const char *p;	if (!word)		return;	for (p = q = word; *p; q++) {		c = *p++;		if (c != '\\') {			*q = c;		} else {			*q = bb_process_escape_sequence(&p);		}	}	*q = 0;}static void free_instance(struct fsck_instance *i){	if (i->prog)		free(i->prog);	if (i->device)		free(i->device);	if (i->base_device)		free(i->base_device);	free(i);}static struct fs_info *create_fs_device(const char *device, const char *mntpnt,					const char *type, const char *opts,					int freq, int passno){	struct fs_info *fs;	if (!(fs = malloc(sizeof(struct fs_info))))		return NULL;	fs->device = string_copy(device);	fs->mountpt = string_copy(mntpnt);	fs->type = string_copy(type);	fs->opts = string_copy(opts ? opts : "");	fs->freq = freq;	fs->passno = passno;	fs->flags = 0;	fs->next = NULL;	if (!filesys_info)		filesys_info = fs;	else		filesys_last->next = fs;	filesys_last = fs;	return fs;}static int parse_fstab_line(char *line, struct fs_info **ret_fs){	char    *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;	struct fs_info *fs;	*ret_fs = 0;	strip_line(line);	if ((cp = strchr(line, '#')))		*cp = 0;        /* Ignore everything after the comment char */	cp = line;	device = parse_word(&cp);	mntpnt = parse_word(&cp);	type = parse_word(&cp);	opts = parse_word(&cp);	freq = parse_word(&cp);	passno = parse_word(&cp);	if (!device)		return 0;       /* Allow blank lines */	if (!mntpnt || !type)		return -1;	parse_escape(device);	parse_escape(mntpnt);	parse_escape(type);	parse_escape(opts);	parse_escape(freq);	parse_escape(passno);	dev = blkid_get_devname(cache, device, NULL);	if (dev)		device = dev;	if (strchr(type, ','))		type = 0;	fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,			      freq ? atoi(freq) : -1,			      passno ? atoi(passno) : -1);	if (dev)		free(dev);	if (!fs)		return -1;	*ret_fs = fs;	return 0;}static void interpret_type(struct fs_info *fs){	char    *t;	if (strcmp(fs->type, "auto") != 0)		return;	t = blkid_get_tag_value(cache, "TYPE", fs->device);	if (t) {		free(fs->type);		fs->type = t;	}}/* * Load the filesystem database from /etc/fstab */static void load_fs_info(const char *filename){	FILE    *f;	char    buf[1024];	int     lineno = 0;	int     old_fstab = 1;	struct fs_info *fs;	if ((f = fopen(filename, "r")) == NULL) {		bb_perror_msg("WARNING: cannot open %s", filename);		return;	}	while (!feof(f)) {		lineno++;		if (!fgets(buf, sizeof(buf), f))			break;		buf[sizeof(buf)-1] = 0;		if (parse_fstab_line(buf, &fs) < 0) {			bb_error_msg("WARNING: bad format "				"on line %d of %s\n", lineno, filename);			continue;		}		if (!fs)			continue;		if (fs->passno < 0)			fs->passno = 0;		else			old_fstab = 0;	}	fclose(f);	if (old_fstab) {		fputs("\007\007\007"		"WARNING: Your /etc/fstab does not contain the fsck passno\n"		"       field.  I will kludge around things for you, but you\n"		"       should fix your /etc/fstab file as soon as you can.\n\n", stderr);		for (fs = filesys_info; fs; fs = fs->next) {			fs->passno = 1;		}	}}/* Lookup filesys in /etc/fstab and return the corresponding entry. */static struct fs_info *lookup(char *filesys){	struct fs_info *fs;	/* No filesys name given. */	if (filesys == NULL)		return NULL;	for (fs = filesys_info; fs; fs = fs->next) {		if (!strcmp(filesys, fs->device) ||		    (fs->mountpt && !strcmp(filesys, fs->mountpt)))			break;	}	return fs;}/* Find fsck program for a given fs type. */static char *find_fsck(char *type){  char *s;  const char *tpl;  char *p = string_copy(fsck_path);  struct stat st;  /* Are we looking for a program or just a type? */  tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");  for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {	s = xasprintf(tpl, s, type);	if (stat(s, &st) == 0) break;	free(s);  }  free(p);  return s;}static int progress_active(void){	struct fsck_instance *inst;	for (inst = instance_list; inst; inst = inst->next) {		if (inst->flags & FLAG_DONE)			continue;		if (inst->flags & FLAG_PROGRESS)			return 1;	}	return 0;}/* * Execute a particular fsck program, and link it into the list of * child processes we are waiting for. */static int execute(const char *type, const char *device, const char *mntpt,		   int interactive){	char *s, *argv[80];	char *prog;	int  argc, i;	struct fsck_instance *inst, *p;	pid_t   pid;	inst = malloc(sizeof(struct fsck_instance));	if (!inst)		return ENOMEM;	memset(inst, 0, sizeof(struct fsck_instance));	prog = xasprintf("fsck.%s", type);	argv[0] = prog;	argc = 1;	for (i=0; i <num_args; i++)		argv[argc++] = string_copy(args[i]);	if (progress && !progress_active()) {		if ((strcmp(type, "ext2") == 0) ||		    (strcmp(type, "ext3") == 0)) {			char tmp[80];			snprintf(tmp, 80, "-C%d", progress_fd);			argv[argc++] = string_copy(tmp);			inst->flags |= FLAG_PROGRESS;		}	}	argv[argc++] = string_copy(device);	argv[argc] = 0;	s = find_fsck(prog);	if (s == NULL) {		bb_error_msg("%s: not found", prog);		return ENOENT;	}	if (verbose || noexecute) {		printf("[%s (%d) -- %s] ", s, num_running,		       mntpt ? mntpt : device);		for (i=0; i < argc; i++)			printf("%s ", argv[i]);		puts("");	}	/* Fork and execute the correct program. */	if (noexecute)		pid = -1;	else if ((pid = fork()) < 0) {		perror("fork");		return errno;	} else if (pid == 0) {		if (!interactive)			close(0);		(void) execv(s, argv);		bb_perror_msg_and_die("%s", argv[0]);	}	for (i = 1; i < argc; i++)		free(argv[i]);	free(s);	inst->pid = pid;	inst->prog = prog;	inst->type = string_copy(type);	inst->device = string_copy(device);	inst->base_device = base_device(device);	inst->start_time = time(0);	inst->next = NULL;	/*	 * Find the end of the list, so we add the instance on at the end.	 */	for (p = instance_list; p && p->next; p = p->next);	if (p)		p->next = inst;	else		instance_list = inst;	return 0;}/* * Send a signal to all outstanding fsck child processes */static int kill_all(int signum){	struct fsck_instance *inst;	int     n = 0;	for (inst = instance_list; inst; inst = inst->next) {		if (inst->flags & FLAG_DONE)			continue;		kill(inst->pid, signum);		n++;	}	return n;}/* * Wait for one child process to exit; when it does, unlink it from * the list of executing child processes, and return it. */static struct fsck_instance *wait_one(int flags){	int     status;	int     sig;	struct fsck_instance *inst, *inst2, *prev;	pid_t   pid;	if (!instance_list)		return NULL;	if (noexecute) {		inst = instance_list;		prev = 0;#ifdef RANDOM_DEBUG		while (inst->next && (random() & 1)) {			prev = inst;			inst = inst->next;		}#endif		inst->exit_status = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -