xref: /illumos-gate/usr/src/cmd/devmgmt/cmds/getdev.c (revision 113f4232)
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  */
277c478bd9Sstevel@tonic-gate /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /*        All Rights Reserved   */
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  *  getdev.c
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  *  Contains
377c478bd9Sstevel@tonic-gate  *	getdev	Writes on the standard output stream a list of devices
387c478bd9Sstevel@tonic-gate  *		that match certain criteria.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate #include	<sys/types.h>
417c478bd9Sstevel@tonic-gate #include	<stdio.h>
427c478bd9Sstevel@tonic-gate #include	<errno.h>
437c478bd9Sstevel@tonic-gate #include	<stdlib.h>
447c478bd9Sstevel@tonic-gate #include	<string.h>
457c478bd9Sstevel@tonic-gate #include	<fmtmsg.h>
467c478bd9Sstevel@tonic-gate #include	<devmgmt.h>
477c478bd9Sstevel@tonic-gate #include	<devtab.h>
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  *  Local Definitions
527c478bd9Sstevel@tonic-gate  *	TRUE		Boolean TRUE value
537c478bd9Sstevel@tonic-gate  *	FALSE		Boolean FALSE value
547c478bd9Sstevel@tonic-gate  *	EX_OK		Exit Value if all went well
557c478bd9Sstevel@tonic-gate  *	EX_ERROR	Exit Value if an error occurred
567c478bd9Sstevel@tonic-gate  *	EX_DEVTAB	Exit Value if the device table couldn't be opened
587c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate #ifndef	TRUE
617c478bd9Sstevel@tonic-gate #define	TRUE		(1)
627c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate #ifndef	FALSE
657c478bd9Sstevel@tonic-gate #define FALSE		(0)
667c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate #define	EX_OK		0
697c478bd9Sstevel@tonic-gate #define	EX_ERROR	1
707c478bd9Sstevel@tonic-gate #define	EX_DEVTAB	2
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  *  Messages:
757c478bd9Sstevel@tonic-gate  *	M_USAGE		Usage error
767c478bd9Sstevel@tonic-gate  *	M_DEVTAB	Can't open the device table
777c478bd9Sstevel@tonic-gate  *	M_NODEV		Device not found in the device table
787c478bd9Sstevel@tonic-gate  *	M_ERROR		Unexpected or internal error
797c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate #define	M_USAGE		"usage: getdev [-ae] [criterion [...]] [device [...]]"
827c478bd9Sstevel@tonic-gate #define	M_DEVTAB	"Cannot open the device table: %s"
837c478bd9Sstevel@tonic-gate #define	M_NODEV		"Device not found in the device table: %s"
847c478bd9Sstevel@tonic-gate #define	M_ERROR		"Internal error, errno=%d"
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  *  Local (Static) Definitions and macros
897c478bd9Sstevel@tonic-gate  *	buildcriterialist()	Builds the criteria list from the command-line
907c478bd9Sstevel@tonic-gate  *	builddevlist()		Builds the device list from the command-line
917c478bd9Sstevel@tonic-gate  *	lbl			Buffer for the standard message label
927c478bd9Sstevel@tonic-gate  *	txt			Buffer for the standard message text
937c478bd9Sstevel@tonic-gate  *	stdmsg(r,l,s,t)		Write a standard message:
947c478bd9Sstevel@tonic-gate  *				    r	Recoverability flag
957c478bd9Sstevel@tonic-gate  *				    l	Standard label
967c478bd9Sstevel@tonic-gate  *				    s	Severity
977c478bd9Sstevel@tonic-gate  *				    t	Text
987c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate static	char  **buildcriterialist();
1017c478bd9Sstevel@tonic-gate static	char  **builddevlist();
1037c478bd9Sstevel@tonic-gate static	char	lbl[MM_MXLABELLN+1];
1047c478bd9Sstevel@tonic-gate static	char	txt[MM_MXTXTLN+1];
1067c478bd9Sstevel@tonic-gate #define	stdmsg(r,l,s,t)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  *  getdev [-ae] [criterion [...]] [device [...]]
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  *	This command generates a list of devices that match the
1127c478bd9Sstevel@tonic-gate  *	specified criteria.
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  *  Options:
1157c478bd9Sstevel@tonic-gate  *	-a		A device must meet all of the criteria to be
1167c478bd9Sstevel@tonic-gate  *			included in the generated list instead of just
1177c478bd9Sstevel@tonic-gate  *			one of the criteria (the "and" flag)
1187c478bd9Sstevel@tonic-gate  *	-e		Exclude the devices mentioned from the generated
1197c478bd9Sstevel@tonic-gate  *			list.  If this flag is omitted, the devices in the
1207c478bd9Sstevel@tonic-gate  *			list are selected from the devices mentioned.
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  *  Arguments:
1237c478bd9Sstevel@tonic-gate  *	criterion	An <attr><op><value> expression that describes
1247c478bd9Sstevel@tonic-gate  *			a device attribute.
1257c478bd9Sstevel@tonic-gate  *			<attr>	is a device attribute
1267c478bd9Sstevel@tonic-gate  *			<op> 	may be = != : !: indicating equals, does not
1277c478bd9Sstevel@tonic-gate  *				equal, is defined, and is not defined
1287c478bd9Sstevel@tonic-gate  *				respectively
1297c478bd9Sstevel@tonic-gate  *			<value>	is the attribute value.  Currently, the only
1307c478bd9Sstevel@tonic-gate  *				value supported for the : and !: operators
1317c478bd9Sstevel@tonic-gate  *				is *
1327c478bd9Sstevel@tonic-gate  *	device		A device to select for or exclude from the generated
1337c478bd9Sstevel@tonic-gate  *			list
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  *  Exit values:
1367c478bd9Sstevel@tonic-gate  *	EX_OK		All went well
1377c478bd9Sstevel@tonic-gate  *	EX_ERROR	An error (syntax, internal, or resource) occurred
1387c478bd9Sstevel@tonic-gate  *	EX_DEVTAB	The device-table could not be opened for reading
1397c478bd9Sstevel@tonic-gate  */
141*113f4232Sakaplan int
main(int argc,char ** argv)142*113f4232Sakaplan main(int argc, char **argv)
1437c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	/*
1467c478bd9Sstevel@tonic-gate 	 *  Automatic data
1477c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 	char	      **arglist;	/* List of arguments */
1507c478bd9Sstevel@tonic-gate 	char	      **criterialist;	/* List of criteria */
1517c478bd9Sstevel@tonic-gate 	char	      **devicelist;	/* List of devices to search/ignore */
1527c478bd9Sstevel@tonic-gate 	char	      **fitdevlist;	/* List of devices that fit criteria */
1537c478bd9Sstevel@tonic-gate 	char	       *cmdname;	/* Simple command name */
1547c478bd9Sstevel@tonic-gate 	char	       *device;		/* Device name in the list */
1557c478bd9Sstevel@tonic-gate 	char	       *devtab;		/* The device table name */
1567c478bd9Sstevel@tonic-gate 	int		exitcode;	/* Value to return to the caller */
1577c478bd9Sstevel@tonic-gate 	int		sev;		/* Message severity */
1587c478bd9Sstevel@tonic-gate 	int		optchar;	/* Option character (from getopt()) */
1597c478bd9Sstevel@tonic-gate 	int		andflag;	/* TRUE if criteria are to be anded */
1607c478bd9Sstevel@tonic-gate 	int		excludeflag;	/* TRUE if exclude "devices" lists */
1617c478bd9Sstevel@tonic-gate 	int		options;	/* Options to pass to getdev() */
1627c478bd9Sstevel@tonic-gate 	int		usageerr;	/* TRUE if syntax error */
1657c478bd9Sstevel@tonic-gate 	/* Build the message label from the (simple) command name */
1667c478bd9Sstevel@tonic-gate 	if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++;
1677c478bd9Sstevel@tonic-gate 	else cmdname = argv[0];
1687c478bd9Sstevel@tonic-gate 	(void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl));
1707c478bd9Sstevel@tonic-gate 	/* Write text-component of messages only (goes away in SVR4.1) */
1717c478bd9Sstevel@tonic-gate 	(void) putenv("MSGVERB=text");
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 *  Parse the command line:
1757c478bd9Sstevel@tonic-gate 	 *	- Options
1767c478bd9Sstevel@tonic-gate 	 *	- Selection criteria
1777c478bd9Sstevel@tonic-gate 	 *	- Devices to include or exclude
1787c478bd9Sstevel@tonic-gate 	 */
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 *  Extract options from the command line
1827c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	/* Initializations */
1857c478bd9Sstevel@tonic-gate 	andflag = FALSE;	/* No -a -- Or criteria data */
1867c478bd9Sstevel@tonic-gate 	excludeflag = FALSE;	/* No -e -- Include only mentioned devices */
1877c478bd9Sstevel@tonic-gate 	usageerr = FALSE;	/* No errors on the command line (yet) */
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 *  Loop until all of the command line options have been parced
1917c478bd9Sstevel@tonic-gate 	 */
1927c478bd9Sstevel@tonic-gate 	opterr = FALSE;			/* Don't let getopt() write messages */
1937c478bd9Sstevel@tonic-gate 	while ((optchar = getopt(argc, argv, "ae")) != EOF) switch (optchar) {
1957c478bd9Sstevel@tonic-gate 	/* -a  List devices that fit all of the criteria listed */
1967c478bd9Sstevel@tonic-gate 	case 'a':
1977c478bd9Sstevel@tonic-gate 	    if (andflag) usageerr = TRUE;
1987c478bd9Sstevel@tonic-gate 	    else andflag = TRUE;
1997c478bd9Sstevel@tonic-gate 	    break;
2017c478bd9Sstevel@tonic-gate 	/* -e  Exclude those devices mentioned on the command line */
2027c478bd9Sstevel@tonic-gate 	case 'e':
2037c478bd9Sstevel@tonic-gate 	    if (excludeflag) usageerr = TRUE;
2047c478bd9Sstevel@tonic-gate 	    else excludeflag = TRUE;
2057c478bd9Sstevel@tonic-gate 	    break;
2077c478bd9Sstevel@tonic-gate 	/* Default case -- command usage error */
2087c478bd9Sstevel@tonic-gate 	case '?':
2097c478bd9Sstevel@tonic-gate 	default:
2107c478bd9Sstevel@tonic-gate 	    usageerr = TRUE;
2117c478bd9Sstevel@tonic-gate 	    break;
2127c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 	/* If there is a usage error, write an appropriate message and exit */
2157c478bd9Sstevel@tonic-gate 	if (usageerr) {
2167c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
2177c478bd9Sstevel@tonic-gate 	    exit(EX_ERROR);
2187c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 	/* Open the device file (if there's one to be opened) */
2217c478bd9Sstevel@tonic-gate 	if (!_opendevtab("r")) {
2227c478bd9Sstevel@tonic-gate 	    if (devtab = _devtabpath()) {
2237c478bd9Sstevel@tonic-gate 		(void) snprintf(txt, sizeof(txt), M_DEVTAB, devtab);
2247c478bd9Sstevel@tonic-gate 		sev = MM_ERROR;
2257c478bd9Sstevel@tonic-gate 		exitcode = EX_DEVTAB;
2267c478bd9Sstevel@tonic-gate 	    } else {
2277c478bd9Sstevel@tonic-gate 		(void) sprintf(txt, M_ERROR, errno);
2287c478bd9Sstevel@tonic-gate 		sev = MM_HALT;
2297c478bd9Sstevel@tonic-gate 		exitcode = EX_ERROR;
2307c478bd9Sstevel@tonic-gate 	    }
2317c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, sev, txt);
2327c478bd9Sstevel@tonic-gate 	    exit(exitcode);
2337c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 	/* Build the list of criteria and devices */
2367c478bd9Sstevel@tonic-gate 	arglist = argv + optind;
2377c478bd9Sstevel@tonic-gate 	criterialist = buildcriterialist(arglist);
2387c478bd9Sstevel@tonic-gate 	devicelist = builddevlist(arglist);
2397c478bd9Sstevel@tonic-gate 	options = (excludeflag?DTAB_EXCLUDEFLAG:0)|(andflag?DTAB_ANDCRITERIA:0);
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 *  Get the list of devices that meets the criteria requested.  If we
2437c478bd9Sstevel@tonic-gate 	 *  got a list (that might be empty), write that list to the standard
2447c478bd9Sstevel@tonic-gate 	 *  output file (stdout).
2457c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	exitcode = 0;
2487c478bd9Sstevel@tonic-gate 	if (!(fitdevlist = getdev(devicelist, criterialist, options))) {
2497c478bd9Sstevel@tonic-gate 	    exitcode = 1;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	else for (device = *fitdevlist++ ; device ; device = *fitdevlist++)
2527c478bd9Sstevel@tonic-gate 		(void) puts(device);
2547c478bd9Sstevel@tonic-gate 	/* Finished */
2557c478bd9Sstevel@tonic-gate 	return(exitcode);
2567c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  *  char **buildcriterialist(arglist)
2607c478bd9Sstevel@tonic-gate  *	char  **arglist
2617c478bd9Sstevel@tonic-gate  *
2627c478bd9Sstevel@tonic-gate  *	Build a list of pointers to the criterion on the command-line
2637c478bd9Sstevel@tonic-gate  *
2647c478bd9Sstevel@tonic-gate  *  Arguments:
2657c478bd9Sstevel@tonic-gate  *	arglist		The list of arguments on the command-line
2667c478bd9Sstevel@tonic-gate  *
2677c478bd9Sstevel@tonic-gate  *  Returns:  char **
2687c478bd9Sstevel@tonic-gate  *	The address of the first item of the list of criterion on the
2697c478bd9Sstevel@tonic-gate  *	command-line.  This is a pointer to malloc()ed space.
2707c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate static char  **
buildcriterialist(arglist)2737c478bd9Sstevel@tonic-gate buildcriterialist(arglist)
2747c478bd9Sstevel@tonic-gate 	char  **arglist;	/* Pointer to the list of argument pointers */
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 *  Automatic data
2787c478bd9Sstevel@tonic-gate 	 */
2807c478bd9Sstevel@tonic-gate 	char  **pp;			/* Pointer to a criteria */
2817c478bd9Sstevel@tonic-gate 	char  **allocbuf;		/* Pointer to the allocated data */
2827c478bd9Sstevel@tonic-gate 	int	ncriteria;		/* Number of criteria found */
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 *  Search the argument list, looking for the end of the list or
2877c478bd9Sstevel@tonic-gate 	 *  the first thing that's not a criteria.  (A criteria is a
2887c478bd9Sstevel@tonic-gate 	 *  character-string that contains a colon (':') or an equal-sign ('=')
2897c478bd9Sstevel@tonic-gate 	 */
2917c478bd9Sstevel@tonic-gate 	pp = arglist;
2927c478bd9Sstevel@tonic-gate 	ncriteria = 0;
2937c478bd9Sstevel@tonic-gate 	while (*pp && (strchr(*pp, '=') || strchr(*pp, ':'))) {
2947c478bd9Sstevel@tonic-gate 	    ncriteria++;
2957c478bd9Sstevel@tonic-gate 	    pp++;
2967c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	if (ncriteria > 0) {
3007c478bd9Sstevel@tonic-gate 	    /* Allocate space for the list of criteria pointers */
3017c478bd9Sstevel@tonic-gate 	    allocbuf = (char **) malloc((ncriteria+1)*sizeof(char **));
3037c478bd9Sstevel@tonic-gate 	    /*
3047c478bd9Sstevel@tonic-gate 	     *  Build the list of criteria arguments
3057c478bd9Sstevel@tonic-gate 	     */
3067c478bd9Sstevel@tonic-gate 	    pp = allocbuf;	/* Beginning of the list */
3077c478bd9Sstevel@tonic-gate 	    while (*arglist &&			/* If there's more to do ... */
3087c478bd9Sstevel@tonic-gate 		   (strchr(*arglist, '=') ||	/* and it's a = criterion ... */
3097c478bd9Sstevel@tonic-gate 		    strchr(*arglist, ':')))	/* or it's a : criterion ... */
3107c478bd9Sstevel@tonic-gate 			*pp++ = *arglist++;	/* Include it in the list */
3117c478bd9Sstevel@tonic-gate 	    *pp = (char *) NULL;	/* Terminate the list */
3137c478bd9Sstevel@tonic-gate 	} else allocbuf = (char **) NULL;	/* NO criteria */
3167c478bd9Sstevel@tonic-gate 	return (allocbuf);
3177c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  *  char **builddevlist(arglist)
3217c478bd9Sstevel@tonic-gate  *	char  **arglist
3227c478bd9Sstevel@tonic-gate  *
3237c478bd9Sstevel@tonic-gate  *	Builds a list of pointers to the devices mentioned on the command-
3247c478bd9Sstevel@tonic-gate  *	line and returns the address of that list.
3257c478bd9Sstevel@tonic-gate  *
3267c478bd9Sstevel@tonic-gate  *  Arguments:
3277c478bd9Sstevel@tonic-gate  *	arglist		The address of the list of arguments to the
3287c478bd9Sstevel@tonic-gate  *			getdev command.
3297c478bd9Sstevel@tonic-gate  *
3307c478bd9Sstevel@tonic-gate  *  Returns:  char **
3317c478bd9Sstevel@tonic-gate  *	A pointer to the first item in the list of pointers to devices
3327c478bd9Sstevel@tonic-gate  *	specified on the command-line
3337c478bd9Sstevel@tonic-gate  */
3357c478bd9Sstevel@tonic-gate static char  **
builddevlist(arglist)3367c478bd9Sstevel@tonic-gate builddevlist(arglist)
3377c478bd9Sstevel@tonic-gate 	char  **arglist;	/* Pointer to the list of pointers to args */
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 *  Automatic data
3417c478bd9Sstevel@tonic-gate 	 */
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 *  Search the argument list, looking for the end of the list or the
3457c478bd9Sstevel@tonic-gate 	 *  first thing that's not a criteria.  It is the first device in the
3467c478bd9Sstevel@tonic-gate 	 *  list of devices (if any).
3477c478bd9Sstevel@tonic-gate 	 */
3497c478bd9Sstevel@tonic-gate 	while (*arglist && (strchr(*arglist, '=') || strchr(*arglist, ':'))) arglist++;
3517c478bd9Sstevel@tonic-gate 	/* Return a pointer to the argument list. */
3527c478bd9Sstevel@tonic-gate 	return(*arglist?arglist:(char **) NULL);
3537c478bd9Sstevel@tonic-gate }