mount.c revision 160303
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 160303 2006-07-12 16:05:51Z des $"; 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" }, 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 123158400Smaximstatic const char userquotaeq[] = "userquota="; 124158400Smaximstatic const char groupquotaeq[] = "groupquota="; 125158400Smaxim 126152344Srodrigcstatic int 127152344Srodrigcuse_mountprog(const char *vfstype) 128152344Srodrigc{ 129152344Srodrigc /* XXX: We need to get away from implementing external mount 130152344Srodrigc * programs for every filesystem, and move towards having 131152344Srodrigc * each filesystem properly implement the nmount() system call. 132152344Srodrigc */ 133152344Srodrigc unsigned int i; 134152344Srodrigc const char *fs[] = { 135152778Savatar "cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs", 136153360Srodrigc "nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs", 137152344Srodrigc "unionfs", 138152344Srodrigc NULL 139152344Srodrigc }; 140152344Srodrigc 141159473Sjmallett for (i = 0; fs[i] != NULL; ++i) { 142152344Srodrigc if (strcmp(vfstype, fs[i]) == 0) 143159473Sjmallett return (1); 144152344Srodrigc } 145152344Srodrigc 146159473Sjmallett return (0); 147152344Srodrigc} 148152344Srodrigc 149152344Srodrigcstatic int 150159473Sjmallettexec_mountprog(const char *name, const char *execname, char *const argv[]) 151152344Srodrigc{ 152152344Srodrigc pid_t pid; 153152344Srodrigc int status; 154152344Srodrigc 155152344Srodrigc switch (pid = fork()) { 156152344Srodrigc case -1: /* Error. */ 157152344Srodrigc warn("fork"); 158152344Srodrigc exit (1); 159152344Srodrigc case 0: /* Child. */ 160152344Srodrigc /* Go find an executable. */ 161152344Srodrigc execvP(execname, _PATH_SYSPATH, argv); 162152344Srodrigc if (errno == ENOENT) { 163152344Srodrigc warn("exec %s not found in %s", execname, 164152344Srodrigc _PATH_SYSPATH); 165152344Srodrigc } 166152344Srodrigc exit(1); 167152344Srodrigc default: /* Parent. */ 168152344Srodrigc if (waitpid(pid, &status, 0) < 0) { 169152344Srodrigc warn("waitpid"); 170152344Srodrigc return (1); 171152344Srodrigc } 172152344Srodrigc 173152344Srodrigc if (WIFEXITED(status)) { 174152344Srodrigc if (WEXITSTATUS(status) != 0) 175152344Srodrigc return (WEXITSTATUS(status)); 176152344Srodrigc } else if (WIFSIGNALED(status)) { 177152344Srodrigc warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 178152344Srodrigc return (1); 179152344Srodrigc } 180152344Srodrigc break; 181152344Srodrigc } 182152344Srodrigc 183152344Srodrigc return (0); 184152344Srodrigc} 185152344Srodrigc 1861558Srgrimesint 187151042Srodrigcmain(int argc, char *argv[]) 1881558Srgrimes{ 18923678Speter const char *mntfromname, **vfslist, *vfstype; 1901558Srgrimes struct fstab *fs; 1911558Srgrimes struct statfs *mntbuf; 1921558Srgrimes FILE *mountdfp; 1931558Srgrimes pid_t pid; 194160303Sdes int all, ch, i, init_flags, late, mntsize, rval, have_fstab; 19595289Smux char *cp, *ep, *options; 1961558Srgrimes 197159472Sjmallett options = strdup("noro"); 198159472Sjmallett if (options == NULL) 199159472Sjmallett errx(1, "malloc failed"); 200159472Sjmallett 201160303Sdes all = init_flags = late = 0; 2021558Srgrimes vfslist = NULL; 2031558Srgrimes vfstype = "ufs"; 204160303Sdes while ((ch = getopt(argc, argv, "adlF:fo:prwt:uv")) != -1) 2051558Srgrimes switch (ch) { 2061558Srgrimes case 'a': 2071558Srgrimes all = 1; 2081558Srgrimes break; 2091558Srgrimes case 'd': 2101558Srgrimes debug = 1; 2111558Srgrimes break; 212113220Smdodd case 'F': 213113220Smdodd setfstab(optarg); 214113220Smdodd break; 2151558Srgrimes case 'f': 2161558Srgrimes init_flags |= MNT_FORCE; 2171558Srgrimes break; 218160303Sdes case 'l': 219160303Sdes late = 1; 220160303Sdes break; 2211558Srgrimes case 'o': 222159472Sjmallett options = catopt(options, optarg); 2231558Srgrimes break; 22423805Sbde case 'p': 22523805Sbde fstab_style = 1; 22623805Sbde verbose = 1; 22723805Sbde break; 2281558Srgrimes case 'r': 22976198Sdd options = catopt(options, "ro"); 2301558Srgrimes break; 2311558Srgrimes case 't': 2321558Srgrimes if (vfslist != NULL) 23337425Scharnier errx(1, "only one -t option may be specified"); 2341558Srgrimes vfslist = makevfslist(optarg); 2351558Srgrimes vfstype = optarg; 2361558Srgrimes break; 2371558Srgrimes case 'u': 2381558Srgrimes init_flags |= MNT_UPDATE; 2391558Srgrimes break; 2401558Srgrimes case 'v': 2411558Srgrimes verbose = 1; 2421558Srgrimes break; 2431558Srgrimes case 'w': 24476198Sdd options = catopt(options, "noro"); 2451558Srgrimes break; 2461558Srgrimes case '?': 2471558Srgrimes default: 2481558Srgrimes usage(); 2491558Srgrimes /* NOTREACHED */ 2501558Srgrimes } 2511558Srgrimes argc -= optind; 2521558Srgrimes argv += optind; 2531558Srgrimes 2541558Srgrimes#define BADTYPE(type) \ 2551558Srgrimes (strcmp(type, FSTAB_RO) && \ 2561558Srgrimes strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 2571558Srgrimes 2581558Srgrimes rval = 0; 2591558Srgrimes switch (argc) { 2601558Srgrimes case 0: 26128671Ssteve if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 26228671Ssteve err(1, "getmntinfo"); 26328671Ssteve if (all) { 2641558Srgrimes while ((fs = getfsent()) != NULL) { 2651558Srgrimes if (BADTYPE(fs->fs_type)) 2661558Srgrimes continue; 26723678Speter if (checkvfsname(fs->fs_vfstype, vfslist)) 2681558Srgrimes continue; 26923678Speter if (hasopt(fs->fs_mntops, "noauto")) 27010288Sdg continue; 271160303Sdes if (hasopt(fs->fs_mntops, "late") && !late) 272160303Sdes continue; 27344811Sbde if (!(init_flags & MNT_UPDATE) && 27444811Sbde ismounted(fs, mntbuf, mntsize)) 27528671Ssteve continue; 276125194Sguido options = update_options(options, fs->fs_mntops, 277125194Sguido mntbuf->f_flags); 2781558Srgrimes if (mountfs(fs->fs_vfstype, fs->fs_spec, 27923805Sbde fs->fs_file, init_flags, options, 28023805Sbde fs->fs_mntops)) 28123805Sbde rval = 1; 2821558Srgrimes } 28328671Ssteve } else if (fstab_style) { 28417243Sjkh for (i = 0; i < mntsize; i++) { 28523678Speter if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 28617243Sjkh continue; 28723805Sbde putfsent(&mntbuf[i]); 28817243Sjkh } 28923805Sbde } else { 2901558Srgrimes for (i = 0; i < mntsize; i++) { 29123805Sbde if (checkvfsname(mntbuf[i].f_fstypename, 29223805Sbde vfslist)) 2931558Srgrimes continue; 29423678Speter prmount(&mntbuf[i]); 2951558Srgrimes } 2961558Srgrimes } 2971558Srgrimes exit(rval); 2981558Srgrimes case 1: 2991558Srgrimes if (vfslist != NULL) 3001558Srgrimes usage(); 3011558Srgrimes 302124201Sanholt rmslashes(*argv, *argv); 3031558Srgrimes if (init_flags & MNT_UPDATE) { 30452678Sgreen mntfromname = NULL; 30552678Sgreen have_fstab = 0; 3061558Srgrimes if ((mntbuf = getmntpt(*argv)) == NULL) 30752678Sgreen errx(1, "not currently mounted %s", *argv); 30852678Sgreen /* 30952678Sgreen * Only get the mntflags from fstab if both mntpoint 31052678Sgreen * and mntspec are identical. Also handle the special 31152678Sgreen * case where just '/' is mounted and 'spec' is not 31252678Sgreen * identical with the one from fstab ('/dev' is missing 31352678Sgreen * in the spec-string at boot-time). 31452678Sgreen */ 31546619Sjkoshy if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 31652678Sgreen if (strcmp(fs->fs_spec, 31752678Sgreen mntbuf->f_mntfromname) == 0 && 31852678Sgreen strcmp(fs->fs_file, 31952678Sgreen mntbuf->f_mntonname) == 0) { 32052678Sgreen have_fstab = 1; 32152678Sgreen mntfromname = mntbuf->f_mntfromname; 32252678Sgreen } else if (argv[0][0] == '/' && 32352678Sgreen argv[0][1] == '\0') { 32452678Sgreen fs = getfsfile("/"); 32552678Sgreen have_fstab = 1; 32652678Sgreen mntfromname = fs->fs_spec; 32752678Sgreen } 32852678Sgreen } 32952678Sgreen if (have_fstab) { 33046619Sjkoshy options = update_options(options, fs->fs_mntops, 33146619Sjkoshy mntbuf->f_flags); 33246619Sjkoshy } else { 33323678Speter mntfromname = mntbuf->f_mntfromname; 33446619Sjkoshy options = update_options(options, NULL, 33546619Sjkoshy mntbuf->f_flags); 33646619Sjkoshy } 33723678Speter rval = mountfs(mntbuf->f_fstypename, mntfromname, 33823678Speter mntbuf->f_mntonname, init_flags, options, 0); 33923678Speter break; 3401558Srgrimes } 34123678Speter if ((fs = getfsfile(*argv)) == NULL && 34223678Speter (fs = getfsspec(*argv)) == NULL) 343102231Strhodes errx(1, "%s: unknown special file or file system", 34423678Speter *argv); 34523678Speter if (BADTYPE(fs->fs_type)) 346102231Strhodes errx(1, "%s has unknown file system type", 34723678Speter *argv); 34823678Speter rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 34923678Speter init_flags, options, fs->fs_mntops); 3501558Srgrimes break; 3511558Srgrimes case 2: 3521558Srgrimes /* 35352678Sgreen * If -t flag has not been specified, the path cannot be 35495289Smux * found, spec contains either a ':' or a '@', then assume 355102231Strhodes * that an NFS file system is being specified ala Sun. 35695289Smux * Check if the hostname contains only allowed characters 35795289Smux * to reduce false positives. IPv6 addresses containing 35895289Smux * ':' will be correctly parsed only if the separator is '@'. 35995289Smux * The definition of a valid hostname is taken from RFC 1034. 3601558Srgrimes */ 361123162Siedowse if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL || 362123162Siedowse (ep = strchr(argv[0], ':')) != NULL)) { 36395318Smux if (*ep == '@') { 36495318Smux cp = ep + 1; 36595318Smux ep = cp + strlen(cp); 36695318Smux } else 36795318Smux cp = argv[0]; 36895289Smux while (cp != ep) { 36995289Smux if (!isdigit(*cp) && !isalpha(*cp) && 37095289Smux *cp != '.' && *cp != '-' && *cp != ':') 37195289Smux break; 37295289Smux cp++; 37395289Smux } 37495289Smux if (cp == ep) 37595289Smux vfstype = "nfs"; 37695289Smux } 3771558Srgrimes rval = mountfs(vfstype, 3781558Srgrimes argv[0], argv[1], init_flags, options, NULL); 3791558Srgrimes break; 3801558Srgrimes default: 3811558Srgrimes usage(); 3821558Srgrimes /* NOTREACHED */ 3831558Srgrimes } 3841558Srgrimes 3851558Srgrimes /* 3861558Srgrimes * If the mount was successfully, and done by root, tell mountd the 3871558Srgrimes * good news. Pid checks are probably unnecessary, but don't hurt. 3881558Srgrimes */ 3891558Srgrimes if (rval == 0 && getuid() == 0 && 3901558Srgrimes (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 39128671Ssteve if (fscanf(mountdfp, "%d", &pid) == 1 && 3921558Srgrimes pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH) 3931558Srgrimes err(1, "signal mountd"); 3941558Srgrimes (void)fclose(mountdfp); 3951558Srgrimes } 3961558Srgrimes 3971558Srgrimes exit(rval); 3981558Srgrimes} 3991558Srgrimes 4001558Srgrimesint 401151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize) 40228671Ssteve{ 403154512Spjd char realfsfile[PATH_MAX]; 40428671Ssteve int i; 40528671Ssteve 40628671Ssteve if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 407102231Strhodes /* the root file system can always be remounted */ 40828671Ssteve return (0); 40928671Ssteve 410154512Spjd /* The user may have specified a symlink in fstab, resolve the path */ 411154512Spjd if (realpath(fs->fs_file, realfsfile) == NULL) { 412154512Spjd /* Cannot resolve the path, use original one */ 413154512Spjd strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile)); 414154512Spjd } 415154512Spjd 41628671Ssteve for (i = mntsize - 1; i >= 0; --i) 417154512Spjd if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 && 41828671Ssteve (!isremountable(fs->fs_vfstype) || 41928671Ssteve strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)) 42028671Ssteve return (1); 42128671Ssteve return (0); 42228671Ssteve} 42328671Ssteve 42428671Ssteveint 425151042Srodrigcisremountable(const char *vfsname) 42628671Ssteve{ 42728671Ssteve const char **cp; 42828671Ssteve 42928671Ssteve for (cp = remountable_fs_names; *cp; cp++) 43028671Ssteve if (strcmp(*cp, vfsname) == 0) 43128671Ssteve return (1); 43228671Ssteve return (0); 43328671Ssteve} 43428671Ssteve 43528671Ssteveint 436151042Srodrigchasopt(const char *mntopts, const char *option) 43723678Speter{ 43823678Speter int negative, found; 43923678Speter char *opt, *optbuf; 44023678Speter 44123678Speter if (option[0] == 'n' && option[1] == 'o') { 44223678Speter negative = 1; 44323678Speter option += 2; 44423678Speter } else 44523678Speter negative = 0; 44623678Speter optbuf = strdup(mntopts); 44723678Speter found = 0; 44823678Speter for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 44923678Speter if (opt[0] == 'n' && opt[1] == 'o') { 45023678Speter if (!strcasecmp(opt + 2, option)) 45123678Speter found = negative; 45223678Speter } else if (!strcasecmp(opt, option)) 45323678Speter found = !negative; 45423678Speter } 45523678Speter free(optbuf); 45623678Speter return (found); 45723678Speter} 45823678Speter 45923678Speterint 460151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags, 461151042Srodrigc const char *options, const char *mntopts) 4621558Srgrimes{ 463152344Srodrigc char *argv[100]; 4641558Srgrimes struct statfs sf; 465152344Srodrigc int argc, i, ret; 466118580Simp char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; 4671558Srgrimes 46852055Sphk /* resolve the mountpoint with realpath(3) */ 46952055Sphk (void)checkpath(name, mntpath); 4701558Srgrimes name = mntpath; 4711558Srgrimes 47223678Speter if (mntopts == NULL) 47323678Speter mntopts = ""; 4741558Srgrimes optbuf = catopt(strdup(mntopts), options); 4751558Srgrimes 4761558Srgrimes if (strcmp(name, "/") == 0) 4771558Srgrimes flags |= MNT_UPDATE; 4781558Srgrimes if (flags & MNT_FORCE) 4791558Srgrimes optbuf = catopt(optbuf, "force"); 4801558Srgrimes if (flags & MNT_RDONLY) 4811558Srgrimes optbuf = catopt(optbuf, "ro"); 4821558Srgrimes /* 4831558Srgrimes * XXX 4841558Srgrimes * The mount_mfs (newfs) command uses -o to select the 48537425Scharnier * optimization mode. We don't pass the default "-o rw" 4861558Srgrimes * for that reason. 4871558Srgrimes */ 4881558Srgrimes if (flags & MNT_UPDATE) 4891558Srgrimes optbuf = catopt(optbuf, "update"); 4901558Srgrimes 49177577Sru /* Compatibility glue. */ 49277577Sru if (strcmp(vfstype, "msdos") == 0) 49377577Sru vfstype = "msdosfs"; 49477577Sru 495144133Scperciva /* Construct the name of the appropriate mount command */ 496144133Scperciva (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 497144133Scperciva 4981558Srgrimes argc = 0; 499144133Scperciva argv[argc++] = execname; 5001558Srgrimes mangle(optbuf, &argc, argv); 501152344Srodrigc argv[argc++] = strdup(spec); 502152344Srodrigc argv[argc++] = strdup(name); 5031558Srgrimes argv[argc] = NULL; 5041558Srgrimes 5051558Srgrimes if (debug) { 5061558Srgrimes (void)printf("exec: mount_%s", vfstype); 5071558Srgrimes for (i = 1; i < argc; i++) 5081558Srgrimes (void)printf(" %s", argv[i]); 5091558Srgrimes (void)printf("\n"); 5101558Srgrimes return (0); 5111558Srgrimes } 5121558Srgrimes 513152668Srodrigc if (use_mountprog(vfstype)) { 514152344Srodrigc ret = exec_mountprog(name, execname, argv); 515152344Srodrigc } else { 516152344Srodrigc ret = mount_fs(vfstype, argc, argv); 517152344Srodrigc } 5181558Srgrimes 519152344Srodrigc free(optbuf); 5201558Srgrimes 521152344Srodrigc if (verbose) { 522152344Srodrigc if (statfs(name, &sf) < 0) { 523152344Srodrigc warn("statfs %s", name); 5241558Srgrimes return (1); 5251558Srgrimes } 526152344Srodrigc if (fstab_style) 527152344Srodrigc putfsent(&sf); 528152344Srodrigc else 529152344Srodrigc prmount(&sf); 5301558Srgrimes } 5311558Srgrimes 5321558Srgrimes return (0); 5331558Srgrimes} 5341558Srgrimes 5351558Srgrimesvoid 536151042Srodrigcprmount(struct statfs *sfp) 53723678Speter{ 538151043Srodrigc int flags; 539151043Srodrigc unsigned int i; 5401558Srgrimes struct opt *o; 54123678Speter struct passwd *pw; 5421558Srgrimes 54352036Sn_hibma (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 54452036Sn_hibma sfp->f_fstypename); 5451558Srgrimes 54623678Speter flags = sfp->f_flags & MNT_VISFLAGMASK; 54752036Sn_hibma for (o = optnames; flags && o->o_opt; o++) 5481558Srgrimes if (flags & o->o_opt) { 54952036Sn_hibma (void)printf(", %s", o->o_name); 5501558Srgrimes flags &= ~o->o_opt; 5511558Srgrimes } 552125339Spjd /* 553125339Spjd * Inform when file system is mounted by an unprivileged user 554125339Spjd * or privileged non-root user. 555125339Spjd */ 556125365Snectar if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 55752036Sn_hibma (void)printf(", mounted by "); 55823678Speter if ((pw = getpwuid(sfp->f_owner)) != NULL) 55923678Speter (void)printf("%s", pw->pw_name); 56023678Speter else 56123678Speter (void)printf("%d", sfp->f_owner); 56223678Speter } 56365023Ssheldonh if (verbose) { 56465023Ssheldonh if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 565123242Siedowse (void)printf(", writes: sync %ju async %ju", 566123242Siedowse (uintmax_t)sfp->f_syncwrites, 567123242Siedowse (uintmax_t)sfp->f_asyncwrites); 56865023Ssheldonh if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 569123242Siedowse (void)printf(", reads: sync %ju async %ju", 570123242Siedowse (uintmax_t)sfp->f_syncreads, 571123242Siedowse (uintmax_t)sfp->f_asyncreads); 572123162Siedowse if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 573123162Siedowse printf(", fsid "); 574123162Siedowse for (i = 0; i < sizeof(sfp->f_fsid); i++) 575123162Siedowse printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 576123162Siedowse } 57765023Ssheldonh } 57852036Sn_hibma (void)printf(")\n"); 5791558Srgrimes} 5801558Srgrimes 5811558Srgrimesstruct statfs * 582151042Srodrigcgetmntpt(const char *name) 5831558Srgrimes{ 5841558Srgrimes struct statfs *mntbuf; 5851558Srgrimes int i, mntsize; 5861558Srgrimes 5871558Srgrimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 58852678Sgreen for (i = mntsize - 1; i >= 0; i--) { 5891558Srgrimes if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 5901558Srgrimes strcmp(mntbuf[i].f_mntonname, name) == 0) 5911558Srgrimes return (&mntbuf[i]); 59252678Sgreen } 5931558Srgrimes return (NULL); 5941558Srgrimes} 5951558Srgrimes 5961558Srgrimeschar * 597151042Srodrigccatopt(char *s0, const char *s1) 5981558Srgrimes{ 5991558Srgrimes size_t i; 6001558Srgrimes char *cp; 6011558Srgrimes 60246619Sjkoshy if (s1 == NULL || *s1 == '\0') 603159473Sjmallett return (s0); 60446619Sjkoshy 6051558Srgrimes if (s0 && *s0) { 6061558Srgrimes i = strlen(s0) + strlen(s1) + 1 + 1; 6071558Srgrimes if ((cp = malloc(i)) == NULL) 60837425Scharnier errx(1, "malloc failed"); 6091558Srgrimes (void)snprintf(cp, i, "%s,%s", s0, s1); 6101558Srgrimes } else 6111558Srgrimes cp = strdup(s1); 6121558Srgrimes 6131558Srgrimes if (s0) 6141558Srgrimes free(s0); 6151558Srgrimes return (cp); 6161558Srgrimes} 6171558Srgrimes 6181558Srgrimesvoid 619159473Sjmallettmangle(char *options, int *argcp, 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; 636160303Sdes } else if (strcmp(p, "late") == 0) { 637160303Sdes /* 638160303Sdes * "late" is used to prevent certain file 639160303Sdes * systems from being mounted before late 640160303Sdes * in the boot cycle; for instance, 641160303Sdes * loopback NFS mounts can't be mounted 642160303Sdes * before mountd starts. 643160303Sdes */ 644160303Sdes continue; 645152732Srodrigc } else if (strcmp(p, "userquota") == 0) { 646152732Srodrigc continue; 647158400Smaxim } else if (strncmp(p, userquotaeq, 648158400Smaxim sizeof(userquotaeq) - 1) == 0) { 649158400Smaxim continue; 650152732Srodrigc } else if (strcmp(p, "groupquota") == 0) { 651152732Srodrigc continue; 652158400Smaxim } else if (strncmp(p, groupquotaeq, 653158400Smaxim sizeof(groupquotaeq) - 1) == 0) { 654158400Smaxim continue; 655152465Srodrigc } else if (*p == '-') { 6561558Srgrimes argv[argc++] = p; 6571558Srgrimes p = strchr(p, '='); 658123268Strhodes if (p != NULL) { 6591558Srgrimes *p = '\0'; 6601558Srgrimes argv[argc++] = p+1; 6611558Srgrimes } 662153038Srodrigc } else { 663152344Srodrigc argv[argc++] = strdup("-o"); 6641558Srgrimes argv[argc++] = p; 6651558Srgrimes } 66646619Sjkoshy } 6671558Srgrimes 6681558Srgrimes *argcp = argc; 6691558Srgrimes} 6701558Srgrimes 67146619Sjkoshy 67246619Sjkoshychar * 673159473Sjmallettupdate_options(char *opts, char *fstab, int curflags) 67446619Sjkoshy{ 67546619Sjkoshy char *o, *p; 67646619Sjkoshy char *cur; 67746619Sjkoshy char *expopt, *newopt, *tmpopt; 67846619Sjkoshy 67946619Sjkoshy if (opts == NULL) 680159473Sjmallett return (strdup("")); 68146619Sjkoshy 68246619Sjkoshy /* remove meta options from list */ 68346619Sjkoshy remopt(fstab, MOUNT_META_OPTION_FSTAB); 68446619Sjkoshy remopt(fstab, MOUNT_META_OPTION_CURRENT); 68546619Sjkoshy cur = flags2opts(curflags); 68646619Sjkoshy 68746619Sjkoshy /* 68846619Sjkoshy * Expand all meta-options passed to us first. 68946619Sjkoshy */ 69046619Sjkoshy expopt = NULL; 69146619Sjkoshy for (p = opts; (o = strsep(&p, ",")) != NULL;) { 69246619Sjkoshy if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 69346619Sjkoshy expopt = catopt(expopt, fstab); 69446619Sjkoshy else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 69546619Sjkoshy expopt = catopt(expopt, cur); 69646619Sjkoshy else 69746619Sjkoshy expopt = catopt(expopt, o); 69846619Sjkoshy } 69946619Sjkoshy free(cur); 70046619Sjkoshy free(opts); 70146619Sjkoshy 70246619Sjkoshy /* 70346619Sjkoshy * Remove previous contradictory arguments. Given option "foo" we 70446619Sjkoshy * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 70546619Sjkoshy * and "foo" - so we can deal with possible options like "notice". 70646619Sjkoshy */ 70746619Sjkoshy newopt = NULL; 70846619Sjkoshy for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 70946619Sjkoshy if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 71046619Sjkoshy errx(1, "malloc failed"); 711125933Sgrog 71246619Sjkoshy strcpy(tmpopt, "no"); 71346619Sjkoshy strcat(tmpopt, o); 71446619Sjkoshy remopt(newopt, tmpopt); 71546619Sjkoshy free(tmpopt); 71646619Sjkoshy 71746619Sjkoshy if (strncmp("no", o, 2) == 0) 71846619Sjkoshy remopt(newopt, o+2); 71946619Sjkoshy 72046619Sjkoshy newopt = catopt(newopt, o); 72146619Sjkoshy } 72246619Sjkoshy free(expopt); 72346619Sjkoshy 724159473Sjmallett return (newopt); 72546619Sjkoshy} 72646619Sjkoshy 7271558Srgrimesvoid 728159473Sjmallettremopt(char *string, const char *opt) 72946619Sjkoshy{ 73046619Sjkoshy char *o, *p, *r; 73146619Sjkoshy 73246619Sjkoshy if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 73346619Sjkoshy return; 73446619Sjkoshy 73546619Sjkoshy r = string; 73646619Sjkoshy 73746619Sjkoshy for (p = string; (o = strsep(&p, ",")) != NULL;) { 73846619Sjkoshy if (strcmp(opt, o) != 0) { 73946619Sjkoshy if (*r == ',' && *o != '\0') 74046619Sjkoshy r++; 74146619Sjkoshy while ((*r++ = *o++) != '\0') 74246619Sjkoshy ; 74346619Sjkoshy *--r = ','; 74446619Sjkoshy } 74546619Sjkoshy } 74646619Sjkoshy *r = '\0'; 74746619Sjkoshy} 74846619Sjkoshy 74946619Sjkoshyvoid 750159473Sjmallettusage(void) 7511558Srgrimes{ 7521558Srgrimes 75337425Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 754160303Sdes"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 755141611Sru" mount [-dfpruvw] special | node", 756141611Sru" mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 7571558Srgrimes exit(1); 7581558Srgrimes} 75917243Sjkh 76017243Sjkhvoid 761159473Sjmallettputfsent(const struct statfs *ent) 76217243Sjkh{ 76323805Sbde struct fstab *fst; 76446619Sjkoshy char *opts; 765125933Sgrog 76646619Sjkoshy opts = flags2opts(ent->f_flags); 767156229Skeramida 768156229Skeramida /* 769156229Skeramida * "rw" is not a real mount option; this is why we print NULL as "rw" 770156229Skeramida * if opts is still NULL here. 771156229Skeramida */ 77223805Sbde printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname, 773156229Skeramida ent->f_fstypename, opts ? opts : "rw"); 77446619Sjkoshy free(opts); 77517243Sjkh 77628671Ssteve if ((fst = getfsspec(ent->f_mntfromname))) 77723805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 77828671Ssteve else if ((fst = getfsfile(ent->f_mntonname))) 77923805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 78056038Sgreen else if (strcmp(ent->f_fstypename, "ufs") == 0) { 78156038Sgreen if (strcmp(ent->f_mntonname, "/") == 0) 78256038Sgreen printf("\t1 1\n"); 78356038Sgreen else 78456038Sgreen printf("\t2 2\n"); 78556038Sgreen } else 78623805Sbde printf("\t0 0\n"); 78717243Sjkh} 78846619Sjkoshy 78946619Sjkoshy 79046619Sjkoshychar * 791159473Sjmallettflags2opts(int flags) 79246619Sjkoshy{ 79346619Sjkoshy char *res; 79446619Sjkoshy 79546619Sjkoshy res = NULL; 79646619Sjkoshy 797155997Srodrigc if (flags & MNT_RDONLY) res = catopt(res, "ro"); 79846619Sjkoshy if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 79946619Sjkoshy if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 80046619Sjkoshy if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 80146619Sjkoshy if (flags & MNT_UNION) res = catopt(res, "union"); 80246619Sjkoshy if (flags & MNT_ASYNC) res = catopt(res, "async"); 80346619Sjkoshy if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 80446619Sjkoshy if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 80546619Sjkoshy if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 80646619Sjkoshy if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 80746619Sjkoshy if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 808101205Srwatson if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 809105118Srwatson if (flags & MNT_ACLS) res = catopt(res, "acls"); 81046619Sjkoshy 811159473Sjmallett return (res); 81246619Sjkoshy} 813