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