mount.c revision 163843
128671Ssteve/*- 21558Srgrimes * Copyright (c) 1980, 1989, 1993, 1994 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 4. Neither the name of the University nor the names of its contributors 141558Srgrimes * may be used to endorse or promote products derived from this software 151558Srgrimes * without specific prior written permission. 161558Srgrimes * 171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271558Srgrimes * SUCH DAMAGE. 281558Srgrimes */ 291558Srgrimes 301558Srgrimes#ifndef lint 3128671Sstevestatic const char copyright[] = 321558Srgrimes"@(#) Copyright (c) 1980, 1989, 1993, 1994\n\ 331558Srgrimes The Regents of the University of California. All rights reserved.\n"; 341558Srgrimes#endif /* not lint */ 351558Srgrimes 361558Srgrimes#ifndef lint 3728671Ssteve#if 0 3823678Speterstatic char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95"; 3937425Scharnier#endif 4028671Sstevestatic const char rcsid[] = 4150476Speter "$FreeBSD: head/sbin/mount/mount.c 163843 2006-10-31 21:54:51Z pjd $"; 421558Srgrimes#endif /* not lint */ 431558Srgrimes 441558Srgrimes#include <sys/param.h> 451558Srgrimes#include <sys/mount.h> 4623805Sbde#include <sys/stat.h> 471558Srgrimes#include <sys/wait.h> 481558Srgrimes 4995289Smux#include <ctype.h> 501558Srgrimes#include <err.h> 511558Srgrimes#include <errno.h> 521558Srgrimes#include <fstab.h> 53117031Sgordon#include <paths.h> 5423678Speter#include <pwd.h> 551558Srgrimes#include <signal.h> 56123242Siedowse#include <stdint.h> 571558Srgrimes#include <stdio.h> 581558Srgrimes#include <stdlib.h> 591558Srgrimes#include <string.h> 601558Srgrimes#include <unistd.h> 611558Srgrimes 6228671Ssteve#include "extern.h" 6352055Sphk#include "mntopts.h" 641558Srgrimes#include "pathnames.h" 651558Srgrimes 6646619Sjkoshy/* `meta' options */ 67125933Sgrog#define MOUNT_META_OPTION_FSTAB "fstab" 6846619Sjkoshy#define MOUNT_META_OPTION_CURRENT "current" 6946619Sjkoshy 7023805Sbdeint debug, fstab_style, verbose; 711558Srgrimes 7292882Simpchar *catopt(char *, const char *); 7392882Simpstruct statfs *getmntpt(const char *); 7492882Simpint hasopt(const char *, const char *); 7592882Simpint ismounted(struct fstab *, struct statfs *, int); 7692882Simpint isremountable(const char *); 77159473Sjmallettvoid mangle(char *, int *, char *[]); 7892882Simpchar *update_options(char *, char *, int); 7992882Simpint mountfs(const char *, const char *, const char *, 8092882Simp int, const char *, const char *); 8192882Simpvoid remopt(char *, const char *); 8292882Simpvoid prmount(struct statfs *); 8392882Simpvoid putfsent(const struct statfs *); 8492882Simpvoid usage(void); 8592882Simpchar *flags2opts(int); 861558Srgrimes 8737425Scharnier/* Map from mount options to printable formats. */ 881558Srgrimesstatic struct opt { 891558Srgrimes int o_opt; 901558Srgrimes const char *o_name; 911558Srgrimes} optnames[] = { 921558Srgrimes { MNT_ASYNC, "asynchronous" }, 931558Srgrimes { MNT_EXPORTED, "NFS exported" }, 941558Srgrimes { MNT_LOCAL, "local" }, 9518007Sdg { MNT_NOATIME, "noatime" }, 961558Srgrimes { MNT_NOEXEC, "noexec" }, 971558Srgrimes { MNT_NOSUID, "nosuid" }, 9835105Swosch { MNT_NOSYMFOLLOW, "nosymfollow" }, 991558Srgrimes { MNT_QUOTA, "with quotas" }, 1001558Srgrimes { MNT_RDONLY, "read-only" }, 1011558Srgrimes { MNT_SYNCHRONOUS, "synchronous" }, 1021558Srgrimes { MNT_UNION, "union" }, 10329890Skato { MNT_NOCLUSTERR, "noclusterr" }, 10429890Skato { MNT_NOCLUSTERW, "noclusterw" }, 10531144Sjulian { MNT_SUIDDIR, "suiddir" }, 10634266Sjulian { MNT_SOFTDEP, "soft-updates" }, 107101205Srwatson { MNT_MULTILABEL, "multilabel" }, 108105118Srwatson { MNT_ACLS, "acls" }, 109163843Spjd { MNT_GJOURNAL, "gjournal" }, 11046619Sjkoshy { 0, NULL } 1111558Srgrimes}; 1121558Srgrimes 11328671Ssteve/* 11428671Ssteve * List of VFS types that can be remounted without becoming mounted on top 11528671Ssteve * of each other. 11628671Ssteve * XXX Is this list correct? 11728671Ssteve */ 11828671Sstevestatic const char * 11928671Ssteveremountable_fs_names[] = { 12069056Sphantom "ufs", "ffs", "ext2fs", 12128671Ssteve 0 12228671Ssteve}; 12328671Ssteve 124158400Smaximstatic const char userquotaeq[] = "userquota="; 125158400Smaximstatic const char groupquotaeq[] = "groupquota="; 126158400Smaxim 127152344Srodrigcstatic int 128152344Srodrigcuse_mountprog(const char *vfstype) 129152344Srodrigc{ 130152344Srodrigc /* XXX: We need to get away from implementing external mount 131152344Srodrigc * programs for every filesystem, and move towards having 132152344Srodrigc * each filesystem properly implement the nmount() system call. 133152344Srodrigc */ 134152344Srodrigc unsigned int i; 135152344Srodrigc const char *fs[] = { 136152778Savatar "cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs", 137153360Srodrigc "nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs", 138152344Srodrigc "unionfs", 139152344Srodrigc NULL 140152344Srodrigc }; 141152344Srodrigc 142159473Sjmallett for (i = 0; fs[i] != NULL; ++i) { 143152344Srodrigc if (strcmp(vfstype, fs[i]) == 0) 144159473Sjmallett return (1); 145152344Srodrigc } 146152344Srodrigc 147159473Sjmallett return (0); 148152344Srodrigc} 149152344Srodrigc 150152344Srodrigcstatic int 151159473Sjmallettexec_mountprog(const char *name, const char *execname, char *const argv[]) 152152344Srodrigc{ 153152344Srodrigc pid_t pid; 154152344Srodrigc int status; 155152344Srodrigc 156152344Srodrigc switch (pid = fork()) { 157152344Srodrigc case -1: /* Error. */ 158152344Srodrigc warn("fork"); 159152344Srodrigc exit (1); 160152344Srodrigc case 0: /* Child. */ 161152344Srodrigc /* Go find an executable. */ 162152344Srodrigc execvP(execname, _PATH_SYSPATH, argv); 163152344Srodrigc if (errno == ENOENT) { 164152344Srodrigc warn("exec %s not found in %s", execname, 165152344Srodrigc _PATH_SYSPATH); 166152344Srodrigc } 167152344Srodrigc exit(1); 168152344Srodrigc default: /* Parent. */ 169152344Srodrigc if (waitpid(pid, &status, 0) < 0) { 170152344Srodrigc warn("waitpid"); 171152344Srodrigc return (1); 172152344Srodrigc } 173152344Srodrigc 174152344Srodrigc if (WIFEXITED(status)) { 175152344Srodrigc if (WEXITSTATUS(status) != 0) 176152344Srodrigc return (WEXITSTATUS(status)); 177152344Srodrigc } else if (WIFSIGNALED(status)) { 178152344Srodrigc warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 179152344Srodrigc return (1); 180152344Srodrigc } 181152344Srodrigc break; 182152344Srodrigc } 183152344Srodrigc 184152344Srodrigc return (0); 185152344Srodrigc} 186152344Srodrigc 187163672Srustatic int 188163672Sruspecified_ro(const char *arg) 189163671Sru{ 190163671Sru char *optbuf, *opt; 191163671Sru int ret = 0; 192163671Sru 193163671Sru optbuf = strdup(arg); 194163671Sru if (optbuf == NULL) 195163671Sru err(1, NULL); 196163671Sru 197163671Sru for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 198163671Sru if (strcmp(opt, "ro") == 0) { 199163671Sru ret = 1; 200163671Sru break; 201163671Sru } 202163671Sru } 203163671Sru free(optbuf); 204163671Sru return (ret); 205163671Sru} 206163671Sru 2071558Srgrimesint 208151042Srodrigcmain(int argc, char *argv[]) 2091558Srgrimes{ 21023678Speter const char *mntfromname, **vfslist, *vfstype; 2111558Srgrimes struct fstab *fs; 2121558Srgrimes struct statfs *mntbuf; 2131558Srgrimes FILE *mountdfp; 2141558Srgrimes pid_t pid; 215163671Sru int all, ch, i, init_flags, late, mntsize, rval, have_fstab, ro; 21695289Smux char *cp, *ep, *options; 2171558Srgrimes 218160303Sdes all = init_flags = late = 0; 219163671Sru ro = 0; 220163671Sru options = NULL; 2211558Srgrimes vfslist = NULL; 2221558Srgrimes vfstype = "ufs"; 223163672Sru while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1) 2241558Srgrimes switch (ch) { 2251558Srgrimes case 'a': 2261558Srgrimes all = 1; 2271558Srgrimes break; 2281558Srgrimes case 'd': 2291558Srgrimes debug = 1; 2301558Srgrimes break; 231113220Smdodd case 'F': 232113220Smdodd setfstab(optarg); 233113220Smdodd break; 2341558Srgrimes case 'f': 2351558Srgrimes init_flags |= MNT_FORCE; 2361558Srgrimes break; 237160303Sdes case 'l': 238160303Sdes late = 1; 239160303Sdes break; 2401558Srgrimes case 'o': 241163671Sru if (*optarg) { 242163671Sru options = catopt(options, optarg); 243163671Sru if (specified_ro(optarg)) 244163671Sru ro = 1; 245163671Sru } 2461558Srgrimes break; 24723805Sbde case 'p': 24823805Sbde fstab_style = 1; 24923805Sbde verbose = 1; 25023805Sbde break; 2511558Srgrimes case 'r': 25276198Sdd options = catopt(options, "ro"); 253163671Sru ro = 1; 2541558Srgrimes break; 2551558Srgrimes case 't': 2561558Srgrimes if (vfslist != NULL) 25737425Scharnier errx(1, "only one -t option may be specified"); 2581558Srgrimes vfslist = makevfslist(optarg); 2591558Srgrimes vfstype = optarg; 2601558Srgrimes break; 2611558Srgrimes case 'u': 2621558Srgrimes init_flags |= MNT_UPDATE; 2631558Srgrimes break; 2641558Srgrimes case 'v': 2651558Srgrimes verbose = 1; 2661558Srgrimes break; 2671558Srgrimes case 'w': 26876198Sdd options = catopt(options, "noro"); 2691558Srgrimes break; 2701558Srgrimes case '?': 2711558Srgrimes default: 2721558Srgrimes usage(); 2731558Srgrimes /* NOTREACHED */ 2741558Srgrimes } 2751558Srgrimes argc -= optind; 2761558Srgrimes argv += optind; 2771558Srgrimes 2781558Srgrimes#define BADTYPE(type) \ 2791558Srgrimes (strcmp(type, FSTAB_RO) && \ 2801558Srgrimes strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 2811558Srgrimes 282163671Sru if ((init_flags & MNT_UPDATE) && (ro == 0)) 283163671Sru options = catopt(options, "noro"); 284163671Sru 2851558Srgrimes rval = 0; 2861558Srgrimes switch (argc) { 2871558Srgrimes case 0: 28828671Ssteve if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 28928671Ssteve err(1, "getmntinfo"); 29028671Ssteve if (all) { 2911558Srgrimes while ((fs = getfsent()) != NULL) { 2921558Srgrimes if (BADTYPE(fs->fs_type)) 2931558Srgrimes continue; 29423678Speter if (checkvfsname(fs->fs_vfstype, vfslist)) 2951558Srgrimes continue; 29623678Speter if (hasopt(fs->fs_mntops, "noauto")) 29710288Sdg continue; 298160303Sdes if (hasopt(fs->fs_mntops, "late") && !late) 299160303Sdes continue; 30044811Sbde if (!(init_flags & MNT_UPDATE) && 30144811Sbde ismounted(fs, mntbuf, mntsize)) 30228671Ssteve continue; 303125194Sguido options = update_options(options, fs->fs_mntops, 304125194Sguido mntbuf->f_flags); 3051558Srgrimes if (mountfs(fs->fs_vfstype, fs->fs_spec, 30623805Sbde fs->fs_file, init_flags, options, 30723805Sbde fs->fs_mntops)) 30823805Sbde rval = 1; 3091558Srgrimes } 31028671Ssteve } else if (fstab_style) { 31117243Sjkh for (i = 0; i < mntsize; i++) { 31223678Speter if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 31317243Sjkh continue; 31423805Sbde putfsent(&mntbuf[i]); 31517243Sjkh } 31623805Sbde } else { 3171558Srgrimes for (i = 0; i < mntsize; i++) { 31823805Sbde if (checkvfsname(mntbuf[i].f_fstypename, 31923805Sbde vfslist)) 3201558Srgrimes continue; 32123678Speter prmount(&mntbuf[i]); 3221558Srgrimes } 3231558Srgrimes } 3241558Srgrimes exit(rval); 3251558Srgrimes case 1: 3261558Srgrimes if (vfslist != NULL) 3271558Srgrimes usage(); 3281558Srgrimes 329124201Sanholt rmslashes(*argv, *argv); 3301558Srgrimes if (init_flags & MNT_UPDATE) { 33152678Sgreen mntfromname = NULL; 33252678Sgreen have_fstab = 0; 3331558Srgrimes if ((mntbuf = getmntpt(*argv)) == NULL) 33452678Sgreen errx(1, "not currently mounted %s", *argv); 33552678Sgreen /* 33652678Sgreen * Only get the mntflags from fstab if both mntpoint 33752678Sgreen * and mntspec are identical. Also handle the special 33852678Sgreen * case where just '/' is mounted and 'spec' is not 33952678Sgreen * identical with the one from fstab ('/dev' is missing 34052678Sgreen * in the spec-string at boot-time). 34152678Sgreen */ 34246619Sjkoshy if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 34352678Sgreen if (strcmp(fs->fs_spec, 34452678Sgreen mntbuf->f_mntfromname) == 0 && 34552678Sgreen strcmp(fs->fs_file, 34652678Sgreen mntbuf->f_mntonname) == 0) { 34752678Sgreen have_fstab = 1; 34852678Sgreen mntfromname = mntbuf->f_mntfromname; 34952678Sgreen } else if (argv[0][0] == '/' && 35052678Sgreen argv[0][1] == '\0') { 35152678Sgreen fs = getfsfile("/"); 35252678Sgreen have_fstab = 1; 35352678Sgreen mntfromname = fs->fs_spec; 35452678Sgreen } 35552678Sgreen } 35652678Sgreen if (have_fstab) { 35746619Sjkoshy options = update_options(options, fs->fs_mntops, 35846619Sjkoshy mntbuf->f_flags); 35946619Sjkoshy } else { 36023678Speter mntfromname = mntbuf->f_mntfromname; 36146619Sjkoshy options = update_options(options, NULL, 36246619Sjkoshy mntbuf->f_flags); 36346619Sjkoshy } 36423678Speter rval = mountfs(mntbuf->f_fstypename, mntfromname, 36523678Speter mntbuf->f_mntonname, init_flags, options, 0); 36623678Speter break; 3671558Srgrimes } 36823678Speter if ((fs = getfsfile(*argv)) == NULL && 36923678Speter (fs = getfsspec(*argv)) == NULL) 370102231Strhodes errx(1, "%s: unknown special file or file system", 37123678Speter *argv); 37223678Speter if (BADTYPE(fs->fs_type)) 373102231Strhodes errx(1, "%s has unknown file system type", 37423678Speter *argv); 37523678Speter rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 37623678Speter init_flags, options, fs->fs_mntops); 3771558Srgrimes break; 3781558Srgrimes case 2: 3791558Srgrimes /* 38052678Sgreen * If -t flag has not been specified, the path cannot be 38195289Smux * found, spec contains either a ':' or a '@', then assume 382102231Strhodes * that an NFS file system is being specified ala Sun. 38395289Smux * Check if the hostname contains only allowed characters 38495289Smux * to reduce false positives. IPv6 addresses containing 38595289Smux * ':' will be correctly parsed only if the separator is '@'. 38695289Smux * The definition of a valid hostname is taken from RFC 1034. 3871558Srgrimes */ 388123162Siedowse if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL || 389123162Siedowse (ep = strchr(argv[0], ':')) != NULL)) { 39095318Smux if (*ep == '@') { 39195318Smux cp = ep + 1; 39295318Smux ep = cp + strlen(cp); 39395318Smux } else 39495318Smux cp = argv[0]; 39595289Smux while (cp != ep) { 39695289Smux if (!isdigit(*cp) && !isalpha(*cp) && 39795289Smux *cp != '.' && *cp != '-' && *cp != ':') 39895289Smux break; 39995289Smux cp++; 40095289Smux } 40195289Smux if (cp == ep) 40295289Smux vfstype = "nfs"; 40395289Smux } 4041558Srgrimes rval = mountfs(vfstype, 4051558Srgrimes argv[0], argv[1], init_flags, options, NULL); 4061558Srgrimes break; 4071558Srgrimes default: 4081558Srgrimes usage(); 4091558Srgrimes /* NOTREACHED */ 4101558Srgrimes } 4111558Srgrimes 4121558Srgrimes /* 4131558Srgrimes * If the mount was successfully, and done by root, tell mountd the 4141558Srgrimes * good news. Pid checks are probably unnecessary, but don't hurt. 4151558Srgrimes */ 4161558Srgrimes if (rval == 0 && getuid() == 0 && 4171558Srgrimes (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 41828671Ssteve if (fscanf(mountdfp, "%d", &pid) == 1 && 4191558Srgrimes pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH) 4201558Srgrimes err(1, "signal mountd"); 4211558Srgrimes (void)fclose(mountdfp); 4221558Srgrimes } 4231558Srgrimes 4241558Srgrimes exit(rval); 4251558Srgrimes} 4261558Srgrimes 4271558Srgrimesint 428151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize) 42928671Ssteve{ 430154512Spjd char realfsfile[PATH_MAX]; 43128671Ssteve int i; 43228671Ssteve 43328671Ssteve if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 434102231Strhodes /* the root file system can always be remounted */ 43528671Ssteve return (0); 43628671Ssteve 437154512Spjd /* The user may have specified a symlink in fstab, resolve the path */ 438154512Spjd if (realpath(fs->fs_file, realfsfile) == NULL) { 439154512Spjd /* Cannot resolve the path, use original one */ 440154512Spjd strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile)); 441154512Spjd } 442154512Spjd 44328671Ssteve for (i = mntsize - 1; i >= 0; --i) 444154512Spjd if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 && 44528671Ssteve (!isremountable(fs->fs_vfstype) || 44628671Ssteve strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)) 44728671Ssteve return (1); 44828671Ssteve return (0); 44928671Ssteve} 45028671Ssteve 45128671Ssteveint 452151042Srodrigcisremountable(const char *vfsname) 45328671Ssteve{ 45428671Ssteve const char **cp; 45528671Ssteve 45628671Ssteve for (cp = remountable_fs_names; *cp; cp++) 45728671Ssteve if (strcmp(*cp, vfsname) == 0) 45828671Ssteve return (1); 45928671Ssteve return (0); 46028671Ssteve} 46128671Ssteve 46228671Ssteveint 463151042Srodrigchasopt(const char *mntopts, const char *option) 46423678Speter{ 46523678Speter int negative, found; 46623678Speter char *opt, *optbuf; 46723678Speter 46823678Speter if (option[0] == 'n' && option[1] == 'o') { 46923678Speter negative = 1; 47023678Speter option += 2; 47123678Speter } else 47223678Speter negative = 0; 47323678Speter optbuf = strdup(mntopts); 47423678Speter found = 0; 47523678Speter for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 47623678Speter if (opt[0] == 'n' && opt[1] == 'o') { 47723678Speter if (!strcasecmp(opt + 2, option)) 47823678Speter found = negative; 47923678Speter } else if (!strcasecmp(opt, option)) 48023678Speter found = !negative; 48123678Speter } 48223678Speter free(optbuf); 48323678Speter return (found); 48423678Speter} 48523678Speter 48623678Speterint 487151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags, 488151042Srodrigc const char *options, const char *mntopts) 4891558Srgrimes{ 490152344Srodrigc char *argv[100]; 4911558Srgrimes struct statfs sf; 492152344Srodrigc int argc, i, ret; 493118580Simp char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; 4941558Srgrimes 49552055Sphk /* resolve the mountpoint with realpath(3) */ 49652055Sphk (void)checkpath(name, mntpath); 4971558Srgrimes name = mntpath; 4981558Srgrimes 49923678Speter if (mntopts == NULL) 50023678Speter mntopts = ""; 5011558Srgrimes optbuf = catopt(strdup(mntopts), options); 5021558Srgrimes 5031558Srgrimes if (strcmp(name, "/") == 0) 5041558Srgrimes flags |= MNT_UPDATE; 5051558Srgrimes if (flags & MNT_FORCE) 5061558Srgrimes optbuf = catopt(optbuf, "force"); 5071558Srgrimes if (flags & MNT_RDONLY) 5081558Srgrimes optbuf = catopt(optbuf, "ro"); 5091558Srgrimes /* 5101558Srgrimes * XXX 5111558Srgrimes * The mount_mfs (newfs) command uses -o to select the 51237425Scharnier * optimization mode. We don't pass the default "-o rw" 5131558Srgrimes * for that reason. 5141558Srgrimes */ 5151558Srgrimes if (flags & MNT_UPDATE) 5161558Srgrimes optbuf = catopt(optbuf, "update"); 5171558Srgrimes 51877577Sru /* Compatibility glue. */ 51977577Sru if (strcmp(vfstype, "msdos") == 0) 52077577Sru vfstype = "msdosfs"; 52177577Sru 522144133Scperciva /* Construct the name of the appropriate mount command */ 523144133Scperciva (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 524144133Scperciva 5251558Srgrimes argc = 0; 526144133Scperciva argv[argc++] = execname; 5271558Srgrimes mangle(optbuf, &argc, argv); 528152344Srodrigc argv[argc++] = strdup(spec); 529152344Srodrigc argv[argc++] = strdup(name); 5301558Srgrimes argv[argc] = NULL; 5311558Srgrimes 5321558Srgrimes if (debug) { 5331558Srgrimes (void)printf("exec: mount_%s", vfstype); 5341558Srgrimes for (i = 1; i < argc; i++) 5351558Srgrimes (void)printf(" %s", argv[i]); 5361558Srgrimes (void)printf("\n"); 5371558Srgrimes return (0); 5381558Srgrimes } 5391558Srgrimes 540152668Srodrigc if (use_mountprog(vfstype)) { 541152344Srodrigc ret = exec_mountprog(name, execname, argv); 542152344Srodrigc } else { 543152344Srodrigc ret = mount_fs(vfstype, argc, argv); 544152344Srodrigc } 5451558Srgrimes 546152344Srodrigc free(optbuf); 5471558Srgrimes 548152344Srodrigc if (verbose) { 549152344Srodrigc if (statfs(name, &sf) < 0) { 550152344Srodrigc warn("statfs %s", name); 5511558Srgrimes return (1); 5521558Srgrimes } 553152344Srodrigc if (fstab_style) 554152344Srodrigc putfsent(&sf); 555152344Srodrigc else 556152344Srodrigc prmount(&sf); 5571558Srgrimes } 5581558Srgrimes 5591558Srgrimes return (0); 5601558Srgrimes} 5611558Srgrimes 5621558Srgrimesvoid 563151042Srodrigcprmount(struct statfs *sfp) 56423678Speter{ 565151043Srodrigc int flags; 566151043Srodrigc unsigned int i; 5671558Srgrimes struct opt *o; 56823678Speter struct passwd *pw; 5691558Srgrimes 57052036Sn_hibma (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 57152036Sn_hibma sfp->f_fstypename); 5721558Srgrimes 57323678Speter flags = sfp->f_flags & MNT_VISFLAGMASK; 57452036Sn_hibma for (o = optnames; flags && o->o_opt; o++) 5751558Srgrimes if (flags & o->o_opt) { 57652036Sn_hibma (void)printf(", %s", o->o_name); 5771558Srgrimes flags &= ~o->o_opt; 5781558Srgrimes } 579125339Spjd /* 580125339Spjd * Inform when file system is mounted by an unprivileged user 581125339Spjd * or privileged non-root user. 582125339Spjd */ 583125365Snectar if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 58452036Sn_hibma (void)printf(", mounted by "); 58523678Speter if ((pw = getpwuid(sfp->f_owner)) != NULL) 58623678Speter (void)printf("%s", pw->pw_name); 58723678Speter else 58823678Speter (void)printf("%d", sfp->f_owner); 58923678Speter } 59065023Ssheldonh if (verbose) { 59165023Ssheldonh if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 592123242Siedowse (void)printf(", writes: sync %ju async %ju", 593123242Siedowse (uintmax_t)sfp->f_syncwrites, 594123242Siedowse (uintmax_t)sfp->f_asyncwrites); 59565023Ssheldonh if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 596123242Siedowse (void)printf(", reads: sync %ju async %ju", 597123242Siedowse (uintmax_t)sfp->f_syncreads, 598123242Siedowse (uintmax_t)sfp->f_asyncreads); 599123162Siedowse if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 600123162Siedowse printf(", fsid "); 601123162Siedowse for (i = 0; i < sizeof(sfp->f_fsid); i++) 602123162Siedowse printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 603123162Siedowse } 60465023Ssheldonh } 60552036Sn_hibma (void)printf(")\n"); 6061558Srgrimes} 6071558Srgrimes 6081558Srgrimesstruct statfs * 609151042Srodrigcgetmntpt(const char *name) 6101558Srgrimes{ 6111558Srgrimes struct statfs *mntbuf; 6121558Srgrimes int i, mntsize; 6131558Srgrimes 6141558Srgrimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 61552678Sgreen for (i = mntsize - 1; i >= 0; i--) { 6161558Srgrimes if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 6171558Srgrimes strcmp(mntbuf[i].f_mntonname, name) == 0) 6181558Srgrimes return (&mntbuf[i]); 61952678Sgreen } 6201558Srgrimes return (NULL); 6211558Srgrimes} 6221558Srgrimes 6231558Srgrimeschar * 624151042Srodrigccatopt(char *s0, const char *s1) 6251558Srgrimes{ 6261558Srgrimes size_t i; 6271558Srgrimes char *cp; 6281558Srgrimes 62946619Sjkoshy if (s1 == NULL || *s1 == '\0') 630159473Sjmallett return (s0); 63146619Sjkoshy 6321558Srgrimes if (s0 && *s0) { 6331558Srgrimes i = strlen(s0) + strlen(s1) + 1 + 1; 6341558Srgrimes if ((cp = malloc(i)) == NULL) 63537425Scharnier errx(1, "malloc failed"); 6361558Srgrimes (void)snprintf(cp, i, "%s,%s", s0, s1); 6371558Srgrimes } else 6381558Srgrimes cp = strdup(s1); 6391558Srgrimes 6401558Srgrimes if (s0) 6411558Srgrimes free(s0); 6421558Srgrimes return (cp); 6431558Srgrimes} 6441558Srgrimes 6451558Srgrimesvoid 646159473Sjmallettmangle(char *options, int *argcp, char *argv[]) 6471558Srgrimes{ 6481558Srgrimes char *p, *s; 6491558Srgrimes int argc; 6501558Srgrimes 6511558Srgrimes argc = *argcp; 6521558Srgrimes for (s = options; (p = strsep(&s, ",")) != NULL;) 65346619Sjkoshy if (*p != '\0') { 654152465Srodrigc if (strcmp(p, "noauto") == 0) { 655152465Srodrigc /* 656152465Srodrigc * Do not pass noauto option to nmount(). 657152465Srodrigc * or external mount program. noauto is 658152465Srodrigc * only used to prevent mounting a filesystem 659152465Srodrigc * when 'mount -a' is specified, and is 660152465Srodrigc * not a real mount option. 661152465Srodrigc */ 662152465Srodrigc continue; 663160303Sdes } else if (strcmp(p, "late") == 0) { 664160303Sdes /* 665160303Sdes * "late" is used to prevent certain file 666160303Sdes * systems from being mounted before late 667160303Sdes * in the boot cycle; for instance, 668160303Sdes * loopback NFS mounts can't be mounted 669160303Sdes * before mountd starts. 670160303Sdes */ 671160303Sdes continue; 672152732Srodrigc } else if (strcmp(p, "userquota") == 0) { 673152732Srodrigc continue; 674158400Smaxim } else if (strncmp(p, userquotaeq, 675158400Smaxim sizeof(userquotaeq) - 1) == 0) { 676158400Smaxim continue; 677152732Srodrigc } else if (strcmp(p, "groupquota") == 0) { 678152732Srodrigc continue; 679158400Smaxim } else if (strncmp(p, groupquotaeq, 680158400Smaxim sizeof(groupquotaeq) - 1) == 0) { 681158400Smaxim continue; 682152465Srodrigc } else if (*p == '-') { 6831558Srgrimes argv[argc++] = p; 6841558Srgrimes p = strchr(p, '='); 685123268Strhodes if (p != NULL) { 6861558Srgrimes *p = '\0'; 6871558Srgrimes argv[argc++] = p+1; 6881558Srgrimes } 689153038Srodrigc } else { 690152344Srodrigc argv[argc++] = strdup("-o"); 6911558Srgrimes argv[argc++] = p; 6921558Srgrimes } 69346619Sjkoshy } 6941558Srgrimes 6951558Srgrimes *argcp = argc; 6961558Srgrimes} 6971558Srgrimes 69846619Sjkoshy 69946619Sjkoshychar * 700159473Sjmallettupdate_options(char *opts, char *fstab, int curflags) 70146619Sjkoshy{ 70246619Sjkoshy char *o, *p; 70346619Sjkoshy char *cur; 70446619Sjkoshy char *expopt, *newopt, *tmpopt; 70546619Sjkoshy 70646619Sjkoshy if (opts == NULL) 707159473Sjmallett return (strdup("")); 70846619Sjkoshy 70946619Sjkoshy /* remove meta options from list */ 71046619Sjkoshy remopt(fstab, MOUNT_META_OPTION_FSTAB); 71146619Sjkoshy remopt(fstab, MOUNT_META_OPTION_CURRENT); 71246619Sjkoshy cur = flags2opts(curflags); 71346619Sjkoshy 71446619Sjkoshy /* 71546619Sjkoshy * Expand all meta-options passed to us first. 71646619Sjkoshy */ 71746619Sjkoshy expopt = NULL; 71846619Sjkoshy for (p = opts; (o = strsep(&p, ",")) != NULL;) { 71946619Sjkoshy if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 72046619Sjkoshy expopt = catopt(expopt, fstab); 72146619Sjkoshy else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 72246619Sjkoshy expopt = catopt(expopt, cur); 72346619Sjkoshy else 72446619Sjkoshy expopt = catopt(expopt, o); 72546619Sjkoshy } 72646619Sjkoshy free(cur); 72746619Sjkoshy free(opts); 72846619Sjkoshy 72946619Sjkoshy /* 73046619Sjkoshy * Remove previous contradictory arguments. Given option "foo" we 73146619Sjkoshy * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 73246619Sjkoshy * and "foo" - so we can deal with possible options like "notice". 73346619Sjkoshy */ 73446619Sjkoshy newopt = NULL; 73546619Sjkoshy for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 73646619Sjkoshy if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 73746619Sjkoshy errx(1, "malloc failed"); 738125933Sgrog 73946619Sjkoshy strcpy(tmpopt, "no"); 74046619Sjkoshy strcat(tmpopt, o); 74146619Sjkoshy remopt(newopt, tmpopt); 74246619Sjkoshy free(tmpopt); 74346619Sjkoshy 74446619Sjkoshy if (strncmp("no", o, 2) == 0) 74546619Sjkoshy remopt(newopt, o+2); 74646619Sjkoshy 74746619Sjkoshy newopt = catopt(newopt, o); 74846619Sjkoshy } 74946619Sjkoshy free(expopt); 75046619Sjkoshy 751159473Sjmallett return (newopt); 75246619Sjkoshy} 75346619Sjkoshy 7541558Srgrimesvoid 755159473Sjmallettremopt(char *string, const char *opt) 75646619Sjkoshy{ 75746619Sjkoshy char *o, *p, *r; 75846619Sjkoshy 75946619Sjkoshy if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 76046619Sjkoshy return; 76146619Sjkoshy 76246619Sjkoshy r = string; 76346619Sjkoshy 76446619Sjkoshy for (p = string; (o = strsep(&p, ",")) != NULL;) { 76546619Sjkoshy if (strcmp(opt, o) != 0) { 76646619Sjkoshy if (*r == ',' && *o != '\0') 76746619Sjkoshy r++; 76846619Sjkoshy while ((*r++ = *o++) != '\0') 76946619Sjkoshy ; 77046619Sjkoshy *--r = ','; 77146619Sjkoshy } 77246619Sjkoshy } 77346619Sjkoshy *r = '\0'; 77446619Sjkoshy} 77546619Sjkoshy 77646619Sjkoshyvoid 777159473Sjmallettusage(void) 7781558Srgrimes{ 7791558Srgrimes 78037425Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 781160303Sdes"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 782141611Sru" mount [-dfpruvw] special | node", 783141611Sru" mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 7841558Srgrimes exit(1); 7851558Srgrimes} 78617243Sjkh 78717243Sjkhvoid 788159473Sjmallettputfsent(const struct statfs *ent) 78917243Sjkh{ 79023805Sbde struct fstab *fst; 79146619Sjkoshy char *opts; 792125933Sgrog 79346619Sjkoshy opts = flags2opts(ent->f_flags); 794156229Skeramida 795156229Skeramida /* 796156229Skeramida * "rw" is not a real mount option; this is why we print NULL as "rw" 797156229Skeramida * if opts is still NULL here. 798156229Skeramida */ 79923805Sbde printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname, 800156229Skeramida ent->f_fstypename, opts ? opts : "rw"); 80146619Sjkoshy free(opts); 80217243Sjkh 80328671Ssteve if ((fst = getfsspec(ent->f_mntfromname))) 80423805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 80528671Ssteve else if ((fst = getfsfile(ent->f_mntonname))) 80623805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 80756038Sgreen else if (strcmp(ent->f_fstypename, "ufs") == 0) { 80856038Sgreen if (strcmp(ent->f_mntonname, "/") == 0) 80956038Sgreen printf("\t1 1\n"); 81056038Sgreen else 81156038Sgreen printf("\t2 2\n"); 81256038Sgreen } else 81323805Sbde printf("\t0 0\n"); 81417243Sjkh} 81546619Sjkoshy 81646619Sjkoshy 81746619Sjkoshychar * 818159473Sjmallettflags2opts(int flags) 81946619Sjkoshy{ 82046619Sjkoshy char *res; 82146619Sjkoshy 82246619Sjkoshy res = NULL; 82346619Sjkoshy 824155997Srodrigc if (flags & MNT_RDONLY) res = catopt(res, "ro"); 82546619Sjkoshy if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 82646619Sjkoshy if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 82746619Sjkoshy if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 82846619Sjkoshy if (flags & MNT_UNION) res = catopt(res, "union"); 82946619Sjkoshy if (flags & MNT_ASYNC) res = catopt(res, "async"); 83046619Sjkoshy if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 83146619Sjkoshy if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 83246619Sjkoshy if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 83346619Sjkoshy if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 83446619Sjkoshy if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 835101205Srwatson if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 836105118Srwatson if (flags & MNT_ACLS) res = catopt(res, "acls"); 837163843Spjd if (flags & MNT_GJOURNAL) res = catopt(res, "gjournal"); 83846619Sjkoshy 839159473Sjmallett return (res); 84046619Sjkoshy} 841