mount.c revision 187093
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: head/sbin/mount/mount.c 187093 2009-01-12 08:22:36Z obrien $"); 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 6923805Sbdeint debug, fstab_style, verbose; 701558Srgrimes 71187035Sobrienstruct cpa { 72187093Sobrien char **a; 73187035Sobrien int c; 74187035Sobrien}; 75187035Sobrien 7692882Simpchar *catopt(char *, const char *); 7792882Simpstruct statfs *getmntpt(const char *); 7892882Simpint hasopt(const char *, const char *); 7992882Simpint ismounted(struct fstab *, struct statfs *, int); 8092882Simpint isremountable(const char *); 81187035Sobrienvoid mangle(char *, struct cpa *); 8292882Simpchar *update_options(char *, char *, int); 8392882Simpint mountfs(const char *, const char *, const char *, 8492882Simp int, const char *, const char *); 8592882Simpvoid remopt(char *, const char *); 8692882Simpvoid prmount(struct statfs *); 87175949Smatteovoid putfsent(struct statfs *); 8892882Simpvoid usage(void); 8992882Simpchar *flags2opts(int); 901558Srgrimes 9137425Scharnier/* Map from mount options to printable formats. */ 921558Srgrimesstatic struct opt { 931558Srgrimes int o_opt; 941558Srgrimes const char *o_name; 951558Srgrimes} optnames[] = { 961558Srgrimes { MNT_ASYNC, "asynchronous" }, 971558Srgrimes { MNT_EXPORTED, "NFS exported" }, 981558Srgrimes { MNT_LOCAL, "local" }, 9918007Sdg { MNT_NOATIME, "noatime" }, 1001558Srgrimes { MNT_NOEXEC, "noexec" }, 1011558Srgrimes { MNT_NOSUID, "nosuid" }, 10235105Swosch { MNT_NOSYMFOLLOW, "nosymfollow" }, 1031558Srgrimes { MNT_QUOTA, "with quotas" }, 1041558Srgrimes { MNT_RDONLY, "read-only" }, 1051558Srgrimes { MNT_SYNCHRONOUS, "synchronous" }, 1061558Srgrimes { MNT_UNION, "union" }, 10729890Skato { MNT_NOCLUSTERR, "noclusterr" }, 10829890Skato { MNT_NOCLUSTERW, "noclusterw" }, 10931144Sjulian { MNT_SUIDDIR, "suiddir" }, 11034266Sjulian { MNT_SOFTDEP, "soft-updates" }, 111101205Srwatson { MNT_MULTILABEL, "multilabel" }, 112105118Srwatson { MNT_ACLS, "acls" }, 113163843Spjd { MNT_GJOURNAL, "gjournal" }, 11446619Sjkoshy { 0, NULL } 1151558Srgrimes}; 1161558Srgrimes 11728671Ssteve/* 11828671Ssteve * List of VFS types that can be remounted without becoming mounted on top 11928671Ssteve * of each other. 12028671Ssteve * XXX Is this list correct? 12128671Ssteve */ 12228671Sstevestatic const char * 12328671Ssteveremountable_fs_names[] = { 12469056Sphantom "ufs", "ffs", "ext2fs", 12528671Ssteve 0 12628671Ssteve}; 12728671Ssteve 128158400Smaximstatic const char userquotaeq[] = "userquota="; 129158400Smaximstatic const char groupquotaeq[] = "groupquota="; 130158400Smaxim 131152344Srodrigcstatic int 132152344Srodrigcuse_mountprog(const char *vfstype) 133152344Srodrigc{ 134152344Srodrigc /* XXX: We need to get away from implementing external mount 135152344Srodrigc * programs for every filesystem, and move towards having 136152344Srodrigc * each filesystem properly implement the nmount() system call. 137152344Srodrigc */ 138152344Srodrigc unsigned int i; 139152344Srodrigc const char *fs[] = { 140152778Savatar "cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs", 141171023Srafan "nwfs", "nullfs", "portalfs", "smbfs", "udf", "unionfs", 142152344Srodrigc NULL 143152344Srodrigc }; 144152344Srodrigc 145159473Sjmallett for (i = 0; fs[i] != NULL; ++i) { 146152344Srodrigc if (strcmp(vfstype, fs[i]) == 0) 147159473Sjmallett return (1); 148152344Srodrigc } 149186505Sobrien 150159473Sjmallett return (0); 151152344Srodrigc} 152152344Srodrigc 153152344Srodrigcstatic int 154159473Sjmallettexec_mountprog(const char *name, const char *execname, char *const argv[]) 155152344Srodrigc{ 156152344Srodrigc pid_t pid; 157152344Srodrigc int status; 158152344Srodrigc 159152344Srodrigc switch (pid = fork()) { 160152344Srodrigc case -1: /* Error. */ 161152344Srodrigc warn("fork"); 162152344Srodrigc exit (1); 163152344Srodrigc case 0: /* Child. */ 164152344Srodrigc /* Go find an executable. */ 165152344Srodrigc execvP(execname, _PATH_SYSPATH, argv); 166152344Srodrigc if (errno == ENOENT) { 167152344Srodrigc warn("exec %s not found in %s", execname, 168152344Srodrigc _PATH_SYSPATH); 169152344Srodrigc } 170152344Srodrigc exit(1); 171152344Srodrigc default: /* Parent. */ 172152344Srodrigc if (waitpid(pid, &status, 0) < 0) { 173152344Srodrigc warn("waitpid"); 174152344Srodrigc return (1); 175152344Srodrigc } 176152344Srodrigc 177152344Srodrigc if (WIFEXITED(status)) { 178152344Srodrigc if (WEXITSTATUS(status) != 0) 179152344Srodrigc return (WEXITSTATUS(status)); 180152344Srodrigc } else if (WIFSIGNALED(status)) { 181152344Srodrigc warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 182152344Srodrigc return (1); 183152344Srodrigc } 184152344Srodrigc break; 185152344Srodrigc } 186152344Srodrigc 187152344Srodrigc return (0); 188152344Srodrigc} 189152344Srodrigc 190163672Srustatic int 191163672Sruspecified_ro(const char *arg) 192163671Sru{ 193163671Sru char *optbuf, *opt; 194163671Sru int ret = 0; 195163671Sru 196163671Sru optbuf = strdup(arg); 197163671Sru if (optbuf == NULL) 198163671Sru err(1, NULL); 199163671Sru 200163671Sru for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 201163671Sru if (strcmp(opt, "ro") == 0) { 202163671Sru ret = 1; 203163671Sru break; 204163671Sru } 205163671Sru } 206163671Sru free(optbuf); 207163671Sru return (ret); 208163671Sru} 209163671Sru 210166439Spjdstatic void 211166439Spjdrestart_mountd(void) 212166439Spjd{ 213166439Spjd struct pidfh *pfh; 214186505Sobrien pid_t mountdpid; 215166439Spjd 216166439Spjd pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid); 217166439Spjd if (pfh != NULL) { 218166439Spjd /* Mountd is not running. */ 219166439Spjd pidfile_remove(pfh); 220166439Spjd return; 221166439Spjd } 222166439Spjd if (errno != EEXIST) { 223166439Spjd /* Cannot open pidfile for some reason. */ 224166439Spjd return; 225166439Spjd } 226166439Spjd /* We have mountd(8) PID in mountdpid varible, let's signal it. */ 227166439Spjd if (kill(mountdpid, SIGHUP) == -1) 228166439Spjd err(1, "signal mountd"); 229166439Spjd} 230166439Spjd 2311558Srgrimesint 232151042Srodrigcmain(int argc, char *argv[]) 2331558Srgrimes{ 23423678Speter const char *mntfromname, **vfslist, *vfstype; 2351558Srgrimes struct fstab *fs; 2361558Srgrimes struct statfs *mntbuf; 237163671Sru int all, ch, i, init_flags, late, mntsize, rval, have_fstab, ro; 23895289Smux char *cp, *ep, *options; 2391558Srgrimes 240160303Sdes all = init_flags = late = 0; 241163671Sru ro = 0; 242163671Sru options = NULL; 2431558Srgrimes vfslist = NULL; 2441558Srgrimes vfstype = "ufs"; 245163672Sru while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1) 2461558Srgrimes switch (ch) { 2471558Srgrimes case 'a': 2481558Srgrimes all = 1; 2491558Srgrimes break; 2501558Srgrimes case 'd': 2511558Srgrimes debug = 1; 2521558Srgrimes break; 253113220Smdodd case 'F': 254113220Smdodd setfstab(optarg); 255113220Smdodd break; 2561558Srgrimes case 'f': 2571558Srgrimes init_flags |= MNT_FORCE; 2581558Srgrimes break; 259160303Sdes case 'l': 260160303Sdes late = 1; 261160303Sdes break; 2621558Srgrimes case 'o': 263163671Sru if (*optarg) { 264163671Sru options = catopt(options, optarg); 265163671Sru if (specified_ro(optarg)) 266163671Sru ro = 1; 267163671Sru } 2681558Srgrimes break; 26923805Sbde case 'p': 27023805Sbde fstab_style = 1; 27123805Sbde verbose = 1; 27223805Sbde break; 2731558Srgrimes case 'r': 27476198Sdd options = catopt(options, "ro"); 275163671Sru ro = 1; 2761558Srgrimes break; 2771558Srgrimes case 't': 2781558Srgrimes if (vfslist != NULL) 27937425Scharnier errx(1, "only one -t option may be specified"); 2801558Srgrimes vfslist = makevfslist(optarg); 2811558Srgrimes vfstype = optarg; 2821558Srgrimes break; 2831558Srgrimes case 'u': 2841558Srgrimes init_flags |= MNT_UPDATE; 2851558Srgrimes break; 2861558Srgrimes case 'v': 2871558Srgrimes verbose = 1; 2881558Srgrimes break; 2891558Srgrimes case 'w': 29076198Sdd options = catopt(options, "noro"); 2911558Srgrimes break; 2921558Srgrimes case '?': 2931558Srgrimes default: 2941558Srgrimes usage(); 2951558Srgrimes /* NOTREACHED */ 2961558Srgrimes } 2971558Srgrimes argc -= optind; 2981558Srgrimes argv += optind; 2991558Srgrimes 3001558Srgrimes#define BADTYPE(type) \ 3011558Srgrimes (strcmp(type, FSTAB_RO) && \ 3021558Srgrimes strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 3031558Srgrimes 304163671Sru if ((init_flags & MNT_UPDATE) && (ro == 0)) 305163671Sru options = catopt(options, "noro"); 306186505Sobrien 3071558Srgrimes rval = 0; 3081558Srgrimes switch (argc) { 3091558Srgrimes case 0: 31028671Ssteve if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 31128671Ssteve err(1, "getmntinfo"); 31228671Ssteve if (all) { 3131558Srgrimes while ((fs = getfsent()) != NULL) { 3141558Srgrimes if (BADTYPE(fs->fs_type)) 3151558Srgrimes continue; 31623678Speter if (checkvfsname(fs->fs_vfstype, vfslist)) 3171558Srgrimes continue; 31823678Speter if (hasopt(fs->fs_mntops, "noauto")) 31910288Sdg continue; 320160303Sdes if (hasopt(fs->fs_mntops, "late") && !late) 321160303Sdes continue; 32244811Sbde if (!(init_flags & MNT_UPDATE) && 32344811Sbde ismounted(fs, mntbuf, mntsize)) 32428671Ssteve continue; 325125194Sguido options = update_options(options, fs->fs_mntops, 326125194Sguido mntbuf->f_flags); 3271558Srgrimes if (mountfs(fs->fs_vfstype, fs->fs_spec, 32823805Sbde fs->fs_file, init_flags, options, 32923805Sbde fs->fs_mntops)) 33023805Sbde rval = 1; 3311558Srgrimes } 33228671Ssteve } else if (fstab_style) { 33317243Sjkh for (i = 0; i < mntsize; i++) { 33423678Speter if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 33517243Sjkh continue; 33623805Sbde putfsent(&mntbuf[i]); 33717243Sjkh } 33823805Sbde } else { 3391558Srgrimes for (i = 0; i < mntsize; i++) { 34023805Sbde if (checkvfsname(mntbuf[i].f_fstypename, 34123805Sbde vfslist)) 3421558Srgrimes continue; 34323678Speter prmount(&mntbuf[i]); 3441558Srgrimes } 3451558Srgrimes } 3461558Srgrimes exit(rval); 3471558Srgrimes case 1: 3481558Srgrimes if (vfslist != NULL) 3491558Srgrimes usage(); 3501558Srgrimes 351124201Sanholt rmslashes(*argv, *argv); 3521558Srgrimes if (init_flags & MNT_UPDATE) { 35352678Sgreen mntfromname = NULL; 35452678Sgreen have_fstab = 0; 3551558Srgrimes if ((mntbuf = getmntpt(*argv)) == NULL) 35652678Sgreen errx(1, "not currently mounted %s", *argv); 35752678Sgreen /* 35852678Sgreen * Only get the mntflags from fstab if both mntpoint 35952678Sgreen * and mntspec are identical. Also handle the special 36052678Sgreen * case where just '/' is mounted and 'spec' is not 36152678Sgreen * identical with the one from fstab ('/dev' is missing 36252678Sgreen * in the spec-string at boot-time). 36352678Sgreen */ 36446619Sjkoshy if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 36552678Sgreen if (strcmp(fs->fs_spec, 36652678Sgreen mntbuf->f_mntfromname) == 0 && 36752678Sgreen strcmp(fs->fs_file, 36852678Sgreen mntbuf->f_mntonname) == 0) { 36952678Sgreen have_fstab = 1; 37052678Sgreen mntfromname = mntbuf->f_mntfromname; 37152678Sgreen } else if (argv[0][0] == '/' && 37252678Sgreen argv[0][1] == '\0') { 37352678Sgreen fs = getfsfile("/"); 37452678Sgreen have_fstab = 1; 37552678Sgreen mntfromname = fs->fs_spec; 37652678Sgreen } 37752678Sgreen } 37852678Sgreen if (have_fstab) { 37946619Sjkoshy options = update_options(options, fs->fs_mntops, 38046619Sjkoshy mntbuf->f_flags); 38146619Sjkoshy } else { 38223678Speter mntfromname = mntbuf->f_mntfromname; 38346619Sjkoshy options = update_options(options, NULL, 38446619Sjkoshy mntbuf->f_flags); 38546619Sjkoshy } 38623678Speter rval = mountfs(mntbuf->f_fstypename, mntfromname, 38723678Speter mntbuf->f_mntonname, init_flags, options, 0); 38823678Speter break; 3891558Srgrimes } 39023678Speter if ((fs = getfsfile(*argv)) == NULL && 39123678Speter (fs = getfsspec(*argv)) == NULL) 392102231Strhodes errx(1, "%s: unknown special file or file system", 39323678Speter *argv); 39423678Speter if (BADTYPE(fs->fs_type)) 395102231Strhodes errx(1, "%s has unknown file system type", 39623678Speter *argv); 39723678Speter rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 39823678Speter init_flags, options, fs->fs_mntops); 3991558Srgrimes break; 4001558Srgrimes case 2: 4011558Srgrimes /* 40252678Sgreen * If -t flag has not been specified, the path cannot be 40395289Smux * found, spec contains either a ':' or a '@', then assume 404102231Strhodes * that an NFS file system is being specified ala Sun. 40595289Smux * Check if the hostname contains only allowed characters 40695289Smux * to reduce false positives. IPv6 addresses containing 40795289Smux * ':' will be correctly parsed only if the separator is '@'. 40895289Smux * The definition of a valid hostname is taken from RFC 1034. 4091558Srgrimes */ 410123162Siedowse if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL || 411123162Siedowse (ep = strchr(argv[0], ':')) != NULL)) { 41295318Smux if (*ep == '@') { 41395318Smux cp = ep + 1; 41495318Smux ep = cp + strlen(cp); 41595318Smux } else 41695318Smux cp = argv[0]; 41795289Smux while (cp != ep) { 41895289Smux if (!isdigit(*cp) && !isalpha(*cp) && 41995289Smux *cp != '.' && *cp != '-' && *cp != ':') 42095289Smux break; 42195289Smux cp++; 42295289Smux } 42395289Smux if (cp == ep) 42495289Smux vfstype = "nfs"; 42595289Smux } 4261558Srgrimes rval = mountfs(vfstype, 4271558Srgrimes argv[0], argv[1], init_flags, options, NULL); 4281558Srgrimes break; 4291558Srgrimes default: 4301558Srgrimes usage(); 4311558Srgrimes /* NOTREACHED */ 4321558Srgrimes } 4331558Srgrimes 4341558Srgrimes /* 4351558Srgrimes * If the mount was successfully, and done by root, tell mountd the 436166439Spjd * good news. 4371558Srgrimes */ 438166439Spjd if (rval == 0 && getuid() == 0) 439166439Spjd restart_mountd(); 4401558Srgrimes 4411558Srgrimes exit(rval); 4421558Srgrimes} 4431558Srgrimes 4441558Srgrimesint 445151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize) 44628671Ssteve{ 447154512Spjd char realfsfile[PATH_MAX]; 44828671Ssteve int i; 44928671Ssteve 45028671Ssteve if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 451102231Strhodes /* the root file system can always be remounted */ 45228671Ssteve return (0); 45328671Ssteve 454154512Spjd /* The user may have specified a symlink in fstab, resolve the path */ 455154512Spjd if (realpath(fs->fs_file, realfsfile) == NULL) { 456154512Spjd /* Cannot resolve the path, use original one */ 457154512Spjd strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile)); 458154512Spjd } 459154512Spjd 46028671Ssteve for (i = mntsize - 1; i >= 0; --i) 461154512Spjd if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 && 46228671Ssteve (!isremountable(fs->fs_vfstype) || 46328671Ssteve strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)) 46428671Ssteve return (1); 46528671Ssteve return (0); 46628671Ssteve} 46728671Ssteve 46828671Ssteveint 469151042Srodrigcisremountable(const char *vfsname) 47028671Ssteve{ 47128671Ssteve const char **cp; 47228671Ssteve 47328671Ssteve for (cp = remountable_fs_names; *cp; cp++) 47428671Ssteve if (strcmp(*cp, vfsname) == 0) 47528671Ssteve return (1); 47628671Ssteve return (0); 47728671Ssteve} 47828671Ssteve 47928671Ssteveint 480151042Srodrigchasopt(const char *mntopts, const char *option) 48123678Speter{ 48223678Speter int negative, found; 48323678Speter char *opt, *optbuf; 48423678Speter 48523678Speter if (option[0] == 'n' && option[1] == 'o') { 48623678Speter negative = 1; 48723678Speter option += 2; 48823678Speter } else 48923678Speter negative = 0; 49023678Speter optbuf = strdup(mntopts); 49123678Speter found = 0; 49223678Speter for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 49323678Speter if (opt[0] == 'n' && opt[1] == 'o') { 49423678Speter if (!strcasecmp(opt + 2, option)) 49523678Speter found = negative; 49623678Speter } else if (!strcasecmp(opt, option)) 49723678Speter found = !negative; 49823678Speter } 49923678Speter free(optbuf); 50023678Speter return (found); 50123678Speter} 50223678Speter 503187035Sobrienstatic void 504187035Sobrienappend_arg(struct cpa *sa, char *arg) 505187035Sobrien{ 506187093Sobrien static int a_sz; 507187035Sobrien 508187093Sobrien if (sa->c + 1 == a_sz) { 509187093Sobrien a_sz = a_sz == 0 ? 8 : a_sz * 2; 510187093Sobrien sa->a = realloc(sa->a, sizeof(sa->a) * a_sz); 511187093Sobrien if (sa->a == NULL) 512187093Sobrien errx(1, "realloc failed"); 513187093Sobrien } 514187035Sobrien sa->a[++sa->c] = arg; 515187035Sobrien} 516187035Sobrien 51723678Speterint 518151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags, 519151042Srodrigc const char *options, const char *mntopts) 5201558Srgrimes{ 521187035Sobrien struct cpa mnt_argv; 5221558Srgrimes struct statfs sf; 523186504Sobrien int i, ret; 524118580Simp char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; 525187093Sobrien static int mnt_argv_inited; 5261558Srgrimes 52752055Sphk /* resolve the mountpoint with realpath(3) */ 52852055Sphk (void)checkpath(name, mntpath); 5291558Srgrimes name = mntpath; 5301558Srgrimes 53123678Speter if (mntopts == NULL) 53223678Speter mntopts = ""; 5331558Srgrimes optbuf = catopt(strdup(mntopts), options); 5341558Srgrimes 5351558Srgrimes if (strcmp(name, "/") == 0) 5361558Srgrimes flags |= MNT_UPDATE; 5371558Srgrimes if (flags & MNT_FORCE) 5381558Srgrimes optbuf = catopt(optbuf, "force"); 5391558Srgrimes if (flags & MNT_RDONLY) 5401558Srgrimes optbuf = catopt(optbuf, "ro"); 5411558Srgrimes /* 5421558Srgrimes * XXX 5431558Srgrimes * The mount_mfs (newfs) command uses -o to select the 54437425Scharnier * optimization mode. We don't pass the default "-o rw" 5451558Srgrimes * for that reason. 5461558Srgrimes */ 5471558Srgrimes if (flags & MNT_UPDATE) 5481558Srgrimes optbuf = catopt(optbuf, "update"); 5491558Srgrimes 55077577Sru /* Compatibility glue. */ 551166526Srodrigc if (strcmp(vfstype, "msdos") == 0) { 552166526Srodrigc warnx( 553166526Srodrigc "Using \"-t msdosfs\", since \"-t msdos\" is deprecated."); 55477577Sru vfstype = "msdosfs"; 555166526Srodrigc } 55677577Sru 557144133Scperciva /* Construct the name of the appropriate mount command */ 558144133Scperciva (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 559144133Scperciva 560187093Sobrien if (!mnt_argv_inited) { 561187093Sobrien mnt_argv_inited++; 562187093Sobrien mnt_argv.a = NULL; 563187093Sobrien } 564187035Sobrien mnt_argv.c = -1; 565187035Sobrien append_arg(&mnt_argv, execname); 566187035Sobrien mangle(optbuf, &mnt_argv); 567187035Sobrien append_arg(&mnt_argv, strdup(spec)); 568187035Sobrien append_arg(&mnt_argv, strdup(name)); 569187035Sobrien append_arg(&mnt_argv, NULL); 5701558Srgrimes 5711558Srgrimes if (debug) { 572164266Srodrigc if (use_mountprog(vfstype)) 573164266Srodrigc printf("exec: mount_%s", vfstype); 574164266Srodrigc else 575164266Srodrigc printf("mount -t %s", vfstype); 576187035Sobrien for (i = 1; i < mnt_argv.c; i++) 577187035Sobrien (void)printf(" %s", mnt_argv.a[i]); 5781558Srgrimes (void)printf("\n"); 5791558Srgrimes return (0); 5801558Srgrimes } 5811558Srgrimes 582152668Srodrigc if (use_mountprog(vfstype)) { 583187035Sobrien ret = exec_mountprog(name, execname, mnt_argv.a); 584152344Srodrigc } else { 585187035Sobrien ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a); 586152344Srodrigc } 5871558Srgrimes 588152344Srodrigc free(optbuf); 5891558Srgrimes 590152344Srodrigc if (verbose) { 591152344Srodrigc if (statfs(name, &sf) < 0) { 592152344Srodrigc warn("statfs %s", name); 5931558Srgrimes return (1); 5941558Srgrimes } 595152344Srodrigc if (fstab_style) 596152344Srodrigc putfsent(&sf); 597152344Srodrigc else 598152344Srodrigc prmount(&sf); 5991558Srgrimes } 6001558Srgrimes 601182570Smatteo return (ret); 6021558Srgrimes} 6031558Srgrimes 6041558Srgrimesvoid 605151042Srodrigcprmount(struct statfs *sfp) 60623678Speter{ 607151043Srodrigc int flags; 608151043Srodrigc unsigned int i; 6091558Srgrimes struct opt *o; 61023678Speter struct passwd *pw; 6111558Srgrimes 61252036Sn_hibma (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 61352036Sn_hibma sfp->f_fstypename); 6141558Srgrimes 61523678Speter flags = sfp->f_flags & MNT_VISFLAGMASK; 61652036Sn_hibma for (o = optnames; flags && o->o_opt; o++) 6171558Srgrimes if (flags & o->o_opt) { 61852036Sn_hibma (void)printf(", %s", o->o_name); 6191558Srgrimes flags &= ~o->o_opt; 6201558Srgrimes } 621125339Spjd /* 622125339Spjd * Inform when file system is mounted by an unprivileged user 623125339Spjd * or privileged non-root user. 624125339Spjd */ 625125365Snectar if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 62652036Sn_hibma (void)printf(", mounted by "); 62723678Speter if ((pw = getpwuid(sfp->f_owner)) != NULL) 62823678Speter (void)printf("%s", pw->pw_name); 62923678Speter else 63023678Speter (void)printf("%d", sfp->f_owner); 63123678Speter } 63265023Ssheldonh if (verbose) { 63365023Ssheldonh if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 634123242Siedowse (void)printf(", writes: sync %ju async %ju", 635123242Siedowse (uintmax_t)sfp->f_syncwrites, 636123242Siedowse (uintmax_t)sfp->f_asyncwrites); 63765023Ssheldonh if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 638123242Siedowse (void)printf(", reads: sync %ju async %ju", 639123242Siedowse (uintmax_t)sfp->f_syncreads, 640123242Siedowse (uintmax_t)sfp->f_asyncreads); 641123162Siedowse if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 642123162Siedowse printf(", fsid "); 643123162Siedowse for (i = 0; i < sizeof(sfp->f_fsid); i++) 644123162Siedowse printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 645123162Siedowse } 64665023Ssheldonh } 64752036Sn_hibma (void)printf(")\n"); 6481558Srgrimes} 6491558Srgrimes 6501558Srgrimesstruct statfs * 651151042Srodrigcgetmntpt(const char *name) 6521558Srgrimes{ 6531558Srgrimes struct statfs *mntbuf; 6541558Srgrimes int i, mntsize; 6551558Srgrimes 6561558Srgrimes mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 65752678Sgreen for (i = mntsize - 1; i >= 0; i--) { 6581558Srgrimes if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 6591558Srgrimes strcmp(mntbuf[i].f_mntonname, name) == 0) 6601558Srgrimes return (&mntbuf[i]); 66152678Sgreen } 6621558Srgrimes return (NULL); 6631558Srgrimes} 6641558Srgrimes 6651558Srgrimeschar * 666151042Srodrigccatopt(char *s0, const char *s1) 6671558Srgrimes{ 6681558Srgrimes size_t i; 6691558Srgrimes char *cp; 6701558Srgrimes 67146619Sjkoshy if (s1 == NULL || *s1 == '\0') 672159473Sjmallett return (s0); 67346619Sjkoshy 6741558Srgrimes if (s0 && *s0) { 6751558Srgrimes i = strlen(s0) + strlen(s1) + 1 + 1; 6761558Srgrimes if ((cp = malloc(i)) == NULL) 67737425Scharnier errx(1, "malloc failed"); 6781558Srgrimes (void)snprintf(cp, i, "%s,%s", s0, s1); 6791558Srgrimes } else 6801558Srgrimes cp = strdup(s1); 6811558Srgrimes 6821558Srgrimes if (s0) 6831558Srgrimes free(s0); 6841558Srgrimes return (cp); 6851558Srgrimes} 6861558Srgrimes 6871558Srgrimesvoid 688187035Sobrienmangle(char *options, struct cpa *a) 6891558Srgrimes{ 6901558Srgrimes char *p, *s; 6911558Srgrimes 6921558Srgrimes for (s = options; (p = strsep(&s, ",")) != NULL;) 69346619Sjkoshy if (*p != '\0') { 694152465Srodrigc if (strcmp(p, "noauto") == 0) { 695152465Srodrigc /* 696152465Srodrigc * Do not pass noauto option to nmount(). 697152465Srodrigc * or external mount program. noauto is 698152465Srodrigc * only used to prevent mounting a filesystem 699152465Srodrigc * when 'mount -a' is specified, and is 700152465Srodrigc * not a real mount option. 701152465Srodrigc */ 702152465Srodrigc continue; 703160303Sdes } else if (strcmp(p, "late") == 0) { 704160303Sdes /* 705160303Sdes * "late" is used to prevent certain file 706160303Sdes * systems from being mounted before late 707160303Sdes * in the boot cycle; for instance, 708160303Sdes * loopback NFS mounts can't be mounted 709160303Sdes * before mountd starts. 710160303Sdes */ 711160303Sdes continue; 712152732Srodrigc } else if (strcmp(p, "userquota") == 0) { 713152732Srodrigc continue; 714158400Smaxim } else if (strncmp(p, userquotaeq, 715158400Smaxim sizeof(userquotaeq) - 1) == 0) { 716158400Smaxim continue; 717152732Srodrigc } else if (strcmp(p, "groupquota") == 0) { 718152732Srodrigc continue; 719158400Smaxim } else if (strncmp(p, groupquotaeq, 720158400Smaxim sizeof(groupquotaeq) - 1) == 0) { 721158400Smaxim continue; 722152465Srodrigc } else if (*p == '-') { 723187035Sobrien append_arg(a, p); 7241558Srgrimes p = strchr(p, '='); 725123268Strhodes if (p != NULL) { 7261558Srgrimes *p = '\0'; 727187035Sobrien append_arg(a, p + 1); 7281558Srgrimes } 729153038Srodrigc } else { 730187035Sobrien append_arg(a, strdup("-o")); 731187035Sobrien append_arg(a, p); 7321558Srgrimes } 73346619Sjkoshy } 7341558Srgrimes} 7351558Srgrimes 73646619Sjkoshy 73746619Sjkoshychar * 738159473Sjmallettupdate_options(char *opts, char *fstab, int curflags) 73946619Sjkoshy{ 74046619Sjkoshy char *o, *p; 74146619Sjkoshy char *cur; 74246619Sjkoshy char *expopt, *newopt, *tmpopt; 74346619Sjkoshy 74446619Sjkoshy if (opts == NULL) 745159473Sjmallett return (strdup("")); 74646619Sjkoshy 74746619Sjkoshy /* remove meta options from list */ 74846619Sjkoshy remopt(fstab, MOUNT_META_OPTION_FSTAB); 74946619Sjkoshy remopt(fstab, MOUNT_META_OPTION_CURRENT); 75046619Sjkoshy cur = flags2opts(curflags); 75146619Sjkoshy 75246619Sjkoshy /* 75346619Sjkoshy * Expand all meta-options passed to us first. 75446619Sjkoshy */ 75546619Sjkoshy expopt = NULL; 75646619Sjkoshy for (p = opts; (o = strsep(&p, ",")) != NULL;) { 75746619Sjkoshy if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 75846619Sjkoshy expopt = catopt(expopt, fstab); 75946619Sjkoshy else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 76046619Sjkoshy expopt = catopt(expopt, cur); 76146619Sjkoshy else 76246619Sjkoshy expopt = catopt(expopt, o); 76346619Sjkoshy } 76446619Sjkoshy free(cur); 76546619Sjkoshy free(opts); 76646619Sjkoshy 76746619Sjkoshy /* 76846619Sjkoshy * Remove previous contradictory arguments. Given option "foo" we 76946619Sjkoshy * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 77046619Sjkoshy * and "foo" - so we can deal with possible options like "notice". 77146619Sjkoshy */ 77246619Sjkoshy newopt = NULL; 77346619Sjkoshy for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 77446619Sjkoshy if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 77546619Sjkoshy errx(1, "malloc failed"); 776125933Sgrog 77746619Sjkoshy strcpy(tmpopt, "no"); 77846619Sjkoshy strcat(tmpopt, o); 77946619Sjkoshy remopt(newopt, tmpopt); 78046619Sjkoshy free(tmpopt); 78146619Sjkoshy 78246619Sjkoshy if (strncmp("no", o, 2) == 0) 78346619Sjkoshy remopt(newopt, o+2); 78446619Sjkoshy 78546619Sjkoshy newopt = catopt(newopt, o); 78646619Sjkoshy } 78746619Sjkoshy free(expopt); 78846619Sjkoshy 789159473Sjmallett return (newopt); 79046619Sjkoshy} 79146619Sjkoshy 7921558Srgrimesvoid 793159473Sjmallettremopt(char *string, const char *opt) 79446619Sjkoshy{ 79546619Sjkoshy char *o, *p, *r; 79646619Sjkoshy 79746619Sjkoshy if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 79846619Sjkoshy return; 79946619Sjkoshy 80046619Sjkoshy r = string; 80146619Sjkoshy 80246619Sjkoshy for (p = string; (o = strsep(&p, ",")) != NULL;) { 80346619Sjkoshy if (strcmp(opt, o) != 0) { 80446619Sjkoshy if (*r == ',' && *o != '\0') 80546619Sjkoshy r++; 80646619Sjkoshy while ((*r++ = *o++) != '\0') 80746619Sjkoshy ; 80846619Sjkoshy *--r = ','; 80946619Sjkoshy } 81046619Sjkoshy } 81146619Sjkoshy *r = '\0'; 81246619Sjkoshy} 81346619Sjkoshy 81446619Sjkoshyvoid 815159473Sjmallettusage(void) 8161558Srgrimes{ 8171558Srgrimes 81837425Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 819160303Sdes"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 820141611Sru" mount [-dfpruvw] special | node", 821141611Sru" mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 8221558Srgrimes exit(1); 8231558Srgrimes} 82417243Sjkh 82517243Sjkhvoid 826175949Smatteoputfsent(struct statfs *ent) 82717243Sjkh{ 82823805Sbde struct fstab *fst; 82946619Sjkoshy char *opts; 830168698Sphk int l; 831125933Sgrog 83246619Sjkoshy opts = flags2opts(ent->f_flags); 833156229Skeramida 834175949Smatteo if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 || 835175949Smatteo strncmp(ent->f_mntfromname, "<above>", 7) == 0) { 836175949Smatteo strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8) 837175949Smatteo +1)); 838175949Smatteo } 839175949Smatteo 840156229Skeramida /* 841156229Skeramida * "rw" is not a real mount option; this is why we print NULL as "rw" 842156229Skeramida * if opts is still NULL here. 843156229Skeramida */ 844168698Sphk l = strlen(ent->f_mntfromname); 845168698Sphk printf("%s%s%s%s", ent->f_mntfromname, 846168698Sphk l < 8 ? "\t" : "", 847168698Sphk l < 16 ? "\t" : "", 848168698Sphk l < 24 ? "\t" : " "); 849168698Sphk l = strlen(ent->f_mntonname); 850168698Sphk printf("%s%s%s%s", ent->f_mntonname, 851168698Sphk l < 8 ? "\t" : "", 852168698Sphk l < 16 ? "\t" : "", 853168698Sphk l < 24 ? "\t" : " "); 854168698Sphk printf("%s\t", ent->f_fstypename); 855168698Sphk if (opts == NULL) { 856168698Sphk printf("%s\t", "rw"); 857168698Sphk } else { 858168698Sphk l = strlen(opts); 859168698Sphk printf("%s%s", opts, 860168698Sphk l < 8 ? "\t" : " "); 861168698Sphk } 86246619Sjkoshy free(opts); 86317243Sjkh 86428671Ssteve if ((fst = getfsspec(ent->f_mntfromname))) 86523805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 86628671Ssteve else if ((fst = getfsfile(ent->f_mntonname))) 86723805Sbde printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 86856038Sgreen else if (strcmp(ent->f_fstypename, "ufs") == 0) { 86956038Sgreen if (strcmp(ent->f_mntonname, "/") == 0) 87056038Sgreen printf("\t1 1\n"); 87156038Sgreen else 87256038Sgreen printf("\t2 2\n"); 87356038Sgreen } else 87423805Sbde printf("\t0 0\n"); 87517243Sjkh} 87646619Sjkoshy 87746619Sjkoshy 87846619Sjkoshychar * 879159473Sjmallettflags2opts(int flags) 88046619Sjkoshy{ 88146619Sjkoshy char *res; 88246619Sjkoshy 88346619Sjkoshy res = NULL; 88446619Sjkoshy 885155997Srodrigc if (flags & MNT_RDONLY) res = catopt(res, "ro"); 88646619Sjkoshy if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 88746619Sjkoshy if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 88846619Sjkoshy if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 88946619Sjkoshy if (flags & MNT_UNION) res = catopt(res, "union"); 89046619Sjkoshy if (flags & MNT_ASYNC) res = catopt(res, "async"); 89146619Sjkoshy if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 89246619Sjkoshy if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 89346619Sjkoshy if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 89446619Sjkoshy if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 89546619Sjkoshy if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 896101205Srwatson if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 897105118Srwatson if (flags & MNT_ACLS) res = catopt(res, "acls"); 89846619Sjkoshy 899159473Sjmallett return (res); 90046619Sjkoshy} 901