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
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.
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]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <sys/types.h>
34 #include <sys/param.h>
47 * TRUE Boolean TRUE value
48 * FALSE Boolean FALSE value
49 * TOKDELIMS Char string of delimiters for lists
60 #define TOKDELIMS ", \t\n"
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
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
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
97 * Local functions and static data
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
108 * lbl Buffer for the label-component of a message
109 * txt Buffer for the text-component of a message
112 static char ***buildreqlist();
113 static void freereqlist();
114 static int ndevsin();
116 #define stdmsg(r,l,s,m) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,m,MM_NULLACT,MM_NULLTAG)
118 static char lbl
[MM_MXLABELLN
+1];
119 static char txt
[MM_MXTXTLN
+1];
122 * devreserv [key [devlist [devlist [...]]]]
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.
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.
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
150 main(int argc
, char *argv
[])
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 */
177 /* Build a message label */
178 if (p
= strrchr(argv
[0], '/')) p
++;
180 (void) strlcat(strcpy(lbl
, "UX:"), p
, sizeof(lbl
));
184 * Allow only the text component of messages to be written
185 * (this will probably go away in SVR4.1)
188 (void) putenv("MSGVERB=text");
192 * Parse the options from the command line
197 while ((c
= getopt(argc
, argv
, "")) != EOF
) switch(c
) {
203 /* If there's (an obvious) syntax error, write a message and quit */
205 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, M_USAGE
);
209 /* Argument initializations */
210 argcount
= argc
- optind
;
211 argp
= &argv
[optind
];
217 * If euid == 0, write a list of all currently allocated devices.
222 /* Get the list of reserved devices */
223 if (rsvd
= reservdev()) {
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
231 for (plk
= rsvd
; *plk
; plk
++) {
232 if ((i
= fputs((*plk
)->devname
, stdout
)) >= 0) do
233 (void) fputc(' ', stdout
);
235 (void) fprintf(stdout
, "%ld\n", (*plk
)->key
);
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
;
246 (void) sprintf(txt
, M_ERROR
, errno
);
250 stdmsg(MM_NRECOV
, lbl
, sev
, txt
);
261 * Generate a list of the devices allocated on a specific key.
266 /* Extract the key from the command */
267 lkey
= strtol(*argp
, &p
, 10);
268 if (*p
|| (lkey
<= 0) || (lkey
> MAXINT
)) {
270 /* <key> argument invalid */
271 (void) snprintf(txt
, sizeof(txt
), M_INVKEY
, *argp
);
272 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, txt
);
279 /* Get the list of reserved devices ... */
280 if (rsvd
= reservdev()) {
282 /* For each reserved device, write the alias to stdout */
284 for (plk
= rsvd
; *plk
; plk
++) {
285 if ((*plk
)->key
== key
) (void) puts((*plk
)->devname
);
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
;
296 (void) sprintf(txt
, M_ERROR
, errno
);
300 stdmsg(MM_NRECOV
, lbl
, sev
, txt
);
310 * devreserv key devlist [...]
312 * Reserve specific devices
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
;
322 (void) sprintf(txt
, M_ERROR
, errno
);
326 stdmsg(MM_NRECOV
, lbl
, sev
, txt
);
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
);
341 /* Build the device request list from the command arguments */
342 if (reqlist
= buildreqlist(argp
)) {
344 /* Attempt to allocate the devices */
345 if (alloclist
= devreserv(key
, reqlist
)) {
348 * For each allocated device, write the alias to stdout
349 * and free the space allocated for the string.
352 for (pp
= alloclist
; *pp
; pp
++) {
357 /* Free the list of allocated devices */
358 free((char *) alloclist
);
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
;
370 (void) sprintf(txt
, M_ERROR
, errno
);
371 stdmsg(MM_NRECOV
, lbl
, MM_HALT
, txt
);
375 freereqlist(reqlist
);
379 /* Exit with the appropriate code */
384 * char ***buildreqlist(args)
387 * Build the list of lists of devices to request, as described by the
388 * arguments on the command line.
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.)
396 * A pointer to a list containing addresses of lists of pointers to
397 * character-strings, as expected by "devreserv()"
400 * - Assuming that strtok() won't return "". If it does, the
401 * parsing algorithm needs to be enhanced a bit to eliminate
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 */
416 int n
; /* Another counter */
419 /* Count the number of lists we have to work with */
421 for (pp
= args
; *pp
; pp
++) i
++;
424 /* If we can allocate space for the list of lists ... */
425 if (addrlist
= (char ***) malloc(i
*sizeof(char **))) {
427 /* Parse each list, putting that list in the list of lists */
430 for (pp
= args
; noerror
&& *pp
; pp
++) {
431 n
= ndevsin(*pp
, TOKDELIMS
);
432 if (*ppp
= (char **) malloc((n
+1)*sizeof(char *))) {
434 if (*qq
++ = strtok(*pp
, TOKDELIMS
))
435 while (*qq
++ = strtok((char *) NULL
, TOKDELIMS
));
436 } else noerror
= FALSE
;
439 /* If there was an error, clean up the malloc()s we've made */
441 freereqlist(addrlist
);
442 addrlist
= (char ***) NULL
;
446 /* Return ptr to the list of addresses of lists (or NULL if none) */
451 * void freereqlist(list)
454 * This function frees the space allocated to the list of lists
455 * referenced by <list>
458 * char ***list Address of the list of lists
469 for (ppp
= list
; *ppp
; ppp
++) free((char *) *ppp
);
475 * int ndevsin(list, delims)
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.
484 * char *list The <delims>list of tokens to scan
485 * char *delims The list of delimiters that define the list
488 * The number of elements in the list.
491 * - This function does not recognize "null" elements. For example,
492 * a,b,,,,c,,d contains 4 elememts (if delims contains a ',')
496 ndevsin(list
, delims
)
497 char *list
; /* List to scan */
498 char *delims
; /* Delimiters */
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 */
504 count
= 0; /* None seen yet */
505 tokflag
= FALSE
; /* Not in a token */
507 /* Scan the character-string containing the list of tokens */
508 for (p
= list
; *p
; p
++) {
510 /* If a delimiter, we're not in a token */
511 if (strchr(delims
, *p
)) tokflag
= FALSE
;
513 /* Otherwise, if we weren't in a token, we've found one */
520 /* Return the number of elements in the list */