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

📄 mount.c

📁 Util-linux 软件包包含许多工具。其中比较重要的是加载、卸载、格式化、分区和管理硬盘驱动器
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * A mount(8) for Linux 0.99. * mount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp * * Modifications by many people. Distributed under GPL. */#include <unistd.h>#include <ctype.h>#include <errno.h>#include <string.h>#include <getopt.h>#include <stdio.h>#include <pwd.h>#include <grp.h>#include <sys/types.h>#include <sys/ioctl.h>#include <sys/stat.h>#include <sys/wait.h>#include <sys/mount.h>#include "mount_blkid.h"#include "mount_constants.h"#include "sundries.h"#include "mntent.h"#include "fstab.h"#include "lomount.h"#include "loop.h"#include "linux_fs.h"		/* for BLKGETSIZE */#include "mount_guess_rootdev.h"#include "mount_guess_fstype.h"#include "mount_by_label.h"#include "getusername.h"#include "paths.h"#include "env.h"#include "nls.h"#define DO_PS_FIDDLING#ifdef DO_PS_FIDDLING#include "setproctitle.h"#endif/* True for fake mount (-f).  */static int fake = 0;/* True if we are allowed to call /sbin/mount.${FSTYPE} */static int external_allowed = 1;/* Don't write a entry in /etc/mtab (-n).  */static int nomtab = 0;/* True for explicit readonly (-r).  */static int readonly = 0;/* Nonzero for chatty (-v).  */int verbose = 0;/* Nonzero for sloppy (-s).  */int sloppy = 0;/* True for explicit read/write (-w).  */static int readwrite = 0;/* True for all mount (-a).  */int mount_all = 0;/* True for fork() during all mount (-F).  */static int optfork = 0;/* Add volumelabel in a listing of mounted devices (-l). */static int list_with_volumelabel = 0;/* Nonzero for mount {--bind|--replace|--before|--after|--over|--move} */static int mounttype = 0;/* True if ruid != euid.  */static int suid = 0;/* Contains the fd to read the passphrase from, if any. */static int pfd = -1;/* Map from -o and fstab option strings to the flag argument to mount(2).  */struct opt_map {  const char *opt;		/* option name */  int  skip;			/* skip in mtab option string */  int  inv;			/* true if flag value should be inverted */  int  mask;			/* flag mask value */};/* Custom mount options for our own purposes.  *//* Maybe these should now be freed for kernel use again */#define MS_NOAUTO	0x80000000#define MS_USERS	0x40000000#define MS_USER		0x20000000#define MS_OWNER	0x10000000#define MS_GROUP	0x08000000#define MS_COMMENT	0x00020000#define MS_LOOP		0x00010000/* Options that we keep the mount system call from seeing.  */#define MS_NOSYS	(MS_NOAUTO|MS_USERS|MS_USER|MS_COMMENT|MS_LOOP)/* Options that we keep from appearing in the options field in the mtab.  */#define MS_NOMTAB	(MS_REMOUNT|MS_NOAUTO|MS_USERS|MS_USER)/* Options that we make ordinary users have by default.  */#define MS_SECURE	(MS_NOEXEC|MS_NOSUID|MS_NODEV)/* Options that we make owner-mounted devices have by default */#define MS_OWNERSECURE	(MS_NOSUID|MS_NODEV)static const struct opt_map opt_map[] = {  { "defaults",	0, 0, 0		},	/* default options */  { "ro",	1, 0, MS_RDONLY	},	/* read-only */  { "rw",	1, 1, MS_RDONLY	},	/* read-write */  { "exec",	0, 1, MS_NOEXEC	},	/* permit execution of binaries */  { "noexec",	0, 0, MS_NOEXEC	},	/* don't execute binaries */  { "suid",	0, 1, MS_NOSUID	},	/* honor suid executables */  { "nosuid",	0, 0, MS_NOSUID	},	/* don't honor suid executables */  { "dev",	0, 1, MS_NODEV	},	/* interpret device files  */  { "nodev",	0, 0, MS_NODEV	},	/* don't interpret devices */  { "sync",	0, 0, MS_SYNCHRONOUS},	/* synchronous I/O */  { "async",	0, 1, MS_SYNCHRONOUS},	/* asynchronous I/O */  { "dirsync",	0, 0, MS_DIRSYNC},	/* synchronous directory modifications */  { "remount",  0, 0, MS_REMOUNT},      /* Alter flags of mounted FS */  { "bind",	0, 0, MS_BIND   },	/* Remount part of tree elsewhere */  { "rbind",	0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */  { "auto",	0, 1, MS_NOAUTO	},	/* Can be mounted using -a */  { "noauto",	0, 0, MS_NOAUTO	},	/* Can  only be mounted explicitly */  { "users",	0, 0, MS_USERS	},	/* Allow ordinary user to mount */  { "nousers",	0, 1, MS_USERS	},	/* Forbid ordinary user to mount */  { "user",	0, 0, MS_USER	},	/* Allow ordinary user to mount */  { "nouser",	0, 1, MS_USER	},	/* Forbid ordinary user to mount */  { "owner",	0, 0, MS_OWNER  },	/* Let the owner of the device mount */  { "noowner",	0, 1, MS_OWNER  },	/* Device owner has no special privs */  { "group",	0, 0, MS_GROUP  },	/* Let the group of the device mount */  { "nogroup",	0, 1, MS_GROUP  },	/* Device group has no special privs */  { "_netdev",	0, 0, MS_COMMENT},	/* Device requires network */  { "comment",	0, 0, MS_COMMENT},	/* fstab comment only (kudzu,_netdev)*/  /* add new options here */#ifdef MS_NOSUB  { "sub",	0, 1, MS_NOSUB	},	/* allow submounts */  { "nosub",	0, 0, MS_NOSUB	},	/* don't allow submounts */#endif#ifdef MS_SILENT  { "quiet",	0, 0, MS_SILENT    },	/* be quiet  */  { "loud",	0, 1, MS_SILENT    },	/* print out messages. */#endif#ifdef MS_MANDLOCK  { "mand",	0, 0, MS_MANDLOCK },	/* Allow mandatory locks on this FS */  { "nomand",	0, 1, MS_MANDLOCK },	/* Forbid mandatory locks on this FS */#endif  { "loop",	1, 0, MS_LOOP	},	/* use a loop device */#ifdef MS_NOATIME  { "atime",	0, 1, MS_NOATIME },	/* Update access time */  { "noatime",	0, 0, MS_NOATIME },	/* Do not update access time */#endif#ifdef MS_NODIRATIME  { "diratime",	0, 1, MS_NODIRATIME },	/* Update dir access times */  { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */#endif  { NULL,	0, 0, 0		}};static const char *opt_loopdev, *opt_vfstype, *opt_offset, *opt_encryption,	*opt_speed, *opt_comment;static struct string_opt_map {  char *tag;  int skip;  const char **valptr;} string_opt_map[] = {  { "loop=",	0, &opt_loopdev },  { "vfs=",	1, &opt_vfstype },  { "offset=",	0, &opt_offset },  { "encryption=", 0, &opt_encryption },  { "speed=", 0, &opt_speed },  { "comment=", 1, &opt_comment },  { NULL, 0, NULL }};static voidclear_string_opts(void) {	struct string_opt_map *m;	for (m = &string_opt_map[0]; m->tag; m++)		*(m->valptr) = NULL;}static intparse_string_opt(char *s) {	struct string_opt_map *m;	int lth;	for (m = &string_opt_map[0]; m->tag; m++) {		lth = strlen(m->tag);		if (!strncmp(s, m->tag, lth)) {			*(m->valptr) = xstrdup(s + lth);			return 1;		}	}	return 0;}int mount_quiet=0;/* Report on a single mount.  */static voidprint_one (const struct my_mntent *me) {	if (mount_quiet)		return;	printf ("%s on %s", me->mnt_fsname, me->mnt_dir);	if (me->mnt_type != NULL && *(me->mnt_type) != '\0')		printf (" type %s", me->mnt_type);	if (me->mnt_opts != NULL)		printf (" (%s)", me->mnt_opts);	if (list_with_volumelabel) {		const char *label;		label = mount_get_volume_label_by_spec(me->mnt_fsname);		if (label) {			printf (" [%s]", label);			/* free(label); */		}	}	printf ("\n");}/* Report on everything in mtab (of the specified types if any).  */static intprint_all (char *types) {     struct mntentchn *mc, *mc0;     mc0 = mtab_head();     for (mc = mc0->nxt; mc && mc != mc0; mc = mc->nxt) {	  if (matching_type (mc->m.mnt_type, types))	       print_one (&(mc->m));     }     exit (0);}static voidmy_free(const void *s) {	if (s)		free((void *) s);}/* * Look for OPT in opt_map table and return mask value. * If OPT isn't found, tack it onto extra_opts (which is non-NULL). * For the options uid= and gid= replace user or group name by its value. */static inline voidparse_opt (const char *opt, int *mask, char *extra_opts) {	const struct opt_map *om;	for (om = opt_map; om->opt != NULL; om++)		if (streq (opt, om->opt)) {			if (om->inv)				*mask &= ~om->mask;			else				*mask |= om->mask;			if ((om->mask == MS_USER || om->mask == MS_USERS)			    && !om->inv)				*mask |= MS_SECURE;			if ((om->mask == MS_OWNER || om->mask == MS_GROUP)			    && !om->inv)				*mask |= MS_OWNERSECURE;#ifdef MS_SILENT			if (om->mask == MS_SILENT && om->inv)  {				mount_quiet = 1;				verbose = 0;			}#endif			return;		}	if (*extra_opts)		strcat(extra_opts, ",");	/* convert nonnumeric ids to numeric */	if (!strncmp(opt, "uid=", 4) && !isdigit(opt[4])) {		struct passwd *pw = getpwnam(opt+4);		char uidbuf[20];		if (pw) {			sprintf(uidbuf, "uid=%d", pw->pw_uid);			strcat(extra_opts, uidbuf);			return;		}	}	if (!strncmp(opt, "gid=", 4) && !isdigit(opt[4])) {		struct group *gr = getgrnam(opt+4);		char gidbuf[20];		if (gr) {			sprintf(gidbuf, "gid=%d", gr->gr_gid);			strcat(extra_opts, gidbuf);			return;		}	}	strcat(extra_opts, opt);}  /* Take -o options list and compute 4th and 5th args to mount(2).  flags   gets the standard options (indicated by bits) and extra_opts all the rest */static voidparse_opts (const char *options, int *flags, char **extra_opts) {	*flags = 0;	*extra_opts = NULL;	clear_string_opts();	if (options != NULL) {		char *opts = xstrdup(options);		char *opt;		*extra_opts = xmalloc (strlen (opts) + 1); 		**extra_opts = '\0';		for (opt = strtok (opts, ","); opt; opt = strtok (NULL, ","))			if (!parse_string_opt (opt))				parse_opt (opt, flags, *extra_opts);		free(opts);	}	if (readonly)		*flags |= MS_RDONLY;	if (readwrite)		*flags &= ~MS_RDONLY;	*flags |= mounttype;}/* Try to build a canonical options string.  */static char *fix_opts_string (int flags, const char *extra_opts, const char *user) {	const struct opt_map *om;	const struct string_opt_map *m;	char *new_opts;	new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");	for (om = opt_map; om->opt != NULL; om++) {		if (om->skip)			continue;		if (om->inv || !om->mask || (flags & om->mask) != om->mask)			continue;		new_opts = xstrconcat3(new_opts, ",", om->opt);		flags &= ~om->mask;	}	for (m = &string_opt_map[0]; m->tag; m++) {		if (!m->skip && *(m->valptr))			new_opts = xstrconcat4(new_opts, ",",					       m->tag, *(m->valptr));	}	if (extra_opts && *extra_opts) {		new_opts = xstrconcat3(new_opts, ",", extra_opts);	}	if (user) {		new_opts = xstrconcat3(new_opts, ",user=", user);	}	return new_opts;}static intalready (const char *spec, const char *node) {	struct mntentchn *mc;	int ret = 1;	if ((mc = getmntfile(node)) != NULL)		error (_("mount: according to mtab, "			 "%s is already mounted on %s"),		       mc->m.mnt_fsname, node);	else if (spec && strcmp (spec, "none") &&		 (mc = getmntfile(spec)) != NULL)		error (_("mount: according to mtab, %s is mounted on %s"),		       spec, mc->m.mnt_dir);	else		ret = 0;	return ret;}/* Create mtab with a root entry.  */static voidcreate_mtab (void) {	struct mntentchn *fstab;	struct my_mntent mnt;	int flags;	mntFILE *mfp;	lock_mtab();	mfp = my_setmntent (MOUNTED, "a+");	if (mfp == NULL || mfp->mntent_fp == NULL) {		int errsv = errno;		die (EX_FILEIO, _("mount: can't open %s for writing: %s"),		     MOUNTED, strerror (errsv));	}	/* Find the root entry by looking it up in fstab */	if ((fstab = getfsfile ("/")) || (fstab = getfsfile ("root"))) {		char *extra_opts;		parse_opts (fstab->m.mnt_opts, &flags, &extra_opts);		mnt.mnt_dir = "/";		mnt.mnt_fsname = canonicalize (fstab->m.mnt_fsname);		mnt.mnt_type = fstab->m.mnt_type;		mnt.mnt_opts = fix_opts_string (flags, extra_opts, NULL);		mnt.mnt_freq = mnt.mnt_passno = 0;		my_free(extra_opts);		if (my_addmntent (mfp, &mnt) == 1) {			int errsv = errno;			die (EX_FILEIO, _("mount: error writing %s: %s"),			     MOUNTED, strerror (errsv));		}	}	if (fchmod (fileno (mfp->mntent_fp), 0644) < 0)		if (errno != EROFS) {			int errsv = errno;			die (EX_FILEIO,			     _("mount: error changing mode of %s: %s"),			     MOUNTED, strerror (errsv));		}	my_endmntent (mfp);	unlock_mtab();}/* count successful mount system calls */static int mountcount = 0;/* * do_mount_syscall() *	Mount a single file system. Keep track of successes. * returns: 0: OK, -1: error in errno */static intdo_mount_syscall (struct mountargs *args) {	int flags = args->flags;	int ret;	if ((flags & MS_MGC_MSK) == 0)		flags |= MS_MGC_VAL;	ret = mount (args->spec, args->node, args->type, flags, args->data);	if (ret == 0)		mountcount++;	return ret;}/* * guess_fstype_and_mount() *	Mount a single file system. Guess the type when unknown. * returns: 0: OK, -1: error in errno, 1: other error *	don't exit on non-fatal errors. *	on return types is filled with the type used. */static intguess_fstype_and_mount(const char *spec, const char *node, const char **types,		       int flags, char *mount_opts) {   struct mountargs args = { spec, node, NULL, flags & ~MS_NOSYS, mount_opts };      if (*types && strcasecmp (*types, "auto") == 0)      *types = NULL;   if (!*types && (flags & (MS_BIND | MS_MOVE)))      *types = "none";		/* random, but not "bind" */   if (!*types && !(flags & MS_REMOUNT)) {      *types = guess_fstype(spec);      if (*types && !strcmp(*types, "swap")) {	  error(_("%s looks like swapspace - not mounted"), spec);	  *types = NULL;	  return 1;      }   }   /* Accept a comma-separated list of types, and try them one by one */   /* A list like "nonfs,.." indicates types not to use */   if (*types && strncmp(*types, "no", 2) && index(*types,',')) {      char *t = strdup(*types);      char *p;      while((p = index(t,',')) != NULL) {	 *p = 0;	 args.type = *types = t;	 if(do_mount_syscall (&args) == 0)	    return 0;	 t = p+1;      }      /* do last type below */      *types = t;   }   if (*types || (flags & MS_REMOUNT)) {      args.type = *types;      return do_mount_syscall (&args);   }   return procfsloop(do_mount_syscall, &args, types);}/* * suid_check() *	Die if the user is not allowed to do this. */static voidsuid_check(const char *spec, const char *node, int *flags, char **user) {  if (suid) {      /*       * MS_OWNER: Allow owners to mount when fstab contains       * the owner option.  Note that this should never be used       * in a high security environment, but may be useful to give       * people at the console the possibility of mounting a floppy.       * MS_GROUP: Allow members of device group to mount. (Martin Dickopp)       */      if (*flags & (MS_OWNER | MS_GROUP)) {	  struct stat sb;	  if (!strncmp(spec, "/dev/", 5) && stat(spec, &sb) == 0) {	      if (*flags & MS_OWNER) {		  if (getuid() == sb.st_uid)		      *flags |= MS_USER;	      }	      if (*flags & MS_GROUP) {		  if (getgid() == sb.st_gid)		      *flags |= MS_USER;		  else {		      int n = getgroups(0, NULL);		      if (n > 0) {			      gid_t *groups = xmalloc(n * sizeof(*groups));			      if (getgroups(n, groups) == n) {				      int i;				      for (i = 0; i < n; i++) {					      if (groups[i] == sb.st_gid) {						      *flags |= MS_USER;						      break;					      }				      }			      }			      free(groups);		      }		  }	      }	  }      }      /* James Kehl <mkehl@gil.com.au> came with a similar patch:	 allow an arbitrary user to mount when he is the owner of	 the mount-point and has write-access to the device.	 This is even less secure. Let me skip it for the time being;	 there should be an explicit fstab line allowing such things. */      if (!(*flags & (MS_USER | MS_USERS))) {	  if (already (spec, node))	    die (EX_USAGE, _("mount failed"));	  else	    die (EX_USAGE, _("mount: only root can mount %s on %s"), spec, node);      }      if (*flags & MS_USER)

⌨️ 快捷键说明

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