xref: /onnv-gate/usr/src/cmd/devmgmt/cmds/listdgrp.c (revision 644:bca1986a1faa)
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 
270Sstevel@tonic-gate /*    Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /*      All Rights Reserved   */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  *  listdgrp.c
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  *  Contains
370Sstevel@tonic-gate  *	listdgrp	Writes on the standard output stream a list of devices
380Sstevel@tonic-gate  *			that belong to the specified device group
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include	<sys/types.h>
420Sstevel@tonic-gate #include	<stdio.h>
430Sstevel@tonic-gate #include	<stdlib.h>
440Sstevel@tonic-gate #include	<string.h>
450Sstevel@tonic-gate #include	<errno.h>
460Sstevel@tonic-gate #include	<devmgmt.h>
470Sstevel@tonic-gate #include	<devtab.h>
480Sstevel@tonic-gate #include	<fmtmsg.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  *  Local Definitions
530Sstevel@tonic-gate  *	TRUE		Boolean TRUE value (if not already defined)
540Sstevel@tonic-gate  *	FALSE		Boolean not-TRUE value (if not already defined)
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #ifndef	TRUE
580Sstevel@tonic-gate #define	TRUE		('t')
590Sstevel@tonic-gate #endif
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #ifndef	FALSE
620Sstevel@tonic-gate #define	FALSE		(0)
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  *  Messages:
670Sstevel@tonic-gate  *	M_USAGE		Command usage error
680Sstevel@tonic-gate  *	M_NODGRP	Device group not found
690Sstevel@tonic-gate  *	M_DGRPTAB	Device-group table not found
700Sstevel@tonic-gate  *	M_ERROR		Internal error
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #define	M_USAGE		"usage: listdgrp dgroup"
740Sstevel@tonic-gate #define	M_NODGRP	"Device group not found: %s"
750Sstevel@tonic-gate #define	M_DGRPTAB	"Cannot open device-group table: %s"
760Sstevel@tonic-gate #define	M_ERROR		"Internal error, errno=%d"
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  *  Exit codes
810Sstevel@tonic-gate  *	EX_OK		Exiting okay, no problem
820Sstevel@tonic-gate  *	EX_ERROR	Some problem with the command
830Sstevel@tonic-gate  *	EX_NODGRPTAB	Device group table could not be opened
840Sstevel@tonic-gate  *	EX_NODGROUP	Device group couldn't be found
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #define	EX_OK		0
880Sstevel@tonic-gate #define	EX_ERROR	1
890Sstevel@tonic-gate #define	EX_NODGRPTAB	2
900Sstevel@tonic-gate #define	EX_NODGROUP	3
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  *  Macros
950Sstevel@tonic-gate  *	stdmsg(r,l,s,t)	    Write a message in standard format
960Sstevel@tonic-gate  *				r	Recoverability flag
970Sstevel@tonic-gate  *				l	Label
980Sstevel@tonic-gate  *				s	Severity
990Sstevel@tonic-gate  *				t	Tag
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #define	stdmsg(r,l,s,t)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  *  Global Variables
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  *  Static Variables
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  *	lbl	Buffer for the message label
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static	char	lbl[MM_MXLABELLN+1];
1160Sstevel@tonic-gate static	char	msg[MM_MXTXTLN+1];
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  *  listdgrp <dgroup>
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  *	List the devices that belong to the device group <dgroup>.
1220Sstevel@tonic-gate  *	It writes the list to the standard output file (stdout)
1230Sstevel@tonic-gate  *	in a new-line list.
1240Sstevel@tonic-gate  *
1250Sstevel@tonic-gate  *  Returns:
1260Sstevel@tonic-gate  *	0	Ok
1270Sstevel@tonic-gate  *	1	Syntax or other error
1280Sstevel@tonic-gate  *	2	Device table can't be opened
1290Sstevel@tonic-gate  *	3	Device group doesn't exist
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate 
132*644Sakaplan int
main(int argc,char ** argv)133*644Sakaplan main(int argc, char **argv)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/*
1370Sstevel@tonic-gate 	 *  Automatic data
1380Sstevel@tonic-gate 	 */
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	char	      **devices;	/* List of devices in the group */
1410Sstevel@tonic-gate 	char	      **pp;		/* Running pointer to device names */
1420Sstevel@tonic-gate 	char	       *cmdname;	/* Simple command name */
1430Sstevel@tonic-gate 	char	       *dgrptab;	/* The device-group table name */
1440Sstevel@tonic-gate 	char	       *dgroup;		/* Device group to list */
1450Sstevel@tonic-gate 	int		exitcode;	/* Value to return to the caller */
1460Sstevel@tonic-gate 	int		sev;		/* Message severity */
1470Sstevel@tonic-gate 	int		optchar;	/* Option char (from getopt()) */
1480Sstevel@tonic-gate 	int		usageerr;	/* TRUE if syntax error on command */
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/* Build the message label from the (simple) command name */
1520Sstevel@tonic-gate 	if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++;
1530Sstevel@tonic-gate 	else cmdname = argv[0];
1540Sstevel@tonic-gate 	(void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl));
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/* Only write the text component of a message (this goes away in SVR4.1) */
1570Sstevel@tonic-gate 	(void) putenv("MSGVERB=text");
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/*
1600Sstevel@tonic-gate 	 *  Parse the command line:
1610Sstevel@tonic-gate 	 *	- Options
1620Sstevel@tonic-gate 	 *	- Device group to display
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 *  Extract options from the command line
1670Sstevel@tonic-gate 	 */
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	/* Initializations */
1700Sstevel@tonic-gate 	usageerr = FALSE;	/* No errors on the command line (yet) */
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/*
1730Sstevel@tonic-gate 	 *  Loop until all of the command line options have been parced
1740Sstevel@tonic-gate 	 *  (and don't let getopt() write messages)
1750Sstevel@tonic-gate 	 */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	opterr = FALSE;
1780Sstevel@tonic-gate 	while ((optchar = getopt(argc, argv, "")) != EOF) switch (optchar) {
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/* Default case -- command usage error */
1810Sstevel@tonic-gate 	case '?':
1820Sstevel@tonic-gate 	default:
1830Sstevel@tonic-gate 	    usageerr = TRUE;
1840Sstevel@tonic-gate 	    break;
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	/* Check the argument count and extract the device group name */
1880Sstevel@tonic-gate 	if (usageerr || (optind != argc-1)) usageerr = TRUE;
1890Sstevel@tonic-gate 	else dgroup = argv[optind];
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/* If there is a usage error, write an appropriate message and exit */
1920Sstevel@tonic-gate 	if (usageerr) {
1930Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
1940Sstevel@tonic-gate 	    exit(EX_ERROR);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* Open the device-group file (if there's one to be opened) */
1980Sstevel@tonic-gate 	if (!_opendgrptab("r")) {
1990Sstevel@tonic-gate 	    if (dgrptab = _dgrptabpath()) {
2000Sstevel@tonic-gate 		(void) snprintf(msg, sizeof(msg), M_DGRPTAB, dgrptab);
2010Sstevel@tonic-gate 		exitcode = EX_NODGRPTAB;
2020Sstevel@tonic-gate 		sev = MM_ERROR;
2030Sstevel@tonic-gate 	    } else {
2040Sstevel@tonic-gate 		(void) sprintf(msg, M_ERROR, errno);
2050Sstevel@tonic-gate 		exitcode = EX_ERROR;
2060Sstevel@tonic-gate 		sev = MM_HALT;
2070Sstevel@tonic-gate 	    }
2080Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, sev, msg);
2090Sstevel@tonic-gate 	    exit(exitcode);
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * Get the list of devices associated with the device group.
2150Sstevel@tonic-gate 	 * If we get one, write the list to the standard output.
2160Sstevel@tonic-gate 	 * Otherwise, write an appropriate error message
2170Sstevel@tonic-gate 	 */
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	exitcode = EX_OK;
2200Sstevel@tonic-gate 	if (devices = listdgrp(dgroup)) {
2210Sstevel@tonic-gate 	    for (pp = devices ; *pp ; pp++) (void) puts(*pp);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 	else {
2240Sstevel@tonic-gate 	    if (errno == EINVAL) {
2250Sstevel@tonic-gate 		(void) snprintf(msg, sizeof(msg), M_NODGRP, dgroup);
2260Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
2270Sstevel@tonic-gate 		exitcode = EX_NODGROUP;
2280Sstevel@tonic-gate 	    }
2290Sstevel@tonic-gate 	    else {
2300Sstevel@tonic-gate 		(void) sprintf(msg, M_ERROR, errno);
2310Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_HALT, msg);
2320Sstevel@tonic-gate 		exitcode = EX_ERROR;
2330Sstevel@tonic-gate 	    }
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	/* Finished (now wasn't that special?) */
2370Sstevel@tonic-gate 	return(exitcode);
2380Sstevel@tonic-gate }
239