xref: /illumos-gate/usr/src/lib/libvolmgt/common/volmgt_on_private.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*004388ebScasper  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * routines in this module are meant to be called by other libvolmgt
307c478bd9Sstevel@tonic-gate  * routines only
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include	<stdio.h>
347c478bd9Sstevel@tonic-gate #include	<string.h>
357c478bd9Sstevel@tonic-gate #include	<dirent.h>
367c478bd9Sstevel@tonic-gate #include	<fcntl.h>
377c478bd9Sstevel@tonic-gate #include	<string.h>
387c478bd9Sstevel@tonic-gate #ifdef	DEBUG
397c478bd9Sstevel@tonic-gate #include	<errno.h>
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate #include	<libintl.h>
427c478bd9Sstevel@tonic-gate #include	<limits.h>
437c478bd9Sstevel@tonic-gate #include	<unistd.h>
447c478bd9Sstevel@tonic-gate #include	<stdlib.h>
457c478bd9Sstevel@tonic-gate #include	<volmgt.h>
467c478bd9Sstevel@tonic-gate #include	<sys/types.h>
477c478bd9Sstevel@tonic-gate #include	<sys/mkdev.h>
487c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
497c478bd9Sstevel@tonic-gate #include	<sys/dkio.h>
507c478bd9Sstevel@tonic-gate #include	<sys/param.h>
517c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
527c478bd9Sstevel@tonic-gate #include	<sys/mnttab.h>
537c478bd9Sstevel@tonic-gate #include	<sys/vol.h>
547c478bd9Sstevel@tonic-gate #include	"volmgt_private.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	NULL_PATH		"/dev/null"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * This is an ON Consolidation Private interface.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * Is the specified path mounted?
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  * This function is really inadequate for ejection testing.  For example,
677c478bd9Sstevel@tonic-gate  * I could have /dev/fd0a mounted and eject /dev/fd0c, and it would be
687c478bd9Sstevel@tonic-gate  * ejected.  There needs to be some better way to make this check, although
697c478bd9Sstevel@tonic-gate  * short of looking up the mounted dev_t in the kernel mount table and
707c478bd9Sstevel@tonic-gate  * building in all kinds of knowledge into this function,  I'm not sure
717c478bd9Sstevel@tonic-gate  * how to do it.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate int
747c478bd9Sstevel@tonic-gate _dev_mounted(char *path)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	static int	vol_getmntdev(FILE *, struct mnttab *, dev_t,
777c478bd9Sstevel@tonic-gate 			    struct dk_cinfo *);
787c478bd9Sstevel@tonic-gate 	int		fd = -1;
797c478bd9Sstevel@tonic-gate 	struct dk_cinfo	info;
807c478bd9Sstevel@tonic-gate 	static FILE 	*fp = NULL;		/* mnttab file pointer */
817c478bd9Sstevel@tonic-gate 	struct mnttab	mnt;			/* set bug not used */
827c478bd9Sstevel@tonic-gate 	char		*cn = NULL;		/* char spcl pathname */
837c478bd9Sstevel@tonic-gate 	struct stat64	sb;
847c478bd9Sstevel@tonic-gate 	int		ret_val = 0;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #ifdef	DEBUG
897c478bd9Sstevel@tonic-gate 	denter("_dev_mounted(%s): entering\n", path);
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/* ensure we have the block spcl pathname */
937c478bd9Sstevel@tonic-gate 	if ((cn = (char *)volmgt_getfullrawname(path)) == NULL) {
947c478bd9Sstevel@tonic-gate #ifdef	DEBUG
957c478bd9Sstevel@tonic-gate 		dprintf("_dev_mounted: volmgt_getfullrawname failed\n");
967c478bd9Sstevel@tonic-gate #endif
977c478bd9Sstevel@tonic-gate 		goto dun;
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #ifdef	DEBUG_OPEN
1017c478bd9Sstevel@tonic-gate 	dprintf("_dev_mounted: fopen()ing \"%s\"\n", MNTTAB);
1027c478bd9Sstevel@tonic-gate #endif
103*004388ebScasper 	if ((fp = fopen(MNTTAB, "rF")) == NULL) {
1047c478bd9Sstevel@tonic-gate 		/* mtab is gone... let him go */
1057c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1067c478bd9Sstevel@tonic-gate 		perror(MNTTAB);
1077c478bd9Sstevel@tonic-gate #endif
1087c478bd9Sstevel@tonic-gate 		goto dun;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #ifdef	DEBUG_OPEN
1127c478bd9Sstevel@tonic-gate 	dprintf("_dev_mounted: open()ing \"%s\"\n", cn);
1137c478bd9Sstevel@tonic-gate #endif
1147c478bd9Sstevel@tonic-gate 	if ((fd = open(cn, O_RDONLY|O_NDELAY)) < 0) {
1157c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1167c478bd9Sstevel@tonic-gate 		dprintf("_dev_mounted: can't open \"%s\" (%d)\n", cn, errno);
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate 		goto dun;
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #ifdef	DEBUG_STAT
1227c478bd9Sstevel@tonic-gate 	dprintf("_dev_mounted: fstat()ing \"%s\"\n", cn);
1237c478bd9Sstevel@tonic-gate #endif
1247c478bd9Sstevel@tonic-gate 	if (fstat64(fd, &sb) < 0) {
1257c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1267c478bd9Sstevel@tonic-gate 		dprintf("_dev_mounted: stat of \"%s\" failed (%d)\n", cn,
1277c478bd9Sstevel@tonic-gate 		    errno);
1287c478bd9Sstevel@tonic-gate #endif
1297c478bd9Sstevel@tonic-gate 		goto dun;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #ifdef	DEBUG_IOCTL
1337c478bd9Sstevel@tonic-gate 	dprintf("_dev_mounted: ioctl(%s, DKIOCINFO)\n", cn);
1347c478bd9Sstevel@tonic-gate #endif
1357c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, &info) != 0) {
1367c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1377c478bd9Sstevel@tonic-gate 		dprintf(
1387c478bd9Sstevel@tonic-gate 		    "_dev_mounted: ioctl(DKIOCINFO) on \"%s\" failed (%d)\n",
1397c478bd9Sstevel@tonic-gate 		    cn, errno);
1407c478bd9Sstevel@tonic-gate #endif
1417c478bd9Sstevel@tonic-gate 		goto dun;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (vol_getmntdev(fp, &mnt, sb.st_rdev, &info) != 0) {
1457c478bd9Sstevel@tonic-gate 		ret_val = 1;			/* match found! */
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate dun:
1497c478bd9Sstevel@tonic-gate 	if (cn != NULL) {
1507c478bd9Sstevel@tonic-gate 		free(cn);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	if (fp != NULL) {
1537c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
1567c478bd9Sstevel@tonic-gate 		(void) close(fd);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1597c478bd9Sstevel@tonic-gate 	dexit("_dev_mounted: returning %s\n",
1607c478bd9Sstevel@tonic-gate 	    ret_val ? "TRUE" : "FALSE");
1617c478bd9Sstevel@tonic-gate #endif
1627c478bd9Sstevel@tonic-gate 	return (ret_val);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * This is an ON Consolidation Private interface.
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  * Forks off rmmount and (in essence) returns the result
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  * a return value of 0 (FALSE) means failure, non-zero (TRUE) means success
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate int
1747c478bd9Sstevel@tonic-gate _dev_unmount(char *path)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	static int	call_unmount_prog(int, int, char *, int, char *,
1777c478bd9Sstevel@tonic-gate 			    char *);
1787c478bd9Sstevel@tonic-gate 	static int	get_media_info(char *, char **, int *, char **);
1797c478bd9Sstevel@tonic-gate 	char		*bn = NULL;		/* block name */
1807c478bd9Sstevel@tonic-gate 	char		*mtype = NULL;		/* media type */
1817c478bd9Sstevel@tonic-gate 	char		*spcl = NULL;		/* special dev. path */
1827c478bd9Sstevel@tonic-gate 	char		*spcl_failed = NULL;	/* spcl that failed */
1837c478bd9Sstevel@tonic-gate 	int		ret_val = FALSE;	/* what we return */
1847c478bd9Sstevel@tonic-gate 	char		*vr;			/* volmgt root dir */
1857c478bd9Sstevel@tonic-gate 	int		media_info_gotten = 0;
1867c478bd9Sstevel@tonic-gate 	int		mnum = 0;
1877c478bd9Sstevel@tonic-gate 	int		volume_is_not_managed;
1887c478bd9Sstevel@tonic-gate 	char		*pathbuf, *absname;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1917c478bd9Sstevel@tonic-gate 	denter("_dev_unmount(%s): entering\n", path);
1927c478bd9Sstevel@tonic-gate #endif
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if ((bn = (char *)volmgt_getfullblkname(path)) == NULL) {
1957c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1967c478bd9Sstevel@tonic-gate 		dprintf("_dev_unmount: volmgt_getfullblkname failed\n");
1977c478bd9Sstevel@tonic-gate #endif
1987c478bd9Sstevel@tonic-gate 		goto dun;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if ((pathbuf = malloc(PATH_MAX+1)) == NULL)
2027c478bd9Sstevel@tonic-gate 		goto dun;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	absname = bn;
2057c478bd9Sstevel@tonic-gate 	if (realpath(bn, pathbuf) != NULL)
2067c478bd9Sstevel@tonic-gate 		absname = pathbuf;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	volume_is_not_managed = !volmgt_running() ||
2097c478bd9Sstevel@tonic-gate 		(!volmgt_ownspath(absname) && volmgt_symname(bn) == NULL);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	free(pathbuf);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	/* decide of we should use rmmount to unmount the media */
2147c478bd9Sstevel@tonic-gate 	if (!volume_is_not_managed) {
2157c478bd9Sstevel@tonic-gate 		int		use_rmm = FALSE;	/* use rmmount??  */
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		/* at least volmgt is running */
2187c478bd9Sstevel@tonic-gate 		vr = (char *)volmgt_root();
2197c478bd9Sstevel@tonic-gate 		if (strncmp(bn, vr, strlen(vr)) == 0) {
2207c478bd9Sstevel@tonic-gate 			/* the block path is rooted in /vol */
2217c478bd9Sstevel@tonic-gate 			use_rmm = TRUE;
2227c478bd9Sstevel@tonic-gate 		}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		/* try to get info about media */
2257c478bd9Sstevel@tonic-gate 		media_info_gotten = get_media_info(bn, &mtype, &mnum, &spcl);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		ret_val = call_unmount_prog(media_info_gotten, use_rmm, mtype,
2287c478bd9Sstevel@tonic-gate 		    mnum, spcl, bn);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	} else {
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		/* volmgt is *not* running */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		if (get_media_info(bn, &mtype, &mnum, &spcl)) {
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 			/*
2377c478bd9Sstevel@tonic-gate 			 * volmgt is off and get_media_info() has returned
2387c478bd9Sstevel@tonic-gate 			 * info on the media -- soo (this is kinda' a hack)
2397c478bd9Sstevel@tonic-gate 			 * ... we iterate, looking for multiple slices
2407c478bd9Sstevel@tonic-gate 			 * of (say) a floppy being mounted
2417c478bd9Sstevel@tonic-gate 			 *
2427c478bd9Sstevel@tonic-gate 			 * note: if an unmount fails we don't want to try
2437c478bd9Sstevel@tonic-gate 			 * to unmount the same device on the next try, so
2447c478bd9Sstevel@tonic-gate 			 * we try to watch for that
2457c478bd9Sstevel@tonic-gate 			 */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 			do {
2487c478bd9Sstevel@tonic-gate 				/*
2497c478bd9Sstevel@tonic-gate 				 * don't call the unmount program is we're just
2507c478bd9Sstevel@tonic-gate 				 * trying to unmount the same device that
2517c478bd9Sstevel@tonic-gate 				 * failed last time -- if that's the case,
2527c478bd9Sstevel@tonic-gate 				 * then bail
2537c478bd9Sstevel@tonic-gate 				 */
2547c478bd9Sstevel@tonic-gate 				if (spcl_failed != NULL) {
2557c478bd9Sstevel@tonic-gate 					if (strcmp(spcl, spcl_failed) == 0) {
2567c478bd9Sstevel@tonic-gate 						break;
2577c478bd9Sstevel@tonic-gate 					}
2587c478bd9Sstevel@tonic-gate 				}
2597c478bd9Sstevel@tonic-gate 				ret_val = call_unmount_prog(TRUE, FALSE,
2607c478bd9Sstevel@tonic-gate 				    mtype, mnum, spcl, bn);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 				if (!ret_val) {
2637c478bd9Sstevel@tonic-gate 					/* save spcl device name that failed */
2647c478bd9Sstevel@tonic-gate 					spcl_failed = strdup(spcl);
2657c478bd9Sstevel@tonic-gate 				} else {
2667c478bd9Sstevel@tonic-gate 					/*
2677c478bd9Sstevel@tonic-gate 					 * unmount succeeded, so clean up
2687c478bd9Sstevel@tonic-gate 					 */
2697c478bd9Sstevel@tonic-gate 					if (spcl_failed != NULL) {
2707c478bd9Sstevel@tonic-gate 						free(spcl_failed);
2717c478bd9Sstevel@tonic-gate 						spcl_failed = NULL;
2727c478bd9Sstevel@tonic-gate 					}
2737c478bd9Sstevel@tonic-gate 				}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 			} while (get_media_info(bn, &mtype, &mnum, &spcl));
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		} else {
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 			/* just do the unmmount cycle once */
2807c478bd9Sstevel@tonic-gate 			ret_val = call_unmount_prog(FALSE, FALSE, NULL, 0,
2817c478bd9Sstevel@tonic-gate 			    NULL, bn);
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (mtype != NULL) {
2877c478bd9Sstevel@tonic-gate 		free(mtype);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	if (spcl != NULL) {
2907c478bd9Sstevel@tonic-gate 		free(spcl);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	if (spcl_failed != NULL) {
2937c478bd9Sstevel@tonic-gate 		free(spcl_failed);
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 	if (bn != NULL) {
2967c478bd9Sstevel@tonic-gate 		free(bn);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate dun:
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3027c478bd9Sstevel@tonic-gate 	dexit("_dev_unmount: returning %s\n", ret_val ? "TRUE" : "FALSE");
3037c478bd9Sstevel@tonic-gate #endif
3047c478bd9Sstevel@tonic-gate 	return (ret_val);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * find a mnttab entry that has the same dev as the supplied dev,
3107c478bd9Sstevel@tonic-gate  *  returning it and a non-zero value if found, else returning 0
3117c478bd9Sstevel@tonic-gate  *
3127c478bd9Sstevel@tonic-gate  * this is just like getmntany(), except that it scans based on st_rdev,
3137c478bd9Sstevel@tonic-gate  * and it even finds different slices on the same device/unit (thanx to
3147c478bd9Sstevel@tonic-gate  * code copied from format.c)
3157c478bd9Sstevel@tonic-gate  */
3167c478bd9Sstevel@tonic-gate static int
3177c478bd9Sstevel@tonic-gate vol_getmntdev(FILE *fp, struct mnttab *mp, dev_t dev, struct dk_cinfo *ip)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	int		fd;		/* dev-in-question fd */
3207c478bd9Sstevel@tonic-gate 	struct stat64	sb;		/* dev-in-question stat struct */
3217c478bd9Sstevel@tonic-gate 	int		ret_val = 0;	/* default value: no match found */
3227c478bd9Sstevel@tonic-gate 	char		*cn;		/* char pathname */
3237c478bd9Sstevel@tonic-gate 	struct dk_cinfo	dkinfo;		/* for testing for slices */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3277c478bd9Sstevel@tonic-gate 	denter(
3287c478bd9Sstevel@tonic-gate 	    "vol_getmntdev: entering for %d.%d, ctype/cnum/unit = %d/%d/%d\n",
3297c478bd9Sstevel@tonic-gate 	    (int)major(dev), (int)minor(dev), ip->dki_ctype, ip->dki_cnum,
3307c478bd9Sstevel@tonic-gate 	    ip->dki_unit);
3317c478bd9Sstevel@tonic-gate #endif
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/* reset the mnttab -- just in case */
3347c478bd9Sstevel@tonic-gate 	rewind(fp);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/* scan each entry in mnttab */
3377c478bd9Sstevel@tonic-gate 	while (getmntent(fp, mp) == 0) {
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		/* don't even try unless it's a local pathname */
3407c478bd9Sstevel@tonic-gate 		if (mp->mnt_special[0] != '/') {
3417c478bd9Sstevel@tonic-gate 			continue;
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		/* get char pathname */
3457c478bd9Sstevel@tonic-gate 		if ((cn = volmgt_getfullrawname(mp->mnt_special)) == NULL) {
3467c478bd9Sstevel@tonic-gate 			continue;
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 		if (cn[0] == NULLC) {
3497c478bd9Sstevel@tonic-gate 			free(cn);
3507c478bd9Sstevel@tonic-gate 			continue;	/* couldn't get raw name */
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		/* open the device */
3547c478bd9Sstevel@tonic-gate #ifdef	DEBUG_OPEN
3557c478bd9Sstevel@tonic-gate 		dprintf("vol_getmntdev: open()ing \"%s\"\n", cn);
3567c478bd9Sstevel@tonic-gate #endif
3577c478bd9Sstevel@tonic-gate 		if ((fd = open(cn, O_RDONLY|O_NDELAY)) < 0) {
3587c478bd9Sstevel@tonic-gate 			/* if we can't open it *assume* it's not a match */
3597c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3607c478bd9Sstevel@tonic-gate 			dprintf(
3617c478bd9Sstevel@tonic-gate 			    "vol_getmntdev: open of \"%s\" (%s) failed (%d)\n",
3627c478bd9Sstevel@tonic-gate 			    cn, mp->mnt_fstype, errno);
3637c478bd9Sstevel@tonic-gate #endif
3647c478bd9Sstevel@tonic-gate 			free(cn);
3657c478bd9Sstevel@tonic-gate 			continue;
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		/* stat the device */
3697c478bd9Sstevel@tonic-gate #ifdef	DEBUG_STAT
3707c478bd9Sstevel@tonic-gate 		dprintf("vol_getmntdev: fstat()ing \"%s\"\n", cn);
3717c478bd9Sstevel@tonic-gate #endif
3727c478bd9Sstevel@tonic-gate 		if (fstat64(fd, &sb) < 0) {
3737c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3747c478bd9Sstevel@tonic-gate 			dprintf(
3757c478bd9Sstevel@tonic-gate 			    "vol_getmntdev: stat of \"%s\" (%s) failed (%d)\n",
3767c478bd9Sstevel@tonic-gate 			    cn, mp->mnt_fstype, errno);
3777c478bd9Sstevel@tonic-gate #endif
3787c478bd9Sstevel@tonic-gate 			free(cn);
3797c478bd9Sstevel@tonic-gate 			(void) close(fd);
3807c478bd9Sstevel@tonic-gate 			continue;	/* ain't there: can't be a match */
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		/* ensure we have a spcl device (a double check) */
3847c478bd9Sstevel@tonic-gate 		if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)) {
3857c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3867c478bd9Sstevel@tonic-gate 			dprintf(
3877c478bd9Sstevel@tonic-gate 		"vol_getmntdev: \"%s\" not a blk- or chr-spcl device\n",
3887c478bd9Sstevel@tonic-gate 			    cn);
3897c478bd9Sstevel@tonic-gate #endif
3907c478bd9Sstevel@tonic-gate 			free(cn);
3917c478bd9Sstevel@tonic-gate 			(void) close(fd);
3927c478bd9Sstevel@tonic-gate 			continue;
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		/* (almost) finally -- check the dev_t for equality */
3967c478bd9Sstevel@tonic-gate 		if (sb.st_rdev == dev) {
3977c478bd9Sstevel@tonic-gate 			ret_val = 1;		/* match found! */
3987c478bd9Sstevel@tonic-gate 			free(cn);
3997c478bd9Sstevel@tonic-gate 			(void) close(fd);
4007c478bd9Sstevel@tonic-gate 			break;
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		/*
4047c478bd9Sstevel@tonic-gate 		 * check that the major numbers match, since if they
4057c478bd9Sstevel@tonic-gate 		 * don't then there's no reason to use the DKIOCINFO
4067c478bd9Sstevel@tonic-gate 		 * ioctl to see if we have to major/minor pairs that
4077c478bd9Sstevel@tonic-gate 		 * really point to the same device
4087c478bd9Sstevel@tonic-gate 		 */
4097c478bd9Sstevel@tonic-gate 		if (major(sb.st_rdev) != major(dev)) {
4107c478bd9Sstevel@tonic-gate 			/* no use continuing, since major devs are different */
4117c478bd9Sstevel@tonic-gate 			free(cn);
4127c478bd9Sstevel@tonic-gate 			(void) close(fd);
4137c478bd9Sstevel@tonic-gate 			continue;
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate #ifdef	DEBUG_IOCTL
4177c478bd9Sstevel@tonic-gate 		dprintf("vol_getmntdev: ioctl(%s, DKIOCINFO)\n", cn);
4187c478bd9Sstevel@tonic-gate #endif
4197c478bd9Sstevel@tonic-gate 		/* one last check -- for diff. slices of the same dev/unit */
4207c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DKIOCINFO, &dkinfo) < 0) {
4217c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4227c478bd9Sstevel@tonic-gate 			dprintf(
4237c478bd9Sstevel@tonic-gate 		"vol_getmntdev: ioctl(DKIOCINFO) of \"%s\" failed (%d)\n",
4247c478bd9Sstevel@tonic-gate 			    cn, errno);
4257c478bd9Sstevel@tonic-gate #endif
4267c478bd9Sstevel@tonic-gate 			free(cn);
4277c478bd9Sstevel@tonic-gate 			(void) close(fd);
4287c478bd9Sstevel@tonic-gate 			continue;
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		free(cn);		/* all done with raw pathname */
4327c478bd9Sstevel@tonic-gate 		(void) close(fd);	/* all done with file descriptor */
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		/* if ctrler type/number and unit match, it's a match */
4357c478bd9Sstevel@tonic-gate 		if ((ip->dki_ctype == dkinfo.dki_ctype) &&
4367c478bd9Sstevel@tonic-gate 		    (ip->dki_cnum == dkinfo.dki_cnum) &&
4377c478bd9Sstevel@tonic-gate 		    (ip->dki_unit == dkinfo.dki_unit)) {
4387c478bd9Sstevel@tonic-gate 			/*
4397c478bd9Sstevel@tonic-gate 			 * even though minor numbers differ we have a
4407c478bd9Sstevel@tonic-gate 			 * match
4417c478bd9Sstevel@tonic-gate 			 */
4427c478bd9Sstevel@tonic-gate 			ret_val = 1;
4437c478bd9Sstevel@tonic-gate 			break;
4447c478bd9Sstevel@tonic-gate 		}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		/* go around again */
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4507c478bd9Sstevel@tonic-gate 	dexit("vol_getmntdev: returning %d (%s)\n", ret_val,
4517c478bd9Sstevel@tonic-gate 	    ret_val == 1 ? "SUCCESS" : "FAILURE");
4527c478bd9Sstevel@tonic-gate #endif
4537c478bd9Sstevel@tonic-gate 	return (ret_val);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate char *
4587c478bd9Sstevel@tonic-gate vol_basename(char *path)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	char	*cp;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/* check for the degenerate case */
4647c478bd9Sstevel@tonic-gate 	if (strcmp(path, "/") == 0) {
4657c478bd9Sstevel@tonic-gate 		return (path);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/* look for the last slash in the name */
4697c478bd9Sstevel@tonic-gate 	if ((cp = strrchr(path, '/')) == NULL) {
4707c478bd9Sstevel@tonic-gate 		/* no slash */
4717c478bd9Sstevel@tonic-gate 		return (path);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	/* ensure something is after the slash */
4757c478bd9Sstevel@tonic-gate 	if (*++cp != NULLC) {
4767c478bd9Sstevel@tonic-gate 		return (cp);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/* a name that ends in slash -- back up until previous slash */
4807c478bd9Sstevel@tonic-gate 	while (cp != path) {
4817c478bd9Sstevel@tonic-gate 		if (*--cp == '/') {
4827c478bd9Sstevel@tonic-gate 			return (--cp);
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	/* the only slash is the end of the name */
4877c478bd9Sstevel@tonic-gate 	return (path);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate static int
4927c478bd9Sstevel@tonic-gate get_media_info(char *path, char **mtypep, int *mnump, char **spclp)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	static int	vol_getmntdev(FILE *, struct mnttab *, dev_t,
4957c478bd9Sstevel@tonic-gate 			    struct dk_cinfo *);
4967c478bd9Sstevel@tonic-gate 	FILE		*fp = NULL;
4977c478bd9Sstevel@tonic-gate 	int		fd = -1;
4987c478bd9Sstevel@tonic-gate 	char		*cn = NULL;		/* char spcl pathname */
4997c478bd9Sstevel@tonic-gate 	struct stat64	sb;
5007c478bd9Sstevel@tonic-gate 	struct dk_cinfo	info;
5017c478bd9Sstevel@tonic-gate 	struct mnttab	mnt;
5027c478bd9Sstevel@tonic-gate 	int		ret_val = FALSE;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate #ifdef	DEBUG
5077c478bd9Sstevel@tonic-gate 	denter("get_media_info(%s): entering\n", path);
5087c478bd9Sstevel@tonic-gate #endif
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate #ifdef	DEBUG_OPEN
5117c478bd9Sstevel@tonic-gate 	dprintf("get_media_info: fopen()ing \"%s\"\n", MNTTAB);
5127c478bd9Sstevel@tonic-gate #endif
513*004388ebScasper 	if ((fp = fopen(MNTTAB, "rF")) == NULL) {
5147c478bd9Sstevel@tonic-gate 		/* mtab is gone... let him go */
5157c478bd9Sstevel@tonic-gate #ifdef	DEBUG
5167c478bd9Sstevel@tonic-gate 		dprintf("get_media_info: can't open \"%s\" (%d)\n", MNTTAB,
5177c478bd9Sstevel@tonic-gate 		    errno);
5187c478bd9Sstevel@tonic-gate #endif
5197c478bd9Sstevel@tonic-gate 		goto dun;
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/* get char spcl pathname */
5237c478bd9Sstevel@tonic-gate 	if ((cn = volmgt_getfullrawname(path)) == NULL) {
5247c478bd9Sstevel@tonic-gate 		goto dun;
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	if (cn[0] == NULLC) {
5277c478bd9Sstevel@tonic-gate 		goto dun;
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate #ifdef	DEBUG_OPEN
5317c478bd9Sstevel@tonic-gate 	dprintf("get_media_info: open()ing \"%s\"\n", cn);
5327c478bd9Sstevel@tonic-gate #endif
5337c478bd9Sstevel@tonic-gate 	if ((fd = open(cn, O_RDONLY|O_NDELAY)) < 0) {
5347c478bd9Sstevel@tonic-gate #ifdef	DEBUG
5357c478bd9Sstevel@tonic-gate 		dprintf("get_media_info(): can't open \"%s\" (%d)\n", cn,
5367c478bd9Sstevel@tonic-gate 		    errno);
5377c478bd9Sstevel@tonic-gate #endif
5387c478bd9Sstevel@tonic-gate 		goto dun;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate #ifdef	DEBUG_STAT
5427c478bd9Sstevel@tonic-gate 	dprintf("get_media_info: fstat()ing \"%s\"\n", cn);
5437c478bd9Sstevel@tonic-gate #endif
5447c478bd9Sstevel@tonic-gate 	if (fstat64(fd, &sb) < 0) {
5457c478bd9Sstevel@tonic-gate #ifdef	DEBUG
5467c478bd9Sstevel@tonic-gate 		dprintf("get_media_info: can't stat \"%s\" (%d)\n", cn, errno);
5477c478bd9Sstevel@tonic-gate #endif
5487c478bd9Sstevel@tonic-gate 		goto dun;
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate #ifdef	DEBUG_IOCTL
5527c478bd9Sstevel@tonic-gate 	dprintf("get_media_info: ioctl(%s, DKIOCINFO)\n", cn);
5537c478bd9Sstevel@tonic-gate #endif
5547c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, &info) != 0) {
5557c478bd9Sstevel@tonic-gate #ifdef	DEBUG
5567c478bd9Sstevel@tonic-gate 		dprintf(
5577c478bd9Sstevel@tonic-gate 		    "get_media_info: ioctl(DKIOCINFO) on \"%s\" failed (%d)\n",
5587c478bd9Sstevel@tonic-gate 		    cn, errno);
5597c478bd9Sstevel@tonic-gate #endif
5607c478bd9Sstevel@tonic-gate 		goto dun;
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/* if we found the entry then disect it */
5647c478bd9Sstevel@tonic-gate 	if (vol_getmntdev(fp, &mnt, sb.st_rdev, &info) != 0) {
5657c478bd9Sstevel@tonic-gate 		char		*cp;
5667c478bd9Sstevel@tonic-gate 		char		*mtype;
5677c478bd9Sstevel@tonic-gate 		char		*mnt_dir;
5687c478bd9Sstevel@tonic-gate 		int		mtype_len;
5697c478bd9Sstevel@tonic-gate 		DIR		*dirp = NULL;
5707c478bd9Sstevel@tonic-gate 		struct dirent64	*dp;
5717c478bd9Sstevel@tonic-gate 		char		*volname;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		/* return the spcl device name found */
5757c478bd9Sstevel@tonic-gate 		*spclp = strdup(mnt.mnt_special);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		/*
5787c478bd9Sstevel@tonic-gate 		 * try to get the media type (e.g. "floppy") from the mount
5797c478bd9Sstevel@tonic-gate 		 * point (e.g. "/floppy/NAME") if vold is running
5807c478bd9Sstevel@tonic-gate 		 */
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		if (!volmgt_running() ||
5837c478bd9Sstevel@tonic-gate 		    (!volmgt_ownspath(*spclp) &&
5847c478bd9Sstevel@tonic-gate 			volmgt_symname(*spclp) == NULL)) {
5857c478bd9Sstevel@tonic-gate 			ret_val = TRUE;		/* success (if limited) */
5867c478bd9Sstevel@tonic-gate 			goto dun;
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		/* get the first part of the mount point (e.g. "floppy") */
5907c478bd9Sstevel@tonic-gate 		cp = mnt.mnt_mountp;
5917c478bd9Sstevel@tonic-gate 		if (*cp++ != '/') {
5927c478bd9Sstevel@tonic-gate #ifdef	DEBUG
5937c478bd9Sstevel@tonic-gate 			dprintf(
5947c478bd9Sstevel@tonic-gate 	"get_media_info warning: no leading '/' in mount point \"%s\"\n",
5957c478bd9Sstevel@tonic-gate 			    mnt.mnt_mountp);
5967c478bd9Sstevel@tonic-gate #endif
5977c478bd9Sstevel@tonic-gate 			goto dun;
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 		mtype = cp;
6007c478bd9Sstevel@tonic-gate 		if ((cp = strchr(mtype, '/')) == NULL) {
6017c478bd9Sstevel@tonic-gate #ifdef	DEBUG
6027c478bd9Sstevel@tonic-gate 			dprintf(
6037c478bd9Sstevel@tonic-gate 		"get_media_info warning: no 2nd '/' in mount point \"%s\"\n",
6047c478bd9Sstevel@tonic-gate 			    mnt.mnt_mountp);
6057c478bd9Sstevel@tonic-gate #endif
6067c478bd9Sstevel@tonic-gate 			goto dun;
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 		*cp++ = NULLC;
6097c478bd9Sstevel@tonic-gate 		mnt_dir = mnt.mnt_mountp;	/* save dir path */
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		/* get the volume name (e.g. "unnamed_floppy") */
6127c478bd9Sstevel@tonic-gate 		volname = cp;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		/* scan for the symlink that points to our volname */
6157c478bd9Sstevel@tonic-gate 		if ((dirp = opendir(mnt_dir)) == NULL) {
6167c478bd9Sstevel@tonic-gate #ifdef	DEBUG
6177c478bd9Sstevel@tonic-gate 			dprintf(
6187c478bd9Sstevel@tonic-gate 		"get_media_info warning: can't open directory \"%s\"\n",
6197c478bd9Sstevel@tonic-gate 			    mnt_dir);
6207c478bd9Sstevel@tonic-gate #endif
6217c478bd9Sstevel@tonic-gate 			goto dun;
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 		mtype_len = strlen(mtype);
6247c478bd9Sstevel@tonic-gate 		while ((dp = readdir64(dirp)) != NULL) {
6257c478bd9Sstevel@tonic-gate 			char		lpath[2 * (MAXNAMELEN+1)];
6267c478bd9Sstevel@tonic-gate 			char		linkbuf[MAXPATHLEN+4];
6277c478bd9Sstevel@tonic-gate 			int		lb_len;
6287c478bd9Sstevel@tonic-gate 			struct stat64	sb;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 			if (strncmp(dp->d_name, mtype, mtype_len) != 0) {
6327c478bd9Sstevel@tonic-gate 				continue;	/* not even close */
6337c478bd9Sstevel@tonic-gate 			}
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 			(void) sprintf(lpath, "%s/%s", mnt_dir,
6367c478bd9Sstevel@tonic-gate 			    dp->d_name);
6377c478bd9Sstevel@tonic-gate #ifdef	DEBUG_STAT
6387c478bd9Sstevel@tonic-gate 			dprintf("get_media_info: lstat()ing \"%s\"\n", lpath);
6397c478bd9Sstevel@tonic-gate #endif
6407c478bd9Sstevel@tonic-gate 			if (lstat64(lpath, &sb) < 0) {
6417c478bd9Sstevel@tonic-gate 				continue;	/* what? */
6427c478bd9Sstevel@tonic-gate 			}
6437c478bd9Sstevel@tonic-gate 			if (!S_ISLNK(sb.st_mode)) {
6447c478bd9Sstevel@tonic-gate 				continue;	/* not our baby */
6457c478bd9Sstevel@tonic-gate 			}
6467c478bd9Sstevel@tonic-gate 			if ((lb_len = readlink(lpath, linkbuf,
6477c478bd9Sstevel@tonic-gate 			    sizeof (linkbuf))) < 0) {
6487c478bd9Sstevel@tonic-gate 				continue;
6497c478bd9Sstevel@tonic-gate 			}
6507c478bd9Sstevel@tonic-gate 			linkbuf[lb_len] = NULLC; /* null terminate */
6517c478bd9Sstevel@tonic-gate 			if ((cp = vol_basename(linkbuf)) == NULL) {
6527c478bd9Sstevel@tonic-gate 				continue;
6537c478bd9Sstevel@tonic-gate 			}
6547c478bd9Sstevel@tonic-gate 			/* now we have the name! */
6557c478bd9Sstevel@tonic-gate 			if (strcmp(cp, volname) == 0) {
6567c478bd9Sstevel@tonic-gate 				/* found it !! */
6577c478bd9Sstevel@tonic-gate 				if (sscanf(dp->d_name + mtype_len, "%d",
6587c478bd9Sstevel@tonic-gate 				    mnump) == 1) {
6597c478bd9Sstevel@tonic-gate 					*mtypep = strdup(mtype);
6607c478bd9Sstevel@tonic-gate 					ret_val = TRUE;
6617c478bd9Sstevel@tonic-gate 				}
6627c478bd9Sstevel@tonic-gate 				break;
6637c478bd9Sstevel@tonic-gate 			}
6647c478bd9Sstevel@tonic-gate 		}
6657c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate dun:
6697c478bd9Sstevel@tonic-gate 	if (fp != NULL) {
6707c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
6737c478bd9Sstevel@tonic-gate 		(void) close(fd);
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 	if (cn != NULL) {
6767c478bd9Sstevel@tonic-gate 		free(cn);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate #ifdef	DEBUG
6797c478bd9Sstevel@tonic-gate 	if (ret_val) {
6807c478bd9Sstevel@tonic-gate 		dexit("get_media_info: returning mtype=%s, mnum=%d, spcl=%s\n",
6817c478bd9Sstevel@tonic-gate 		    *mtypep == NULL ? "<null ptr>" : *mtypep,
6827c478bd9Sstevel@tonic-gate 		    *mnump,
6837c478bd9Sstevel@tonic-gate 		    *spclp == NULL ? "<null ptr>" : *spclp);
6847c478bd9Sstevel@tonic-gate 	} else {
6857c478bd9Sstevel@tonic-gate 		dexit("get_media_info: FAILED\n");
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate #endif
6887c478bd9Sstevel@tonic-gate 	return (ret_val);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * call the appropriate unmount program, returning its success (TRUE)
6947c478bd9Sstevel@tonic-gate  * or failure (FALSE)
6957c478bd9Sstevel@tonic-gate  */
6967c478bd9Sstevel@tonic-gate static int
6977c478bd9Sstevel@tonic-gate call_unmount_prog(int mi_gotten, int use_rmm, char *mtype, int mnum,
6987c478bd9Sstevel@tonic-gate     char *spcl, char *bn)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	pid_t		pid;			/* forked proc's pid */
7017c478bd9Sstevel@tonic-gate 	int		ret_val = FALSE;
7027c478bd9Sstevel@tonic-gate 	const char	*etc_umount = "/etc/umount";
7037c478bd9Sstevel@tonic-gate 	const char	*rmm = "/usr/sbin/rmmount";
7047c478bd9Sstevel@tonic-gate 	int		rval;			/* proc's return value */
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7087c478bd9Sstevel@tonic-gate 	denter(
7097c478bd9Sstevel@tonic-gate 	"call_unmount_prog(%s, %s, \"%s\", %d, \"%s\", \"%s\"): entering\n",
7107c478bd9Sstevel@tonic-gate 	    mi_gotten ? "TRUE" : "FALSE", use_rmm ? "TRUE" : "FALSE",
7117c478bd9Sstevel@tonic-gate 	    mtype ? mtype : "<null ptr>", mnum, spcl ? spcl : "<null ptr>",
7127c478bd9Sstevel@tonic-gate 	    bn);
7137c478bd9Sstevel@tonic-gate #endif
7147c478bd9Sstevel@tonic-gate 	/* create a child to unmount the path */
7157c478bd9Sstevel@tonic-gate 	if ((pid = fork()) < 0) {
7167c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7177c478bd9Sstevel@tonic-gate 		dprintf("error in call_unmount_prog: fork failed (errno %d)\n",
7187c478bd9Sstevel@tonic-gate 		    errno);
7197c478bd9Sstevel@tonic-gate #endif
7207c478bd9Sstevel@tonic-gate 		goto dun;
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (pid == 0) {
7247c478bd9Sstevel@tonic-gate 		/* the child */
7257c478bd9Sstevel@tonic-gate #ifndef	DEBUG
7267c478bd9Sstevel@tonic-gate 		int		xfd;
7277c478bd9Sstevel@tonic-gate #endif
7287c478bd9Sstevel@tonic-gate 		char		env_buf[MAXPATHLEN];
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate #ifndef	DEBUG
7317c478bd9Sstevel@tonic-gate 		/* get rid of those nasty err messages */
7327c478bd9Sstevel@tonic-gate 		if ((xfd = open(NULL_PATH, O_RDWR)) >= 0) {
7337c478bd9Sstevel@tonic-gate 			(void) dup2(xfd, fileno(stdin));
7347c478bd9Sstevel@tonic-gate 			(void) dup2(xfd, fileno(stdout));
7357c478bd9Sstevel@tonic-gate 			(void) dup2(xfd, fileno(stderr));
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate #endif
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		if (use_rmm) {
7407c478bd9Sstevel@tonic-gate 			/* set up environment vars */
7417c478bd9Sstevel@tonic-gate 			(void) putenv("VOLUME_ACTION=eject");
7427c478bd9Sstevel@tonic-gate 			(void) putenv(strdup(env_buf));
7437c478bd9Sstevel@tonic-gate 			if (mi_gotten) {
7447c478bd9Sstevel@tonic-gate 				(void) sprintf(env_buf,
7457c478bd9Sstevel@tonic-gate 				    "VOLUME_MEDIATYPE=%s", mtype);
7467c478bd9Sstevel@tonic-gate 				(void) putenv(strdup(env_buf));
7477c478bd9Sstevel@tonic-gate 				(void) sprintf(env_buf, "VOLUME_SYMDEV=%s%d",
7487c478bd9Sstevel@tonic-gate 				    mtype, mnum);
7497c478bd9Sstevel@tonic-gate 				(void) putenv(strdup(env_buf));
7507c478bd9Sstevel@tonic-gate 				(void) sprintf(env_buf, "VOLUME_PATH=%s",
7517c478bd9Sstevel@tonic-gate 				    spcl);
7527c478bd9Sstevel@tonic-gate 				(void) putenv(strdup(env_buf));
7537c478bd9Sstevel@tonic-gate 				(void) sprintf(env_buf, "VOLUME_NAME=%s",
7547c478bd9Sstevel@tonic-gate 				    vol_basename(spcl));
7557c478bd9Sstevel@tonic-gate 				(void) putenv(strdup(env_buf));
7567c478bd9Sstevel@tonic-gate 			} else {
7577c478bd9Sstevel@tonic-gate 				(void) sprintf(env_buf, "VOLUME_PATH=%s", bn);
7587c478bd9Sstevel@tonic-gate 				(void) putenv(strdup(env_buf));
7597c478bd9Sstevel@tonic-gate 				(void) sprintf(env_buf, "VOLUME_NAME=%s",
7607c478bd9Sstevel@tonic-gate 				    vol_basename(bn));
7617c478bd9Sstevel@tonic-gate 				(void) putenv(strdup(env_buf));
7627c478bd9Sstevel@tonic-gate 			}
7637c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7647c478bd9Sstevel@tonic-gate 			dprintf("call_unmount_prog: calling \"%s -D\"\n", rmm);
7657c478bd9Sstevel@tonic-gate 			(void) execl(rmm, rmm, "-D", NULL);
7667c478bd9Sstevel@tonic-gate #else
7677c478bd9Sstevel@tonic-gate 			(void) execl(rmm, rmm, NULL);
7687c478bd9Sstevel@tonic-gate #endif
7697c478bd9Sstevel@tonic-gate 		} else {
7707c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7717c478bd9Sstevel@tonic-gate 			dprintf("call_unmount_prog: calling \"%s %s\"\n",
7727c478bd9Sstevel@tonic-gate 			    etc_umount, mi_gotten ? spcl : bn);
7737c478bd9Sstevel@tonic-gate #endif
7747c478bd9Sstevel@tonic-gate 			(void) execl(etc_umount, etc_umount,
7757c478bd9Sstevel@tonic-gate 			    mi_gotten ? spcl : bn,
7767c478bd9Sstevel@tonic-gate 			    NULL);
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7797c478bd9Sstevel@tonic-gate 		dprintf("call_unmount_prog: exec failed (errno %d)\n", errno);
7807c478bd9Sstevel@tonic-gate #endif
7817c478bd9Sstevel@tonic-gate 		exit(-1);
7827c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	/* wait for the umount command to exit */
7867c478bd9Sstevel@tonic-gate 	if (waitpid(pid, &rval, 0) == pid) {
7877c478bd9Sstevel@tonic-gate 		if (WIFEXITED(rval)) {
7887c478bd9Sstevel@tonic-gate 			if (WEXITSTATUS(rval) == 0) {
7897c478bd9Sstevel@tonic-gate 				ret_val = TRUE;	/* success */
7907c478bd9Sstevel@tonic-gate 			}
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate dun:
7957c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7967c478bd9Sstevel@tonic-gate 	dexit("call_unmount_prog: returning %s\n", ret_val ? "TRUE" : "FALSE");
7977c478bd9Sstevel@tonic-gate #endif
7987c478bd9Sstevel@tonic-gate 	return (ret_val);
7997c478bd9Sstevel@tonic-gate }
800