1fba1c154SSteve Price /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 48fae3551SRodney W. Grimes * Copyright (c) 1980, 1989, 1993, 1994 58fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 68fae3551SRodney W. Grimes * 78fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 88fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 98fae3551SRodney W. Grimes * are met: 108fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 118fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 128fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 138fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 148fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 168fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 178fae3551SRodney W. Grimes * without specific prior written permission. 188fae3551SRodney W. Grimes * 198fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 208fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 218fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 228fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 238fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 248fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 258fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 268fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 278fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 288fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 298fae3551SRodney W. Grimes * SUCH DAMAGE. 308fae3551SRodney W. Grimes */ 318fae3551SRodney W. Grimes 328fae3551SRodney W. Grimes #ifndef lint 33fba1c154SSteve Price static const char copyright[] = 348fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\ 358fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 36fba1c154SSteve Price #if 0 37c06fe0a0SPeter Wemm static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95"; 38fba1c154SSteve Price #endif 398fae3551SRodney W. Grimes #endif /* not lint */ 408fae3551SRodney W. Grimes 4100356118SDavid E. O'Brien #include <sys/cdefs.h> 4200356118SDavid E. O'Brien __FBSDID("$FreeBSD$"); 4300356118SDavid E. O'Brien 448fae3551SRodney W. Grimes #include <sys/param.h> 458fae3551SRodney W. Grimes #include <sys/mount.h> 468a978495SDavid Greenman #include <sys/stat.h> 4774cf460bSBruce Evans #include <sys/wait.h> 488fae3551SRodney W. Grimes 49003dbca6SMaxime Henrion #include <ctype.h> 508fae3551SRodney W. Grimes #include <err.h> 518fae3551SRodney W. Grimes #include <errno.h> 528fae3551SRodney W. Grimes #include <fstab.h> 53a3ba4c65SGordon Tetlow #include <paths.h> 54c06fe0a0SPeter Wemm #include <pwd.h> 558fae3551SRodney W. Grimes #include <signal.h> 5696c65ccbSIan Dowse #include <stdint.h> 578fae3551SRodney W. Grimes #include <stdio.h> 588fae3551SRodney W. Grimes #include <stdlib.h> 598fae3551SRodney W. Grimes #include <string.h> 608fae3551SRodney W. Grimes #include <unistd.h> 61e9988cedSPawel Jakub Dawidek #include <libutil.h> 628fae3551SRodney W. Grimes 63fba1c154SSteve Price #include "extern.h" 6473dd3167SPoul-Henning Kamp #include "mntopts.h" 658fae3551SRodney W. Grimes #include "pathnames.h" 668fae3551SRodney W. Grimes 6718af6044SJoseph Koshy /* `meta' options */ 6818af6044SJoseph Koshy #define MOUNT_META_OPTION_FSTAB "fstab" 6918af6044SJoseph Koshy #define MOUNT_META_OPTION_CURRENT "current" 7018af6044SJoseph Koshy 711efe3c6bSEd Schouten static int debug, fstab_style, verbose; 72a257a45eSJordan K. Hubbard 73a86de995SDavid E. O'Brien struct cpa { 7400356118SDavid E. O'Brien char **a; 750e969ed7SDavid E. O'Brien ssize_t sz; 76a86de995SDavid E. O'Brien int c; 77a86de995SDavid E. O'Brien }; 78a86de995SDavid E. O'Brien 7985429990SWarner Losh char *catopt(char *, const char *); 8085429990SWarner Losh struct statfs *getmntpt(const char *); 8185429990SWarner Losh int hasopt(const char *, const char *); 8285429990SWarner Losh int ismounted(struct fstab *, struct statfs *, int); 8385429990SWarner Losh int isremountable(const char *); 84a86de995SDavid E. O'Brien void mangle(char *, struct cpa *); 8585429990SWarner Losh char *update_options(char *, char *, int); 8685429990SWarner Losh int mountfs(const char *, const char *, const char *, 8785429990SWarner Losh int, const char *, const char *); 8885429990SWarner Losh void remopt(char *, const char *); 8985429990SWarner Losh void prmount(struct statfs *); 90031ea52fSMatteo Riondato void putfsent(struct statfs *); 9185429990SWarner Losh void usage(void); 9285429990SWarner Losh char *flags2opts(int); 938fae3551SRodney W. Grimes 94bcb1d846SPhilippe Charnier /* Map from mount options to printable formats. */ 958fae3551SRodney W. Grimes static struct opt { 969df3a164SBjoern A. Zeeb uint64_t o_opt; 978fae3551SRodney W. Grimes const char *o_name; 988fae3551SRodney W. Grimes } optnames[] = { 998fae3551SRodney W. Grimes { MNT_ASYNC, "asynchronous" }, 1008fae3551SRodney W. Grimes { MNT_EXPORTED, "NFS exported" }, 1018fae3551SRodney W. Grimes { MNT_LOCAL, "local" }, 10255e50aceSDavid Greenman { MNT_NOATIME, "noatime" }, 1038fae3551SRodney W. Grimes { MNT_NOEXEC, "noexec" }, 1048fae3551SRodney W. Grimes { MNT_NOSUID, "nosuid" }, 1055ddc8dedSWolfram Schneider { MNT_NOSYMFOLLOW, "nosymfollow" }, 1068fae3551SRodney W. Grimes { MNT_QUOTA, "with quotas" }, 1078fae3551SRodney W. Grimes { MNT_RDONLY, "read-only" }, 1088fae3551SRodney W. Grimes { MNT_SYNCHRONOUS, "synchronous" }, 1098fae3551SRodney W. Grimes { MNT_UNION, "union" }, 11075b714acSKATO Takenori { MNT_NOCLUSTERR, "noclusterr" }, 11175b714acSKATO Takenori { MNT_NOCLUSTERW, "noclusterw" }, 11252bf64c7SJulian Elischer { MNT_SUIDDIR, "suiddir" }, 113b1897c19SJulian Elischer { MNT_SOFTDEP, "soft-updates" }, 114d716efa9SKirk McKusick { MNT_SUJ, "journaled soft-updates" }, 115ba0fbe96SRobert Watson { MNT_MULTILABEL, "multilabel" }, 11603d94b50SRobert Watson { MNT_ACLS, "acls" }, 1179340fc72SEdward Tomasz Napierala { MNT_NFS4ACLS, "nfsv4acls" }, 118ac88569cSPawel Jakub Dawidek { MNT_GJOURNAL, "gjournal" }, 1193914ddf8SEdward Tomasz Napierala { MNT_AUTOMOUNTED, "automounted" }, 1209a81ba0fSStephen J. Kiernan { MNT_VERIFIED, "verified" }, 12118af6044SJoseph Koshy { 0, NULL } 1228fae3551SRodney W. Grimes }; 1238fae3551SRodney W. Grimes 124fba1c154SSteve Price /* 125fba1c154SSteve Price * List of VFS types that can be remounted without becoming mounted on top 126fba1c154SSteve Price * of each other. 127fba1c154SSteve Price * XXX Is this list correct? 128fba1c154SSteve Price */ 129fba1c154SSteve Price static const char * 130fba1c154SSteve Price remountable_fs_names[] = { 1315a4420e3SAlexey Zelkin "ufs", "ffs", "ext2fs", 132fba1c154SSteve Price 0 133fba1c154SSteve Price }; 134fba1c154SSteve Price 1356e74fb9dSMaxim Konovalov static const char userquotaeq[] = "userquota="; 1366e74fb9dSMaxim Konovalov static const char groupquotaeq[] = "groupquota="; 1376e74fb9dSMaxim Konovalov 138c7835769SCraig Rodrigues static char *mountprog = NULL; 139c7835769SCraig Rodrigues 1406f5f1a6bSCraig Rodrigues static int 1416f5f1a6bSCraig Rodrigues use_mountprog(const char *vfstype) 1426f5f1a6bSCraig Rodrigues { 1436f5f1a6bSCraig Rodrigues /* XXX: We need to get away from implementing external mount 1446f5f1a6bSCraig Rodrigues * programs for every filesystem, and move towards having 1456f5f1a6bSCraig Rodrigues * each filesystem properly implement the nmount() system call. 1466f5f1a6bSCraig Rodrigues */ 1476f5f1a6bSCraig Rodrigues unsigned int i; 1486f5f1a6bSCraig Rodrigues const char *fs[] = { 149a42ac676SAttilio Rao "cd9660", "mfs", "msdosfs", "nfs", 1504e25c86fSEdward Tomasz Napierala "nullfs", "smbfs", "udf", "unionfs", 1516f5f1a6bSCraig Rodrigues NULL 1526f5f1a6bSCraig Rodrigues }; 1536f5f1a6bSCraig Rodrigues 154c7835769SCraig Rodrigues if (mountprog != NULL) 155c7835769SCraig Rodrigues return (1); 156c7835769SCraig Rodrigues 1576f5f1a6bSCraig Rodrigues for (i = 0; fs[i] != NULL; ++i) { 1586f5f1a6bSCraig Rodrigues if (strcmp(vfstype, fs[i]) == 0) 1594796c6ccSJuli Mallett return (1); 1606f5f1a6bSCraig Rodrigues } 1616f5f1a6bSCraig Rodrigues 1624796c6ccSJuli Mallett return (0); 1636f5f1a6bSCraig Rodrigues } 1646f5f1a6bSCraig Rodrigues 1656f5f1a6bSCraig Rodrigues static int 1664796c6ccSJuli Mallett exec_mountprog(const char *name, const char *execname, char *const argv[]) 1676f5f1a6bSCraig Rodrigues { 1686f5f1a6bSCraig Rodrigues pid_t pid; 1696f5f1a6bSCraig Rodrigues int status; 1706f5f1a6bSCraig Rodrigues 1716f5f1a6bSCraig Rodrigues switch (pid = fork()) { 1726f5f1a6bSCraig Rodrigues case -1: /* Error. */ 1736f5f1a6bSCraig Rodrigues warn("fork"); 1746f5f1a6bSCraig Rodrigues exit (1); 1756f5f1a6bSCraig Rodrigues case 0: /* Child. */ 1766f5f1a6bSCraig Rodrigues /* Go find an executable. */ 1776f5f1a6bSCraig Rodrigues execvP(execname, _PATH_SYSPATH, argv); 1786f5f1a6bSCraig Rodrigues if (errno == ENOENT) { 179c7835769SCraig Rodrigues warn("exec %s not found", execname); 180c7835769SCraig Rodrigues if (execname[0] != '/') { 181c7835769SCraig Rodrigues warnx("in path: %s", _PATH_SYSPATH); 182c7835769SCraig Rodrigues } 1836f5f1a6bSCraig Rodrigues } 1846f5f1a6bSCraig Rodrigues exit(1); 1856f5f1a6bSCraig Rodrigues default: /* Parent. */ 1866f5f1a6bSCraig Rodrigues if (waitpid(pid, &status, 0) < 0) { 1876f5f1a6bSCraig Rodrigues warn("waitpid"); 1886f5f1a6bSCraig Rodrigues return (1); 1896f5f1a6bSCraig Rodrigues } 1906f5f1a6bSCraig Rodrigues 1916f5f1a6bSCraig Rodrigues if (WIFEXITED(status)) { 1926f5f1a6bSCraig Rodrigues if (WEXITSTATUS(status) != 0) 1936f5f1a6bSCraig Rodrigues return (WEXITSTATUS(status)); 1946f5f1a6bSCraig Rodrigues } else if (WIFSIGNALED(status)) { 1956f5f1a6bSCraig Rodrigues warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 1966f5f1a6bSCraig Rodrigues return (1); 1976f5f1a6bSCraig Rodrigues } 1986f5f1a6bSCraig Rodrigues break; 1996f5f1a6bSCraig Rodrigues } 2006f5f1a6bSCraig Rodrigues 2016f5f1a6bSCraig Rodrigues return (0); 2026f5f1a6bSCraig Rodrigues } 2036f5f1a6bSCraig Rodrigues 20449a41c4fSRuslan Ermilov static int 20549a41c4fSRuslan Ermilov specified_ro(const char *arg) 2063cbf527eSRuslan Ermilov { 2073cbf527eSRuslan Ermilov char *optbuf, *opt; 2083cbf527eSRuslan Ermilov int ret = 0; 2093cbf527eSRuslan Ermilov 2103cbf527eSRuslan Ermilov optbuf = strdup(arg); 2113cbf527eSRuslan Ermilov if (optbuf == NULL) 2123cbf527eSRuslan Ermilov err(1, NULL); 2133cbf527eSRuslan Ermilov 2143cbf527eSRuslan Ermilov for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 2153cbf527eSRuslan Ermilov if (strcmp(opt, "ro") == 0) { 2163cbf527eSRuslan Ermilov ret = 1; 2173cbf527eSRuslan Ermilov break; 2183cbf527eSRuslan Ermilov } 2193cbf527eSRuslan Ermilov } 2203cbf527eSRuslan Ermilov free(optbuf); 2213cbf527eSRuslan Ermilov return (ret); 2223cbf527eSRuslan Ermilov } 2233cbf527eSRuslan Ermilov 224e9988cedSPawel Jakub Dawidek static void 225e9988cedSPawel Jakub Dawidek restart_mountd(void) 226e9988cedSPawel Jakub Dawidek { 227e9988cedSPawel Jakub Dawidek struct pidfh *pfh; 228e9988cedSPawel Jakub Dawidek pid_t mountdpid; 229e9988cedSPawel Jakub Dawidek 230e9988cedSPawel Jakub Dawidek pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid); 231e9988cedSPawel Jakub Dawidek if (pfh != NULL) { 232e9988cedSPawel Jakub Dawidek /* Mountd is not running. */ 233e9988cedSPawel Jakub Dawidek pidfile_remove(pfh); 234e9988cedSPawel Jakub Dawidek return; 235e9988cedSPawel Jakub Dawidek } 236e9988cedSPawel Jakub Dawidek if (errno != EEXIST) { 237e9988cedSPawel Jakub Dawidek /* Cannot open pidfile for some reason. */ 238e9988cedSPawel Jakub Dawidek return; 239e9988cedSPawel Jakub Dawidek } 240e9988cedSPawel Jakub Dawidek /* We have mountd(8) PID in mountdpid varible, let's signal it. */ 241e9988cedSPawel Jakub Dawidek if (kill(mountdpid, SIGHUP) == -1) 242e9988cedSPawel Jakub Dawidek err(1, "signal mountd"); 243e9988cedSPawel Jakub Dawidek } 244e9988cedSPawel Jakub Dawidek 2458fae3551SRodney W. Grimes int 246e24dc56aSCraig Rodrigues main(int argc, char *argv[]) 2478fae3551SRodney W. Grimes { 248c06fe0a0SPeter Wemm const char *mntfromname, **vfslist, *vfstype; 2498fae3551SRodney W. Grimes struct fstab *fs; 2508fae3551SRodney W. Grimes struct statfs *mntbuf; 251c7383075SXin LI int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro; 252b48b774fSChris Rees int onlylate; 253003dbca6SMaxime Henrion char *cp, *ep, *options; 2548fae3551SRodney W. Grimes 255b48b774fSChris Rees all = init_flags = late = onlylate = 0; 2563cbf527eSRuslan Ermilov ro = 0; 2573cbf527eSRuslan Ermilov options = NULL; 2588fae3551SRodney W. Grimes vfslist = NULL; 2598fae3551SRodney W. Grimes vfstype = "ufs"; 260fc98db27SRobert Millan while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1) 2618fae3551SRodney W. Grimes switch (ch) { 2628fae3551SRodney W. Grimes case 'a': 2638fae3551SRodney W. Grimes all = 1; 2648fae3551SRodney W. Grimes break; 2658fae3551SRodney W. Grimes case 'd': 2668fae3551SRodney W. Grimes debug = 1; 2678fae3551SRodney W. Grimes break; 268ef258dd9SMatthew N. Dodd case 'F': 269ef258dd9SMatthew N. Dodd setfstab(optarg); 270ef258dd9SMatthew N. Dodd break; 2718fae3551SRodney W. Grimes case 'f': 2728fae3551SRodney W. Grimes init_flags |= MNT_FORCE; 2738fae3551SRodney W. Grimes break; 274b48b774fSChris Rees case 'L': 275b48b774fSChris Rees onlylate = 1; 276b48b774fSChris Rees late = 1; 277b48b774fSChris Rees break; 2784b4f9170SDag-Erling Smørgrav case 'l': 2794b4f9170SDag-Erling Smørgrav late = 1; 2804b4f9170SDag-Erling Smørgrav break; 281fc98db27SRobert Millan case 'n': 282fc98db27SRobert Millan /* For compatibility with the Linux version of mount. */ 283fc98db27SRobert Millan break; 2848fae3551SRodney W. Grimes case 'o': 2853cbf527eSRuslan Ermilov if (*optarg) { 2868fae3551SRodney W. Grimes options = catopt(options, optarg); 2873cbf527eSRuslan Ermilov if (specified_ro(optarg)) 2883cbf527eSRuslan Ermilov ro = 1; 2893cbf527eSRuslan Ermilov } 2908fae3551SRodney W. Grimes break; 29174cf460bSBruce Evans case 'p': 29274cf460bSBruce Evans fstab_style = 1; 29374cf460bSBruce Evans verbose = 1; 29474cf460bSBruce Evans break; 2958fae3551SRodney W. Grimes case 'r': 296ad044771SDima Dorfman options = catopt(options, "ro"); 2973cbf527eSRuslan Ermilov ro = 1; 2988fae3551SRodney W. Grimes break; 2998fae3551SRodney W. Grimes case 't': 3008fae3551SRodney W. Grimes if (vfslist != NULL) 301bcb1d846SPhilippe Charnier errx(1, "only one -t option may be specified"); 3028fae3551SRodney W. Grimes vfslist = makevfslist(optarg); 3038fae3551SRodney W. Grimes vfstype = optarg; 3048fae3551SRodney W. Grimes break; 3058fae3551SRodney W. Grimes case 'u': 3068fae3551SRodney W. Grimes init_flags |= MNT_UPDATE; 3078fae3551SRodney W. Grimes break; 3088fae3551SRodney W. Grimes case 'v': 3098fae3551SRodney W. Grimes verbose = 1; 3108fae3551SRodney W. Grimes break; 3118fae3551SRodney W. Grimes case 'w': 312ad044771SDima Dorfman options = catopt(options, "noro"); 3138fae3551SRodney W. Grimes break; 3148fae3551SRodney W. Grimes case '?': 3158fae3551SRodney W. Grimes default: 3168fae3551SRodney W. Grimes usage(); 3178fae3551SRodney W. Grimes /* NOTREACHED */ 3188fae3551SRodney W. Grimes } 3198fae3551SRodney W. Grimes argc -= optind; 3208fae3551SRodney W. Grimes argv += optind; 3218fae3551SRodney W. Grimes 3228fae3551SRodney W. Grimes #define BADTYPE(type) \ 3238fae3551SRodney W. Grimes (strcmp(type, FSTAB_RO) && \ 3248fae3551SRodney W. Grimes strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 3258fae3551SRodney W. Grimes 3263cbf527eSRuslan Ermilov if ((init_flags & MNT_UPDATE) && (ro == 0)) 3273cbf527eSRuslan Ermilov options = catopt(options, "noro"); 3283cbf527eSRuslan Ermilov 3298fae3551SRodney W. Grimes rval = 0; 3308fae3551SRodney W. Grimes switch (argc) { 3318fae3551SRodney W. Grimes case 0: 332fddf7baeSKirk McKusick if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 333fba1c154SSteve Price err(1, "getmntinfo"); 334fba1c154SSteve Price if (all) { 3358fae3551SRodney W. Grimes while ((fs = getfsent()) != NULL) { 3368fae3551SRodney W. Grimes if (BADTYPE(fs->fs_type)) 3378fae3551SRodney W. Grimes continue; 338c06fe0a0SPeter Wemm if (checkvfsname(fs->fs_vfstype, vfslist)) 3398fae3551SRodney W. Grimes continue; 340c06fe0a0SPeter Wemm if (hasopt(fs->fs_mntops, "noauto")) 34189beb278SDavid Greenman continue; 342b48b774fSChris Rees if (!hasopt(fs->fs_mntops, "late") && onlylate) 343b48b774fSChris Rees continue; 3444b4f9170SDag-Erling Smørgrav if (hasopt(fs->fs_mntops, "late") && !late) 3454b4f9170SDag-Erling Smørgrav continue; 346c7383075SXin LI if (hasopt(fs->fs_mntops, "failok")) 347c7383075SXin LI failok = 1; 348c7383075SXin LI else 349c7383075SXin LI failok = 0; 35098201b0cSBruce Evans if (!(init_flags & MNT_UPDATE) && 35198201b0cSBruce Evans ismounted(fs, mntbuf, mntsize)) 352fba1c154SSteve Price continue; 35313cbdf24SGuido van Rooij options = update_options(options, fs->fs_mntops, 35413cbdf24SGuido van Rooij mntbuf->f_flags); 3558fae3551SRodney W. Grimes if (mountfs(fs->fs_vfstype, fs->fs_spec, 3568fae3551SRodney W. Grimes fs->fs_file, init_flags, options, 357c7383075SXin LI fs->fs_mntops) && !failok) 3588fae3551SRodney W. Grimes rval = 1; 3598fae3551SRodney W. Grimes } 360fba1c154SSteve Price } else if (fstab_style) { 361a257a45eSJordan K. Hubbard for (i = 0; i < mntsize; i++) { 362c06fe0a0SPeter Wemm if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 363a257a45eSJordan K. Hubbard continue; 364a257a45eSJordan K. Hubbard putfsent(&mntbuf[i]); 365a257a45eSJordan K. Hubbard } 36674cf460bSBruce Evans } else { 3678fae3551SRodney W. Grimes for (i = 0; i < mntsize; i++) { 36874cf460bSBruce Evans if (checkvfsname(mntbuf[i].f_fstypename, 36974cf460bSBruce Evans vfslist)) 3708fae3551SRodney W. Grimes continue; 3718abb2a6eSPawel Jakub Dawidek if (!verbose && 3728abb2a6eSPawel Jakub Dawidek (mntbuf[i].f_flags & MNT_IGNORE) != 0) 3738abb2a6eSPawel Jakub Dawidek continue; 374c06fe0a0SPeter Wemm prmount(&mntbuf[i]); 3758fae3551SRodney W. Grimes } 3768fae3551SRodney W. Grimes } 3778fae3551SRodney W. Grimes exit(rval); 3788fae3551SRodney W. Grimes case 1: 3798fae3551SRodney W. Grimes if (vfslist != NULL) 3808fae3551SRodney W. Grimes usage(); 3818fae3551SRodney W. Grimes 38201a9bce5SEric Anholt rmslashes(*argv, *argv); 3838fae3551SRodney W. Grimes if (init_flags & MNT_UPDATE) { 384cf96af72SBrian Feldman mntfromname = NULL; 385cf96af72SBrian Feldman have_fstab = 0; 3868fae3551SRodney W. Grimes if ((mntbuf = getmntpt(*argv)) == NULL) 387cf96af72SBrian Feldman errx(1, "not currently mounted %s", *argv); 388cf96af72SBrian Feldman /* 389cf96af72SBrian Feldman * Only get the mntflags from fstab if both mntpoint 390cf96af72SBrian Feldman * and mntspec are identical. Also handle the special 391cf96af72SBrian Feldman * case where just '/' is mounted and 'spec' is not 392cf96af72SBrian Feldman * identical with the one from fstab ('/dev' is missing 393cf96af72SBrian Feldman * in the spec-string at boot-time). 394cf96af72SBrian Feldman */ 39518af6044SJoseph Koshy if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 396cf96af72SBrian Feldman if (strcmp(fs->fs_spec, 397cf96af72SBrian Feldman mntbuf->f_mntfromname) == 0 && 398cf96af72SBrian Feldman strcmp(fs->fs_file, 399cf96af72SBrian Feldman mntbuf->f_mntonname) == 0) { 400cf96af72SBrian Feldman have_fstab = 1; 401cf96af72SBrian Feldman mntfromname = mntbuf->f_mntfromname; 402cf96af72SBrian Feldman } else if (argv[0][0] == '/' && 4031f3aded0SEdward Tomasz Napierala argv[0][1] == '\0' && 4041f3aded0SEdward Tomasz Napierala strcmp(fs->fs_vfstype, 4051f3aded0SEdward Tomasz Napierala mntbuf->f_fstypename) == 0) { 406cf96af72SBrian Feldman fs = getfsfile("/"); 407cf96af72SBrian Feldman have_fstab = 1; 408c06fe0a0SPeter Wemm mntfromname = fs->fs_spec; 409cf96af72SBrian Feldman } 410cf96af72SBrian Feldman } 411cf96af72SBrian Feldman if (have_fstab) { 41218af6044SJoseph Koshy options = update_options(options, fs->fs_mntops, 41318af6044SJoseph Koshy mntbuf->f_flags); 41418af6044SJoseph Koshy } else { 415c06fe0a0SPeter Wemm mntfromname = mntbuf->f_mntfromname; 41618af6044SJoseph Koshy options = update_options(options, NULL, 41718af6044SJoseph Koshy mntbuf->f_flags); 41818af6044SJoseph Koshy } 419c06fe0a0SPeter Wemm rval = mountfs(mntbuf->f_fstypename, mntfromname, 420c06fe0a0SPeter Wemm mntbuf->f_mntonname, init_flags, options, 0); 421c06fe0a0SPeter Wemm break; 422c06fe0a0SPeter Wemm } 4238fae3551SRodney W. Grimes if ((fs = getfsfile(*argv)) == NULL && 4248fae3551SRodney W. Grimes (fs = getfsspec(*argv)) == NULL) 425bcb1d846SPhilippe Charnier errx(1, "%s: unknown special file or file system", 4268fae3551SRodney W. Grimes *argv); 4278fae3551SRodney W. Grimes if (BADTYPE(fs->fs_type)) 428bcb1d846SPhilippe Charnier errx(1, "%s has unknown file system type", 4298fae3551SRodney W. Grimes *argv); 430c06fe0a0SPeter Wemm rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 431c06fe0a0SPeter Wemm init_flags, options, fs->fs_mntops); 4328fae3551SRodney W. Grimes break; 4338fae3551SRodney W. Grimes case 2: 4348fae3551SRodney W. Grimes /* 435cf96af72SBrian Feldman * If -t flag has not been specified, the path cannot be 436003dbca6SMaxime Henrion * found, spec contains either a ':' or a '@', then assume 437cf96af72SBrian Feldman * that an NFS file system is being specified ala Sun. 438003dbca6SMaxime Henrion * Check if the hostname contains only allowed characters 439003dbca6SMaxime Henrion * to reduce false positives. IPv6 addresses containing 440003dbca6SMaxime Henrion * ':' will be correctly parsed only if the separator is '@'. 441003dbca6SMaxime Henrion * The definition of a valid hostname is taken from RFC 1034. 4428fae3551SRodney W. Grimes */ 44305779418SIan Dowse if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL || 44405779418SIan Dowse (ep = strchr(argv[0], ':')) != NULL)) { 445da85c82bSMaxime Henrion if (*ep == '@') { 446da85c82bSMaxime Henrion cp = ep + 1; 447da85c82bSMaxime Henrion ep = cp + strlen(cp); 448da85c82bSMaxime Henrion } else 449003dbca6SMaxime Henrion cp = argv[0]; 450003dbca6SMaxime Henrion while (cp != ep) { 451003dbca6SMaxime Henrion if (!isdigit(*cp) && !isalpha(*cp) && 452003dbca6SMaxime Henrion *cp != '.' && *cp != '-' && *cp != ':') 453003dbca6SMaxime Henrion break; 454003dbca6SMaxime Henrion cp++; 455003dbca6SMaxime Henrion } 456003dbca6SMaxime Henrion if (cp == ep) 4578fae3551SRodney W. Grimes vfstype = "nfs"; 458003dbca6SMaxime Henrion } 4598fae3551SRodney W. Grimes rval = mountfs(vfstype, 4608fae3551SRodney W. Grimes argv[0], argv[1], init_flags, options, NULL); 4618fae3551SRodney W. Grimes break; 4628fae3551SRodney W. Grimes default: 4638fae3551SRodney W. Grimes usage(); 4648fae3551SRodney W. Grimes /* NOTREACHED */ 4658fae3551SRodney W. Grimes } 4668fae3551SRodney W. Grimes 4678fae3551SRodney W. Grimes /* 4688fae3551SRodney W. Grimes * If the mount was successfully, and done by root, tell mountd the 469e9988cedSPawel Jakub Dawidek * good news. 4708fae3551SRodney W. Grimes */ 471e9988cedSPawel Jakub Dawidek if (rval == 0 && getuid() == 0) 472e9988cedSPawel Jakub Dawidek restart_mountd(); 4738fae3551SRodney W. Grimes 4748fae3551SRodney W. Grimes exit(rval); 4758fae3551SRodney W. Grimes } 4768fae3551SRodney W. Grimes 4778fae3551SRodney W. Grimes int 478e24dc56aSCraig Rodrigues ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize) 479fba1c154SSteve Price { 48090742659SPawel Jakub Dawidek char realfsfile[PATH_MAX]; 481fba1c154SSteve Price int i; 482fba1c154SSteve Price 483fba1c154SSteve Price if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 484fba1c154SSteve Price /* the root file system can always be remounted */ 485fba1c154SSteve Price return (0); 486fba1c154SSteve Price 48790742659SPawel Jakub Dawidek /* The user may have specified a symlink in fstab, resolve the path */ 48890742659SPawel Jakub Dawidek if (realpath(fs->fs_file, realfsfile) == NULL) { 48990742659SPawel Jakub Dawidek /* Cannot resolve the path, use original one */ 49090742659SPawel Jakub Dawidek strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile)); 49190742659SPawel Jakub Dawidek } 49290742659SPawel Jakub Dawidek 493b5bbeb21SAlan Somers /* 494b5bbeb21SAlan Somers * Consider the filesystem to be mounted if: 495b5bbeb21SAlan Somers * It has the same mountpoint as a mounted filesytem, and 496b5bbeb21SAlan Somers * It has the same type as that same mounted filesystem, and 497b5bbeb21SAlan Somers * It has the same device name as that same mounted filesystem, OR 498b5bbeb21SAlan Somers * It is a nonremountable filesystem 499b5bbeb21SAlan Somers */ 500fba1c154SSteve Price for (i = mntsize - 1; i >= 0; --i) 50190742659SPawel Jakub Dawidek if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 && 502b5bbeb21SAlan Somers strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 && 503fba1c154SSteve Price (!isremountable(fs->fs_vfstype) || 504b5bbeb21SAlan Somers (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))) 505fba1c154SSteve Price return (1); 506fba1c154SSteve Price return (0); 507fba1c154SSteve Price } 508fba1c154SSteve Price 509fba1c154SSteve Price int 510e24dc56aSCraig Rodrigues isremountable(const char *vfsname) 511fba1c154SSteve Price { 512fba1c154SSteve Price const char **cp; 513fba1c154SSteve Price 514fba1c154SSteve Price for (cp = remountable_fs_names; *cp; cp++) 515fba1c154SSteve Price if (strcmp(*cp, vfsname) == 0) 516fba1c154SSteve Price return (1); 517fba1c154SSteve Price return (0); 518fba1c154SSteve Price } 519fba1c154SSteve Price 520fba1c154SSteve Price int 521e24dc56aSCraig Rodrigues hasopt(const char *mntopts, const char *option) 522c06fe0a0SPeter Wemm { 523c06fe0a0SPeter Wemm int negative, found; 524c06fe0a0SPeter Wemm char *opt, *optbuf; 525c06fe0a0SPeter Wemm 526c06fe0a0SPeter Wemm if (option[0] == 'n' && option[1] == 'o') { 527c06fe0a0SPeter Wemm negative = 1; 528c06fe0a0SPeter Wemm option += 2; 529c06fe0a0SPeter Wemm } else 530c06fe0a0SPeter Wemm negative = 0; 531c06fe0a0SPeter Wemm optbuf = strdup(mntopts); 532c06fe0a0SPeter Wemm found = 0; 533c06fe0a0SPeter Wemm for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 534c06fe0a0SPeter Wemm if (opt[0] == 'n' && opt[1] == 'o') { 535c06fe0a0SPeter Wemm if (!strcasecmp(opt + 2, option)) 536c06fe0a0SPeter Wemm found = negative; 537c06fe0a0SPeter Wemm } else if (!strcasecmp(opt, option)) 538c06fe0a0SPeter Wemm found = !negative; 539c06fe0a0SPeter Wemm } 540c06fe0a0SPeter Wemm free(optbuf); 541c06fe0a0SPeter Wemm return (found); 542c06fe0a0SPeter Wemm } 543c06fe0a0SPeter Wemm 544a86de995SDavid E. O'Brien static void 545a86de995SDavid E. O'Brien append_arg(struct cpa *sa, char *arg) 546a86de995SDavid E. O'Brien { 5470e969ed7SDavid E. O'Brien if (sa->c + 1 == sa->sz) { 5480e969ed7SDavid E. O'Brien sa->sz = sa->sz == 0 ? 8 : sa->sz * 2; 549e96092e8SUlrich Spörlein sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz); 55000356118SDavid E. O'Brien if (sa->a == NULL) 55100356118SDavid E. O'Brien errx(1, "realloc failed"); 55200356118SDavid E. O'Brien } 553a86de995SDavid E. O'Brien sa->a[++sa->c] = arg; 554a86de995SDavid E. O'Brien } 555a86de995SDavid E. O'Brien 556c06fe0a0SPeter Wemm int 557e24dc56aSCraig Rodrigues mountfs(const char *vfstype, const char *spec, const char *name, int flags, 558e24dc56aSCraig Rodrigues const char *options, const char *mntopts) 5598fae3551SRodney W. Grimes { 5608fae3551SRodney W. Grimes struct statfs sf; 56176c46216SDavid E. O'Brien int i, ret; 56268dd1ff4SWarner Losh char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; 5630e969ed7SDavid E. O'Brien static struct cpa mnt_argv; 5648fae3551SRodney W. Grimes 56573dd3167SPoul-Henning Kamp /* resolve the mountpoint with realpath(3) */ 566d3250014SJaakko Heinonen if (checkpath(name, mntpath) != 0) { 567d3250014SJaakko Heinonen warn("%s", mntpath); 568d3250014SJaakko Heinonen return (1); 569d3250014SJaakko Heinonen } 5708fae3551SRodney W. Grimes name = mntpath; 5718fae3551SRodney W. Grimes 572c06fe0a0SPeter Wemm if (mntopts == NULL) 573c06fe0a0SPeter Wemm mntopts = ""; 5748fae3551SRodney W. Grimes optbuf = catopt(strdup(mntopts), options); 5758fae3551SRodney W. Grimes 5768fae3551SRodney W. Grimes if (strcmp(name, "/") == 0) 5778fae3551SRodney W. Grimes flags |= MNT_UPDATE; 5788fae3551SRodney W. Grimes if (flags & MNT_FORCE) 5798fae3551SRodney W. Grimes optbuf = catopt(optbuf, "force"); 5808fae3551SRodney W. Grimes if (flags & MNT_RDONLY) 5818fae3551SRodney W. Grimes optbuf = catopt(optbuf, "ro"); 5828fae3551SRodney W. Grimes /* 5838fae3551SRodney W. Grimes * XXX 5848fae3551SRodney W. Grimes * The mount_mfs (newfs) command uses -o to select the 585bcb1d846SPhilippe Charnier * optimization mode. We don't pass the default "-o rw" 5868fae3551SRodney W. Grimes * for that reason. 5878fae3551SRodney W. Grimes */ 5888fae3551SRodney W. Grimes if (flags & MNT_UPDATE) 5898fae3551SRodney W. Grimes optbuf = catopt(optbuf, "update"); 5908fae3551SRodney W. Grimes 5914ccd7546SRuslan Ermilov /* Compatibility glue. */ 592*0b8c3359SWarner Losh if (strcmp(vfstype, "msdos") == 0) 5934ccd7546SRuslan Ermilov vfstype = "msdosfs"; 5944ccd7546SRuslan Ermilov 5956e34b479SColin Percival /* Construct the name of the appropriate mount command */ 5966e34b479SColin Percival (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 5976e34b479SColin Percival 598a86de995SDavid E. O'Brien mnt_argv.c = -1; 599a86de995SDavid E. O'Brien append_arg(&mnt_argv, execname); 600a86de995SDavid E. O'Brien mangle(optbuf, &mnt_argv); 601c7835769SCraig Rodrigues if (mountprog != NULL) 6026e24000aSAlan Somers strlcpy(execname, mountprog, sizeof(execname)); 603c7835769SCraig Rodrigues 604a86de995SDavid E. O'Brien append_arg(&mnt_argv, strdup(spec)); 605a86de995SDavid E. O'Brien append_arg(&mnt_argv, strdup(name)); 606a86de995SDavid E. O'Brien append_arg(&mnt_argv, NULL); 607fce5f960SDavid E. O'Brien 6088fae3551SRodney W. Grimes if (debug) { 609c195c7f6SCraig Rodrigues if (use_mountprog(vfstype)) 610c7835769SCraig Rodrigues printf("exec: %s", execname); 611c195c7f6SCraig Rodrigues else 612c195c7f6SCraig Rodrigues printf("mount -t %s", vfstype); 613a86de995SDavid E. O'Brien for (i = 1; i < mnt_argv.c; i++) 614a86de995SDavid E. O'Brien (void)printf(" %s", mnt_argv.a[i]); 6158fae3551SRodney W. Grimes (void)printf("\n"); 616d2e17ce9SJohn Baldwin free(optbuf); 617d2e17ce9SJohn Baldwin free(mountprog); 618d2e17ce9SJohn Baldwin mountprog = NULL; 6198fae3551SRodney W. Grimes return (0); 6208fae3551SRodney W. Grimes } 6218fae3551SRodney W. Grimes 622b1e6b712SCraig Rodrigues if (use_mountprog(vfstype)) { 623a86de995SDavid E. O'Brien ret = exec_mountprog(name, execname, mnt_argv.a); 6246f5f1a6bSCraig Rodrigues } else { 625a86de995SDavid E. O'Brien ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a); 6266f5f1a6bSCraig Rodrigues } 6276f5f1a6bSCraig Rodrigues 6288fae3551SRodney W. Grimes free(optbuf); 629d2e17ce9SJohn Baldwin free(mountprog); 630d2e17ce9SJohn Baldwin mountprog = NULL; 6318fae3551SRodney W. Grimes 6328fae3551SRodney W. Grimes if (verbose) { 6338fae3551SRodney W. Grimes if (statfs(name, &sf) < 0) { 634c06fe0a0SPeter Wemm warn("statfs %s", name); 6358fae3551SRodney W. Grimes return (1); 6368fae3551SRodney W. Grimes } 637a257a45eSJordan K. Hubbard if (fstab_style) 638a257a45eSJordan K. Hubbard putfsent(&sf); 639a257a45eSJordan K. Hubbard else 640c06fe0a0SPeter Wemm prmount(&sf); 6418fae3551SRodney W. Grimes } 6428fae3551SRodney W. Grimes 643bbe9d7daSMatteo Riondato return (ret); 6448fae3551SRodney W. Grimes } 6458fae3551SRodney W. Grimes 6468fae3551SRodney W. Grimes void 647e24dc56aSCraig Rodrigues prmount(struct statfs *sfp) 6488fae3551SRodney W. Grimes { 6499df3a164SBjoern A. Zeeb uint64_t flags; 650aedf10acSCraig Rodrigues unsigned int i; 6518fae3551SRodney W. Grimes struct opt *o; 652c06fe0a0SPeter Wemm struct passwd *pw; 6538fae3551SRodney W. Grimes 654af2ea5aaSNick Hibma (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 655af2ea5aaSNick Hibma sfp->f_fstypename); 6568fae3551SRodney W. Grimes 657c06fe0a0SPeter Wemm flags = sfp->f_flags & MNT_VISFLAGMASK; 6589df3a164SBjoern A. Zeeb for (o = optnames; flags != 0 && o->o_opt != 0; o++) 6598fae3551SRodney W. Grimes if (flags & o->o_opt) { 660af2ea5aaSNick Hibma (void)printf(", %s", o->o_name); 6618fae3551SRodney W. Grimes flags &= ~o->o_opt; 6628fae3551SRodney W. Grimes } 663dc9c6194SPawel Jakub Dawidek /* 664dc9c6194SPawel Jakub Dawidek * Inform when file system is mounted by an unprivileged user 665dc9c6194SPawel Jakub Dawidek * or privileged non-root user. 666dc9c6194SPawel Jakub Dawidek */ 667ddb842ccSJacques Vidrine if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 668af2ea5aaSNick Hibma (void)printf(", mounted by "); 669c06fe0a0SPeter Wemm if ((pw = getpwuid(sfp->f_owner)) != NULL) 670c06fe0a0SPeter Wemm (void)printf("%s", pw->pw_name); 671c06fe0a0SPeter Wemm else 672c06fe0a0SPeter Wemm (void)printf("%d", sfp->f_owner); 673c06fe0a0SPeter Wemm } 674c62ffab6SSheldon Hearn if (verbose) { 675677b9b3fSBruce Evans if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 67696c65ccbSIan Dowse (void)printf(", writes: sync %ju async %ju", 67796c65ccbSIan Dowse (uintmax_t)sfp->f_syncwrites, 67896c65ccbSIan Dowse (uintmax_t)sfp->f_asyncwrites); 679461bb71eSKirk McKusick if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 68096c65ccbSIan Dowse (void)printf(", reads: sync %ju async %ju", 68196c65ccbSIan Dowse (uintmax_t)sfp->f_syncreads, 68296c65ccbSIan Dowse (uintmax_t)sfp->f_asyncreads); 68305779418SIan Dowse if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 68438f102c2SIan Dowse printf(", fsid "); 68538f102c2SIan Dowse for (i = 0; i < sizeof(sfp->f_fsid); i++) 68638f102c2SIan Dowse printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 687c62ffab6SSheldon Hearn } 68805779418SIan Dowse } 689af2ea5aaSNick Hibma (void)printf(")\n"); 6908fae3551SRodney W. Grimes } 6918fae3551SRodney W. Grimes 6928fae3551SRodney W. Grimes struct statfs * 693e24dc56aSCraig Rodrigues getmntpt(const char *name) 6948fae3551SRodney W. Grimes { 6958fae3551SRodney W. Grimes struct statfs *mntbuf; 6968fae3551SRodney W. Grimes int i, mntsize; 6978fae3551SRodney W. Grimes 698fddf7baeSKirk McKusick mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 699cf96af72SBrian Feldman for (i = mntsize - 1; i >= 0; i--) { 7008fae3551SRodney W. Grimes if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 7018fae3551SRodney W. Grimes strcmp(mntbuf[i].f_mntonname, name) == 0) 7028fae3551SRodney W. Grimes return (&mntbuf[i]); 703cf96af72SBrian Feldman } 7048fae3551SRodney W. Grimes return (NULL); 7058fae3551SRodney W. Grimes } 7068fae3551SRodney W. Grimes 7078fae3551SRodney W. Grimes char * 708e24dc56aSCraig Rodrigues catopt(char *s0, const char *s1) 7098fae3551SRodney W. Grimes { 7108fae3551SRodney W. Grimes char *cp; 7118fae3551SRodney W. Grimes 71218af6044SJoseph Koshy if (s1 == NULL || *s1 == '\0') 7134796c6ccSJuli Mallett return (s0); 71418af6044SJoseph Koshy 7158fae3551SRodney W. Grimes if (s0 && *s0) { 7168967299bSPedro F. Giffuni if (asprintf(&cp, "%s,%s", s0, s1) == -1) 7178967299bSPedro F. Giffuni errx(1, "asprintf failed"); 7188fae3551SRodney W. Grimes } else 7198fae3551SRodney W. Grimes cp = strdup(s1); 7208fae3551SRodney W. Grimes 7218fae3551SRodney W. Grimes if (s0) 7228fae3551SRodney W. Grimes free(s0); 7238fae3551SRodney W. Grimes return (cp); 7248fae3551SRodney W. Grimes } 7258fae3551SRodney W. Grimes 7268fae3551SRodney W. Grimes void 727a86de995SDavid E. O'Brien mangle(char *options, struct cpa *a) 7288fae3551SRodney W. Grimes { 729c7835769SCraig Rodrigues char *p, *s, *val; 7308fae3551SRodney W. Grimes 7318fae3551SRodney W. Grimes for (s = options; (p = strsep(&s, ",")) != NULL;) 73218af6044SJoseph Koshy if (*p != '\0') { 733d9fa6ce7SCraig Rodrigues if (strcmp(p, "noauto") == 0) { 734d9fa6ce7SCraig Rodrigues /* 735d9fa6ce7SCraig Rodrigues * Do not pass noauto option to nmount(). 736d9fa6ce7SCraig Rodrigues * or external mount program. noauto is 737d9fa6ce7SCraig Rodrigues * only used to prevent mounting a filesystem 738d9fa6ce7SCraig Rodrigues * when 'mount -a' is specified, and is 739d9fa6ce7SCraig Rodrigues * not a real mount option. 740d9fa6ce7SCraig Rodrigues */ 741d9fa6ce7SCraig Rodrigues continue; 7424b4f9170SDag-Erling Smørgrav } else if (strcmp(p, "late") == 0) { 7434b4f9170SDag-Erling Smørgrav /* 7444b4f9170SDag-Erling Smørgrav * "late" is used to prevent certain file 7454b4f9170SDag-Erling Smørgrav * systems from being mounted before late 7464b4f9170SDag-Erling Smørgrav * in the boot cycle; for instance, 7474b4f9170SDag-Erling Smørgrav * loopback NFS mounts can't be mounted 7484b4f9170SDag-Erling Smørgrav * before mountd starts. 7494b4f9170SDag-Erling Smørgrav */ 7504b4f9170SDag-Erling Smørgrav continue; 751c7383075SXin LI } else if (strcmp(p, "failok") == 0) { 752c7383075SXin LI /* 753c7383075SXin LI * "failok" is used to prevent certain file 754c7383075SXin LI * systems from being causing the system to 755c7383075SXin LI * drop into single user mode in the boot 756c7383075SXin LI * cycle, and is not a real mount option. 757c7383075SXin LI */ 758c7383075SXin LI continue; 759c7835769SCraig Rodrigues } else if (strncmp(p, "mountprog", 9) == 0) { 760c7835769SCraig Rodrigues /* 761c7835769SCraig Rodrigues * "mountprog" is used to force the use of 762c7835769SCraig Rodrigues * userland mount programs. 763c7835769SCraig Rodrigues */ 764c7835769SCraig Rodrigues val = strchr(p, '='); 765c7835769SCraig Rodrigues if (val != NULL) { 766c7835769SCraig Rodrigues ++val; 767c7835769SCraig Rodrigues if (*val != '\0') 768c7835769SCraig Rodrigues mountprog = strdup(val); 769c7835769SCraig Rodrigues } 770c7835769SCraig Rodrigues 771c7835769SCraig Rodrigues if (mountprog == NULL) { 772c7835769SCraig Rodrigues errx(1, "Need value for -o mountprog"); 773c7835769SCraig Rodrigues } 774c7835769SCraig Rodrigues continue; 77535d6c7f5SCraig Rodrigues } else if (strcmp(p, "userquota") == 0) { 77635d6c7f5SCraig Rodrigues continue; 7776e74fb9dSMaxim Konovalov } else if (strncmp(p, userquotaeq, 7786e74fb9dSMaxim Konovalov sizeof(userquotaeq) - 1) == 0) { 7796e74fb9dSMaxim Konovalov continue; 78035d6c7f5SCraig Rodrigues } else if (strcmp(p, "groupquota") == 0) { 78135d6c7f5SCraig Rodrigues continue; 7826e74fb9dSMaxim Konovalov } else if (strncmp(p, groupquotaeq, 7836e74fb9dSMaxim Konovalov sizeof(groupquotaeq) - 1) == 0) { 7846e74fb9dSMaxim Konovalov continue; 785d9fa6ce7SCraig Rodrigues } else if (*p == '-') { 786a86de995SDavid E. O'Brien append_arg(a, p); 7878fae3551SRodney W. Grimes p = strchr(p, '='); 788adfdbe22STom Rhodes if (p != NULL) { 7898fae3551SRodney W. Grimes *p = '\0'; 790a86de995SDavid E. O'Brien append_arg(a, p + 1); 7918fae3551SRodney W. Grimes } 792748e259bSCraig Rodrigues } else { 793a86de995SDavid E. O'Brien append_arg(a, strdup("-o")); 794a86de995SDavid E. O'Brien append_arg(a, p); 7958fae3551SRodney W. Grimes } 79618af6044SJoseph Koshy } 7978fae3551SRodney W. Grimes } 7988fae3551SRodney W. Grimes 79918af6044SJoseph Koshy 80018af6044SJoseph Koshy char * 8014796c6ccSJuli Mallett update_options(char *opts, char *fstab, int curflags) 80218af6044SJoseph Koshy { 80318af6044SJoseph Koshy char *o, *p; 80418af6044SJoseph Koshy char *cur; 80518af6044SJoseph Koshy char *expopt, *newopt, *tmpopt; 80618af6044SJoseph Koshy 80718af6044SJoseph Koshy if (opts == NULL) 8084796c6ccSJuli Mallett return (strdup("")); 80918af6044SJoseph Koshy 81018af6044SJoseph Koshy /* remove meta options from list */ 81118af6044SJoseph Koshy remopt(fstab, MOUNT_META_OPTION_FSTAB); 81218af6044SJoseph Koshy remopt(fstab, MOUNT_META_OPTION_CURRENT); 81318af6044SJoseph Koshy cur = flags2opts(curflags); 81418af6044SJoseph Koshy 81518af6044SJoseph Koshy /* 81618af6044SJoseph Koshy * Expand all meta-options passed to us first. 81718af6044SJoseph Koshy */ 81818af6044SJoseph Koshy expopt = NULL; 81918af6044SJoseph Koshy for (p = opts; (o = strsep(&p, ",")) != NULL;) { 82018af6044SJoseph Koshy if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 82118af6044SJoseph Koshy expopt = catopt(expopt, fstab); 82218af6044SJoseph Koshy else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 82318af6044SJoseph Koshy expopt = catopt(expopt, cur); 82418af6044SJoseph Koshy else 82518af6044SJoseph Koshy expopt = catopt(expopt, o); 82618af6044SJoseph Koshy } 82718af6044SJoseph Koshy free(cur); 82818af6044SJoseph Koshy free(opts); 82918af6044SJoseph Koshy 83018af6044SJoseph Koshy /* 83118af6044SJoseph Koshy * Remove previous contradictory arguments. Given option "foo" we 83218af6044SJoseph Koshy * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 83318af6044SJoseph Koshy * and "foo" - so we can deal with possible options like "notice". 83418af6044SJoseph Koshy */ 83518af6044SJoseph Koshy newopt = NULL; 83618af6044SJoseph Koshy for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 83718af6044SJoseph Koshy if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 83818af6044SJoseph Koshy errx(1, "malloc failed"); 83918af6044SJoseph Koshy 84018af6044SJoseph Koshy strcpy(tmpopt, "no"); 84118af6044SJoseph Koshy strcat(tmpopt, o); 84218af6044SJoseph Koshy remopt(newopt, tmpopt); 84318af6044SJoseph Koshy free(tmpopt); 84418af6044SJoseph Koshy 84518af6044SJoseph Koshy if (strncmp("no", o, 2) == 0) 84618af6044SJoseph Koshy remopt(newopt, o+2); 84718af6044SJoseph Koshy 84818af6044SJoseph Koshy newopt = catopt(newopt, o); 84918af6044SJoseph Koshy } 85018af6044SJoseph Koshy free(expopt); 85118af6044SJoseph Koshy 8524796c6ccSJuli Mallett return (newopt); 85318af6044SJoseph Koshy } 85418af6044SJoseph Koshy 85518af6044SJoseph Koshy void 8564796c6ccSJuli Mallett remopt(char *string, const char *opt) 85718af6044SJoseph Koshy { 85818af6044SJoseph Koshy char *o, *p, *r; 85918af6044SJoseph Koshy 86018af6044SJoseph Koshy if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 86118af6044SJoseph Koshy return; 86218af6044SJoseph Koshy 86318af6044SJoseph Koshy r = string; 86418af6044SJoseph Koshy 86518af6044SJoseph Koshy for (p = string; (o = strsep(&p, ",")) != NULL;) { 86618af6044SJoseph Koshy if (strcmp(opt, o) != 0) { 86718af6044SJoseph Koshy if (*r == ',' && *o != '\0') 86818af6044SJoseph Koshy r++; 86918af6044SJoseph Koshy while ((*r++ = *o++) != '\0') 87018af6044SJoseph Koshy ; 87118af6044SJoseph Koshy *--r = ','; 87218af6044SJoseph Koshy } 87318af6044SJoseph Koshy } 87418af6044SJoseph Koshy *r = '\0'; 87518af6044SJoseph Koshy } 87618af6044SJoseph Koshy 8778fae3551SRodney W. Grimes void 8784796c6ccSJuli Mallett usage(void) 8798fae3551SRodney W. Grimes { 8808fae3551SRodney W. Grimes 881bcb1d846SPhilippe Charnier (void)fprintf(stderr, "%s\n%s\n%s\n", 8824b4f9170SDag-Erling Smørgrav "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 8838d646af5SRuslan Ermilov " mount [-dfpruvw] special | node", 8848d646af5SRuslan Ermilov " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 8858fae3551SRodney W. Grimes exit(1); 8868fae3551SRodney W. Grimes } 887a257a45eSJordan K. Hubbard 888a257a45eSJordan K. Hubbard void 889031ea52fSMatteo Riondato putfsent(struct statfs *ent) 890a257a45eSJordan K. Hubbard { 891a257a45eSJordan K. Hubbard struct fstab *fst; 89272da5470SJaakko Heinonen char *opts, *rw; 893306d73d6SPoul-Henning Kamp int l; 894a257a45eSJordan K. Hubbard 89572da5470SJaakko Heinonen opts = NULL; 89672da5470SJaakko Heinonen /* flags2opts() doesn't return the "rw" option. */ 89772da5470SJaakko Heinonen if ((ent->f_flags & MNT_RDONLY) != 0) 89872da5470SJaakko Heinonen rw = NULL; 89972da5470SJaakko Heinonen else 90072da5470SJaakko Heinonen rw = catopt(NULL, "rw"); 90172da5470SJaakko Heinonen 90218af6044SJoseph Koshy opts = flags2opts(ent->f_flags); 90372da5470SJaakko Heinonen opts = catopt(rw, opts); 9043ae7ea68SGiorgos Keramidas 905031ea52fSMatteo Riondato if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 || 906031ea52fSMatteo Riondato strncmp(ent->f_mntfromname, "<above>", 7) == 0) { 9076e24000aSAlan Somers strlcpy(ent->f_mntfromname, 9086e24000aSAlan Somers (strnstr(ent->f_mntfromname, ":", 8) +1), 9096e24000aSAlan Somers sizeof(ent->f_mntfromname)); 910031ea52fSMatteo Riondato } 911031ea52fSMatteo Riondato 912306d73d6SPoul-Henning Kamp l = strlen(ent->f_mntfromname); 913306d73d6SPoul-Henning Kamp printf("%s%s%s%s", ent->f_mntfromname, 914306d73d6SPoul-Henning Kamp l < 8 ? "\t" : "", 915306d73d6SPoul-Henning Kamp l < 16 ? "\t" : "", 916306d73d6SPoul-Henning Kamp l < 24 ? "\t" : " "); 917306d73d6SPoul-Henning Kamp l = strlen(ent->f_mntonname); 918306d73d6SPoul-Henning Kamp printf("%s%s%s%s", ent->f_mntonname, 919306d73d6SPoul-Henning Kamp l < 8 ? "\t" : "", 920306d73d6SPoul-Henning Kamp l < 16 ? "\t" : "", 921306d73d6SPoul-Henning Kamp l < 24 ? "\t" : " "); 922306d73d6SPoul-Henning Kamp printf("%s\t", ent->f_fstypename); 923306d73d6SPoul-Henning Kamp l = strlen(opts); 924306d73d6SPoul-Henning Kamp printf("%s%s", opts, 925306d73d6SPoul-Henning Kamp l < 8 ? "\t" : " "); 92618af6044SJoseph Koshy free(opts); 927c06fe0a0SPeter Wemm 928fba1c154SSteve Price if ((fst = getfsspec(ent->f_mntfromname))) 929a257a45eSJordan K. Hubbard printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 930fba1c154SSteve Price else if ((fst = getfsfile(ent->f_mntonname))) 931a257a45eSJordan K. Hubbard printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 932ab80d6faSBrian Feldman else if (strcmp(ent->f_fstypename, "ufs") == 0) { 933ab80d6faSBrian Feldman if (strcmp(ent->f_mntonname, "/") == 0) 934a257a45eSJordan K. Hubbard printf("\t1 1\n"); 935a257a45eSJordan K. Hubbard else 936ab80d6faSBrian Feldman printf("\t2 2\n"); 937ab80d6faSBrian Feldman } else 938a257a45eSJordan K. Hubbard printf("\t0 0\n"); 939a257a45eSJordan K. Hubbard } 94018af6044SJoseph Koshy 94118af6044SJoseph Koshy 94218af6044SJoseph Koshy char * 9434796c6ccSJuli Mallett flags2opts(int flags) 94418af6044SJoseph Koshy { 94518af6044SJoseph Koshy char *res; 94618af6044SJoseph Koshy 94718af6044SJoseph Koshy res = NULL; 94818af6044SJoseph Koshy 94988e2c335SCraig Rodrigues if (flags & MNT_RDONLY) res = catopt(res, "ro"); 95018af6044SJoseph Koshy if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 95118af6044SJoseph Koshy if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 95218af6044SJoseph Koshy if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 95318af6044SJoseph Koshy if (flags & MNT_UNION) res = catopt(res, "union"); 95418af6044SJoseph Koshy if (flags & MNT_ASYNC) res = catopt(res, "async"); 95518af6044SJoseph Koshy if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 95618af6044SJoseph Koshy if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 95718af6044SJoseph Koshy if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 95818af6044SJoseph Koshy if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 95918af6044SJoseph Koshy if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 960ba0fbe96SRobert Watson if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 96103d94b50SRobert Watson if (flags & MNT_ACLS) res = catopt(res, "acls"); 9629340fc72SEdward Tomasz Napierala if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls"); 96318af6044SJoseph Koshy 9644796c6ccSJuli Mallett return (res); 96518af6044SJoseph Koshy } 966