10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*644Sakaplan * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate /* 330Sstevel@tonic-gate * Implements the main body of the "getdgrp" command. 340Sstevel@tonic-gate */ 350Sstevel@tonic-gate #include <sys/types.h> 360Sstevel@tonic-gate #include <stdio.h> 370Sstevel@tonic-gate #include <errno.h> 380Sstevel@tonic-gate #include <stdlib.h> 390Sstevel@tonic-gate #include <string.h> 400Sstevel@tonic-gate #include <devmgmt.h> 410Sstevel@tonic-gate #include <devtab.h> 420Sstevel@tonic-gate #include <fmtmsg.h> 430Sstevel@tonic-gate 440Sstevel@tonic-gate 450Sstevel@tonic-gate /* 460Sstevel@tonic-gate * Local Definitions 470Sstevel@tonic-gate * TRUE Boolean TRUE value 480Sstevel@tonic-gate * FALSE Boolean FALSE value 490Sstevel@tonic-gate * NULL Null address 500Sstevel@tonic-gate */ 510Sstevel@tonic-gate 520Sstevel@tonic-gate #ifndef TRUE 530Sstevel@tonic-gate #define TRUE 1 540Sstevel@tonic-gate #endif 550Sstevel@tonic-gate 560Sstevel@tonic-gate #ifndef FALSE 570Sstevel@tonic-gate #define FALSE 0 580Sstevel@tonic-gate #endif 590Sstevel@tonic-gate 600Sstevel@tonic-gate 610Sstevel@tonic-gate /* 620Sstevel@tonic-gate * Exit codes: 630Sstevel@tonic-gate * EX_OK All's well that ends well 640Sstevel@tonic-gate * EX_ERROR Some other error occurred 650Sstevel@tonic-gate * EX_DTAB Device table couldn't be opened 660Sstevel@tonic-gate * EX_DGRP Device-group table couldn't be open. 670Sstevel@tonic-gate */ 680Sstevel@tonic-gate 690Sstevel@tonic-gate #define EX_OK 0 700Sstevel@tonic-gate #define EX_ERROR 1 710Sstevel@tonic-gate #define EX_DTAB 2 720Sstevel@tonic-gate #define EX_DGRP 2 730Sstevel@tonic-gate 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 760Sstevel@tonic-gate * Messages: 770Sstevel@tonic-gate * M_USAGE Command usage error 780Sstevel@tonic-gate * M_ERROR Some unexpected error 790Sstevel@tonic-gate * M_DEVTAB Device table couldn't be opened 800Sstevel@tonic-gate * M_DGROUP Device-group table couldn't be opened 810Sstevel@tonic-gate */ 820Sstevel@tonic-gate 830Sstevel@tonic-gate #define M_USAGE "usage: getdgrp [-ael] [criterion [...]] [dgroup [...]]" 840Sstevel@tonic-gate #define M_ERROR "Internal error, errno=%d" 850Sstevel@tonic-gate #define M_DEVTAB "Cannot open the device table: %s" 860Sstevel@tonic-gate #define M_DGROUP "Cannot open the device-group table: %s" 870Sstevel@tonic-gate 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * Internal References 910Sstevel@tonic-gate * buildcriterialist() Builds a list of the criteria on the 920Sstevel@tonic-gate * command line 930Sstevel@tonic-gate * buildgrouplist() Builds a list of the device-groups mentioned 940Sstevel@tonic-gate * on the command line 950Sstevel@tonic-gate */ 960Sstevel@tonic-gate 970Sstevel@tonic-gate static char **buildcriterialist(); /* Builds criteria list from command line */ 980Sstevel@tonic-gate static char **builddgrouplist(); /* Builds dgroup list from command line */ 990Sstevel@tonic-gate 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate /* 1020Sstevel@tonic-gate * Macros 1030Sstevel@tonic-gate * stdmsg(r,l,s,t) Generate a standard message 1040Sstevel@tonic-gate * r Recoverability flag 1050Sstevel@tonic-gate * l Standard label 1060Sstevel@tonic-gate * s Severity 1070Sstevel@tonic-gate * t Text 1080Sstevel@tonic-gate * isacriterion(p) Returns TRUE if *p is a criterion, FALSE otherwise 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate #define stdmsg(r,l,s,t) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG) 1120Sstevel@tonic-gate #define isacriterion(p) (strchr(*arglist,'=')||strchr(*arglist,':')) 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate /* 1160Sstevel@tonic-gate * Static Variables 1170Sstevel@tonic-gate * lbl Buffer for standard message label 1180Sstevel@tonic-gate * txt Buffer for standard message text 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate static char lbl[MM_MXLABELLN+1]; 1220Sstevel@tonic-gate static char txt[MM_MXTXTLN+1]; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate /* 1250Sstevel@tonic-gate * getdgrp [-ael] [criterion [...]] [dgroup [...]] 1260Sstevel@tonic-gate * 1270Sstevel@tonic-gate * This function gets the device groups that contain as members devices 1280Sstevel@tonic-gate * that match the given criteria. 1290Sstevel@tonic-gate * 1300Sstevel@tonic-gate * Options: 1310Sstevel@tonic-gate * -a A device must meet all criteria before the device-group in 1320Sstevel@tonic-gate * which it is a member can be selected for inclusion in the 1330Sstevel@tonic-gate * generated list. If this option is missing, a device must 1340Sstevel@tonic-gate * meet at least one criterion before it's group can be 1350Sstevel@tonic-gate * selected. This option has no affect if there are no criterion 1360Sstevel@tonic-gate * on the command-line. 1370Sstevel@tonic-gate * -e The list of device groups specifies groups to exclude from 1380Sstevel@tonic-gate * the generated list. If this option is omitted, the list 1390Sstevel@tonic-gate * of groups is the set of groups that can be selected. This 1400Sstevel@tonic-gate * option has no effect if there are no device-groups on the 1410Sstevel@tonic-gate * command-line. 1420Sstevel@tonic-gate * -l List all device groups, even those that have no valid 1430Sstevel@tonic-gate * members (this option has no effect if criterion are specified 1440Sstevel@tonic-gate * 1450Sstevel@tonic-gate * Arguments: 1460Sstevel@tonic-gate * criterion A device criterion of the form <attr><op><val> where 1470Sstevel@tonic-gate * <attr> is the name of an attribute, <op> is "=", "!=", 1480Sstevel@tonic-gate * ":", or "!:" for "is equal to", "is not equal to", 1490Sstevel@tonic-gate * "is defined," or "is not defined." <val> is the value 1500Sstevel@tonic-gate * that the attribute must be equal to or not equal to. 1510Sstevel@tonic-gate * (<val> must be "*" if <op> is ":" or "!:"). 1520Sstevel@tonic-gate * dgroup A device group that is to be exclude selected for the 1530Sstevel@tonic-gate * generated list or excluded from the the generated 1540Sstevel@tonic-gate * list. 1550Sstevel@tonic-gate * 1560Sstevel@tonic-gate * Exit values: 1570Sstevel@tonic-gate * 0 Success 1580Sstevel@tonic-gate * 1 Usage or an internal error 1590Sstevel@tonic-gate * 2 The device table or the device-group table could not be 1600Sstevel@tonic-gate * opened for reading 1610Sstevel@tonic-gate */ 1620Sstevel@tonic-gate 163*644Sakaplan int 164*644Sakaplan main(int argc, char **argv) 1650Sstevel@tonic-gate { 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * Automatic data 1690Sstevel@tonic-gate */ 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate char **arglist; /* List of arguments (subset of argv) */ 1720Sstevel@tonic-gate char **criterialist; /* List of criteria */ 1730Sstevel@tonic-gate char **dgrouplist; /* List of device groups to search or ignore */ 1740Sstevel@tonic-gate char **fitgrouplist; /* List of device groups that fit criteria */ 1750Sstevel@tonic-gate char *cmdname; /* Simple command name */ 1760Sstevel@tonic-gate char *dgroup; /* Pointer to device group name in list */ 1770Sstevel@tonic-gate char *filename; /* Pointer to filename in "error" */ 1780Sstevel@tonic-gate int exitcode; /* Value to return to the caller */ 1790Sstevel@tonic-gate int sev; /* Message severity */ 1800Sstevel@tonic-gate int optchar; /* Option character (returned by getopt()) */ 1810Sstevel@tonic-gate int andflag; /* TRUE if anding criteria, FALSE if or'ed */ 1820Sstevel@tonic-gate int excludeflag; /* TRUE if the dgroups list those to exclude */ 1830Sstevel@tonic-gate int allflag; /* TRUE if all device grps are to be displayed */ 1840Sstevel@tonic-gate int options; /* Options to pass to getdgrp() */ 1850Sstevel@tonic-gate int usageerr; /* TRUE if syntax error */ 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate /* Build the message label from the (simple) command name */ 1890Sstevel@tonic-gate if (cmdname = strrchr(argv[0], '/')) cmdname++; 1900Sstevel@tonic-gate else cmdname = argv[0]; 1910Sstevel@tonic-gate (void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl)); 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate /* Only write the text-component of messages (this goes away in SVR4.1) */ 1940Sstevel@tonic-gate (void) putenv("MSGVERB=text"); 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* 1970Sstevel@tonic-gate * Parse the command line: 1980Sstevel@tonic-gate * - Options 1990Sstevel@tonic-gate * - Selection criteria 2000Sstevel@tonic-gate * - Device groups to include or exclude 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* 2040Sstevel@tonic-gate * Extract options from the command line 2050Sstevel@tonic-gate */ 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* Initializations */ 2080Sstevel@tonic-gate andflag = FALSE; /* No -a */ 2090Sstevel@tonic-gate excludeflag = FALSE; /* No -e */ 2100Sstevel@tonic-gate allflag = FALSE; /* No -l */ 2110Sstevel@tonic-gate usageerr = FALSE; /* No errors yet */ 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate /* 2140Sstevel@tonic-gate * Loop until all of the command line options have been parced 2150Sstevel@tonic-gate */ 2160Sstevel@tonic-gate opterr = FALSE; /* Don't let getopt() write messages */ 2170Sstevel@tonic-gate while ((optchar = getopt(argc, argv, "ael")) != EOF) switch (optchar) { 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* -a List device groups that fit all of the criteria listed */ 2200Sstevel@tonic-gate case 'a': 2210Sstevel@tonic-gate if (andflag) usageerr = TRUE; 2220Sstevel@tonic-gate else andflag = TRUE; 2230Sstevel@tonic-gate break; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* -e Exclude those device groups mentioned on the command line */ 2260Sstevel@tonic-gate case 'e': 2270Sstevel@tonic-gate if (excludeflag) usageerr = TRUE; 2280Sstevel@tonic-gate else excludeflag = TRUE; 2290Sstevel@tonic-gate break; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* -l List all device groups (if no criteria is specified) */ 2320Sstevel@tonic-gate case 'l': 2330Sstevel@tonic-gate if (allflag) usageerr = TRUE; 2340Sstevel@tonic-gate else allflag = TRUE; 2350Sstevel@tonic-gate break; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate /* Default case -- command usage error */ 2380Sstevel@tonic-gate case '?': 2390Sstevel@tonic-gate default: 2400Sstevel@tonic-gate usageerr = TRUE; 2410Sstevel@tonic-gate break; 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* If there is a usage error, write an appropriate message and exit */ 2450Sstevel@tonic-gate if (usageerr) { 2460Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE); 2470Sstevel@tonic-gate exit(EX_ERROR); 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate /* Open the device file (if there's one to be opened) */ 2510Sstevel@tonic-gate if (!_opendevtab("r")) { 2520Sstevel@tonic-gate if (filename = _devtabpath()) { 2530Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_DEVTAB, filename); 2540Sstevel@tonic-gate exitcode = EX_DTAB; 2550Sstevel@tonic-gate sev = MM_ERROR; 2560Sstevel@tonic-gate } else { 2570Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 2580Sstevel@tonic-gate exitcode = EX_ERROR; 2590Sstevel@tonic-gate sev = MM_HALT; 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, sev, txt); 2620Sstevel@tonic-gate exit(exitcode); 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate /* Open the device file (if there's one to be opened) */ 2660Sstevel@tonic-gate if (!_opendgrptab("r")) { 2670Sstevel@tonic-gate if (filename = _dgrptabpath()) { 2680Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_DGROUP, filename); 2690Sstevel@tonic-gate exitcode = EX_DGRP; 2700Sstevel@tonic-gate sev = MM_ERROR; 2710Sstevel@tonic-gate } else { 2720Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 2730Sstevel@tonic-gate exitcode = EX_ERROR; 2740Sstevel@tonic-gate sev = MM_HALT; 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, sev, txt); 2770Sstevel@tonic-gate exit(exitcode); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate /* Build the list of criteria and device groups */ 2810Sstevel@tonic-gate arglist = argv + optind; 2820Sstevel@tonic-gate criterialist = buildcriterialist(arglist); 2830Sstevel@tonic-gate dgrouplist = builddgrouplist(arglist); 2840Sstevel@tonic-gate options = (excludeflag ? DTAB_EXCLUDEFLAG : 0) | 2850Sstevel@tonic-gate (andflag ? DTAB_ANDCRITERIA : 0) | 2860Sstevel@tonic-gate (allflag ? DTAB_LISTALL : 0) ; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * Get the list of device groups that meets the criteria requested. 2900Sstevel@tonic-gate * If we got a list (that might be empty), write that list to the 2910Sstevel@tonic-gate * standard output file (stdout). 2920Sstevel@tonic-gate */ 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate exitcode = EX_OK; 2950Sstevel@tonic-gate if (!(fitgrouplist = getdgrp(dgrouplist, criterialist, options))) { 2960Sstevel@tonic-gate exitcode = EX_ERROR; 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate else for (dgroup = *fitgrouplist++ ; dgroup ; dgroup = *fitgrouplist++) 2990Sstevel@tonic-gate (void) puts(dgroup); 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate /* Finished */ 3020Sstevel@tonic-gate return(exitcode); 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate /* 3060Sstevel@tonic-gate * char **buildcriterialist(arglist) 3070Sstevel@tonic-gate * char **arglist 3080Sstevel@tonic-gate * 3090Sstevel@tonic-gate * This function builds a list of criteria descriptions from the 3100Sstevel@tonic-gate * list of arguments given. The list returned is in malloc()ed 3110Sstevel@tonic-gate * space. 3120Sstevel@tonic-gate * 3130Sstevel@tonic-gate * Arguments: 3140Sstevel@tonic-gate * arglist The address of the first element in the list 3150Sstevel@tonic-gate * of arguments (possibly) containing criterion 3160Sstevel@tonic-gate * 3170Sstevel@tonic-gate * Returns: char ** 3180Sstevel@tonic-gate * A pointer to the first element in the list of criterion. 3190Sstevel@tonic-gate * If there was a problem, the function returns (char **) NULL. 3200Sstevel@tonic-gate * If there are no criteria in the list, the function returns 3210Sstevel@tonic-gate * an empty list. 3220Sstevel@tonic-gate */ 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate static char ** 3250Sstevel@tonic-gate buildcriterialist(arglist) 3260Sstevel@tonic-gate char **arglist; /* Pointer to the list of argument pointers */ 3270Sstevel@tonic-gate { 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * Automatic data 3300Sstevel@tonic-gate */ 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate char **pp; /* Pointer to a criteria */ 3330Sstevel@tonic-gate void *allocbuf; /* Pointer to the allocated data */ 3340Sstevel@tonic-gate int ncriteria; /* Number of criteria found */ 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * Search the argument list, looking for the end of the list or 3390Sstevel@tonic-gate * the first thing that's not a criteria. (A criteria is a 3400Sstevel@tonic-gate * character-string that contains a colon (':') or an equal-sign ('=') 3410Sstevel@tonic-gate */ 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate pp = arglist; 3440Sstevel@tonic-gate ncriteria = 1; 3450Sstevel@tonic-gate while (*pp && (strchr(*pp, '=') || strchr(*pp, ':'))) { 3460Sstevel@tonic-gate ncriteria++; 3470Sstevel@tonic-gate pp++; 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate /* Allocate space for the list of criteria pointers */ 3510Sstevel@tonic-gate if (allocbuf = malloc(ncriteria*sizeof(char **))) { 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate /* Build the list of criteria arguments */ 3540Sstevel@tonic-gate pp = (char **) allocbuf; 3550Sstevel@tonic-gate while ((*arglist != (char *) NULL) && isacriterion(*arglist)) *pp++ = *arglist++; 3560Sstevel@tonic-gate *pp = (char *) NULL; 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate return ((char **) allocbuf); 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate /* 3630Sstevel@tonic-gate * char **builddgrouplist(arglist) 3640Sstevel@tonic-gate * char **arglist 3650Sstevel@tonic-gate * 3660Sstevel@tonic-gate * This function returns a pointer to the first element in a list of 3670Sstevel@tonic-gate * device-groups (i.e. not criteria) specified in the list of arguments 3680Sstevel@tonic-gate * whose first element is pointed to by <arglist>. 3690Sstevel@tonic-gate * 3700Sstevel@tonic-gate * Arguments: 3710Sstevel@tonic-gate * arglist The address of the first element in the list of 3720Sstevel@tonic-gate * arguments to be searched for non-criteria 3730Sstevel@tonic-gate * 3740Sstevel@tonic-gate * Returns: char ** 3750Sstevel@tonic-gate * The address of the first item in the list of arguments that are 3760Sstevel@tonic-gate * not criteria. If none, the function returns a pointer to a 3770Sstevel@tonic-gate * null list. 3780Sstevel@tonic-gate * 3790Sstevel@tonic-gate * Note: 3800Sstevel@tonic-gate * - The current implementation returns a pointer to an element in 3810Sstevel@tonic-gate * <arglist>. 3820Sstevel@tonic-gate */ 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate static char ** 3850Sstevel@tonic-gate builddgrouplist(arglist) 3860Sstevel@tonic-gate char **arglist; /* First item in the list of arguments */ 3870Sstevel@tonic-gate { 3880Sstevel@tonic-gate /* 3890Sstevel@tonic-gate * Automatic data 3900Sstevel@tonic-gate */ 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate /* 3930Sstevel@tonic-gate * Search the argument list, looking for the end of the list or 3940Sstevel@tonic-gate * the first thing that's not a criteria. It is the first device 3950Sstevel@tonic-gate * group in the list of device groups (if any). 3960Sstevel@tonic-gate */ 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate while (*arglist && isacriterion(*arglist)) arglist++; 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate /* Return a pointer to the argument list. */ 4010Sstevel@tonic-gate return(arglist); 4020Sstevel@tonic-gate } 403