xref: /illumos-gate/usr/src/cmd/devmgmt/cmds/listdgrp.c (revision 2a8bcb4e)
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 /*
23113f4232Sakaplan  * 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  *  listdgrp.c
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *  Contains
347c478bd9Sstevel@tonic-gate  *	listdgrp	Writes on the standard output stream a list of devices
357c478bd9Sstevel@tonic-gate  *			that belong to the specified device group
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include	<sys/types.h>
397c478bd9Sstevel@tonic-gate #include	<stdio.h>
407c478bd9Sstevel@tonic-gate #include	<stdlib.h>
417c478bd9Sstevel@tonic-gate #include	<string.h>
427c478bd9Sstevel@tonic-gate #include	<errno.h>
437c478bd9Sstevel@tonic-gate #include	<devmgmt.h>
447c478bd9Sstevel@tonic-gate #include	<devtab.h>
457c478bd9Sstevel@tonic-gate #include	<fmtmsg.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  *  Local Definitions
507c478bd9Sstevel@tonic-gate  *	TRUE		Boolean TRUE value (if not already defined)
517c478bd9Sstevel@tonic-gate  *	FALSE		Boolean not-TRUE value (if not already defined)
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifndef	TRUE
557c478bd9Sstevel@tonic-gate #define	TRUE		('t')
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #ifndef	FALSE
597c478bd9Sstevel@tonic-gate #define	FALSE		(0)
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  *  Messages:
647c478bd9Sstevel@tonic-gate  *	M_USAGE		Command usage error
657c478bd9Sstevel@tonic-gate  *	M_NODGRP	Device group not found
667c478bd9Sstevel@tonic-gate  *	M_DGRPTAB	Device-group table not found
677c478bd9Sstevel@tonic-gate  *	M_ERROR		Internal error
687c478bd9Sstevel@tonic-gate  */
69*2a8bcb4eSToomas Soome 
707c478bd9Sstevel@tonic-gate #define	M_USAGE		"usage: listdgrp dgroup"
717c478bd9Sstevel@tonic-gate #define	M_NODGRP	"Device group not found: %s"
727c478bd9Sstevel@tonic-gate #define	M_DGRPTAB	"Cannot open device-group table: %s"
737c478bd9Sstevel@tonic-gate #define	M_ERROR		"Internal error, errno=%d"
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  *  Exit codes
787c478bd9Sstevel@tonic-gate  *	EX_OK		Exiting okay, no problem
797c478bd9Sstevel@tonic-gate  *	EX_ERROR	Some problem with the command
807c478bd9Sstevel@tonic-gate  *	EX_NODGRPTAB	Device group table could not be opened
817c478bd9Sstevel@tonic-gate  *	EX_NODGROUP	Device group couldn't be found
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	EX_OK		0
857c478bd9Sstevel@tonic-gate #define	EX_ERROR	1
867c478bd9Sstevel@tonic-gate #define	EX_NODGRPTAB	2
877c478bd9Sstevel@tonic-gate #define	EX_NODGROUP	3
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  *  Macros
92*2a8bcb4eSToomas Soome  *	stdmsg(r,l,s,t)	    Write a message in standard format
937c478bd9Sstevel@tonic-gate  *				r	Recoverability flag
947c478bd9Sstevel@tonic-gate  *				l	Label
957c478bd9Sstevel@tonic-gate  *				s	Severity
967c478bd9Sstevel@tonic-gate  *				t	Tag
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	stdmsg(r,l,s,t)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  *  Global Variables
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  *  Static Variables
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  *	lbl	Buffer for the message label
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static	char	lbl[MM_MXLABELLN+1];
1137c478bd9Sstevel@tonic-gate static	char	msg[MM_MXTXTLN+1];
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  *  listdgrp <dgroup>
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  *	List the devices that belong to the device group <dgroup>.
1197c478bd9Sstevel@tonic-gate  *	It writes the list to the standard output file (stdout)
1207c478bd9Sstevel@tonic-gate  *	in a new-line list.
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  *  Returns:
1237c478bd9Sstevel@tonic-gate  *	0	Ok
1247c478bd9Sstevel@tonic-gate  *	1	Syntax or other error
1257c478bd9Sstevel@tonic-gate  *	2	Device table can't be opened
1267c478bd9Sstevel@tonic-gate  *	3	Device group doesn't exist
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate 
129113f4232Sakaplan int
main(int argc,char ** argv)130113f4232Sakaplan main(int argc, char **argv)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 
133*2a8bcb4eSToomas Soome 	/*
1347c478bd9Sstevel@tonic-gate 	 *  Automatic data
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	char	      **devices;	/* List of devices in the group */
1387c478bd9Sstevel@tonic-gate 	char	      **pp;		/* Running pointer to device names */
1397c478bd9Sstevel@tonic-gate 	char	       *cmdname;	/* Simple command name */
1407c478bd9Sstevel@tonic-gate 	char	       *dgrptab;	/* The device-group table name */
1417c478bd9Sstevel@tonic-gate 	char	       *dgroup;		/* Device group to list */
1427c478bd9Sstevel@tonic-gate 	int		exitcode;	/* Value to return to the caller */
1437c478bd9Sstevel@tonic-gate 	int		sev;		/* Message severity */
1447c478bd9Sstevel@tonic-gate 	int		optchar;	/* Option char (from getopt()) */
1457c478bd9Sstevel@tonic-gate 	int		usageerr;	/* TRUE if syntax error on command */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/* Build the message label from the (simple) command name */
1497c478bd9Sstevel@tonic-gate 	if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++;
1507c478bd9Sstevel@tonic-gate 	else cmdname = argv[0];
1517c478bd9Sstevel@tonic-gate 	(void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl));
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/* Only write the text component of a message (this goes away in SVR4.1) */
1547c478bd9Sstevel@tonic-gate 	(void) putenv("MSGVERB=text");
1557c478bd9Sstevel@tonic-gate 
156*2a8bcb4eSToomas Soome 	/*
1577c478bd9Sstevel@tonic-gate 	 *  Parse the command line:
1587c478bd9Sstevel@tonic-gate 	 *	- Options
1597c478bd9Sstevel@tonic-gate 	 *	- Device group to display
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 
162*2a8bcb4eSToomas Soome 	/*
163*2a8bcb4eSToomas Soome 	 *  Extract options from the command line
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* Initializations */
1677c478bd9Sstevel@tonic-gate 	usageerr = FALSE;	/* No errors on the command line (yet) */
1687c478bd9Sstevel@tonic-gate 
169*2a8bcb4eSToomas Soome 	/*
170*2a8bcb4eSToomas Soome 	 *  Loop until all of the command line options have been parced
171*2a8bcb4eSToomas Soome 	 *  (and don't let getopt() write messages)
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate 
174*2a8bcb4eSToomas Soome 	opterr = FALSE;
1757c478bd9Sstevel@tonic-gate 	while ((optchar = getopt(argc, argv, "")) != EOF) switch (optchar) {
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* Default case -- command usage error */
1787c478bd9Sstevel@tonic-gate 	case '?':
1797c478bd9Sstevel@tonic-gate 	default:
1807c478bd9Sstevel@tonic-gate 	    usageerr = TRUE;
1817c478bd9Sstevel@tonic-gate 	    break;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/* Check the argument count and extract the device group name */
1857c478bd9Sstevel@tonic-gate 	if (usageerr || (optind != argc-1)) usageerr = TRUE;
1867c478bd9Sstevel@tonic-gate 	else dgroup = argv[optind];
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/* If there is a usage error, write an appropriate message and exit */
1897c478bd9Sstevel@tonic-gate 	if (usageerr) {
1907c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
1917c478bd9Sstevel@tonic-gate 	    exit(EX_ERROR);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/* Open the device-group file (if there's one to be opened) */
1957c478bd9Sstevel@tonic-gate 	if (!_opendgrptab("r")) {
1967c478bd9Sstevel@tonic-gate 	    if (dgrptab = _dgrptabpath()) {
1977c478bd9Sstevel@tonic-gate 		(void) snprintf(msg, sizeof(msg), M_DGRPTAB, dgrptab);
1987c478bd9Sstevel@tonic-gate 		exitcode = EX_NODGRPTAB;
1997c478bd9Sstevel@tonic-gate 		sev = MM_ERROR;
2007c478bd9Sstevel@tonic-gate 	    } else {
2017c478bd9Sstevel@tonic-gate 		(void) sprintf(msg, M_ERROR, errno);
2027c478bd9Sstevel@tonic-gate 		exitcode = EX_ERROR;
2037c478bd9Sstevel@tonic-gate 		sev = MM_HALT;
2047c478bd9Sstevel@tonic-gate 	    }
2057c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, sev, msg);
2067c478bd9Sstevel@tonic-gate 	    exit(exitcode);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * Get the list of devices associated with the device group.
2127c478bd9Sstevel@tonic-gate 	 * If we get one, write the list to the standard output.
2137c478bd9Sstevel@tonic-gate 	 * Otherwise, write an appropriate error message
2147c478bd9Sstevel@tonic-gate 	 */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	exitcode = EX_OK;
2177c478bd9Sstevel@tonic-gate 	if (devices = listdgrp(dgroup)) {
2187c478bd9Sstevel@tonic-gate 	    for (pp = devices ; *pp ; pp++) (void) puts(*pp);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 	else {
2217c478bd9Sstevel@tonic-gate 	    if (errno == EINVAL) {
2227c478bd9Sstevel@tonic-gate 		(void) snprintf(msg, sizeof(msg), M_NODGRP, dgroup);
2237c478bd9Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
2247c478bd9Sstevel@tonic-gate 		exitcode = EX_NODGROUP;
2257c478bd9Sstevel@tonic-gate 	    }
2267c478bd9Sstevel@tonic-gate 	    else {
2277c478bd9Sstevel@tonic-gate 		(void) sprintf(msg, M_ERROR, errno);
2287c478bd9Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_HALT, msg);
2297c478bd9Sstevel@tonic-gate 		exitcode = EX_ERROR;
2307c478bd9Sstevel@tonic-gate 	    }
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/* Finished (now wasn't that special?) */
2347c478bd9Sstevel@tonic-gate 	return(exitcode);
2357c478bd9Sstevel@tonic-gate }
236