xref: /titanic_50/usr/src/cmd/devmgmt/cmds/devreserv.c (revision 1f0c339e39c0c782f9d4c4f3f11d373e24e1d76f)
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 /*      All Rights Reserved   */
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include	<sys/types.h>
34 #include	<sys/param.h>
35 #include	<stdio.h>
36 #include	<errno.h>
37 #include	<stdlib.h>
38 #include	<string.h>
39 #include	<fmtmsg.h>
40 #include	<devmgmt.h>
41 #include	<devtab.h>
42 #include	<values.h>
43 
44 
45 /*
46  *  Local definitions
47  *	TRUE		Boolean TRUE value
48  *	FALSE		Boolean FALSE value
49  *	TOKDELIMS	Char string of delimiters for lists
50  */
51 
52 #ifndef		TRUE
53 #define		TRUE		('t')
54 #endif
55 
56 #ifndef		FALSE
57 #define		FALSE		0
58 #endif
59 
60 #define		TOKDELIMS	", \t\n"
61 
62 
63 /*
64  *  Exit codes:
65  *	EX_OK		Exit code for all went well
66  *	EX_ERROR	Exit code for something failed
67  *	EX_TABLES	A table couldn't be accessed
68  *	EX_NOALLOC	Exit code for allocation failed
69  */
70 
71 #define		EX_OK		0
72 #define		EX_ERROR	1
73 #define		EX_TABLES	2
74 #define		EX_NOALLOC	3
75 
76 /*
77  *  Messages:
78  *	M_USAGE		Usage error
79  *	M_INVKEY	Invalid key specified
80  *	M_ERROR		Some strange error
81  *	M_UNABLE	A list of devices is unavailable
82  *	M_DEVTAB	Can't access device table (for reading)
83  *	M_RSVTAB	Can't access device reservation table (for r/w)
84  *	M_NODEV		A list of devices is invalid
85  */
86 
87 #define	M_USAGE		"usage: devreserv [key [devicelist [...]]]"
88 #define	M_INVKEY	"Invalid key: %s"
89 #define	M_ERROR		"Internal error, errno=%d"
90 #define	M_UNABLE	"Cannot reserve devices"
91 #define	M_DEVTAB	"Cannot open the device table: %s"
92 #define	M_RSVTAB	"Cannot open the device-reservation table: %s"
93 #define	M_NODEV		M_UNABLE
94 
95 
96 /*
97  *  Local functions and static data
98  *
99  *	buildreqlist()	Builds the list of requested devices for devreserv()
100  *	freereqlist()	Free space allocated to the list of requested devices
101  *	ndevsin()	Get number of elements in a list
102  *	stdmsg(r,l,s,m)	Standard message generation
103  *			r	Recoverability flag
104  *			l	Label
105  *			s	Severity
106  *			m	Message
107  *
108  *	lbl		Buffer for the label-component of a message
109  *	txt		Buffer for the text-component of a message
110  */
111 
112 static char  ***buildreqlist();
113 static void	freereqlist();
114 static int	ndevsin();
115 
116 #define	stdmsg(r,l,s,m)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,m,MM_NULLACT,MM_NULLTAG)
117 
118 static	char	lbl[MM_MXLABELLN+1];
119 static	char	txt[MM_MXTXTLN+1];
120 
121 /*
122  *  devreserv [key [devlist [devlist [...]]]]
123  *
124  *	This command reserves sets of devices known to the OA&M device
125  *	management system.  It reserves a device from each of the device
126  *	lists presented to it, reserving them on the key (<key>).  If no
127  *	device-lists are provided, the command lists those devices reserved
128  *	on the given key (<key>).  If no key (<key>) is provided, the
129  *	command lists all devices currently reserved.
130  *
131  *  Options:  None
132  *
133  *  Arguments:
134  *	key		Key to lock the devices on
135  *	devlist		A comma-, space-, or tab-list containing devices
136  *			(pathnames or aliases).  For typical shells, space-
137  *			and tab-lists should be quoted or the separator should
138  *			be somehow escaped.
139  *
140  *  Command Values:
141  *	EX_OK		0	Device(s) successfully allocated
142  *	EX_ERROR	1	A syntax or other error occurred
143  *	EX_TABLES	2	Either the device-table or the device-
144  *				reservation table couldn't be opened as needed
145  *	EX_NOALLOC	3	The device-reservation request couldn't be
146  *				fulfilled.
147  */
148 
149 int
150 main(int argc, char *argv[])
151 {
152 
153 	/* Automatics */
154 	char		     ***reqlist;	/* * to list of lists */
155 	char		      **argp;		/* Ptr to current argument */
156 	char		      **alloclist;	/* List of allocated devices */
157 	char		      **pp;		/* Temp ptr to char ptrs */
158 	struct reservdev      **rsvd;		/* Ptr to list of rsvd devs */
159 	struct reservdev      **plk;		/* Running ptr to locks */
160 	char		       *p;		/* Temp char ptr */
161 	char		       *devtab;		/* Device table pathname */
162 	char		       *rsvtab;		/* Dev-rsv tbl pathname */
163 	int			argcount;	/* Number of args on cmd */
164 	long			lkey;		/* Key for locking (long) */
165 	int			key;		/* Key for locking */
166 	int			exitcode;	/* Value to return */
167 	int			sev;		/* Message severity */
168 	int			syntaxerr;	/* Flag, TRUE if syntax error */
169 	int			c;		/* Option character */
170 	int			i;		/* Temp counter */
171 
172 
173 	/*
174 	 * Initializations
175 	 */
176 
177 	/* Build a message label */
178 	if (p = strrchr(argv[0], '/')) p++;
179 	else p = argv[0];
180 	(void) strlcat(strcpy(lbl, "UX:"), p, sizeof(lbl));
181 
182 
183 	/*
184 	 * Allow only the text component of messages to be written
185 	 * (this will probably go away in SVR4.1)
186 	 */
187 
188 	(void) putenv("MSGVERB=text");
189 
190 
191 	/*
192 	 * Parse the options from the command line
193 	 */
194 
195 	opterr = 0;
196 	syntaxerr = FALSE;
197 	while ((c = getopt(argc, argv, "")) != EOF) switch(c) {
198 	default:
199 	    syntaxerr = FALSE;
200 	    break;
201 	}
202 
203 	/* If there's (an obvious) syntax error, write a message and quit */
204 	if (syntaxerr) {
205 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
206 	    exit(EX_ERROR);
207 	}
208 
209 	/* Argument initializations */
210 	argcount = argc - optind;
211 	argp = &argv[optind];
212 
213 
214 	/*
215 	 *  devreserv
216 	 *
217 	 *  	If euid == 0, write a list of all currently allocated devices.
218 	 */
219 
220 	if (argcount == 0) {
221 
222 	    /* Get the list of reserved devices */
223 	    if (rsvd = reservdev()) {
224 
225 		/* Write the list of reserved devices with the key
226 		 * that the device was locked on.  The key should go
227 		 * in column 16, but separate it from the alias with at
228 		 * least one space */
229 
230 		exitcode = EX_OK;
231 		for (plk = rsvd ; *plk ; plk++) {
232 		    if ((i = fputs((*plk)->devname, stdout)) >= 0) do
233 			(void) fputc(' ', stdout);
234 		    while (++i < 16);
235 		    (void) fprintf(stdout, "%ld\n", (*plk)->key);
236 		}
237 
238 	    } else {
239 
240 		/* Problems getting the list of reserved devices */
241 		if (((errno == EINVAL) || (errno == EACCES)) && (rsvtab = _rsvtabpath())) {
242 		    (void) snprintf(txt, sizeof(txt), M_RSVTAB, rsvtab);
243 		    exitcode = EX_TABLES;
244 		    sev = MM_ERROR;
245 		} else {
246 		    (void) sprintf(txt, M_ERROR, errno);
247 		    exitcode = EX_ERROR;
248 		    sev = MM_HALT;
249 		}
250 		stdmsg(MM_NRECOV, lbl, sev, txt);
251 	    }
252 
253 	    /* Finished */
254 	    exit(exitcode);
255 	}
256 
257 
258 	/*
259 	 *  devreserv key
260 	 *
261 	 *  	Generate a list of the devices allocated on a specific key.
262 	 */
263 
264 	if (argcount == 1) {
265 
266 	    /* Extract the key from the command */
267 	    lkey = strtol(*argp, &p, 10);
268 	    if (*p || (lkey <= 0) || (lkey > MAXINT)) {
269 
270 		/* <key> argument invalid */
271 		(void) snprintf(txt, sizeof(txt), M_INVKEY, *argp);
272 		stdmsg(MM_NRECOV, lbl, MM_ERROR, txt);
273 		exitcode = EX_ERROR;
274 
275 	    } else {
276 
277 		key = (int) lkey;
278 
279 		/* Get the list of reserved devices ... */
280 		if (rsvd = reservdev()) {
281 
282 		    /* For each reserved device, write the alias to stdout */
283 		    exitcode = EX_OK;
284 		    for (plk = rsvd ; *plk ; plk++) {
285 			if ((*plk)->key == key) (void) puts((*plk)->devname);
286 		    }
287 
288 		} else {
289 
290 		    /* Problems getting the list of reserved devices */
291 		    if (((errno == EINVAL) || (errno == EACCES)) && (rsvtab = _rsvtabpath())) {
292 			(void) snprintf(txt, sizeof(txt), M_RSVTAB, rsvtab);
293 			exitcode = EX_TABLES;
294 			sev = MM_ERROR;
295 		    } else {
296 			(void) sprintf(txt, M_ERROR, errno);
297 			exitcode = EX_ERROR;
298 			sev = MM_HALT;
299 		    }
300 		    stdmsg(MM_NRECOV, lbl, sev, txt);
301 		}
302 	    }
303 
304 	    /* Finished */
305 	    exit(exitcode);
306 	}
307 
308 
309 	/*
310 	 *  devreserv key devlist [...]
311 	 *
312 	 *	Reserve specific devices
313 	 */
314 
315 	/* Open the device file (if there's one to be opened) */
316 	if (!_opendevtab("r")) {
317 	    if (devtab = _devtabpath()) {
318 		(void) snprintf(txt, sizeof(txt), M_DEVTAB, devtab);
319 		exitcode = EX_TABLES;
320 		sev = MM_ERROR;
321 	    } else {
322 		(void) sprintf(txt, M_ERROR, errno);
323 		exitcode = EX_ERROR;
324 		sev = MM_HALT;
325 	    }
326 	    stdmsg(MM_NRECOV, lbl, sev, txt);
327 	    exit(exitcode);
328 	}
329 
330 	/* Extract the key from the command */
331 	lkey = strtol(*argp, &p, 10);
332 	if (*p || (lkey <= 0) || (lkey > MAXINT)) {
333 	    (void) snprintf(txt, sizeof(txt), M_INVKEY, *argp);
334 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, txt);
335 	    exit(EX_ERROR);
336 	}
337 
338 	key = (int) lkey;
339 	argp++;
340 
341 	/* Build the device request list from the command arguments */
342 	if (reqlist = buildreqlist(argp)) {
343 
344 	    /* Attempt to allocate the devices */
345 	    if (alloclist = devreserv(key, reqlist)) {
346 
347 		/*
348 		 * For each allocated device, write the alias to stdout
349 		 * and free the space allocated for the string.
350 		 */
351 
352 		for (pp = alloclist; *pp; pp++) {
353 		    (void) puts(*pp);
354 		    free(*pp);
355 		}
356 
357 		/* Free the list of allocated devices */
358 		free((char *) alloclist);
359 		exitcode = EX_OK;
360 	    }
361 	    else {
362 		/* Device allocation failed */
363 		if (errno == EAGAIN) {
364 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_UNABLE);
365 		    exitcode = EX_NOALLOC;
366 		} else if (errno == ENODEV) {
367 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_NODEV);
368 		    exitcode = EX_NOALLOC;
369 		} else {
370 		    (void) sprintf(txt, M_ERROR, errno);
371 		    stdmsg(MM_NRECOV, lbl, MM_HALT, txt);
372 		    exitcode = EX_ERROR;
373 		}
374 	    }
375 	    freereqlist(reqlist);
376 	}
377 
378 
379 	/* Exit with the appropriate code */
380 	return(exitcode);
381 }
382 
383 /*
384  * char ***buildreqlist(args)
385  *	char   **args
386  *
387  *	Build the list of lists of devices to request, as described by the
388  *	arguments on the command line.
389  *
390  *  Arguments:
391  *	char **args	The address of the first argument of the list of
392  *			lists of devices to allocate.   (This list is
393  *			terminated with a (char *) NULL.)
394  *
395  *  Returns:  char ***
396  *	A pointer to a list containing addresses of lists of pointers to
397  *	character-strings, as expected by "devreserv()"
398  *
399  *  Notes:
400  *    -	Assuming that strtok() won't return "".  If it does, the
401  *	parsing algorithm needs to be enhanced a bit to eliminate
402  *	these cases.
403  */
404 
405 static char ***
406 buildreqlist(args)
407 	char  **args;
408 {
409 	/* Local automatic data */
410 	char	     ***addrlist;	/* Addr of space for ptrs to lists */
411 	char	     ***ppp;		/* Pointer to pointers to pointers */
412 	char	      **pp;		/* Pointer to pointers */
413 	char	      **qq;		/* Pointer to pointers */
414 	int		noerror;	/* FLAG, TRUE if all's well */
415 	int		i;		/* Counter */
416 	int		n;		/* Another counter */
417 
418 
419 	/* Count the number of lists we have to work with */
420 	i = 1;
421 	for (pp = args ; *pp ; pp++) i++;
422 
423 
424 	/* If we can allocate space for the list of lists ... */
425 	if (addrlist = (char ***) malloc(i*sizeof(char **))) {
426 
427 	    /* Parse each list, putting that list in the list of lists */
428 	    ppp = addrlist;
429 	    noerror = TRUE;
430 	    for (pp = args ; noerror && *pp ; pp++) {
431 		n = ndevsin(*pp, TOKDELIMS);
432 		if (*ppp = (char **) malloc((n+1)*sizeof(char *))) {
433 		     qq = *ppp++;
434 		     if (*qq++ = strtok(*pp, TOKDELIMS))
435 			 while (*qq++ = strtok((char *) NULL, TOKDELIMS));
436 		} else noerror = FALSE;
437 	    }
438 
439 	    /* If there was an error, clean up the malloc()s we've made */
440 	    if (!noerror) {
441 		freereqlist(addrlist);
442 		addrlist = (char ***) NULL;
443 	    }
444 	}
445 
446 	/* Return ptr to the list of addresses of lists (or NULL if none) */
447 	return(addrlist);
448 }
449 
450 /*
451  *  void freereqlist(list)
452  *	char ***list
453  *
454  *	This function frees the space allocated to the list of lists
455  *	referenced by <list>
456  *
457  *  Arguments:
458  *	char ***list	Address of the list of lists
459  *
460  *  Returns:  void
461  */
462 
463 static void
464 freereqlist(list)
465 	char ***list;
466 {
467 	char ***ppp;
468 	if (list) {
469 	    for (ppp = list ; *ppp ; ppp++) free((char *) *ppp);
470 	    free((char *) list);
471 	}
472 }
473 
474 /*
475  * int ndevsin(list, delims)
476  *	char   *list
477  *	char   *delims
478  *
479  *	This function determines how many tokens are in the list <list>.
480  *	The tokens are delimited by fields of characters in the string
481  *	<delims>.  It returns the number of tokens in the list.
482  *
483  *  Arguments:
484  *	char *list	The <delims>list of tokens to scan
485  *	char *delims	The list of delimiters that define the list
486  *
487  *  Returns: int
488  *	The number of elements in the list.
489  *
490  *  Notes:
491  *    -	This function does not recognize "null" elements.  For example,
492  *	a,b,,,,c,,d contains 4 elememts (if delims contains a ',')
493  */
494 
495 static int
496 ndevsin(list, delims)
497 	char   *list;			/* List to scan */
498 	char   *delims;			/* Delimiters */
499 {
500 	char   *p;			/* Running character pointer */
501 	int	count;			/* Number of tokens seen so far */
502 	int	tokflag;		/* TRUE if we're parsing a token */
503 
504 	count = 0;			/* None seen yet */
505 	tokflag = FALSE;		/* Not in a token */
506 
507 	/* Scan the character-string containing the list of tokens */
508 	for (p = list ; *p ; p++) {
509 
510 	    /* If a delimiter, we're not in a token */
511 	    if (strchr(delims, *p)) tokflag = FALSE;
512 
513 	    /* Otherwise, if we weren't in a token, we've found one */
514 	    else if (!tokflag) {
515 		tokflag = TRUE;
516 		count++;
517 	    }
518 	}
519 
520 	/* Return the number of elements in the list */
521 	return(count);
522 }
523