10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1914Scasper * Common Development and Distribution License (the "License"). 6*1914Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 220Sstevel@tonic-gate /* All Rights Reserved */ 230Sstevel@tonic-gate 240Sstevel@tonic-gate 250Sstevel@tonic-gate /* 26*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 270Sstevel@tonic-gate * Use is subject to license terms. 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.82 */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <stdio.h> 33*1914Scasper #include <stdio_ext.h> 340Sstevel@tonic-gate #include <limits.h> 350Sstevel@tonic-gate #include <fcntl.h> 360Sstevel@tonic-gate #include <unistd.h> 370Sstevel@tonic-gate #include <stdlib.h> 380Sstevel@tonic-gate #include <string.h> 390Sstevel@tonic-gate #include <stdarg.h> 400Sstevel@tonic-gate #include <sys/types.h> 410Sstevel@tonic-gate #include <sys/stat.h> 420Sstevel@tonic-gate #include <sys/statvfs.h> 430Sstevel@tonic-gate #include <errno.h> 440Sstevel@tonic-gate #include <sys/mnttab.h> 450Sstevel@tonic-gate #include <sys/mntent.h> 460Sstevel@tonic-gate #include <sys/mount.h> 470Sstevel@tonic-gate #include <sys/vfstab.h> 480Sstevel@tonic-gate #include <sys/param.h> 490Sstevel@tonic-gate #include <sys/wait.h> 500Sstevel@tonic-gate #include <sys/signal.h> 510Sstevel@tonic-gate #include <sys/resource.h> 520Sstevel@tonic-gate #include <stropts.h> 530Sstevel@tonic-gate #include <sys/conf.h> 540Sstevel@tonic-gate #include <locale.h> 550Sstevel@tonic-gate #include "fslib.h" 560Sstevel@tonic-gate 570Sstevel@tonic-gate #define VFS_PATH "/usr/lib/fs" 580Sstevel@tonic-gate #define ALT_PATH "/etc/fs" 590Sstevel@tonic-gate #define REMOTE "/etc/dfs/fstypes" 600Sstevel@tonic-gate 610Sstevel@tonic-gate #define ARGV_MAX 16 620Sstevel@tonic-gate #define TIME_MAX 50 630Sstevel@tonic-gate #define FSTYPE_MAX 8 640Sstevel@tonic-gate #define REMOTE_MAX 64 650Sstevel@tonic-gate 660Sstevel@tonic-gate #define OLD 0 670Sstevel@tonic-gate #define NEW 1 680Sstevel@tonic-gate 690Sstevel@tonic-gate #define READONLY 0 700Sstevel@tonic-gate #define READWRITE 1 710Sstevel@tonic-gate #define SUID 2 720Sstevel@tonic-gate #define NOSUID 3 730Sstevel@tonic-gate #define SETUID 4 740Sstevel@tonic-gate #define NOSETUID 5 750Sstevel@tonic-gate #define DEVICES 6 760Sstevel@tonic-gate #define NODEVICES 7 770Sstevel@tonic-gate 780Sstevel@tonic-gate #define FORMAT "%a %b %e %H:%M:%S %Y\n" /* date time format */ 790Sstevel@tonic-gate /* a - abbreviated weekday name */ 800Sstevel@tonic-gate /* b - abbreviated month name */ 810Sstevel@tonic-gate /* e - day of month */ 820Sstevel@tonic-gate /* H - hour */ 830Sstevel@tonic-gate /* M - minute */ 840Sstevel@tonic-gate /* S - second */ 850Sstevel@tonic-gate /* Y - Year */ 860Sstevel@tonic-gate /* n - newline */ 870Sstevel@tonic-gate 880Sstevel@tonic-gate extern int optind; 890Sstevel@tonic-gate extern char *optarg; 900Sstevel@tonic-gate 910Sstevel@tonic-gate extern void usage(void); 920Sstevel@tonic-gate extern char *flags(char *, int); 930Sstevel@tonic-gate extern char *remote(char *, FILE *); 940Sstevel@tonic-gate extern char *default_fstype(char *); 950Sstevel@tonic-gate 960Sstevel@tonic-gate char *myopts[] = { 970Sstevel@tonic-gate MNTOPT_RO, 980Sstevel@tonic-gate MNTOPT_RW, 990Sstevel@tonic-gate MNTOPT_SUID, 1000Sstevel@tonic-gate MNTOPT_NOSUID, 1010Sstevel@tonic-gate MNTOPT_SETUID, 1020Sstevel@tonic-gate MNTOPT_NOSETUID, 1030Sstevel@tonic-gate MNTOPT_DEVICES, 1040Sstevel@tonic-gate MNTOPT_NODEVICES, 1050Sstevel@tonic-gate NULL 1060Sstevel@tonic-gate }; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate static char *myname; /* point to argv[0] */ 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* 1110Sstevel@tonic-gate * Set the limit to double the number of characters a user should be allowed to 1120Sstevel@tonic-gate * type in one line. 1130Sstevel@tonic-gate * This should cover the different shells, which don't use POSIX_MAX_INPUT, 1140Sstevel@tonic-gate * and should cover the case where a long option string can be in 1150Sstevel@tonic-gate * the /etc/vfstab file. 1160Sstevel@tonic-gate */ 1170Sstevel@tonic-gate char mntflags[(_POSIX_MAX_INPUT+1) * 2]; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate char realdir[MAXPATHLEN]; /* buffer for realpath() calls */ 1200Sstevel@tonic-gate char *vfstab = VFSTAB; 1210Sstevel@tonic-gate char *mnttab = MNTTAB; 1220Sstevel@tonic-gate char *specific_opts; /* holds specific mount options */ 1230Sstevel@tonic-gate char *generic_opts; /* holds generic mount options */ 1240Sstevel@tonic-gate int maxrun; 1250Sstevel@tonic-gate int nrun; 1260Sstevel@tonic-gate int lofscnt; /* presence of lofs prohibits parallel */ 1270Sstevel@tonic-gate /* mounting */ 1280Sstevel@tonic-gate int exitcode; 1290Sstevel@tonic-gate int aflg, cflg, fflg, Fflg, gflg, oflg, pflg, rflg, vflg, Vflg, mflg, Oflg, 1300Sstevel@tonic-gate dashflg, questflg, dflg, qflg; 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate /* 1330Sstevel@tonic-gate * Currently, mounting cachefs instances simultaneously uncovers various 1340Sstevel@tonic-gate * problems. For the short term, we serialize cachefs activity while we fix 1350Sstevel@tonic-gate * these cachefs bugs. 1360Sstevel@tonic-gate */ 1370Sstevel@tonic-gate #define CACHEFS_BUG 1380Sstevel@tonic-gate #ifdef CACHEFS_BUG 1390Sstevel@tonic-gate int cachefs_running; /* parallel cachefs not supported yet */ 1400Sstevel@tonic-gate #endif 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* 1430Sstevel@tonic-gate * Each vfsent_t describes a vfstab entry. It is used to manage and cleanup 1440Sstevel@tonic-gate * each child that performs the particular mount for the entry. 1450Sstevel@tonic-gate */ 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate typedef struct vfsent { 1480Sstevel@tonic-gate struct vfstab v; /* the vfstab entry */ 1490Sstevel@tonic-gate char *rpath; /* resolved pathname so far */ 1500Sstevel@tonic-gate int mlevel; /* how deep is this mount point */ 1510Sstevel@tonic-gate int order; /* vfstab serial order of this vfs */ 1520Sstevel@tonic-gate int flag; 1530Sstevel@tonic-gate pid_t pid; /* the pid of this mount process */ 1540Sstevel@tonic-gate int exitcode; /* process's exitcode */ 1550Sstevel@tonic-gate #define RDPIPE 0 1560Sstevel@tonic-gate #define WRPIPE 1 1570Sstevel@tonic-gate int sopipe[2]; /* pipe attached to child's stdout */ 1580Sstevel@tonic-gate int sepipe[2]; /* pipe attached to child's stderr */ 1590Sstevel@tonic-gate struct vfsent *next; /* used when in linked list */ 1600Sstevel@tonic-gate } vfsent_t; 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate #define VRPFAILED 0x01 /* most recent realpath failed on */ 1630Sstevel@tonic-gate /* this mount point */ 1640Sstevel@tonic-gate #define VNOTMOUNTED 0x02 /* mount point could not be mounted */ 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate vfsent_t *vfsll, *vfslltail; /* head and tail of the global */ 1670Sstevel@tonic-gate /* linked list of vfstab entries */ 1680Sstevel@tonic-gate vfsent_t **vfsarray; /* global array of vfsent_t's */ 1690Sstevel@tonic-gate int vfsarraysize; /* length of the list */ 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate /* 1720Sstevel@tonic-gate * This structure is used to build a linked list of 1730Sstevel@tonic-gate * mnttab structures from /etc/mnttab. 1740Sstevel@tonic-gate */ 1750Sstevel@tonic-gate typedef struct mountent { 1760Sstevel@tonic-gate struct extmnttab *ment; 1770Sstevel@tonic-gate int flag; 1780Sstevel@tonic-gate struct mountent *next; 1790Sstevel@tonic-gate } mountent_t; 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate #define MSORTED 0x1 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate static vfsent_t **make_vfsarray(char **, int); 1840Sstevel@tonic-gate static vfsent_t *new_vfsent(struct vfstab *, int); 1850Sstevel@tonic-gate static vfsent_t *getvfsall(char *, int); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate static void doexec(char *, char **); 1880Sstevel@tonic-gate static void nomem(); 1890Sstevel@tonic-gate static void cleanup(int); 1900Sstevel@tonic-gate static char *setrpath(vfsent_t *); 1910Sstevel@tonic-gate static int dowait(); 1920Sstevel@tonic-gate static int setup_iopipe(vfsent_t *); 1930Sstevel@tonic-gate static void setup_output(vfsent_t *); 1940Sstevel@tonic-gate static void doio(vfsent_t *); 1950Sstevel@tonic-gate static void do_mounts(); 1960Sstevel@tonic-gate static int parmount(char **, int, char *); 1970Sstevel@tonic-gate static int mlevelcmp(const void *, const void *); 1980Sstevel@tonic-gate static int mordercmp(const void *, const void *); 1990Sstevel@tonic-gate static int check_fields(char *, char *); 2000Sstevel@tonic-gate static int cleanupkid(pid_t, int); 2010Sstevel@tonic-gate static void print_mnttab(int, int); 2020Sstevel@tonic-gate static void vfserror(int, char *); 2030Sstevel@tonic-gate static void mnterror(int); 2040Sstevel@tonic-gate static int ignore(char *); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * This is /usr/sbin/mount: the generic command that in turn 2080Sstevel@tonic-gate * execs the appropriate /usr/lib/fs/{fstype}/mount. 2090Sstevel@tonic-gate * The -F flag and argument are NOT passed. 2100Sstevel@tonic-gate * If the usr file system is not mounted a duplicate copy 2110Sstevel@tonic-gate * can be found in /sbin and this version execs the 2120Sstevel@tonic-gate * appropriate /etc/fs/{fstype}/mount 2130Sstevel@tonic-gate * 2140Sstevel@tonic-gate * If the -F fstype, special or directory are missing, 2150Sstevel@tonic-gate * /etc/vfstab is searched to fill in the missing arguments. 2160Sstevel@tonic-gate * 2170Sstevel@tonic-gate * -V will print the built command on the stdout. 2180Sstevel@tonic-gate * It isn't passed either. 2190Sstevel@tonic-gate */ 220821Sdh145677 int 221821Sdh145677 main(int argc, char *argv[]) 2220Sstevel@tonic-gate { 2230Sstevel@tonic-gate char *special, /* argument of special/resource */ 2240Sstevel@tonic-gate *mountp, /* argument of mount directory */ 2250Sstevel@tonic-gate *fstype, /* wherein the fstype name is filled */ 2260Sstevel@tonic-gate *newargv[ARGV_MAX], /* arg list for specific command */ 2270Sstevel@tonic-gate *farg = NULL, *Farg = NULL; 2280Sstevel@tonic-gate int ii, ret, cc, fscnt; 2290Sstevel@tonic-gate struct stat64 stbuf; 2300Sstevel@tonic-gate struct vfstab vget, vref; 2310Sstevel@tonic-gate mode_t mode; 2320Sstevel@tonic-gate FILE *fd; 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 2370Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 2380Sstevel@tonic-gate #endif 2390Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate myname = strrchr(argv[0], '/'); 2420Sstevel@tonic-gate if (myname) 2430Sstevel@tonic-gate myname++; 2440Sstevel@tonic-gate else 2450Sstevel@tonic-gate myname = argv[0]; 2460Sstevel@tonic-gate if (myname == 0) myname = "path unknown"; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate /* Process the args. */ 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate while ((cc = getopt(argc, argv, "?acd:f:F:gmno:pqrvVO")) != -1) 2510Sstevel@tonic-gate switch (cc) { 2520Sstevel@tonic-gate case 'a': 2530Sstevel@tonic-gate aflg++; 2540Sstevel@tonic-gate break; 2550Sstevel@tonic-gate case 'c': 2560Sstevel@tonic-gate cflg++; 2570Sstevel@tonic-gate break; 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate #ifdef DEBUG 2600Sstevel@tonic-gate case 'd': 2610Sstevel@tonic-gate dflg = atoi(optarg); 2620Sstevel@tonic-gate break; 2630Sstevel@tonic-gate #endif 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate case 'f': 2660Sstevel@tonic-gate fflg++; 2670Sstevel@tonic-gate farg = optarg; 2680Sstevel@tonic-gate break; 2690Sstevel@tonic-gate case 'F': 2700Sstevel@tonic-gate Fflg++; 2710Sstevel@tonic-gate Farg = optarg; 2720Sstevel@tonic-gate break; 2730Sstevel@tonic-gate case 'g': 2740Sstevel@tonic-gate gflg++; 2750Sstevel@tonic-gate break; 2760Sstevel@tonic-gate case 'm': 2770Sstevel@tonic-gate mflg++; 2780Sstevel@tonic-gate break; /* do not update /etc/mnttab */ 2790Sstevel@tonic-gate case 'o': 2800Sstevel@tonic-gate oflg++; 2810Sstevel@tonic-gate if ((specific_opts = strdup(optarg)) == NULL) 2820Sstevel@tonic-gate nomem(); 2830Sstevel@tonic-gate break; /* fstype dependent options */ 2840Sstevel@tonic-gate case 'O': 2850Sstevel@tonic-gate Oflg++; 2860Sstevel@tonic-gate break; 2870Sstevel@tonic-gate case 'p': 2880Sstevel@tonic-gate pflg++; 2890Sstevel@tonic-gate break; 2900Sstevel@tonic-gate case 'q': 2910Sstevel@tonic-gate qflg++; 2920Sstevel@tonic-gate break; 2930Sstevel@tonic-gate case 'r': 2940Sstevel@tonic-gate rflg++; 2950Sstevel@tonic-gate generic_opts = "ro"; 2960Sstevel@tonic-gate break; 2970Sstevel@tonic-gate case 'v': 2980Sstevel@tonic-gate vflg++; 2990Sstevel@tonic-gate break; 3000Sstevel@tonic-gate case 'V': 3010Sstevel@tonic-gate Vflg++; 3020Sstevel@tonic-gate break; 3030Sstevel@tonic-gate case '?': 3040Sstevel@tonic-gate questflg++; 3050Sstevel@tonic-gate break; 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* copy '--' to specific */ 3090Sstevel@tonic-gate if (strcmp(argv[optind-1], "--") == 0) 3100Sstevel@tonic-gate dashflg++; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* option checking */ 3130Sstevel@tonic-gate /* more than two args not allowed if !aflg */ 3140Sstevel@tonic-gate if (!aflg && (argc - optind > 2)) 3150Sstevel@tonic-gate usage(); 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate /* pv mututally exclusive */ 3180Sstevel@tonic-gate if (pflg + vflg + aflg > 1) { 3190Sstevel@tonic-gate fprintf(stderr, gettext 3200Sstevel@tonic-gate ("%s: -a, -p, and -v are mutually exclusive\n"), 3210Sstevel@tonic-gate myname); 3220Sstevel@tonic-gate usage(); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * Can't have overlaying mounts on the same mount point during 3270Sstevel@tonic-gate * a parallel mount. 3280Sstevel@tonic-gate */ 3290Sstevel@tonic-gate if (aflg && Oflg) { 3300Sstevel@tonic-gate fprintf(stderr, gettext 3310Sstevel@tonic-gate ("%s: -a and -O are mutually exclusive\n"), myname); 3320Sstevel@tonic-gate usage(); 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate /* dfF mutually exclusive */ 3360Sstevel@tonic-gate if (fflg + Fflg > 1) { 3370Sstevel@tonic-gate fprintf(stderr, gettext 3380Sstevel@tonic-gate ("%s: More than one FSType specified\n"), myname); 3390Sstevel@tonic-gate usage(); 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate /* no arguments, only allow p,v,V or [F]? */ 3430Sstevel@tonic-gate if (!aflg && optind == argc) { 3440Sstevel@tonic-gate if (cflg || fflg || mflg || oflg || rflg || qflg) 3450Sstevel@tonic-gate usage(); 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate if (Fflg && !questflg) 3480Sstevel@tonic-gate usage(); 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate if (questflg) { 3510Sstevel@tonic-gate if (Fflg) { 3520Sstevel@tonic-gate newargv[2] = "-?"; 3530Sstevel@tonic-gate newargv[3] = NULL; 3540Sstevel@tonic-gate doexec(Farg, newargv); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate usage(); 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate if (questflg) 3610Sstevel@tonic-gate usage(); 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate /* one or two args, allow any but p,v */ 3640Sstevel@tonic-gate if (optind != argc && (pflg || vflg)) { 3650Sstevel@tonic-gate fprintf(stderr, 3660Sstevel@tonic-gate gettext("%s: Cannot use -p and -v with arguments\n"), myname); 3670Sstevel@tonic-gate usage(); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* if only reporting mnttab, generic prints mnttab and exits */ 3720Sstevel@tonic-gate if (!aflg && optind == argc) { 3730Sstevel@tonic-gate if (Vflg) { 3740Sstevel@tonic-gate printf("%s", myname); 3750Sstevel@tonic-gate if (pflg) 3760Sstevel@tonic-gate printf(" -p"); 3770Sstevel@tonic-gate if (vflg) 3780Sstevel@tonic-gate printf(" -v"); 3790Sstevel@tonic-gate printf("\n"); 3800Sstevel@tonic-gate exit(0); 3810Sstevel@tonic-gate } 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate print_mnttab(vflg, pflg); 3840Sstevel@tonic-gate exit(0); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate /* 3880Sstevel@tonic-gate * Get filesystem type here. If "-F FStype" is specified, use 3890Sstevel@tonic-gate * that fs type. Otherwise, determine the fs type from /etc/vfstab 3900Sstevel@tonic-gate * if the entry exists. Otherwise, determine the local or remote 3910Sstevel@tonic-gate * fs type from /etc/default/df or /etc/dfs/fstypes respectively. 3920Sstevel@tonic-gate */ 3930Sstevel@tonic-gate if (fflg) { 3940Sstevel@tonic-gate if ((strcmp(farg, "S51K") != 0) && 3950Sstevel@tonic-gate (strcmp(farg, "S52K") != 0)) { 3960Sstevel@tonic-gate fstype = farg; 3970Sstevel@tonic-gate } 3980Sstevel@tonic-gate else 3990Sstevel@tonic-gate fstype = "ufs"; 4000Sstevel@tonic-gate } else /* if (Fflg) */ 4010Sstevel@tonic-gate fstype = Farg; 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate fscnt = argc - optind; 4040Sstevel@tonic-gate if (aflg && (fscnt != 1)) 4050Sstevel@tonic-gate exit(parmount(argv + optind, fscnt, fstype)); 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate /* 4080Sstevel@tonic-gate * Then don't bother with the parallel over head. Everything 4090Sstevel@tonic-gate * from this point is simple/normal single execution. 4100Sstevel@tonic-gate */ 4110Sstevel@tonic-gate aflg = 0; 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate /* get special and/or mount-point from arg(s) */ 4140Sstevel@tonic-gate if (fscnt == 2) 4150Sstevel@tonic-gate special = argv[optind++]; 4160Sstevel@tonic-gate else 4170Sstevel@tonic-gate special = NULL; 4180Sstevel@tonic-gate if (optind < argc) 4190Sstevel@tonic-gate mountp = argv[optind++]; 4200Sstevel@tonic-gate else 4210Sstevel@tonic-gate mountp = NULL; 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* lookup only if we need to */ 4240Sstevel@tonic-gate if (fstype == NULL || specific_opts == NULL || special == NULL || 4250Sstevel@tonic-gate mountp == NULL) { 4260Sstevel@tonic-gate if ((fd = fopen(vfstab, "r")) == NULL) { 4270Sstevel@tonic-gate if (fstype == NULL || special == NULL || 4280Sstevel@tonic-gate mountp == NULL) { 4290Sstevel@tonic-gate fprintf(stderr, gettext( 4300Sstevel@tonic-gate "%s: Cannot open %s\n"), 4310Sstevel@tonic-gate myname, vfstab); 4320Sstevel@tonic-gate exit(1); 4330Sstevel@tonic-gate } else { 4340Sstevel@tonic-gate /* 4350Sstevel@tonic-gate * No vfstab, but we know what we want 4360Sstevel@tonic-gate * to mount. 4370Sstevel@tonic-gate */ 4380Sstevel@tonic-gate goto out; 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate vfsnull(&vref); 4420Sstevel@tonic-gate vref.vfs_special = special; 4430Sstevel@tonic-gate vref.vfs_mountp = mountp; 4440Sstevel@tonic-gate vref.vfs_fstype = fstype; 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate /* get a vfstab entry matching mountp or special */ 4470Sstevel@tonic-gate while ((ret = getvfsany(fd, &vget, &vref)) > 0) 4480Sstevel@tonic-gate vfserror(ret, vget.vfs_special); 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate /* if no entry and there was only one argument */ 4510Sstevel@tonic-gate /* then the argument could be the special */ 4520Sstevel@tonic-gate /* and not mount point as we thought earlier */ 4530Sstevel@tonic-gate if (ret == -1 && special == NULL) { 4540Sstevel@tonic-gate rewind(fd); 4550Sstevel@tonic-gate special = vref.vfs_special = mountp; 4560Sstevel@tonic-gate mountp = vref.vfs_mountp = NULL; 4570Sstevel@tonic-gate /* skip erroneous lines; they were reported above */ 4580Sstevel@tonic-gate while ((ret = getvfsany(fd, &vget, &vref)) > 0); 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate fclose(fd); 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate if (ret == 0) { 4640Sstevel@tonic-gate if (fstype == NULL) 4650Sstevel@tonic-gate fstype = vget.vfs_fstype; 4660Sstevel@tonic-gate if (special == NULL) 4670Sstevel@tonic-gate special = vget.vfs_special; 4680Sstevel@tonic-gate if (mountp == NULL) 4690Sstevel@tonic-gate mountp = vget.vfs_mountp; 4700Sstevel@tonic-gate if (oflg == 0 && vget.vfs_mntopts) { 4710Sstevel@tonic-gate oflg++; 4720Sstevel@tonic-gate specific_opts = vget.vfs_mntopts; 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate } else if (special == NULL) { 4750Sstevel@tonic-gate if (stat64(mountp, &stbuf) == -1) { 4760Sstevel@tonic-gate fprintf(stderr, gettext("%s: cannot stat %s\n"), 4770Sstevel@tonic-gate myname, mountp); 4780Sstevel@tonic-gate exit(2); 4790Sstevel@tonic-gate } 4800Sstevel@tonic-gate if (((mode = (stbuf.st_mode & S_IFMT)) == S_IFBLK) || 4810Sstevel@tonic-gate (mode == S_IFCHR)) { 4820Sstevel@tonic-gate fprintf(stderr, 4830Sstevel@tonic-gate gettext("%s: mount point cannot be determined\n"), 4840Sstevel@tonic-gate myname); 4850Sstevel@tonic-gate exit(1); 4860Sstevel@tonic-gate } else 4870Sstevel@tonic-gate { 4880Sstevel@tonic-gate fprintf(stderr, 4890Sstevel@tonic-gate gettext("%s: special cannot be determined\n"), 4900Sstevel@tonic-gate myname); 4910Sstevel@tonic-gate exit(1); 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate } else if (fstype == NULL) 4940Sstevel@tonic-gate fstype = default_fstype(special); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate out: 4980Sstevel@tonic-gate if (check_fields(fstype, mountp)) 4990Sstevel@tonic-gate exit(1); 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate if (realpath(mountp, realdir) == NULL) { 5020Sstevel@tonic-gate (void) fprintf(stderr, "mount: "); 5030Sstevel@tonic-gate perror(mountp); 5040Sstevel@tonic-gate exit(1); 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate if ((mountp = strdup(realdir)) == NULL) 5080Sstevel@tonic-gate nomem(); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* create the new arg list, and end the list with a null pointer */ 5110Sstevel@tonic-gate ii = 2; 5120Sstevel@tonic-gate if (cflg) 5130Sstevel@tonic-gate newargv[ii++] = "-c"; 5140Sstevel@tonic-gate if (gflg) 5150Sstevel@tonic-gate newargv[ii++] = "-g"; 5160Sstevel@tonic-gate if (mflg) 5170Sstevel@tonic-gate newargv[ii++] = "-m"; 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * The q option needs to go before the -o option as some 5200Sstevel@tonic-gate * filesystems complain during first pass option parsing. 5210Sstevel@tonic-gate */ 5220Sstevel@tonic-gate if (qflg) 5230Sstevel@tonic-gate newargv[ii++] = "-q"; 5240Sstevel@tonic-gate if (oflg) { 5250Sstevel@tonic-gate newargv[ii++] = "-o"; 5260Sstevel@tonic-gate newargv[ii++] = specific_opts; 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate if (Oflg) 5290Sstevel@tonic-gate newargv[ii++] = "-O"; 5300Sstevel@tonic-gate if (rflg) 5310Sstevel@tonic-gate newargv[ii++] = "-r"; 5320Sstevel@tonic-gate if (dashflg) 5330Sstevel@tonic-gate newargv[ii++] = "--"; 5340Sstevel@tonic-gate newargv[ii++] = special; 5350Sstevel@tonic-gate newargv[ii++] = mountp; 5360Sstevel@tonic-gate newargv[ii] = NULL; 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate doexec(fstype, newargv); 539821Sdh145677 return (0); 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate void 543821Sdh145677 usage(void) 5440Sstevel@tonic-gate { 5450Sstevel@tonic-gate fprintf(stderr, gettext("Usage:\n%s [-v | -p]\n"), myname); 5460Sstevel@tonic-gate fprintf(stderr, gettext( 5470Sstevel@tonic-gate "%s [-F FSType] [-V] [current_options] [-o specific_options]"), 5480Sstevel@tonic-gate myname); 5490Sstevel@tonic-gate fprintf(stderr, gettext("\n\t{special | mount_point}\n")); 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate fprintf(stderr, gettext( 5520Sstevel@tonic-gate "%s [-F FSType] [-V] [current_options] [-o specific_options]"), 5530Sstevel@tonic-gate myname); 5540Sstevel@tonic-gate fprintf(stderr, gettext("\n\tspecial mount_point\n")); 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate fprintf(stderr, gettext( 5570Sstevel@tonic-gate "%s -a [-F FSType ] [-V] [current_options] [-o specific_options]\n"), 5580Sstevel@tonic-gate myname); 5590Sstevel@tonic-gate fprintf(stderr, gettext("\t[mount_point ...]\n")); 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate exit(1); 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate /* 5650Sstevel@tonic-gate * Get rid of "dev=[hex string]" clause, if any. It's not legal 5660Sstevel@tonic-gate * when printing in vfstab format. 5670Sstevel@tonic-gate */ 5680Sstevel@tonic-gate void 5690Sstevel@tonic-gate elide_dev(char *mntopts) 5700Sstevel@tonic-gate { 5710Sstevel@tonic-gate char *dev, *other; 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate if (mntopts != NULL) { 5740Sstevel@tonic-gate dev = strstr(mntopts, "dev="); 5750Sstevel@tonic-gate if (dev != NULL) { 5760Sstevel@tonic-gate other = strpbrk(dev, ","); 5770Sstevel@tonic-gate if (other == NULL) { 5780Sstevel@tonic-gate /* last option */ 5790Sstevel@tonic-gate if (dev != mntopts) { 5800Sstevel@tonic-gate *--dev = '\0'; 5810Sstevel@tonic-gate } else { 5820Sstevel@tonic-gate *dev = '\0'; 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate } else { 5850Sstevel@tonic-gate /* first or intermediate option */ 5860Sstevel@tonic-gate memmove(dev, other+1, strlen(other+1)+1); 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate } 5890Sstevel@tonic-gate } 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate void 593821Sdh145677 print_mnttab(int vflg, int pflg) 5940Sstevel@tonic-gate { 5950Sstevel@tonic-gate FILE *fd; 5960Sstevel@tonic-gate FILE *rfp; /* this will be NULL if fopen fails */ 5970Sstevel@tonic-gate int ret; 5980Sstevel@tonic-gate char time_buf[TIME_MAX]; /* array to hold date and time */ 5990Sstevel@tonic-gate struct extmnttab mget; 6000Sstevel@tonic-gate time_t ltime; 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate if ((fd = fopen(mnttab, "r")) == NULL) { 6030Sstevel@tonic-gate fprintf(stderr, gettext("%s: Cannot open mnttab\n"), myname); 6040Sstevel@tonic-gate exit(1); 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate rfp = fopen(REMOTE, "r"); 6070Sstevel@tonic-gate while ((ret = getextmntent(fd, &mget, sizeof (struct extmnttab))) 6080Sstevel@tonic-gate == 0) { 6090Sstevel@tonic-gate if (ignore(mget.mnt_mntopts)) 6100Sstevel@tonic-gate continue; 6110Sstevel@tonic-gate if (mget.mnt_special && mget.mnt_mountp && 6120Sstevel@tonic-gate mget.mnt_fstype && mget.mnt_time) { 6130Sstevel@tonic-gate ltime = atol(mget.mnt_time); 6140Sstevel@tonic-gate cftime(time_buf, FORMAT, <ime); 6150Sstevel@tonic-gate if (pflg) { 6160Sstevel@tonic-gate elide_dev(mget.mnt_mntopts); 6170Sstevel@tonic-gate printf("%s - %s %s - no %s\n", 6180Sstevel@tonic-gate mget.mnt_special, 6190Sstevel@tonic-gate mget.mnt_mountp, 6200Sstevel@tonic-gate mget.mnt_fstype, 6210Sstevel@tonic-gate mget.mnt_mntopts != NULL ? 6220Sstevel@tonic-gate mget.mnt_mntopts : "-"); 6230Sstevel@tonic-gate } else if (vflg) { 6240Sstevel@tonic-gate printf("%s on %s type %s %s%s on %s", 6250Sstevel@tonic-gate mget.mnt_special, 6260Sstevel@tonic-gate mget.mnt_mountp, 6270Sstevel@tonic-gate mget.mnt_fstype, 6280Sstevel@tonic-gate remote(mget.mnt_fstype, rfp), 6290Sstevel@tonic-gate flags(mget.mnt_mntopts, NEW), 6300Sstevel@tonic-gate time_buf); 6310Sstevel@tonic-gate } else 6320Sstevel@tonic-gate printf("%s on %s %s%s on %s", 6330Sstevel@tonic-gate mget.mnt_mountp, 6340Sstevel@tonic-gate mget.mnt_special, 6350Sstevel@tonic-gate remote(mget.mnt_fstype, rfp), 6360Sstevel@tonic-gate flags(mget.mnt_mntopts, OLD), 6370Sstevel@tonic-gate time_buf); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate if (ret > 0) 6410Sstevel@tonic-gate mnterror(ret); 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate char * 645821Sdh145677 flags(char *mntopts, int flag) 6460Sstevel@tonic-gate { 6470Sstevel@tonic-gate char opts[sizeof (mntflags)]; 6480Sstevel@tonic-gate char *value; 6490Sstevel@tonic-gate int rdwr = 1; 6500Sstevel@tonic-gate int suid = 1; 6510Sstevel@tonic-gate int devices = 1; 6520Sstevel@tonic-gate int setuid = 1; 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate if (mntopts == NULL || *mntopts == '\0') 6550Sstevel@tonic-gate return ("read/write/setuid/devices"); 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate strcpy(opts, ""); 6580Sstevel@tonic-gate while (*mntopts != '\0') { 6590Sstevel@tonic-gate switch (getsubopt(&mntopts, myopts, &value)) { 6600Sstevel@tonic-gate case READONLY: 6610Sstevel@tonic-gate rdwr = 0; 6620Sstevel@tonic-gate break; 6630Sstevel@tonic-gate case READWRITE: 6640Sstevel@tonic-gate rdwr = 1; 6650Sstevel@tonic-gate break; 6660Sstevel@tonic-gate case SUID: 6670Sstevel@tonic-gate suid = 1; 6680Sstevel@tonic-gate break; 6690Sstevel@tonic-gate case NOSUID: 6700Sstevel@tonic-gate suid = 0; 6710Sstevel@tonic-gate break; 6720Sstevel@tonic-gate case SETUID: 6730Sstevel@tonic-gate setuid = 1; 6740Sstevel@tonic-gate break; 6750Sstevel@tonic-gate case NOSETUID: 6760Sstevel@tonic-gate setuid = 0; 6770Sstevel@tonic-gate break; 6780Sstevel@tonic-gate case DEVICES: 6790Sstevel@tonic-gate devices = 1; 6800Sstevel@tonic-gate break; 6810Sstevel@tonic-gate case NODEVICES: 6820Sstevel@tonic-gate devices = 0; 6830Sstevel@tonic-gate break; 6840Sstevel@tonic-gate default: 6850Sstevel@tonic-gate /* cat '/' separator to mntflags */ 6860Sstevel@tonic-gate if (*opts != '\0' && value != NULL) 6870Sstevel@tonic-gate strcat(opts, "/"); 6880Sstevel@tonic-gate strcat(opts, value); 6890Sstevel@tonic-gate break; 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate } 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate strcpy(mntflags, ""); 6940Sstevel@tonic-gate if (rdwr) 6950Sstevel@tonic-gate strcat(mntflags, "read/write"); 6960Sstevel@tonic-gate else if (flag == OLD) 6970Sstevel@tonic-gate strcat(mntflags, "read only"); 6980Sstevel@tonic-gate else 6990Sstevel@tonic-gate strcat(mntflags, "read-only"); 7000Sstevel@tonic-gate if (suid) { 7010Sstevel@tonic-gate if (setuid) 7020Sstevel@tonic-gate strcat(mntflags, "/setuid"); 7030Sstevel@tonic-gate else 7040Sstevel@tonic-gate strcat(mntflags, "/nosetuid"); 7050Sstevel@tonic-gate if (devices) 7060Sstevel@tonic-gate strcat(mntflags, "/devices"); 7070Sstevel@tonic-gate else 7080Sstevel@tonic-gate strcat(mntflags, "/nodevices"); 7090Sstevel@tonic-gate } else { 7100Sstevel@tonic-gate strcat(mntflags, "/nosetuid/nodevices"); 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate if (*opts != '\0') { 7130Sstevel@tonic-gate strcat(mntflags, "/"); 7140Sstevel@tonic-gate strcat(mntflags, opts); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate /* 7180Sstevel@tonic-gate * The assumed assertion 7190Sstevel@tonic-gate * assert (strlen(mntflags) < sizeof mntflags); 7200Sstevel@tonic-gate * is valid at this point in the code. Note that a call to "assert" 7210Sstevel@tonic-gate * is not appropriate in production code since it halts the program. 7220Sstevel@tonic-gate */ 7230Sstevel@tonic-gate return (mntflags); 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate char * 727821Sdh145677 remote(char *fstype, FILE *rfp) 7280Sstevel@tonic-gate { 7290Sstevel@tonic-gate char buf[BUFSIZ]; 7300Sstevel@tonic-gate char *fs; 7310Sstevel@tonic-gate extern char *strtok(); 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate if (rfp == NULL || fstype == NULL || 7340Sstevel@tonic-gate strlen(fstype) > (size_t)FSTYPE_MAX) 7350Sstevel@tonic-gate return (""); /* not a remote */ 7360Sstevel@tonic-gate rewind(rfp); 7370Sstevel@tonic-gate while (fgets(buf, sizeof (buf), rfp) != NULL) { 7380Sstevel@tonic-gate fs = strtok(buf, " \t\n"); 7390Sstevel@tonic-gate if (strcmp(fstype, fs) == 0) 7400Sstevel@tonic-gate return ("remote/"); /* is a remote fs */ 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate return (""); /* not a remote */ 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate void 7470Sstevel@tonic-gate vfserror(int flag, char *special) 7480Sstevel@tonic-gate { 7490Sstevel@tonic-gate if (special == NULL) 7500Sstevel@tonic-gate special = "<null>"; 7510Sstevel@tonic-gate switch (flag) { 7520Sstevel@tonic-gate case VFS_TOOLONG: 7530Sstevel@tonic-gate fprintf(stderr, 7540Sstevel@tonic-gate gettext("%s: Warning: Line in vfstab for \"%s\" exceeds %d characters\n"), 7550Sstevel@tonic-gate myname, special, VFS_LINE_MAX-1); 7560Sstevel@tonic-gate break; 7570Sstevel@tonic-gate case VFS_TOOFEW: 7580Sstevel@tonic-gate fprintf(stderr, 7590Sstevel@tonic-gate gettext("%s: Warning: Line for \"%s\" in vfstab has too few entries\n"), 7600Sstevel@tonic-gate myname, special); 7610Sstevel@tonic-gate break; 7620Sstevel@tonic-gate case VFS_TOOMANY: 7630Sstevel@tonic-gate fprintf(stderr, 7640Sstevel@tonic-gate gettext("%s: Warning: Line for \"%s\" in vfstab has too many entries\n"), 7650Sstevel@tonic-gate myname, special); 7660Sstevel@tonic-gate break; 7670Sstevel@tonic-gate default: 7680Sstevel@tonic-gate fprintf(stderr, gettext( 7690Sstevel@tonic-gate "%s: Warning: Error in line for \"%s\" in vfstab\n"), 7700Sstevel@tonic-gate myname, special); 7710Sstevel@tonic-gate } 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate void 775821Sdh145677 mnterror(int flag) 7760Sstevel@tonic-gate { 7770Sstevel@tonic-gate switch (flag) { 7780Sstevel@tonic-gate case MNT_TOOLONG: 7790Sstevel@tonic-gate fprintf(stderr, 7800Sstevel@tonic-gate gettext("%s: Line in mnttab exceeds %d characters\n"), 7810Sstevel@tonic-gate myname, MNT_LINE_MAX-2); 7820Sstevel@tonic-gate break; 7830Sstevel@tonic-gate case MNT_TOOFEW: 7840Sstevel@tonic-gate fprintf(stderr, 7850Sstevel@tonic-gate gettext("%s: Line in mnttab has too few entries\n"), 7860Sstevel@tonic-gate myname); 7870Sstevel@tonic-gate break; 7880Sstevel@tonic-gate case MNT_TOOMANY: 7890Sstevel@tonic-gate fprintf(stderr, 7900Sstevel@tonic-gate gettext("%s: Line in mnttab has too many entries\n"), 7910Sstevel@tonic-gate myname); 7920Sstevel@tonic-gate break; 7930Sstevel@tonic-gate } 7940Sstevel@tonic-gate exit(1); 7950Sstevel@tonic-gate } 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate void 798821Sdh145677 doexec(char *fstype, char *newargv[]) 7990Sstevel@tonic-gate { 8000Sstevel@tonic-gate char full_path[PATH_MAX]; 8010Sstevel@tonic-gate char alter_path[PATH_MAX]; 8020Sstevel@tonic-gate char *vfs_path = VFS_PATH; 8030Sstevel@tonic-gate char *alt_path = ALT_PATH; 8040Sstevel@tonic-gate int i; 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate /* build the full pathname of the fstype dependent command. */ 8070Sstevel@tonic-gate sprintf(full_path, "%s/%s/%s", vfs_path, fstype, myname); 8080Sstevel@tonic-gate sprintf(alter_path, "%s/%s/%s", alt_path, fstype, myname); 8090Sstevel@tonic-gate newargv[1] = myname; 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate if (Vflg) { 8120Sstevel@tonic-gate printf("%s -F %s", newargv[1], fstype); 8130Sstevel@tonic-gate for (i = 2; newargv[i]; i++) 8140Sstevel@tonic-gate printf(" %s", newargv[i]); 8150Sstevel@tonic-gate printf("\n"); 8160Sstevel@tonic-gate fflush(stdout); 8170Sstevel@tonic-gate exit(0); 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate /* 8210Sstevel@tonic-gate * Try to exec the fstype dependent portion of the mount. 8220Sstevel@tonic-gate * See if the directory is there before trying to exec dependent 8230Sstevel@tonic-gate * portion. This is only useful for eliminating the 8240Sstevel@tonic-gate * '..mount: not found' message when '/usr' is mounted 8250Sstevel@tonic-gate */ 8260Sstevel@tonic-gate if (access(full_path, 0) == 0) { 8270Sstevel@tonic-gate execv(full_path, &newargv[1]); 8280Sstevel@tonic-gate if (errno == EACCES) { 8290Sstevel@tonic-gate fprintf(stderr, 8300Sstevel@tonic-gate gettext("%s: Cannot execute %s - permission denied\n"), 8310Sstevel@tonic-gate myname, full_path); 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate if (errno == ENOEXEC) { 8340Sstevel@tonic-gate newargv[0] = "sh"; 8350Sstevel@tonic-gate newargv[1] = full_path; 8360Sstevel@tonic-gate execv("/sbin/sh", &newargv[0]); 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate execv(alter_path, &newargv[1]); 8400Sstevel@tonic-gate if (errno == EACCES) { 8410Sstevel@tonic-gate fprintf(stderr, gettext( 8420Sstevel@tonic-gate "%s: Cannot execute %s - permission denied\n"), 8430Sstevel@tonic-gate myname, alter_path); 8440Sstevel@tonic-gate exit(1); 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate if (errno == ENOEXEC) { 8470Sstevel@tonic-gate newargv[0] = "sh"; 8480Sstevel@tonic-gate newargv[1] = alter_path; 8490Sstevel@tonic-gate execv("/sbin/sh", &newargv[0]); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate fprintf(stderr, 8520Sstevel@tonic-gate gettext("%s: Operation not applicable to FSType %s\n"), 8530Sstevel@tonic-gate myname, fstype); 8540Sstevel@tonic-gate exit(1); 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate 8570Sstevel@tonic-gate char *mntopts[] = { MNTOPT_IGNORE, NULL }; 8580Sstevel@tonic-gate #define IGNORE 0 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate /* 8610Sstevel@tonic-gate * Return 1 if "ignore" appears in the options string 8620Sstevel@tonic-gate */ 8630Sstevel@tonic-gate int 864821Sdh145677 ignore(char *opts) 8650Sstevel@tonic-gate { 8660Sstevel@tonic-gate char *value; 8670Sstevel@tonic-gate char *saveptr, *my_opts; 8680Sstevel@tonic-gate int rval = 0; 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate if (opts == NULL || *opts == NULL) 8710Sstevel@tonic-gate return (0); 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate /* 8740Sstevel@tonic-gate * we make a copy of the option string to pass to getsubopt(), 8750Sstevel@tonic-gate * because getsubopt() modifies the string. We also save 8760Sstevel@tonic-gate * the original pointer returned by strdup, because getsubopt 8770Sstevel@tonic-gate * changes the pointer passed into it. If strdup fails (unlikely), 8780Sstevel@tonic-gate * we act as if the "ignore" option isn't set rather than fail. 8790Sstevel@tonic-gate */ 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate if ((saveptr = my_opts = strdup(opts)) == NULL) 8820Sstevel@tonic-gate nomem(); 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate while (*my_opts != '\0') { 8850Sstevel@tonic-gate if (getsubopt(&my_opts, mntopts, &value) == IGNORE) 8860Sstevel@tonic-gate rval = 1; 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate free(saveptr); 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate return (rval); 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate /* 8950Sstevel@tonic-gate * Perform the parallel version of mount. If count == 0, mount all 8960Sstevel@tonic-gate * vfstab filesystems with the automnt field == "yes". Use fstype if 8970Sstevel@tonic-gate * supplied. If mntlist supplied, then attempt to only mount those. 8980Sstevel@tonic-gate */ 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate int 9010Sstevel@tonic-gate parmount(char **mntlist, int count, char *fstype) 9020Sstevel@tonic-gate { 9030Sstevel@tonic-gate int maxfd = OPEN_MAX; 9040Sstevel@tonic-gate struct rlimit rl; 9050Sstevel@tonic-gate vfsent_t **vl, *vp; 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate /* 9080Sstevel@tonic-gate * Process scaling. After running a series 9090Sstevel@tonic-gate * of tests based on the number of simultaneous processes and 9100Sstevel@tonic-gate * processors available, optimum performance was achieved near or 9110Sstevel@tonic-gate * at (PROCN * 2). 9120Sstevel@tonic-gate */ 9130Sstevel@tonic-gate if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1) 9140Sstevel@tonic-gate maxrun = 4; 9150Sstevel@tonic-gate else 9160Sstevel@tonic-gate maxrun = maxrun * 2 + 1; 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { 9190Sstevel@tonic-gate rl.rlim_cur = rl.rlim_max; 9200Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rl) == 0) 9210Sstevel@tonic-gate maxfd = (int)rl.rlim_cur; 9220Sstevel@tonic-gate } 923*1914Scasper (void) enable_extended_FILE_stdio(-1, -1); 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate /* 9260Sstevel@tonic-gate * The parent needs to maintain 3 of its own fd's, plus 2 for 9270Sstevel@tonic-gate * each child (the stdout and stderr pipes). 9280Sstevel@tonic-gate */ 9290Sstevel@tonic-gate maxfd = (maxfd / 2) - 6; /* 6 takes care of temporary */ 9300Sstevel@tonic-gate /* periods of open fds */ 9310Sstevel@tonic-gate if (maxfd < maxrun) 9320Sstevel@tonic-gate maxrun = maxfd; 9330Sstevel@tonic-gate if (maxrun < 4) 9340Sstevel@tonic-gate maxrun = 4; /* sanity check */ 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate if (count == 0) 9370Sstevel@tonic-gate mntlist = NULL; /* used as a flag later */ 9380Sstevel@tonic-gate else 9390Sstevel@tonic-gate fstype = NULL; /* mount points supplied: */ 9400Sstevel@tonic-gate /* ignore fstype */ 9410Sstevel@tonic-gate /* 9420Sstevel@tonic-gate * Read the whole vfstab into a linked list for quick processing. 9430Sstevel@tonic-gate * On average, this is the most efficient way to collect and 9440Sstevel@tonic-gate * manipulate the vfstab data. 9450Sstevel@tonic-gate */ 9460Sstevel@tonic-gate vfsll = getvfsall(fstype, mntlist == NULL); 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate /* 9490Sstevel@tonic-gate * Make an array out of the vfs linked list for sorting purposes. 9500Sstevel@tonic-gate */ 9510Sstevel@tonic-gate if (vfsll == NULL || 9520Sstevel@tonic-gate (vfsarray = make_vfsarray(mntlist, count)) == NULL) { 9530Sstevel@tonic-gate if (mntlist == NULL) /* not an error - just none found */ 9540Sstevel@tonic-gate return (0); 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate fprintf(stderr, gettext("%s: No valid entries found in %s\n"), 9570Sstevel@tonic-gate myname, vfstab); 9580Sstevel@tonic-gate return (1); 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate 9610Sstevel@tonic-gate /* 9620Sstevel@tonic-gate * Sort the entries based on their resolved path names 9630Sstevel@tonic-gate * 9640Sstevel@tonic-gate * If an lofs is encountered, then the original order of the vfstab 9650Sstevel@tonic-gate * file needs to be maintained until we are done mounting lofs's. 9660Sstevel@tonic-gate */ 9670Sstevel@tonic-gate if (!lofscnt) 9680Sstevel@tonic-gate qsort((void *)vfsarray, vfsarraysize, sizeof (vfsent_t *), 9690Sstevel@tonic-gate mlevelcmp); 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate /* 9720Sstevel@tonic-gate * Shrink the vfsll linked list down to the new list. This will 9730Sstevel@tonic-gate * speed up the pid search in cleanupkid() later. 9740Sstevel@tonic-gate */ 9750Sstevel@tonic-gate vfsll = vfsarray[0]; 9760Sstevel@tonic-gate for (vl = vfsarray; vp = *vl; ) 9770Sstevel@tonic-gate vp->next = *++vl; 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate /* 9800Sstevel@tonic-gate * Try to handle interrupts in a reasonable way. 9810Sstevel@tonic-gate */ 9820Sstevel@tonic-gate sigset(SIGHUP, cleanup); 9830Sstevel@tonic-gate sigset(SIGQUIT, cleanup); 9840Sstevel@tonic-gate sigset(SIGINT, cleanup); 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate do_mounts(); /* do the mounts */ 9870Sstevel@tonic-gate return (exitcode); 9880Sstevel@tonic-gate } 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate /* 9910Sstevel@tonic-gate * Read all vstab (fp) entries into memory if fstype == NULL. 9920Sstevel@tonic-gate * If fstype is specified, than read all those that match it. 9930Sstevel@tonic-gate * 9940Sstevel@tonic-gate * Returns a linked list. 9950Sstevel@tonic-gate */ 9960Sstevel@tonic-gate vfsent_t * 9970Sstevel@tonic-gate getvfsall(char *fstype, int takeall) 9980Sstevel@tonic-gate { 9990Sstevel@tonic-gate vfsent_t *vhead, *vtail; 10000Sstevel@tonic-gate struct vfstab vget; 10010Sstevel@tonic-gate FILE *fp; 10020Sstevel@tonic-gate int cnt = 0, ret; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate if ((fp = fopen(vfstab, "r")) == NULL) { 10050Sstevel@tonic-gate fprintf(stderr, gettext("%s: Cannot open %s\n"), 10060Sstevel@tonic-gate myname, vfstab); 10070Sstevel@tonic-gate exit(1); 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate vhead = vtail = NULL; 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate while ((ret = getvfsent(fp, &vget)) != -1) { 10130Sstevel@tonic-gate vfsent_t *vp; 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate if (ret > 0) { 10160Sstevel@tonic-gate vfserror(ret, vget.vfs_mountp); 10170Sstevel@tonic-gate continue; 10180Sstevel@tonic-gate } 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate /* 10210Sstevel@tonic-gate * If mount points were not specified, then we ignore 10220Sstevel@tonic-gate * entries that aren't marked "yes". 10230Sstevel@tonic-gate */ 10240Sstevel@tonic-gate if (takeall && 10250Sstevel@tonic-gate (vget.vfs_automnt == NULL || 10260Sstevel@tonic-gate strcmp(vget.vfs_automnt, "yes"))) 10270Sstevel@tonic-gate continue; 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate if (fstype && vget.vfs_fstype && 10300Sstevel@tonic-gate strcmp(fstype, vget.vfs_fstype)) 10310Sstevel@tonic-gate continue; 10320Sstevel@tonic-gate 10330Sstevel@tonic-gate if (vget.vfs_mountp == NULL || 10340Sstevel@tonic-gate (vget.vfs_fstype && (strcmp(vget.vfs_fstype, "swap") == 0))) 10350Sstevel@tonic-gate continue; 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate if (check_fields(vget.vfs_fstype, vget.vfs_mountp)) { 10380Sstevel@tonic-gate exitcode = 1; 10390Sstevel@tonic-gate continue; 10400Sstevel@tonic-gate } 10410Sstevel@tonic-gate 10420Sstevel@tonic-gate vp = new_vfsent(&vget, cnt); /* create new vfs entry */ 10430Sstevel@tonic-gate if (vhead == NULL) 10440Sstevel@tonic-gate vhead = vp; 10450Sstevel@tonic-gate else 10460Sstevel@tonic-gate vtail->next = vp; 10470Sstevel@tonic-gate vtail = vp; 10480Sstevel@tonic-gate cnt++; 10490Sstevel@tonic-gate } 10500Sstevel@tonic-gate fclose(fp); 10510Sstevel@tonic-gate if (vtail == NULL) { 10520Sstevel@tonic-gate vfsarraysize = 0; 10530Sstevel@tonic-gate vfslltail = NULL; 10540Sstevel@tonic-gate return (NULL); 10550Sstevel@tonic-gate } 10560Sstevel@tonic-gate vtail->next = NULL; 10570Sstevel@tonic-gate vfslltail = vtail; /* save it in the global variable */ 10580Sstevel@tonic-gate vfsarraysize = cnt; 10590Sstevel@tonic-gate return (vhead); 10600Sstevel@tonic-gate } 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate /* 10640Sstevel@tonic-gate * Returns an array of vfsent_t's based on vfsll & mntlist. 10650Sstevel@tonic-gate */ 10660Sstevel@tonic-gate vfsent_t ** 10670Sstevel@tonic-gate make_vfsarray(char **mntlist, int count) 10680Sstevel@tonic-gate { 10690Sstevel@tonic-gate vfsent_t *vp, *vmark, *vpprev, **vpp; 10700Sstevel@tonic-gate int ndx, found; 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate if (vfsll == NULL) 10730Sstevel@tonic-gate return (NULL); 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate if (count > 0) 10760Sstevel@tonic-gate vfsarraysize = count; 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate vpp = (vfsent_t **)malloc(sizeof (*vpp) * (vfsarraysize + 1)); 10790Sstevel@tonic-gate if (vpp == NULL) 10800Sstevel@tonic-gate nomem(); 10810Sstevel@tonic-gate 10820Sstevel@tonic-gate if (mntlist == NULL) { 10830Sstevel@tonic-gate /* 10840Sstevel@tonic-gate * No mount list specified: take all vfstab mount points. 10850Sstevel@tonic-gate */ 10860Sstevel@tonic-gate for (ndx = 0, vp = vfsll; vp; vp = vp->next) { 10870Sstevel@tonic-gate (void) setrpath(vp); 10880Sstevel@tonic-gate /* 10890Sstevel@tonic-gate * Sigh. lofs entries can complicate matters so much 10900Sstevel@tonic-gate * that the best way to avoid problems is to 10910Sstevel@tonic-gate * stop parallel mounting when an lofs is 10920Sstevel@tonic-gate * encountered, so we keep a count of how many 10930Sstevel@tonic-gate * there are. 10940Sstevel@tonic-gate * Fortunately this is rare. 10950Sstevel@tonic-gate */ 10960Sstevel@tonic-gate if (vp->v.vfs_fstype && 10970Sstevel@tonic-gate (strcmp(vp->v.vfs_fstype, MNTTYPE_LOFS) == 0)) 10980Sstevel@tonic-gate lofscnt++; 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate vpp[ndx++] = vp; 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate vpp[ndx] = NULL; 11030Sstevel@tonic-gate return (vpp); 11040Sstevel@tonic-gate } 11050Sstevel@tonic-gate 11060Sstevel@tonic-gate /* 11070Sstevel@tonic-gate * A list of mount points was specified on the command line 11080Sstevel@tonic-gate * and we need to search for each one. 11090Sstevel@tonic-gate */ 11100Sstevel@tonic-gate vpprev = vfslltail; 11110Sstevel@tonic-gate vpprev->next = vfsll; /* make a circle out of it */ 11120Sstevel@tonic-gate vmark = vp = vfsll; 11130Sstevel@tonic-gate /* 11140Sstevel@tonic-gate * For each specified mount point: 11150Sstevel@tonic-gate */ 11160Sstevel@tonic-gate for (ndx = 0; *mntlist; mntlist++) { 11170Sstevel@tonic-gate found = 0; 11180Sstevel@tonic-gate /* 11190Sstevel@tonic-gate * Circle our entire linked list, looking for *mntlist. 11200Sstevel@tonic-gate */ 11210Sstevel@tonic-gate while (vp) { 11220Sstevel@tonic-gate if (strcmp(*mntlist, vp->v.vfs_mountp) == 0) { 11230Sstevel@tonic-gate vpp[ndx++] = vp; /* found it. */ 11240Sstevel@tonic-gate (void) setrpath(vp); 11250Sstevel@tonic-gate if (vp->v.vfs_fstype && 11260Sstevel@tonic-gate (strcmp(vp->v.vfs_fstype, 11270Sstevel@tonic-gate MNTTYPE_LOFS) == 0)) 11280Sstevel@tonic-gate lofscnt++; 11290Sstevel@tonic-gate 11300Sstevel@tonic-gate if (vp == vpprev) { /* list exhausted */ 11310Sstevel@tonic-gate vp = NULL; 11320Sstevel@tonic-gate found++; 11330Sstevel@tonic-gate break; 11340Sstevel@tonic-gate } 11350Sstevel@tonic-gate /* 11360Sstevel@tonic-gate * Remove it from the circular list. vpprev 11370Sstevel@tonic-gate * remains unchanged. 11380Sstevel@tonic-gate */ 11390Sstevel@tonic-gate vp = vp->next; 11400Sstevel@tonic-gate vpprev->next->next = NULL; 11410Sstevel@tonic-gate vpprev->next = vp; 11420Sstevel@tonic-gate /* 11430Sstevel@tonic-gate * Set vmark to the first elem that we check 11440Sstevel@tonic-gate * each time. 11450Sstevel@tonic-gate */ 11460Sstevel@tonic-gate vmark = vp; 11470Sstevel@tonic-gate found++; 11480Sstevel@tonic-gate break; 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate vpprev = vp; 11510Sstevel@tonic-gate vp = vp->next; 11520Sstevel@tonic-gate if (vp == vmark) /* break out if we completed */ 11530Sstevel@tonic-gate /* the circle */ 11540Sstevel@tonic-gate break; 11550Sstevel@tonic-gate } 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate if (!found) { 11580Sstevel@tonic-gate fprintf(stderr, gettext( 11590Sstevel@tonic-gate "%s: Warning: %s not found in %s\n"), 11600Sstevel@tonic-gate myname, *mntlist, vfstab); 11610Sstevel@tonic-gate exitcode = 1; 11620Sstevel@tonic-gate } 11630Sstevel@tonic-gate } 11640Sstevel@tonic-gate if (ndx == 0) 11650Sstevel@tonic-gate return (NULL); 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate vpp[ndx] = NULL; /* null terminate the list */ 11680Sstevel@tonic-gate vfsarraysize = ndx; /* adjust vfsarraysize */ 11690Sstevel@tonic-gate return (vpp); 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate /* 11730Sstevel@tonic-gate * Performs the exec argument processing, all of the child forking and 11740Sstevel@tonic-gate * execing, and child cleanup. 11750Sstevel@tonic-gate * Sets exitcode to non-zero if any errors occurred. 11760Sstevel@tonic-gate */ 11770Sstevel@tonic-gate void 1178821Sdh145677 do_mounts(void) 11790Sstevel@tonic-gate { 11800Sstevel@tonic-gate int i, isave, cnt; 11810Sstevel@tonic-gate vfsent_t *vp, *vpprev, **vl; 11820Sstevel@tonic-gate char *newargv[ARGV_MAX]; 11830Sstevel@tonic-gate pid_t child; 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate /* 11860Sstevel@tonic-gate * create the arg list once; the only differences among 11870Sstevel@tonic-gate * the calls are the options, special and mountp fields. 11880Sstevel@tonic-gate */ 11890Sstevel@tonic-gate i = 2; 11900Sstevel@tonic-gate if (cflg) 11910Sstevel@tonic-gate newargv[i++] = "-c"; 11920Sstevel@tonic-gate if (gflg) 11930Sstevel@tonic-gate newargv[i++] = "-g"; 11940Sstevel@tonic-gate if (mflg) 11950Sstevel@tonic-gate newargv[i++] = "-m"; 11960Sstevel@tonic-gate if (Oflg) 11970Sstevel@tonic-gate newargv[i++] = "-O"; 11980Sstevel@tonic-gate if (qflg) 11990Sstevel@tonic-gate newargv[i++] = "-q"; 12000Sstevel@tonic-gate if (rflg) 12010Sstevel@tonic-gate newargv[i++] = "-r"; 12020Sstevel@tonic-gate if (dashflg) 12030Sstevel@tonic-gate newargv[i++] = "--"; 12040Sstevel@tonic-gate if (oflg) { 12050Sstevel@tonic-gate newargv[i++] = "-o"; 12060Sstevel@tonic-gate newargv[i++] = specific_opts; 12070Sstevel@tonic-gate } 12080Sstevel@tonic-gate isave = i; 12090Sstevel@tonic-gate 12100Sstevel@tonic-gate /* 12110Sstevel@tonic-gate * Main loop for the mount processes 12120Sstevel@tonic-gate */ 12130Sstevel@tonic-gate vl = vfsarray; 12140Sstevel@tonic-gate cnt = vfsarraysize; 12150Sstevel@tonic-gate for (vpprev = *vl; vp = *vl; vpprev = vp, vl++, cnt--) { 12160Sstevel@tonic-gate /* 12170Sstevel@tonic-gate * Check to see if we cross a mount level: e.g., 12180Sstevel@tonic-gate * /a/b -> /a/b/c. If so, we need to wait for all current 12190Sstevel@tonic-gate * mounts to finish, rerun realpath on the remaining mount 12200Sstevel@tonic-gate * points, and resort the list. 12210Sstevel@tonic-gate * 12220Sstevel@tonic-gate * Also, we mount serially as long as there are lofs's 12230Sstevel@tonic-gate * to mount to avoid improper mount ordering. 12240Sstevel@tonic-gate */ 12250Sstevel@tonic-gate if (vp->mlevel > vpprev->mlevel || lofscnt > 0) { 12260Sstevel@tonic-gate vfsent_t **vlp; 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate while (nrun > 0 && (dowait() != -1)) 12290Sstevel@tonic-gate ; 12300Sstevel@tonic-gate /* 12310Sstevel@tonic-gate * Gads! It's possible for real path mounts points to 12320Sstevel@tonic-gate * change after mounts are done at a lower mount 12330Sstevel@tonic-gate * level. 12340Sstevel@tonic-gate * Thus, we need to recalculate mount levels and 12350Sstevel@tonic-gate * resort the list from this point. 12360Sstevel@tonic-gate */ 12370Sstevel@tonic-gate for (vlp = vl; *vlp; vlp++) 12380Sstevel@tonic-gate (void) setrpath(*vlp); 12390Sstevel@tonic-gate /* 12400Sstevel@tonic-gate * Sort the remaining entries based on their newly 12410Sstevel@tonic-gate * resolved path names. 12420Sstevel@tonic-gate * Do not sort if we still have lofs's to mount. 12430Sstevel@tonic-gate */ 12440Sstevel@tonic-gate if (lofscnt == 0) { 12450Sstevel@tonic-gate qsort((void *)vl, cnt, sizeof (vfsent_t *), 12460Sstevel@tonic-gate mlevelcmp); 12470Sstevel@tonic-gate vp = *vl; 12480Sstevel@tonic-gate } 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate if (vp->flag & VRPFAILED) { 12520Sstevel@tonic-gate fprintf(stderr, gettext( 12530Sstevel@tonic-gate "%s: Nonexistent mount point: %s\n"), 12540Sstevel@tonic-gate myname, vp->v.vfs_mountp); 12550Sstevel@tonic-gate vp->flag |= VNOTMOUNTED; 12560Sstevel@tonic-gate exitcode = 1; 12570Sstevel@tonic-gate continue; 12580Sstevel@tonic-gate } 12590Sstevel@tonic-gate 12600Sstevel@tonic-gate /* 12610Sstevel@tonic-gate * If mount options were not specified on the command 12620Sstevel@tonic-gate * line, then use the ones found in the vfstab entry, 12630Sstevel@tonic-gate * if any. 12640Sstevel@tonic-gate */ 12650Sstevel@tonic-gate i = isave; 12660Sstevel@tonic-gate if (!oflg && vp->v.vfs_mntopts) { 12670Sstevel@tonic-gate newargv[i++] = "-o"; 12680Sstevel@tonic-gate newargv[i++] = vp->v.vfs_mntopts; 12690Sstevel@tonic-gate } 12700Sstevel@tonic-gate newargv[i++] = vp->v.vfs_special; 12710Sstevel@tonic-gate newargv[i++] = vp->rpath; 12720Sstevel@tonic-gate newargv[i] = NULL; 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate /* 12750Sstevel@tonic-gate * This should never really fail. 12760Sstevel@tonic-gate */ 12770Sstevel@tonic-gate while (setup_iopipe(vp) == -1 && (dowait() != -1)) 12780Sstevel@tonic-gate ; 12790Sstevel@tonic-gate 12800Sstevel@tonic-gate while (nrun >= maxrun && (dowait() != -1)) /* throttle */ 12810Sstevel@tonic-gate ; 12820Sstevel@tonic-gate 12830Sstevel@tonic-gate #ifdef CACHEFS_BUG 12840Sstevel@tonic-gate if (vp->v.vfs_fstype && 12850Sstevel@tonic-gate (strcmp(vp->v.vfs_fstype, "cachefs") == 0)) { 12860Sstevel@tonic-gate while (cachefs_running && (dowait() != -1)) 12870Sstevel@tonic-gate ; 12880Sstevel@tonic-gate cachefs_running = 1; 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate #endif 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate if ((child = fork()) == -1) { 12930Sstevel@tonic-gate perror("fork"); 12940Sstevel@tonic-gate cleanup(-1); 12950Sstevel@tonic-gate /* not reached */ 12960Sstevel@tonic-gate } 12970Sstevel@tonic-gate if (child == 0) { /* child */ 12980Sstevel@tonic-gate signal(SIGHUP, SIG_IGN); 12990Sstevel@tonic-gate signal(SIGQUIT, SIG_IGN); 13000Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 13010Sstevel@tonic-gate setup_output(vp); 13020Sstevel@tonic-gate doexec(vp->v.vfs_fstype, newargv); 13030Sstevel@tonic-gate perror("exec"); 13040Sstevel@tonic-gate exit(1); 13050Sstevel@tonic-gate } 13060Sstevel@tonic-gate 13070Sstevel@tonic-gate /* parent */ 13080Sstevel@tonic-gate (void) close(vp->sopipe[WRPIPE]); 13090Sstevel@tonic-gate (void) close(vp->sepipe[WRPIPE]); 13100Sstevel@tonic-gate vp->pid = child; 13110Sstevel@tonic-gate nrun++; 13120Sstevel@tonic-gate } 13130Sstevel@tonic-gate /* 13140Sstevel@tonic-gate * Mostly done by now - wait and clean up the stragglers. 13150Sstevel@tonic-gate */ 13160Sstevel@tonic-gate cleanup(0); 13170Sstevel@tonic-gate } 13180Sstevel@tonic-gate 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate /* 13210Sstevel@tonic-gate * Setup stdout and stderr pipes for the children's output. 13220Sstevel@tonic-gate */ 13230Sstevel@tonic-gate int 13240Sstevel@tonic-gate setup_iopipe(vfsent_t *mp) 13250Sstevel@tonic-gate { 13260Sstevel@tonic-gate /* 13270Sstevel@tonic-gate * Make a stdout and stderr pipe. This should never fail. 13280Sstevel@tonic-gate */ 13290Sstevel@tonic-gate if (pipe(mp->sopipe) == -1) 13300Sstevel@tonic-gate return (-1); 13310Sstevel@tonic-gate if (pipe(mp->sepipe) == -1) { 13320Sstevel@tonic-gate (void) close(mp->sopipe[RDPIPE]); 13330Sstevel@tonic-gate (void) close(mp->sopipe[WRPIPE]); 13340Sstevel@tonic-gate return (-1); 13350Sstevel@tonic-gate } 13360Sstevel@tonic-gate /* 13370Sstevel@tonic-gate * Don't block on an empty pipe. 13380Sstevel@tonic-gate */ 13390Sstevel@tonic-gate (void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK); 13400Sstevel@tonic-gate (void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK); 13410Sstevel@tonic-gate /* 13420Sstevel@tonic-gate * Don't pass extra fds into children. 13430Sstevel@tonic-gate */ 13440Sstevel@tonic-gate (void) fcntl(mp->sopipe[RDPIPE], F_SETFD, FD_CLOEXEC); 13450Sstevel@tonic-gate (void) fcntl(mp->sepipe[RDPIPE], F_SETFD, FD_CLOEXEC); 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate return (0); 13480Sstevel@tonic-gate } 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate /* 13510Sstevel@tonic-gate * Called by a child to attach its stdout and stderr to the write side of 13520Sstevel@tonic-gate * the pipes. 13530Sstevel@tonic-gate */ 13540Sstevel@tonic-gate void 13550Sstevel@tonic-gate setup_output(vfsent_t *vp) 13560Sstevel@tonic-gate { 13570Sstevel@tonic-gate 13580Sstevel@tonic-gate (void) close(fileno(stdout)); 13590Sstevel@tonic-gate (void) dup(vp->sopipe[WRPIPE]); 13600Sstevel@tonic-gate (void) close(vp->sopipe[WRPIPE]); 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate (void) close(fileno(stderr)); 13630Sstevel@tonic-gate (void) dup(vp->sepipe[WRPIPE]); 13640Sstevel@tonic-gate (void) close(vp->sepipe[WRPIPE]); 13650Sstevel@tonic-gate } 13660Sstevel@tonic-gate 13670Sstevel@tonic-gate /* 13680Sstevel@tonic-gate * Parent uses this to print any stdout or stderr output issued by 13690Sstevel@tonic-gate * the child. 13700Sstevel@tonic-gate */ 13710Sstevel@tonic-gate static void 13720Sstevel@tonic-gate doio(vfsent_t *vp) 13730Sstevel@tonic-gate { 13740Sstevel@tonic-gate int bytes; 13750Sstevel@tonic-gate char ibuf[BUFSIZ]; 13760Sstevel@tonic-gate 13770Sstevel@tonic-gate while ((bytes = read(vp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0) 13780Sstevel@tonic-gate write(fileno(stderr), ibuf, bytes); 13790Sstevel@tonic-gate while ((bytes = read(vp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0) 13800Sstevel@tonic-gate write(fileno(stdout), ibuf, bytes); 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate (void) close(vp->sopipe[RDPIPE]); 13830Sstevel@tonic-gate (void) close(vp->sepipe[RDPIPE]); 13840Sstevel@tonic-gate } 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate /* 13870Sstevel@tonic-gate * Waits for 1 child to die. 13880Sstevel@tonic-gate * 13890Sstevel@tonic-gate * Returns -1 if no children are left to wait for. 13900Sstevel@tonic-gate * Returns 0 if a child died without an error. 13910Sstevel@tonic-gate * Returns 1 if a child died with an error. 13920Sstevel@tonic-gate */ 13930Sstevel@tonic-gate int 1394821Sdh145677 dowait(void) 13950Sstevel@tonic-gate { 13960Sstevel@tonic-gate int child, wstat; 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate if ((child = wait(&wstat)) == -1) 13990Sstevel@tonic-gate return (-1); 14000Sstevel@tonic-gate nrun--; 14010Sstevel@tonic-gate return (cleanupkid(child, wstat) != 0); 14020Sstevel@tonic-gate } 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate /* 14050Sstevel@tonic-gate * Locates the child mount process represented by pid, outputs any io 14060Sstevel@tonic-gate * it may have, and returns its exit code. 14070Sstevel@tonic-gate * Sets the global exitcode if an error occurred. 14080Sstevel@tonic-gate */ 14090Sstevel@tonic-gate int 14100Sstevel@tonic-gate cleanupkid(pid_t pid, int wstat) 14110Sstevel@tonic-gate { 14120Sstevel@tonic-gate vfsent_t *vp, *prevp; 14130Sstevel@tonic-gate int ret; 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate if (WIFEXITED(wstat)) /* this should always be true */ 14160Sstevel@tonic-gate ret = WEXITSTATUS(wstat); 14170Sstevel@tonic-gate else 14180Sstevel@tonic-gate ret = 1; /* assume some kind of error */ 14190Sstevel@tonic-gate if (ret) 14200Sstevel@tonic-gate exitcode = 1; 14210Sstevel@tonic-gate 14220Sstevel@tonic-gate /* 14230Sstevel@tonic-gate * Find our child. 14240Sstevel@tonic-gate * This search gets smaller and smaller as children are cleaned 14250Sstevel@tonic-gate * up. 14260Sstevel@tonic-gate */ 14270Sstevel@tonic-gate for (prevp = NULL, vp = vfsll; vp; vp = vp->next) { 14280Sstevel@tonic-gate if (vp->pid != pid) { 14290Sstevel@tonic-gate prevp = vp; 14300Sstevel@tonic-gate continue; 14310Sstevel@tonic-gate } 14320Sstevel@tonic-gate /* 14330Sstevel@tonic-gate * Found: let's remove it from this linked list. 14340Sstevel@tonic-gate */ 14350Sstevel@tonic-gate if (prevp) { 14360Sstevel@tonic-gate prevp->next = vp->next; 14370Sstevel@tonic-gate vp->next = NULL; 14380Sstevel@tonic-gate } 14390Sstevel@tonic-gate break; 14400Sstevel@tonic-gate } 14410Sstevel@tonic-gate 14420Sstevel@tonic-gate if (vp == NULL) { 14430Sstevel@tonic-gate /* 14440Sstevel@tonic-gate * This should never happen. 14450Sstevel@tonic-gate */ 14460Sstevel@tonic-gate fprintf(stderr, gettext( 14470Sstevel@tonic-gate "%s: Unknown child %d\n"), myname, pid); 14480Sstevel@tonic-gate exitcode = 1; 14490Sstevel@tonic-gate return (ret); 14500Sstevel@tonic-gate } 14510Sstevel@tonic-gate doio(vp); /* Any output? */ 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate if (vp->v.vfs_fstype && (strcmp(vp->v.vfs_fstype, MNTTYPE_LOFS) == 0)) 14540Sstevel@tonic-gate lofscnt--; 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate #ifdef CACHEFS_BUG 14570Sstevel@tonic-gate if (vp->v.vfs_fstype && (strcmp(vp->v.vfs_fstype, "cachefs") == 0)) 14580Sstevel@tonic-gate cachefs_running = 0; 14590Sstevel@tonic-gate #endif 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate vp->exitcode = ret; 14620Sstevel@tonic-gate return (ret); 14630Sstevel@tonic-gate } 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate static vfsent_t zvmount = { 0 }; 14670Sstevel@tonic-gate 14680Sstevel@tonic-gate vfsent_t * 14690Sstevel@tonic-gate new_vfsent(struct vfstab *vin, int order) 14700Sstevel@tonic-gate { 14710Sstevel@tonic-gate vfsent_t *new; 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate new = (vfsent_t *)malloc(sizeof (*new)); 14740Sstevel@tonic-gate if (new == NULL) 14750Sstevel@tonic-gate nomem(); 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate *new = zvmount; 14780Sstevel@tonic-gate if (vin->vfs_special && 14790Sstevel@tonic-gate (new->v.vfs_special = strdup(vin->vfs_special)) == NULL) 14800Sstevel@tonic-gate nomem(); 14810Sstevel@tonic-gate if (vin->vfs_mountp && 14820Sstevel@tonic-gate (new->v.vfs_mountp = strdup(vin->vfs_mountp)) == NULL) 14830Sstevel@tonic-gate nomem(); 14840Sstevel@tonic-gate if (vin->vfs_fstype && 14850Sstevel@tonic-gate (new->v.vfs_fstype = strdup(vin->vfs_fstype)) == NULL) 14860Sstevel@tonic-gate nomem(); 14870Sstevel@tonic-gate /* 14880Sstevel@tonic-gate * If specific mount options were specified on the command 14890Sstevel@tonic-gate * line, then use those. Else, use the ones on the vfstab 14900Sstevel@tonic-gate * line, if any. In other words, specific options on the 14910Sstevel@tonic-gate * command line override those in /etc/vfstab. 14920Sstevel@tonic-gate */ 14930Sstevel@tonic-gate if (oflg) { 14940Sstevel@tonic-gate if ((new->v.vfs_mntopts = strdup(specific_opts)) == NULL) 14950Sstevel@tonic-gate nomem(); 14960Sstevel@tonic-gate } else if (vin->vfs_mntopts && 14970Sstevel@tonic-gate (new->v.vfs_mntopts = strdup(vin->vfs_mntopts)) == NULL) 14980Sstevel@tonic-gate nomem(); 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate new->order = order; 15010Sstevel@tonic-gate return (new); 15020Sstevel@tonic-gate } 15030Sstevel@tonic-gate 15040Sstevel@tonic-gate /* 15050Sstevel@tonic-gate * Runs realpath on vp's mount point, records success or failure, 15060Sstevel@tonic-gate * resets the mount level based on the new realpath, and returns 15070Sstevel@tonic-gate * realpath()'s return value. 15080Sstevel@tonic-gate */ 15090Sstevel@tonic-gate char * 15100Sstevel@tonic-gate setrpath(vfsent_t *vp) 15110Sstevel@tonic-gate { 15120Sstevel@tonic-gate char *rp; 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate if ((rp = realpath(vp->v.vfs_mountp, realdir)) == NULL) 15150Sstevel@tonic-gate vp->flag |= VRPFAILED; 15160Sstevel@tonic-gate else 15170Sstevel@tonic-gate vp->flag &= ~VRPFAILED; 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate if (vp->rpath) 15200Sstevel@tonic-gate free(vp->rpath); 15210Sstevel@tonic-gate if ((vp->rpath = strdup(realdir)) == NULL) 15220Sstevel@tonic-gate nomem(); 15230Sstevel@tonic-gate vp->mlevel = fsgetmlevel(vp->rpath); 15240Sstevel@tonic-gate return (rp); 15250Sstevel@tonic-gate } 15260Sstevel@tonic-gate 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate /* 15290Sstevel@tonic-gate * sort first by mlevel (1...N), then by vfstab order. 15300Sstevel@tonic-gate */ 15310Sstevel@tonic-gate int 15320Sstevel@tonic-gate mlevelcmp(const void *a, const void *b) 15330Sstevel@tonic-gate { 15340Sstevel@tonic-gate vfsent_t *a1, *b1; 15350Sstevel@tonic-gate int lcmp; 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate a1 = *(vfsent_t **)a; 15380Sstevel@tonic-gate b1 = *(vfsent_t **)b; 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate lcmp = a1->mlevel - b1->mlevel; 15410Sstevel@tonic-gate if (lcmp == 0) 15420Sstevel@tonic-gate lcmp = a1->order - b1->order; 15430Sstevel@tonic-gate return (lcmp); 15440Sstevel@tonic-gate } 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate /* sort by vfstab order. 0..N */ 1547821Sdh145677 static int 15480Sstevel@tonic-gate mordercmp(const void *a, const void *b) 15490Sstevel@tonic-gate { 15500Sstevel@tonic-gate vfsent_t *a1, *b1; 15510Sstevel@tonic-gate 15520Sstevel@tonic-gate a1 = *(vfsent_t **)a; 15530Sstevel@tonic-gate b1 = *(vfsent_t **)b; 15540Sstevel@tonic-gate return (a1->order - b1->order); 15550Sstevel@tonic-gate } 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate /* 15580Sstevel@tonic-gate * cleanup the existing children and exit with an error 15590Sstevel@tonic-gate * if asig != 0. 15600Sstevel@tonic-gate */ 15610Sstevel@tonic-gate void 15620Sstevel@tonic-gate cleanup(int asig) 15630Sstevel@tonic-gate { 15640Sstevel@tonic-gate while (nrun > 0 && (dowait() != -1)) 15650Sstevel@tonic-gate ; 15660Sstevel@tonic-gate 15670Sstevel@tonic-gate if (asig != 0) 15680Sstevel@tonic-gate exit(1); 15690Sstevel@tonic-gate } 15700Sstevel@tonic-gate 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate int 15730Sstevel@tonic-gate check_fields(char *fstype, char *mountp) 15740Sstevel@tonic-gate { 15750Sstevel@tonic-gate struct stat64 stbuf; 15760Sstevel@tonic-gate 15770Sstevel@tonic-gate if (strlen(fstype) > (size_t)FSTYPE_MAX) { 15780Sstevel@tonic-gate fprintf(stderr, 15790Sstevel@tonic-gate gettext("%s: FSType %s exceeds %d characters\n"), 15800Sstevel@tonic-gate myname, fstype, FSTYPE_MAX); 15810Sstevel@tonic-gate return (1); 15820Sstevel@tonic-gate } 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate if (mountp == NULL) { 15850Sstevel@tonic-gate fprintf(stderr, 15860Sstevel@tonic-gate gettext("%s: Mount point cannot be determined\n"), 15870Sstevel@tonic-gate myname); 15880Sstevel@tonic-gate return (1); 15890Sstevel@tonic-gate } 15900Sstevel@tonic-gate if (*mountp != '/') { 15910Sstevel@tonic-gate fprintf(stderr, gettext( 15920Sstevel@tonic-gate "%s: Mount point %s is not an absolute pathname.\n"), 15930Sstevel@tonic-gate myname, mountp); 15940Sstevel@tonic-gate return (1); 15950Sstevel@tonic-gate } 15960Sstevel@tonic-gate /* 15970Sstevel@tonic-gate * Don't do some of these checks if aflg because a mount point may 15980Sstevel@tonic-gate * not exist now, but will be mounted before we get to it. 15990Sstevel@tonic-gate * This is one of the quirks of "secondary mounting". 16000Sstevel@tonic-gate */ 16010Sstevel@tonic-gate if (!aflg && stat64(mountp, &stbuf) < 0) { 16020Sstevel@tonic-gate if (errno == ENOENT || errno == ENOTDIR) 16030Sstevel@tonic-gate fprintf(stderr, 16040Sstevel@tonic-gate gettext("%s: Mount point %s does not exist.\n"), 16050Sstevel@tonic-gate myname, mountp); 16060Sstevel@tonic-gate else { 16070Sstevel@tonic-gate fprintf(stderr, 16080Sstevel@tonic-gate gettext("%s: Cannot stat mount point %s.\n"), 16090Sstevel@tonic-gate myname, mountp); 16100Sstevel@tonic-gate perror(myname); 16110Sstevel@tonic-gate } 16120Sstevel@tonic-gate return (1); 16130Sstevel@tonic-gate } 16140Sstevel@tonic-gate return (0); 16150Sstevel@tonic-gate } 16160Sstevel@tonic-gate 16170Sstevel@tonic-gate void 1618821Sdh145677 nomem(void) 16190Sstevel@tonic-gate { 16200Sstevel@tonic-gate fprintf(stderr, gettext("%s: Out of memory\n"), myname); 16210Sstevel@tonic-gate while (nrun > 0 && (dowait() != -1)) 16220Sstevel@tonic-gate ; 16230Sstevel@tonic-gate exit(1); 16240Sstevel@tonic-gate } 1625