xref: /illumos-gate/usr/src/cmd/devmgmt/cmds/devattr.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  *  devattr.c
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *  Contains the following:
347c478bd9Sstevel@tonic-gate  *	devattr		Command that returns [specific] attributes for
357c478bd9Sstevel@tonic-gate  *			a device.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  *  devattr [-v] device [attr [...]]
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *	This command searches the device table file for the device specified.
42*2a8bcb4eSToomas Soome  *	If it finds the device (matched either by alias or major and minor
43*2a8bcb4eSToomas Soome  *	device number), it extracts the attribute(s) specified and writes
447c478bd9Sstevel@tonic-gate  *	that value to the standard output stream (stdout).
457c478bd9Sstevel@tonic-gate  *
46*2a8bcb4eSToomas Soome  *	The command writes the values of the attributes to stdout one per
47*2a8bcb4eSToomas Soome  *	line, in the order that they were requested.  If the -v option is
487c478bd9Sstevel@tonic-gate  *	requested, it writes the attributes in the form <attr>='<value>' where
49*2a8bcb4eSToomas Soome  *	<attr> is the name of the attribute and <value> is the value of that
507c478bd9Sstevel@tonic-gate  *	attribute.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *  Returns:
537c478bd9Sstevel@tonic-gate  *	0	The command succeeded
547c478bd9Sstevel@tonic-gate  *	1	The command syntax is incorrect,
557c478bd9Sstevel@tonic-gate  *		An invalid option was used,
567c478bd9Sstevel@tonic-gate  *		An internal error occurred that prevented completion
577c478bd9Sstevel@tonic-gate  *	2	The device table could not be opened for reading.
587c478bd9Sstevel@tonic-gate  *	3	The requested device was not found in the device table
597c478bd9Sstevel@tonic-gate  *	4	A requested attribute was not defined for the device
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include	<sys/types.h>
637c478bd9Sstevel@tonic-gate #include	<stdio.h>
647c478bd9Sstevel@tonic-gate #include	<string.h>
657c478bd9Sstevel@tonic-gate #include	<errno.h>
667c478bd9Sstevel@tonic-gate #include	<fmtmsg.h>
677c478bd9Sstevel@tonic-gate #include	<devmgmt.h>
687c478bd9Sstevel@tonic-gate #include	<devtab.h>
697c478bd9Sstevel@tonic-gate #include	<stdlib.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  *  Local constant definitions
747c478bd9Sstevel@tonic-gate  *	TRUE		Boolean TRUE
757c478bd9Sstevel@tonic-gate  *	FALSE		Boolean FALSE
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #ifndef	TRUE
797c478bd9Sstevel@tonic-gate #define	TRUE	1
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #ifndef	FALSE
837c478bd9Sstevel@tonic-gate #define	FALSE	0
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  *  Messages
887c478bd9Sstevel@tonic-gate  *	M_USAGE		Usage error
897c478bd9Sstevel@tonic-gate  *	M_ERROR		Unexpected internal error
907c478bd9Sstevel@tonic-gate  *	M_NODEV		Device not found in the device table
917c478bd9Sstevel@tonic-gate  *	M_NOATTR	Attribute not found
927c478bd9Sstevel@tonic-gate  *	M_DEVTAB	Can't open the device table
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	M_USAGE		"usage: devattr [-v] device [attribute [...]]"
967c478bd9Sstevel@tonic-gate #define	M_ERROR		"Internal error, errno=%d"
977c478bd9Sstevel@tonic-gate #define	M_NODEV		"Device not found in the device table: %s"
987c478bd9Sstevel@tonic-gate #define	M_NOATTR	"Attrubute not found: %s"
997c478bd9Sstevel@tonic-gate #define	M_DEVTAB	"Cannot open the device table: %s"
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 
102*2a8bcb4eSToomas Soome /*
1037c478bd9Sstevel@tonic-gate  * Exit codes:
1047c478bd9Sstevel@tonic-gate  *	EX_OK		All's well that ends well
1057c478bd9Sstevel@tonic-gate  *	EX_ERROR	Some problem caused termination
1067c478bd9Sstevel@tonic-gate  *	EX_DEVTAB	Device table could not be opened
1077c478bd9Sstevel@tonic-gate  *	EX_NODEV	The device wasn't found in the device table
1087c478bd9Sstevel@tonic-gate  *	EX_NOATTR	A requested attribute wasn't defined for the device
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #define	EX_OK 		0
1127c478bd9Sstevel@tonic-gate #define	EX_ERROR	1
1137c478bd9Sstevel@tonic-gate #define	EX_DEVTAB	2
1147c478bd9Sstevel@tonic-gate #define	EX_NODEV	3
1157c478bd9Sstevel@tonic-gate #define	EX_NOATTR	4
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  *  Macros
1207c478bd9Sstevel@tonic-gate  *	stdmsg(r,l,s,t)	    Standard Message Generator
1217c478bd9Sstevel@tonic-gate  *				r	Recoverability flag
1227c478bd9Sstevel@tonic-gate  *				l	Standard Label
1237c478bd9Sstevel@tonic-gate  *				s	Severity
1247c478bd9Sstevel@tonic-gate  *				t	Text
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #define	stdmsg(r,l,s,t)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  *  Local static data
1327c478bd9Sstevel@tonic-gate  *	lbl	Buffer for the command label (for messages)
1337c478bd9Sstevel@tonic-gate  *	txt		Buffer for the text of messages
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static char	lbl[MM_MXLABELLN+1];
1377c478bd9Sstevel@tonic-gate static char	txt[MM_MXTXTLN+1];
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  *  main()
1417c478bd9Sstevel@tonic-gate  *
142*2a8bcb4eSToomas Soome  *	Implements the command "devattr".   This function parses the command
143*2a8bcb4eSToomas Soome  *	line, then calls the devattr() function looking for the specified
1447c478bd9Sstevel@tonic-gate  *	device and the requested attribute.  It writes the information to
1457c478bd9Sstevel@tonic-gate  *	the standard output file in the requested format.
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  * Exits:
1487c478bd9Sstevel@tonic-gate  *	0	The command succeeded
1497c478bd9Sstevel@tonic-gate  *	1	The command syntax is incorrect,
1507c478bd9Sstevel@tonic-gate  *		An invalid option was used,
1517c478bd9Sstevel@tonic-gate  *		An internal error occurred that prevented completion
1527c478bd9Sstevel@tonic-gate  *	2	The device table could not be opened for reading.
1537c478bd9Sstevel@tonic-gate  *	3	The requested device was not found in the device table
1547c478bd9Sstevel@tonic-gate  *	4	A requested attribute was not defined for the device
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate 
157113f4232Sakaplan int
main(int argc,char * argv[])158113f4232Sakaplan main(int argc, char *argv[])
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/* Automatic data */
1627c478bd9Sstevel@tonic-gate 	char   *cmdname;		/* Pointer to command name */
1637c478bd9Sstevel@tonic-gate 	char   *device;			/* Pointer to device name */
1647c478bd9Sstevel@tonic-gate 	char   *attr;			/* Pointer to current attribute */
1657c478bd9Sstevel@tonic-gate 	char   *value;			/* Pointer to current attr value */
1667c478bd9Sstevel@tonic-gate 	char   *p;			/* Temporary character pointer */
1677c478bd9Sstevel@tonic-gate 	char  **argptr;			/* Pointer into argv[] list */
1687c478bd9Sstevel@tonic-gate 	int	syntaxerr;		/* TRUE if invalid option seen */
1697c478bd9Sstevel@tonic-gate 	int	noerr;			/* TRUE if all's well in processing */
1707c478bd9Sstevel@tonic-gate 	int	v_seen;			/* TRUE if -v is on the command-line */
1717c478bd9Sstevel@tonic-gate 	int	exitcode;		/* Value to return */
1727c478bd9Sstevel@tonic-gate 	int	severity;		/* Message severity */
1737c478bd9Sstevel@tonic-gate 	int	c;			/* Temp char value */
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 *  Parse the command-line.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	syntaxerr = FALSE;
1817c478bd9Sstevel@tonic-gate 	v_seen = FALSE;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* Extract options */
1847c478bd9Sstevel@tonic-gate 	opterr = FALSE;
1857c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "v")) != EOF) switch(c) {
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	    /* -v option:  No argument, may be seen only once */
1887c478bd9Sstevel@tonic-gate 	    case 'v':
1897c478bd9Sstevel@tonic-gate 		if (!v_seen) v_seen = TRUE;
1907c478bd9Sstevel@tonic-gate 		else syntaxerr = TRUE;
1917c478bd9Sstevel@tonic-gate 		break;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	    /* Unknown option */
1947c478bd9Sstevel@tonic-gate 	    default:
1957c478bd9Sstevel@tonic-gate 		syntaxerr = TRUE;
1967c478bd9Sstevel@tonic-gate 	    break;
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/* Build the command name */
2007c478bd9Sstevel@tonic-gate 	cmdname = argv[0];
2017c478bd9Sstevel@tonic-gate 	if ((p = strrchr(cmdname, '/')) != (char *) NULL) cmdname = p+1;
2027c478bd9Sstevel@tonic-gate 	(void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl));
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* Make only the text-component of messages appear (remove this in SVR4.1) */
2057c478bd9Sstevel@tonic-gate 	(void) putenv("MSGVERB=text");
2067c478bd9Sstevel@tonic-gate 
207*2a8bcb4eSToomas Soome 	/*
208*2a8bcb4eSToomas Soome 	 * Check for a usage error
2097c478bd9Sstevel@tonic-gate 	 *  - invalid option
2107c478bd9Sstevel@tonic-gate 	 *  - arg count < 2
211*2a8bcb4eSToomas Soome 	 *  - arg count < 3 && -v used
2127c478bd9Sstevel@tonic-gate 	 */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (syntaxerr || (argc < (optind+1))) {
2157c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
2167c478bd9Sstevel@tonic-gate 	    exit(EX_ERROR);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* Open the device file (if there's one to be opened) */
2207c478bd9Sstevel@tonic-gate 	if (!_opendevtab("r")) {
2217c478bd9Sstevel@tonic-gate 	    if (p = _devtabpath()) {
2227c478bd9Sstevel@tonic-gate 		(void) snprintf(txt, sizeof(txt), M_DEVTAB, p);
2237c478bd9Sstevel@tonic-gate 		exitcode = EX_DEVTAB;
2247c478bd9Sstevel@tonic-gate 		severity = MM_ERROR;
2257c478bd9Sstevel@tonic-gate 	    } else {
2267c478bd9Sstevel@tonic-gate 		(void) sprintf(txt, M_ERROR, errno);
2277c478bd9Sstevel@tonic-gate 		exitcode = EX_ERROR;
2287c478bd9Sstevel@tonic-gate 		severity = MM_HALT;
2297c478bd9Sstevel@tonic-gate 	    }
2307c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, severity, txt);
2317c478bd9Sstevel@tonic-gate 	    exit(exitcode);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 
235*2a8bcb4eSToomas Soome 	/*
236*2a8bcb4eSToomas Soome 	 *  Get the list of known attributes for the device.  This does
237*2a8bcb4eSToomas Soome 	 *  two things.  First, it verifies that the device is known in the
238*2a8bcb4eSToomas Soome 	 *  device table.  Second, it gets the attributes to list just in
239*2a8bcb4eSToomas Soome 	 *  case no attributes were specified.  Then, set a pointer to the
240*2a8bcb4eSToomas Soome 	 *  list of attributes to be extracted and listed...
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	device = argv[optind];
2447c478bd9Sstevel@tonic-gate 	if ((argptr = listdev(device)) == (char **) NULL) {
2457c478bd9Sstevel@tonic-gate 	    if (errno == ENODEV) {
2467c478bd9Sstevel@tonic-gate 		(void) snprintf(txt, sizeof(txt), M_NODEV, device);
2477c478bd9Sstevel@tonic-gate 		exitcode = EX_NODEV;
2487c478bd9Sstevel@tonic-gate 		severity = MM_ERROR;
2497c478bd9Sstevel@tonic-gate 	    } else {
2507c478bd9Sstevel@tonic-gate 		(void) sprintf(txt, M_ERROR, errno);
2517c478bd9Sstevel@tonic-gate 		exitcode = EX_ERROR;
2527c478bd9Sstevel@tonic-gate 		severity = MM_HALT;
2537c478bd9Sstevel@tonic-gate 	    }
2547c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, severity, txt);
2557c478bd9Sstevel@tonic-gate 	    exit(exitcode);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 	if (argc > (optind+1)) argptr = &argv[optind+1];
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/*
261*2a8bcb4eSToomas Soome 	 *  List attributes.  If a requested attribute is not defined,
262*2a8bcb4eSToomas Soome 	 *  list the value of that attribute as null.  (Using shell
2637c478bd9Sstevel@tonic-gate 	 *  variables as the model for this.)
2647c478bd9Sstevel@tonic-gate 	 */
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	exitcode = EX_OK;
2677c478bd9Sstevel@tonic-gate 	noerr = TRUE;
2687c478bd9Sstevel@tonic-gate 	while (noerr && ((attr = *argptr++) != (char *) NULL)) {
2697c478bd9Sstevel@tonic-gate 	    if (!(value = devattr(device, attr))) {
2707c478bd9Sstevel@tonic-gate 		if (errno == EINVAL) {
2717c478bd9Sstevel@tonic-gate 		    value = "";
2727c478bd9Sstevel@tonic-gate 		    (void) snprintf(txt, sizeof(txt), M_NOATTR, attr);
2737c478bd9Sstevel@tonic-gate 		    /* stdmsg(MM_RECOVER, lbl, MM_WARNING, txt); */
2747c478bd9Sstevel@tonic-gate 		    exitcode = EX_NOATTR;
2757c478bd9Sstevel@tonic-gate 		} else {
2767c478bd9Sstevel@tonic-gate 		    noerr = FALSE;
2777c478bd9Sstevel@tonic-gate 		    (void) sprintf(txt, M_ERROR, errno);
2787c478bd9Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, txt);
2797c478bd9Sstevel@tonic-gate 		    exitcode = EX_ERROR;
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 	    }
2827c478bd9Sstevel@tonic-gate 	    if (noerr && v_seen) {
2837c478bd9Sstevel@tonic-gate 		(void) fputs(attr, stdout);
2847c478bd9Sstevel@tonic-gate 		(void) fputs("='", stdout);
2857c478bd9Sstevel@tonic-gate 		for (p = value ; *p ; p++) {
2867c478bd9Sstevel@tonic-gate 		    (void) putc(*p, stdout);
2877c478bd9Sstevel@tonic-gate 		    if (*p == '\'') (void) fputs("\"'\"'", stdout);
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 		(void) fputs("'\n", stdout);
2907c478bd9Sstevel@tonic-gate 	    } else if (noerr) {
2917c478bd9Sstevel@tonic-gate 		(void) fputs(value, stdout);
2927c478bd9Sstevel@tonic-gate 		(void) putc('\n', stdout);
2937c478bd9Sstevel@tonic-gate 	    }
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	return (exitcode);
2977c478bd9Sstevel@tonic-gate }
298