118c2aff7Sartem /*
218c2aff7Sartem * CDDL HEADER START
318c2aff7Sartem *
418c2aff7Sartem * The contents of this file are subject to the terms of the
518c2aff7Sartem * Common Development and Distribution License (the "License").
618c2aff7Sartem * You may not use this file except in compliance with the License.
718c2aff7Sartem *
818c2aff7Sartem * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
918c2aff7Sartem * or http://www.opensolaris.org/os/licensing.
1018c2aff7Sartem * See the License for the specific language governing permissions
1118c2aff7Sartem * and limitations under the License.
1218c2aff7Sartem *
1318c2aff7Sartem * When distributing Covered Code, include this CDDL HEADER in each
1418c2aff7Sartem * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1518c2aff7Sartem * If applicable, add the following below this CDDL HEADER, with the
1618c2aff7Sartem * fields enclosed by brackets "[]" replaced with your own identifying
1718c2aff7Sartem * information: Portions Copyright [yyyy] [name of copyright owner]
1818c2aff7Sartem *
1918c2aff7Sartem * CDDL HEADER END
2018c2aff7Sartem */
2118c2aff7Sartem /*
2218c2aff7Sartem * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2318c2aff7Sartem * Use is subject to license terms.
24*48bbca81SDaniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
2518c2aff7Sartem */
2618c2aff7Sartem
2718c2aff7Sartem /*
2818c2aff7Sartem * Program to eject one or more pieces of media.
2918c2aff7Sartem */
3018c2aff7Sartem
3118c2aff7Sartem #include <stdio.h>
3218c2aff7Sartem #include <stdlib.h>
3318c2aff7Sartem #include <string.h>
3418c2aff7Sartem #include <sys/types.h>
3518c2aff7Sartem #include <sys/stat.h>
3618c2aff7Sartem #include <sys/fdio.h>
3718c2aff7Sartem #include <sys/dkio.h>
3818c2aff7Sartem #include <sys/cdio.h>
3918c2aff7Sartem #include <sys/param.h>
4018c2aff7Sartem #include <sys/wait.h>
4118c2aff7Sartem #include <dirent.h>
4218c2aff7Sartem #include <fcntl.h>
4318c2aff7Sartem #include <string.h>
4418c2aff7Sartem #include <errno.h>
4518c2aff7Sartem #include <locale.h>
4618c2aff7Sartem #include <libintl.h>
4718c2aff7Sartem #include <unistd.h>
4818c2aff7Sartem #include <pwd.h>
4918c2aff7Sartem #include <volmgt.h>
5018c2aff7Sartem #include <sys/mnttab.h>
5118c2aff7Sartem #include <signal.h>
5218c2aff7Sartem
5318c2aff7Sartem static char *prog_name = NULL;
5418c2aff7Sartem static boolean_t do_default = B_FALSE;
5518c2aff7Sartem static boolean_t do_list = B_FALSE;
5618c2aff7Sartem static boolean_t do_closetray = B_FALSE;
5718c2aff7Sartem static boolean_t force_eject = B_FALSE;
5818c2aff7Sartem static boolean_t do_query = B_FALSE;
5918c2aff7Sartem static boolean_t is_direct = B_FALSE;
6018c2aff7Sartem
6118c2aff7Sartem static int work(char *, char *);
6218c2aff7Sartem static void usage(void);
6318c2aff7Sartem static int ejectit(char *);
6418c2aff7Sartem static boolean_t query(char *, boolean_t);
6518c2aff7Sartem static boolean_t floppy_in_drive(char *, int, boolean_t *);
6618c2aff7Sartem static boolean_t display_busy(char *, boolean_t);
6718c2aff7Sartem static char *eject_getfullblkname(char *, boolean_t);
6818c2aff7Sartem extern char *getfullrawname(char *);
6918c2aff7Sartem
7018c2aff7Sartem /*
7118c2aff7Sartem * ON-private libvolmgt routines
7218c2aff7Sartem */
7318c2aff7Sartem int _dev_mounted(char *path);
7418c2aff7Sartem int _dev_unmount(char *path);
7518c2aff7Sartem char *_media_oldaliases(char *name);
7618c2aff7Sartem void _media_printaliases(void);
7718c2aff7Sartem
7818c2aff7Sartem
7918c2aff7Sartem /*
8018c2aff7Sartem * Hold over from old eject.
8118c2aff7Sartem * returns exit codes: (KEEP THESE - especially important for query)
8218c2aff7Sartem * 0 = -n, -d or eject operation was ok, -q = media in drive
8318c2aff7Sartem * 1 = -q only = media not in drive
8418c2aff7Sartem * 2 = various parameter errors, etc.
8518c2aff7Sartem * 3 = eject ioctl failed
8618c2aff7Sartem * New Value (2/94)
8718c2aff7Sartem * 4 = eject partially succeeded, but now manually remove media
8818c2aff7Sartem */
8918c2aff7Sartem
9018c2aff7Sartem #define EJECT_OK 0
9118c2aff7Sartem #define EJECT_NO_MEDIA 1
9218c2aff7Sartem #define EJECT_PARM_ERR 2
9318c2aff7Sartem #define EJECT_IOCTL_ERR 3
9418c2aff7Sartem #define EJECT_MAN_EJ 4
9518c2aff7Sartem
9618c2aff7Sartem #define AVAIL_MSG "%s is available\n"
9718c2aff7Sartem #define NOT_AVAIL_MSG "%s is not available\n"
9818c2aff7Sartem
9918c2aff7Sartem #define OK_TO_EJECT_MSG "%s can now be manually ejected\n"
10018c2aff7Sartem
10118c2aff7Sartem #define FLOPPY_MEDIA_TYPE "floppy"
10218c2aff7Sartem #define CDROM_MEDIA_TYPE "cdrom"
10318c2aff7Sartem
10418c2aff7Sartem
10518c2aff7Sartem int
main(int argc,char ** argv)10618c2aff7Sartem main(int argc, char **argv)
10718c2aff7Sartem {
10818c2aff7Sartem int c;
1097544909dSartem const char *opts = "dqflt";
11018c2aff7Sartem int excode;
11118c2aff7Sartem int res;
11218c2aff7Sartem boolean_t err_seen = B_FALSE;
11318c2aff7Sartem boolean_t man_eject_seen = B_FALSE;
11418c2aff7Sartem char *rmmount_opt = NULL;
11518c2aff7Sartem
11618c2aff7Sartem (void) setlocale(LC_ALL, "");
11718c2aff7Sartem
11818c2aff7Sartem #if !defined(TEXT_DOMAIN)
11918c2aff7Sartem #define TEXT_DOMAIN "SYS_TEST"
12018c2aff7Sartem #endif
12118c2aff7Sartem
12218c2aff7Sartem (void) textdomain(TEXT_DOMAIN);
12318c2aff7Sartem
12418c2aff7Sartem prog_name = argv[0];
12518c2aff7Sartem
12618c2aff7Sartem is_direct = (getenv("EJECT_DIRECT") != NULL);
12718c2aff7Sartem
12818c2aff7Sartem /* process arguments */
12918c2aff7Sartem while ((c = getopt(argc, argv, opts)) != EOF) {
13018c2aff7Sartem switch (c) {
13118c2aff7Sartem case 'd':
13218c2aff7Sartem do_default = B_TRUE;
13318c2aff7Sartem rmmount_opt = "-d";
13418c2aff7Sartem break;
13518c2aff7Sartem case 'q':
13618c2aff7Sartem do_query = B_TRUE;
13718c2aff7Sartem break;
13818c2aff7Sartem case 'l':
13918c2aff7Sartem do_list = B_TRUE;
14018c2aff7Sartem rmmount_opt = "-l";
14118c2aff7Sartem break;
14218c2aff7Sartem case 'f':
14318c2aff7Sartem force_eject = B_TRUE;
14418c2aff7Sartem break;
14518c2aff7Sartem case 't':
14618c2aff7Sartem do_closetray = B_TRUE;
14718c2aff7Sartem break;
14818c2aff7Sartem default:
14918c2aff7Sartem usage();
15018c2aff7Sartem exit(EJECT_PARM_ERR);
15118c2aff7Sartem }
15218c2aff7Sartem }
15318c2aff7Sartem
15418c2aff7Sartem if (argc == optind) {
15518c2aff7Sartem /* no argument -- use the default */
15618c2aff7Sartem excode = work(NULL, rmmount_opt);
15718c2aff7Sartem } else {
15818c2aff7Sartem /* multiple things to eject */
15918c2aff7Sartem for (; optind < argc; optind++) {
16018c2aff7Sartem res = work(argv[optind], rmmount_opt);
16118c2aff7Sartem if (res == EJECT_MAN_EJ) {
16218c2aff7Sartem man_eject_seen = B_TRUE;
16318c2aff7Sartem } else if (res != EJECT_OK) {
16418c2aff7Sartem err_seen = B_TRUE;
16518c2aff7Sartem }
16618c2aff7Sartem }
16718c2aff7Sartem if (err_seen) {
16818c2aff7Sartem if (!is_direct) {
16918c2aff7Sartem excode = res;
17018c2aff7Sartem } else {
17118c2aff7Sartem excode = EJECT_IOCTL_ERR;
17218c2aff7Sartem }
17318c2aff7Sartem } else if (man_eject_seen) {
17418c2aff7Sartem excode = EJECT_MAN_EJ;
17518c2aff7Sartem } else {
17618c2aff7Sartem excode = EJECT_OK;
17718c2aff7Sartem }
17818c2aff7Sartem }
17918c2aff7Sartem
18018c2aff7Sartem return (excode);
18118c2aff7Sartem }
18218c2aff7Sartem
18318c2aff7Sartem /*
18418c2aff7Sartem * the the real work of ejecting (and notifying)
18518c2aff7Sartem */
18618c2aff7Sartem static int
work(char * arg,char * rmmount_opt)18718c2aff7Sartem work(char *arg, char *rmmount_opt)
18818c2aff7Sartem {
18918c2aff7Sartem char *name;
19018c2aff7Sartem int excode = EJECT_OK;
19118c2aff7Sartem struct stat64 sb;
19218c2aff7Sartem char *arg1, *arg2;
19318c2aff7Sartem pid_t pid;
19418c2aff7Sartem int status = 1;
19518c2aff7Sartem
19618c2aff7Sartem if (!is_direct) {
19718c2aff7Sartem /* exec rmmount */
19818c2aff7Sartem if (do_closetray) {
19918c2aff7Sartem (void) putenv("EJECT_CLOSETRAY=1");
20018c2aff7Sartem }
20118c2aff7Sartem if (do_query) {
20218c2aff7Sartem (void) putenv("EJECT_QUERY=1");
20318c2aff7Sartem }
20418c2aff7Sartem pid = fork();
20518c2aff7Sartem if (pid < 0) {
20618c2aff7Sartem exit(1);
20718c2aff7Sartem } else if (pid == 0) {
20818c2aff7Sartem /* child */
20918c2aff7Sartem if (rmmount_opt != NULL) {
21018c2aff7Sartem arg1 = rmmount_opt;
21118c2aff7Sartem arg2 = arg;
21218c2aff7Sartem } else {
21318c2aff7Sartem arg1 = arg;
21418c2aff7Sartem arg2 = NULL;
21518c2aff7Sartem }
21618c2aff7Sartem
21718c2aff7Sartem if (execl("/usr/bin/rmmount", "eject",
21818c2aff7Sartem arg1, arg2, 0) < 0) {
2197544909dSartem excode = 99;
22018c2aff7Sartem } else {
22118c2aff7Sartem exit(0);
22218c2aff7Sartem }
22318c2aff7Sartem } else {
2247544909dSartem /* parent */
2257544909dSartem if (waitpid(pid, &status, 0) != pid) {
2267544909dSartem excode = 1;
2277544909dSartem } else if (WIFEXITED(status) &&
2287544909dSartem (WEXITSTATUS(status) != 0)) {
2297544909dSartem excode = WEXITSTATUS(status);
2307544909dSartem } else {
2317544909dSartem excode = 0;
2327544909dSartem }
23318c2aff7Sartem }
23418c2aff7Sartem }
23518c2aff7Sartem
23618c2aff7Sartem /*
23718c2aff7Sartem * rmmount returns 99 if HAL not running -
23818c2aff7Sartem * fallback to direct in that case
23918c2aff7Sartem */
24018c2aff7Sartem if (is_direct || (excode == 99)) {
2417544909dSartem excode = EJECT_OK;
2427544909dSartem
24318c2aff7Sartem if (arg == NULL) {
24418c2aff7Sartem arg = "floppy";
24518c2aff7Sartem }
24618c2aff7Sartem if ((name = _media_oldaliases(arg)) == NULL) {
24718c2aff7Sartem name = arg;
24818c2aff7Sartem }
24918c2aff7Sartem if (do_default) {
25018c2aff7Sartem (void) printf("%s\n", name);
25118c2aff7Sartem goto out;
25218c2aff7Sartem }
25318c2aff7Sartem if (do_list) {
25418c2aff7Sartem (void) printf("%s\t%s\n", name, arg);
25518c2aff7Sartem goto out;
25618c2aff7Sartem }
25718c2aff7Sartem if (access(name, R_OK) != 0) {
25818c2aff7Sartem if (do_query) {
25918c2aff7Sartem (void) fprintf(stderr,
26018c2aff7Sartem gettext("%s: no media\n"), name);
26118c2aff7Sartem return (EJECT_NO_MEDIA);
26218c2aff7Sartem } else {
26318c2aff7Sartem perror(name);
26418c2aff7Sartem return (EJECT_PARM_ERR);
26518c2aff7Sartem }
26618c2aff7Sartem }
26718c2aff7Sartem
26818c2aff7Sartem if (do_query) {
26918c2aff7Sartem if ((stat64(name, &sb) == 0) && S_ISDIR(sb.st_mode)) {
27018c2aff7Sartem (void) fprintf(stderr,
27118c2aff7Sartem gettext("%s: no media\n"), name);
27218c2aff7Sartem return (EJECT_NO_MEDIA);
27318c2aff7Sartem }
27418c2aff7Sartem if (!query(name, B_TRUE)) {
27518c2aff7Sartem excode = EJECT_NO_MEDIA;
27618c2aff7Sartem }
27718c2aff7Sartem } else {
27818c2aff7Sartem excode = ejectit(name);
27918c2aff7Sartem }
28018c2aff7Sartem }
28118c2aff7Sartem out:
28218c2aff7Sartem return (excode);
28318c2aff7Sartem }
28418c2aff7Sartem
28518c2aff7Sartem
28618c2aff7Sartem static void
usage(void)28718c2aff7Sartem usage(void)
28818c2aff7Sartem {
28918c2aff7Sartem (void) fprintf(stderr,
29018c2aff7Sartem gettext("usage: %s [-fldqt] [name | nickname]\n"),
29118c2aff7Sartem prog_name);
29218c2aff7Sartem (void) fprintf(stderr,
29318c2aff7Sartem gettext("options:\t-f force eject\n"));
29418c2aff7Sartem (void) fprintf(stderr,
29518c2aff7Sartem gettext("\t\t-l list ejectable devices\n"));
29618c2aff7Sartem (void) fprintf(stderr,
29718c2aff7Sartem gettext("\t\t-d show default device\n"));
29818c2aff7Sartem (void) fprintf(stderr,
29918c2aff7Sartem gettext("\t\t-q query for media present\n"));
30018c2aff7Sartem (void) fprintf(stderr,
30118c2aff7Sartem gettext("\t\t-t close tray\n"));
30218c2aff7Sartem }
30318c2aff7Sartem
30418c2aff7Sartem
30518c2aff7Sartem static int
ejectit(char * name)30618c2aff7Sartem ejectit(char *name)
30718c2aff7Sartem {
30818c2aff7Sartem int fd, r;
30918c2aff7Sartem boolean_t mejectable = B_FALSE; /* manually ejectable */
31018c2aff7Sartem int result = EJECT_OK;
31118c2aff7Sartem
31218c2aff7Sartem /*
31318c2aff7Sartem * If volume management is either not running or not being managed by
31418c2aff7Sartem * vold, and the device is mounted, we try to umount the device. If we
315*48bbca81SDaniel Hoffman * fail, we give up, unless it used the -f flag.
31618c2aff7Sartem */
31718c2aff7Sartem
31818c2aff7Sartem if (_dev_mounted(name)) {
31918c2aff7Sartem r = _dev_unmount(name);
32018c2aff7Sartem if (r == 0) {
32118c2aff7Sartem if (!force_eject) {
32218c2aff7Sartem (void) fprintf(stderr,
32318c2aff7Sartem gettext("WARNING: can not unmount %s, the file system is (probably) busy\n"),
32418c2aff7Sartem name);
32518c2aff7Sartem return (EJECT_PARM_ERR);
32618c2aff7Sartem } else {
32718c2aff7Sartem (void) fprintf(stderr,
32818c2aff7Sartem gettext("WARNING: %s has a mounted filesystem, ejecting anyway\n"),
32918c2aff7Sartem name);
33018c2aff7Sartem }
33118c2aff7Sartem }
33218c2aff7Sartem }
33318c2aff7Sartem
33418c2aff7Sartem /*
33518c2aff7Sartem * Require O_NDELAY for when floppy is not formatted
33618c2aff7Sartem * will still id floppy in drive
33718c2aff7Sartem */
33818c2aff7Sartem
33918c2aff7Sartem /*
34018c2aff7Sartem * make sure we are dealing with a raw device
34118c2aff7Sartem *
34218c2aff7Sartem * XXX: NOTE: results from getfullrawname()
34318c2aff7Sartem * really should be free()d when no longer
34418c2aff7Sartem * in use
34518c2aff7Sartem */
34618c2aff7Sartem name = getfullrawname(name);
34718c2aff7Sartem
34818c2aff7Sartem if ((fd = open(name, O_RDONLY | O_NDELAY)) < 0) {
34918c2aff7Sartem if (errno == EBUSY) {
35018c2aff7Sartem (void) fprintf(stderr,
35118c2aff7Sartem gettext("%s is busy (try 'eject floppy' or 'eject cdrom'?)\n"),
35218c2aff7Sartem name);
35318c2aff7Sartem return (EJECT_PARM_ERR);
35418c2aff7Sartem }
35518c2aff7Sartem perror(name);
35618c2aff7Sartem return (EJECT_PARM_ERR);
35718c2aff7Sartem }
35818c2aff7Sartem
35918c2aff7Sartem if (do_closetray) {
36018c2aff7Sartem if (ioctl(fd, CDROMCLOSETRAY) < 0) {
36118c2aff7Sartem result = EJECT_IOCTL_ERR;
36218c2aff7Sartem }
36318c2aff7Sartem } else if (ioctl(fd, DKIOCEJECT, 0) < 0) {
36418c2aff7Sartem /* check on why eject failed */
36518c2aff7Sartem
36618c2aff7Sartem /* check for no floppy in manually ejectable drive */
36718c2aff7Sartem if ((errno == ENOSYS) &&
36818c2aff7Sartem !floppy_in_drive(name, fd, &mejectable)) {
36918c2aff7Sartem /* use code below to handle "not present" */
37018c2aff7Sartem errno = ENXIO;
37118c2aff7Sartem }
37218c2aff7Sartem
37318c2aff7Sartem if (errno == ENOSYS || errno == ENOTSUP) {
37418c2aff7Sartem (void) fprintf(stderr, gettext(OK_TO_EJECT_MSG), name);
37518c2aff7Sartem }
37618c2aff7Sartem
37718c2aff7Sartem if ((errno == ENOSYS || errno == ENOTSUP) && mejectable) {
37818c2aff7Sartem /*
37918c2aff7Sartem * keep track of the fact that this is a manual
38018c2aff7Sartem * ejection
38118c2aff7Sartem */
38218c2aff7Sartem result = EJECT_MAN_EJ;
38318c2aff7Sartem
38418c2aff7Sartem } else if (errno == EBUSY) {
38518c2aff7Sartem /*
38618c2aff7Sartem * if our pathname is s slice (UFS is great) then
38718c2aff7Sartem * check to see what really is busy
38818c2aff7Sartem */
38918c2aff7Sartem if (!display_busy(name, B_FALSE)) {
39018c2aff7Sartem perror(name);
39118c2aff7Sartem }
39218c2aff7Sartem result = EJECT_IOCTL_ERR;
39318c2aff7Sartem
39418c2aff7Sartem } else if ((errno == EAGAIN) || (errno == ENODEV) ||
39518c2aff7Sartem (errno == ENXIO)) {
39618c2aff7Sartem (void) fprintf(stderr,
39718c2aff7Sartem gettext("%s not present in a drive\n"),
39818c2aff7Sartem name);
39918c2aff7Sartem result = EJECT_OK;
40018c2aff7Sartem } else {
40118c2aff7Sartem perror(name);
40218c2aff7Sartem result = EJECT_IOCTL_ERR;
40318c2aff7Sartem }
40418c2aff7Sartem }
40518c2aff7Sartem
40618c2aff7Sartem (void) close(fd);
40718c2aff7Sartem return (result);
40818c2aff7Sartem }
40918c2aff7Sartem
41018c2aff7Sartem
41118c2aff7Sartem /*
41218c2aff7Sartem * return B_TRUE if a floppy is in the drive, B_FALSE otherwise
41318c2aff7Sartem *
41418c2aff7Sartem * this routine assumes that the file descriptor passed in is for
41518c2aff7Sartem * a floppy disk. this works because it's only called if the device
41618c2aff7Sartem * is "manually ejectable", which only (currently) occurs for floppies.
41718c2aff7Sartem */
41818c2aff7Sartem static boolean_t
floppy_in_drive(char * name,int fd,boolean_t * is_floppy)41918c2aff7Sartem floppy_in_drive(char *name, int fd, boolean_t *is_floppy)
42018c2aff7Sartem {
42118c2aff7Sartem int ival = 0;
42218c2aff7Sartem boolean_t rval = B_FALSE;
42318c2aff7Sartem
42418c2aff7Sartem
42518c2aff7Sartem if (ioctl(fd, FDGETCHANGE, &ival) >= 0) {
42618c2aff7Sartem if (!(ival & FDGC_CURRENT)) {
42718c2aff7Sartem rval = B_TRUE;
42818c2aff7Sartem }
42918c2aff7Sartem *is_floppy = B_TRUE;
43018c2aff7Sartem } else {
43118c2aff7Sartem *is_floppy = B_FALSE;
43218c2aff7Sartem (void) fprintf(stderr, gettext("%s is not a floppy disk\n"),
43318c2aff7Sartem name);
43418c2aff7Sartem }
43518c2aff7Sartem
43618c2aff7Sartem return (rval);
43718c2aff7Sartem }
43818c2aff7Sartem
43918c2aff7Sartem
44018c2aff7Sartem /*
44118c2aff7Sartem * display a "busy" message for the supplied pathname
44218c2aff7Sartem *
44318c2aff7Sartem * if the pathname is not a slice, then just display a busy message
44418c2aff7Sartem * else if the pathname is some slice subdirectory then look for the
44518c2aff7Sartem * *real* culprits
44618c2aff7Sartem *
44718c2aff7Sartem * if this is not done then the user can get a message like
44818c2aff7Sartem * /vol/dev/rdsk/c0t6d0/solaris_2_5_sparc/s5: Device busy
44918c2aff7Sartem * when they try to eject "cdrom0", but "s0" (e.g.) may be the only busy
45018c2aff7Sartem * slice
45118c2aff7Sartem *
45218c2aff7Sartem * return B_TRUE iff we printed the appropriate error message, else
45318c2aff7Sartem * return B_FALSE (and caller will print error message itself)
45418c2aff7Sartem */
45518c2aff7Sartem static boolean_t
display_busy(char * path,boolean_t vm_running)45618c2aff7Sartem display_busy(char *path, boolean_t vm_running)
45718c2aff7Sartem {
45818c2aff7Sartem int errno_save = errno; /* to save errno */
45918c2aff7Sartem char *blk; /* block name */
46018c2aff7Sartem FILE *fp = NULL; /* for scanning mnttab */
46118c2aff7Sartem struct mnttab mref; /* for scanning mnttab */
46218c2aff7Sartem struct mnttab mp; /* for scanning mnttab */
46318c2aff7Sartem boolean_t res = B_FALSE; /* return value */
46418c2aff7Sartem char busy_base[MAXPATHLEN]; /* for keeping base dir name */
46518c2aff7Sartem uint_t bblen; /* busy_base string length */
46618c2aff7Sartem char *cp; /* for truncating path */
46718c2aff7Sartem
46818c2aff7Sartem
46918c2aff7Sartem
47018c2aff7Sartem #ifdef DEBUG
47118c2aff7Sartem (void) fprintf(stderr, "display_busy(\"%s\"): entering\n", path);
47218c2aff7Sartem #endif
47318c2aff7Sartem
47418c2aff7Sartem /*
47518c2aff7Sartem * get the block pathname.
47618c2aff7Sartem * eject_getfullblkname returns NULL or pathname which
47718c2aff7Sartem * has length < MAXPATHLEN.
47818c2aff7Sartem */
47918c2aff7Sartem blk = eject_getfullblkname(path, vm_running);
48018c2aff7Sartem if (blk == NULL)
48118c2aff7Sartem goto dun;
48218c2aff7Sartem
48318c2aff7Sartem /* open mnttab for scanning */
48418c2aff7Sartem if ((fp = fopen(MNTTAB, "r")) == NULL) {
48518c2aff7Sartem /* can't open mnttab!? -- give up */
48618c2aff7Sartem goto dun;
48718c2aff7Sartem }
48818c2aff7Sartem
48918c2aff7Sartem (void) memset((void *)&mref, '\0', sizeof (struct mnttab));
49018c2aff7Sartem mref.mnt_special = blk;
49118c2aff7Sartem if (getmntany(fp, &mp, &mref) == 0) {
49218c2aff7Sartem /* we found our entry -- we're done */
49318c2aff7Sartem goto dun;
49418c2aff7Sartem }
49518c2aff7Sartem
49618c2aff7Sartem /* perhaps we have a sub-slice (which is what we exist to test for) */
49718c2aff7Sartem
49818c2aff7Sartem /* create a base pathname */
49918c2aff7Sartem (void) strcpy(busy_base, blk);
50018c2aff7Sartem if ((cp = strrchr(busy_base, '/')) == NULL) {
50118c2aff7Sartem /* no last slash in pathname!!?? -- give up */
50218c2aff7Sartem goto dun;
50318c2aff7Sartem }
50418c2aff7Sartem *cp = '\0';
50518c2aff7Sartem bblen = strlen(busy_base);
50618c2aff7Sartem /* bblen = (uint)(cp - busy_base); */
50718c2aff7Sartem
50818c2aff7Sartem /* scan for matches */
50918c2aff7Sartem rewind(fp); /* rescan mnttab */
51018c2aff7Sartem while (getmntent(fp, &mp) == 0) {
51118c2aff7Sartem /*
51218c2aff7Sartem * work around problem where '-' in /etc/mnttab for
51318c2aff7Sartem * special device turns to NULL which isn't expected
51418c2aff7Sartem */
51518c2aff7Sartem if (mp.mnt_special == NULL)
51618c2aff7Sartem mp.mnt_special = "-";
51718c2aff7Sartem if (strncmp(busy_base, mp.mnt_special, bblen) == 0) {
51818c2aff7Sartem res = B_TRUE;
51918c2aff7Sartem (void) fprintf(stderr, "%s: %s\n", mp.mnt_special,
52018c2aff7Sartem strerror(EBUSY));
52118c2aff7Sartem }
52218c2aff7Sartem }
52318c2aff7Sartem
52418c2aff7Sartem dun:
52518c2aff7Sartem if (fp != NULL) {
52618c2aff7Sartem (void) fclose(fp);
52718c2aff7Sartem }
52818c2aff7Sartem #ifdef DEBUG
52918c2aff7Sartem (void) fprintf(stderr, "display_busy: returning %s\n",
53018c2aff7Sartem res ? "B_TRUE" : "B_FALSE");
53118c2aff7Sartem #endif
53218c2aff7Sartem errno = errno_save;
53318c2aff7Sartem return (res);
53418c2aff7Sartem }
53518c2aff7Sartem
53618c2aff7Sartem
53718c2aff7Sartem /*
53818c2aff7Sartem * In my experience with removable media drivers so far... the
53918c2aff7Sartem * most reliable way to tell if a piece of media is in a drive
54018c2aff7Sartem * is simply to open it. If the open works, there's something there,
54118c2aff7Sartem * if it fails, there's not. We check for two errnos which we
54218c2aff7Sartem * want to interpret for the user, ENOENT and EPERM. All other
54318c2aff7Sartem * errors are considered to be "media isn't there".
54418c2aff7Sartem *
54518c2aff7Sartem * return B_TRUE if media found, else B_FALSE (XXX: was 0 and -1)
54618c2aff7Sartem */
54718c2aff7Sartem static boolean_t
query(char * name,boolean_t doprint)54818c2aff7Sartem query(char *name, boolean_t doprint)
54918c2aff7Sartem {
55018c2aff7Sartem int fd;
55118c2aff7Sartem int rval; /* FDGETCHANGE return value */
55218c2aff7Sartem enum dkio_state state;
55318c2aff7Sartem
55418c2aff7Sartem if ((fd = open(name, O_RDONLY|O_NONBLOCK)) < 0) {
55518c2aff7Sartem if ((errno == EPERM) || (errno == ENOENT)) {
55618c2aff7Sartem if (doprint) {
55718c2aff7Sartem perror(name);
55818c2aff7Sartem }
55918c2aff7Sartem } else {
56018c2aff7Sartem if (doprint) {
56118c2aff7Sartem (void) fprintf(stderr, gettext(NOT_AVAIL_MSG),
56218c2aff7Sartem name);
56318c2aff7Sartem }
56418c2aff7Sartem }
56518c2aff7Sartem return (B_FALSE);
56618c2aff7Sartem }
56718c2aff7Sartem
56818c2aff7Sartem rval = 0;
56918c2aff7Sartem if (ioctl(fd, FDGETCHANGE, &rval) >= 0) {
57018c2aff7Sartem /* hey, it worked, what a deal, it must be a floppy */
57118c2aff7Sartem (void) close(fd);
57218c2aff7Sartem if (!(rval & FDGC_CURRENT)) {
57318c2aff7Sartem if (doprint) {
57418c2aff7Sartem (void) fprintf(stderr, gettext(AVAIL_MSG),
57518c2aff7Sartem name);
57618c2aff7Sartem }
57718c2aff7Sartem return (B_TRUE);
57818c2aff7Sartem }
57918c2aff7Sartem if (rval & FDGC_CURRENT) {
58018c2aff7Sartem if (doprint) {
58118c2aff7Sartem (void) fprintf(stderr, gettext(NOT_AVAIL_MSG),
58218c2aff7Sartem name);
58318c2aff7Sartem }
58418c2aff7Sartem return (B_FALSE);
58518c2aff7Sartem }
58618c2aff7Sartem }
58718c2aff7Sartem
58818c2aff7Sartem again:
58918c2aff7Sartem state = DKIO_NONE;
59018c2aff7Sartem if (ioctl(fd, DKIOCSTATE, &state) >= 0) {
59118c2aff7Sartem /* great, the fancy ioctl is supported. */
59218c2aff7Sartem if (state == DKIO_INSERTED) {
59318c2aff7Sartem if (doprint) {
59418c2aff7Sartem (void) fprintf(stderr, gettext(AVAIL_MSG),
59518c2aff7Sartem name);
59618c2aff7Sartem }
59718c2aff7Sartem (void) close(fd);
59818c2aff7Sartem return (B_TRUE);
59918c2aff7Sartem }
60018c2aff7Sartem if (state == DKIO_EJECTED) {
60118c2aff7Sartem if (doprint) {
60218c2aff7Sartem (void) fprintf(stderr, gettext(NOT_AVAIL_MSG),
60318c2aff7Sartem name);
60418c2aff7Sartem }
60518c2aff7Sartem (void) close(fd);
60618c2aff7Sartem return (B_FALSE);
60718c2aff7Sartem }
60818c2aff7Sartem /*
60918c2aff7Sartem * Silly retry loop.
61018c2aff7Sartem */
61118c2aff7Sartem (void) sleep(1);
61218c2aff7Sartem goto again;
61318c2aff7Sartem }
61418c2aff7Sartem (void) close(fd);
61518c2aff7Sartem
61618c2aff7Sartem /*
61718c2aff7Sartem * Ok, we've tried the non-blocking/ioctl route. The
61818c2aff7Sartem * device doesn't support any of our nice ioctls, so
61918c2aff7Sartem * we'll just say that if it opens it's there, if it
62018c2aff7Sartem * doesn't, it's not.
62118c2aff7Sartem */
62218c2aff7Sartem if ((fd = open(name, O_RDONLY)) < 0) {
62318c2aff7Sartem if (doprint) {
62418c2aff7Sartem (void) fprintf(stderr, gettext(NOT_AVAIL_MSG), name);
62518c2aff7Sartem }
62618c2aff7Sartem return (B_FALSE);
62718c2aff7Sartem }
62818c2aff7Sartem
62918c2aff7Sartem (void) close(fd);
63018c2aff7Sartem if (doprint) {
63118c2aff7Sartem (void) fprintf(stderr, gettext(AVAIL_MSG), name);
63218c2aff7Sartem }
63318c2aff7Sartem return (B_TRUE); /* success */
63418c2aff7Sartem }
63518c2aff7Sartem
63618c2aff7Sartem
63718c2aff7Sartem /*
63818c2aff7Sartem * this routine will return the volmgt block name given the volmgt
63918c2aff7Sartem * raw (char spcl) name
64018c2aff7Sartem *
64118c2aff7Sartem * if anything but a volmgt raw pathname is supplied that pathname will
64218c2aff7Sartem * be returned
64318c2aff7Sartem *
64418c2aff7Sartem * NOTE: non-null return value will point to static data, overwritten with
64518c2aff7Sartem * each call
64618c2aff7Sartem *
64718c2aff7Sartem * e.g. names starting with "/vol/r" will be changed to start with "/vol/",
64818c2aff7Sartem * and names starting with "vol/dev/r" will be changed to start with
64918c2aff7Sartem * "/vol/dev/"
65018c2aff7Sartem */
65118c2aff7Sartem static char *
eject_getfullblkname(char * path,boolean_t vm_running)65218c2aff7Sartem eject_getfullblkname(char *path, boolean_t vm_running)
65318c2aff7Sartem {
65418c2aff7Sartem char raw_root[MAXPATHLEN];
65518c2aff7Sartem const char *vm_root;
65618c2aff7Sartem static char res_buf[MAXPATHLEN];
65718c2aff7Sartem uint_t raw_root_len;
65818c2aff7Sartem
65918c2aff7Sartem #ifdef DEBUG
66018c2aff7Sartem (void) fprintf(stderr, "eject_getfullblkname(\"%s\", %s): entering\n",
66118c2aff7Sartem path, vm_running ? "B_TRUE" : "B_FALSE");
66218c2aff7Sartem #endif
66318c2aff7Sartem /*
66418c2aff7Sartem * try different strategies based on whether or not vold is running
66518c2aff7Sartem */
66618c2aff7Sartem if (vm_running) {
66718c2aff7Sartem
66818c2aff7Sartem /* vold IS running -- look in /vol (or its alternate) */
66918c2aff7Sartem
67018c2aff7Sartem /* get vm root dir */
67118c2aff7Sartem vm_root = volmgt_root();
67218c2aff7Sartem
67318c2aff7Sartem /* get first volmgt root dev directory (and its length) */
67418c2aff7Sartem (void) snprintf(raw_root, sizeof (raw_root), "%s/r", vm_root);
67518c2aff7Sartem raw_root_len = strlen(raw_root);
67618c2aff7Sartem
67718c2aff7Sartem /* see if we have a raw volmgt pathname (e.g. "/vol/r*") */
67818c2aff7Sartem if (strncmp(path, raw_root, raw_root_len) == 0) {
67918c2aff7Sartem if (snprintf(res_buf, sizeof (res_buf), "%s/%s",
68018c2aff7Sartem vm_root, path + raw_root_len) >= sizeof (res_buf)) {
68118c2aff7Sartem return (NULL);
68218c2aff7Sartem }
68318c2aff7Sartem goto dun; /* found match in /vol */
68418c2aff7Sartem }
68518c2aff7Sartem
68618c2aff7Sartem /* get second volmgt root dev directory (and its length) */
68718c2aff7Sartem (void) snprintf(raw_root, sizeof (raw_root),
68818c2aff7Sartem "%s/dev/r", vm_root);
68918c2aff7Sartem raw_root_len = strlen(raw_root);
69018c2aff7Sartem
69118c2aff7Sartem /* see if we have a raw volmgt pathname (e.g. "/vol/dev/r*") */
69218c2aff7Sartem if (strncmp(path, raw_root, raw_root_len) == 0) {
69318c2aff7Sartem if (snprintf(res_buf, sizeof (res_buf), "%s/dev/%s",
69418c2aff7Sartem vm_root, path + raw_root_len) >= sizeof (res_buf)) {
69518c2aff7Sartem return (NULL);
69618c2aff7Sartem }
69718c2aff7Sartem goto dun; /* found match in /vol/dev */
69818c2aff7Sartem }
69918c2aff7Sartem
70018c2aff7Sartem } else {
70118c2aff7Sartem
70218c2aff7Sartem /* vold is NOT running -- look in /dev */
70318c2aff7Sartem
70418c2aff7Sartem (void) strcpy(raw_root, "/dev/r");
70518c2aff7Sartem raw_root_len = strlen(raw_root);
70618c2aff7Sartem if (strncmp(path, raw_root, raw_root_len) == 0) {
70718c2aff7Sartem if (snprintf(res_buf, sizeof (res_buf), "/dev/%s",
70818c2aff7Sartem path + raw_root_len) >= sizeof (res_buf)) {
70918c2aff7Sartem return (NULL);
71018c2aff7Sartem }
71118c2aff7Sartem goto dun; /* found match in /dev */
71218c2aff7Sartem }
71318c2aff7Sartem }
71418c2aff7Sartem
71518c2aff7Sartem /* no match -- return what we got */
71618c2aff7Sartem (void) strcpy(res_buf, path);
71718c2aff7Sartem
71818c2aff7Sartem dun:
71918c2aff7Sartem #ifdef DEBUG
72018c2aff7Sartem (void) fprintf(stderr, "eject_getfullblkname: returning %s\n",
72118c2aff7Sartem res_buf ? res_buf : "<null ptr>");
72218c2aff7Sartem #endif
72318c2aff7Sartem return (res_buf);
72418c2aff7Sartem }
725