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