1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2002-2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate /* 34*0Sstevel@tonic-gate * listdgrp.c 35*0Sstevel@tonic-gate * 36*0Sstevel@tonic-gate * Contains 37*0Sstevel@tonic-gate * listdgrp Writes on the standard output stream a list of devices 38*0Sstevel@tonic-gate * that belong to the specified device group 39*0Sstevel@tonic-gate */ 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #include <sys/types.h> 42*0Sstevel@tonic-gate #include <stdio.h> 43*0Sstevel@tonic-gate #include <stdlib.h> 44*0Sstevel@tonic-gate #include <string.h> 45*0Sstevel@tonic-gate #include <errno.h> 46*0Sstevel@tonic-gate #include <devmgmt.h> 47*0Sstevel@tonic-gate #include <devtab.h> 48*0Sstevel@tonic-gate #include <fmtmsg.h> 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate /* 52*0Sstevel@tonic-gate * Local Definitions 53*0Sstevel@tonic-gate * TRUE Boolean TRUE value (if not already defined) 54*0Sstevel@tonic-gate * FALSE Boolean not-TRUE value (if not already defined) 55*0Sstevel@tonic-gate */ 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate #ifndef TRUE 58*0Sstevel@tonic-gate #define TRUE ('t') 59*0Sstevel@tonic-gate #endif 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #ifndef FALSE 62*0Sstevel@tonic-gate #define FALSE (0) 63*0Sstevel@tonic-gate #endif 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate /* 66*0Sstevel@tonic-gate * Messages: 67*0Sstevel@tonic-gate * M_USAGE Command usage error 68*0Sstevel@tonic-gate * M_NODGRP Device group not found 69*0Sstevel@tonic-gate * M_DGRPTAB Device-group table not found 70*0Sstevel@tonic-gate * M_ERROR Internal error 71*0Sstevel@tonic-gate */ 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate #define M_USAGE "usage: listdgrp dgroup" 74*0Sstevel@tonic-gate #define M_NODGRP "Device group not found: %s" 75*0Sstevel@tonic-gate #define M_DGRPTAB "Cannot open device-group table: %s" 76*0Sstevel@tonic-gate #define M_ERROR "Internal error, errno=%d" 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate /* 80*0Sstevel@tonic-gate * Exit codes 81*0Sstevel@tonic-gate * EX_OK Exiting okay, no problem 82*0Sstevel@tonic-gate * EX_ERROR Some problem with the command 83*0Sstevel@tonic-gate * EX_NODGRPTAB Device group table could not be opened 84*0Sstevel@tonic-gate * EX_NODGROUP Device group couldn't be found 85*0Sstevel@tonic-gate */ 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate #define EX_OK 0 88*0Sstevel@tonic-gate #define EX_ERROR 1 89*0Sstevel@tonic-gate #define EX_NODGRPTAB 2 90*0Sstevel@tonic-gate #define EX_NODGROUP 3 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate /* 94*0Sstevel@tonic-gate * Macros 95*0Sstevel@tonic-gate * stdmsg(r,l,s,t) Write a message in standard format 96*0Sstevel@tonic-gate * r Recoverability flag 97*0Sstevel@tonic-gate * l Label 98*0Sstevel@tonic-gate * s Severity 99*0Sstevel@tonic-gate * t Tag 100*0Sstevel@tonic-gate */ 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate #define stdmsg(r,l,s,t) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG) 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate /* 105*0Sstevel@tonic-gate * Global Variables 106*0Sstevel@tonic-gate */ 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate /* 110*0Sstevel@tonic-gate * Static Variables 111*0Sstevel@tonic-gate * 112*0Sstevel@tonic-gate * lbl Buffer for the message label 113*0Sstevel@tonic-gate */ 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate static char lbl[MM_MXLABELLN+1]; 116*0Sstevel@tonic-gate static char msg[MM_MXTXTLN+1]; 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate /* 119*0Sstevel@tonic-gate * listdgrp <dgroup> 120*0Sstevel@tonic-gate * 121*0Sstevel@tonic-gate * List the devices that belong to the device group <dgroup>. 122*0Sstevel@tonic-gate * It writes the list to the standard output file (stdout) 123*0Sstevel@tonic-gate * in a new-line list. 124*0Sstevel@tonic-gate * 125*0Sstevel@tonic-gate * Returns: 126*0Sstevel@tonic-gate * 0 Ok 127*0Sstevel@tonic-gate * 1 Syntax or other error 128*0Sstevel@tonic-gate * 2 Device table can't be opened 129*0Sstevel@tonic-gate * 3 Device group doesn't exist 130*0Sstevel@tonic-gate */ 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate main(argc, argv) 133*0Sstevel@tonic-gate int argc; /* Number of items in command */ 134*0Sstevel@tonic-gate char **argv; /* List of pointers to the arguments */ 135*0Sstevel@tonic-gate { 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate /* 138*0Sstevel@tonic-gate * Automatic data 139*0Sstevel@tonic-gate */ 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate char **devices; /* List of devices in the group */ 142*0Sstevel@tonic-gate char **pp; /* Running pointer to device names */ 143*0Sstevel@tonic-gate char *cmdname; /* Simple command name */ 144*0Sstevel@tonic-gate char *dgrptab; /* The device-group table name */ 145*0Sstevel@tonic-gate char *dgroup; /* Device group to list */ 146*0Sstevel@tonic-gate int exitcode; /* Value to return to the caller */ 147*0Sstevel@tonic-gate int sev; /* Message severity */ 148*0Sstevel@tonic-gate int optchar; /* Option char (from getopt()) */ 149*0Sstevel@tonic-gate int usageerr; /* TRUE if syntax error on command */ 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate /* Build the message label from the (simple) command name */ 153*0Sstevel@tonic-gate if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++; 154*0Sstevel@tonic-gate else cmdname = argv[0]; 155*0Sstevel@tonic-gate (void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl)); 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate /* Only write the text component of a message (this goes away in SVR4.1) */ 158*0Sstevel@tonic-gate (void) putenv("MSGVERB=text"); 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate /* 161*0Sstevel@tonic-gate * Parse the command line: 162*0Sstevel@tonic-gate * - Options 163*0Sstevel@tonic-gate * - Device group to display 164*0Sstevel@tonic-gate */ 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate /* 167*0Sstevel@tonic-gate * Extract options from the command line 168*0Sstevel@tonic-gate */ 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate /* Initializations */ 171*0Sstevel@tonic-gate usageerr = FALSE; /* No errors on the command line (yet) */ 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate /* 174*0Sstevel@tonic-gate * Loop until all of the command line options have been parced 175*0Sstevel@tonic-gate * (and don't let getopt() write messages) 176*0Sstevel@tonic-gate */ 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate opterr = FALSE; 179*0Sstevel@tonic-gate while ((optchar = getopt(argc, argv, "")) != EOF) switch (optchar) { 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate /* Default case -- command usage error */ 182*0Sstevel@tonic-gate case '?': 183*0Sstevel@tonic-gate default: 184*0Sstevel@tonic-gate usageerr = TRUE; 185*0Sstevel@tonic-gate break; 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate /* Check the argument count and extract the device group name */ 189*0Sstevel@tonic-gate if (usageerr || (optind != argc-1)) usageerr = TRUE; 190*0Sstevel@tonic-gate else dgroup = argv[optind]; 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate /* If there is a usage error, write an appropriate message and exit */ 193*0Sstevel@tonic-gate if (usageerr) { 194*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE); 195*0Sstevel@tonic-gate exit(EX_ERROR); 196*0Sstevel@tonic-gate } 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate /* Open the device-group file (if there's one to be opened) */ 199*0Sstevel@tonic-gate if (!_opendgrptab("r")) { 200*0Sstevel@tonic-gate if (dgrptab = _dgrptabpath()) { 201*0Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), M_DGRPTAB, dgrptab); 202*0Sstevel@tonic-gate exitcode = EX_NODGRPTAB; 203*0Sstevel@tonic-gate sev = MM_ERROR; 204*0Sstevel@tonic-gate } else { 205*0Sstevel@tonic-gate (void) sprintf(msg, M_ERROR, errno); 206*0Sstevel@tonic-gate exitcode = EX_ERROR; 207*0Sstevel@tonic-gate sev = MM_HALT; 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, sev, msg); 210*0Sstevel@tonic-gate exit(exitcode); 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate /* 215*0Sstevel@tonic-gate * Get the list of devices associated with the device group. 216*0Sstevel@tonic-gate * If we get one, write the list to the standard output. 217*0Sstevel@tonic-gate * Otherwise, write an appropriate error message 218*0Sstevel@tonic-gate */ 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate exitcode = EX_OK; 221*0Sstevel@tonic-gate if (devices = listdgrp(dgroup)) { 222*0Sstevel@tonic-gate for (pp = devices ; *pp ; pp++) (void) puts(*pp); 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate else { 225*0Sstevel@tonic-gate if (errno == EINVAL) { 226*0Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), M_NODGRP, dgroup); 227*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg); 228*0Sstevel@tonic-gate exitcode = EX_NODGROUP; 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate else { 231*0Sstevel@tonic-gate (void) sprintf(msg, M_ERROR, errno); 232*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_HALT, msg); 233*0Sstevel@tonic-gate exitcode = EX_ERROR; 234*0Sstevel@tonic-gate } 235*0Sstevel@tonic-gate } 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate /* Finished (now wasn't that special?) */ 238*0Sstevel@tonic-gate return(exitcode); 239*0Sstevel@tonic-gate } 240