mount.c revision 186504
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 186504 2008-12-26 22:54:53Z obrien $"; 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> 61166439Spjd#include <libutil.h> 621558Srgrimes 6328671Ssteve#include "extern.h" 6452055Sphk#include "mntopts.h" 651558Srgrimes#include "pathnames.h" 661558Srgrimes 6746619Sjkoshy/* `meta' options */ 68125933Sgrog#define MOUNT_META_OPTION_FSTAB "fstab" 6946619Sjkoshy#define MOUNT_META_OPTION_CURRENT "current" 7046619Sjkoshy 71186291Sobrien#define MAX_ARGS 100 72186291Sobrien 7323805Sbdeint debug, fstab_style, verbose; 741558Srgrimes 7592882Simpchar *catopt(char *, const char *); 7692882Simpstruct statfs *getmntpt(const char *); 7792882Simpint hasopt(const char *, const char *); 7892882Simpint ismounted(struct fstab *, struct statfs *, int); 7992882Simpint isremountable(const char *); 80159473Sjmallettvoid mangle(char *, int *, char *[]); 8192882Simpchar *update_options(char *, char *, int); 8292882Simpint mountfs(const char *, const char *, const char *, 8392882Simp int, const char *, const char *); 8492882Simpvoid remopt(char *, const char *); 8592882Simpvoid prmount(struct statfs *); 86175949Smatteovoid putfsent(struct statfs *); 8792882Simpvoid usage(void); 8892882Simpchar *flags2opts(int); 891558Srgrimes 9037425Scharnier/* Map from mount options to printable formats. */ 911558Srgrimesstatic struct opt { 921558Srgrimes int o_opt; 931558Srgrimes const char *o_name; 941558Srgrimes} optnames[] = { 951558Srgrimes { MNT_ASYNC, "asynchronous" }, 961558Srgrimes { MNT_EXPORTED, "NFS exported" }, 971558Srgrimes { MNT_LOCAL, "local" }, 9818007Sdg { MNT_NOATIME, "noatime" }, 991558Srgrimes { MNT_NOEXEC, "noexec" }, 1001558Srgrimes { MNT_NOSUID, "nosuid" }, 10135105Swosch { MNT_NOSYMFOLLOW, "nosymfollow" }, 1021558Srgrimes { MNT_QUOTA, "with quotas" }, 1031558Srgrimes { MNT_RDONLY, "read-only" }, 1041558Srgrimes { MNT_SYNCHRONOUS, "synchronous" }, 1051558Srgrimes { MNT_UNION, "union" }, 10629890Skato { MNT_NOCLUSTERR, "noclusterr" }, 10729890Skato { MNT_NOCLUSTERW, "noclusterw" }, 10831144Sjulian { MNT_SUIDDIR, "suiddir" }, 10934266Sjulian { MNT_SOFTDEP, "soft-updates" }, 110101205Srwatson { MNT_MULTILABEL, "multilabel" }, 111105118Srwatson { MNT_ACLS, "acls" }, 112163843Spjd { MNT_GJOURNAL, "gjournal" }, 11346619Sjkoshy { 0, NULL } 1141558Srgrimes}; 1151558Srgrimes 11628671Ssteve/* 11728671Ssteve * List of VFS types that can be remounted without becoming mounted on top 11828671Ssteve * of each other. 11928671Ssteve * XXX Is this list correct? 12028671Ssteve */ 12128671Sstevestatic const char * 12228671Ssteveremountable_fs_names[] = { 12369056Sphantom "ufs", "ffs", "ext2fs", 12428671Ssteve 0 12528671Ssteve}; 12628671Ssteve 127158400Smaximstatic const char userquotaeq[] = "userquota="; 128158400Smaximstatic const char groupquotaeq[] = "groupquota="; 129158400Smaxim 130152344Srodrigcstatic int 131152344Srodrigcuse_mountprog(const char *vfstype) 132152344Srodrigc{ 133152344Srodrigc /* XXX: We need to get away from implementing external mount 134152344Srodrigc * programs for every filesystem, and move towards having 135152344Srodrigc * each filesystem properly implement the nmount() system call. 136152344Srodrigc */ 137152344Srodrigc unsigned int i; 138152344Srodrigc const char *fs[] = { 139152778Savatar "cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs", 140171023Srafan "nwfs", "nullfs", "portalfs", "smbfs", "udf", "unionfs", 141152344Srodrigc NULL 142152344Srodrigc }; 143152344Srodrigc 144159473Sjmallett for (i = 0; fs[i] != NULL; ++i) { 145152344Srodrigc if (strcmp(vfstype, fs[i]) == 0) 146159473Sjmallett return (1); 147152344Srodrigc } 148152344Srodrigc 149159473Sjmallett return (0); 150152344Srodrigc} 151152344Srodrigc 152152344Srodrigcstatic int 153159473Sjmallettexec_mountprog(const char *name, const char *execname, char *const argv[]) 154152344Srodrigc{ 155152344Srodrigc pid_t pid; 156152344Srodrigc int status; 157152344Srodrigc 158152344Srodrigc switch (pid = fork()) { 159152344Srodrigc case -1: /* Error. */ 160152344Srodrigc warn("fork"); 161152344Srodrigc exit (1); 162152344Srodrigc case 0: /* Child. */ 163152344Srodrigc /* Go find an executable. */ 164152344Srodrigc execvP(execname, _PATH_SYSPATH, argv); 165152344Srodrigc if (errno == ENOENT) { 166152344Srodrigc warn("exec %s not found in %s", execname, 167152344Srodrigc _PATH_SYSPATH); 168152344Srodrigc } 169152344Srodrigc exit(1); 170152344Srodrigc default: /* Parent. */ 171152344Srodrigc if (waitpid(pid, &status, 0) < 0) { 172152344Srodrigc warn("waitpid"); 173152344Srodrigc return (1); 174152344Srodrigc } 175152344Srodrigc 176152344Srodrigc if (WIFEXITED(status)) { 177152344Srodrigc if (WEXITSTATUS(status) != 0) 178152344Srodrigc return (WEXITSTATUS(status)); 179152344Srodrigc } else if (WIFSIGNALED(status)) { 180152344Srodrigc warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 181152344Srodrigc return (1); 182152344Srodrigc } 183152344Srodrigc break; 184152344Srodrigc } 185152344Srodrigc 186152344Srodrigc return (0); 187152344Srodrigc} 188152344Srodrigc 189163672Srustatic int 190163672Sruspecified_ro(const char *arg) 191163671Sru{ 192163671Sru char *optbuf, *opt; 193163671Sru int ret = 0; 194163671Sru 195163671Sru optbuf = strdup(arg); 196163671Sru if (optbuf == NULL) 197163671Sru err(1, NULL); 198163671Sru 199163671Sru for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 200163671Sru if (strcmp(opt, "ro") == 0) { 201163671Sru ret = 1; 202163671Sru break; 203163671Sru } 204163671Sru } 205163671Sru free(optbuf); 206163671Sru return (ret); 207163671Sru} 208163671Sru 209166439Spjdstatic void 210166439Spjdrestart_mountd(void) 211166439Spjd{ 212166439Spjd struct pidfh *pfh; 213166439Spjd pid_t mountdpid; 214166439Spjd 215166439Spjd pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid); 216166439Spjd if (pfh != NULL) { 217166439Spjd /* Mountd is not running. */ 218166439Spjd pidfile_remove(pfh); 219166439Spjd return; 220166439Spjd } 221166439Spjd if (errno != EEXIST) { 222166439Spjd /* Cannot open pidfile for some reason. */ 223166439Spjd return; 224166439Spjd } 225166439Spjd /* We have mountd(8) PID in mountdpid varible, let's signal it. */ 226166439Spjd if (kill(mountdpid, SIGHUP) == -1) 227166439Spjd err(1, "signal mountd"); 228166439Spjd} 229166439Spjd 2301558Srgrimesint 231151042Srodrigcmain(int argc, char *argv[]) 2321558Srgrimes{ 23323678Speter const char *mntfromname, **vfslist, *vfstype; 2341558Srgrimes struct fstab *fs; 2351558Srgrimes struct statfs *mntbuf; 236163671Sru int all, ch, i, init_flags, late, mntsize, rval, have_fstab, ro; 23795289Smux char *cp, *ep, *options; 2381558Srgrimes 239160303Sdes all = init_flags = late = 0; 240163671Sru ro = 0; 241163671Sru options = NULL; 2421558Srgrimes vfslist = NULL; 2431558Srgrimes vfstype = "ufs"; 244163672Sru while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1) 2451558Srgrimes switch (ch) { 2461558Srgrimes case 'a': 2471558Srgrimes all = 1; 2481558Srgrimes break; 2491558Srgrimes case 'd': 2501558Srgrimes debug = 1; 2511558Srgrimes break; 252113220Smdodd case 'F': 253113220Smdodd setfstab(optarg); 254113220Smdodd break; 2551558Srgrimes case 'f': 2561558Srgrimes init_flags |= MNT_FORCE; 2571558Srgrimes break; 258160303Sdes case 'l': 259160303Sdes late = 1; 260160303Sdes break; 2611558Srgrimes case 'o': 262163671Sru if (*optarg) { 263163671Sru options = catopt(options, optarg); 264163671Sru if (specified_ro(optarg)) 265163671Sru ro = 1; 266163671Sru } 2671558Srgrimes break; 26823805Sbde case 'p': 26923805Sbde fstab_style = 1; 27023805Sbde verbose = 1; 27123805Sbde break; 2721558Srgrimes case 'r': 27376198Sdd options = catopt(options, "ro"); 274163671Sru ro = 1; 2751558Srgrimes break; 2761558Srgrimes case 't': 2771558Srgrimes if (vfslist != NULL) 27837425Scharnier errx(1, "only one -t option may be specified"); 2791558Srgrimes vfslist = makevfslist(optarg); 2801558Srgrimes vfstype = optarg; 2811558Srgrimes break; 2821558Srgrimes case 'u': 2831558Srgrimes init_flags |= MNT_UPDATE; 2841558Srgrimes break; 2851558Srgrimes case 'v': 2861558Srgrimes verbose = 1; 2871558Srgrimes break; 2881558Srgrimes case 'w': 28976198Sdd options = catopt(options, "noro"); 2901558Srgrimes break; 2911558Srgrimes case '?': 2921558Srgrimes default: 2931558Srgrimes usage(); 2941558Srgrimes /* NOTREACHED */ 2951558Srgrimes } 2961558Srgrimes argc -= optind; 2971558Srgrimes argv += optind; 2981558Srgrimes 2991558Srgrimes#define BADTYPE(type) \ 3001558Srgrimes (strcmp(type, FSTAB_RO) && \ 3011558Srgrimes strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 3021558Srgrimes 303163671Sru if ((init_flags & MNT_UPDATE) && (ro == 0)) 304163671Sru options = catopt(options, "noro"); 305163671Sru 3061558Srgrimes rval = 0; 3071558Srgrimes switch (argc) { 3081558Srgrimes case 0: 30928671Ssteve if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 31028671Ssteve err(1, "getmntinfo"); 31128671Ssteve if (all) { 3121558Srgrimes while ((fs = getfsent()) != NULL) { 3131558Srgrimes if (BADTYPE(fs->fs_type)) 3141558Srgrimes continue; 31523678Speter if (checkvfsname(fs->fs_vfstype, vfslist)) 3161558Srgrimes continue; 31723678Speter if (hasopt(fs->fs_mntops, "noauto")) 31810288Sdg continue; 319160303Sdes if (hasopt(fs->fs_mntops, "late") && !late) 320160303Sdes continue; 32144811Sbde if (!(init_flags & MNT_UPDATE) && 32244811Sbde ismounted(fs, mntbuf, mntsize)) 32328671Ssteve continue; 324125194Sguido options = update_options(options, fs->fs_mntops, 325125194Sguido mntbuf->f_flags); 3261558Srgrimes if (mountfs(fs->fs_vfstype, fs->fs_spec, 32723805Sbde fs->fs_file, init_flags, options, 32823805Sbde fs->fs_mntops)) 32923805Sbde rval = 1; 3301558Srgrimes } 33128671Ssteve } else if (fstab_style) { 33217243Sjkh for (i = 0; i < mntsize; i++) { 33323678Speter if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 33417243Sjkh continue; 33523805Sbde putfsent(&mntbuf[i]); 33617243Sjkh } 33723805Sbde } else { 3381558Srgrimes for (i = 0; i < mntsize; i++) { 33923805Sbde if (checkvfsname(mntbuf[i].f_fstypename, 34023805Sbde vfslist)) 3411558Srgrimes continue; 34223678Speter prmount(&mntbuf[i]); 3431558Srgrimes } 3441558Srgrimes } 3451558Srgrimes exit(rval); 3461558Srgrimes case 1: 3471558Srgrimes if (vfslist != NULL) 3481558Srgrimes usage(); 3491558Srgrimes 350124201Sanholt rmslashes(*argv, *argv); 3511558Srgrimes if (init_flags & MNT_UPDATE) { 35252678Sgreen mntfromname = NULL; 35352678Sgreen have_fstab = 0; 3541558Srgrimes if ((mntbuf = getmntpt(*argv)) == NULL) 35552678Sgreen errx(1, "not currently mounted %s", *argv); 35652678Sgreen /* 35752678Sgreen * Only get the mntflags from fstab if both mntpoint 35852678Sgreen * and mntspec are identical. Also handle the special 35952678Sgreen * case where just '/' is mounted and 'spec' is not 36052678Sgreen * identical with the one from fstab ('/dev' is missing 36152678Sgreen * in the spec-string at boot-time). 36252678Sgreen */ 36346619Sjkoshy if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 36452678Sgreen if (strcmp(fs->fs_spec, 36552678Sgreen mntbuf->f_mntfromname) == 0 && 36652678Sgreen strcmp(fs->fs_file, 36752678Sgreen mntbuf->f_mntonname) == 0) { 36852678Sgreen have_fstab = 1; 36952678Sgreen mntfromname = mntbuf->f_mntfromname; 37052678Sgreen } else if (argv[0][0] == '/' && 37152678Sgreen argv[0][1] == '\0') { 37252678Sgreen fs = getfsfile("/"); 37352678Sgreen have_fstab = 1; 37452678Sgreen mntfromname = fs->fs_spec; 37552678Sgreen } 37652678Sgreen } 37752678Sgreen if (have_fstab) { 37846619Sjkoshy options = update_options(options, fs->fs_mntops, 37946619Sjkoshy mntbuf->f_flags); 38046619Sjkoshy } else { 38123678Speter mntfromname = mntbuf->f_mntfromname; 38246619Sjkoshy options = update_options(options, NULL, 38346619Sjkoshy mntbuf->f_flags); 38446619Sjkoshy } 38523678Speter rval = mountfs(mntbuf->f_fstypename, mntfromname, 38623678Speter mntbuf->f_mntonname, init_flags, options, 0); 38723678Speter break; 3881558Srgrimes } 38923678Speter if ((fs = getfsfile(*argv)) == NULL && 39023678Speter (fs = getfsspec(*argv)) == NULL) 391102231Strhodes errx(1, "%s: unknown special file or file system", 39223678Speter *argv); 39323678Speter if (BADTYPE(fs->fs_type)) 394102231Strhodes errx(1, "%s has unknown file system type", 39523678Speter *argv); 39623678Speter rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 39723678Speter init_flags, options, fs->fs_mntops); 3981558Srgrimes break; 3991558Srgrimes case 2: 4001558Srgrimes /* 40152678Sgreen * If -t flag has not been specified, the path cannot be 40295289Smux * found, spec contains either a ':' or a '@', then assume 403102231Strhodes * that an NFS file system is being specified ala Sun. 40495289Smux * Check if the hostname contains only allowed characters 40595289Smux * to reduce false positives. IPv6 addresses containing 40695289Smux * ':' will be correctly parsed only if the separator is '@'. 40795289Smux * The definition of a valid hostname is taken from RFC 1034. 4081558Srgrimes */ 409123162Siedowse if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL || 410123162Siedowse (ep = strchr(argv[0], ':')) != NULL)) { 41195318Smux if (*ep == '@') { 41295318Smux cp = ep + 1; 41395318Smux ep = cp + strlen(cp); 41495318Smux } else 41595318Smux cp = argv[0]; 41695289Smux while (cp != ep) { 41795289Smux if (!isdigit(*cp) && !isalpha(*cp) && 41895289Smux *cp != '.' && *cp != '-' && *cp != ':') 41995289Smux break; 42095289Smux cp++; 42195289Smux } 42295289Smux if (cp == ep) 42395289Smux vfstype = "nfs"; 42495289Smux } 4251558Srgrimes rval = mountfs(vfstype, 4261558Srgrimes argv[0], argv[1], init_flags, options, NULL); 4271558Srgrimes break; 4281558Srgrimes default: 4291558Srgrimes usage(); 4301558Srgrimes /* NOTREACHED */ 4311558Srgrimes } 4321558Srgrimes 4331558Srgrimes /* 4341558Srgrimes * If the mount was successfully, and done by root, tell mountd the 435166439Spjd * good news. 4361558Srgrimes */ 437166439Spjd if (rval == 0 && getuid() == 0) 438166439Spjd restart_mountd(); 4391558Srgrimes 4401558Srgrimes exit(rval); 4411558Srgrimes} 4421558Srgrimes 4431558Srgrimesint 444151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize) 44528671Ssteve{ 446154512Spjd char realfsfile[PATH_MAX]; 44728671Ssteve int i; 44828671Ssteve 44928671Ssteve if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 450102231Strhodes /* the root file system can always be remounted */ 45128671Ssteve return (0); 45228671Ssteve 453154512Spjd /* The user may have specified a symlink in fstab, resolve the path */ 454154512Spjd if (realpath(fs->fs_file, realfsfile) == NULL) { 455154512Spjd /* Cannot resolve the path, use original one */ 456154512Spjd strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile)); 457154512Spjd } 458154512Spjd 45928671Ssteve for (i = mntsize - 1; i >= 0; --i) 460154512Spjd if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 && 46128671Ssteve (!isremountable(fs->fs_vfstype) || 46228671Ssteve strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)) 46328671Ssteve return (1); 46428671Ssteve return (0); 46528671Ssteve} 46628671Ssteve 46728671Ssteveint 468151042Srodrigcisremountable(const char *vfsname) 46928671Ssteve{ 47028671Ssteve const char **cp; 47128671Ssteve 47228671Ssteve for (cp = remountable_fs_names; *cp; cp++) 47328671Ssteve if (strcmp(*cp, vfsname) == 0) 47428671Ssteve return (1); 47528671Ssteve return (0); 47628671Ssteve} 47728671Ssteve 47828671Ssteveint 479151042Srodrigchasopt(const char *mntopts, const char *option) 48023678Speter{ 48123678Speter int negative, found; 48223678Speter char *opt, *optbuf; 48323678Speter 48423678Speter if (option[0] == 'n' && option[1] == 'o') { 48523678Speter negative = 1; 48623678Speter option += 2; 48723678Speter } else 48823678Speter negative = 0; 48923678Speter optbuf = strdup(mntopts); 49023678Speter found = 0; 49123678Speter for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 49223678Speter if (opt[0] == 'n' && opt[1] == 'o') { 49323678Speter if (!strcasecmp(opt + 2, option)) 49423678Speter found = negative; 49523678Speter } else if (!strcasecmp(opt, option)) 49623678Speter found = !negative; 49723678Speter } 49823678Speter free(optbuf); 49923678Speter return (found); 50023678Speter} 50123678Speter 50223678Speterint 503151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags, 504151042Srodrigc const char *options, const char *mntopts) 5051558Srgrimes{ 506186504Sobrien static int argc; 507186291Sobrien char *argv[MAX_ARGS]; 5081558Srgrimes struct statfs sf; 509186504Sobrien int i, ret; 510118580Simp char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; 5111558Srgrimes 51252055Sphk /* resolve the mountpoint with realpath(3) */ 51352055Sphk (void)checkpath(name, mntpath); 5141558Srgrimes name = mntpath; 5151558Srgrimes 51623678Speter if (mntopts == NULL) 51723678Speter mntopts = ""; 5181558Srgrimes optbuf = catopt(strdup(mntopts), options); 5191558Srgrimes 5201558Srgrimes if (strcmp(name, "/") == 0) 5211558Srgrimes flags |= MNT_UPDATE; 5221558Srgrimes if (flags & MNT_FORCE) 5231558Srgrimes optbuf = catopt(optbuf, "force"); 5241558Srgrimes if (flags & MNT_RDONLY) 5251558Srgrimes optbuf = catopt(optbuf, "ro"); 5261558Srgrimes /* 5271558Srgrimes * XXX 5281558Srgrimes * The mount_mfs (newfs) command uses -o to select the 52937425Scharnier * optimization mode. We don't pass the default "-o rw" 5301558Srgrimes * for that reason. 5311558Srgrimes */ 5321558Srgrimes if (flags & MNT_UPDATE) 5331558Srgrimes optbuf = catopt(optbuf, "update"); 5341558Srgrimes 53577577Sru /* Compatibility glue. */ 536166526Srodrigc if (strcmp(vfstype, "msdos") == 0) { 537166526Srodrigc warnx( 538166526Srodrigc "Using \"-t msdosfs\", since \"-t msdos\" is deprecated."); 53977577Sru vfstype = "msdosfs"; 540166526Srodrigc } 54177577Sru 542144133Scperciva /* Construct the name of the appropriate mount command */ 543144133Scperciva (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 544144133Scperciva 5451558Srgrimes argc = 0; 546144133Scperciva argv[argc++] = execname; 5471558Srgrimes mangle(optbuf, &argc, argv); 548152344Srodrigc argv[argc++] = strdup(spec); 549152344Srodrigc argv[argc++] = strdup(name); 5501558Srgrimes argv[argc] = NULL; 5511558Srgrimes 552186291Sobrien if (MAX_ARGS <= argc ) 553186291Sobrien errx(1, "Cannot process more than %d mount arguments", 554186291Sobrien MAX_ARGS); 555186291Sobrien 5561558Srgrimes if (debug) { 557164266Srodrigc if (use_mountprog(vfstype)) 558164266Srodrigc printf("exec: mount_%s", vfstype); 559164266Srodrigc else 560164266Srodrigc printf("mount -t %s", vfstype); 5611558Srgrimes for (i = 1; i < argc; i++) 5621558Srgrimes (void)printf(" %s", argv[i]); 5631558Srgrimes (void)printf("\n"); 5641558Srgrimes return (0); 5651558Srgrimes } 5661558Srgrimes 567152668Srodrigc if (use_mountprog(vfstype)) { 568152344Srodrigc ret = exec_mountprog(name, execname, argv); 569152344Srodrigc } else { 570152344Srodrigc ret = mount_fs(vfstype, argc, argv); 571152344Srodrigc } 5721558Srgrimes 573152344Srodrigc free(optbuf); 5741558Srgrimes 575152344Srodrigc if (verbose) { 576152344Srodrigc if (statfs(name, &sf) < 0) { 577152344Srodrigc warn("statfs %s", name); 5781558Srgrimes return (1); 5791558Srgrimes } 580152344Srodrigc if (fstab_style) 581152344Srodrigc putfsent(&sf); 582152344Srodrigc else 583152344Srodrigc prmount(&sf); 5841558Srgrimes } 5851558Srgrimes 586182570Smatteo return (ret); 5871558Srgrimes} 5881558Srgrimes 5891558Srgrimesvoid 590151042Srodrigcprmount(struct statfs *sfp) 59123678Speter{ 592151043Srodrigc int flags; 593151043Srodrigc unsigned int i; 5941558Srgrimes struct opt *o; 59523678Speter struct passwd *pw; 5961558Srgrimes 59752036Sn_hibma (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 59852036Sn_hibma sfp->f_fstypename); 5991558Srgrimes 60023678Speter flags = sfp->f_flags & MNT_VISFLAGMASK; 60152036Sn_hibma for (o = optnames; flags && o->o_opt; o++) 6021558Srgrimes if (flags & o->o_opt) { 60352036Sn_hibma (void)printf(", %s", o->o_name); 6041558Srgrimes flags &= ~o->o_opt; 6051558Srgrimes } 606125339Spjd /* 607125339Spjd * Inform when file system is mounted by an unprivileged user 608125339Spjd * or privileged non-root user. 609125339Spjd */ 610125365Snectar if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 61152036Sn_hibma (void)printf(", mounted by "); 61223678Speter if ((pw = getpwuid(sfp->f_owner)) != NULL) 61323678Speter (void)printf("%s", pw->pw_name); 61423678Speter else 61523678Speter (void)printf("%d", sfp->f_owner); 61623678Speter } 61765023Ssheldonh if (verbose) { 61865023Ssheldonh if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 619123242Siedowse (void)printf(", writes: sync %ju async %ju", 620123242Siedowse (uintmax_t)sfp->f_syncwrites, 621123242Siedowse (uintmax_t)sfp->f_asyncwrites); 62265023Ssheldonh if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 623123242Siedowse (void)printf(", reads: sync %ju async %ju", 624123242Siedowse (uintmax_t)sfp->f_syncreads, 625123242Siedowse (uintmax_t)sfp->f_asyncreads); 626123162Siedowse if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 627123162Siedowse printf(", fsid "); 628123162Siedowse for (i = 0; i < sizeof(sfp->f_fsid); i++) 629123162Siedowse printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 630123162Siedowse } 63165023Ssheldonh } 63252036Sn_hibma (void)printf(")\n"); 6331558Srgrimes} 6341558Srgrimes 6351558Srgrimesstruct statfs * 636151042Srodrigcgetmntpt(const char *name) 6371558Srgrimes{ 6381558Srgrimes struct statfs *mntbuf; 6391558Srgrimes int i, mntsize; 6401558Srgrimes 6411558Srgrimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 64252678Sgreen for (i = mntsize - 1; i >= 0; i--) { 6431558Srgrimes if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 6441558Srgrimes strcmp(mntbuf[i].f_mntonname, name) == 0) 6451558Srgrimes return (&mntbuf[i]); 64652678Sgreen } 6471558Srgrimes return (NULL); 6481558Srgrimes} 6491558Srgrimes 6501558Srgrimeschar * 651151042Srodrigccatopt(char *s0, const char *s1) 6521558Srgrimes{ 6531558Srgrimes size_t i; 6541558Srgrimes char *cp; 6551558Srgrimes 65646619Sjkoshy if (s1 == NULL || *s1 == '\0') 657159473Sjmallett return (s0); 65846619Sjkoshy 6591558Srgrimes if (s0 && *s0) { 6601558Srgrimes i = strlen(s0) + strlen(s1) + 1 + 1; 6611558Srgrimes if ((cp = malloc(i)) == NULL) 66237425Scharnier errx(1, "malloc failed"); 6631558Srgrimes (void)snprintf(cp, i, "%s,%s", s0, s1); 6641558Srgrimes } else 6651558Srgrimes cp = strdup(s1); 6661558Srgrimes 6671558Srgrimes if (s0) 6681558Srgrimes free(s0); 6691558Srgrimes return (cp); 6701558Srgrimes} 6711558Srgrimes 6721558Srgrimesvoid 673159473Sjmallettmangle(char *options, int *argcp, char *argv[]) 6741558Srgrimes{ 6751558Srgrimes char *p, *s; 6761558Srgrimes int argc; 6771558Srgrimes 6781558Srgrimes argc = *argcp; 6791558Srgrimes for (s = options; (p = strsep(&s, ",")) != NULL;) 68046619Sjkoshy if (*p != '\0') { 681152465Srodrigc if (strcmp(p, "noauto") == 0) { 682152465Srodrigc /* 683152465Srodrigc * Do not pass noauto option to nmount(). 684152465Srodrigc * or external mount program. noauto is 685152465Srodrigc * only used to prevent mounting a filesystem 686152465Srodrigc * when 'mount -a' is specified, and is 687152465Srodrigc * not a real mount option. 688152465Srodrigc */ 689152465Srodrigc continue; 690160303Sdes } else if (strcmp(p, "late") == 0) { 691160303Sdes /* 692160303Sdes * "late" is used to prevent certain file 693160303Sdes * systems from being mounted before late 694160303Sdes * in the boot cycle; for instance, 695160303Sdes * loopback NFS mounts can't be mounted 696160303Sdes * before mountd starts. 697160303Sdes */ 698160303Sdes continue; 699152732Srodrigc } else if (strcmp(p, "userquota") == 0) { 700152732Srodrigc continue; 701158400Smaxim } else if (strncmp(p, userquotaeq, 702158400Smaxim sizeof(userquotaeq) - 1) == 0) { 703158400Smaxim continue; 704152732Srodrigc } else if (strcmp(p, "groupquota") == 0) { 705152732Srodrigc continue; 706158400Smaxim } else if (strncmp(p, groupquotaeq, 707158400Smaxim sizeof(groupquotaeq) - 1) == 0) { 708158400Smaxim continue; 709152465Srodrigc } else if (*p == '-') { 7101558Srgrimes argv[argc++] = p; 7111558Srgrimes p = strchr(p, '='); 712123268Strhodes if (p != NULL) { 7131558Srgrimes *p = '\0'; 7141558Srgrimes argv[argc++] = p+1; 7151558Srgrimes } 716153038Srodrigc } else { 717152344Srodrigc argv[argc++] = strdup("-o"); 7181558Srgrimes argv[argc++] = p; 7191558Srgrimes } 72046619Sjkoshy } 7211558Srgrimes 7221558Srgrimes *argcp = argc; 7231558Srgrimes} 7241558Srgrimes 72546619Sjkoshy 72646619Sjkoshychar * 727159473Sjmallettupdate_options(char *opts, char *fstab, int curflags) 72846619Sjkoshy{ 72946619Sjkoshy char *o, *p; 73046619Sjkoshy char *cur; 73146619Sjkoshy char *expopt, *newopt, *tmpopt; 73246619Sjkoshy 73346619Sjkoshy if (opts == NULL) 734159473Sjmallett return (strdup("")); 73546619Sjkoshy 73646619Sjkoshy /* remove meta options from list */ 73746619Sjkoshy remopt(fstab, MOUNT_META_OPTION_FSTAB); 73846619Sjkoshy remopt(fstab, MOUNT_META_OPTION_CURRENT); 73946619Sjkoshy cur = flags2opts(curflags); 74046619Sjkoshy 74146619Sjkoshy /* 74246619Sjkoshy * Expand all meta-options passed to us first. 74346619Sjkoshy */ 74446619Sjkoshy expopt = NULL; 74546619Sjkoshy for (p = opts; (o = strsep(&p, ",")) != NULL;) { 74646619Sjkoshy if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 74746619Sjkoshy expopt = catopt(expopt, fstab); 74846619Sjkoshy else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 74946619Sjkoshy expopt = catopt(expopt, cur); 75046619Sjkoshy else 75146619Sjkoshy expopt = catopt(expopt, o); 75246619Sjkoshy } 75346619Sjkoshy free(cur); 75446619Sjkoshy free(opts); 75546619Sjkoshy 75646619Sjkoshy /* 75746619Sjkoshy * Remove previous contradictory arguments. Given option "foo" we 75846619Sjkoshy * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 75946619Sjkoshy * and "foo" - so we can deal with possible options like "notice". 76046619Sjkoshy */ 76146619Sjkoshy newopt = NULL; 76246619Sjkoshy for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 76346619Sjkoshy if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 76446619Sjkoshy errx(1, "malloc failed"); 765125933Sgrog 76646619Sjkoshy strcpy(tmpopt, "no"); 76746619Sjkoshy strcat(tmpopt, o); 76846619Sjkoshy remopt(newopt, tmpopt); 76946619Sjkoshy free(tmpopt); 77046619Sjkoshy 77146619Sjkoshy if (strncmp("no", o, 2) == 0) 77246619Sjkoshy remopt(newopt, o+2); 77346619Sjkoshy 77446619Sjkoshy newopt = catopt(newopt, o); 77546619Sjkoshy } 77646619Sjkoshy free(expopt); 77746619Sjkoshy 778159473Sjmallett return (newopt); 77946619Sjkoshy} 78046619Sjkoshy 7811558Srgrimesvoid 782159473Sjmallettremopt(char *string, const char *opt) 78346619Sjkoshy{ 78446619Sjkoshy char *o, *p, *r; 78546619Sjkoshy 78646619Sjkoshy if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 78746619Sjkoshy return; 78846619Sjkoshy 78946619Sjkoshy r = string; 79046619Sjkoshy 79146619Sjkoshy for (p = string; (o = strsep(&p, ",")) != NULL;) { 79246619Sjkoshy if (strcmp(opt, o) != 0) { 79346619Sjkoshy if (*r == ',' && *o != '\0') 79446619Sjkoshy r++; 79546619Sjkoshy while ((*r++ = *o++) != '\0') 79646619Sjkoshy ; 79746619Sjkoshy *--r = ','; 79846619Sjkoshy } 79946619Sjkoshy } 80046619Sjkoshy *r = '\0'; 80146619Sjkoshy} 80246619Sjkoshy 80346619Sjkoshyvoid 804159473Sjmallettusage(void) 8051558Srgrimes{ 8061558Srgrimes 80737425Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 808160303Sdes"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 809141611Sru" mount [-dfpruvw] special | node", 810141611Sru" mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 8111558Srgrimes exit(1); 8121558Srgrimes} 81317243Sjkh 81417243Sjkhvoid 815175949Smatteoputfsent(struct statfs *ent) 81617243Sjkh{ 81723805Sbde struct fstab *fst; 81846619Sjkoshy char *opts; 819168698Sphk int l; 820125933Sgrog 82146619Sjkoshy opts = flags2opts(ent->f_flags); 822156229Skeramida 823175949Smatteo if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 || 824175949Smatteo strncmp(ent->f_mntfromname, "<above>", 7) == 0) { 825175949Smatteo strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8) 826175949Smatteo +1)); 827175949Smatteo } 828175949Smatteo 829156229Skeramida /* 830156229Skeramida * "rw" is not a real mount option; this is why we print NULL as "rw" 831156229Skeramida * if opts is still NULL here. 832156229Skeramida */ 833168698Sphk l = strlen(ent->f_mntfromname); 834168698Sphk printf("%s%s%s%s", ent->f_mntfromname, 835168698Sphk l < 8 ? "\t" : "", 836168698Sphk l < 16 ? "\t" : "", 837168698Sphk l < 24 ? "\t" : " "); 838168698Sphk l = strlen(ent->f_mntonname); 839168698Sphk printf("%s%s%s%s", ent->f_mntonname, 840168698Sphk l < 8 ? "\t" : "", 841168698Sphk l < 16 ? "\t" : "", 842168698Sphk l < 24 ? "\t" : " "); 843168698Sphk printf("%s\t", ent->f_fstypename); 844168698Sphk if (opts == NULL) { 845168698Sphk printf("%s\t", "rw"); 846168698Sphk } else { 847168698Sphk l = strlen(opts); 848168698Sphk printf("%s%s", opts, 849168698Sphk l < 8 ? "\t" : " "); 850168698Sphk } 85146619Sjkoshy free(opts); 85217243Sjkh 85328671Ssteve if ((fst = getfsspec(ent->f_mntfromname))) 85423805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 85528671Ssteve else if ((fst = getfsfile(ent->f_mntonname))) 85623805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 85756038Sgreen else if (strcmp(ent->f_fstypename, "ufs") == 0) { 85856038Sgreen if (strcmp(ent->f_mntonname, "/") == 0) 85956038Sgreen printf("\t1 1\n"); 86056038Sgreen else 86156038Sgreen printf("\t2 2\n"); 86256038Sgreen } else 86323805Sbde printf("\t0 0\n"); 86417243Sjkh} 86546619Sjkoshy 86646619Sjkoshy 86746619Sjkoshychar * 868159473Sjmallettflags2opts(int flags) 86946619Sjkoshy{ 87046619Sjkoshy char *res; 87146619Sjkoshy 87246619Sjkoshy res = NULL; 87346619Sjkoshy 874155997Srodrigc if (flags & MNT_RDONLY) res = catopt(res, "ro"); 87546619Sjkoshy if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 87646619Sjkoshy if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 87746619Sjkoshy if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 87846619Sjkoshy if (flags & MNT_UNION) res = catopt(res, "union"); 87946619Sjkoshy if (flags & MNT_ASYNC) res = catopt(res, "async"); 88046619Sjkoshy if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 88146619Sjkoshy if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 88246619Sjkoshy if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 88346619Sjkoshy if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 88446619Sjkoshy if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 885101205Srwatson if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 886105118Srwatson if (flags & MNT_ACLS) res = catopt(res, "acls"); 88746619Sjkoshy 888159473Sjmallett return (res); 88946619Sjkoshy} 890