xref: /illumos-gate/usr/src/cmd/devmgmt/cmds/getvol.c (revision 942c5e3c2dd127463517e5cc1694ee94ca45e021)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 
29 /*LINTLIBRARY*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <sys/types.h>
39 #include <devmgmt.h>
40 
41 extern char	*optarg;
42 extern int	optind,
43 		ckquit,
44 		ckwidth;
45 
46 char	*prog;
47 char	*label, *fsname;
48 char	*prompt;
49 int	options = 0;
50 int	kpid = (-2);
51 int	signo = SIGKILL;
52 
53 static void usage(void);
54 
55 /*
56  * Given argv[0], return a pointer to the basename of the program.
57  */
58 static char *
59 prog_name(char *arg0)
60 {
61 	char *str;
62 
63 	/* first strip trailing '/' characters (exec() allows these!) */
64 	str = arg0 + strlen(arg0);
65 	while (str > arg0 && *--str == '/')
66 		*str = '\0';
67 	if ((str = strrchr(arg0, '/')) != NULL)
68 		return (str + 1);
69 	return (arg0);
70 }
71 
72 int
73 main(int argc, char **argv)
74 {
75 	int c, n;
76 
77 	prog = prog_name(argv[0]);
78 
79 	while ((c = getopt(argc, argv, "fFownx:l:p:k:s:?QW:")) != EOF) {
80 		switch (c) {
81 		case 'Q':
82 			ckquit = 0;
83 			break;
84 
85 		case 'W':
86 			ckwidth = atol(optarg);
87 			break;
88 
89 		case 'f':
90 			options |= DM_FORMAT;
91 			break;
92 
93 		case 'F':
94 			options |= DM_FORMFS;
95 			break;
96 
97 		case 'o':
98 			options |= DM_OLABEL;
99 			break;
100 
101 		case 'n':
102 			options |= DM_BATCH;
103 			break;
104 
105 		case 'w':
106 			options |= DM_WLABEL;
107 			break;
108 
109 		case 'l':
110 			if (label)
111 				usage();
112 			label = optarg;
113 			break;
114 
115 		case 'p':
116 			prompt = optarg;
117 			break;
118 
119 		case 'x':
120 			if (label)
121 				usage();
122 			label = optarg;
123 			options |= DM_ELABEL;
124 			break;
125 
126 		case 'k':
127 			kpid = atol(optarg);
128 			break;
129 
130 		case 's':
131 			signo = atol(optarg);
132 			break;
133 
134 		default:
135 			usage();
136 		}
137 	}
138 
139 	if ((optind+1) != argc)
140 		usage();
141 
142 	switch (n = getvol(argv[optind], label, options, prompt)) {
143 	case 0:
144 		break;
145 
146 	case 1:
147 		(void) fprintf(stderr,
148 			"%s: ERROR: unable to access device <%s>\n",
149 			prog, argv[optind]);
150 		break;
151 
152 	case 2:
153 		(void) fprintf(stderr, "%s: ERROR: unknown device <%s>\n",
154 			prog, argv[optind]);
155 		break;
156 
157 	case 3:
158 		if (kpid > -2)
159 			(void) kill(kpid, signo);
160 		break;
161 
162 	case 4:
163 		(void) fprintf(stderr, "%s: ERROR: bad label on <%s>\n",
164 			prog, argv[optind]);
165 		break;
166 
167 	default:
168 		(void) fprintf(stderr, "%s: ERROR: unknown device error\n",
169 			prog);
170 		break;
171 	}
172 
173 	return (n);
174 }
175 
176 static void
177 usage()
178 {
179 	fprintf(stderr,
180 	    "usage: %s [-owfF] [-x extlabel] [-l [fsname],volname] device\n",
181 	    prog);
182 	fprintf(stderr,
183 	    "usage: %s [-n] [-x extlabel] [-l [fsname],volname] device\n",
184 	    prog);
185 	exit(1);
186 }
187