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 /* 22*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 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 <unistd.h> 360Sstevel@tonic-gate #include <stdlib.h> 370Sstevel@tonic-gate #include <string.h> 380Sstevel@tonic-gate #include <sys/signal.h> 390Sstevel@tonic-gate #include <sys/mnttab.h> 400Sstevel@tonic-gate #include <errno.h> 410Sstevel@tonic-gate #include <sys/types.h> 420Sstevel@tonic-gate #include <sys/stat.h> 430Sstevel@tonic-gate #include <sys/param.h> 440Sstevel@tonic-gate #include <sys/wait.h> 450Sstevel@tonic-gate #include <sys/vfstab.h> 460Sstevel@tonic-gate #include <sys/fcntl.h> 470Sstevel@tonic-gate #include <sys/resource.h> 480Sstevel@tonic-gate #include <sys/mntent.h> 490Sstevel@tonic-gate #include <sys/ctfs.h> 500Sstevel@tonic-gate #include <locale.h> 510Sstevel@tonic-gate #include <stdarg.h> 520Sstevel@tonic-gate #include <sys/mount.h> 530Sstevel@tonic-gate #include <sys/objfs.h> 540Sstevel@tonic-gate #include "fslib.h" 550Sstevel@tonic-gate 560Sstevel@tonic-gate #define FS_PATH "/usr/lib/fs" 570Sstevel@tonic-gate #define ALT_PATH "/etc/fs" 580Sstevel@tonic-gate #define FULLPATH_MAX 32 590Sstevel@tonic-gate #define FSTYPE_MAX 8 600Sstevel@tonic-gate #define ARGV_MAX 16 610Sstevel@tonic-gate 620Sstevel@tonic-gate int aflg, oflg, Vflg, dashflg, dflg, fflg; 630Sstevel@tonic-gate 640Sstevel@tonic-gate extern void rpterr(), usage(), mnterror(); 650Sstevel@tonic-gate 660Sstevel@tonic-gate extern char *optarg; /* used by getopt */ 670Sstevel@tonic-gate extern int optind, opterr; 680Sstevel@tonic-gate 690Sstevel@tonic-gate static char *myname; 700Sstevel@tonic-gate char fs_path[] = FS_PATH; 710Sstevel@tonic-gate char alt_path[] = ALT_PATH; 720Sstevel@tonic-gate char mnttab[MAXPATHLEN + 1]; 730Sstevel@tonic-gate char *oarg, *farg; 740Sstevel@tonic-gate int maxrun, nrun; 750Sstevel@tonic-gate int no_mnttab; 760Sstevel@tonic-gate int lofscnt; /* presence of lofs prohibits parallel */ 770Sstevel@tonic-gate /* umounting */ 780Sstevel@tonic-gate int exitcode; 790Sstevel@tonic-gate char resolve[MAXPATHLEN]; 800Sstevel@tonic-gate static char ibuf[BUFSIZ]; 810Sstevel@tonic-gate 820Sstevel@tonic-gate /* 830Sstevel@tonic-gate * Currently, mounting cachefs's simultaneous uncovers various problems. 840Sstevel@tonic-gate * For the short term, we serialize cachefs activity while we fix 850Sstevel@tonic-gate * these cachefs bugs. 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate #define CACHEFS_BUG 880Sstevel@tonic-gate #ifdef CACHEFS_BUG 890Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h> /* for BACKMNT_NAME */ 900Sstevel@tonic-gate int cachefs_running; /* parallel cachefs not supported yet */ 910Sstevel@tonic-gate #endif 920Sstevel@tonic-gate 930Sstevel@tonic-gate /* 940Sstevel@tonic-gate * The basic mount struct that describes an mnttab entry. 950Sstevel@tonic-gate * It is used both in an array and as a linked list elem. 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate 980Sstevel@tonic-gate typedef struct mountent { 990Sstevel@tonic-gate struct mnttab ment; /* the mnttab data */ 1000Sstevel@tonic-gate int mlevel; /* mount level of the mount pt */ 1010Sstevel@tonic-gate pid_t pid; /* the pid of this mount process */ 1020Sstevel@tonic-gate #define RDPIPE 0 1030Sstevel@tonic-gate #define WRPIPE 1 1040Sstevel@tonic-gate int sopipe[2]; /* pipe attached to child's stdout */ 1050Sstevel@tonic-gate int sepipe[2]; /* pipe attached to child's stderr */ 1060Sstevel@tonic-gate struct mountent *link; /* used when in linked list */ 1070Sstevel@tonic-gate } mountent_t; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate static mountent_t *mntll; /* head of global linked list of */ 1100Sstevel@tonic-gate /* mountents */ 1110Sstevel@tonic-gate int listlength; /* # of elems in this list */ 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate /* 1140Sstevel@tonic-gate * If the automatic flag (-a) is given and mount points are not specified 1150Sstevel@tonic-gate * on the command line, then do not attempt to umount these. These 1160Sstevel@tonic-gate * generally need to be kept mounted until system shutdown. 1170Sstevel@tonic-gate */ 1180Sstevel@tonic-gate static const char *keeplist[] = { 1190Sstevel@tonic-gate "/", 1200Sstevel@tonic-gate "/dev", 1210Sstevel@tonic-gate "/dev/fd", 1220Sstevel@tonic-gate "/devices", 1230Sstevel@tonic-gate "/etc/mnttab", 1240Sstevel@tonic-gate "/etc/svc/volatile", 1250Sstevel@tonic-gate "/lib", 1260Sstevel@tonic-gate "/proc", 1270Sstevel@tonic-gate "/sbin", 1280Sstevel@tonic-gate CTFS_ROOT, 1290Sstevel@tonic-gate OBJFS_ROOT, 1300Sstevel@tonic-gate "/tmp", 1310Sstevel@tonic-gate "/usr", 1320Sstevel@tonic-gate "/var", 1330Sstevel@tonic-gate "/var/adm", 1340Sstevel@tonic-gate "/var/run", 1350Sstevel@tonic-gate NULL 1360Sstevel@tonic-gate }; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate static void nomem(); 1390Sstevel@tonic-gate static void doexec(struct mnttab *); 1400Sstevel@tonic-gate static int setup_iopipe(mountent_t *); 1410Sstevel@tonic-gate static void setup_output(mountent_t *); 1420Sstevel@tonic-gate static void doio(mountent_t *); 1430Sstevel@tonic-gate static void do_umounts(mountent_t **); 1440Sstevel@tonic-gate static int dowait(); 1450Sstevel@tonic-gate static int parumount(); 1460Sstevel@tonic-gate static int mcompar(const void *, const void *); 1470Sstevel@tonic-gate static void cleanup(int); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate static mountent_t **make_mntarray(char **, int); 1500Sstevel@tonic-gate static mountent_t *getmntall(); 1510Sstevel@tonic-gate static mountent_t *new_mountent(struct mnttab *); 1520Sstevel@tonic-gate static mountent_t *getmntlast(mountent_t *, char *, char *); 1530Sstevel@tonic-gate 154821Sdh145677 int 155821Sdh145677 main(int argc, char **argv) 1560Sstevel@tonic-gate { 1570Sstevel@tonic-gate int cc; 1580Sstevel@tonic-gate struct mnttab mget; 1590Sstevel@tonic-gate char *mname, *is_special; 1600Sstevel@tonic-gate int fscnt; 1610Sstevel@tonic-gate mountent_t *mp; 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 1660Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 1670Sstevel@tonic-gate #endif 1680Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate myname = strrchr(argv[0], '/'); 1710Sstevel@tonic-gate if (myname) 1720Sstevel@tonic-gate myname++; 1730Sstevel@tonic-gate else 1740Sstevel@tonic-gate myname = argv[0]; 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate /* 1770Sstevel@tonic-gate * Process the args. 1780Sstevel@tonic-gate * "-d" for compatibility 1790Sstevel@tonic-gate */ 1800Sstevel@tonic-gate while ((cc = getopt(argc, argv, "ado:Vf?")) != -1) 1810Sstevel@tonic-gate switch (cc) { 1820Sstevel@tonic-gate case 'a': 1830Sstevel@tonic-gate aflg++; 1840Sstevel@tonic-gate break; 1850Sstevel@tonic-gate #ifdef DEBUG 1860Sstevel@tonic-gate case 'd': 1870Sstevel@tonic-gate dflg++; 1880Sstevel@tonic-gate break; 1890Sstevel@tonic-gate #endif 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate case '?': 1920Sstevel@tonic-gate usage(); 1930Sstevel@tonic-gate break; 1940Sstevel@tonic-gate case 'o': 1950Sstevel@tonic-gate if (oflg) 1960Sstevel@tonic-gate usage(); 1970Sstevel@tonic-gate else { 1980Sstevel@tonic-gate oflg++; 1990Sstevel@tonic-gate oarg = optarg; 2000Sstevel@tonic-gate } 2010Sstevel@tonic-gate break; 2020Sstevel@tonic-gate case 'f': 2030Sstevel@tonic-gate fflg++; 2040Sstevel@tonic-gate break; 2050Sstevel@tonic-gate case 'V': 2060Sstevel@tonic-gate if (Vflg) 2070Sstevel@tonic-gate usage(); 2080Sstevel@tonic-gate else 2090Sstevel@tonic-gate Vflg++; 2100Sstevel@tonic-gate break; 2110Sstevel@tonic-gate default: 2120Sstevel@tonic-gate usage(); 2130Sstevel@tonic-gate break; 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate fscnt = argc - optind; 2170Sstevel@tonic-gate if (!aflg && fscnt != 1) 2180Sstevel@tonic-gate usage(); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* copy '--' to specific */ 2210Sstevel@tonic-gate if (strcmp(argv[optind-1], "--") == 0) 2220Sstevel@tonic-gate dashflg++; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* 2250Sstevel@tonic-gate * mnttab may be a symlink to a file in another file system. 2260Sstevel@tonic-gate * This happens during install when / is mounted read-only 2270Sstevel@tonic-gate * and /etc/mnttab is symlinked to a file in /tmp. 2280Sstevel@tonic-gate * If this is the case, we need to follow the symlink to the 2290Sstevel@tonic-gate * read-write file itself so that the subsequent mnttab.temp 2300Sstevel@tonic-gate * open and rename will work. 2310Sstevel@tonic-gate */ 2320Sstevel@tonic-gate if (realpath(MNTTAB, mnttab) == NULL) { 2330Sstevel@tonic-gate strcpy(mnttab, MNTTAB); 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate /* 2370Sstevel@tonic-gate * bugid 1205242 2380Sstevel@tonic-gate * call the realpath() here, so that if the user is 2390Sstevel@tonic-gate * trying to umount an autofs directory, the directory 2400Sstevel@tonic-gate * is forced to mount. 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate mname = argv[optind]; 2440Sstevel@tonic-gate is_special = realpath(mname, resolve); 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * Read the whole mnttab into memory. 2480Sstevel@tonic-gate */ 2490Sstevel@tonic-gate mntll = getmntall(); 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate if (aflg && fscnt != 1) 2520Sstevel@tonic-gate exit(parumount(argv + optind, fscnt)); 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate aflg = 0; 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate mntnull(&mget); 2570Sstevel@tonic-gate if (listlength == 0) { 2580Sstevel@tonic-gate fprintf(stderr, gettext( 2590Sstevel@tonic-gate "%s: warning: no entries found in %s\n"), 2600Sstevel@tonic-gate myname, mnttab); 2610Sstevel@tonic-gate mget.mnt_mountp = mname; /* assume mount point */ 2620Sstevel@tonic-gate no_mnttab++; 2630Sstevel@tonic-gate doexec(&mget); 2640Sstevel@tonic-gate exit(0); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate mp = NULL; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * if realpath fails, it can't be a mount point, so we'll 2710Sstevel@tonic-gate * go straight to the code that treats the arg as a special. 2720Sstevel@tonic-gate * if realpath succeeds, it could be a special or a mount point; 2730Sstevel@tonic-gate * we'll start by assuming it's a mount point, and if it's not, 2740Sstevel@tonic-gate * try to treat it as a special. 2750Sstevel@tonic-gate */ 2760Sstevel@tonic-gate if (is_special != NULL) { 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * if this succeeds, 2790Sstevel@tonic-gate * we'll have the appropriate record; if it fails 2800Sstevel@tonic-gate * we'll assume the arg is a special of some sort 2810Sstevel@tonic-gate */ 2820Sstevel@tonic-gate mp = getmntlast(mntll, NULL, resolve); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate /* 2850Sstevel@tonic-gate * Since stackable mount is allowed (RFE 2001535), 2860Sstevel@tonic-gate * we will un-mount the last entry in the MNTTAB that matches. 2870Sstevel@tonic-gate */ 2880Sstevel@tonic-gate if (mp == NULL) { 2890Sstevel@tonic-gate /* 2900Sstevel@tonic-gate * Perhaps there is a bogus mnttab entry that 2910Sstevel@tonic-gate * can't be resolved: 2920Sstevel@tonic-gate */ 2930Sstevel@tonic-gate if ((mp = getmntlast(mntll, NULL, mname)) == NULL) 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * assume it's a device (special) now 2960Sstevel@tonic-gate */ 2970Sstevel@tonic-gate mp = getmntlast(mntll, mname, NULL); 2980Sstevel@tonic-gate if (mp) { 2990Sstevel@tonic-gate /* 3000Sstevel@tonic-gate * Found it. 3010Sstevel@tonic-gate * This is a device. Now we want to know if 3020Sstevel@tonic-gate * it stackmounted on by something else. 3030Sstevel@tonic-gate * The original fix for bug 1103850 has a 3040Sstevel@tonic-gate * problem with lockfs (bug 1119731). This 3050Sstevel@tonic-gate * is a revised method. 3060Sstevel@tonic-gate */ 3070Sstevel@tonic-gate mountent_t *lmp; 3080Sstevel@tonic-gate lmp = getmntlast(mntll, NULL, mp->ment.mnt_mountp); 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate if (lmp && strcmp(lmp->ment.mnt_special, 3110Sstevel@tonic-gate mp->ment.mnt_special)) { 3120Sstevel@tonic-gate errno = EBUSY; 3130Sstevel@tonic-gate rpterr(mname); 3140Sstevel@tonic-gate exit(1); 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate } else { 3170Sstevel@tonic-gate fprintf(stderr, gettext( 3180Sstevel@tonic-gate "%s: warning: %s not in mnttab\n"), 3190Sstevel@tonic-gate myname, mname); 3200Sstevel@tonic-gate if (Vflg) 3210Sstevel@tonic-gate exit(1); 3220Sstevel@tonic-gate /* 3230Sstevel@tonic-gate * same error as mount -V 3240Sstevel@tonic-gate * would give for unknown 3250Sstevel@tonic-gate * mount point 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate mget.mnt_special = mget.mnt_mountp = mname; 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate if (mp) 3320Sstevel@tonic-gate doexec(&mp->ment); 3330Sstevel@tonic-gate else 3340Sstevel@tonic-gate doexec(&mget); 3350Sstevel@tonic-gate 336821Sdh145677 return (0); 3370Sstevel@tonic-gate } 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate void 3400Sstevel@tonic-gate doexec(struct mnttab *ment) 3410Sstevel@tonic-gate { 3420Sstevel@tonic-gate int ret; 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate #ifdef DEBUG 3450Sstevel@tonic-gate if (dflg) 3460Sstevel@tonic-gate fprintf(stderr, "%d: umounting %s\n", 3470Sstevel@tonic-gate getpid(), ment->mnt_mountp); 3480Sstevel@tonic-gate #endif 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate /* try to exec the dependent portion */ 3510Sstevel@tonic-gate if ((ment->mnt_fstype != NULL) || Vflg) { 3520Sstevel@tonic-gate char full_path[FULLPATH_MAX]; 3530Sstevel@tonic-gate char alter_path[FULLPATH_MAX]; 3540Sstevel@tonic-gate char *newargv[ARGV_MAX]; 3550Sstevel@tonic-gate int ii; 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate if (strlen(ment->mnt_fstype) > (size_t)FSTYPE_MAX) { 3580Sstevel@tonic-gate fprintf(stderr, gettext( 3590Sstevel@tonic-gate "%s: FSType %s exceeds %d characters\n"), 3600Sstevel@tonic-gate myname, ment->mnt_fstype, FSTYPE_MAX); 3610Sstevel@tonic-gate exit(1); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* build the full pathname of the fstype dependent command. */ 3650Sstevel@tonic-gate sprintf(full_path, "%s/%s/%s", fs_path, ment->mnt_fstype, 3660Sstevel@tonic-gate myname); 3670Sstevel@tonic-gate sprintf(alter_path, "%s/%s/%s", alt_path, ment->mnt_fstype, 3680Sstevel@tonic-gate myname); 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * create the new arg list, and end the list with a 3720Sstevel@tonic-gate * null pointer 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate ii = 2; 3750Sstevel@tonic-gate if (oflg) { 3760Sstevel@tonic-gate newargv[ii++] = "-o"; 3770Sstevel@tonic-gate newargv[ii++] = oarg; 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate if (dashflg) { 3800Sstevel@tonic-gate newargv[ii++] = "--"; 3810Sstevel@tonic-gate } 3820Sstevel@tonic-gate if (fflg) { 3830Sstevel@tonic-gate newargv[ii++] = "-f"; 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate newargv[ii++] = (ment->mnt_mountp) 3860Sstevel@tonic-gate ? ment->mnt_mountp : ment->mnt_special; 3870Sstevel@tonic-gate newargv[ii] = NULL; 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate /* set the new argv[0] to the filename */ 3900Sstevel@tonic-gate newargv[1] = myname; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate if (Vflg) { 3930Sstevel@tonic-gate printf("%s", myname); 3940Sstevel@tonic-gate for (ii = 2; newargv[ii]; ii++) 3950Sstevel@tonic-gate printf(" %s", newargv[ii]); 3960Sstevel@tonic-gate printf("\n"); 3970Sstevel@tonic-gate fflush(stdout); 3980Sstevel@tonic-gate exit(0); 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate /* Try to exec the fstype dependent umount. */ 4020Sstevel@tonic-gate execv(full_path, &newargv[1]); 4030Sstevel@tonic-gate if (errno == ENOEXEC) { 4040Sstevel@tonic-gate newargv[0] = "sh"; 4050Sstevel@tonic-gate newargv[1] = full_path; 4060Sstevel@tonic-gate execv("/sbin/sh", &newargv[0]); 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate newargv[1] = myname; 4090Sstevel@tonic-gate execv(alter_path, &newargv[1]); 4100Sstevel@tonic-gate if (errno == ENOEXEC) { 4110Sstevel@tonic-gate newargv[0] = "sh"; 4120Sstevel@tonic-gate newargv[1] = alter_path; 4130Sstevel@tonic-gate execv("/sbin/sh", &newargv[0]); 4140Sstevel@tonic-gate } 4150Sstevel@tonic-gate /* exec failed */ 4160Sstevel@tonic-gate if (errno != ENOENT) { 4170Sstevel@tonic-gate fprintf(stderr, gettext("umount: cannot execute %s\n"), 4180Sstevel@tonic-gate full_path); 4190Sstevel@tonic-gate exit(1); 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate /* 4230Sstevel@tonic-gate * No fstype independent executable then. We'll go generic 4240Sstevel@tonic-gate * from here. 4250Sstevel@tonic-gate */ 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate /* don't use -o with generic */ 4280Sstevel@tonic-gate if (oflg) { 4290Sstevel@tonic-gate fprintf(stderr, gettext( 4300Sstevel@tonic-gate "%s: %s specific umount does not exist; -o suboption ignored\n"), 4310Sstevel@tonic-gate myname, ment->mnt_fstype ? ment->mnt_fstype : "<null>"); 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate signal(SIGHUP, SIG_IGN); 4350Sstevel@tonic-gate signal(SIGQUIT, SIG_IGN); 4360Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 4370Sstevel@tonic-gate /* 4380Sstevel@tonic-gate * Try to umount the mountpoint. 4390Sstevel@tonic-gate * If that fails, try the corresponding special. 4400Sstevel@tonic-gate * (This ordering is necessary for nfs umounts.) 4410Sstevel@tonic-gate * (for remote resources: if the first umount returns EBUSY 4420Sstevel@tonic-gate * don't call umount again - umount() with a resource name 4430Sstevel@tonic-gate * will return a misleading error to the user 4440Sstevel@tonic-gate */ 4450Sstevel@tonic-gate if (fflg) { 4460Sstevel@tonic-gate if (((ret = umount2(ment->mnt_mountp, MS_FORCE)) < 0) && 4470Sstevel@tonic-gate (errno != EBUSY && errno != ENOTSUP && 4480Sstevel@tonic-gate errno != EPERM)) 4490Sstevel@tonic-gate ret = umount2(ment->mnt_special, MS_FORCE); 4500Sstevel@tonic-gate } else { 4510Sstevel@tonic-gate if (((ret = umount2(ment->mnt_mountp, 0)) < 0) && 4520Sstevel@tonic-gate (errno != EBUSY) && (errno != EPERM)) 4530Sstevel@tonic-gate ret = umount2(ment->mnt_special, 0); 4540Sstevel@tonic-gate } 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate if (ret < 0) { 4570Sstevel@tonic-gate rpterr(ment->mnt_mountp); 4580Sstevel@tonic-gate if (errno != EINVAL && errno != EFAULT) 4590Sstevel@tonic-gate exit(1); 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate exitcode = 1; 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate exit(exitcode); 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate void 468821Sdh145677 rpterr(char *sp) 4690Sstevel@tonic-gate { 4700Sstevel@tonic-gate switch (errno) { 4710Sstevel@tonic-gate case EPERM: 4720Sstevel@tonic-gate fprintf(stderr, gettext("%s: permission denied\n"), myname); 4730Sstevel@tonic-gate break; 4740Sstevel@tonic-gate case ENXIO: 4750Sstevel@tonic-gate fprintf(stderr, gettext("%s: %s no device\n"), myname, sp); 4760Sstevel@tonic-gate break; 4770Sstevel@tonic-gate case ENOENT: 4780Sstevel@tonic-gate fprintf(stderr, 4790Sstevel@tonic-gate gettext("%s: %s no such file or directory\n"), 4800Sstevel@tonic-gate myname, sp); 4810Sstevel@tonic-gate break; 4820Sstevel@tonic-gate case EINVAL: 4830Sstevel@tonic-gate fprintf(stderr, gettext("%s: %s not mounted\n"), myname, sp); 4840Sstevel@tonic-gate break; 4850Sstevel@tonic-gate case EBUSY: 4860Sstevel@tonic-gate fprintf(stderr, gettext("%s: %s busy\n"), myname, sp); 4870Sstevel@tonic-gate break; 4880Sstevel@tonic-gate case ENOTBLK: 4890Sstevel@tonic-gate fprintf(stderr, 4900Sstevel@tonic-gate gettext("%s: %s block device required\n"), myname, sp); 4910Sstevel@tonic-gate break; 4920Sstevel@tonic-gate case ECOMM: 4930Sstevel@tonic-gate fprintf(stderr, 4940Sstevel@tonic-gate gettext("%s: warning: broken link detected\n"), myname); 4950Sstevel@tonic-gate break; 4960Sstevel@tonic-gate default: 4970Sstevel@tonic-gate perror(myname); 4980Sstevel@tonic-gate fprintf(stderr, gettext("%s: cannot unmount %s\n"), myname, sp); 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate void 503821Sdh145677 usage(void) 5040Sstevel@tonic-gate { 5050Sstevel@tonic-gate fprintf(stderr, gettext( 5060Sstevel@tonic-gate "Usage:\n%s [-f] [-V] [-o specific_options] {special | mount-point}\n"), 5070Sstevel@tonic-gate myname); 5080Sstevel@tonic-gate fprintf(stderr, gettext( 5090Sstevel@tonic-gate "%s -a [-f] [-V] [-o specific_options] [mount_point ...]\n"), myname); 5100Sstevel@tonic-gate exit(1); 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate void 514821Sdh145677 mnterror(int flag) 5150Sstevel@tonic-gate { 5160Sstevel@tonic-gate switch (flag) { 5170Sstevel@tonic-gate case MNT_TOOLONG: 5180Sstevel@tonic-gate fprintf(stderr, 5190Sstevel@tonic-gate gettext("%s: line in mnttab exceeds %d characters\n"), 5200Sstevel@tonic-gate myname, MNT_LINE_MAX-2); 5210Sstevel@tonic-gate break; 5220Sstevel@tonic-gate case MNT_TOOFEW: 5230Sstevel@tonic-gate fprintf(stderr, 5240Sstevel@tonic-gate gettext("%s: line in mnttab has too few entries\n"), 5250Sstevel@tonic-gate myname); 5260Sstevel@tonic-gate break; 5270Sstevel@tonic-gate default: 5280Sstevel@tonic-gate break; 5290Sstevel@tonic-gate } 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate /* 5330Sstevel@tonic-gate * Search the mlist linked list for the 5340Sstevel@tonic-gate * first match of specp or mntp. The list is expected to be in reverse 5350Sstevel@tonic-gate * order of /etc/mnttab. 5360Sstevel@tonic-gate * If both are specified, then both have to match. 5370Sstevel@tonic-gate * Returns the (mountent_t *) of the match, otherwise returns NULL. 5380Sstevel@tonic-gate */ 5390Sstevel@tonic-gate mountent_t * 5400Sstevel@tonic-gate getmntlast(mountent_t *mlist, char *specp, char *mntp) 5410Sstevel@tonic-gate { 5420Sstevel@tonic-gate int mfound, sfound; 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate for (/* */; mlist; mlist = mlist->link) { 5450Sstevel@tonic-gate mfound = sfound = 0; 5460Sstevel@tonic-gate if (mntp && (strcmp(mlist->ment.mnt_mountp, mntp) == 0)) { 5470Sstevel@tonic-gate if (specp == NULL) 5480Sstevel@tonic-gate return (mlist); 5490Sstevel@tonic-gate mfound++; 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate if (specp && (strcmp(mlist->ment.mnt_special, specp) == 0)) { 5520Sstevel@tonic-gate if (mntp == NULL) 5530Sstevel@tonic-gate return (mlist); 5540Sstevel@tonic-gate sfound++; 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate if (mfound && sfound) 5570Sstevel@tonic-gate return (mlist); 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate return (NULL); 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate /* 5650Sstevel@tonic-gate * Perform the parallel version of umount. Returns 0 if no errors occurred, 5660Sstevel@tonic-gate * non zero otherwise. 5670Sstevel@tonic-gate */ 5680Sstevel@tonic-gate int 5690Sstevel@tonic-gate parumount(char **mntlist, int count) 5700Sstevel@tonic-gate { 5710Sstevel@tonic-gate int maxfd = OPEN_MAX; 5720Sstevel@tonic-gate struct rlimit rl; 5730Sstevel@tonic-gate mountent_t **mntarray, **ml, *mp; 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate /* 5760Sstevel@tonic-gate * If no mount points are specified and none were found in mnttab, 5770Sstevel@tonic-gate * then end it all here. 5780Sstevel@tonic-gate */ 5790Sstevel@tonic-gate if (count == 0 && mntll == NULL) 5800Sstevel@tonic-gate return (0); 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate /* 5830Sstevel@tonic-gate * This is the process scaling section. After running a series 5840Sstevel@tonic-gate * of tests based on the number of simultaneous processes and 5850Sstevel@tonic-gate * processors available, optimum performance was achieved near or 5860Sstevel@tonic-gate * at (PROCN * 2). 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1) 5890Sstevel@tonic-gate maxrun = 4; 5900Sstevel@tonic-gate else 5910Sstevel@tonic-gate maxrun = maxrun * 2 + 1; 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { 5940Sstevel@tonic-gate rl.rlim_cur = rl.rlim_max; 5950Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rl) == 0) 5960Sstevel@tonic-gate maxfd = (int)rl.rlim_cur; 597*1914Scasper (void) enable_extended_FILE_stdio(-1, -1); 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate /* 6010Sstevel@tonic-gate * The parent needs to maintain 3 of its own fd's, plus 2 for 6020Sstevel@tonic-gate * each child (the stdout and stderr pipes). 6030Sstevel@tonic-gate */ 6040Sstevel@tonic-gate maxfd = (maxfd / 2) - 6; /* 6 takes care of temporary */ 6050Sstevel@tonic-gate /* periods of open fds */ 6060Sstevel@tonic-gate if (maxfd < maxrun) 6070Sstevel@tonic-gate maxrun = maxfd; 6080Sstevel@tonic-gate if (maxrun < 4) 6090Sstevel@tonic-gate maxrun = 4; /* sanity check */ 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate mntarray = make_mntarray(mntlist, count); 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate if (listlength == 0) { 6140Sstevel@tonic-gate if (count == 0) /* not an error, just none found */ 6150Sstevel@tonic-gate return (0); 6160Sstevel@tonic-gate fprintf(stderr, gettext("%s: no valid entries found in %s\n"), 6170Sstevel@tonic-gate myname, mnttab); 6180Sstevel@tonic-gate return (1); 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate /* 6220Sstevel@tonic-gate * Sort the entries based on their mount level only if lofs's are 6230Sstevel@tonic-gate * not present. 6240Sstevel@tonic-gate */ 6250Sstevel@tonic-gate if (lofscnt == 0) { 6260Sstevel@tonic-gate qsort((void *)mntarray, listlength, sizeof (mountent_t *), 6270Sstevel@tonic-gate mcompar); 6280Sstevel@tonic-gate /* 6290Sstevel@tonic-gate * If we do not detect a lofs by now, we never will. 6300Sstevel@tonic-gate */ 6310Sstevel@tonic-gate lofscnt = -1; 6320Sstevel@tonic-gate } 6330Sstevel@tonic-gate /* 6340Sstevel@tonic-gate * Now link them up so that a given pid is easier to find when 6350Sstevel@tonic-gate * we go to clean up after they are done. 6360Sstevel@tonic-gate */ 6370Sstevel@tonic-gate mntll = mntarray[0]; 6380Sstevel@tonic-gate for (ml = mntarray; mp = *ml; /* */) 6390Sstevel@tonic-gate mp->link = *++ml; 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * Try to handle interrupts in a reasonable way. 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate sigset(SIGHUP, cleanup); 6450Sstevel@tonic-gate sigset(SIGQUIT, cleanup); 6460Sstevel@tonic-gate sigset(SIGINT, cleanup); 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate do_umounts(mntarray); /* do the umounts */ 6490Sstevel@tonic-gate return (exitcode); 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate /* 6530Sstevel@tonic-gate * Returns a mountent_t array based on mntlist. If mntlist is NULL, then 6540Sstevel@tonic-gate * it returns all mnttab entries with a few exceptions. Sets the global 6550Sstevel@tonic-gate * variable listlength to the number of entries in the array. 6560Sstevel@tonic-gate */ 6570Sstevel@tonic-gate mountent_t ** 6580Sstevel@tonic-gate make_mntarray(char **mntlist, int count) 6590Sstevel@tonic-gate { 6600Sstevel@tonic-gate mountent_t *mp, **mpp; 6610Sstevel@tonic-gate int ndx; 6620Sstevel@tonic-gate char *cp; 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate if (count > 0) 6650Sstevel@tonic-gate listlength = count; 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate mpp = (mountent_t **)malloc(sizeof (*mp) * (listlength + 1)); 6680Sstevel@tonic-gate if (mpp == NULL) 6690Sstevel@tonic-gate nomem(); 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate if (count == 0) { 6720Sstevel@tonic-gate if (mntll == NULL) { /* no entries? */ 6730Sstevel@tonic-gate listlength = 0; 6740Sstevel@tonic-gate return (NULL); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate /* 6770Sstevel@tonic-gate * No mount list specified: take all mnttab mount points 6780Sstevel@tonic-gate * except for a few cases. 6790Sstevel@tonic-gate */ 6800Sstevel@tonic-gate for (ndx = 0, mp = mntll; mp; mp = mp->link) { 6810Sstevel@tonic-gate if (fsstrinlist(mp->ment.mnt_mountp, keeplist)) 6820Sstevel@tonic-gate continue; 6830Sstevel@tonic-gate mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp); 6840Sstevel@tonic-gate if (mp->ment.mnt_fstype && 6850Sstevel@tonic-gate (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0)) 6860Sstevel@tonic-gate lofscnt++; 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate mpp[ndx++] = mp; 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate mpp[ndx] = NULL; 6910Sstevel@tonic-gate listlength = ndx; 6920Sstevel@tonic-gate return (mpp); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate /* 6960Sstevel@tonic-gate * A list of mount points was specified on the command line. 6970Sstevel@tonic-gate * Build an array out of these. 6980Sstevel@tonic-gate */ 6990Sstevel@tonic-gate for (ndx = 0; count--; ) { 7000Sstevel@tonic-gate cp = *mntlist++; 7010Sstevel@tonic-gate if (realpath(cp, resolve) == NULL) { 7020Sstevel@tonic-gate fprintf(stderr, 7030Sstevel@tonic-gate gettext("%s: warning: can't resolve %s\n"), 7040Sstevel@tonic-gate myname, cp); 7050Sstevel@tonic-gate exitcode = 1; 7060Sstevel@tonic-gate mp = getmntlast(mntll, NULL, cp); /* try anyways */ 7070Sstevel@tonic-gate } else 7080Sstevel@tonic-gate mp = getmntlast(mntll, NULL, resolve); 7090Sstevel@tonic-gate if (mp == NULL) { 7100Sstevel@tonic-gate struct mnttab mnew; 7110Sstevel@tonic-gate /* 7120Sstevel@tonic-gate * Then we've reached the end without finding 7130Sstevel@tonic-gate * what we are looking for, but we still have to 7140Sstevel@tonic-gate * try to umount it: append it to mntarray. 7150Sstevel@tonic-gate */ 7160Sstevel@tonic-gate fprintf(stderr, gettext( 7170Sstevel@tonic-gate "%s: warning: %s not found in %s\n"), 7180Sstevel@tonic-gate myname, resolve, mnttab); 7190Sstevel@tonic-gate exitcode = 1; 7200Sstevel@tonic-gate mntnull(&mnew); 7210Sstevel@tonic-gate mnew.mnt_special = mnew.mnt_mountp = strdup(resolve); 7220Sstevel@tonic-gate if (mnew.mnt_special == NULL) 7230Sstevel@tonic-gate nomem(); 7240Sstevel@tonic-gate mp = new_mountent(&mnew); 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate if (mp->ment.mnt_fstype && 7270Sstevel@tonic-gate (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0)) 7280Sstevel@tonic-gate lofscnt++; 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate mp->mlevel = fsgetmlevel(mp->ment.mnt_mountp); 7310Sstevel@tonic-gate mpp[ndx++] = mp; 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate mpp[ndx] = NULL; 7340Sstevel@tonic-gate listlength = ndx; 7350Sstevel@tonic-gate return (mpp); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate /* 7390Sstevel@tonic-gate * Returns the tail of a linked list of all mnttab entries. I.e, it's faster 7400Sstevel@tonic-gate * to return the mnttab in reverse order. 7410Sstevel@tonic-gate * Sets listlength to the number of entries in the list. 7420Sstevel@tonic-gate * Returns NULL if none are found. 7430Sstevel@tonic-gate */ 7440Sstevel@tonic-gate mountent_t * 745821Sdh145677 getmntall(void) 7460Sstevel@tonic-gate { 7470Sstevel@tonic-gate FILE *fp; 7480Sstevel@tonic-gate mountent_t *mtail; 7490Sstevel@tonic-gate int cnt = 0, ret; 7500Sstevel@tonic-gate struct mnttab mget; 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate if ((fp = fopen(mnttab, "r")) == NULL) { 7530Sstevel@tonic-gate fprintf(stderr, gettext("%s: warning cannot open %s\n"), 7540Sstevel@tonic-gate myname, mnttab); 7550Sstevel@tonic-gate return (0); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate mtail = NULL; 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate while ((ret = getmntent(fp, &mget)) != -1) { 7600Sstevel@tonic-gate mountent_t *mp; 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate if (ret > 0) { 7630Sstevel@tonic-gate mnterror(ret); 7640Sstevel@tonic-gate continue; 7650Sstevel@tonic-gate } 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate mp = new_mountent(&mget); 7680Sstevel@tonic-gate mp->link = mtail; 7690Sstevel@tonic-gate mtail = mp; 7700Sstevel@tonic-gate cnt++; 7710Sstevel@tonic-gate } 7720Sstevel@tonic-gate fclose(fp); 7730Sstevel@tonic-gate if (mtail == NULL) { 7740Sstevel@tonic-gate listlength = 0; 7750Sstevel@tonic-gate return (NULL); 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate listlength = cnt; 7780Sstevel@tonic-gate return (mtail); 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate void 7820Sstevel@tonic-gate do_umounts(mountent_t **mntarray) 7830Sstevel@tonic-gate { 7840Sstevel@tonic-gate mountent_t *mp, *mpprev, **ml = mntarray; 7850Sstevel@tonic-gate int cnt = listlength; 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate /* 7880Sstevel@tonic-gate * Main loop for the forked children: 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate for (mpprev = *ml; mp = *ml; mpprev = mp, ml++, cnt--) { 7910Sstevel@tonic-gate pid_t pid; 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate /* 7940Sstevel@tonic-gate * Check to see if we cross a mount level: e.g., 7950Sstevel@tonic-gate * /a/b/c -> /a/b. If so, we need to wait for all current 7960Sstevel@tonic-gate * umounts to finish before umounting the rest. 7970Sstevel@tonic-gate * 7980Sstevel@tonic-gate * Also, we unmount serially as long as there are lofs's 7990Sstevel@tonic-gate * to mount to avoid improper umount ordering. 8000Sstevel@tonic-gate */ 8010Sstevel@tonic-gate if (mp->mlevel < mpprev->mlevel || lofscnt > 0) 8020Sstevel@tonic-gate while (nrun > 0 && (dowait() != -1)) 8030Sstevel@tonic-gate ; 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate if (lofscnt == 0) { 8060Sstevel@tonic-gate /* 8070Sstevel@tonic-gate * We can now go to parallel umounting. 8080Sstevel@tonic-gate */ 8090Sstevel@tonic-gate qsort((void *)ml, cnt, sizeof (mountent_t *), mcompar); 8100Sstevel@tonic-gate mp = *ml; /* possible first entry */ 8110Sstevel@tonic-gate lofscnt--; /* so we don't do this again */ 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate while (setup_iopipe(mp) == -1 && (dowait() != -1)) 8150Sstevel@tonic-gate ; 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate while (nrun >= maxrun && (dowait() != -1)) /* throttle */ 8180Sstevel@tonic-gate ; 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate #ifdef CACHEFS_BUG 8210Sstevel@tonic-gate /* 8220Sstevel@tonic-gate * If this is the back file system, then let cachefs/umount 8230Sstevel@tonic-gate * unmount it. 8240Sstevel@tonic-gate */ 8250Sstevel@tonic-gate if (strstr(mp->ment.mnt_mountp, BACKMNT_NAME)) 8260Sstevel@tonic-gate continue; 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate if (mp->ment.mnt_fstype && 8300Sstevel@tonic-gate (strcmp(mp->ment.mnt_fstype, "cachefs") == 0)) { 8310Sstevel@tonic-gate while (cachefs_running && (dowait() != -1)) 8320Sstevel@tonic-gate ; 8330Sstevel@tonic-gate cachefs_running = 1; 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate #endif 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate if ((pid = fork()) == -1) { 8380Sstevel@tonic-gate perror("fork"); 8390Sstevel@tonic-gate cleanup(-1); 8400Sstevel@tonic-gate /* not reached */ 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate #ifdef DEBUG 8430Sstevel@tonic-gate if (dflg && pid > 0) { 8440Sstevel@tonic-gate fprintf(stderr, "parent %d: umounting %d %s\n", 8450Sstevel@tonic-gate getpid(), pid, mp->ment.mnt_mountp); 8460Sstevel@tonic-gate } 8470Sstevel@tonic-gate #endif 8480Sstevel@tonic-gate if (pid == 0) { /* child */ 8490Sstevel@tonic-gate signal(SIGHUP, SIG_IGN); 8500Sstevel@tonic-gate signal(SIGQUIT, SIG_IGN); 8510Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 8520Sstevel@tonic-gate setup_output(mp); 8530Sstevel@tonic-gate doexec(&mp->ment); 8540Sstevel@tonic-gate perror("exec"); 8550Sstevel@tonic-gate exit(1); 8560Sstevel@tonic-gate } 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate /* parent */ 8590Sstevel@tonic-gate (void) close(mp->sopipe[WRPIPE]); 8600Sstevel@tonic-gate (void) close(mp->sepipe[WRPIPE]); 8610Sstevel@tonic-gate mp->pid = pid; 8620Sstevel@tonic-gate nrun++; 8630Sstevel@tonic-gate } 8640Sstevel@tonic-gate cleanup(0); 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate /* 8680Sstevel@tonic-gate * cleanup the existing children and exit with an error 8690Sstevel@tonic-gate * if asig != 0. 8700Sstevel@tonic-gate */ 8710Sstevel@tonic-gate void 8720Sstevel@tonic-gate cleanup(int asig) 8730Sstevel@tonic-gate { 8740Sstevel@tonic-gate /* 8750Sstevel@tonic-gate * Let the stragglers finish. 8760Sstevel@tonic-gate */ 8770Sstevel@tonic-gate while (nrun > 0 && (dowait() != -1)) 8780Sstevel@tonic-gate ; 8790Sstevel@tonic-gate if (asig != 0) 8800Sstevel@tonic-gate exit(1); 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate /* 8850Sstevel@tonic-gate * Waits for 1 child to die. 8860Sstevel@tonic-gate * 8870Sstevel@tonic-gate * Returns -1 if no children are left to wait for. 8880Sstevel@tonic-gate * Returns 0 if a child died without an error. 8890Sstevel@tonic-gate * Returns 1 if a child died with an error. 8900Sstevel@tonic-gate * Sets the global exitcode if an error occurred. 8910Sstevel@tonic-gate */ 8920Sstevel@tonic-gate int 893821Sdh145677 dowait(void) 8940Sstevel@tonic-gate { 8950Sstevel@tonic-gate int wstat, child, ret; 8960Sstevel@tonic-gate mountent_t *mp, *prevp; 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate if ((child = wait(&wstat)) == -1) 8990Sstevel@tonic-gate return (-1); 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate if (WIFEXITED(wstat)) /* this should always be true */ 9020Sstevel@tonic-gate ret = WEXITSTATUS(wstat); 9030Sstevel@tonic-gate else 9040Sstevel@tonic-gate ret = 1; /* assume some kind of error */ 9050Sstevel@tonic-gate nrun--; 9060Sstevel@tonic-gate if (ret) 9070Sstevel@tonic-gate exitcode = 1; 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate /* 9100Sstevel@tonic-gate * Find our child so we can process its std output, if any. 9110Sstevel@tonic-gate * This search gets smaller and smaller as children are cleaned 9120Sstevel@tonic-gate * up. 9130Sstevel@tonic-gate */ 9140Sstevel@tonic-gate for (prevp = NULL, mp = mntll; mp; mp = mp->link) { 9150Sstevel@tonic-gate if (mp->pid != child) { 9160Sstevel@tonic-gate prevp = mp; 9170Sstevel@tonic-gate continue; 9180Sstevel@tonic-gate } 9190Sstevel@tonic-gate /* 9200Sstevel@tonic-gate * Found: let's remove it from this list. 9210Sstevel@tonic-gate */ 9220Sstevel@tonic-gate if (prevp) { 9230Sstevel@tonic-gate prevp->link = mp->link; 9240Sstevel@tonic-gate mp->link = NULL; 9250Sstevel@tonic-gate } 9260Sstevel@tonic-gate break; 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate if (mp == NULL) { 9300Sstevel@tonic-gate /* 9310Sstevel@tonic-gate * This should never happen. 9320Sstevel@tonic-gate */ 9330Sstevel@tonic-gate #ifdef DEBUG 9340Sstevel@tonic-gate fprintf(stderr, gettext( 9350Sstevel@tonic-gate "%s: unknown child %d\n"), myname, child); 9360Sstevel@tonic-gate #endif 9370Sstevel@tonic-gate exitcode = 1; 9380Sstevel@tonic-gate return (1); 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate doio(mp); /* Any output? */ 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate if (mp->ment.mnt_fstype && 9430Sstevel@tonic-gate (strcmp(mp->ment.mnt_fstype, MNTTYPE_LOFS) == 0)) 9440Sstevel@tonic-gate lofscnt--; 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate #ifdef CACHEFS_BUG 9470Sstevel@tonic-gate if (mp->ment.mnt_fstype && 9480Sstevel@tonic-gate (strcmp(mp->ment.mnt_fstype, "cachefs") == 0)) 9490Sstevel@tonic-gate cachefs_running = 0; 9500Sstevel@tonic-gate #endif 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate return (ret); 9530Sstevel@tonic-gate } 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate static const mountent_t zmount = { 0 }; 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate mountent_t * 9580Sstevel@tonic-gate new_mountent(struct mnttab *ment) 9590Sstevel@tonic-gate { 9600Sstevel@tonic-gate mountent_t *new; 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate new = (mountent_t *)malloc(sizeof (*new)); 9630Sstevel@tonic-gate if (new == NULL) 9640Sstevel@tonic-gate nomem(); 9650Sstevel@tonic-gate 9660Sstevel@tonic-gate *new = zmount; 9670Sstevel@tonic-gate if (ment->mnt_special && 9680Sstevel@tonic-gate (new->ment.mnt_special = strdup(ment->mnt_special)) == NULL) 9690Sstevel@tonic-gate nomem(); 9700Sstevel@tonic-gate if (ment->mnt_mountp && 9710Sstevel@tonic-gate (new->ment.mnt_mountp = strdup(ment->mnt_mountp)) == NULL) 9720Sstevel@tonic-gate nomem(); 9730Sstevel@tonic-gate if (ment->mnt_fstype && 9740Sstevel@tonic-gate (new->ment.mnt_fstype = strdup(ment->mnt_fstype)) == NULL) 9750Sstevel@tonic-gate nomem(); 9760Sstevel@tonic-gate return (new); 9770Sstevel@tonic-gate } 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate /* 9810Sstevel@tonic-gate * Sort in descending order of "mount level". For example, /a/b/c is 9820Sstevel@tonic-gate * placed before /a/b . 9830Sstevel@tonic-gate */ 9840Sstevel@tonic-gate int 9850Sstevel@tonic-gate mcompar(const void *a, const void *b) 9860Sstevel@tonic-gate { 9870Sstevel@tonic-gate mountent_t *a1, *b1; 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate a1 = *(mountent_t **)a; 9900Sstevel@tonic-gate b1 = *(mountent_t **)b; 9910Sstevel@tonic-gate return (b1->mlevel - a1->mlevel); 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate /* 9950Sstevel@tonic-gate * The purpose of this routine is to form stdout and stderr 9960Sstevel@tonic-gate * pipes for the children's output. The parent then reads and writes it 9970Sstevel@tonic-gate * out it serially in order to ensure that the output is 9980Sstevel@tonic-gate * not garbled. 9990Sstevel@tonic-gate */ 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate int 10020Sstevel@tonic-gate setup_iopipe(mountent_t *mp) 10030Sstevel@tonic-gate { 10040Sstevel@tonic-gate /* 10050Sstevel@tonic-gate * Make a stdout and stderr pipe. This should never fail. 10060Sstevel@tonic-gate */ 10070Sstevel@tonic-gate if (pipe(mp->sopipe) == -1) 10080Sstevel@tonic-gate return (-1); 10090Sstevel@tonic-gate if (pipe(mp->sepipe) == -1) { 10100Sstevel@tonic-gate (void) close(mp->sopipe[RDPIPE]); 10110Sstevel@tonic-gate (void) close(mp->sopipe[WRPIPE]); 10120Sstevel@tonic-gate return (-1); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate /* 10150Sstevel@tonic-gate * Don't block on an empty pipe. 10160Sstevel@tonic-gate */ 10170Sstevel@tonic-gate (void) fcntl(mp->sopipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK); 10180Sstevel@tonic-gate (void) fcntl(mp->sepipe[RDPIPE], F_SETFL, O_NDELAY|O_NONBLOCK); 10190Sstevel@tonic-gate return (0); 10200Sstevel@tonic-gate } 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate /* 10230Sstevel@tonic-gate * Called by a child to attach its stdout and stderr to the write side of 10240Sstevel@tonic-gate * the pipes. 10250Sstevel@tonic-gate */ 10260Sstevel@tonic-gate void 10270Sstevel@tonic-gate setup_output(mountent_t *mp) 10280Sstevel@tonic-gate { 10290Sstevel@tonic-gate (void) close(fileno(stdout)); 10300Sstevel@tonic-gate (void) dup(mp->sopipe[WRPIPE]); 10310Sstevel@tonic-gate (void) close(mp->sopipe[WRPIPE]); 10320Sstevel@tonic-gate 10330Sstevel@tonic-gate (void) close(fileno(stderr)); 10340Sstevel@tonic-gate (void) dup(mp->sepipe[WRPIPE]); 10350Sstevel@tonic-gate (void) close(mp->sepipe[WRPIPE]); 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate /* 10390Sstevel@tonic-gate * Parent uses this to print any stdout or stderr output issued by 10400Sstevel@tonic-gate * the child. 10410Sstevel@tonic-gate */ 10420Sstevel@tonic-gate static void 10430Sstevel@tonic-gate doio(mountent_t *mp) 10440Sstevel@tonic-gate { 10450Sstevel@tonic-gate int bytes; 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate while ((bytes = read(mp->sepipe[RDPIPE], ibuf, sizeof (ibuf))) > 0) 10480Sstevel@tonic-gate write(fileno(stderr), ibuf, bytes); 10490Sstevel@tonic-gate while ((bytes = read(mp->sopipe[RDPIPE], ibuf, sizeof (ibuf))) > 0) 10500Sstevel@tonic-gate write(fileno(stdout), ibuf, bytes); 10510Sstevel@tonic-gate 10520Sstevel@tonic-gate (void) close(mp->sopipe[RDPIPE]); 10530Sstevel@tonic-gate (void) close(mp->sepipe[RDPIPE]); 10540Sstevel@tonic-gate } 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate void 1057821Sdh145677 nomem(void) 10580Sstevel@tonic-gate { 10590Sstevel@tonic-gate fprintf(stderr, gettext("%s: out of memory\n"), myname); 10600Sstevel@tonic-gate /* 10610Sstevel@tonic-gate * Let the stragglers finish. 10620Sstevel@tonic-gate */ 10630Sstevel@tonic-gate while (nrun > 0 && (dowait() != -1)) 10640Sstevel@tonic-gate ; 10650Sstevel@tonic-gate exit(1); 10660Sstevel@tonic-gate } 1067