1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*    Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*0Sstevel@tonic-gate /*      All Rights Reserved   */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  *	devfree key [device [...]]
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include	<sys/types.h>
38*0Sstevel@tonic-gate #include	<sys/param.h>
39*0Sstevel@tonic-gate #include	<stdio.h>
40*0Sstevel@tonic-gate #include	<errno.h>
41*0Sstevel@tonic-gate #include	<stdlib.h>
42*0Sstevel@tonic-gate #include	<string.h>
43*0Sstevel@tonic-gate #include	<fmtmsg.h>
44*0Sstevel@tonic-gate #include	<devmgmt.h>
45*0Sstevel@tonic-gate #include	<values.h>
46*0Sstevel@tonic-gate #include	<devtab.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  *  Local definitions
51*0Sstevel@tonic-gate  *	TRUE		Boolean TRUE value
52*0Sstevel@tonic-gate  *	FALSE		Boolean FALSE value
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate #ifndef		TRUE
55*0Sstevel@tonic-gate #define		TRUE		('t')
56*0Sstevel@tonic-gate #endif
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #ifndef		FALSE
59*0Sstevel@tonic-gate #define		FALSE		0
60*0Sstevel@tonic-gate #endif
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate  *  Exit codes:
65*0Sstevel@tonic-gate  *	EX_OK		Exit code for all went well
66*0Sstevel@tonic-gate  *	EX_ERROR	Exit code for something failed
67*0Sstevel@tonic-gate  *	EX_TBLERR	Exit code for errors relating to device or lock tables
68*0Sstevel@tonic-gate  *	EX_NOFREE	Exit code for free failed
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate #define		EX_OK		0
72*0Sstevel@tonic-gate #define		EX_ERROR	1
73*0Sstevel@tonic-gate #define		EX_TBLERR	2
74*0Sstevel@tonic-gate #define		EX_NOFREE	3
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate /*
78*0Sstevel@tonic-gate  * Messages
79*0Sstevel@tonic-gate  *	M_USAGE		Usage error
80*0Sstevel@tonic-gate  *	M_INVKEY	Invalid key specified
81*0Sstevel@tonic-gate  *	M_NOTRSVD	Attempting to free something not alloc'd
82*0Sstevel@tonic-gate  *	M_NOTONKEY	Attempting to free with wrong key
83*0Sstevel@tonic-gate  *	M_DEVTAB	Error opening the device table
84*0Sstevel@tonic-gate  *	M_RSVTAB	Error opening the device-reservation table
85*0Sstevel@tonic-gate  *	M_ERROR		Some internal error
86*0Sstevel@tonic-gate  */
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate #define		M_USAGE		"usage: devfree key [device [...]]"
89*0Sstevel@tonic-gate #define		M_INVKEY	"Invalid key: %s"
90*0Sstevel@tonic-gate #define		M_NOTRSVD	"Device not reserved: %s"
91*0Sstevel@tonic-gate #define		M_NOTONKEY	"Cannot unreserve device: %s"
92*0Sstevel@tonic-gate #define		M_DEVTAB	"Cannot open the device table: %s"
93*0Sstevel@tonic-gate #define		M_RSVTAB	"Cannot open the device-reservation table: %s"
94*0Sstevel@tonic-gate #define		M_ERROR		"Internal error, errno=%d"
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate /*
98*0Sstevel@tonic-gate  *  Local functions and static data
99*0Sstevel@tonic-gate  *	stdmsg(r,l,s,m)		Macro for standard message generation
100*0Sstevel@tonic-gate  *				r	MM_NRECOV or MM_RECOV (recoverability)
101*0Sstevel@tonic-gate  *				l	Label
102*0Sstevel@tonic-gate  *				s	Severity
103*0Sstevel@tonic-gate  *				m	Message
104*0Sstevel@tonic-gate  *	lbl			Buffer for the label-component of a message.
105*0Sstevel@tonic-gate  *	msg			Buffer for the text-component of a message.
106*0Sstevel@tonic-gate  */
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate #define	stdmsg(r,l,s,m)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,m,MM_NULLACT,MM_NULLTAG)
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate static	char	lbl[MM_MXLABELLN+1];
111*0Sstevel@tonic-gate static	char	msg[MM_MXTXTLN+1];
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate /*
114*0Sstevel@tonic-gate  *  devfree key [device [device [...]]]
115*0Sstevel@tonic-gate  *
116*0Sstevel@tonic-gate  *	This command frees devices that have been reserved using
117*0Sstevel@tonic-gate  *	the devreserv command (or the devreserv() function).
118*0Sstevel@tonic-gate  *
119*0Sstevel@tonic-gate  *  Options:  None
120*0Sstevel@tonic-gate  *
121*0Sstevel@tonic-gate  *  Arguments:
122*0Sstevel@tonic-gate  *	key		The key on which the device to free was allocated on.
123*0Sstevel@tonic-gate  *			If omitted, all keys are assumed.
124*0Sstevel@tonic-gate  *	device		The device to free.  If omitted, all devices allocated
125*0Sstevel@tonic-gate  *			using the key are freed.
126*0Sstevel@tonic-gate  *
127*0Sstevel@tonic-gate  *  Command Values:
128*0Sstevel@tonic-gate  *	EX_OK		0	Device(s) successfully freed
129*0Sstevel@tonic-gate  *	EX_ERROR	1	A syntax error or other error occurred
130*0Sstevel@tonic-gate  *	EX_TBLERR	2	A problem with device management tables
131*0Sstevel@tonic-gate  *	EX_NOFREE	3	A requested device couldn't be freed
132*0Sstevel@tonic-gate  */
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate main(argc, argv)
135*0Sstevel@tonic-gate 	int		argc;		/* Arg count */
136*0Sstevel@tonic-gate 	char	       *argv[];		/* Arg vector */
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate 	/* Automatics */
139*0Sstevel@tonic-gate 	char		      **argp;		/* Ptr to current argument */
140*0Sstevel@tonic-gate 	struct reservdev      **rsvd;		/* Ptr to list of locks */
141*0Sstevel@tonic-gate 	struct reservdev      **plk;		/* Running ptr to locks */
142*0Sstevel@tonic-gate 	char		       *devtab;		/* Ptr to device table name */
143*0Sstevel@tonic-gate 	char		       *rsvtab;		/* Ptr to dev-rsv-tbl name */
144*0Sstevel@tonic-gate 	char		       *p;		/* Temp char pointer */
145*0Sstevel@tonic-gate 	int			argcount;	/* Number of args on cmd */
146*0Sstevel@tonic-gate 	long			lkey;		/* Key for locking (long) */
147*0Sstevel@tonic-gate 	int			key;		/* Key for locking */
148*0Sstevel@tonic-gate 	int			halt;		/* TRUE if we need to stop */
149*0Sstevel@tonic-gate 	int			sev;		/* Message severity */
150*0Sstevel@tonic-gate 	int			exitcode;	/* Value of command */
151*0Sstevel@tonic-gate 	int			syntaxerr;	/* Flag, TRUE if syntax error */
152*0Sstevel@tonic-gate 	int			exitcd;		/* Value for exit() */
153*0Sstevel@tonic-gate 	int			c;		/* Option character */
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	/*
157*0Sstevel@tonic-gate 	 * Initializations
158*0Sstevel@tonic-gate 	 */
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	/* Build a message label */
161*0Sstevel@tonic-gate 	if (p = strrchr(argv[0], '/')) p++;
162*0Sstevel@tonic-gate 	else p = argv[0];
163*0Sstevel@tonic-gate 	(void) strlcat(strcpy(lbl, "UX:"), p, sizeof(lbl));
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	/* Make only the text component of messages appear (remove this in SVR4.1) */
166*0Sstevel@tonic-gate 	(void) putenv("MSGVERB=text");
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	/*
170*0Sstevel@tonic-gate 	 * Parse the options from the command line
171*0Sstevel@tonic-gate 	 */
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	opterr = 0;
174*0Sstevel@tonic-gate 	syntaxerr = FALSE;
175*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "")) != EOF) switch(c) {
176*0Sstevel@tonic-gate 	default:
177*0Sstevel@tonic-gate 	    syntaxerr = FALSE;
178*0Sstevel@tonic-gate 	    break;
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	/* Argument initializations */
183*0Sstevel@tonic-gate 	argp = &argv[optind];
184*0Sstevel@tonic-gate 	if ((argcount = argc-optind) < 1) syntaxerr = TRUE;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	/* If there's (an obvious) syntax error, write a message and quit */
188*0Sstevel@tonic-gate 	if (syntaxerr) {
189*0Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
190*0Sstevel@tonic-gate 	    exit(EX_ERROR);
191*0Sstevel@tonic-gate 	}
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	/*
195*0Sstevel@tonic-gate 	 *  devfree key
196*0Sstevel@tonic-gate 	 *
197*0Sstevel@tonic-gate 	 *  	Free all devices that have been reserved using the key "key".
198*0Sstevel@tonic-gate 	 */
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	if (argcount == 1) {
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	    /* Extract the key from the command */
203*0Sstevel@tonic-gate 	    lkey = strtol(*argp, &p, 10);
204*0Sstevel@tonic-gate 	    if (*p || (lkey <= 0) || (lkey > MAXINT)) {
205*0Sstevel@tonic-gate 		(void) snprintf(msg, sizeof (msg), M_INVKEY, *argp);
206*0Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
207*0Sstevel@tonic-gate 		exit(EX_ERROR);
208*0Sstevel@tonic-gate 	    }
209*0Sstevel@tonic-gate 	    key = (int) lkey;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	    /* Get the list of devices currently reserved */
212*0Sstevel@tonic-gate 	    if (rsvd = reservdev()) {
213*0Sstevel@tonic-gate 		exitcd = EX_OK;
214*0Sstevel@tonic-gate 		for (plk = rsvd ; *plk ; plk++) {
215*0Sstevel@tonic-gate 		    if ((*plk)->key == key)
216*0Sstevel@tonic-gate 			if (devfree(key, (*plk)->devname) != 0)
217*0Sstevel@tonic-gate 			    exitcd = EX_NOFREE;
218*0Sstevel@tonic-gate 		}
219*0Sstevel@tonic-gate 	    } else {
220*0Sstevel@tonic-gate 		if (((errno == ENOENT) || (errno == EACCES)) && (rsvtab = _rsvtabpath())) {
221*0Sstevel@tonic-gate 		    (void) snprintf(msg, sizeof(msg), M_RSVTAB, rsvtab);
222*0Sstevel@tonic-gate 		    exitcd = EX_TBLERR;
223*0Sstevel@tonic-gate 		    sev = MM_ERROR;
224*0Sstevel@tonic-gate 		} else {
225*0Sstevel@tonic-gate 		    (void) snprintf(msg, sizeof (msg), M_ERROR, errno);
226*0Sstevel@tonic-gate 		    exitcd = EX_ERROR;
227*0Sstevel@tonic-gate 		    sev = MM_HALT;
228*0Sstevel@tonic-gate 		}
229*0Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, sev, msg);
230*0Sstevel@tonic-gate 	    }
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	    /* Done */
233*0Sstevel@tonic-gate 	    exit(exitcd);
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	/*
238*0Sstevel@tonic-gate 	 *  devfree key device [...]
239*0Sstevel@tonic-gate 	 *
240*0Sstevel@tonic-gate 	 *	Free specific devices
241*0Sstevel@tonic-gate 	 */
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	/* Open the device file (if there's one to be opened) */
244*0Sstevel@tonic-gate 	if (!_opendevtab("r")) {
245*0Sstevel@tonic-gate 	    if (devtab = _devtabpath()) {
246*0Sstevel@tonic-gate 		(void) snprintf(msg, sizeof(msg), M_DEVTAB, devtab);
247*0Sstevel@tonic-gate 		exitcd = EX_TBLERR;
248*0Sstevel@tonic-gate 		sev = MM_ERROR;
249*0Sstevel@tonic-gate 	    } else {
250*0Sstevel@tonic-gate 		(void) snprintf(msg, sizeof (msg), M_ERROR, errno);
251*0Sstevel@tonic-gate 		exitcd = EX_ERROR;
252*0Sstevel@tonic-gate 		sev = MM_HALT;
253*0Sstevel@tonic-gate 	    }
254*0Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, sev, msg);
255*0Sstevel@tonic-gate 	    exit(exitcd);
256*0Sstevel@tonic-gate 	}
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	/* Extract the key from the command */
259*0Sstevel@tonic-gate 	lkey = strtol(*argp, &p, 10);
260*0Sstevel@tonic-gate 	if (*p || (lkey <= 0) || (lkey > MAXINT)) {
261*0Sstevel@tonic-gate 	    (void) snprintf(msg, sizeof(msg), M_INVKEY, *argp);
262*0Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
263*0Sstevel@tonic-gate 	    exit(EX_ERROR);
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 	key = (int) lkey;
266*0Sstevel@tonic-gate 	argp++;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	/* Loop through the list of devices to free */
269*0Sstevel@tonic-gate 	exitcode = EX_OK;
270*0Sstevel@tonic-gate 	halt = FALSE;
271*0Sstevel@tonic-gate 	while (!halt && *argp) {
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	    /* Try to free the device */
274*0Sstevel@tonic-gate 	    if (devfree(key, *argp) != 0) {
275*0Sstevel@tonic-gate 		if ((errno == EACCES) || (errno == ENOENT)) {
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 		    /* Can't get at reservation file */
278*0Sstevel@tonic-gate 		    if (rsvtab = _rsvtabpath()) {
279*0Sstevel@tonic-gate 			exitcode = EX_TBLERR;
280*0Sstevel@tonic-gate 			(void) snprintf(msg, sizeof(msg), M_RSVTAB, rsvtab);
281*0Sstevel@tonic-gate 			sev = MM_ERROR;
282*0Sstevel@tonic-gate 		    }
283*0Sstevel@tonic-gate 		    else {
284*0Sstevel@tonic-gate 			exitcode = EX_ERROR;
285*0Sstevel@tonic-gate 			(void) snprintf(msg, sizeof (msg), M_ERROR, errno);
286*0Sstevel@tonic-gate 			sev = MM_HALT;
287*0Sstevel@tonic-gate 		    }
288*0Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, sev, msg);
289*0Sstevel@tonic-gate 		    halt = TRUE;
290*0Sstevel@tonic-gate 		}
291*0Sstevel@tonic-gate 	        else if (errno == EPERM) {
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 		    /* Wrong key */
294*0Sstevel@tonic-gate 		    (void) snprintf(msg, sizeof(msg), M_NOTONKEY, *argp);
295*0Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
296*0Sstevel@tonic-gate 		    exitcode = EX_NOFREE;
297*0Sstevel@tonic-gate 		}
298*0Sstevel@tonic-gate 		else if (errno == EINVAL) {
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 		    /* Device not reserved */
301*0Sstevel@tonic-gate 		    (void) snprintf(msg, sizeof(msg), M_NOTRSVD, *argp);
302*0Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
303*0Sstevel@tonic-gate 		    exitcode = EX_NOFREE;
304*0Sstevel@tonic-gate 		}
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 		else {
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 		    /* Some other strange error occurred */
309*0Sstevel@tonic-gate 		    (void) snprintf(msg, sizeof (msg), M_ERROR, errno);
310*0Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, MM_HALT, msg);
311*0Sstevel@tonic-gate 		    exitcode = EX_ERROR;
312*0Sstevel@tonic-gate 		    halt = TRUE;
313*0Sstevel@tonic-gate 		}
314*0Sstevel@tonic-gate 	    }
315*0Sstevel@tonic-gate 	    argp++;
316*0Sstevel@tonic-gate 	}
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	/* Exit with the appropriate code */
320*0Sstevel@tonic-gate 	return(exitcode);
321*0Sstevel@tonic-gate }
322