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"; 3428671Ssteve#if 0 3523678Speterstatic char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95"; 3637425Scharnier#endif 371558Srgrimes#endif /* not lint */ 381558Srgrimes 39187093Sobrien#include <sys/cdefs.h> 40187093Sobrien__FBSDID("$FreeBSD: stable/10/sbin/mount/mount.c 319261 2017-05-30 22:36:24Z asomers $"); 41187093Sobrien 421558Srgrimes#include <sys/param.h> 431558Srgrimes#include <sys/mount.h> 4423805Sbde#include <sys/stat.h> 451558Srgrimes#include <sys/wait.h> 461558Srgrimes 4795289Smux#include <ctype.h> 481558Srgrimes#include <err.h> 491558Srgrimes#include <errno.h> 501558Srgrimes#include <fstab.h> 51117031Sgordon#include <paths.h> 5223678Speter#include <pwd.h> 531558Srgrimes#include <signal.h> 54123242Siedowse#include <stdint.h> 551558Srgrimes#include <stdio.h> 561558Srgrimes#include <stdlib.h> 571558Srgrimes#include <string.h> 581558Srgrimes#include <unistd.h> 59166439Spjd#include <libutil.h> 601558Srgrimes 6128671Ssteve#include "extern.h" 6252055Sphk#include "mntopts.h" 631558Srgrimes#include "pathnames.h" 641558Srgrimes 6546619Sjkoshy/* `meta' options */ 66125933Sgrog#define MOUNT_META_OPTION_FSTAB "fstab" 6746619Sjkoshy#define MOUNT_META_OPTION_CURRENT "current" 6846619Sjkoshy 69227081Sedstatic int debug, fstab_style, verbose; 701558Srgrimes 71187035Sobrienstruct cpa { 72187093Sobrien char **a; 73187130Sobrien ssize_t sz; 74187035Sobrien int c; 75187035Sobrien}; 76187035Sobrien 7792882Simpchar *catopt(char *, const char *); 7892882Simpstruct statfs *getmntpt(const char *); 7992882Simpint hasopt(const char *, const char *); 8092882Simpint ismounted(struct fstab *, struct statfs *, int); 8192882Simpint isremountable(const char *); 82187035Sobrienvoid mangle(char *, struct cpa *); 8392882Simpchar *update_options(char *, char *, int); 8492882Simpint mountfs(const char *, const char *, const char *, 8592882Simp int, const char *, const char *); 8692882Simpvoid remopt(char *, const char *); 8792882Simpvoid prmount(struct statfs *); 88175949Smatteovoid putfsent(struct statfs *); 8992882Simpvoid usage(void); 9092882Simpchar *flags2opts(int); 911558Srgrimes 9237425Scharnier/* Map from mount options to printable formats. */ 931558Srgrimesstatic struct opt { 94204840Sbz uint64_t o_opt; 951558Srgrimes const char *o_name; 961558Srgrimes} optnames[] = { 971558Srgrimes { MNT_ASYNC, "asynchronous" }, 981558Srgrimes { MNT_EXPORTED, "NFS exported" }, 991558Srgrimes { MNT_LOCAL, "local" }, 10018007Sdg { MNT_NOATIME, "noatime" }, 1011558Srgrimes { MNT_NOEXEC, "noexec" }, 1021558Srgrimes { MNT_NOSUID, "nosuid" }, 10335105Swosch { MNT_NOSYMFOLLOW, "nosymfollow" }, 1041558Srgrimes { MNT_QUOTA, "with quotas" }, 1051558Srgrimes { MNT_RDONLY, "read-only" }, 1061558Srgrimes { MNT_SYNCHRONOUS, "synchronous" }, 1071558Srgrimes { MNT_UNION, "union" }, 10829890Skato { MNT_NOCLUSTERR, "noclusterr" }, 10929890Skato { MNT_NOCLUSTERW, "noclusterw" }, 11031144Sjulian { MNT_SUIDDIR, "suiddir" }, 11134266Sjulian { MNT_SOFTDEP, "soft-updates" }, 112224294Smckusick { MNT_SUJ, "journaled soft-updates" }, 113101205Srwatson { MNT_MULTILABEL, "multilabel" }, 114105118Srwatson { MNT_ACLS, "acls" }, 115200796Strasz { MNT_NFS4ACLS, "nfsv4acls" }, 116163843Spjd { MNT_GJOURNAL, "gjournal" }, 117270892Strasz { MNT_AUTOMOUNTED, "automounted" }, 11846619Sjkoshy { 0, NULL } 1191558Srgrimes}; 1201558Srgrimes 12128671Ssteve/* 12228671Ssteve * List of VFS types that can be remounted without becoming mounted on top 12328671Ssteve * of each other. 12428671Ssteve * XXX Is this list correct? 12528671Ssteve */ 12628671Sstevestatic const char * 12728671Ssteveremountable_fs_names[] = { 12869056Sphantom "ufs", "ffs", "ext2fs", 12928671Ssteve 0 13028671Ssteve}; 13128671Ssteve 132158400Smaximstatic const char userquotaeq[] = "userquota="; 133158400Smaximstatic const char groupquotaeq[] = "groupquota="; 134158400Smaxim 135189397Srodrigcstatic char *mountprog = NULL; 136189397Srodrigc 137152344Srodrigcstatic int 138152344Srodrigcuse_mountprog(const char *vfstype) 139152344Srodrigc{ 140152344Srodrigc /* XXX: We need to get away from implementing external mount 141152344Srodrigc * programs for every filesystem, and move towards having 142152344Srodrigc * each filesystem properly implement the nmount() system call. 143152344Srodrigc */ 144152344Srodrigc unsigned int i; 145152344Srodrigc const char *fs[] = { 146241636Sattilio "cd9660", "mfs", "msdosfs", "nfs", 147252356Sdavide "nullfs", "oldnfs", "smbfs", "udf", "unionfs", 148152344Srodrigc NULL 149152344Srodrigc }; 150152344Srodrigc 151189397Srodrigc if (mountprog != NULL) 152189397Srodrigc return (1); 153189397Srodrigc 154159473Sjmallett for (i = 0; fs[i] != NULL; ++i) { 155152344Srodrigc if (strcmp(vfstype, fs[i]) == 0) 156159473Sjmallett return (1); 157152344Srodrigc } 158186505Sobrien 159159473Sjmallett return (0); 160152344Srodrigc} 161152344Srodrigc 162152344Srodrigcstatic int 163159473Sjmallettexec_mountprog(const char *name, const char *execname, char *const argv[]) 164152344Srodrigc{ 165152344Srodrigc pid_t pid; 166152344Srodrigc int status; 167152344Srodrigc 168152344Srodrigc switch (pid = fork()) { 169152344Srodrigc case -1: /* Error. */ 170152344Srodrigc warn("fork"); 171152344Srodrigc exit (1); 172152344Srodrigc case 0: /* Child. */ 173152344Srodrigc /* Go find an executable. */ 174152344Srodrigc execvP(execname, _PATH_SYSPATH, argv); 175152344Srodrigc if (errno == ENOENT) { 176189397Srodrigc warn("exec %s not found", execname); 177189397Srodrigc if (execname[0] != '/') { 178189397Srodrigc warnx("in path: %s", _PATH_SYSPATH); 179189397Srodrigc } 180152344Srodrigc } 181152344Srodrigc exit(1); 182152344Srodrigc default: /* Parent. */ 183152344Srodrigc if (waitpid(pid, &status, 0) < 0) { 184152344Srodrigc warn("waitpid"); 185152344Srodrigc return (1); 186152344Srodrigc } 187152344Srodrigc 188152344Srodrigc if (WIFEXITED(status)) { 189152344Srodrigc if (WEXITSTATUS(status) != 0) 190152344Srodrigc return (WEXITSTATUS(status)); 191152344Srodrigc } else if (WIFSIGNALED(status)) { 192152344Srodrigc warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 193152344Srodrigc return (1); 194152344Srodrigc } 195152344Srodrigc break; 196152344Srodrigc } 197152344Srodrigc 198152344Srodrigc return (0); 199152344Srodrigc} 200152344Srodrigc 201163672Srustatic int 202163672Sruspecified_ro(const char *arg) 203163671Sru{ 204163671Sru char *optbuf, *opt; 205163671Sru int ret = 0; 206163671Sru 207163671Sru optbuf = strdup(arg); 208163671Sru if (optbuf == NULL) 209163671Sru err(1, NULL); 210163671Sru 211163671Sru for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 212163671Sru if (strcmp(opt, "ro") == 0) { 213163671Sru ret = 1; 214163671Sru break; 215163671Sru } 216163671Sru } 217163671Sru free(optbuf); 218163671Sru return (ret); 219163671Sru} 220163671Sru 221166439Spjdstatic void 222166439Spjdrestart_mountd(void) 223166439Spjd{ 224166439Spjd struct pidfh *pfh; 225186505Sobrien pid_t mountdpid; 226166439Spjd 227166439Spjd pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid); 228166439Spjd if (pfh != NULL) { 229166439Spjd /* Mountd is not running. */ 230166439Spjd pidfile_remove(pfh); 231166439Spjd return; 232166439Spjd } 233166439Spjd if (errno != EEXIST) { 234166439Spjd /* Cannot open pidfile for some reason. */ 235166439Spjd return; 236166439Spjd } 237166439Spjd /* We have mountd(8) PID in mountdpid varible, let's signal it. */ 238166439Spjd if (kill(mountdpid, SIGHUP) == -1) 239166439Spjd err(1, "signal mountd"); 240166439Spjd} 241166439Spjd 2421558Srgrimesint 243151042Srodrigcmain(int argc, char *argv[]) 2441558Srgrimes{ 24523678Speter const char *mntfromname, **vfslist, *vfstype; 2461558Srgrimes struct fstab *fs; 2471558Srgrimes struct statfs *mntbuf; 248222832Sdelphij int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro; 249250235Screes int onlylate; 25095289Smux char *cp, *ep, *options; 2511558Srgrimes 252250235Screes all = init_flags = late = onlylate = 0; 253163671Sru ro = 0; 254163671Sru options = NULL; 2551558Srgrimes vfslist = NULL; 2561558Srgrimes vfstype = "ufs"; 257253372Srmh while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1) 2581558Srgrimes switch (ch) { 2591558Srgrimes case 'a': 2601558Srgrimes all = 1; 2611558Srgrimes break; 2621558Srgrimes case 'd': 2631558Srgrimes debug = 1; 2641558Srgrimes break; 265113220Smdodd case 'F': 266113220Smdodd setfstab(optarg); 267113220Smdodd break; 2681558Srgrimes case 'f': 2691558Srgrimes init_flags |= MNT_FORCE; 2701558Srgrimes break; 271250235Screes case 'L': 272250235Screes onlylate = 1; 273250235Screes late = 1; 274250235Screes break; 275160303Sdes case 'l': 276160303Sdes late = 1; 277160303Sdes break; 278253372Srmh case 'n': 279253372Srmh /* For compatibility with the Linux version of mount. */ 280253372Srmh break; 2811558Srgrimes case 'o': 282163671Sru if (*optarg) { 283163671Sru options = catopt(options, optarg); 284163671Sru if (specified_ro(optarg)) 285163671Sru ro = 1; 286163671Sru } 2871558Srgrimes break; 28823805Sbde case 'p': 28923805Sbde fstab_style = 1; 29023805Sbde verbose = 1; 29123805Sbde break; 2921558Srgrimes case 'r': 29376198Sdd options = catopt(options, "ro"); 294163671Sru ro = 1; 2951558Srgrimes break; 2961558Srgrimes case 't': 2971558Srgrimes if (vfslist != NULL) 29837425Scharnier errx(1, "only one -t option may be specified"); 2991558Srgrimes vfslist = makevfslist(optarg); 3001558Srgrimes vfstype = optarg; 3011558Srgrimes break; 3021558Srgrimes case 'u': 3031558Srgrimes init_flags |= MNT_UPDATE; 3041558Srgrimes break; 3051558Srgrimes case 'v': 3061558Srgrimes verbose = 1; 3071558Srgrimes break; 3081558Srgrimes case 'w': 30976198Sdd options = catopt(options, "noro"); 3101558Srgrimes break; 3111558Srgrimes case '?': 3121558Srgrimes default: 3131558Srgrimes usage(); 3141558Srgrimes /* NOTREACHED */ 3151558Srgrimes } 3161558Srgrimes argc -= optind; 3171558Srgrimes argv += optind; 3181558Srgrimes 3191558Srgrimes#define BADTYPE(type) \ 3201558Srgrimes (strcmp(type, FSTAB_RO) && \ 3211558Srgrimes strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 3221558Srgrimes 323163671Sru if ((init_flags & MNT_UPDATE) && (ro == 0)) 324163671Sru options = catopt(options, "noro"); 325186505Sobrien 3261558Srgrimes rval = 0; 3271558Srgrimes switch (argc) { 3281558Srgrimes case 0: 329224503Smckusick if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 33028671Ssteve err(1, "getmntinfo"); 33128671Ssteve if (all) { 3321558Srgrimes while ((fs = getfsent()) != NULL) { 3331558Srgrimes if (BADTYPE(fs->fs_type)) 3341558Srgrimes continue; 33523678Speter if (checkvfsname(fs->fs_vfstype, vfslist)) 3361558Srgrimes continue; 33723678Speter if (hasopt(fs->fs_mntops, "noauto")) 33810288Sdg continue; 339250235Screes if (!hasopt(fs->fs_mntops, "late") && onlylate) 340250235Screes continue; 341160303Sdes if (hasopt(fs->fs_mntops, "late") && !late) 342160303Sdes continue; 343222832Sdelphij if (hasopt(fs->fs_mntops, "failok")) 344222832Sdelphij failok = 1; 345222832Sdelphij else 346222832Sdelphij failok = 0; 34744811Sbde if (!(init_flags & MNT_UPDATE) && 34844811Sbde ismounted(fs, mntbuf, mntsize)) 34928671Ssteve continue; 350125194Sguido options = update_options(options, fs->fs_mntops, 351125194Sguido mntbuf->f_flags); 3521558Srgrimes if (mountfs(fs->fs_vfstype, fs->fs_spec, 35323805Sbde fs->fs_file, init_flags, options, 354222832Sdelphij fs->fs_mntops) && !failok) 35523805Sbde rval = 1; 3561558Srgrimes } 35728671Ssteve } else if (fstab_style) { 35817243Sjkh for (i = 0; i < mntsize; i++) { 35923678Speter if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 36017243Sjkh continue; 36123805Sbde putfsent(&mntbuf[i]); 36217243Sjkh } 36323805Sbde } else { 3641558Srgrimes for (i = 0; i < mntsize; i++) { 36523805Sbde if (checkvfsname(mntbuf[i].f_fstypename, 36623805Sbde vfslist)) 3671558Srgrimes continue; 368197200Spjd if (!verbose && 369197200Spjd (mntbuf[i].f_flags & MNT_IGNORE) != 0) 370197200Spjd continue; 37123678Speter prmount(&mntbuf[i]); 3721558Srgrimes } 3731558Srgrimes } 3741558Srgrimes exit(rval); 3751558Srgrimes case 1: 3761558Srgrimes if (vfslist != NULL) 3771558Srgrimes usage(); 3781558Srgrimes 379124201Sanholt rmslashes(*argv, *argv); 3801558Srgrimes if (init_flags & MNT_UPDATE) { 38152678Sgreen mntfromname = NULL; 38252678Sgreen have_fstab = 0; 3831558Srgrimes if ((mntbuf = getmntpt(*argv)) == NULL) 38452678Sgreen errx(1, "not currently mounted %s", *argv); 38552678Sgreen /* 38652678Sgreen * Only get the mntflags from fstab if both mntpoint 38752678Sgreen * and mntspec are identical. Also handle the special 38852678Sgreen * case where just '/' is mounted and 'spec' is not 38952678Sgreen * identical with the one from fstab ('/dev' is missing 39052678Sgreen * in the spec-string at boot-time). 39152678Sgreen */ 39246619Sjkoshy if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 39352678Sgreen if (strcmp(fs->fs_spec, 39452678Sgreen mntbuf->f_mntfromname) == 0 && 39552678Sgreen strcmp(fs->fs_file, 39652678Sgreen mntbuf->f_mntonname) == 0) { 39752678Sgreen have_fstab = 1; 39852678Sgreen mntfromname = mntbuf->f_mntfromname; 39952678Sgreen } else if (argv[0][0] == '/' && 40052678Sgreen argv[0][1] == '\0') { 40152678Sgreen fs = getfsfile("/"); 40252678Sgreen have_fstab = 1; 40352678Sgreen mntfromname = fs->fs_spec; 40452678Sgreen } 40552678Sgreen } 40652678Sgreen if (have_fstab) { 40746619Sjkoshy options = update_options(options, fs->fs_mntops, 40846619Sjkoshy mntbuf->f_flags); 40946619Sjkoshy } else { 41023678Speter mntfromname = mntbuf->f_mntfromname; 41146619Sjkoshy options = update_options(options, NULL, 41246619Sjkoshy mntbuf->f_flags); 41346619Sjkoshy } 41423678Speter rval = mountfs(mntbuf->f_fstypename, mntfromname, 41523678Speter mntbuf->f_mntonname, init_flags, options, 0); 41623678Speter break; 4171558Srgrimes } 41823678Speter if ((fs = getfsfile(*argv)) == NULL && 41923678Speter (fs = getfsspec(*argv)) == NULL) 420102231Strhodes errx(1, "%s: unknown special file or file system", 42123678Speter *argv); 42223678Speter if (BADTYPE(fs->fs_type)) 423102231Strhodes errx(1, "%s has unknown file system type", 42423678Speter *argv); 42523678Speter rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 42623678Speter init_flags, options, fs->fs_mntops); 4271558Srgrimes break; 4281558Srgrimes case 2: 4291558Srgrimes /* 43052678Sgreen * If -t flag has not been specified, the path cannot be 43195289Smux * found, spec contains either a ':' or a '@', then assume 432102231Strhodes * that an NFS file system is being specified ala Sun. 43395289Smux * Check if the hostname contains only allowed characters 43495289Smux * to reduce false positives. IPv6 addresses containing 43595289Smux * ':' will be correctly parsed only if the separator is '@'. 43695289Smux * The definition of a valid hostname is taken from RFC 1034. 4371558Srgrimes */ 438123162Siedowse if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL || 439123162Siedowse (ep = strchr(argv[0], ':')) != NULL)) { 44095318Smux if (*ep == '@') { 44195318Smux cp = ep + 1; 44295318Smux ep = cp + strlen(cp); 44395318Smux } else 44495318Smux cp = argv[0]; 44595289Smux while (cp != ep) { 44695289Smux if (!isdigit(*cp) && !isalpha(*cp) && 44795289Smux *cp != '.' && *cp != '-' && *cp != ':') 44895289Smux break; 44995289Smux cp++; 45095289Smux } 45195289Smux if (cp == ep) 45295289Smux vfstype = "nfs"; 45395289Smux } 4541558Srgrimes rval = mountfs(vfstype, 4551558Srgrimes argv[0], argv[1], init_flags, options, NULL); 4561558Srgrimes break; 4571558Srgrimes default: 4581558Srgrimes usage(); 4591558Srgrimes /* NOTREACHED */ 4601558Srgrimes } 4611558Srgrimes 4621558Srgrimes /* 4631558Srgrimes * If the mount was successfully, and done by root, tell mountd the 464166439Spjd * good news. 4651558Srgrimes */ 466166439Spjd if (rval == 0 && getuid() == 0) 467166439Spjd restart_mountd(); 4681558Srgrimes 4691558Srgrimes exit(rval); 4701558Srgrimes} 4711558Srgrimes 4721558Srgrimesint 473151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize) 47428671Ssteve{ 475154512Spjd char realfsfile[PATH_MAX]; 47628671Ssteve int i; 47728671Ssteve 47828671Ssteve if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 479102231Strhodes /* the root file system can always be remounted */ 48028671Ssteve return (0); 48128671Ssteve 482154512Spjd /* The user may have specified a symlink in fstab, resolve the path */ 483154512Spjd if (realpath(fs->fs_file, realfsfile) == NULL) { 484154512Spjd /* Cannot resolve the path, use original one */ 485154512Spjd strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile)); 486154512Spjd } 487154512Spjd 488294278Sasomers /* 489294278Sasomers * Consider the filesystem to be mounted if: 490294278Sasomers * It has the same mountpoint as a mounted filesytem, and 491294278Sasomers * It has the same type as that same mounted filesystem, and 492294278Sasomers * It has the same device name as that same mounted filesystem, OR 493294278Sasomers * It is a nonremountable filesystem 494294278Sasomers */ 49528671Ssteve for (i = mntsize - 1; i >= 0; --i) 496154512Spjd if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 && 497294278Sasomers strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 && 49828671Ssteve (!isremountable(fs->fs_vfstype) || 499294278Sasomers (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))) 50028671Ssteve return (1); 50128671Ssteve return (0); 50228671Ssteve} 50328671Ssteve 50428671Ssteveint 505151042Srodrigcisremountable(const char *vfsname) 50628671Ssteve{ 50728671Ssteve const char **cp; 50828671Ssteve 50928671Ssteve for (cp = remountable_fs_names; *cp; cp++) 51028671Ssteve if (strcmp(*cp, vfsname) == 0) 51128671Ssteve return (1); 51228671Ssteve return (0); 51328671Ssteve} 51428671Ssteve 51528671Ssteveint 516151042Srodrigchasopt(const char *mntopts, const char *option) 51723678Speter{ 51823678Speter int negative, found; 51923678Speter char *opt, *optbuf; 52023678Speter 52123678Speter if (option[0] == 'n' && option[1] == 'o') { 52223678Speter negative = 1; 52323678Speter option += 2; 52423678Speter } else 52523678Speter negative = 0; 52623678Speter optbuf = strdup(mntopts); 52723678Speter found = 0; 52823678Speter for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 52923678Speter if (opt[0] == 'n' && opt[1] == 'o') { 53023678Speter if (!strcasecmp(opt + 2, option)) 53123678Speter found = negative; 53223678Speter } else if (!strcasecmp(opt, option)) 53323678Speter found = !negative; 53423678Speter } 53523678Speter free(optbuf); 53623678Speter return (found); 53723678Speter} 53823678Speter 539187035Sobrienstatic void 540187035Sobrienappend_arg(struct cpa *sa, char *arg) 541187035Sobrien{ 542187130Sobrien if (sa->c + 1 == sa->sz) { 543187130Sobrien sa->sz = sa->sz == 0 ? 8 : sa->sz * 2; 544187130Sobrien sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz); 545187093Sobrien if (sa->a == NULL) 546187093Sobrien errx(1, "realloc failed"); 547187093Sobrien } 548187035Sobrien sa->a[++sa->c] = arg; 549187035Sobrien} 550187035Sobrien 55123678Speterint 552151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags, 553151042Srodrigc const char *options, const char *mntopts) 5541558Srgrimes{ 5551558Srgrimes struct statfs sf; 556186504Sobrien int i, ret; 557118580Simp char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; 558187130Sobrien static struct cpa mnt_argv; 5591558Srgrimes 56052055Sphk /* resolve the mountpoint with realpath(3) */ 561230226Sjh if (checkpath(name, mntpath) != 0) { 562230226Sjh warn("%s", mntpath); 563230226Sjh return (1); 564230226Sjh } 5651558Srgrimes name = mntpath; 5661558Srgrimes 56723678Speter if (mntopts == NULL) 56823678Speter mntopts = ""; 5691558Srgrimes optbuf = catopt(strdup(mntopts), options); 5701558Srgrimes 5711558Srgrimes if (strcmp(name, "/") == 0) 5721558Srgrimes flags |= MNT_UPDATE; 5731558Srgrimes if (flags & MNT_FORCE) 5741558Srgrimes optbuf = catopt(optbuf, "force"); 5751558Srgrimes if (flags & MNT_RDONLY) 5761558Srgrimes optbuf = catopt(optbuf, "ro"); 5771558Srgrimes /* 5781558Srgrimes * XXX 5791558Srgrimes * The mount_mfs (newfs) command uses -o to select the 58037425Scharnier * optimization mode. We don't pass the default "-o rw" 5811558Srgrimes * for that reason. 5821558Srgrimes */ 5831558Srgrimes if (flags & MNT_UPDATE) 5841558Srgrimes optbuf = catopt(optbuf, "update"); 5851558Srgrimes 58677577Sru /* Compatibility glue. */ 587166526Srodrigc if (strcmp(vfstype, "msdos") == 0) { 588166526Srodrigc warnx( 589166526Srodrigc "Using \"-t msdosfs\", since \"-t msdos\" is deprecated."); 59077577Sru vfstype = "msdosfs"; 591166526Srodrigc } 59277577Sru 593144133Scperciva /* Construct the name of the appropriate mount command */ 594144133Scperciva (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 595144133Scperciva 596187035Sobrien mnt_argv.c = -1; 597187035Sobrien append_arg(&mnt_argv, execname); 598187035Sobrien mangle(optbuf, &mnt_argv); 599189397Srodrigc if (mountprog != NULL) 600319261Sasomers strlcpy(execname, mountprog, sizeof(execname)); 601189397Srodrigc 602187035Sobrien append_arg(&mnt_argv, strdup(spec)); 603187035Sobrien append_arg(&mnt_argv, strdup(name)); 604187035Sobrien append_arg(&mnt_argv, NULL); 6051558Srgrimes 6061558Srgrimes if (debug) { 607164266Srodrigc if (use_mountprog(vfstype)) 608189397Srodrigc printf("exec: %s", execname); 609164266Srodrigc else 610164266Srodrigc printf("mount -t %s", vfstype); 611187035Sobrien for (i = 1; i < mnt_argv.c; i++) 612187035Sobrien (void)printf(" %s", mnt_argv.a[i]); 6131558Srgrimes (void)printf("\n"); 614225341Sjhb free(optbuf); 615225341Sjhb free(mountprog); 616225341Sjhb mountprog = NULL; 6171558Srgrimes return (0); 6181558Srgrimes } 6191558Srgrimes 620152668Srodrigc if (use_mountprog(vfstype)) { 621187035Sobrien ret = exec_mountprog(name, execname, mnt_argv.a); 622152344Srodrigc } else { 623187035Sobrien ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a); 624152344Srodrigc } 6251558Srgrimes 626152344Srodrigc free(optbuf); 627225341Sjhb free(mountprog); 628225341Sjhb mountprog = NULL; 6291558Srgrimes 630152344Srodrigc if (verbose) { 631152344Srodrigc if (statfs(name, &sf) < 0) { 632152344Srodrigc warn("statfs %s", name); 6331558Srgrimes return (1); 6341558Srgrimes } 635152344Srodrigc if (fstab_style) 636152344Srodrigc putfsent(&sf); 637152344Srodrigc else 638152344Srodrigc prmount(&sf); 6391558Srgrimes } 6401558Srgrimes 641182570Smatteo return (ret); 6421558Srgrimes} 6431558Srgrimes 6441558Srgrimesvoid 645151042Srodrigcprmount(struct statfs *sfp) 64623678Speter{ 647204840Sbz uint64_t flags; 648151043Srodrigc unsigned int i; 6491558Srgrimes struct opt *o; 65023678Speter struct passwd *pw; 6511558Srgrimes 65252036Sn_hibma (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 65352036Sn_hibma sfp->f_fstypename); 6541558Srgrimes 65523678Speter flags = sfp->f_flags & MNT_VISFLAGMASK; 656204840Sbz for (o = optnames; flags != 0 && o->o_opt != 0; o++) 6571558Srgrimes if (flags & o->o_opt) { 65852036Sn_hibma (void)printf(", %s", o->o_name); 6591558Srgrimes flags &= ~o->o_opt; 6601558Srgrimes } 661125339Spjd /* 662125339Spjd * Inform when file system is mounted by an unprivileged user 663125339Spjd * or privileged non-root user. 664125339Spjd */ 665125365Snectar if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 66652036Sn_hibma (void)printf(", mounted by "); 66723678Speter if ((pw = getpwuid(sfp->f_owner)) != NULL) 66823678Speter (void)printf("%s", pw->pw_name); 66923678Speter else 67023678Speter (void)printf("%d", sfp->f_owner); 67123678Speter } 67265023Ssheldonh if (verbose) { 67365023Ssheldonh if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 674123242Siedowse (void)printf(", writes: sync %ju async %ju", 675123242Siedowse (uintmax_t)sfp->f_syncwrites, 676123242Siedowse (uintmax_t)sfp->f_asyncwrites); 67765023Ssheldonh if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 678123242Siedowse (void)printf(", reads: sync %ju async %ju", 679123242Siedowse (uintmax_t)sfp->f_syncreads, 680123242Siedowse (uintmax_t)sfp->f_asyncreads); 681123162Siedowse if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 682123162Siedowse printf(", fsid "); 683123162Siedowse for (i = 0; i < sizeof(sfp->f_fsid); i++) 684123162Siedowse printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 685123162Siedowse } 68665023Ssheldonh } 68752036Sn_hibma (void)printf(")\n"); 6881558Srgrimes} 6891558Srgrimes 6901558Srgrimesstruct statfs * 691151042Srodrigcgetmntpt(const char *name) 6921558Srgrimes{ 6931558Srgrimes struct statfs *mntbuf; 6941558Srgrimes int i, mntsize; 6951558Srgrimes 696224503Smckusick mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 69752678Sgreen for (i = mntsize - 1; i >= 0; i--) { 6981558Srgrimes if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 6991558Srgrimes strcmp(mntbuf[i].f_mntonname, name) == 0) 7001558Srgrimes return (&mntbuf[i]); 70152678Sgreen } 7021558Srgrimes return (NULL); 7031558Srgrimes} 7041558Srgrimes 7051558Srgrimeschar * 706151042Srodrigccatopt(char *s0, const char *s1) 7071558Srgrimes{ 7081558Srgrimes size_t i; 7091558Srgrimes char *cp; 7101558Srgrimes 71146619Sjkoshy if (s1 == NULL || *s1 == '\0') 712159473Sjmallett return (s0); 71346619Sjkoshy 7141558Srgrimes if (s0 && *s0) { 7151558Srgrimes i = strlen(s0) + strlen(s1) + 1 + 1; 7161558Srgrimes if ((cp = malloc(i)) == NULL) 71737425Scharnier errx(1, "malloc failed"); 7181558Srgrimes (void)snprintf(cp, i, "%s,%s", s0, s1); 7191558Srgrimes } else 7201558Srgrimes cp = strdup(s1); 7211558Srgrimes 7221558Srgrimes if (s0) 7231558Srgrimes free(s0); 7241558Srgrimes return (cp); 7251558Srgrimes} 7261558Srgrimes 7271558Srgrimesvoid 728187035Sobrienmangle(char *options, struct cpa *a) 7291558Srgrimes{ 730189397Srodrigc char *p, *s, *val; 7311558Srgrimes 7321558Srgrimes for (s = options; (p = strsep(&s, ",")) != NULL;) 73346619Sjkoshy if (*p != '\0') { 734152465Srodrigc if (strcmp(p, "noauto") == 0) { 735152465Srodrigc /* 736152465Srodrigc * Do not pass noauto option to nmount(). 737152465Srodrigc * or external mount program. noauto is 738152465Srodrigc * only used to prevent mounting a filesystem 739152465Srodrigc * when 'mount -a' is specified, and is 740152465Srodrigc * not a real mount option. 741152465Srodrigc */ 742152465Srodrigc continue; 743160303Sdes } else if (strcmp(p, "late") == 0) { 744160303Sdes /* 745160303Sdes * "late" is used to prevent certain file 746160303Sdes * systems from being mounted before late 747160303Sdes * in the boot cycle; for instance, 748160303Sdes * loopback NFS mounts can't be mounted 749160303Sdes * before mountd starts. 750160303Sdes */ 751160303Sdes continue; 752222832Sdelphij } else if (strcmp(p, "failok") == 0) { 753222832Sdelphij /* 754222832Sdelphij * "failok" is used to prevent certain file 755222832Sdelphij * systems from being causing the system to 756222832Sdelphij * drop into single user mode in the boot 757222832Sdelphij * cycle, and is not a real mount option. 758222832Sdelphij */ 759222832Sdelphij continue; 760189397Srodrigc } else if (strncmp(p, "mountprog", 9) == 0) { 761189397Srodrigc /* 762189397Srodrigc * "mountprog" is used to force the use of 763189397Srodrigc * userland mount programs. 764189397Srodrigc */ 765189397Srodrigc val = strchr(p, '='); 766189397Srodrigc if (val != NULL) { 767189397Srodrigc ++val; 768189397Srodrigc if (*val != '\0') 769189397Srodrigc mountprog = strdup(val); 770189397Srodrigc } 771189397Srodrigc 772189397Srodrigc if (mountprog == NULL) { 773189397Srodrigc errx(1, "Need value for -o mountprog"); 774189397Srodrigc } 775189397Srodrigc continue; 776152732Srodrigc } else if (strcmp(p, "userquota") == 0) { 777152732Srodrigc continue; 778158400Smaxim } else if (strncmp(p, userquotaeq, 779158400Smaxim sizeof(userquotaeq) - 1) == 0) { 780158400Smaxim continue; 781152732Srodrigc } else if (strcmp(p, "groupquota") == 0) { 782152732Srodrigc continue; 783158400Smaxim } else if (strncmp(p, groupquotaeq, 784158400Smaxim sizeof(groupquotaeq) - 1) == 0) { 785158400Smaxim continue; 786152465Srodrigc } else if (*p == '-') { 787187035Sobrien append_arg(a, p); 7881558Srgrimes p = strchr(p, '='); 789123268Strhodes if (p != NULL) { 7901558Srgrimes *p = '\0'; 791187035Sobrien append_arg(a, p + 1); 7921558Srgrimes } 793153038Srodrigc } else { 794187035Sobrien append_arg(a, strdup("-o")); 795187035Sobrien append_arg(a, p); 7961558Srgrimes } 79746619Sjkoshy } 7981558Srgrimes} 7991558Srgrimes 80046619Sjkoshy 80146619Sjkoshychar * 802159473Sjmallettupdate_options(char *opts, char *fstab, int curflags) 80346619Sjkoshy{ 80446619Sjkoshy char *o, *p; 80546619Sjkoshy char *cur; 80646619Sjkoshy char *expopt, *newopt, *tmpopt; 80746619Sjkoshy 80846619Sjkoshy if (opts == NULL) 809159473Sjmallett return (strdup("")); 81046619Sjkoshy 81146619Sjkoshy /* remove meta options from list */ 81246619Sjkoshy remopt(fstab, MOUNT_META_OPTION_FSTAB); 81346619Sjkoshy remopt(fstab, MOUNT_META_OPTION_CURRENT); 81446619Sjkoshy cur = flags2opts(curflags); 81546619Sjkoshy 81646619Sjkoshy /* 81746619Sjkoshy * Expand all meta-options passed to us first. 81846619Sjkoshy */ 81946619Sjkoshy expopt = NULL; 82046619Sjkoshy for (p = opts; (o = strsep(&p, ",")) != NULL;) { 82146619Sjkoshy if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 82246619Sjkoshy expopt = catopt(expopt, fstab); 82346619Sjkoshy else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 82446619Sjkoshy expopt = catopt(expopt, cur); 82546619Sjkoshy else 82646619Sjkoshy expopt = catopt(expopt, o); 82746619Sjkoshy } 82846619Sjkoshy free(cur); 82946619Sjkoshy free(opts); 83046619Sjkoshy 83146619Sjkoshy /* 83246619Sjkoshy * Remove previous contradictory arguments. Given option "foo" we 83346619Sjkoshy * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 83446619Sjkoshy * and "foo" - so we can deal with possible options like "notice". 83546619Sjkoshy */ 83646619Sjkoshy newopt = NULL; 83746619Sjkoshy for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 83846619Sjkoshy if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 83946619Sjkoshy errx(1, "malloc failed"); 840125933Sgrog 84146619Sjkoshy strcpy(tmpopt, "no"); 84246619Sjkoshy strcat(tmpopt, o); 84346619Sjkoshy remopt(newopt, tmpopt); 84446619Sjkoshy free(tmpopt); 84546619Sjkoshy 84646619Sjkoshy if (strncmp("no", o, 2) == 0) 84746619Sjkoshy remopt(newopt, o+2); 84846619Sjkoshy 84946619Sjkoshy newopt = catopt(newopt, o); 85046619Sjkoshy } 85146619Sjkoshy free(expopt); 85246619Sjkoshy 853159473Sjmallett return (newopt); 85446619Sjkoshy} 85546619Sjkoshy 8561558Srgrimesvoid 857159473Sjmallettremopt(char *string, const char *opt) 85846619Sjkoshy{ 85946619Sjkoshy char *o, *p, *r; 86046619Sjkoshy 86146619Sjkoshy if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 86246619Sjkoshy return; 86346619Sjkoshy 86446619Sjkoshy r = string; 86546619Sjkoshy 86646619Sjkoshy for (p = string; (o = strsep(&p, ",")) != NULL;) { 86746619Sjkoshy if (strcmp(opt, o) != 0) { 86846619Sjkoshy if (*r == ',' && *o != '\0') 86946619Sjkoshy r++; 87046619Sjkoshy while ((*r++ = *o++) != '\0') 87146619Sjkoshy ; 87246619Sjkoshy *--r = ','; 87346619Sjkoshy } 87446619Sjkoshy } 87546619Sjkoshy *r = '\0'; 87646619Sjkoshy} 87746619Sjkoshy 87846619Sjkoshyvoid 879159473Sjmallettusage(void) 8801558Srgrimes{ 8811558Srgrimes 88237425Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 883160303Sdes"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 884141611Sru" mount [-dfpruvw] special | node", 885141611Sru" mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 8861558Srgrimes exit(1); 8871558Srgrimes} 88817243Sjkh 88917243Sjkhvoid 890175949Smatteoputfsent(struct statfs *ent) 89117243Sjkh{ 89223805Sbde struct fstab *fst; 893213298Sjh char *opts, *rw; 894168698Sphk int l; 895125933Sgrog 896213298Sjh opts = NULL; 897213298Sjh /* flags2opts() doesn't return the "rw" option. */ 898213298Sjh if ((ent->f_flags & MNT_RDONLY) != 0) 899213298Sjh rw = NULL; 900213298Sjh else 901213298Sjh rw = catopt(NULL, "rw"); 902213298Sjh 90346619Sjkoshy opts = flags2opts(ent->f_flags); 904213298Sjh opts = catopt(rw, opts); 905156229Skeramida 906175949Smatteo if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 || 907175949Smatteo strncmp(ent->f_mntfromname, "<above>", 7) == 0) { 908319261Sasomers strlcpy(ent->f_mntfromname, 909319261Sasomers (strnstr(ent->f_mntfromname, ":", 8) +1), 910319261Sasomers sizeof(ent->f_mntfromname)); 911175949Smatteo } 912175949Smatteo 913168698Sphk l = strlen(ent->f_mntfromname); 914168698Sphk printf("%s%s%s%s", ent->f_mntfromname, 915168698Sphk l < 8 ? "\t" : "", 916168698Sphk l < 16 ? "\t" : "", 917168698Sphk l < 24 ? "\t" : " "); 918168698Sphk l = strlen(ent->f_mntonname); 919168698Sphk printf("%s%s%s%s", ent->f_mntonname, 920168698Sphk l < 8 ? "\t" : "", 921168698Sphk l < 16 ? "\t" : "", 922168698Sphk l < 24 ? "\t" : " "); 923168698Sphk printf("%s\t", ent->f_fstypename); 924213298Sjh l = strlen(opts); 925213298Sjh printf("%s%s", opts, 926213298Sjh l < 8 ? "\t" : " "); 92746619Sjkoshy free(opts); 92817243Sjkh 92928671Ssteve if ((fst = getfsspec(ent->f_mntfromname))) 93023805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 93128671Ssteve else if ((fst = getfsfile(ent->f_mntonname))) 93223805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 93356038Sgreen else if (strcmp(ent->f_fstypename, "ufs") == 0) { 93456038Sgreen if (strcmp(ent->f_mntonname, "/") == 0) 93556038Sgreen printf("\t1 1\n"); 93656038Sgreen else 93756038Sgreen printf("\t2 2\n"); 93856038Sgreen } else 93923805Sbde printf("\t0 0\n"); 94017243Sjkh} 94146619Sjkoshy 94246619Sjkoshy 94346619Sjkoshychar * 944159473Sjmallettflags2opts(int flags) 94546619Sjkoshy{ 94646619Sjkoshy char *res; 94746619Sjkoshy 94846619Sjkoshy res = NULL; 94946619Sjkoshy 950155997Srodrigc if (flags & MNT_RDONLY) res = catopt(res, "ro"); 95146619Sjkoshy if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 95246619Sjkoshy if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 95346619Sjkoshy if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 95446619Sjkoshy if (flags & MNT_UNION) res = catopt(res, "union"); 95546619Sjkoshy if (flags & MNT_ASYNC) res = catopt(res, "async"); 95646619Sjkoshy if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 95746619Sjkoshy if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 95846619Sjkoshy if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 95946619Sjkoshy if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 96046619Sjkoshy if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 961101205Srwatson if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 962105118Srwatson if (flags & MNT_ACLS) res = catopt(res, "acls"); 963200796Strasz if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls"); 96446619Sjkoshy 965159473Sjmallett return (res); 96646619Sjkoshy} 967