mount.c revision 153038
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 153038 2005-12-03 01:57:58Z rodrigc $"; 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 *); 77152344Srodrigcvoid 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" }, 10946619Sjkoshy { 0, NULL } 1101558Srgrimes}; 1111558Srgrimes 11228671Ssteve/* 11328671Ssteve * List of VFS types that can be remounted without becoming mounted on top 11428671Ssteve * of each other. 11528671Ssteve * XXX Is this list correct? 11628671Ssteve */ 11728671Sstevestatic const char * 11828671Ssteveremountable_fs_names[] = { 11969056Sphantom "ufs", "ffs", "ext2fs", 12028671Ssteve 0 12128671Ssteve}; 12228671Ssteve 123152344Srodrigcstatic int 124152344Srodrigcuse_mountprog(const char *vfstype) 125152344Srodrigc{ 126152344Srodrigc /* XXX: We need to get away from implementing external mount 127152344Srodrigc * programs for every filesystem, and move towards having 128152344Srodrigc * each filesystem properly implement the nmount() system call. 129152344Srodrigc */ 130152344Srodrigc unsigned int i; 131152344Srodrigc const char *fs[] = { 132152778Savatar "cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs", 133152344Srodrigc "nwfs", "nullfs", "portalfs", "reiserfs", "smbfs", "udf", "umapfs", 134152344Srodrigc "unionfs", 135152344Srodrigc NULL 136152344Srodrigc }; 137152344Srodrigc 138152344Srodrigc for (i=0; fs[i] != NULL; ++i) { 139152344Srodrigc if (strcmp(vfstype, fs[i]) == 0) 140152344Srodrigc return 1; 141152344Srodrigc } 142152344Srodrigc 143152344Srodrigc return 0; 144152344Srodrigc} 145152344Srodrigc 146152344Srodrigcstatic int 147152344Srodrigcexec_mountprog(const char *name, const char *execname, 148152344Srodrigc char *const argv[]) 149152344Srodrigc{ 150152344Srodrigc pid_t pid; 151152344Srodrigc int status; 152152344Srodrigc 153152344Srodrigc switch (pid = fork()) { 154152344Srodrigc case -1: /* Error. */ 155152344Srodrigc warn("fork"); 156152344Srodrigc exit (1); 157152344Srodrigc case 0: /* Child. */ 158152344Srodrigc /* Go find an executable. */ 159152344Srodrigc execvP(execname, _PATH_SYSPATH, argv); 160152344Srodrigc if (errno == ENOENT) { 161152344Srodrigc warn("exec %s not found in %s", execname, 162152344Srodrigc _PATH_SYSPATH); 163152344Srodrigc } 164152344Srodrigc exit(1); 165152344Srodrigc default: /* Parent. */ 166152344Srodrigc if (waitpid(pid, &status, 0) < 0) { 167152344Srodrigc warn("waitpid"); 168152344Srodrigc return (1); 169152344Srodrigc } 170152344Srodrigc 171152344Srodrigc if (WIFEXITED(status)) { 172152344Srodrigc if (WEXITSTATUS(status) != 0) 173152344Srodrigc return (WEXITSTATUS(status)); 174152344Srodrigc } else if (WIFSIGNALED(status)) { 175152344Srodrigc warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 176152344Srodrigc return (1); 177152344Srodrigc } 178152344Srodrigc break; 179152344Srodrigc } 180152344Srodrigc 181152344Srodrigc return (0); 182152344Srodrigc} 183152344Srodrigc 1841558Srgrimesint 185151042Srodrigcmain(int argc, char *argv[]) 1861558Srgrimes{ 18723678Speter const char *mntfromname, **vfslist, *vfstype; 1881558Srgrimes struct fstab *fs; 1891558Srgrimes struct statfs *mntbuf; 1901558Srgrimes FILE *mountdfp; 1911558Srgrimes pid_t pid; 19252678Sgreen int all, ch, i, init_flags, mntsize, rval, have_fstab; 19395289Smux char *cp, *ep, *options; 1941558Srgrimes 1951558Srgrimes all = init_flags = 0; 1961558Srgrimes options = NULL; 1971558Srgrimes vfslist = NULL; 1981558Srgrimes vfstype = "ufs"; 199113220Smdodd while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1) 2001558Srgrimes switch (ch) { 2011558Srgrimes case 'a': 2021558Srgrimes all = 1; 2031558Srgrimes break; 2041558Srgrimes case 'd': 2051558Srgrimes debug = 1; 2061558Srgrimes break; 207113220Smdodd case 'F': 208113220Smdodd setfstab(optarg); 209113220Smdodd break; 2101558Srgrimes case 'f': 2111558Srgrimes init_flags |= MNT_FORCE; 2121558Srgrimes break; 2131558Srgrimes case 'o': 2141558Srgrimes if (*optarg) 2151558Srgrimes options = catopt(options, optarg); 2161558Srgrimes break; 21723805Sbde case 'p': 21823805Sbde fstab_style = 1; 21923805Sbde verbose = 1; 22023805Sbde break; 2211558Srgrimes case 'r': 22276198Sdd options = catopt(options, "ro"); 2231558Srgrimes break; 2241558Srgrimes case 't': 2251558Srgrimes if (vfslist != NULL) 22637425Scharnier errx(1, "only one -t option may be specified"); 2271558Srgrimes vfslist = makevfslist(optarg); 2281558Srgrimes vfstype = optarg; 2291558Srgrimes break; 2301558Srgrimes case 'u': 2311558Srgrimes init_flags |= MNT_UPDATE; 2321558Srgrimes break; 2331558Srgrimes case 'v': 2341558Srgrimes verbose = 1; 2351558Srgrimes break; 2361558Srgrimes case 'w': 23776198Sdd options = catopt(options, "noro"); 2381558Srgrimes break; 2391558Srgrimes case '?': 2401558Srgrimes default: 2411558Srgrimes usage(); 2421558Srgrimes /* NOTREACHED */ 2431558Srgrimes } 2441558Srgrimes argc -= optind; 2451558Srgrimes argv += optind; 2461558Srgrimes 2471558Srgrimes#define BADTYPE(type) \ 2481558Srgrimes (strcmp(type, FSTAB_RO) && \ 2491558Srgrimes strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 2501558Srgrimes 2511558Srgrimes rval = 0; 2521558Srgrimes switch (argc) { 2531558Srgrimes case 0: 25428671Ssteve if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 25528671Ssteve err(1, "getmntinfo"); 25628671Ssteve if (all) { 2571558Srgrimes while ((fs = getfsent()) != NULL) { 2581558Srgrimes if (BADTYPE(fs->fs_type)) 2591558Srgrimes continue; 26023678Speter if (checkvfsname(fs->fs_vfstype, vfslist)) 2611558Srgrimes continue; 26223678Speter if (hasopt(fs->fs_mntops, "noauto")) 26310288Sdg continue; 26444811Sbde if (!(init_flags & MNT_UPDATE) && 26544811Sbde ismounted(fs, mntbuf, mntsize)) 26628671Ssteve continue; 267125194Sguido options = update_options(options, fs->fs_mntops, 268125194Sguido mntbuf->f_flags); 2691558Srgrimes if (mountfs(fs->fs_vfstype, fs->fs_spec, 27023805Sbde fs->fs_file, init_flags, options, 27123805Sbde fs->fs_mntops)) 27223805Sbde rval = 1; 2731558Srgrimes } 27428671Ssteve } else if (fstab_style) { 27517243Sjkh for (i = 0; i < mntsize; i++) { 27623678Speter if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 27717243Sjkh continue; 27823805Sbde putfsent(&mntbuf[i]); 27917243Sjkh } 28023805Sbde } else { 2811558Srgrimes for (i = 0; i < mntsize; i++) { 28223805Sbde if (checkvfsname(mntbuf[i].f_fstypename, 28323805Sbde vfslist)) 2841558Srgrimes continue; 28523678Speter prmount(&mntbuf[i]); 2861558Srgrimes } 2871558Srgrimes } 2881558Srgrimes exit(rval); 2891558Srgrimes case 1: 2901558Srgrimes if (vfslist != NULL) 2911558Srgrimes usage(); 2921558Srgrimes 293124201Sanholt rmslashes(*argv, *argv); 2941558Srgrimes if (init_flags & MNT_UPDATE) { 29552678Sgreen mntfromname = NULL; 29652678Sgreen have_fstab = 0; 2971558Srgrimes if ((mntbuf = getmntpt(*argv)) == NULL) 29852678Sgreen errx(1, "not currently mounted %s", *argv); 29952678Sgreen /* 30052678Sgreen * Only get the mntflags from fstab if both mntpoint 30152678Sgreen * and mntspec are identical. Also handle the special 30252678Sgreen * case where just '/' is mounted and 'spec' is not 30352678Sgreen * identical with the one from fstab ('/dev' is missing 30452678Sgreen * in the spec-string at boot-time). 30552678Sgreen */ 30646619Sjkoshy if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 30752678Sgreen if (strcmp(fs->fs_spec, 30852678Sgreen mntbuf->f_mntfromname) == 0 && 30952678Sgreen strcmp(fs->fs_file, 31052678Sgreen mntbuf->f_mntonname) == 0) { 31152678Sgreen have_fstab = 1; 31252678Sgreen mntfromname = mntbuf->f_mntfromname; 31352678Sgreen } else if (argv[0][0] == '/' && 31452678Sgreen argv[0][1] == '\0') { 31552678Sgreen fs = getfsfile("/"); 31652678Sgreen have_fstab = 1; 31752678Sgreen mntfromname = fs->fs_spec; 31852678Sgreen } 31952678Sgreen } 32052678Sgreen if (have_fstab) { 32146619Sjkoshy options = update_options(options, fs->fs_mntops, 32246619Sjkoshy mntbuf->f_flags); 32346619Sjkoshy } else { 32423678Speter mntfromname = mntbuf->f_mntfromname; 32546619Sjkoshy options = update_options(options, NULL, 32646619Sjkoshy mntbuf->f_flags); 32746619Sjkoshy } 32823678Speter rval = mountfs(mntbuf->f_fstypename, mntfromname, 32923678Speter mntbuf->f_mntonname, init_flags, options, 0); 33023678Speter break; 3311558Srgrimes } 33223678Speter if ((fs = getfsfile(*argv)) == NULL && 33323678Speter (fs = getfsspec(*argv)) == NULL) 334102231Strhodes errx(1, "%s: unknown special file or file system", 33523678Speter *argv); 33623678Speter if (BADTYPE(fs->fs_type)) 337102231Strhodes errx(1, "%s has unknown file system type", 33823678Speter *argv); 33923678Speter rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 34023678Speter init_flags, options, fs->fs_mntops); 3411558Srgrimes break; 3421558Srgrimes case 2: 3431558Srgrimes /* 34452678Sgreen * If -t flag has not been specified, the path cannot be 34595289Smux * found, spec contains either a ':' or a '@', then assume 346102231Strhodes * that an NFS file system is being specified ala Sun. 34795289Smux * Check if the hostname contains only allowed characters 34895289Smux * to reduce false positives. IPv6 addresses containing 34995289Smux * ':' will be correctly parsed only if the separator is '@'. 35095289Smux * The definition of a valid hostname is taken from RFC 1034. 3511558Srgrimes */ 352123162Siedowse if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL || 353123162Siedowse (ep = strchr(argv[0], ':')) != NULL)) { 35495318Smux if (*ep == '@') { 35595318Smux cp = ep + 1; 35695318Smux ep = cp + strlen(cp); 35795318Smux } else 35895318Smux cp = argv[0]; 35995289Smux while (cp != ep) { 36095289Smux if (!isdigit(*cp) && !isalpha(*cp) && 36195289Smux *cp != '.' && *cp != '-' && *cp != ':') 36295289Smux break; 36395289Smux cp++; 36495289Smux } 36595289Smux if (cp == ep) 36695289Smux vfstype = "nfs"; 36795289Smux } 3681558Srgrimes rval = mountfs(vfstype, 3691558Srgrimes argv[0], argv[1], init_flags, options, NULL); 3701558Srgrimes break; 3711558Srgrimes default: 3721558Srgrimes usage(); 3731558Srgrimes /* NOTREACHED */ 3741558Srgrimes } 3751558Srgrimes 3761558Srgrimes /* 3771558Srgrimes * If the mount was successfully, and done by root, tell mountd the 3781558Srgrimes * good news. Pid checks are probably unnecessary, but don't hurt. 3791558Srgrimes */ 3801558Srgrimes if (rval == 0 && getuid() == 0 && 3811558Srgrimes (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 38228671Ssteve if (fscanf(mountdfp, "%d", &pid) == 1 && 3831558Srgrimes pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH) 3841558Srgrimes err(1, "signal mountd"); 3851558Srgrimes (void)fclose(mountdfp); 3861558Srgrimes } 3871558Srgrimes 3881558Srgrimes exit(rval); 3891558Srgrimes} 3901558Srgrimes 3911558Srgrimesint 392151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize) 39328671Ssteve{ 39428671Ssteve int i; 39528671Ssteve 39628671Ssteve if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 397102231Strhodes /* the root file system can always be remounted */ 39828671Ssteve return (0); 39928671Ssteve 40028671Ssteve for (i = mntsize - 1; i >= 0; --i) 40128671Ssteve if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 && 40228671Ssteve (!isremountable(fs->fs_vfstype) || 40328671Ssteve strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)) 40428671Ssteve return (1); 40528671Ssteve return (0); 40628671Ssteve} 40728671Ssteve 40828671Ssteveint 409151042Srodrigcisremountable(const char *vfsname) 41028671Ssteve{ 41128671Ssteve const char **cp; 41228671Ssteve 41328671Ssteve for (cp = remountable_fs_names; *cp; cp++) 41428671Ssteve if (strcmp(*cp, vfsname) == 0) 41528671Ssteve return (1); 41628671Ssteve return (0); 41728671Ssteve} 41828671Ssteve 41928671Ssteveint 420151042Srodrigchasopt(const char *mntopts, const char *option) 42123678Speter{ 42223678Speter int negative, found; 42323678Speter char *opt, *optbuf; 42423678Speter 42523678Speter if (option[0] == 'n' && option[1] == 'o') { 42623678Speter negative = 1; 42723678Speter option += 2; 42823678Speter } else 42923678Speter negative = 0; 43023678Speter optbuf = strdup(mntopts); 43123678Speter found = 0; 43223678Speter for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 43323678Speter if (opt[0] == 'n' && opt[1] == 'o') { 43423678Speter if (!strcasecmp(opt + 2, option)) 43523678Speter found = negative; 43623678Speter } else if (!strcasecmp(opt, option)) 43723678Speter found = !negative; 43823678Speter } 43923678Speter free(optbuf); 44023678Speter return (found); 44123678Speter} 44223678Speter 44323678Speterint 444151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags, 445151042Srodrigc const char *options, const char *mntopts) 4461558Srgrimes{ 447152344Srodrigc char *argv[100]; 4481558Srgrimes struct statfs sf; 449152344Srodrigc int argc, i, ret; 450118580Simp char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; 4511558Srgrimes 45228671Ssteve#if __GNUC__ 45328671Ssteve (void)&optbuf; 45428671Ssteve (void)&name; 45528671Ssteve#endif 45628671Ssteve 45752055Sphk /* resolve the mountpoint with realpath(3) */ 45852055Sphk (void)checkpath(name, mntpath); 4591558Srgrimes name = mntpath; 4601558Srgrimes 46123678Speter if (mntopts == NULL) 46223678Speter mntopts = ""; 4631558Srgrimes if (options == NULL) { 46423678Speter if (*mntopts == '\0') { 4651558Srgrimes options = "rw"; 46623678Speter } else { 4671558Srgrimes options = mntopts; 46823678Speter mntopts = ""; 46923678Speter } 4701558Srgrimes } 4711558Srgrimes optbuf = catopt(strdup(mntopts), options); 4721558Srgrimes 4731558Srgrimes if (strcmp(name, "/") == 0) 4741558Srgrimes flags |= MNT_UPDATE; 4751558Srgrimes if (flags & MNT_FORCE) 4761558Srgrimes optbuf = catopt(optbuf, "force"); 4771558Srgrimes if (flags & MNT_RDONLY) 4781558Srgrimes optbuf = catopt(optbuf, "ro"); 4791558Srgrimes /* 4801558Srgrimes * XXX 4811558Srgrimes * The mount_mfs (newfs) command uses -o to select the 48237425Scharnier * optimization mode. We don't pass the default "-o rw" 4831558Srgrimes * for that reason. 4841558Srgrimes */ 4851558Srgrimes if (flags & MNT_UPDATE) 4861558Srgrimes optbuf = catopt(optbuf, "update"); 4871558Srgrimes 48877577Sru /* Compatibility glue. */ 48977577Sru if (strcmp(vfstype, "msdos") == 0) 49077577Sru vfstype = "msdosfs"; 49177577Sru 492144133Scperciva /* Construct the name of the appropriate mount command */ 493144133Scperciva (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 494144133Scperciva 4951558Srgrimes argc = 0; 496144133Scperciva argv[argc++] = execname; 4971558Srgrimes mangle(optbuf, &argc, argv); 498152344Srodrigc argv[argc++] = strdup(spec); 499152344Srodrigc argv[argc++] = strdup(name); 5001558Srgrimes argv[argc] = NULL; 5011558Srgrimes 5021558Srgrimes if (debug) { 5031558Srgrimes (void)printf("exec: mount_%s", vfstype); 5041558Srgrimes for (i = 1; i < argc; i++) 5051558Srgrimes (void)printf(" %s", argv[i]); 5061558Srgrimes (void)printf("\n"); 5071558Srgrimes return (0); 5081558Srgrimes } 5091558Srgrimes 510152668Srodrigc if (use_mountprog(vfstype)) { 511152344Srodrigc ret = exec_mountprog(name, execname, argv); 512152344Srodrigc } else { 513152344Srodrigc ret = mount_fs(vfstype, argc, argv); 514152344Srodrigc } 5151558Srgrimes 516152344Srodrigc free(optbuf); 5171558Srgrimes 518152344Srodrigc if (verbose) { 519152344Srodrigc if (statfs(name, &sf) < 0) { 520152344Srodrigc warn("statfs %s", name); 5211558Srgrimes return (1); 5221558Srgrimes } 523152344Srodrigc if (fstab_style) 524152344Srodrigc putfsent(&sf); 525152344Srodrigc else 526152344Srodrigc prmount(&sf); 5271558Srgrimes } 5281558Srgrimes 5291558Srgrimes return (0); 5301558Srgrimes} 5311558Srgrimes 5321558Srgrimesvoid 533151042Srodrigcprmount(struct statfs *sfp) 53423678Speter{ 535151043Srodrigc int flags; 536151043Srodrigc unsigned int i; 5371558Srgrimes struct opt *o; 53823678Speter struct passwd *pw; 5391558Srgrimes 54052036Sn_hibma (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 54152036Sn_hibma sfp->f_fstypename); 5421558Srgrimes 54323678Speter flags = sfp->f_flags & MNT_VISFLAGMASK; 54452036Sn_hibma for (o = optnames; flags && o->o_opt; o++) 5451558Srgrimes if (flags & o->o_opt) { 54652036Sn_hibma (void)printf(", %s", o->o_name); 5471558Srgrimes flags &= ~o->o_opt; 5481558Srgrimes } 549125339Spjd /* 550125339Spjd * Inform when file system is mounted by an unprivileged user 551125339Spjd * or privileged non-root user. 552125339Spjd */ 553125365Snectar if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 55452036Sn_hibma (void)printf(", mounted by "); 55523678Speter if ((pw = getpwuid(sfp->f_owner)) != NULL) 55623678Speter (void)printf("%s", pw->pw_name); 55723678Speter else 55823678Speter (void)printf("%d", sfp->f_owner); 55923678Speter } 56065023Ssheldonh if (verbose) { 56165023Ssheldonh if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 562123242Siedowse (void)printf(", writes: sync %ju async %ju", 563123242Siedowse (uintmax_t)sfp->f_syncwrites, 564123242Siedowse (uintmax_t)sfp->f_asyncwrites); 56565023Ssheldonh if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 566123242Siedowse (void)printf(", reads: sync %ju async %ju", 567123242Siedowse (uintmax_t)sfp->f_syncreads, 568123242Siedowse (uintmax_t)sfp->f_asyncreads); 569123162Siedowse if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 570123162Siedowse printf(", fsid "); 571123162Siedowse for (i = 0; i < sizeof(sfp->f_fsid); i++) 572123162Siedowse printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 573123162Siedowse } 57465023Ssheldonh } 57552036Sn_hibma (void)printf(")\n"); 5761558Srgrimes} 5771558Srgrimes 5781558Srgrimesstruct statfs * 579151042Srodrigcgetmntpt(const char *name) 5801558Srgrimes{ 5811558Srgrimes struct statfs *mntbuf; 5821558Srgrimes int i, mntsize; 5831558Srgrimes 5841558Srgrimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 58552678Sgreen for (i = mntsize - 1; i >= 0; i--) { 5861558Srgrimes if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 5871558Srgrimes strcmp(mntbuf[i].f_mntonname, name) == 0) 5881558Srgrimes return (&mntbuf[i]); 58952678Sgreen } 5901558Srgrimes return (NULL); 5911558Srgrimes} 5921558Srgrimes 5931558Srgrimeschar * 594151042Srodrigccatopt(char *s0, const char *s1) 5951558Srgrimes{ 5961558Srgrimes size_t i; 5971558Srgrimes char *cp; 5981558Srgrimes 59946619Sjkoshy if (s1 == NULL || *s1 == '\0') 60046619Sjkoshy return s0; 60146619Sjkoshy 6021558Srgrimes if (s0 && *s0) { 6031558Srgrimes i = strlen(s0) + strlen(s1) + 1 + 1; 6041558Srgrimes if ((cp = malloc(i)) == NULL) 60537425Scharnier errx(1, "malloc failed"); 6061558Srgrimes (void)snprintf(cp, i, "%s,%s", s0, s1); 6071558Srgrimes } else 6081558Srgrimes cp = strdup(s1); 6091558Srgrimes 6101558Srgrimes if (s0) 6111558Srgrimes free(s0); 6121558Srgrimes return (cp); 6131558Srgrimes} 6141558Srgrimes 6151558Srgrimesvoid 6161558Srgrimesmangle(options, argcp, argv) 6171558Srgrimes char *options; 6181558Srgrimes int *argcp; 619152344Srodrigc char **argv; 6201558Srgrimes{ 6211558Srgrimes char *p, *s; 6221558Srgrimes int argc; 6231558Srgrimes 6241558Srgrimes argc = *argcp; 6251558Srgrimes for (s = options; (p = strsep(&s, ",")) != NULL;) 62646619Sjkoshy if (*p != '\0') { 627152465Srodrigc if (strcmp(p, "noauto") == 0) { 628152465Srodrigc /* 629152465Srodrigc * Do not pass noauto option to nmount(). 630152465Srodrigc * or external mount program. noauto is 631152465Srodrigc * only used to prevent mounting a filesystem 632152465Srodrigc * when 'mount -a' is specified, and is 633152465Srodrigc * not a real mount option. 634152465Srodrigc */ 635152465Srodrigc continue; 636152732Srodrigc } else if (strcmp(p, "userquota") == 0) { 637152732Srodrigc continue; 638152732Srodrigc } else if (strcmp(p, "groupquota") == 0) { 639152732Srodrigc continue; 640152465Srodrigc } else if (*p == '-') { 6411558Srgrimes argv[argc++] = p; 6421558Srgrimes p = strchr(p, '='); 643123268Strhodes if (p != NULL) { 6441558Srgrimes *p = '\0'; 6451558Srgrimes argv[argc++] = p+1; 6461558Srgrimes } 647153038Srodrigc } else { 648152344Srodrigc argv[argc++] = strdup("-o"); 6491558Srgrimes argv[argc++] = p; 6501558Srgrimes } 65146619Sjkoshy } 6521558Srgrimes 6531558Srgrimes *argcp = argc; 6541558Srgrimes} 6551558Srgrimes 65646619Sjkoshy 65746619Sjkoshychar * 65846619Sjkoshyupdate_options(opts, fstab, curflags) 65946619Sjkoshy char *opts; 66046619Sjkoshy char *fstab; 66146619Sjkoshy int curflags; 66246619Sjkoshy{ 66346619Sjkoshy char *o, *p; 66446619Sjkoshy char *cur; 66546619Sjkoshy char *expopt, *newopt, *tmpopt; 66646619Sjkoshy 66746619Sjkoshy if (opts == NULL) 66846619Sjkoshy return strdup(""); 66946619Sjkoshy 67046619Sjkoshy /* remove meta options from list */ 67146619Sjkoshy remopt(fstab, MOUNT_META_OPTION_FSTAB); 67246619Sjkoshy remopt(fstab, MOUNT_META_OPTION_CURRENT); 67346619Sjkoshy cur = flags2opts(curflags); 67446619Sjkoshy 67546619Sjkoshy /* 67646619Sjkoshy * Expand all meta-options passed to us first. 67746619Sjkoshy */ 67846619Sjkoshy expopt = NULL; 67946619Sjkoshy for (p = opts; (o = strsep(&p, ",")) != NULL;) { 68046619Sjkoshy if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 68146619Sjkoshy expopt = catopt(expopt, fstab); 68246619Sjkoshy else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 68346619Sjkoshy expopt = catopt(expopt, cur); 68446619Sjkoshy else 68546619Sjkoshy expopt = catopt(expopt, o); 68646619Sjkoshy } 68746619Sjkoshy free(cur); 68846619Sjkoshy free(opts); 68946619Sjkoshy 69046619Sjkoshy /* 69146619Sjkoshy * Remove previous contradictory arguments. Given option "foo" we 69246619Sjkoshy * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 69346619Sjkoshy * and "foo" - so we can deal with possible options like "notice". 69446619Sjkoshy */ 69546619Sjkoshy newopt = NULL; 69646619Sjkoshy for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 69746619Sjkoshy if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 69846619Sjkoshy errx(1, "malloc failed"); 699125933Sgrog 70046619Sjkoshy strcpy(tmpopt, "no"); 70146619Sjkoshy strcat(tmpopt, o); 70246619Sjkoshy remopt(newopt, tmpopt); 70346619Sjkoshy free(tmpopt); 70446619Sjkoshy 70546619Sjkoshy if (strncmp("no", o, 2) == 0) 70646619Sjkoshy remopt(newopt, o+2); 70746619Sjkoshy 70846619Sjkoshy newopt = catopt(newopt, o); 70946619Sjkoshy } 71046619Sjkoshy free(expopt); 71146619Sjkoshy 71246619Sjkoshy return newopt; 71346619Sjkoshy} 71446619Sjkoshy 7151558Srgrimesvoid 71646619Sjkoshyremopt(string, opt) 71746619Sjkoshy char *string; 71846619Sjkoshy const char *opt; 71946619Sjkoshy{ 72046619Sjkoshy char *o, *p, *r; 72146619Sjkoshy 72246619Sjkoshy if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 72346619Sjkoshy return; 72446619Sjkoshy 72546619Sjkoshy r = string; 72646619Sjkoshy 72746619Sjkoshy for (p = string; (o = strsep(&p, ",")) != NULL;) { 72846619Sjkoshy if (strcmp(opt, o) != 0) { 72946619Sjkoshy if (*r == ',' && *o != '\0') 73046619Sjkoshy r++; 73146619Sjkoshy while ((*r++ = *o++) != '\0') 73246619Sjkoshy ; 73346619Sjkoshy *--r = ','; 73446619Sjkoshy } 73546619Sjkoshy } 73646619Sjkoshy *r = '\0'; 73746619Sjkoshy} 73846619Sjkoshy 73946619Sjkoshyvoid 7401558Srgrimesusage() 7411558Srgrimes{ 7421558Srgrimes 74337425Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 744141611Sru"usage: mount [-adfpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 745141611Sru" mount [-dfpruvw] special | node", 746141611Sru" mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 7471558Srgrimes exit(1); 7481558Srgrimes} 74917243Sjkh 75017243Sjkhvoid 75123805Sbdeputfsent(ent) 75223805Sbde const struct statfs *ent; 75317243Sjkh{ 75423805Sbde struct fstab *fst; 75546619Sjkoshy char *opts; 756125933Sgrog 75746619Sjkoshy opts = flags2opts(ent->f_flags); 75823805Sbde printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname, 75946619Sjkoshy ent->f_fstypename, opts); 76046619Sjkoshy free(opts); 76117243Sjkh 76228671Ssteve if ((fst = getfsspec(ent->f_mntfromname))) 76323805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 76428671Ssteve else if ((fst = getfsfile(ent->f_mntonname))) 76523805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 76656038Sgreen else if (strcmp(ent->f_fstypename, "ufs") == 0) { 76756038Sgreen if (strcmp(ent->f_mntonname, "/") == 0) 76856038Sgreen printf("\t1 1\n"); 76956038Sgreen else 77056038Sgreen printf("\t2 2\n"); 77156038Sgreen } else 77223805Sbde printf("\t0 0\n"); 77317243Sjkh} 77446619Sjkoshy 77546619Sjkoshy 77646619Sjkoshychar * 77746619Sjkoshyflags2opts(flags) 77846619Sjkoshy int flags; 77946619Sjkoshy{ 78046619Sjkoshy char *res; 78146619Sjkoshy 78246619Sjkoshy res = NULL; 78346619Sjkoshy 78446619Sjkoshy res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw"); 78546619Sjkoshy 78646619Sjkoshy if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 78746619Sjkoshy if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 78846619Sjkoshy if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 78946619Sjkoshy if (flags & MNT_UNION) res = catopt(res, "union"); 79046619Sjkoshy if (flags & MNT_ASYNC) res = catopt(res, "async"); 79146619Sjkoshy if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 79246619Sjkoshy if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 79346619Sjkoshy if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 79446619Sjkoshy if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 79546619Sjkoshy if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 796101205Srwatson if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 797105118Srwatson if (flags & MNT_ACLS) res = catopt(res, "acls"); 79846619Sjkoshy 79946619Sjkoshy return res; 80046619Sjkoshy} 801