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 /* 34*7c478bd9Sstevel@tonic-gate * listdgrp.c 35*7c478bd9Sstevel@tonic-gate * 36*7c478bd9Sstevel@tonic-gate * Contains 37*7c478bd9Sstevel@tonic-gate * listdgrp Writes on the standard output stream a list of devices 38*7c478bd9Sstevel@tonic-gate * that belong to the specified device group 39*7c478bd9Sstevel@tonic-gate */ 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 42*7c478bd9Sstevel@tonic-gate #include <stdio.h> 43*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 44*7c478bd9Sstevel@tonic-gate #include <string.h> 45*7c478bd9Sstevel@tonic-gate #include <errno.h> 46*7c478bd9Sstevel@tonic-gate #include <devmgmt.h> 47*7c478bd9Sstevel@tonic-gate #include <devtab.h> 48*7c478bd9Sstevel@tonic-gate #include <fmtmsg.h> 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate /* 52*7c478bd9Sstevel@tonic-gate * Local Definitions 53*7c478bd9Sstevel@tonic-gate * TRUE Boolean TRUE value (if not already defined) 54*7c478bd9Sstevel@tonic-gate * FALSE Boolean not-TRUE value (if not already defined) 55*7c478bd9Sstevel@tonic-gate */ 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate #ifndef TRUE 58*7c478bd9Sstevel@tonic-gate #define TRUE ('t') 59*7c478bd9Sstevel@tonic-gate #endif 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate #ifndef FALSE 62*7c478bd9Sstevel@tonic-gate #define FALSE (0) 63*7c478bd9Sstevel@tonic-gate #endif 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate /* 66*7c478bd9Sstevel@tonic-gate * Messages: 67*7c478bd9Sstevel@tonic-gate * M_USAGE Command usage error 68*7c478bd9Sstevel@tonic-gate * M_NODGRP Device group not found 69*7c478bd9Sstevel@tonic-gate * M_DGRPTAB Device-group table not found 70*7c478bd9Sstevel@tonic-gate * M_ERROR Internal error 71*7c478bd9Sstevel@tonic-gate */ 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate #define M_USAGE "usage: listdgrp dgroup" 74*7c478bd9Sstevel@tonic-gate #define M_NODGRP "Device group not found: %s" 75*7c478bd9Sstevel@tonic-gate #define M_DGRPTAB "Cannot open device-group table: %s" 76*7c478bd9Sstevel@tonic-gate #define M_ERROR "Internal error, errno=%d" 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate /* 80*7c478bd9Sstevel@tonic-gate * Exit codes 81*7c478bd9Sstevel@tonic-gate * EX_OK Exiting okay, no problem 82*7c478bd9Sstevel@tonic-gate * EX_ERROR Some problem with the command 83*7c478bd9Sstevel@tonic-gate * EX_NODGRPTAB Device group table could not be opened 84*7c478bd9Sstevel@tonic-gate * EX_NODGROUP Device group couldn't be found 85*7c478bd9Sstevel@tonic-gate */ 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate #define EX_OK 0 88*7c478bd9Sstevel@tonic-gate #define EX_ERROR 1 89*7c478bd9Sstevel@tonic-gate #define EX_NODGRPTAB 2 90*7c478bd9Sstevel@tonic-gate #define EX_NODGROUP 3 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate /* 94*7c478bd9Sstevel@tonic-gate * Macros 95*7c478bd9Sstevel@tonic-gate * stdmsg(r,l,s,t) Write a message in standard format 96*7c478bd9Sstevel@tonic-gate * r Recoverability flag 97*7c478bd9Sstevel@tonic-gate * l Label 98*7c478bd9Sstevel@tonic-gate * s Severity 99*7c478bd9Sstevel@tonic-gate * t Tag 100*7c478bd9Sstevel@tonic-gate */ 101*7c478bd9Sstevel@tonic-gate 102*7c478bd9Sstevel@tonic-gate #define stdmsg(r,l,s,t) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG) 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate /* 105*7c478bd9Sstevel@tonic-gate * Global Variables 106*7c478bd9Sstevel@tonic-gate */ 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate /* 110*7c478bd9Sstevel@tonic-gate * Static Variables 111*7c478bd9Sstevel@tonic-gate * 112*7c478bd9Sstevel@tonic-gate * lbl Buffer for the message label 113*7c478bd9Sstevel@tonic-gate */ 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate static char lbl[MM_MXLABELLN+1]; 116*7c478bd9Sstevel@tonic-gate static char msg[MM_MXTXTLN+1]; 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate /* 119*7c478bd9Sstevel@tonic-gate * listdgrp <dgroup> 120*7c478bd9Sstevel@tonic-gate * 121*7c478bd9Sstevel@tonic-gate * List the devices that belong to the device group <dgroup>. 122*7c478bd9Sstevel@tonic-gate * It writes the list to the standard output file (stdout) 123*7c478bd9Sstevel@tonic-gate * in a new-line list. 124*7c478bd9Sstevel@tonic-gate * 125*7c478bd9Sstevel@tonic-gate * Returns: 126*7c478bd9Sstevel@tonic-gate * 0 Ok 127*7c478bd9Sstevel@tonic-gate * 1 Syntax or other error 128*7c478bd9Sstevel@tonic-gate * 2 Device table can't be opened 129*7c478bd9Sstevel@tonic-gate * 3 Device group doesn't exist 130*7c478bd9Sstevel@tonic-gate */ 131*7c478bd9Sstevel@tonic-gate 132*7c478bd9Sstevel@tonic-gate main(argc, argv) 133*7c478bd9Sstevel@tonic-gate int argc; /* Number of items in command */ 134*7c478bd9Sstevel@tonic-gate char **argv; /* List of pointers to the arguments */ 135*7c478bd9Sstevel@tonic-gate { 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate /* 138*7c478bd9Sstevel@tonic-gate * Automatic data 139*7c478bd9Sstevel@tonic-gate */ 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate char **devices; /* List of devices in the group */ 142*7c478bd9Sstevel@tonic-gate char **pp; /* Running pointer to device names */ 143*7c478bd9Sstevel@tonic-gate char *cmdname; /* Simple command name */ 144*7c478bd9Sstevel@tonic-gate char *dgrptab; /* The device-group table name */ 145*7c478bd9Sstevel@tonic-gate char *dgroup; /* Device group to list */ 146*7c478bd9Sstevel@tonic-gate int exitcode; /* Value to return to the caller */ 147*7c478bd9Sstevel@tonic-gate int sev; /* Message severity */ 148*7c478bd9Sstevel@tonic-gate int optchar; /* Option char (from getopt()) */ 149*7c478bd9Sstevel@tonic-gate int usageerr; /* TRUE if syntax error on command */ 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate /* Build the message label from the (simple) command name */ 153*7c478bd9Sstevel@tonic-gate if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++; 154*7c478bd9Sstevel@tonic-gate else cmdname = argv[0]; 155*7c478bd9Sstevel@tonic-gate (void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl)); 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate /* Only write the text component of a message (this goes away in SVR4.1) */ 158*7c478bd9Sstevel@tonic-gate (void) putenv("MSGVERB=text"); 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate /* 161*7c478bd9Sstevel@tonic-gate * Parse the command line: 162*7c478bd9Sstevel@tonic-gate * - Options 163*7c478bd9Sstevel@tonic-gate * - Device group to display 164*7c478bd9Sstevel@tonic-gate */ 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate /* 167*7c478bd9Sstevel@tonic-gate * Extract options from the command line 168*7c478bd9Sstevel@tonic-gate */ 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate /* Initializations */ 171*7c478bd9Sstevel@tonic-gate usageerr = FALSE; /* No errors on the command line (yet) */ 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate /* 174*7c478bd9Sstevel@tonic-gate * Loop until all of the command line options have been parced 175*7c478bd9Sstevel@tonic-gate * (and don't let getopt() write messages) 176*7c478bd9Sstevel@tonic-gate */ 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate opterr = FALSE; 179*7c478bd9Sstevel@tonic-gate while ((optchar = getopt(argc, argv, "")) != EOF) switch (optchar) { 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate /* Default case -- command usage error */ 182*7c478bd9Sstevel@tonic-gate case '?': 183*7c478bd9Sstevel@tonic-gate default: 184*7c478bd9Sstevel@tonic-gate usageerr = TRUE; 185*7c478bd9Sstevel@tonic-gate break; 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate /* Check the argument count and extract the device group name */ 189*7c478bd9Sstevel@tonic-gate if (usageerr || (optind != argc-1)) usageerr = TRUE; 190*7c478bd9Sstevel@tonic-gate else dgroup = argv[optind]; 191*7c478bd9Sstevel@tonic-gate 192*7c478bd9Sstevel@tonic-gate /* If there is a usage error, write an appropriate message and exit */ 193*7c478bd9Sstevel@tonic-gate if (usageerr) { 194*7c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE); 195*7c478bd9Sstevel@tonic-gate exit(EX_ERROR); 196*7c478bd9Sstevel@tonic-gate } 197*7c478bd9Sstevel@tonic-gate 198*7c478bd9Sstevel@tonic-gate /* Open the device-group file (if there's one to be opened) */ 199*7c478bd9Sstevel@tonic-gate if (!_opendgrptab("r")) { 200*7c478bd9Sstevel@tonic-gate if (dgrptab = _dgrptabpath()) { 201*7c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), M_DGRPTAB, dgrptab); 202*7c478bd9Sstevel@tonic-gate exitcode = EX_NODGRPTAB; 203*7c478bd9Sstevel@tonic-gate sev = MM_ERROR; 204*7c478bd9Sstevel@tonic-gate } else { 205*7c478bd9Sstevel@tonic-gate (void) sprintf(msg, M_ERROR, errno); 206*7c478bd9Sstevel@tonic-gate exitcode = EX_ERROR; 207*7c478bd9Sstevel@tonic-gate sev = MM_HALT; 208*7c478bd9Sstevel@tonic-gate } 209*7c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, sev, msg); 210*7c478bd9Sstevel@tonic-gate exit(exitcode); 211*7c478bd9Sstevel@tonic-gate } 212*7c478bd9Sstevel@tonic-gate 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate /* 215*7c478bd9Sstevel@tonic-gate * Get the list of devices associated with the device group. 216*7c478bd9Sstevel@tonic-gate * If we get one, write the list to the standard output. 217*7c478bd9Sstevel@tonic-gate * Otherwise, write an appropriate error message 218*7c478bd9Sstevel@tonic-gate */ 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate exitcode = EX_OK; 221*7c478bd9Sstevel@tonic-gate if (devices = listdgrp(dgroup)) { 222*7c478bd9Sstevel@tonic-gate for (pp = devices ; *pp ; pp++) (void) puts(*pp); 223*7c478bd9Sstevel@tonic-gate } 224*7c478bd9Sstevel@tonic-gate else { 225*7c478bd9Sstevel@tonic-gate if (errno == EINVAL) { 226*7c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), M_NODGRP, dgroup); 227*7c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg); 228*7c478bd9Sstevel@tonic-gate exitcode = EX_NODGROUP; 229*7c478bd9Sstevel@tonic-gate } 230*7c478bd9Sstevel@tonic-gate else { 231*7c478bd9Sstevel@tonic-gate (void) sprintf(msg, M_ERROR, errno); 232*7c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_HALT, msg); 233*7c478bd9Sstevel@tonic-gate exitcode = EX_ERROR; 234*7c478bd9Sstevel@tonic-gate } 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate 237*7c478bd9Sstevel@tonic-gate /* Finished (now wasn't that special?) */ 238*7c478bd9Sstevel@tonic-gate return(exitcode); 239*7c478bd9Sstevel@tonic-gate } 240