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
5004388ebScasper * Common Development and Distribution License (the "License").
6004388ebScasper * 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*d67944fbSScott Rotondo * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23004388ebScasper * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * routines in this module are meant to be called by other libvolmgt
287c478bd9Sstevel@tonic-gate * routines only
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <dirent.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #ifdef DEBUG
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate #include <libintl.h>
407c478bd9Sstevel@tonic-gate #include <limits.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <volmgt.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <sys/wait.h>
507c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
517c478bd9Sstevel@tonic-gate #include "volmgt_private.h"
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate #define NULL_PATH "/dev/null"
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate
57*d67944fbSScott Rotondo static int vol_getmntdev(FILE *, struct mnttab *, dev_t,
58*d67944fbSScott Rotondo struct dk_cinfo *);
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate * This is an ON Consolidation Private interface.
627c478bd9Sstevel@tonic-gate *
637c478bd9Sstevel@tonic-gate * Is the specified path mounted?
647c478bd9Sstevel@tonic-gate *
657c478bd9Sstevel@tonic-gate * This function is really inadequate for ejection testing. For example,
667c478bd9Sstevel@tonic-gate * I could have /dev/fd0a mounted and eject /dev/fd0c, and it would be
677c478bd9Sstevel@tonic-gate * ejected. There needs to be some better way to make this check, although
687c478bd9Sstevel@tonic-gate * short of looking up the mounted dev_t in the kernel mount table and
697c478bd9Sstevel@tonic-gate * building in all kinds of knowledge into this function, I'm not sure
707c478bd9Sstevel@tonic-gate * how to do it.
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate int
_dev_mounted(char * path)737c478bd9Sstevel@tonic-gate _dev_mounted(char *path)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate int fd = -1;
767c478bd9Sstevel@tonic-gate struct dk_cinfo info;
777c478bd9Sstevel@tonic-gate static FILE *fp = NULL; /* mnttab file pointer */
787c478bd9Sstevel@tonic-gate struct mnttab mnt; /* set bug not used */
797c478bd9Sstevel@tonic-gate char *cn = NULL; /* char spcl pathname */
807c478bd9Sstevel@tonic-gate struct stat64 sb;
817c478bd9Sstevel@tonic-gate int ret_val = 0;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /* ensure we have the block spcl pathname */
857c478bd9Sstevel@tonic-gate if ((cn = (char *)volmgt_getfullrawname(path)) == NULL) {
867c478bd9Sstevel@tonic-gate goto dun;
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate
89004388ebScasper if ((fp = fopen(MNTTAB, "rF")) == NULL) {
907c478bd9Sstevel@tonic-gate /* mtab is gone... let him go */
917c478bd9Sstevel@tonic-gate goto dun;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate if ((fd = open(cn, O_RDONLY|O_NDELAY)) < 0) {
957c478bd9Sstevel@tonic-gate goto dun;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate if (fstat64(fd, &sb) < 0) {
997c478bd9Sstevel@tonic-gate goto dun;
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCINFO, &info) != 0) {
1037c478bd9Sstevel@tonic-gate goto dun;
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate if (vol_getmntdev(fp, &mnt, sb.st_rdev, &info) != 0) {
1077c478bd9Sstevel@tonic-gate ret_val = 1; /* match found! */
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate dun:
1117c478bd9Sstevel@tonic-gate if (cn != NULL) {
1127c478bd9Sstevel@tonic-gate free(cn);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate if (fp != NULL) {
1157c478bd9Sstevel@tonic-gate (void) fclose(fp);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate if (fd >= 0) {
1187c478bd9Sstevel@tonic-gate (void) close(fd);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate return (ret_val);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate
124*d67944fbSScott Rotondo static int call_unmount_prog(int, int, char *, int, char *,
125*d67944fbSScott Rotondo char *);
126*d67944fbSScott Rotondo static int get_media_info(char *, char **, int *, char **);
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate * This is an ON Consolidation Private interface.
1297c478bd9Sstevel@tonic-gate *
1307c478bd9Sstevel@tonic-gate * Forks off rmmount and (in essence) returns the result
1317c478bd9Sstevel@tonic-gate *
1327c478bd9Sstevel@tonic-gate * a return value of 0 (FALSE) means failure, non-zero (TRUE) means success
1337c478bd9Sstevel@tonic-gate */
1347c478bd9Sstevel@tonic-gate int
_dev_unmount(char * path)1357c478bd9Sstevel@tonic-gate _dev_unmount(char *path)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate char *bn = NULL; /* block name */
1387c478bd9Sstevel@tonic-gate char *mtype = NULL; /* media type */
1397c478bd9Sstevel@tonic-gate char *spcl = NULL; /* special dev. path */
1407c478bd9Sstevel@tonic-gate char *spcl_failed = NULL; /* spcl that failed */
1417c478bd9Sstevel@tonic-gate int ret_val = FALSE; /* what we return */
1427c478bd9Sstevel@tonic-gate char *vr; /* volmgt root dir */
1437c478bd9Sstevel@tonic-gate int media_info_gotten = 0;
1447c478bd9Sstevel@tonic-gate int mnum = 0;
1457c478bd9Sstevel@tonic-gate int volume_is_not_managed;
1467c478bd9Sstevel@tonic-gate char *pathbuf, *absname;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate if ((bn = (char *)volmgt_getfullblkname(path)) == NULL) {
1507c478bd9Sstevel@tonic-gate goto dun;
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate if ((pathbuf = malloc(PATH_MAX+1)) == NULL)
1547c478bd9Sstevel@tonic-gate goto dun;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate absname = bn;
1577c478bd9Sstevel@tonic-gate if (realpath(bn, pathbuf) != NULL)
1587c478bd9Sstevel@tonic-gate absname = pathbuf;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate volume_is_not_managed = !volmgt_running() ||
1617c478bd9Sstevel@tonic-gate (!volmgt_ownspath(absname) && volmgt_symname(bn) == NULL);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate free(pathbuf);
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /* decide of we should use rmmount to unmount the media */
1667c478bd9Sstevel@tonic-gate if (!volume_is_not_managed) {
1677c478bd9Sstevel@tonic-gate int use_rmm = FALSE; /* use rmmount?? */
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /* at least volmgt is running */
1707c478bd9Sstevel@tonic-gate vr = (char *)volmgt_root();
1717c478bd9Sstevel@tonic-gate if (strncmp(bn, vr, strlen(vr)) == 0) {
1727c478bd9Sstevel@tonic-gate /* the block path is rooted in /vol */
1737c478bd9Sstevel@tonic-gate use_rmm = TRUE;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /* try to get info about media */
1777c478bd9Sstevel@tonic-gate media_info_gotten = get_media_info(bn, &mtype, &mnum, &spcl);
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate ret_val = call_unmount_prog(media_info_gotten, use_rmm, mtype,
1807c478bd9Sstevel@tonic-gate mnum, spcl, bn);
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate } else {
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate /* volmgt is *not* running */
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate if (get_media_info(bn, &mtype, &mnum, &spcl)) {
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate * volmgt is off and get_media_info() has returned
1907c478bd9Sstevel@tonic-gate * info on the media -- soo (this is kinda' a hack)
1917c478bd9Sstevel@tonic-gate * ... we iterate, looking for multiple slices
1927c478bd9Sstevel@tonic-gate * of (say) a floppy being mounted
1937c478bd9Sstevel@tonic-gate *
1947c478bd9Sstevel@tonic-gate * note: if an unmount fails we don't want to try
1957c478bd9Sstevel@tonic-gate * to unmount the same device on the next try, so
1967c478bd9Sstevel@tonic-gate * we try to watch for that
1977c478bd9Sstevel@tonic-gate */
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate do {
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate * don't call the unmount program is we're just
2027c478bd9Sstevel@tonic-gate * trying to unmount the same device that
2037c478bd9Sstevel@tonic-gate * failed last time -- if that's the case,
2047c478bd9Sstevel@tonic-gate * then bail
2057c478bd9Sstevel@tonic-gate */
2067c478bd9Sstevel@tonic-gate if (spcl_failed != NULL) {
2077c478bd9Sstevel@tonic-gate if (strcmp(spcl, spcl_failed) == 0) {
2087c478bd9Sstevel@tonic-gate break;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate ret_val = call_unmount_prog(TRUE, FALSE,
2127c478bd9Sstevel@tonic-gate mtype, mnum, spcl, bn);
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate if (!ret_val) {
2157c478bd9Sstevel@tonic-gate /* save spcl device name that failed */
2167c478bd9Sstevel@tonic-gate spcl_failed = strdup(spcl);
2177c478bd9Sstevel@tonic-gate } else {
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate * unmount succeeded, so clean up
2207c478bd9Sstevel@tonic-gate */
2217c478bd9Sstevel@tonic-gate if (spcl_failed != NULL) {
2227c478bd9Sstevel@tonic-gate free(spcl_failed);
2237c478bd9Sstevel@tonic-gate spcl_failed = NULL;
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate } while (get_media_info(bn, &mtype, &mnum, &spcl));
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate } else {
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate /* just do the unmmount cycle once */
2327c478bd9Sstevel@tonic-gate ret_val = call_unmount_prog(FALSE, FALSE, NULL, 0,
2337c478bd9Sstevel@tonic-gate NULL, bn);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate if (mtype != NULL) {
2397c478bd9Sstevel@tonic-gate free(mtype);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate if (spcl != NULL) {
2427c478bd9Sstevel@tonic-gate free(spcl);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate if (spcl_failed != NULL) {
2457c478bd9Sstevel@tonic-gate free(spcl_failed);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate if (bn != NULL) {
2487c478bd9Sstevel@tonic-gate free(bn);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate dun:
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate return (ret_val);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate * find a mnttab entry that has the same dev as the supplied dev,
2597c478bd9Sstevel@tonic-gate * returning it and a non-zero value if found, else returning 0
2607c478bd9Sstevel@tonic-gate *
2617c478bd9Sstevel@tonic-gate * this is just like getmntany(), except that it scans based on st_rdev,
2627c478bd9Sstevel@tonic-gate * and it even finds different slices on the same device/unit (thanx to
2637c478bd9Sstevel@tonic-gate * code copied from format.c)
2647c478bd9Sstevel@tonic-gate */
2657c478bd9Sstevel@tonic-gate static int
vol_getmntdev(FILE * fp,struct mnttab * mp,dev_t dev,struct dk_cinfo * ip)2667c478bd9Sstevel@tonic-gate vol_getmntdev(FILE *fp, struct mnttab *mp, dev_t dev, struct dk_cinfo *ip)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate int fd; /* dev-in-question fd */
2697c478bd9Sstevel@tonic-gate struct stat64 sb; /* dev-in-question stat struct */
2707c478bd9Sstevel@tonic-gate int ret_val = 0; /* default value: no match found */
2717c478bd9Sstevel@tonic-gate char *cn; /* char pathname */
2727c478bd9Sstevel@tonic-gate struct dk_cinfo dkinfo; /* for testing for slices */
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate #ifdef DEBUG
2767c478bd9Sstevel@tonic-gate denter(
2777c478bd9Sstevel@tonic-gate "vol_getmntdev: entering for %d.%d, ctype/cnum/unit = %d/%d/%d\n",
2787c478bd9Sstevel@tonic-gate (int)major(dev), (int)minor(dev), ip->dki_ctype, ip->dki_cnum,
2797c478bd9Sstevel@tonic-gate ip->dki_unit);
2807c478bd9Sstevel@tonic-gate #endif
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate /* reset the mnttab -- just in case */
2837c478bd9Sstevel@tonic-gate rewind(fp);
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate /* scan each entry in mnttab */
2867c478bd9Sstevel@tonic-gate while (getmntent(fp, mp) == 0) {
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate /* don't even try unless it's a local pathname */
2897c478bd9Sstevel@tonic-gate if (mp->mnt_special[0] != '/') {
2907c478bd9Sstevel@tonic-gate continue;
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate /* get char pathname */
2947c478bd9Sstevel@tonic-gate if ((cn = volmgt_getfullrawname(mp->mnt_special)) == NULL) {
2957c478bd9Sstevel@tonic-gate continue;
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate if (cn[0] == NULLC) {
2987c478bd9Sstevel@tonic-gate free(cn);
2997c478bd9Sstevel@tonic-gate continue; /* couldn't get raw name */
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate /* open the device */
3037c478bd9Sstevel@tonic-gate if ((fd = open(cn, O_RDONLY|O_NDELAY)) < 0) {
3047c478bd9Sstevel@tonic-gate /* if we can't open it *assume* it's not a match */
3057c478bd9Sstevel@tonic-gate free(cn);
3067c478bd9Sstevel@tonic-gate continue;
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate /* stat the device */
3107c478bd9Sstevel@tonic-gate if (fstat64(fd, &sb) < 0) {
3117c478bd9Sstevel@tonic-gate free(cn);
3127c478bd9Sstevel@tonic-gate (void) close(fd);
3137c478bd9Sstevel@tonic-gate continue; /* ain't there: can't be a match */
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate /* ensure we have a spcl device (a double check) */
3177c478bd9Sstevel@tonic-gate if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)) {
3187c478bd9Sstevel@tonic-gate free(cn);
3197c478bd9Sstevel@tonic-gate (void) close(fd);
3207c478bd9Sstevel@tonic-gate continue;
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate /* (almost) finally -- check the dev_t for equality */
3247c478bd9Sstevel@tonic-gate if (sb.st_rdev == dev) {
3257c478bd9Sstevel@tonic-gate ret_val = 1; /* match found! */
3267c478bd9Sstevel@tonic-gate free(cn);
3277c478bd9Sstevel@tonic-gate (void) close(fd);
3287c478bd9Sstevel@tonic-gate break;
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate * check that the major numbers match, since if they
3337c478bd9Sstevel@tonic-gate * don't then there's no reason to use the DKIOCINFO
3347c478bd9Sstevel@tonic-gate * ioctl to see if we have to major/minor pairs that
3357c478bd9Sstevel@tonic-gate * really point to the same device
3367c478bd9Sstevel@tonic-gate */
3377c478bd9Sstevel@tonic-gate if (major(sb.st_rdev) != major(dev)) {
3387c478bd9Sstevel@tonic-gate /* no use continuing, since major devs are different */
3397c478bd9Sstevel@tonic-gate free(cn);
3407c478bd9Sstevel@tonic-gate (void) close(fd);
3417c478bd9Sstevel@tonic-gate continue;
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate /* one last check -- for diff. slices of the same dev/unit */
3457c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCINFO, &dkinfo) < 0) {
3467c478bd9Sstevel@tonic-gate free(cn);
3477c478bd9Sstevel@tonic-gate (void) close(fd);
3487c478bd9Sstevel@tonic-gate continue;
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate free(cn); /* all done with raw pathname */
3527c478bd9Sstevel@tonic-gate (void) close(fd); /* all done with file descriptor */
3537c478bd9Sstevel@tonic-gate
3547c478bd9Sstevel@tonic-gate /* if ctrler type/number and unit match, it's a match */
3557c478bd9Sstevel@tonic-gate if ((ip->dki_ctype == dkinfo.dki_ctype) &&
3567c478bd9Sstevel@tonic-gate (ip->dki_cnum == dkinfo.dki_cnum) &&
3577c478bd9Sstevel@tonic-gate (ip->dki_unit == dkinfo.dki_unit)) {
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate * even though minor numbers differ we have a
3607c478bd9Sstevel@tonic-gate * match
3617c478bd9Sstevel@tonic-gate */
3627c478bd9Sstevel@tonic-gate ret_val = 1;
3637c478bd9Sstevel@tonic-gate break;
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate /* go around again */
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate return (ret_val);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate char *
vol_basename(char * path)3747c478bd9Sstevel@tonic-gate vol_basename(char *path)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate char *cp;
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate /* check for the degenerate case */
3807c478bd9Sstevel@tonic-gate if (strcmp(path, "/") == 0) {
3817c478bd9Sstevel@tonic-gate return (path);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate /* look for the last slash in the name */
3857c478bd9Sstevel@tonic-gate if ((cp = strrchr(path, '/')) == NULL) {
3867c478bd9Sstevel@tonic-gate /* no slash */
3877c478bd9Sstevel@tonic-gate return (path);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate /* ensure something is after the slash */
3917c478bd9Sstevel@tonic-gate if (*++cp != NULLC) {
3927c478bd9Sstevel@tonic-gate return (cp);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate /* a name that ends in slash -- back up until previous slash */
3967c478bd9Sstevel@tonic-gate while (cp != path) {
3977c478bd9Sstevel@tonic-gate if (*--cp == '/') {
3987c478bd9Sstevel@tonic-gate return (--cp);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate /* the only slash is the end of the name */
4037c478bd9Sstevel@tonic-gate return (path);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate
406*d67944fbSScott Rotondo static int vol_getmntdev(FILE *, struct mnttab *, dev_t,
407*d67944fbSScott Rotondo struct dk_cinfo *);
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate static int
get_media_info(char * path,char ** mtypep,int * mnump,char ** spclp)4107c478bd9Sstevel@tonic-gate get_media_info(char *path, char **mtypep, int *mnump, char **spclp)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate FILE *fp = NULL;
4137c478bd9Sstevel@tonic-gate int fd = -1;
4147c478bd9Sstevel@tonic-gate char *cn = NULL; /* char spcl pathname */
4157c478bd9Sstevel@tonic-gate struct stat64 sb;
4167c478bd9Sstevel@tonic-gate struct dk_cinfo info;
4177c478bd9Sstevel@tonic-gate struct mnttab mnt;
4187c478bd9Sstevel@tonic-gate int ret_val = FALSE;
4197c478bd9Sstevel@tonic-gate
420004388ebScasper if ((fp = fopen(MNTTAB, "rF")) == NULL) {
4217c478bd9Sstevel@tonic-gate /* mtab is gone... let him go */
4227c478bd9Sstevel@tonic-gate goto dun;
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate /* get char spcl pathname */
4267c478bd9Sstevel@tonic-gate if ((cn = volmgt_getfullrawname(path)) == NULL) {
4277c478bd9Sstevel@tonic-gate goto dun;
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate if (cn[0] == NULLC) {
4307c478bd9Sstevel@tonic-gate goto dun;
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate if ((fd = open(cn, O_RDONLY|O_NDELAY)) < 0) {
4347c478bd9Sstevel@tonic-gate goto dun;
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate if (fstat64(fd, &sb) < 0) {
4387c478bd9Sstevel@tonic-gate goto dun;
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCINFO, &info) != 0) {
4427c478bd9Sstevel@tonic-gate goto dun;
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate /* if we found the entry then disect it */
4467c478bd9Sstevel@tonic-gate if (vol_getmntdev(fp, &mnt, sb.st_rdev, &info) != 0) {
4477c478bd9Sstevel@tonic-gate char *cp;
4487c478bd9Sstevel@tonic-gate char *mtype;
4497c478bd9Sstevel@tonic-gate char *mnt_dir;
4507c478bd9Sstevel@tonic-gate int mtype_len;
4517c478bd9Sstevel@tonic-gate DIR *dirp = NULL;
4527c478bd9Sstevel@tonic-gate struct dirent64 *dp;
4537c478bd9Sstevel@tonic-gate char *volname;
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate /* return the spcl device name found */
4577c478bd9Sstevel@tonic-gate *spclp = strdup(mnt.mnt_special);
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate * try to get the media type (e.g. "floppy") from the mount
4617c478bd9Sstevel@tonic-gate * point (e.g. "/floppy/NAME") if vold is running
4627c478bd9Sstevel@tonic-gate */
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate if (!volmgt_running() ||
4657c478bd9Sstevel@tonic-gate (!volmgt_ownspath(*spclp) &&
4667c478bd9Sstevel@tonic-gate volmgt_symname(*spclp) == NULL)) {
4677c478bd9Sstevel@tonic-gate ret_val = TRUE; /* success (if limited) */
4687c478bd9Sstevel@tonic-gate goto dun;
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate /* get the first part of the mount point (e.g. "floppy") */
4727c478bd9Sstevel@tonic-gate cp = mnt.mnt_mountp;
4737c478bd9Sstevel@tonic-gate if (*cp++ != '/') {
4747c478bd9Sstevel@tonic-gate goto dun;
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate mtype = cp;
4777c478bd9Sstevel@tonic-gate if ((cp = strchr(mtype, '/')) == NULL) {
4787c478bd9Sstevel@tonic-gate goto dun;
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate *cp++ = NULLC;
4817c478bd9Sstevel@tonic-gate mnt_dir = mnt.mnt_mountp; /* save dir path */
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate /* get the volume name (e.g. "unnamed_floppy") */
4847c478bd9Sstevel@tonic-gate volname = cp;
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate /* scan for the symlink that points to our volname */
4877c478bd9Sstevel@tonic-gate if ((dirp = opendir(mnt_dir)) == NULL) {
4887c478bd9Sstevel@tonic-gate goto dun;
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate mtype_len = strlen(mtype);
4917c478bd9Sstevel@tonic-gate while ((dp = readdir64(dirp)) != NULL) {
4927c478bd9Sstevel@tonic-gate char lpath[2 * (MAXNAMELEN+1)];
4937c478bd9Sstevel@tonic-gate char linkbuf[MAXPATHLEN+4];
4947c478bd9Sstevel@tonic-gate int lb_len;
4957c478bd9Sstevel@tonic-gate struct stat64 sb;
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate if (strncmp(dp->d_name, mtype, mtype_len) != 0) {
4997c478bd9Sstevel@tonic-gate continue; /* not even close */
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate (void) sprintf(lpath, "%s/%s", mnt_dir,
5037c478bd9Sstevel@tonic-gate dp->d_name);
5047c478bd9Sstevel@tonic-gate if (lstat64(lpath, &sb) < 0) {
5057c478bd9Sstevel@tonic-gate continue; /* what? */
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate if (!S_ISLNK(sb.st_mode)) {
5087c478bd9Sstevel@tonic-gate continue; /* not our baby */
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate if ((lb_len = readlink(lpath, linkbuf,
5117c478bd9Sstevel@tonic-gate sizeof (linkbuf))) < 0) {
5127c478bd9Sstevel@tonic-gate continue;
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate linkbuf[lb_len] = NULLC; /* null terminate */
5157c478bd9Sstevel@tonic-gate if ((cp = vol_basename(linkbuf)) == NULL) {
5167c478bd9Sstevel@tonic-gate continue;
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate /* now we have the name! */
5197c478bd9Sstevel@tonic-gate if (strcmp(cp, volname) == 0) {
5207c478bd9Sstevel@tonic-gate /* found it !! */
5217c478bd9Sstevel@tonic-gate if (sscanf(dp->d_name + mtype_len, "%d",
5227c478bd9Sstevel@tonic-gate mnump) == 1) {
5237c478bd9Sstevel@tonic-gate *mtypep = strdup(mtype);
5247c478bd9Sstevel@tonic-gate ret_val = TRUE;
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate break;
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate (void) closedir(dirp);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate dun:
5337c478bd9Sstevel@tonic-gate if (fp != NULL) {
5347c478bd9Sstevel@tonic-gate (void) fclose(fp);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate if (fd >= 0) {
5377c478bd9Sstevel@tonic-gate (void) close(fd);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate if (cn != NULL) {
5407c478bd9Sstevel@tonic-gate free(cn);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate #ifdef DEBUG
5437c478bd9Sstevel@tonic-gate if (ret_val) {
5447c478bd9Sstevel@tonic-gate dexit("get_media_info: returning mtype=%s, mnum=%d, spcl=%s\n",
5457c478bd9Sstevel@tonic-gate *mtypep == NULL ? "<null ptr>" : *mtypep,
5467c478bd9Sstevel@tonic-gate *mnump,
5477c478bd9Sstevel@tonic-gate *spclp == NULL ? "<null ptr>" : *spclp);
5487c478bd9Sstevel@tonic-gate } else {
5497c478bd9Sstevel@tonic-gate dexit("get_media_info: FAILED\n");
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate #endif
5527c478bd9Sstevel@tonic-gate return (ret_val);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate * call the appropriate unmount program, returning its success (TRUE)
5587c478bd9Sstevel@tonic-gate * or failure (FALSE)
5597c478bd9Sstevel@tonic-gate */
5607c478bd9Sstevel@tonic-gate static int
call_unmount_prog(int mi_gotten,int use_rmm,char * mtype,int mnum,char * spcl,char * bn)5617c478bd9Sstevel@tonic-gate call_unmount_prog(int mi_gotten, int use_rmm, char *mtype, int mnum,
5627c478bd9Sstevel@tonic-gate char *spcl, char *bn)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate pid_t pid; /* forked proc's pid */
5657c478bd9Sstevel@tonic-gate int ret_val = FALSE;
5667c478bd9Sstevel@tonic-gate const char *etc_umount = "/etc/umount";
5677c478bd9Sstevel@tonic-gate const char *rmm = "/usr/sbin/rmmount";
5687c478bd9Sstevel@tonic-gate int rval; /* proc's return value */
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate #ifdef DEBUG
5727c478bd9Sstevel@tonic-gate denter(
5737c478bd9Sstevel@tonic-gate "call_unmount_prog(%s, %s, \"%s\", %d, \"%s\", \"%s\"): entering\n",
5747c478bd9Sstevel@tonic-gate mi_gotten ? "TRUE" : "FALSE", use_rmm ? "TRUE" : "FALSE",
5757c478bd9Sstevel@tonic-gate mtype ? mtype : "<null ptr>", mnum, spcl ? spcl : "<null ptr>",
5767c478bd9Sstevel@tonic-gate bn);
5777c478bd9Sstevel@tonic-gate #endif
5787c478bd9Sstevel@tonic-gate /* create a child to unmount the path */
5797c478bd9Sstevel@tonic-gate if ((pid = fork()) < 0) {
5807c478bd9Sstevel@tonic-gate goto dun;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate if (pid == 0) {
5847c478bd9Sstevel@tonic-gate /* the child */
5857c478bd9Sstevel@tonic-gate #ifndef DEBUG
5867c478bd9Sstevel@tonic-gate int xfd;
5877c478bd9Sstevel@tonic-gate #endif
5887c478bd9Sstevel@tonic-gate char env_buf[MAXPATHLEN];
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate #ifndef DEBUG
5917c478bd9Sstevel@tonic-gate /* get rid of those nasty err messages */
5927c478bd9Sstevel@tonic-gate if ((xfd = open(NULL_PATH, O_RDWR)) >= 0) {
5937c478bd9Sstevel@tonic-gate (void) dup2(xfd, fileno(stdin));
5947c478bd9Sstevel@tonic-gate (void) dup2(xfd, fileno(stdout));
5957c478bd9Sstevel@tonic-gate (void) dup2(xfd, fileno(stderr));
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate #endif
5987c478bd9Sstevel@tonic-gate
5997c478bd9Sstevel@tonic-gate if (use_rmm) {
6007c478bd9Sstevel@tonic-gate /* set up environment vars */
6017c478bd9Sstevel@tonic-gate (void) putenv("VOLUME_ACTION=eject");
6027c478bd9Sstevel@tonic-gate (void) putenv(strdup(env_buf));
6037c478bd9Sstevel@tonic-gate if (mi_gotten) {
6047c478bd9Sstevel@tonic-gate (void) sprintf(env_buf,
6057c478bd9Sstevel@tonic-gate "VOLUME_MEDIATYPE=%s", mtype);
6067c478bd9Sstevel@tonic-gate (void) putenv(strdup(env_buf));
6077c478bd9Sstevel@tonic-gate (void) sprintf(env_buf, "VOLUME_SYMDEV=%s%d",
6087c478bd9Sstevel@tonic-gate mtype, mnum);
6097c478bd9Sstevel@tonic-gate (void) putenv(strdup(env_buf));
6107c478bd9Sstevel@tonic-gate (void) sprintf(env_buf, "VOLUME_PATH=%s",
6117c478bd9Sstevel@tonic-gate spcl);
6127c478bd9Sstevel@tonic-gate (void) putenv(strdup(env_buf));
6137c478bd9Sstevel@tonic-gate (void) sprintf(env_buf, "VOLUME_NAME=%s",
6147c478bd9Sstevel@tonic-gate vol_basename(spcl));
6157c478bd9Sstevel@tonic-gate (void) putenv(strdup(env_buf));
6167c478bd9Sstevel@tonic-gate } else {
6177c478bd9Sstevel@tonic-gate (void) sprintf(env_buf, "VOLUME_PATH=%s", bn);
6187c478bd9Sstevel@tonic-gate (void) putenv(strdup(env_buf));
6197c478bd9Sstevel@tonic-gate (void) sprintf(env_buf, "VOLUME_NAME=%s",
6207c478bd9Sstevel@tonic-gate vol_basename(bn));
6217c478bd9Sstevel@tonic-gate (void) putenv(strdup(env_buf));
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate #ifdef DEBUG
6247c478bd9Sstevel@tonic-gate dprintf("call_unmount_prog: calling \"%s -D\"\n", rmm);
6257c478bd9Sstevel@tonic-gate (void) execl(rmm, rmm, "-D", NULL);
6267c478bd9Sstevel@tonic-gate #else
6277c478bd9Sstevel@tonic-gate (void) execl(rmm, rmm, NULL);
6287c478bd9Sstevel@tonic-gate #endif
6297c478bd9Sstevel@tonic-gate } else {
6307c478bd9Sstevel@tonic-gate #ifdef DEBUG
6317c478bd9Sstevel@tonic-gate dprintf("call_unmount_prog: calling \"%s %s\"\n",
6327c478bd9Sstevel@tonic-gate etc_umount, mi_gotten ? spcl : bn);
6337c478bd9Sstevel@tonic-gate #endif
6347c478bd9Sstevel@tonic-gate (void) execl(etc_umount, etc_umount,
6357c478bd9Sstevel@tonic-gate mi_gotten ? spcl : bn,
6367c478bd9Sstevel@tonic-gate NULL);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate exit(-1);
6397c478bd9Sstevel@tonic-gate /*NOTREACHED*/
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate /* wait for the umount command to exit */
6437c478bd9Sstevel@tonic-gate if (waitpid(pid, &rval, 0) == pid) {
6447c478bd9Sstevel@tonic-gate if (WIFEXITED(rval)) {
6457c478bd9Sstevel@tonic-gate if (WEXITSTATUS(rval) == 0) {
6467c478bd9Sstevel@tonic-gate ret_val = TRUE; /* success */
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate dun:
6527c478bd9Sstevel@tonic-gate return (ret_val);
6537c478bd9Sstevel@tonic-gate }
654