xref: /titanic_50/usr/src/cmd/devmgmt/cmds/devreserv.c (revision 8461248208fabd3a8230615f8615e5bf1b4dcdcb)
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 2002-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 /*      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 main(argc, argv)
150 	int		argc;		/* Arg count */
151 	char	       *argv[];		/* Arg vector */
152 {
153 
154 	/* Automatics */
155 	char		     ***reqlist;	/* * to list of lists */
156 	char		      **argp;		/* Ptr to current argument */
157 	char		      **alloclist;	/* List of allocated devices */
158 	char		      **pp;		/* Temp ptr to char ptrs */
159 	struct reservdev      **rsvd;		/* Ptr to list of rsvd devs */
160 	struct reservdev      **plk;		/* Running ptr to locks */
161 	char		       *p;		/* Temp char ptr */
162 	char		       *devtab;		/* Device table pathname */
163 	char		       *rsvtab;		/* Dev-rsv tbl pathname */
164 	int			argcount;	/* Number of args on cmd */
165 	long			lkey;		/* Key for locking (long) */
166 	int			key;		/* Key for locking */
167 	int			exitcode;	/* Value to return */
168 	int			sev;		/* Message severity */
169 	int			syntaxerr;	/* Flag, TRUE if syntax error */
170 	int			c;		/* Option character */
171 	int			i;		/* Temp counter */
172 
173 
174 	/*
175 	 * Initializations
176 	 */
177 
178 	/* Build a message label */
179 	if (p = strrchr(argv[0], '/')) p++;
180 	else p = argv[0];
181 	(void) strlcat(strcpy(lbl, "UX:"), p, sizeof(lbl));
182 
183 
184 	/*
185 	 * Allow only the text component of messages to be written
186 	 * (this will probably go away in SVR4.1)
187 	 */
188 
189 	(void) putenv("MSGVERB=text");
190 
191 
192 	/*
193 	 * Parse the options from the command line
194 	 */
195 
196 	opterr = 0;
197 	syntaxerr = FALSE;
198 	while ((c = getopt(argc, argv, "")) != EOF) switch(c) {
199 	default:
200 	    syntaxerr = FALSE;
201 	    break;
202 	}
203 
204 	/* If there's (an obvious) syntax error, write a message and quit */
205 	if (syntaxerr) {
206 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
207 	    exit(EX_ERROR);
208 	}
209 
210 	/* Argument initializations */
211 	argcount = argc - optind;
212 	argp = &argv[optind];
213 
214 
215 	/*
216 	 *  devreserv
217 	 *
218 	 *  	If euid == 0, write a list of all currently allocated devices.
219 	 */
220 
221 	if (argcount == 0) {
222 
223 	    /* Get the list of reserved devices */
224 	    if (rsvd = reservdev()) {
225 
226 		/* Write the list of reserved devices with the key
227 		 * that the device was locked on.  The key should go
228 		 * in column 16, but separate it from the alias with at
229 		 * least one space */
230 
231 		exitcode = EX_OK;
232 		for (plk = rsvd ; *plk ; plk++) {
233 		    if ((i = fputs((*plk)->devname, stdout)) >= 0) do
234 			(void) fputc(' ', stdout);
235 		    while (++i < 16);
236 		    (void) fprintf(stdout, "%ld\n", (*plk)->key);
237 		}
238 
239 	    } else {
240 
241 		/* Problems getting the list of reserved devices */
242 		if (((errno == EINVAL) || (errno == EACCES)) && (rsvtab = _rsvtabpath())) {
243 		    (void) snprintf(txt, sizeof(txt), M_RSVTAB, rsvtab);
244 		    exitcode = EX_TABLES;
245 		    sev = MM_ERROR;
246 		} else {
247 		    (void) sprintf(txt, M_ERROR, errno);
248 		    exitcode = EX_ERROR;
249 		    sev = MM_HALT;
250 		}
251 		stdmsg(MM_NRECOV, lbl, sev, txt);
252 	    }
253 
254 	    /* Finished */
255 	    exit(exitcode);
256 	}
257 
258 
259 	/*
260 	 *  devreserv key
261 	 *
262 	 *  	Generate a list of the devices allocated on a specific key.
263 	 */
264 
265 	if (argcount == 1) {
266 
267 	    /* Extract the key from the command */
268 	    lkey = strtol(*argp, &p, 10);
269 	    if (*p || (lkey <= 0) || (lkey > MAXINT)) {
270 
271 		/* <key> argument invalid */
272 		(void) snprintf(txt, sizeof(txt), M_INVKEY, *argp);
273 		stdmsg(MM_NRECOV, lbl, MM_ERROR, txt);
274 		exitcode = EX_ERROR;
275 
276 	    } else {
277 
278 		key = (int) lkey;
279 
280 		/* Get the list of reserved devices ... */
281 		if (rsvd = reservdev()) {
282 
283 		    /* For each reserved device, write the alias to stdout */
284 		    exitcode = EX_OK;
285 		    for (plk = rsvd ; *plk ; plk++) {
286 			if ((*plk)->key == key) (void) puts((*plk)->devname);
287 		    }
288 
289 		} else {
290 
291 		    /* Problems getting the list of reserved devices */
292 		    if (((errno == EINVAL) || (errno == EACCES)) && (rsvtab = _rsvtabpath())) {
293 			(void) snprintf(txt, sizeof(txt), M_RSVTAB, rsvtab);
294 			exitcode = EX_TABLES;
295 			sev = MM_ERROR;
296 		    } else {
297 			(void) sprintf(txt, M_ERROR, errno);
298 			exitcode = EX_ERROR;
299 			sev = MM_HALT;
300 		    }
301 		    stdmsg(MM_NRECOV, lbl, sev, txt);
302 		}
303 	    }
304 
305 	    /* Finished */
306 	    exit(exitcode);
307 	}
308 
309 
310 	/*
311 	 *  devreserv key devlist [...]
312 	 *
313 	 *	Reserve specific devices
314 	 */
315 
316 	/* Open the device file (if there's one to be opened) */
317 	if (!_opendevtab("r")) {
318 	    if (devtab = _devtabpath()) {
319 		(void) snprintf(txt, sizeof(txt), M_DEVTAB, devtab);
320 		exitcode = EX_TABLES;
321 		sev = MM_ERROR;
322 	    } else {
323 		(void) sprintf(txt, M_ERROR, errno);
324 		exitcode = EX_ERROR;
325 		sev = MM_HALT;
326 	    }
327 	    stdmsg(MM_NRECOV, lbl, sev, txt);
328 	    exit(exitcode);
329 	}
330 
331 	/* Extract the key from the command */
332 	lkey = strtol(*argp, &p, 10);
333 	if (*p || (lkey <= 0) || (lkey > MAXINT)) {
334 	    (void) snprintf(txt, sizeof(txt), M_INVKEY, *argp);
335 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, txt);
336 	    exit(EX_ERROR);
337 	}
338 
339 	key = (int) lkey;
340 	argp++;
341 
342 	/* Build the device request list from the command arguments */
343 	if (reqlist = buildreqlist(argp)) {
344 
345 	    /* Attempt to allocate the devices */
346 	    if (alloclist = devreserv(key, reqlist)) {
347 
348 		/*
349 		 * For each allocated device, write the alias to stdout
350 		 * and free the space allocated for the string.
351 		 */
352 
353 		for (pp = alloclist; *pp; pp++) {
354 		    (void) puts(*pp);
355 		    free(*pp);
356 		}
357 
358 		/* Free the list of allocated devices */
359 		free((char *) alloclist);
360 		exitcode = EX_OK;
361 	    }
362 	    else {
363 		/* Device allocation failed */
364 		if (errno == EAGAIN) {
365 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_UNABLE);
366 		    exitcode = EX_NOALLOC;
367 		} else if (errno == ENODEV) {
368 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_NODEV);
369 		    exitcode = EX_NOALLOC;
370 		} else {
371 		    (void) sprintf(txt, M_ERROR, errno);
372 		    stdmsg(MM_NRECOV, lbl, MM_HALT, txt);
373 		    exitcode = EX_ERROR;
374 		}
375 	    }
376 	    freereqlist(reqlist);
377 	}
378 
379 
380 	/* Exit with the appropriate code */
381 	return(exitcode);
382 }
383 
384 /*
385  * char ***buildreqlist(args)
386  *	char   **args
387  *
388  *	Build the list of lists of devices to request, as described by the
389  *	arguments on the command line.
390  *
391  *  Arguments:
392  *	char **args	The address of the first argument of the list of
393  *			lists of devices to allocate.   (This list is
394  *			terminated with a (char *) NULL.)
395  *
396  *  Returns:  char ***
397  *	A pointer to a list containing addresses of lists of pointers to
398  *	character-strings, as expected by "devreserv()"
399  *
400  *  Notes:
401  *    -	Assuming that strtok() won't return "".  If it does, the
402  *	parsing algorithm needs to be enhanced a bit to eliminate
403  *	these cases.
404  */
405 
406 static char ***
407 buildreqlist(args)
408 	char  **args;
409 {
410 	/* Local automatic data */
411 	char	     ***addrlist;	/* Addr of space for ptrs to lists */
412 	char	     ***ppp;		/* Pointer to pointers to pointers */
413 	char	      **pp;		/* Pointer to pointers */
414 	char	      **qq;		/* Pointer to pointers */
415 	int		noerror;	/* FLAG, TRUE if all's well */
416 	int		i;		/* Counter */
417 	int		n;		/* Another counter */
418 
419 
420 	/* Count the number of lists we have to work with */
421 	i = 1;
422 	for (pp = args ; *pp ; pp++) i++;
423 
424 
425 	/* If we can allocate space for the list of lists ... */
426 	if (addrlist = (char ***) malloc(i*sizeof(char **))) {
427 
428 	    /* Parse each list, putting that list in the list of lists */
429 	    ppp = addrlist;
430 	    noerror = TRUE;
431 	    for (pp = args ; noerror && *pp ; pp++) {
432 		n = ndevsin(*pp, TOKDELIMS);
433 		if (*ppp = (char **) malloc((n+1)*sizeof(char *))) {
434 		     qq = *ppp++;
435 		     if (*qq++ = strtok(*pp, TOKDELIMS))
436 			 while (*qq++ = strtok((char *) NULL, TOKDELIMS));
437 		} else noerror = FALSE;
438 	    }
439 
440 	    /* If there was an error, clean up the malloc()s we've made */
441 	    if (!noerror) {
442 		freereqlist(addrlist);
443 		addrlist = (char ***) NULL;
444 	    }
445 	}
446 
447 	/* Return ptr to the list of addresses of lists (or NULL if none) */
448 	return(addrlist);
449 }
450 
451 /*
452  *  void freereqlist(list)
453  *	char ***list
454  *
455  *	This function frees the space allocated to the list of lists
456  *	referenced by <list>
457  *
458  *  Arguments:
459  *	char ***list	Address of the list of lists
460  *
461  *  Returns:  void
462  */
463 
464 static void
465 freereqlist(list)
466 	char ***list;
467 {
468 	char ***ppp;
469 	if (list) {
470 	    for (ppp = list ; *ppp ; ppp++) free((char *) *ppp);
471 	    free((char *) list);
472 	}
473 }
474 
475 /*
476  * int ndevsin(list, delims)
477  *	char   *list
478  *	char   *delims
479  *
480  *	This function determines how many tokens are in the list <list>.
481  *	The tokens are delimited by fields of characters in the string
482  *	<delims>.  It returns the number of tokens in the list.
483  *
484  *  Arguments:
485  *	char *list	The <delims>list of tokens to scan
486  *	char *delims	The list of delimiters that define the list
487  *
488  *  Returns: int
489  *	The number of elements in the list.
490  *
491  *  Notes:
492  *    -	This function does not recognize "null" elements.  For example,
493  *	a,b,,,,c,,d contains 4 elememts (if delims contains a ',')
494  */
495 
496 static int
497 ndevsin(list, delims)
498 	char   *list;			/* List to scan */
499 	char   *delims;			/* Delimiters */
500 {
501 	char   *p;			/* Running character pointer */
502 	int	count;			/* Number of tokens seen so far */
503 	int	tokflag;		/* TRUE if we're parsing a token */
504 
505 	count = 0;			/* None seen yet */
506 	tokflag = FALSE;		/* Not in a token */
507 
508 	/* Scan the character-string containing the list of tokens */
509 	for (p = list ; *p ; p++) {
510 
511 	    /* If a delimiter, we're not in a token */
512 	    if (strchr(delims, *p)) tokflag = FALSE;
513 
514 	    /* Otherwise, if we weren't in a token, we've found one */
515 	    else if (!tokflag) {
516 		tokflag = TRUE;
517 		count++;
518 	    }
519 	}
520 
521 	/* Return the number of elements in the list */
522 	return(count);
523 }
524