xref: /illumos-gate/usr/src/cmd/devmgmt/cmds/getvol.c (revision 2a8bcb4e)
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 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <signal.h>
36 #include <sys/types.h>
37 #include <devmgmt.h>
38 
39 extern char	*optarg;
40 extern int	optind,
41 		ckquit,
42 		ckwidth;
43 
44 char	*prog;
45 char	*label, *fsname;
46 char	*prompt;
47 int	options = 0;
48 int	kpid = (-2);
49 int	signo = SIGKILL;
50 
51 static void usage(void);
52 
53 /*
54  * Given argv[0], return a pointer to the basename of the program.
55  */
56 static char *
prog_name(char * arg0)57 prog_name(char *arg0)
58 {
59 	char *str;
60 
61 	/* first strip trailing '/' characters (exec() allows these!) */
62 	str = arg0 + strlen(arg0);
63 	while (str > arg0 && *--str == '/')
64 		*str = '\0';
65 	if ((str = strrchr(arg0, '/')) != NULL)
66 		return (str + 1);
67 	return (arg0);
68 }
69 
70 int
main(int argc,char ** argv)71 main(int argc, char **argv)
72 {
73 	int c, n;
74 
75 	prog = prog_name(argv[0]);
76 
77 	while ((c = getopt(argc, argv, "fFownx:l:p:k:s:?QW:")) != EOF) {
78 		switch (c) {
79 		case 'Q':
80 			ckquit = 0;
81 			break;
82 
83 		case 'W':
84 			ckwidth = atol(optarg);
85 			break;
86 
87 		case 'f':
88 			options |= DM_FORMAT;
89 			break;
90 
91 		case 'F':
92 			options |= DM_FORMFS;
93 			break;
94 
95 		case 'o':
96 			options |= DM_OLABEL;
97 			break;
98 
99 		case 'n':
100 			options |= DM_BATCH;
101 			break;
102 
103 		case 'w':
104 			options |= DM_WLABEL;
105 			break;
106 
107 		case 'l':
108 			if (label)
109 				usage();
110 			label = optarg;
111 			break;
112 
113 		case 'p':
114 			prompt = optarg;
115 			break;
116 
117 		case 'x':
118 			if (label)
119 				usage();
120 			label = optarg;
121 			options |= DM_ELABEL;
122 			break;
123 
124 		case 'k':
125 			kpid = atol(optarg);
126 			break;
127 
128 		case 's':
129 			signo = atol(optarg);
130 			break;
131 
132 		default:
133 			usage();
134 		}
135 	}
136 
137 	if ((optind+1) != argc)
138 		usage();
139 
140 	switch (n = getvol(argv[optind], label, options, prompt)) {
141 	case 0:
142 		break;
143 
144 	case 1:
145 		(void) fprintf(stderr,
146 			"%s: ERROR: unable to access device <%s>\n",
147 			prog, argv[optind]);
148 		break;
149 
150 	case 2:
151 		(void) fprintf(stderr, "%s: ERROR: unknown device <%s>\n",
152 			prog, argv[optind]);
153 		break;
154 
155 	case 3:
156 		if (kpid > -2)
157 			(void) kill(kpid, signo);
158 		break;
159 
160 	case 4:
161 		(void) fprintf(stderr, "%s: ERROR: bad label on <%s>\n",
162 			prog, argv[optind]);
163 		break;
164 
165 	default:
166 		(void) fprintf(stderr, "%s: ERROR: unknown device error\n",
167 			prog);
168 		break;
169 	}
170 
171 	return (n);
172 }
173 
174 static void
usage()175 usage()
176 {
177 	fprintf(stderr,
178 	    "usage: %s [-owfF] [-x extlabel] [-l [fsname],volname] device\n",
179 	    prog);
180 	fprintf(stderr,
181 	    "usage: %s [-n] [-x extlabel] [-l [fsname],volname] device\n",
182 	    prog);
183 	exit(1);
184 }
185