xref: /titanic_50/usr/src/cmd/devmgmt/cmds/listdgrp.c (revision 113f42324b1acf3e5144da27ddbda36e338fbd07)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*113f4232Sakaplan  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*    Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /*      All Rights Reserved   */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  *  listdgrp.c
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  *  Contains
377c478bd9Sstevel@tonic-gate  *	listdgrp	Writes on the standard output stream a list of devices
387c478bd9Sstevel@tonic-gate  *			that belong to the specified device group
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include	<sys/types.h>
427c478bd9Sstevel@tonic-gate #include	<stdio.h>
437c478bd9Sstevel@tonic-gate #include	<stdlib.h>
447c478bd9Sstevel@tonic-gate #include	<string.h>
457c478bd9Sstevel@tonic-gate #include	<errno.h>
467c478bd9Sstevel@tonic-gate #include	<devmgmt.h>
477c478bd9Sstevel@tonic-gate #include	<devtab.h>
487c478bd9Sstevel@tonic-gate #include	<fmtmsg.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  *  Local Definitions
537c478bd9Sstevel@tonic-gate  *	TRUE		Boolean TRUE value (if not already defined)
547c478bd9Sstevel@tonic-gate  *	FALSE		Boolean not-TRUE value (if not already defined)
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #ifndef	TRUE
587c478bd9Sstevel@tonic-gate #define	TRUE		('t')
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #ifndef	FALSE
627c478bd9Sstevel@tonic-gate #define	FALSE		(0)
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  *  Messages:
677c478bd9Sstevel@tonic-gate  *	M_USAGE		Command usage error
687c478bd9Sstevel@tonic-gate  *	M_NODGRP	Device group not found
697c478bd9Sstevel@tonic-gate  *	M_DGRPTAB	Device-group table not found
707c478bd9Sstevel@tonic-gate  *	M_ERROR		Internal error
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	M_USAGE		"usage: listdgrp dgroup"
747c478bd9Sstevel@tonic-gate #define	M_NODGRP	"Device group not found: %s"
757c478bd9Sstevel@tonic-gate #define	M_DGRPTAB	"Cannot open device-group table: %s"
767c478bd9Sstevel@tonic-gate #define	M_ERROR		"Internal error, errno=%d"
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  *  Exit codes
817c478bd9Sstevel@tonic-gate  *	EX_OK		Exiting okay, no problem
827c478bd9Sstevel@tonic-gate  *	EX_ERROR	Some problem with the command
837c478bd9Sstevel@tonic-gate  *	EX_NODGRPTAB	Device group table could not be opened
847c478bd9Sstevel@tonic-gate  *	EX_NODGROUP	Device group couldn't be found
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	EX_OK		0
887c478bd9Sstevel@tonic-gate #define	EX_ERROR	1
897c478bd9Sstevel@tonic-gate #define	EX_NODGRPTAB	2
907c478bd9Sstevel@tonic-gate #define	EX_NODGROUP	3
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  *  Macros
957c478bd9Sstevel@tonic-gate  *	stdmsg(r,l,s,t)	    Write a message in standard format
967c478bd9Sstevel@tonic-gate  *				r	Recoverability flag
977c478bd9Sstevel@tonic-gate  *				l	Label
987c478bd9Sstevel@tonic-gate  *				s	Severity
997c478bd9Sstevel@tonic-gate  *				t	Tag
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	stdmsg(r,l,s,t)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  *  Global Variables
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  *  Static Variables
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  *	lbl	Buffer for the message label
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static	char	lbl[MM_MXLABELLN+1];
1167c478bd9Sstevel@tonic-gate static	char	msg[MM_MXTXTLN+1];
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  *  listdgrp <dgroup>
1207c478bd9Sstevel@tonic-gate  *
1217c478bd9Sstevel@tonic-gate  *	List the devices that belong to the device group <dgroup>.
1227c478bd9Sstevel@tonic-gate  *	It writes the list to the standard output file (stdout)
1237c478bd9Sstevel@tonic-gate  *	in a new-line list.
1247c478bd9Sstevel@tonic-gate  *
1257c478bd9Sstevel@tonic-gate  *  Returns:
1267c478bd9Sstevel@tonic-gate  *	0	Ok
1277c478bd9Sstevel@tonic-gate  *	1	Syntax or other error
1287c478bd9Sstevel@tonic-gate  *	2	Device table can't be opened
1297c478bd9Sstevel@tonic-gate  *	3	Device group doesn't exist
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate 
132*113f4232Sakaplan int
main(int argc,char ** argv)133*113f4232Sakaplan main(int argc, char **argv)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 *  Automatic data
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	char	      **devices;	/* List of devices in the group */
1417c478bd9Sstevel@tonic-gate 	char	      **pp;		/* Running pointer to device names */
1427c478bd9Sstevel@tonic-gate 	char	       *cmdname;	/* Simple command name */
1437c478bd9Sstevel@tonic-gate 	char	       *dgrptab;	/* The device-group table name */
1447c478bd9Sstevel@tonic-gate 	char	       *dgroup;		/* Device group to list */
1457c478bd9Sstevel@tonic-gate 	int		exitcode;	/* Value to return to the caller */
1467c478bd9Sstevel@tonic-gate 	int		sev;		/* Message severity */
1477c478bd9Sstevel@tonic-gate 	int		optchar;	/* Option char (from getopt()) */
1487c478bd9Sstevel@tonic-gate 	int		usageerr;	/* TRUE if syntax error on command */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/* Build the message label from the (simple) command name */
1527c478bd9Sstevel@tonic-gate 	if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++;
1537c478bd9Sstevel@tonic-gate 	else cmdname = argv[0];
1547c478bd9Sstevel@tonic-gate 	(void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl));
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/* Only write the text component of a message (this goes away in SVR4.1) */
1577c478bd9Sstevel@tonic-gate 	(void) putenv("MSGVERB=text");
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 *  Parse the command line:
1617c478bd9Sstevel@tonic-gate 	 *	- Options
1627c478bd9Sstevel@tonic-gate 	 *	- Device group to display
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 *  Extract options from the command line
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* Initializations */
1707c478bd9Sstevel@tonic-gate 	usageerr = FALSE;	/* No errors on the command line (yet) */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 *  Loop until all of the command line options have been parced
1747c478bd9Sstevel@tonic-gate 	 *  (and don't let getopt() write messages)
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	opterr = FALSE;
1787c478bd9Sstevel@tonic-gate 	while ((optchar = getopt(argc, argv, "")) != EOF) switch (optchar) {
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/* Default case -- command usage error */
1817c478bd9Sstevel@tonic-gate 	case '?':
1827c478bd9Sstevel@tonic-gate 	default:
1837c478bd9Sstevel@tonic-gate 	    usageerr = TRUE;
1847c478bd9Sstevel@tonic-gate 	    break;
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/* Check the argument count and extract the device group name */
1887c478bd9Sstevel@tonic-gate 	if (usageerr || (optind != argc-1)) usageerr = TRUE;
1897c478bd9Sstevel@tonic-gate 	else dgroup = argv[optind];
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/* If there is a usage error, write an appropriate message and exit */
1927c478bd9Sstevel@tonic-gate 	if (usageerr) {
1937c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
1947c478bd9Sstevel@tonic-gate 	    exit(EX_ERROR);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* Open the device-group file (if there's one to be opened) */
1987c478bd9Sstevel@tonic-gate 	if (!_opendgrptab("r")) {
1997c478bd9Sstevel@tonic-gate 	    if (dgrptab = _dgrptabpath()) {
2007c478bd9Sstevel@tonic-gate 		(void) snprintf(msg, sizeof(msg), M_DGRPTAB, dgrptab);
2017c478bd9Sstevel@tonic-gate 		exitcode = EX_NODGRPTAB;
2027c478bd9Sstevel@tonic-gate 		sev = MM_ERROR;
2037c478bd9Sstevel@tonic-gate 	    } else {
2047c478bd9Sstevel@tonic-gate 		(void) sprintf(msg, M_ERROR, errno);
2057c478bd9Sstevel@tonic-gate 		exitcode = EX_ERROR;
2067c478bd9Sstevel@tonic-gate 		sev = MM_HALT;
2077c478bd9Sstevel@tonic-gate 	    }
2087c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, sev, msg);
2097c478bd9Sstevel@tonic-gate 	    exit(exitcode);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	/*
2147c478bd9Sstevel@tonic-gate 	 * Get the list of devices associated with the device group.
2157c478bd9Sstevel@tonic-gate 	 * If we get one, write the list to the standard output.
2167c478bd9Sstevel@tonic-gate 	 * Otherwise, write an appropriate error message
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	exitcode = EX_OK;
2207c478bd9Sstevel@tonic-gate 	if (devices = listdgrp(dgroup)) {
2217c478bd9Sstevel@tonic-gate 	    for (pp = devices ; *pp ; pp++) (void) puts(*pp);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	else {
2247c478bd9Sstevel@tonic-gate 	    if (errno == EINVAL) {
2257c478bd9Sstevel@tonic-gate 		(void) snprintf(msg, sizeof(msg), M_NODGRP, dgroup);
2267c478bd9Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
2277c478bd9Sstevel@tonic-gate 		exitcode = EX_NODGROUP;
2287c478bd9Sstevel@tonic-gate 	    }
2297c478bd9Sstevel@tonic-gate 	    else {
2307c478bd9Sstevel@tonic-gate 		(void) sprintf(msg, M_ERROR, errno);
2317c478bd9Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_HALT, msg);
2327c478bd9Sstevel@tonic-gate 		exitcode = EX_ERROR;
2337c478bd9Sstevel@tonic-gate 	    }
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/* Finished (now wasn't that special?) */
2377c478bd9Sstevel@tonic-gate 	return(exitcode);
2387c478bd9Sstevel@tonic-gate }
239