xref: /illumos-gate/usr/src/cmd/devmgmt/cmds/getvol.c (revision 7c478bd9)
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 1997-2003 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 main(int argc, char **argv)
73 {
74 	int c, n;
75 
76 	prog = prog_name(argv[0]);
77 
78 	while ((c = getopt(argc, argv, "fFownx:l:p:k:s:?QW:")) != EOF) {
79 		switch (c) {
80 		case 'Q':
81 			ckquit = 0;
82 			break;
83 
84 		case 'W':
85 			ckwidth = atol(optarg);
86 			break;
87 
88 		case 'f':
89 			options |= DM_FORMAT;
90 			break;
91 
92 		case 'F':
93 			options |= DM_FORMFS;
94 			break;
95 
96 		case 'o':
97 			options |= DM_OLABEL;
98 			break;
99 
100 		case 'n':
101 			options |= DM_BATCH;
102 			break;
103 
104 		case 'w':
105 			options |= DM_WLABEL;
106 			break;
107 
108 		case 'l':
109 			if (label)
110 				usage();
111 			label = optarg;
112 			break;
113 
114 		case 'p':
115 			prompt = optarg;
116 			break;
117 
118 		case 'x':
119 			if (label)
120 				usage();
121 			label = optarg;
122 			options |= DM_ELABEL;
123 			break;
124 
125 		case 'k':
126 			kpid = atol(optarg);
127 			break;
128 
129 		case 's':
130 			signo = atol(optarg);
131 			break;
132 
133 		default:
134 			usage();
135 		}
136 	}
137 
138 	if ((optind+1) != argc)
139 		usage();
140 
141 	switch (n = getvol(argv[optind], label, options, prompt)) {
142 	case 0:
143 		break;
144 
145 	case 1:
146 		(void) fprintf(stderr,
147 			"%s: ERROR: unable to access device <%s>\n",
148 			prog, argv[optind]);
149 		break;
150 
151 	case 2:
152 		(void) fprintf(stderr, "%s: ERROR: unknown device <%s>\n",
153 			prog, argv[optind]);
154 		break;
155 
156 	case 3:
157 		if (kpid > -2)
158 			(void) kill(kpid, signo);
159 		break;
160 
161 	case 4:
162 		(void) fprintf(stderr, "%s: ERROR: bad label on <%s>\n",
163 			prog, argv[optind]);
164 		break;
165 
166 	default:
167 		(void) fprintf(stderr, "%s: ERROR: unknown device error\n",
168 			prog);
169 		break;
170 	}
171 
172 	return (n);
173 }
174 
175 static void
176 usage()
177 {
178 	fprintf(stderr,
179 	    "usage: %s [-owfF] [-x extlabel] [-l [fsname],volname] device\n",
180 	    prog);
181 	fprintf(stderr,
182 	    "usage: %s [-n] [-x extlabel] [-l [fsname],volname] device\n",
183 	    prog);
184 	exit(1);
185 }
186