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