xref: /titanic_41/usr/src/cmd/volrmmount/volrmmount.c (revision 70818f5837509317d1f5dac4d82d7b5a2d547c29)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <dirent.h>
35 #include <signal.h>
36 #include <errno.h>
37 #include <libintl.h>
38 #include <sys/types.h>
39 
40 #include "vold.h"
41 #include "rmm_common.h"
42 
43 char *progname = "volrmmount";
44 
45 static void
46 usage()
47 {
48 	(void) fprintf(stderr,
49 	    gettext(
50 	    "\nusage: %s [-i | -e] [DEVICE | NICKNAME]\n"),
51 	    progname);
52 	(void) fprintf(stderr,
53 	    gettext("or:    %s -d\n"), progname);
54 	(void) fprintf(stderr,
55 	    gettext(
56 	    "options:\t-i        simulate volume being put in/inserted\n"));
57 	(void) fprintf(stderr,
58 	    gettext(
59 	    "options:\t-e        simulate volume being taken out/ejected\n"));
60 	(void) fprintf(stderr,
61 	    gettext("options:\t-d        show default device\n"));
62 	(void) fprintf(stderr,
63 	    gettext(
64 	    "\nThis command is deprecated. Use rmmount(1) instead.\n"));
65 }
66 
67 int
68 main(int argc, char **argv)
69 {
70 	const char	*opts = "ied";
71 	int		c;
72 	boolean_t	opt_insert = B_FALSE;
73 	boolean_t	opt_eject = B_FALSE;
74 	boolean_t	opt_default = B_FALSE;
75 	action_t	action;
76 	LibHalContext	*hal_ctx;
77 	DBusError	error;
78 	rmm_error_t	rmm_error;
79 	LibHalDrive	*d;
80 	GSList		*volumes;
81 	const char	*default_name;
82 	int		ret = 0;
83 
84 	while ((c = getopt(argc, argv, opts)) != EOF) {
85 		switch (c) {
86 		case 'i':
87 			opt_insert = B_TRUE;
88 			action = REMOUNT;
89 			break;
90 		case 'e':
91 			opt_eject = B_TRUE;
92 			action = UNMOUNT;
93 			break;
94 		case 'd':
95 			opt_default = B_TRUE;
96 			break;
97 		default:
98 			usage();
99 			return (1);
100 		}
101 	}
102 	if ((opt_insert && opt_eject) ||
103 	    (!opt_insert && !opt_eject && !opt_default)) {
104 		usage();
105 		return (1);
106 	}
107 
108 	if ((hal_ctx = rmm_hal_init(0, 0, 0, &error, &rmm_error)) == NULL) {
109 		(void) fprintf(stderr,
110 		    gettext("HAL initialization failed: %s\n"),
111 		    rmm_strerror(&error, rmm_error));
112 		rmm_dbus_error_free(&error);
113 		return (1);
114 	}
115 
116 	if (opt_default) {
117 		/* -d: print default name and exit */
118 		if ((d = rmm_hal_volume_find_default(hal_ctx, &error,
119 		    &default_name, &volumes)) == NULL) {
120 			default_name = "nothing inserted";
121 		} else {
122 			rmm_volumes_free(volumes);
123 			libhal_drive_free(d);
124 		}
125 		(void) printf(gettext("Default device is: %s\n"), default_name);
126 	} else if (optind == argc) {
127 		/* no name provided, use default */
128 		if ((d = rmm_hal_volume_find_default(hal_ctx, &error,
129 		    &default_name, &volumes)) == NULL) {
130 			(void) fprintf(stderr,
131 			    gettext("No default media available\n"));
132 			ret = 1;
133 		} else {
134 			rmm_volumes_free(volumes);
135 			libhal_drive_free(d);
136 			if (!rmm_action(hal_ctx, default_name, action,
137 			    0, 0, 0, 0)) {
138 				ret = 1;
139 			}
140 		}
141 	} else {
142 		for (; optind < argc; optind++) {
143 			if (rmm_action(hal_ctx, argv[optind], action,
144 			    0, 0, 0, 0) != 0) {
145 				ret = 1;
146 			}
147 		}
148 	}
149 
150 	rmm_hal_fini(hal_ctx);
151 
152 	return (ret);
153 }
154