41 42#include <sys/param.h> 43#include <sys/mount.h> 44#include <sys/stat.h> 45#include <sys/wait.h> 46 47#include <ctype.h> 48#include <err.h> 49#include <errno.h> 50#include <fstab.h> 51#include <paths.h> 52#include <pwd.h> 53#include <signal.h> 54#include <stdint.h> 55#include <stdio.h> 56#include <stdlib.h> 57#include <string.h> 58#include <unistd.h> 59#include <libutil.h> 60 61#include "extern.h" 62#include "mntopts.h" 63#include "pathnames.h" 64 65/* `meta' options */ 66#define MOUNT_META_OPTION_FSTAB "fstab" 67#define MOUNT_META_OPTION_CURRENT "current" 68 69static int debug, fstab_style, verbose; 70 71struct cpa { 72 char **a; 73 ssize_t sz; 74 int c; 75}; 76 77char *catopt(char *, const char *); 78struct statfs *getmntpt(const char *); 79int hasopt(const char *, const char *); 80int ismounted(struct fstab *, struct statfs *, int); 81int isremountable(const char *); 82void mangle(char *, struct cpa *); 83char *update_options(char *, char *, int); 84int mountfs(const char *, const char *, const char *, 85 int, const char *, const char *); 86void remopt(char *, const char *); 87void prmount(struct statfs *); 88void putfsent(struct statfs *); 89void usage(void); 90char *flags2opts(int); 91 92/* Map from mount options to printable formats. */ 93static struct opt { 94 uint64_t o_opt; 95 const char *o_name; 96} optnames[] = { 97 { MNT_ASYNC, "asynchronous" }, 98 { MNT_EXPORTED, "NFS exported" }, 99 { MNT_LOCAL, "local" }, 100 { MNT_NOATIME, "noatime" }, 101 { MNT_NOEXEC, "noexec" }, 102 { MNT_NOSUID, "nosuid" }, 103 { MNT_NOSYMFOLLOW, "nosymfollow" }, 104 { MNT_QUOTA, "with quotas" }, 105 { MNT_RDONLY, "read-only" }, 106 { MNT_SYNCHRONOUS, "synchronous" }, 107 { MNT_UNION, "union" }, 108 { MNT_NOCLUSTERR, "noclusterr" }, 109 { MNT_NOCLUSTERW, "noclusterw" }, 110 { MNT_SUIDDIR, "suiddir" }, 111 { MNT_SOFTDEP, "soft-updates" }, 112 { MNT_SUJ, "journaled soft-updates" }, 113 { MNT_MULTILABEL, "multilabel" }, 114 { MNT_ACLS, "acls" }, 115 { MNT_NFS4ACLS, "nfsv4acls" }, 116 { MNT_GJOURNAL, "gjournal" }, 117 { 0, NULL } 118}; 119 120/* 121 * List of VFS types that can be remounted without becoming mounted on top 122 * of each other. 123 * XXX Is this list correct? 124 */ 125static const char * 126remountable_fs_names[] = { 127 "ufs", "ffs", "ext2fs", 128 0 129}; 130 131static const char userquotaeq[] = "userquota="; 132static const char groupquotaeq[] = "groupquota="; 133 134static char *mountprog = NULL; 135 136static int 137use_mountprog(const char *vfstype) 138{ 139 /* XXX: We need to get away from implementing external mount 140 * programs for every filesystem, and move towards having 141 * each filesystem properly implement the nmount() system call. 142 */ 143 unsigned int i; 144 const char *fs[] = { 145 "cd9660", "mfs", "msdosfs", "nfs", "ntfs", 146 "nwfs", "nullfs", "oldnfs", "portalfs", "smbfs", "udf", "unionfs", 147 NULL 148 }; 149 150 if (mountprog != NULL) 151 return (1); 152 153 for (i = 0; fs[i] != NULL; ++i) { 154 if (strcmp(vfstype, fs[i]) == 0) 155 return (1); 156 } 157 158 return (0); 159} 160 161static int 162exec_mountprog(const char *name, const char *execname, char *const argv[]) 163{ 164 pid_t pid; 165 int status; 166 167 switch (pid = fork()) { 168 case -1: /* Error. */ 169 warn("fork"); 170 exit (1); 171 case 0: /* Child. */ 172 /* Go find an executable. */ 173 execvP(execname, _PATH_SYSPATH, argv); 174 if (errno == ENOENT) { 175 warn("exec %s not found", execname); 176 if (execname[0] != '/') { 177 warnx("in path: %s", _PATH_SYSPATH); 178 } 179 } 180 exit(1); 181 default: /* Parent. */ 182 if (waitpid(pid, &status, 0) < 0) { 183 warn("waitpid"); 184 return (1); 185 } 186 187 if (WIFEXITED(status)) { 188 if (WEXITSTATUS(status) != 0) 189 return (WEXITSTATUS(status)); 190 } else if (WIFSIGNALED(status)) { 191 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 192 return (1); 193 } 194 break; 195 } 196 197 return (0); 198} 199 200static int 201specified_ro(const char *arg) 202{ 203 char *optbuf, *opt; 204 int ret = 0; 205 206 optbuf = strdup(arg); 207 if (optbuf == NULL) 208 err(1, NULL); 209 210 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 211 if (strcmp(opt, "ro") == 0) { 212 ret = 1; 213 break; 214 } 215 } 216 free(optbuf); 217 return (ret); 218} 219 220static void 221restart_mountd(void) 222{ 223 struct pidfh *pfh; 224 pid_t mountdpid; 225 226 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid); 227 if (pfh != NULL) { 228 /* Mountd is not running. */ 229 pidfile_remove(pfh); 230 return; 231 } 232 if (errno != EEXIST) { 233 /* Cannot open pidfile for some reason. */ 234 return; 235 } 236 /* We have mountd(8) PID in mountdpid varible, let's signal it. */ 237 if (kill(mountdpid, SIGHUP) == -1) 238 err(1, "signal mountd"); 239} 240 241int 242main(int argc, char *argv[]) 243{ 244 const char *mntfromname, **vfslist, *vfstype; 245 struct fstab *fs; 246 struct statfs *mntbuf; 247 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro; 248 char *cp, *ep, *options; 249 250 all = init_flags = late = 0; 251 ro = 0; 252 options = NULL; 253 vfslist = NULL; 254 vfstype = "ufs"; 255 while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1) 256 switch (ch) { 257 case 'a': 258 all = 1; 259 break; 260 case 'd': 261 debug = 1; 262 break; 263 case 'F': 264 setfstab(optarg); 265 break; 266 case 'f': 267 init_flags |= MNT_FORCE; 268 break; 269 case 'l': 270 late = 1; 271 break; 272 case 'o': 273 if (*optarg) { 274 options = catopt(options, optarg); 275 if (specified_ro(optarg)) 276 ro = 1; 277 } 278 break; 279 case 'p': 280 fstab_style = 1; 281 verbose = 1; 282 break; 283 case 'r': 284 options = catopt(options, "ro"); 285 ro = 1; 286 break; 287 case 't': 288 if (vfslist != NULL) 289 errx(1, "only one -t option may be specified"); 290 vfslist = makevfslist(optarg); 291 vfstype = optarg; 292 break; 293 case 'u': 294 init_flags |= MNT_UPDATE; 295 break; 296 case 'v': 297 verbose = 1; 298 break; 299 case 'w': 300 options = catopt(options, "noro"); 301 break; 302 case '?': 303 default: 304 usage(); 305 /* NOTREACHED */ 306 } 307 argc -= optind; 308 argv += optind; 309 310#define BADTYPE(type) \ 311 (strcmp(type, FSTAB_RO) && \ 312 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 313 314 if ((init_flags & MNT_UPDATE) && (ro == 0)) 315 options = catopt(options, "noro"); 316 317 rval = 0; 318 switch (argc) { 319 case 0: 320 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 321 err(1, "getmntinfo"); 322 if (all) { 323 while ((fs = getfsent()) != NULL) { 324 if (BADTYPE(fs->fs_type)) 325 continue; 326 if (checkvfsname(fs->fs_vfstype, vfslist)) 327 continue; 328 if (hasopt(fs->fs_mntops, "noauto")) 329 continue; 330 if (hasopt(fs->fs_mntops, "late") && !late) 331 continue; 332 if (hasopt(fs->fs_mntops, "failok")) 333 failok = 1; 334 else 335 failok = 0; 336 if (!(init_flags & MNT_UPDATE) && 337 ismounted(fs, mntbuf, mntsize)) 338 continue; 339 options = update_options(options, fs->fs_mntops, 340 mntbuf->f_flags); 341 if (mountfs(fs->fs_vfstype, fs->fs_spec, 342 fs->fs_file, init_flags, options, 343 fs->fs_mntops) && !failok) 344 rval = 1; 345 } 346 } else if (fstab_style) { 347 for (i = 0; i < mntsize; i++) { 348 if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 349 continue; 350 putfsent(&mntbuf[i]); 351 } 352 } else { 353 for (i = 0; i < mntsize; i++) { 354 if (checkvfsname(mntbuf[i].f_fstypename, 355 vfslist)) 356 continue; 357 if (!verbose && 358 (mntbuf[i].f_flags & MNT_IGNORE) != 0) 359 continue; 360 prmount(&mntbuf[i]); 361 } 362 } 363 exit(rval); 364 case 1: 365 if (vfslist != NULL) 366 usage(); 367 368 rmslashes(*argv, *argv); 369 if (init_flags & MNT_UPDATE) { 370 mntfromname = NULL; 371 have_fstab = 0; 372 if ((mntbuf = getmntpt(*argv)) == NULL) 373 errx(1, "not currently mounted %s", *argv); 374 /* 375 * Only get the mntflags from fstab if both mntpoint 376 * and mntspec are identical. Also handle the special 377 * case where just '/' is mounted and 'spec' is not 378 * identical with the one from fstab ('/dev' is missing 379 * in the spec-string at boot-time). 380 */ 381 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 382 if (strcmp(fs->fs_spec, 383 mntbuf->f_mntfromname) == 0 && 384 strcmp(fs->fs_file, 385 mntbuf->f_mntonname) == 0) { 386 have_fstab = 1; 387 mntfromname = mntbuf->f_mntfromname; 388 } else if (argv[0][0] == '/' && 389 argv[0][1] == '\0') { 390 fs = getfsfile("/"); 391 have_fstab = 1; 392 mntfromname = fs->fs_spec; 393 } 394 } 395 if (have_fstab) { 396 options = update_options(options, fs->fs_mntops, 397 mntbuf->f_flags); 398 } else { 399 mntfromname = mntbuf->f_mntfromname; 400 options = update_options(options, NULL, 401 mntbuf->f_flags); 402 } 403 rval = mountfs(mntbuf->f_fstypename, mntfromname, 404 mntbuf->f_mntonname, init_flags, options, 0); 405 break; 406 } 407 if ((fs = getfsfile(*argv)) == NULL && 408 (fs = getfsspec(*argv)) == NULL) 409 errx(1, "%s: unknown special file or file system", 410 *argv); 411 if (BADTYPE(fs->fs_type)) 412 errx(1, "%s has unknown file system type", 413 *argv); 414 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 415 init_flags, options, fs->fs_mntops); 416 break; 417 case 2: 418 /* 419 * If -t flag has not been specified, the path cannot be 420 * found, spec contains either a ':' or a '@', then assume 421 * that an NFS file system is being specified ala Sun. 422 * Check if the hostname contains only allowed characters 423 * to reduce false positives. IPv6 addresses containing 424 * ':' will be correctly parsed only if the separator is '@'. 425 * The definition of a valid hostname is taken from RFC 1034. 426 */ 427 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL || 428 (ep = strchr(argv[0], ':')) != NULL)) { 429 if (*ep == '@') { 430 cp = ep + 1; 431 ep = cp + strlen(cp); 432 } else 433 cp = argv[0]; 434 while (cp != ep) { 435 if (!isdigit(*cp) && !isalpha(*cp) && 436 *cp != '.' && *cp != '-' && *cp != ':') 437 break; 438 cp++; 439 } 440 if (cp == ep) 441 vfstype = "nfs"; 442 } 443 rval = mountfs(vfstype, 444 argv[0], argv[1], init_flags, options, NULL); 445 break; 446 default: 447 usage(); 448 /* NOTREACHED */ 449 } 450 451 /* 452 * If the mount was successfully, and done by root, tell mountd the 453 * good news. 454 */ 455 if (rval == 0 && getuid() == 0) 456 restart_mountd(); 457 458 exit(rval); 459} 460 461int 462ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize) 463{ 464 char realfsfile[PATH_MAX]; 465 int i; 466 467 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 468 /* the root file system can always be remounted */ 469 return (0); 470 471 /* The user may have specified a symlink in fstab, resolve the path */ 472 if (realpath(fs->fs_file, realfsfile) == NULL) { 473 /* Cannot resolve the path, use original one */ 474 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile)); 475 } 476 477 for (i = mntsize - 1; i >= 0; --i) 478 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 && 479 (!isremountable(fs->fs_vfstype) || 480 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)) 481 return (1); 482 return (0); 483} 484 485int 486isremountable(const char *vfsname) 487{ 488 const char **cp; 489 490 for (cp = remountable_fs_names; *cp; cp++) 491 if (strcmp(*cp, vfsname) == 0) 492 return (1); 493 return (0); 494} 495 496int 497hasopt(const char *mntopts, const char *option) 498{ 499 int negative, found; 500 char *opt, *optbuf; 501 502 if (option[0] == 'n' && option[1] == 'o') { 503 negative = 1; 504 option += 2; 505 } else 506 negative = 0; 507 optbuf = strdup(mntopts); 508 found = 0; 509 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 510 if (opt[0] == 'n' && opt[1] == 'o') { 511 if (!strcasecmp(opt + 2, option)) 512 found = negative; 513 } else if (!strcasecmp(opt, option)) 514 found = !negative; 515 } 516 free(optbuf); 517 return (found); 518} 519 520static void 521append_arg(struct cpa *sa, char *arg) 522{ 523 if (sa->c + 1 == sa->sz) { 524 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2; 525 sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz); 526 if (sa->a == NULL) 527 errx(1, "realloc failed"); 528 } 529 sa->a[++sa->c] = arg; 530} 531 532int 533mountfs(const char *vfstype, const char *spec, const char *name, int flags, 534 const char *options, const char *mntopts) 535{ 536 struct statfs sf; 537 int i, ret; 538 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; 539 static struct cpa mnt_argv; 540 541 /* resolve the mountpoint with realpath(3) */
| 41 42#include <sys/param.h> 43#include <sys/mount.h> 44#include <sys/stat.h> 45#include <sys/wait.h> 46 47#include <ctype.h> 48#include <err.h> 49#include <errno.h> 50#include <fstab.h> 51#include <paths.h> 52#include <pwd.h> 53#include <signal.h> 54#include <stdint.h> 55#include <stdio.h> 56#include <stdlib.h> 57#include <string.h> 58#include <unistd.h> 59#include <libutil.h> 60 61#include "extern.h" 62#include "mntopts.h" 63#include "pathnames.h" 64 65/* `meta' options */ 66#define MOUNT_META_OPTION_FSTAB "fstab" 67#define MOUNT_META_OPTION_CURRENT "current" 68 69static int debug, fstab_style, verbose; 70 71struct cpa { 72 char **a; 73 ssize_t sz; 74 int c; 75}; 76 77char *catopt(char *, const char *); 78struct statfs *getmntpt(const char *); 79int hasopt(const char *, const char *); 80int ismounted(struct fstab *, struct statfs *, int); 81int isremountable(const char *); 82void mangle(char *, struct cpa *); 83char *update_options(char *, char *, int); 84int mountfs(const char *, const char *, const char *, 85 int, const char *, const char *); 86void remopt(char *, const char *); 87void prmount(struct statfs *); 88void putfsent(struct statfs *); 89void usage(void); 90char *flags2opts(int); 91 92/* Map from mount options to printable formats. */ 93static struct opt { 94 uint64_t o_opt; 95 const char *o_name; 96} optnames[] = { 97 { MNT_ASYNC, "asynchronous" }, 98 { MNT_EXPORTED, "NFS exported" }, 99 { MNT_LOCAL, "local" }, 100 { MNT_NOATIME, "noatime" }, 101 { MNT_NOEXEC, "noexec" }, 102 { MNT_NOSUID, "nosuid" }, 103 { MNT_NOSYMFOLLOW, "nosymfollow" }, 104 { MNT_QUOTA, "with quotas" }, 105 { MNT_RDONLY, "read-only" }, 106 { MNT_SYNCHRONOUS, "synchronous" }, 107 { MNT_UNION, "union" }, 108 { MNT_NOCLUSTERR, "noclusterr" }, 109 { MNT_NOCLUSTERW, "noclusterw" }, 110 { MNT_SUIDDIR, "suiddir" }, 111 { MNT_SOFTDEP, "soft-updates" }, 112 { MNT_SUJ, "journaled soft-updates" }, 113 { MNT_MULTILABEL, "multilabel" }, 114 { MNT_ACLS, "acls" }, 115 { MNT_NFS4ACLS, "nfsv4acls" }, 116 { MNT_GJOURNAL, "gjournal" }, 117 { 0, NULL } 118}; 119 120/* 121 * List of VFS types that can be remounted without becoming mounted on top 122 * of each other. 123 * XXX Is this list correct? 124 */ 125static const char * 126remountable_fs_names[] = { 127 "ufs", "ffs", "ext2fs", 128 0 129}; 130 131static const char userquotaeq[] = "userquota="; 132static const char groupquotaeq[] = "groupquota="; 133 134static char *mountprog = NULL; 135 136static int 137use_mountprog(const char *vfstype) 138{ 139 /* XXX: We need to get away from implementing external mount 140 * programs for every filesystem, and move towards having 141 * each filesystem properly implement the nmount() system call. 142 */ 143 unsigned int i; 144 const char *fs[] = { 145 "cd9660", "mfs", "msdosfs", "nfs", "ntfs", 146 "nwfs", "nullfs", "oldnfs", "portalfs", "smbfs", "udf", "unionfs", 147 NULL 148 }; 149 150 if (mountprog != NULL) 151 return (1); 152 153 for (i = 0; fs[i] != NULL; ++i) { 154 if (strcmp(vfstype, fs[i]) == 0) 155 return (1); 156 } 157 158 return (0); 159} 160 161static int 162exec_mountprog(const char *name, const char *execname, char *const argv[]) 163{ 164 pid_t pid; 165 int status; 166 167 switch (pid = fork()) { 168 case -1: /* Error. */ 169 warn("fork"); 170 exit (1); 171 case 0: /* Child. */ 172 /* Go find an executable. */ 173 execvP(execname, _PATH_SYSPATH, argv); 174 if (errno == ENOENT) { 175 warn("exec %s not found", execname); 176 if (execname[0] != '/') { 177 warnx("in path: %s", _PATH_SYSPATH); 178 } 179 } 180 exit(1); 181 default: /* Parent. */ 182 if (waitpid(pid, &status, 0) < 0) { 183 warn("waitpid"); 184 return (1); 185 } 186 187 if (WIFEXITED(status)) { 188 if (WEXITSTATUS(status) != 0) 189 return (WEXITSTATUS(status)); 190 } else if (WIFSIGNALED(status)) { 191 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 192 return (1); 193 } 194 break; 195 } 196 197 return (0); 198} 199 200static int 201specified_ro(const char *arg) 202{ 203 char *optbuf, *opt; 204 int ret = 0; 205 206 optbuf = strdup(arg); 207 if (optbuf == NULL) 208 err(1, NULL); 209 210 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 211 if (strcmp(opt, "ro") == 0) { 212 ret = 1; 213 break; 214 } 215 } 216 free(optbuf); 217 return (ret); 218} 219 220static void 221restart_mountd(void) 222{ 223 struct pidfh *pfh; 224 pid_t mountdpid; 225 226 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid); 227 if (pfh != NULL) { 228 /* Mountd is not running. */ 229 pidfile_remove(pfh); 230 return; 231 } 232 if (errno != EEXIST) { 233 /* Cannot open pidfile for some reason. */ 234 return; 235 } 236 /* We have mountd(8) PID in mountdpid varible, let's signal it. */ 237 if (kill(mountdpid, SIGHUP) == -1) 238 err(1, "signal mountd"); 239} 240 241int 242main(int argc, char *argv[]) 243{ 244 const char *mntfromname, **vfslist, *vfstype; 245 struct fstab *fs; 246 struct statfs *mntbuf; 247 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro; 248 char *cp, *ep, *options; 249 250 all = init_flags = late = 0; 251 ro = 0; 252 options = NULL; 253 vfslist = NULL; 254 vfstype = "ufs"; 255 while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1) 256 switch (ch) { 257 case 'a': 258 all = 1; 259 break; 260 case 'd': 261 debug = 1; 262 break; 263 case 'F': 264 setfstab(optarg); 265 break; 266 case 'f': 267 init_flags |= MNT_FORCE; 268 break; 269 case 'l': 270 late = 1; 271 break; 272 case 'o': 273 if (*optarg) { 274 options = catopt(options, optarg); 275 if (specified_ro(optarg)) 276 ro = 1; 277 } 278 break; 279 case 'p': 280 fstab_style = 1; 281 verbose = 1; 282 break; 283 case 'r': 284 options = catopt(options, "ro"); 285 ro = 1; 286 break; 287 case 't': 288 if (vfslist != NULL) 289 errx(1, "only one -t option may be specified"); 290 vfslist = makevfslist(optarg); 291 vfstype = optarg; 292 break; 293 case 'u': 294 init_flags |= MNT_UPDATE; 295 break; 296 case 'v': 297 verbose = 1; 298 break; 299 case 'w': 300 options = catopt(options, "noro"); 301 break; 302 case '?': 303 default: 304 usage(); 305 /* NOTREACHED */ 306 } 307 argc -= optind; 308 argv += optind; 309 310#define BADTYPE(type) \ 311 (strcmp(type, FSTAB_RO) && \ 312 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 313 314 if ((init_flags & MNT_UPDATE) && (ro == 0)) 315 options = catopt(options, "noro"); 316 317 rval = 0; 318 switch (argc) { 319 case 0: 320 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 321 err(1, "getmntinfo"); 322 if (all) { 323 while ((fs = getfsent()) != NULL) { 324 if (BADTYPE(fs->fs_type)) 325 continue; 326 if (checkvfsname(fs->fs_vfstype, vfslist)) 327 continue; 328 if (hasopt(fs->fs_mntops, "noauto")) 329 continue; 330 if (hasopt(fs->fs_mntops, "late") && !late) 331 continue; 332 if (hasopt(fs->fs_mntops, "failok")) 333 failok = 1; 334 else 335 failok = 0; 336 if (!(init_flags & MNT_UPDATE) && 337 ismounted(fs, mntbuf, mntsize)) 338 continue; 339 options = update_options(options, fs->fs_mntops, 340 mntbuf->f_flags); 341 if (mountfs(fs->fs_vfstype, fs->fs_spec, 342 fs->fs_file, init_flags, options, 343 fs->fs_mntops) && !failok) 344 rval = 1; 345 } 346 } else if (fstab_style) { 347 for (i = 0; i < mntsize; i++) { 348 if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 349 continue; 350 putfsent(&mntbuf[i]); 351 } 352 } else { 353 for (i = 0; i < mntsize; i++) { 354 if (checkvfsname(mntbuf[i].f_fstypename, 355 vfslist)) 356 continue; 357 if (!verbose && 358 (mntbuf[i].f_flags & MNT_IGNORE) != 0) 359 continue; 360 prmount(&mntbuf[i]); 361 } 362 } 363 exit(rval); 364 case 1: 365 if (vfslist != NULL) 366 usage(); 367 368 rmslashes(*argv, *argv); 369 if (init_flags & MNT_UPDATE) { 370 mntfromname = NULL; 371 have_fstab = 0; 372 if ((mntbuf = getmntpt(*argv)) == NULL) 373 errx(1, "not currently mounted %s", *argv); 374 /* 375 * Only get the mntflags from fstab if both mntpoint 376 * and mntspec are identical. Also handle the special 377 * case where just '/' is mounted and 'spec' is not 378 * identical with the one from fstab ('/dev' is missing 379 * in the spec-string at boot-time). 380 */ 381 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 382 if (strcmp(fs->fs_spec, 383 mntbuf->f_mntfromname) == 0 && 384 strcmp(fs->fs_file, 385 mntbuf->f_mntonname) == 0) { 386 have_fstab = 1; 387 mntfromname = mntbuf->f_mntfromname; 388 } else if (argv[0][0] == '/' && 389 argv[0][1] == '\0') { 390 fs = getfsfile("/"); 391 have_fstab = 1; 392 mntfromname = fs->fs_spec; 393 } 394 } 395 if (have_fstab) { 396 options = update_options(options, fs->fs_mntops, 397 mntbuf->f_flags); 398 } else { 399 mntfromname = mntbuf->f_mntfromname; 400 options = update_options(options, NULL, 401 mntbuf->f_flags); 402 } 403 rval = mountfs(mntbuf->f_fstypename, mntfromname, 404 mntbuf->f_mntonname, init_flags, options, 0); 405 break; 406 } 407 if ((fs = getfsfile(*argv)) == NULL && 408 (fs = getfsspec(*argv)) == NULL) 409 errx(1, "%s: unknown special file or file system", 410 *argv); 411 if (BADTYPE(fs->fs_type)) 412 errx(1, "%s has unknown file system type", 413 *argv); 414 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 415 init_flags, options, fs->fs_mntops); 416 break; 417 case 2: 418 /* 419 * If -t flag has not been specified, the path cannot be 420 * found, spec contains either a ':' or a '@', then assume 421 * that an NFS file system is being specified ala Sun. 422 * Check if the hostname contains only allowed characters 423 * to reduce false positives. IPv6 addresses containing 424 * ':' will be correctly parsed only if the separator is '@'. 425 * The definition of a valid hostname is taken from RFC 1034. 426 */ 427 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL || 428 (ep = strchr(argv[0], ':')) != NULL)) { 429 if (*ep == '@') { 430 cp = ep + 1; 431 ep = cp + strlen(cp); 432 } else 433 cp = argv[0]; 434 while (cp != ep) { 435 if (!isdigit(*cp) && !isalpha(*cp) && 436 *cp != '.' && *cp != '-' && *cp != ':') 437 break; 438 cp++; 439 } 440 if (cp == ep) 441 vfstype = "nfs"; 442 } 443 rval = mountfs(vfstype, 444 argv[0], argv[1], init_flags, options, NULL); 445 break; 446 default: 447 usage(); 448 /* NOTREACHED */ 449 } 450 451 /* 452 * If the mount was successfully, and done by root, tell mountd the 453 * good news. 454 */ 455 if (rval == 0 && getuid() == 0) 456 restart_mountd(); 457 458 exit(rval); 459} 460 461int 462ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize) 463{ 464 char realfsfile[PATH_MAX]; 465 int i; 466 467 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 468 /* the root file system can always be remounted */ 469 return (0); 470 471 /* The user may have specified a symlink in fstab, resolve the path */ 472 if (realpath(fs->fs_file, realfsfile) == NULL) { 473 /* Cannot resolve the path, use original one */ 474 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile)); 475 } 476 477 for (i = mntsize - 1; i >= 0; --i) 478 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 && 479 (!isremountable(fs->fs_vfstype) || 480 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)) 481 return (1); 482 return (0); 483} 484 485int 486isremountable(const char *vfsname) 487{ 488 const char **cp; 489 490 for (cp = remountable_fs_names; *cp; cp++) 491 if (strcmp(*cp, vfsname) == 0) 492 return (1); 493 return (0); 494} 495 496int 497hasopt(const char *mntopts, const char *option) 498{ 499 int negative, found; 500 char *opt, *optbuf; 501 502 if (option[0] == 'n' && option[1] == 'o') { 503 negative = 1; 504 option += 2; 505 } else 506 negative = 0; 507 optbuf = strdup(mntopts); 508 found = 0; 509 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 510 if (opt[0] == 'n' && opt[1] == 'o') { 511 if (!strcasecmp(opt + 2, option)) 512 found = negative; 513 } else if (!strcasecmp(opt, option)) 514 found = !negative; 515 } 516 free(optbuf); 517 return (found); 518} 519 520static void 521append_arg(struct cpa *sa, char *arg) 522{ 523 if (sa->c + 1 == sa->sz) { 524 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2; 525 sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz); 526 if (sa->a == NULL) 527 errx(1, "realloc failed"); 528 } 529 sa->a[++sa->c] = arg; 530} 531 532int 533mountfs(const char *vfstype, const char *spec, const char *name, int flags, 534 const char *options, const char *mntopts) 535{ 536 struct statfs sf; 537 int i, ret; 538 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; 539 static struct cpa mnt_argv; 540 541 /* resolve the mountpoint with realpath(3) */
|
543 name = mntpath; 544 545 if (mntopts == NULL) 546 mntopts = ""; 547 optbuf = catopt(strdup(mntopts), options); 548 549 if (strcmp(name, "/") == 0) 550 flags |= MNT_UPDATE; 551 if (flags & MNT_FORCE) 552 optbuf = catopt(optbuf, "force"); 553 if (flags & MNT_RDONLY) 554 optbuf = catopt(optbuf, "ro"); 555 /* 556 * XXX 557 * The mount_mfs (newfs) command uses -o to select the 558 * optimization mode. We don't pass the default "-o rw" 559 * for that reason. 560 */ 561 if (flags & MNT_UPDATE) 562 optbuf = catopt(optbuf, "update"); 563 564 /* Compatibility glue. */ 565 if (strcmp(vfstype, "msdos") == 0) { 566 warnx( 567 "Using \"-t msdosfs\", since \"-t msdos\" is deprecated."); 568 vfstype = "msdosfs"; 569 } 570 571 /* Construct the name of the appropriate mount command */ 572 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 573 574 mnt_argv.c = -1; 575 append_arg(&mnt_argv, execname); 576 mangle(optbuf, &mnt_argv); 577 if (mountprog != NULL) 578 strcpy(execname, mountprog); 579 580 append_arg(&mnt_argv, strdup(spec)); 581 append_arg(&mnt_argv, strdup(name)); 582 append_arg(&mnt_argv, NULL); 583 584 if (debug) { 585 if (use_mountprog(vfstype)) 586 printf("exec: %s", execname); 587 else 588 printf("mount -t %s", vfstype); 589 for (i = 1; i < mnt_argv.c; i++) 590 (void)printf(" %s", mnt_argv.a[i]); 591 (void)printf("\n"); 592 free(optbuf); 593 free(mountprog); 594 mountprog = NULL; 595 return (0); 596 } 597 598 if (use_mountprog(vfstype)) { 599 ret = exec_mountprog(name, execname, mnt_argv.a); 600 } else { 601 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a); 602 } 603 604 free(optbuf); 605 free(mountprog); 606 mountprog = NULL; 607 608 if (verbose) { 609 if (statfs(name, &sf) < 0) { 610 warn("statfs %s", name); 611 return (1); 612 } 613 if (fstab_style) 614 putfsent(&sf); 615 else 616 prmount(&sf); 617 } 618 619 return (ret); 620} 621 622void 623prmount(struct statfs *sfp) 624{ 625 uint64_t flags; 626 unsigned int i; 627 struct opt *o; 628 struct passwd *pw; 629 630 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 631 sfp->f_fstypename); 632 633 flags = sfp->f_flags & MNT_VISFLAGMASK; 634 for (o = optnames; flags != 0 && o->o_opt != 0; o++) 635 if (flags & o->o_opt) { 636 (void)printf(", %s", o->o_name); 637 flags &= ~o->o_opt; 638 } 639 /* 640 * Inform when file system is mounted by an unprivileged user 641 * or privileged non-root user. 642 */ 643 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 644 (void)printf(", mounted by "); 645 if ((pw = getpwuid(sfp->f_owner)) != NULL) 646 (void)printf("%s", pw->pw_name); 647 else 648 (void)printf("%d", sfp->f_owner); 649 } 650 if (verbose) { 651 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 652 (void)printf(", writes: sync %ju async %ju", 653 (uintmax_t)sfp->f_syncwrites, 654 (uintmax_t)sfp->f_asyncwrites); 655 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 656 (void)printf(", reads: sync %ju async %ju", 657 (uintmax_t)sfp->f_syncreads, 658 (uintmax_t)sfp->f_asyncreads); 659 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 660 printf(", fsid "); 661 for (i = 0; i < sizeof(sfp->f_fsid); i++) 662 printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 663 } 664 } 665 (void)printf(")\n"); 666} 667 668struct statfs * 669getmntpt(const char *name) 670{ 671 struct statfs *mntbuf; 672 int i, mntsize; 673 674 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 675 for (i = mntsize - 1; i >= 0; i--) { 676 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 677 strcmp(mntbuf[i].f_mntonname, name) == 0) 678 return (&mntbuf[i]); 679 } 680 return (NULL); 681} 682 683char * 684catopt(char *s0, const char *s1) 685{ 686 size_t i; 687 char *cp; 688 689 if (s1 == NULL || *s1 == '\0') 690 return (s0); 691 692 if (s0 && *s0) { 693 i = strlen(s0) + strlen(s1) + 1 + 1; 694 if ((cp = malloc(i)) == NULL) 695 errx(1, "malloc failed"); 696 (void)snprintf(cp, i, "%s,%s", s0, s1); 697 } else 698 cp = strdup(s1); 699 700 if (s0) 701 free(s0); 702 return (cp); 703} 704 705void 706mangle(char *options, struct cpa *a) 707{ 708 char *p, *s, *val; 709 710 for (s = options; (p = strsep(&s, ",")) != NULL;) 711 if (*p != '\0') { 712 if (strcmp(p, "noauto") == 0) { 713 /* 714 * Do not pass noauto option to nmount(). 715 * or external mount program. noauto is 716 * only used to prevent mounting a filesystem 717 * when 'mount -a' is specified, and is 718 * not a real mount option. 719 */ 720 continue; 721 } else if (strcmp(p, "late") == 0) { 722 /* 723 * "late" is used to prevent certain file 724 * systems from being mounted before late 725 * in the boot cycle; for instance, 726 * loopback NFS mounts can't be mounted 727 * before mountd starts. 728 */ 729 continue; 730 } else if (strcmp(p, "failok") == 0) { 731 /* 732 * "failok" is used to prevent certain file 733 * systems from being causing the system to 734 * drop into single user mode in the boot 735 * cycle, and is not a real mount option. 736 */ 737 continue; 738 } else if (strncmp(p, "mountprog", 9) == 0) { 739 /* 740 * "mountprog" is used to force the use of 741 * userland mount programs. 742 */ 743 val = strchr(p, '='); 744 if (val != NULL) { 745 ++val; 746 if (*val != '\0') 747 mountprog = strdup(val); 748 } 749 750 if (mountprog == NULL) { 751 errx(1, "Need value for -o mountprog"); 752 } 753 continue; 754 } else if (strcmp(p, "userquota") == 0) { 755 continue; 756 } else if (strncmp(p, userquotaeq, 757 sizeof(userquotaeq) - 1) == 0) { 758 continue; 759 } else if (strcmp(p, "groupquota") == 0) { 760 continue; 761 } else if (strncmp(p, groupquotaeq, 762 sizeof(groupquotaeq) - 1) == 0) { 763 continue; 764 } else if (*p == '-') { 765 append_arg(a, p); 766 p = strchr(p, '='); 767 if (p != NULL) { 768 *p = '\0'; 769 append_arg(a, p + 1); 770 } 771 } else { 772 append_arg(a, strdup("-o")); 773 append_arg(a, p); 774 } 775 } 776} 777 778 779char * 780update_options(char *opts, char *fstab, int curflags) 781{ 782 char *o, *p; 783 char *cur; 784 char *expopt, *newopt, *tmpopt; 785 786 if (opts == NULL) 787 return (strdup("")); 788 789 /* remove meta options from list */ 790 remopt(fstab, MOUNT_META_OPTION_FSTAB); 791 remopt(fstab, MOUNT_META_OPTION_CURRENT); 792 cur = flags2opts(curflags); 793 794 /* 795 * Expand all meta-options passed to us first. 796 */ 797 expopt = NULL; 798 for (p = opts; (o = strsep(&p, ",")) != NULL;) { 799 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 800 expopt = catopt(expopt, fstab); 801 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 802 expopt = catopt(expopt, cur); 803 else 804 expopt = catopt(expopt, o); 805 } 806 free(cur); 807 free(opts); 808 809 /* 810 * Remove previous contradictory arguments. Given option "foo" we 811 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 812 * and "foo" - so we can deal with possible options like "notice". 813 */ 814 newopt = NULL; 815 for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 816 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 817 errx(1, "malloc failed"); 818 819 strcpy(tmpopt, "no"); 820 strcat(tmpopt, o); 821 remopt(newopt, tmpopt); 822 free(tmpopt); 823 824 if (strncmp("no", o, 2) == 0) 825 remopt(newopt, o+2); 826 827 newopt = catopt(newopt, o); 828 } 829 free(expopt); 830 831 return (newopt); 832} 833 834void 835remopt(char *string, const char *opt) 836{ 837 char *o, *p, *r; 838 839 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 840 return; 841 842 r = string; 843 844 for (p = string; (o = strsep(&p, ",")) != NULL;) { 845 if (strcmp(opt, o) != 0) { 846 if (*r == ',' && *o != '\0') 847 r++; 848 while ((*r++ = *o++) != '\0') 849 ; 850 *--r = ','; 851 } 852 } 853 *r = '\0'; 854} 855 856void 857usage(void) 858{ 859 860 (void)fprintf(stderr, "%s\n%s\n%s\n", 861"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 862" mount [-dfpruvw] special | node", 863" mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 864 exit(1); 865} 866 867void 868putfsent(struct statfs *ent) 869{ 870 struct fstab *fst; 871 char *opts, *rw; 872 int l; 873 874 opts = NULL; 875 /* flags2opts() doesn't return the "rw" option. */ 876 if ((ent->f_flags & MNT_RDONLY) != 0) 877 rw = NULL; 878 else 879 rw = catopt(NULL, "rw"); 880 881 opts = flags2opts(ent->f_flags); 882 opts = catopt(rw, opts); 883 884 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 || 885 strncmp(ent->f_mntfromname, "<above>", 7) == 0) { 886 strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8) 887 +1)); 888 } 889 890 l = strlen(ent->f_mntfromname); 891 printf("%s%s%s%s", ent->f_mntfromname, 892 l < 8 ? "\t" : "", 893 l < 16 ? "\t" : "", 894 l < 24 ? "\t" : " "); 895 l = strlen(ent->f_mntonname); 896 printf("%s%s%s%s", ent->f_mntonname, 897 l < 8 ? "\t" : "", 898 l < 16 ? "\t" : "", 899 l < 24 ? "\t" : " "); 900 printf("%s\t", ent->f_fstypename); 901 l = strlen(opts); 902 printf("%s%s", opts, 903 l < 8 ? "\t" : " "); 904 free(opts); 905 906 if ((fst = getfsspec(ent->f_mntfromname))) 907 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 908 else if ((fst = getfsfile(ent->f_mntonname))) 909 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 910 else if (strcmp(ent->f_fstypename, "ufs") == 0) { 911 if (strcmp(ent->f_mntonname, "/") == 0) 912 printf("\t1 1\n"); 913 else 914 printf("\t2 2\n"); 915 } else 916 printf("\t0 0\n"); 917} 918 919 920char * 921flags2opts(int flags) 922{ 923 char *res; 924 925 res = NULL; 926 927 if (flags & MNT_RDONLY) res = catopt(res, "ro"); 928 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 929 if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 930 if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 931 if (flags & MNT_UNION) res = catopt(res, "union"); 932 if (flags & MNT_ASYNC) res = catopt(res, "async"); 933 if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 934 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 935 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 936 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 937 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 938 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 939 if (flags & MNT_ACLS) res = catopt(res, "acls"); 940 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls"); 941 942 return (res); 943}
| 546 name = mntpath; 547 548 if (mntopts == NULL) 549 mntopts = ""; 550 optbuf = catopt(strdup(mntopts), options); 551 552 if (strcmp(name, "/") == 0) 553 flags |= MNT_UPDATE; 554 if (flags & MNT_FORCE) 555 optbuf = catopt(optbuf, "force"); 556 if (flags & MNT_RDONLY) 557 optbuf = catopt(optbuf, "ro"); 558 /* 559 * XXX 560 * The mount_mfs (newfs) command uses -o to select the 561 * optimization mode. We don't pass the default "-o rw" 562 * for that reason. 563 */ 564 if (flags & MNT_UPDATE) 565 optbuf = catopt(optbuf, "update"); 566 567 /* Compatibility glue. */ 568 if (strcmp(vfstype, "msdos") == 0) { 569 warnx( 570 "Using \"-t msdosfs\", since \"-t msdos\" is deprecated."); 571 vfstype = "msdosfs"; 572 } 573 574 /* Construct the name of the appropriate mount command */ 575 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 576 577 mnt_argv.c = -1; 578 append_arg(&mnt_argv, execname); 579 mangle(optbuf, &mnt_argv); 580 if (mountprog != NULL) 581 strcpy(execname, mountprog); 582 583 append_arg(&mnt_argv, strdup(spec)); 584 append_arg(&mnt_argv, strdup(name)); 585 append_arg(&mnt_argv, NULL); 586 587 if (debug) { 588 if (use_mountprog(vfstype)) 589 printf("exec: %s", execname); 590 else 591 printf("mount -t %s", vfstype); 592 for (i = 1; i < mnt_argv.c; i++) 593 (void)printf(" %s", mnt_argv.a[i]); 594 (void)printf("\n"); 595 free(optbuf); 596 free(mountprog); 597 mountprog = NULL; 598 return (0); 599 } 600 601 if (use_mountprog(vfstype)) { 602 ret = exec_mountprog(name, execname, mnt_argv.a); 603 } else { 604 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a); 605 } 606 607 free(optbuf); 608 free(mountprog); 609 mountprog = NULL; 610 611 if (verbose) { 612 if (statfs(name, &sf) < 0) { 613 warn("statfs %s", name); 614 return (1); 615 } 616 if (fstab_style) 617 putfsent(&sf); 618 else 619 prmount(&sf); 620 } 621 622 return (ret); 623} 624 625void 626prmount(struct statfs *sfp) 627{ 628 uint64_t flags; 629 unsigned int i; 630 struct opt *o; 631 struct passwd *pw; 632 633 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 634 sfp->f_fstypename); 635 636 flags = sfp->f_flags & MNT_VISFLAGMASK; 637 for (o = optnames; flags != 0 && o->o_opt != 0; o++) 638 if (flags & o->o_opt) { 639 (void)printf(", %s", o->o_name); 640 flags &= ~o->o_opt; 641 } 642 /* 643 * Inform when file system is mounted by an unprivileged user 644 * or privileged non-root user. 645 */ 646 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 647 (void)printf(", mounted by "); 648 if ((pw = getpwuid(sfp->f_owner)) != NULL) 649 (void)printf("%s", pw->pw_name); 650 else 651 (void)printf("%d", sfp->f_owner); 652 } 653 if (verbose) { 654 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 655 (void)printf(", writes: sync %ju async %ju", 656 (uintmax_t)sfp->f_syncwrites, 657 (uintmax_t)sfp->f_asyncwrites); 658 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 659 (void)printf(", reads: sync %ju async %ju", 660 (uintmax_t)sfp->f_syncreads, 661 (uintmax_t)sfp->f_asyncreads); 662 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 663 printf(", fsid "); 664 for (i = 0; i < sizeof(sfp->f_fsid); i++) 665 printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 666 } 667 } 668 (void)printf(")\n"); 669} 670 671struct statfs * 672getmntpt(const char *name) 673{ 674 struct statfs *mntbuf; 675 int i, mntsize; 676 677 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 678 for (i = mntsize - 1; i >= 0; i--) { 679 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 680 strcmp(mntbuf[i].f_mntonname, name) == 0) 681 return (&mntbuf[i]); 682 } 683 return (NULL); 684} 685 686char * 687catopt(char *s0, const char *s1) 688{ 689 size_t i; 690 char *cp; 691 692 if (s1 == NULL || *s1 == '\0') 693 return (s0); 694 695 if (s0 && *s0) { 696 i = strlen(s0) + strlen(s1) + 1 + 1; 697 if ((cp = malloc(i)) == NULL) 698 errx(1, "malloc failed"); 699 (void)snprintf(cp, i, "%s,%s", s0, s1); 700 } else 701 cp = strdup(s1); 702 703 if (s0) 704 free(s0); 705 return (cp); 706} 707 708void 709mangle(char *options, struct cpa *a) 710{ 711 char *p, *s, *val; 712 713 for (s = options; (p = strsep(&s, ",")) != NULL;) 714 if (*p != '\0') { 715 if (strcmp(p, "noauto") == 0) { 716 /* 717 * Do not pass noauto option to nmount(). 718 * or external mount program. noauto is 719 * only used to prevent mounting a filesystem 720 * when 'mount -a' is specified, and is 721 * not a real mount option. 722 */ 723 continue; 724 } else if (strcmp(p, "late") == 0) { 725 /* 726 * "late" is used to prevent certain file 727 * systems from being mounted before late 728 * in the boot cycle; for instance, 729 * loopback NFS mounts can't be mounted 730 * before mountd starts. 731 */ 732 continue; 733 } else if (strcmp(p, "failok") == 0) { 734 /* 735 * "failok" is used to prevent certain file 736 * systems from being causing the system to 737 * drop into single user mode in the boot 738 * cycle, and is not a real mount option. 739 */ 740 continue; 741 } else if (strncmp(p, "mountprog", 9) == 0) { 742 /* 743 * "mountprog" is used to force the use of 744 * userland mount programs. 745 */ 746 val = strchr(p, '='); 747 if (val != NULL) { 748 ++val; 749 if (*val != '\0') 750 mountprog = strdup(val); 751 } 752 753 if (mountprog == NULL) { 754 errx(1, "Need value for -o mountprog"); 755 } 756 continue; 757 } else if (strcmp(p, "userquota") == 0) { 758 continue; 759 } else if (strncmp(p, userquotaeq, 760 sizeof(userquotaeq) - 1) == 0) { 761 continue; 762 } else if (strcmp(p, "groupquota") == 0) { 763 continue; 764 } else if (strncmp(p, groupquotaeq, 765 sizeof(groupquotaeq) - 1) == 0) { 766 continue; 767 } else if (*p == '-') { 768 append_arg(a, p); 769 p = strchr(p, '='); 770 if (p != NULL) { 771 *p = '\0'; 772 append_arg(a, p + 1); 773 } 774 } else { 775 append_arg(a, strdup("-o")); 776 append_arg(a, p); 777 } 778 } 779} 780 781 782char * 783update_options(char *opts, char *fstab, int curflags) 784{ 785 char *o, *p; 786 char *cur; 787 char *expopt, *newopt, *tmpopt; 788 789 if (opts == NULL) 790 return (strdup("")); 791 792 /* remove meta options from list */ 793 remopt(fstab, MOUNT_META_OPTION_FSTAB); 794 remopt(fstab, MOUNT_META_OPTION_CURRENT); 795 cur = flags2opts(curflags); 796 797 /* 798 * Expand all meta-options passed to us first. 799 */ 800 expopt = NULL; 801 for (p = opts; (o = strsep(&p, ",")) != NULL;) { 802 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 803 expopt = catopt(expopt, fstab); 804 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 805 expopt = catopt(expopt, cur); 806 else 807 expopt = catopt(expopt, o); 808 } 809 free(cur); 810 free(opts); 811 812 /* 813 * Remove previous contradictory arguments. Given option "foo" we 814 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 815 * and "foo" - so we can deal with possible options like "notice". 816 */ 817 newopt = NULL; 818 for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 819 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 820 errx(1, "malloc failed"); 821 822 strcpy(tmpopt, "no"); 823 strcat(tmpopt, o); 824 remopt(newopt, tmpopt); 825 free(tmpopt); 826 827 if (strncmp("no", o, 2) == 0) 828 remopt(newopt, o+2); 829 830 newopt = catopt(newopt, o); 831 } 832 free(expopt); 833 834 return (newopt); 835} 836 837void 838remopt(char *string, const char *opt) 839{ 840 char *o, *p, *r; 841 842 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 843 return; 844 845 r = string; 846 847 for (p = string; (o = strsep(&p, ",")) != NULL;) { 848 if (strcmp(opt, o) != 0) { 849 if (*r == ',' && *o != '\0') 850 r++; 851 while ((*r++ = *o++) != '\0') 852 ; 853 *--r = ','; 854 } 855 } 856 *r = '\0'; 857} 858 859void 860usage(void) 861{ 862 863 (void)fprintf(stderr, "%s\n%s\n%s\n", 864"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 865" mount [-dfpruvw] special | node", 866" mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 867 exit(1); 868} 869 870void 871putfsent(struct statfs *ent) 872{ 873 struct fstab *fst; 874 char *opts, *rw; 875 int l; 876 877 opts = NULL; 878 /* flags2opts() doesn't return the "rw" option. */ 879 if ((ent->f_flags & MNT_RDONLY) != 0) 880 rw = NULL; 881 else 882 rw = catopt(NULL, "rw"); 883 884 opts = flags2opts(ent->f_flags); 885 opts = catopt(rw, opts); 886 887 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 || 888 strncmp(ent->f_mntfromname, "<above>", 7) == 0) { 889 strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8) 890 +1)); 891 } 892 893 l = strlen(ent->f_mntfromname); 894 printf("%s%s%s%s", ent->f_mntfromname, 895 l < 8 ? "\t" : "", 896 l < 16 ? "\t" : "", 897 l < 24 ? "\t" : " "); 898 l = strlen(ent->f_mntonname); 899 printf("%s%s%s%s", ent->f_mntonname, 900 l < 8 ? "\t" : "", 901 l < 16 ? "\t" : "", 902 l < 24 ? "\t" : " "); 903 printf("%s\t", ent->f_fstypename); 904 l = strlen(opts); 905 printf("%s%s", opts, 906 l < 8 ? "\t" : " "); 907 free(opts); 908 909 if ((fst = getfsspec(ent->f_mntfromname))) 910 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 911 else if ((fst = getfsfile(ent->f_mntonname))) 912 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 913 else if (strcmp(ent->f_fstypename, "ufs") == 0) { 914 if (strcmp(ent->f_mntonname, "/") == 0) 915 printf("\t1 1\n"); 916 else 917 printf("\t2 2\n"); 918 } else 919 printf("\t0 0\n"); 920} 921 922 923char * 924flags2opts(int flags) 925{ 926 char *res; 927 928 res = NULL; 929 930 if (flags & MNT_RDONLY) res = catopt(res, "ro"); 931 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 932 if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 933 if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 934 if (flags & MNT_UNION) res = catopt(res, "union"); 935 if (flags & MNT_ASYNC) res = catopt(res, "async"); 936 if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 937 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 938 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 939 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 940 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 941 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 942 if (flags & MNT_ACLS) res = catopt(res, "acls"); 943 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls"); 944 945 return (res); 946}
|