10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*644Sakaplan * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate 310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 320Sstevel@tonic-gate 330Sstevel@tonic-gate /* 340Sstevel@tonic-gate * devattr.c 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * Contains the following: 370Sstevel@tonic-gate * devattr Command that returns [specific] attributes for 380Sstevel@tonic-gate * a device. 390Sstevel@tonic-gate */ 400Sstevel@tonic-gate 410Sstevel@tonic-gate /* 420Sstevel@tonic-gate * devattr [-v] device [attr [...]] 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * This command searches the device table file for the device specified. 450Sstevel@tonic-gate * If it finds the device (matched either by alias or major and minor 460Sstevel@tonic-gate * device number), it extracts the attribute(s) specified and writes 470Sstevel@tonic-gate * that value to the standard output stream (stdout). 480Sstevel@tonic-gate * 490Sstevel@tonic-gate * The command writes the values of the attributes to stdout one per 500Sstevel@tonic-gate * line, in the order that they were requested. If the -v option is 510Sstevel@tonic-gate * requested, it writes the attributes in the form <attr>='<value>' where 520Sstevel@tonic-gate * <attr> is the name of the attribute and <value> is the value of that 530Sstevel@tonic-gate * attribute. 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * Returns: 560Sstevel@tonic-gate * 0 The command succeeded 570Sstevel@tonic-gate * 1 The command syntax is incorrect, 580Sstevel@tonic-gate * An invalid option was used, 590Sstevel@tonic-gate * An internal error occurred that prevented completion 600Sstevel@tonic-gate * 2 The device table could not be opened for reading. 610Sstevel@tonic-gate * 3 The requested device was not found in the device table 620Sstevel@tonic-gate * 4 A requested attribute was not defined for the device 630Sstevel@tonic-gate */ 640Sstevel@tonic-gate 650Sstevel@tonic-gate #include <sys/types.h> 660Sstevel@tonic-gate #include <stdio.h> 670Sstevel@tonic-gate #include <string.h> 680Sstevel@tonic-gate #include <errno.h> 690Sstevel@tonic-gate #include <fmtmsg.h> 700Sstevel@tonic-gate #include <devmgmt.h> 710Sstevel@tonic-gate #include <devtab.h> 720Sstevel@tonic-gate #include <stdlib.h> 730Sstevel@tonic-gate 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 760Sstevel@tonic-gate * Local constant definitions 770Sstevel@tonic-gate * TRUE Boolean TRUE 780Sstevel@tonic-gate * FALSE Boolean FALSE 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate 810Sstevel@tonic-gate #ifndef TRUE 820Sstevel@tonic-gate #define TRUE 1 830Sstevel@tonic-gate #endif 840Sstevel@tonic-gate 850Sstevel@tonic-gate #ifndef FALSE 860Sstevel@tonic-gate #define FALSE 0 870Sstevel@tonic-gate #endif 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * Messages 910Sstevel@tonic-gate * M_USAGE Usage error 920Sstevel@tonic-gate * M_ERROR Unexpected internal error 930Sstevel@tonic-gate * M_NODEV Device not found in the device table 940Sstevel@tonic-gate * M_NOATTR Attribute not found 950Sstevel@tonic-gate * M_DEVTAB Can't open the device table 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate 980Sstevel@tonic-gate #define M_USAGE "usage: devattr [-v] device [attribute [...]]" 990Sstevel@tonic-gate #define M_ERROR "Internal error, errno=%d" 1000Sstevel@tonic-gate #define M_NODEV "Device not found in the device table: %s" 1010Sstevel@tonic-gate #define M_NOATTR "Attrubute not found: %s" 1020Sstevel@tonic-gate #define M_DEVTAB "Cannot open the device table: %s" 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate /* 1060Sstevel@tonic-gate * Exit codes: 1070Sstevel@tonic-gate * EX_OK All's well that ends well 1080Sstevel@tonic-gate * EX_ERROR Some problem caused termination 1090Sstevel@tonic-gate * EX_DEVTAB Device table could not be opened 1100Sstevel@tonic-gate * EX_NODEV The device wasn't found in the device table 1110Sstevel@tonic-gate * EX_NOATTR A requested attribute wasn't defined for the device 1120Sstevel@tonic-gate */ 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate #define EX_OK 0 1150Sstevel@tonic-gate #define EX_ERROR 1 1160Sstevel@tonic-gate #define EX_DEVTAB 2 1170Sstevel@tonic-gate #define EX_NODEV 3 1180Sstevel@tonic-gate #define EX_NOATTR 4 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * Macros 1230Sstevel@tonic-gate * stdmsg(r,l,s,t) Standard Message Generator 1240Sstevel@tonic-gate * r Recoverability flag 1250Sstevel@tonic-gate * l Standard Label 1260Sstevel@tonic-gate * s Severity 1270Sstevel@tonic-gate * t Text 1280Sstevel@tonic-gate */ 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate #define stdmsg(r,l,s,t) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG) 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* 1340Sstevel@tonic-gate * Local static data 1350Sstevel@tonic-gate * lbl Buffer for the command label (for messages) 1360Sstevel@tonic-gate * txt Buffer for the text of messages 1370Sstevel@tonic-gate */ 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate static char lbl[MM_MXLABELLN+1]; 1400Sstevel@tonic-gate static char txt[MM_MXTXTLN+1]; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* 1430Sstevel@tonic-gate * main() 1440Sstevel@tonic-gate * 1450Sstevel@tonic-gate * Implements the command "devattr". This function parses the command 1460Sstevel@tonic-gate * line, then calls the devattr() function looking for the specified 1470Sstevel@tonic-gate * device and the requested attribute. It writes the information to 1480Sstevel@tonic-gate * the standard output file in the requested format. 1490Sstevel@tonic-gate * 1500Sstevel@tonic-gate * Exits: 1510Sstevel@tonic-gate * 0 The command succeeded 1520Sstevel@tonic-gate * 1 The command syntax is incorrect, 1530Sstevel@tonic-gate * An invalid option was used, 1540Sstevel@tonic-gate * An internal error occurred that prevented completion 1550Sstevel@tonic-gate * 2 The device table could not be opened for reading. 1560Sstevel@tonic-gate * 3 The requested device was not found in the device table 1570Sstevel@tonic-gate * 4 A requested attribute was not defined for the device 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate 160*644Sakaplan int 161*644Sakaplan main(int argc, char *argv[]) 1620Sstevel@tonic-gate { 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate /* Automatic data */ 1650Sstevel@tonic-gate char *cmdname; /* Pointer to command name */ 1660Sstevel@tonic-gate char *device; /* Pointer to device name */ 1670Sstevel@tonic-gate char *attr; /* Pointer to current attribute */ 1680Sstevel@tonic-gate char *value; /* Pointer to current attr value */ 1690Sstevel@tonic-gate char *p; /* Temporary character pointer */ 1700Sstevel@tonic-gate char **argptr; /* Pointer into argv[] list */ 1710Sstevel@tonic-gate int syntaxerr; /* TRUE if invalid option seen */ 1720Sstevel@tonic-gate int noerr; /* TRUE if all's well in processing */ 1730Sstevel@tonic-gate int v_seen; /* TRUE if -v is on the command-line */ 1740Sstevel@tonic-gate int exitcode; /* Value to return */ 1750Sstevel@tonic-gate int severity; /* Message severity */ 1760Sstevel@tonic-gate int c; /* Temp char value */ 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* 1800Sstevel@tonic-gate * Parse the command-line. 1810Sstevel@tonic-gate */ 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate syntaxerr = FALSE; 1840Sstevel@tonic-gate v_seen = FALSE; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* Extract options */ 1870Sstevel@tonic-gate opterr = FALSE; 1880Sstevel@tonic-gate while ((c = getopt(argc, argv, "v")) != EOF) switch(c) { 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* -v option: No argument, may be seen only once */ 1910Sstevel@tonic-gate case 'v': 1920Sstevel@tonic-gate if (!v_seen) v_seen = TRUE; 1930Sstevel@tonic-gate else syntaxerr = TRUE; 1940Sstevel@tonic-gate break; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* Unknown option */ 1970Sstevel@tonic-gate default: 1980Sstevel@tonic-gate syntaxerr = TRUE; 1990Sstevel@tonic-gate break; 2000Sstevel@tonic-gate } 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* Build the command name */ 2030Sstevel@tonic-gate cmdname = argv[0]; 2040Sstevel@tonic-gate if ((p = strrchr(cmdname, '/')) != (char *) NULL) cmdname = p+1; 2050Sstevel@tonic-gate (void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl)); 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* Make only the text-component of messages appear (remove this in SVR4.1) */ 2080Sstevel@tonic-gate (void) putenv("MSGVERB=text"); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /* 2110Sstevel@tonic-gate * Check for a usage error 2120Sstevel@tonic-gate * - invalid option 2130Sstevel@tonic-gate * - arg count < 2 2140Sstevel@tonic-gate * - arg count < 3 && -v used 2150Sstevel@tonic-gate */ 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate if (syntaxerr || (argc < (optind+1))) { 2180Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE); 2190Sstevel@tonic-gate exit(EX_ERROR); 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate /* Open the device file (if there's one to be opened) */ 2230Sstevel@tonic-gate if (!_opendevtab("r")) { 2240Sstevel@tonic-gate if (p = _devtabpath()) { 2250Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_DEVTAB, p); 2260Sstevel@tonic-gate exitcode = EX_DEVTAB; 2270Sstevel@tonic-gate severity = MM_ERROR; 2280Sstevel@tonic-gate } else { 2290Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 2300Sstevel@tonic-gate exitcode = EX_ERROR; 2310Sstevel@tonic-gate severity = MM_HALT; 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, severity, txt); 2340Sstevel@tonic-gate exit(exitcode); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate /* 2390Sstevel@tonic-gate * Get the list of known attributes for the device. This does 2400Sstevel@tonic-gate * two things. First, it verifies that the device is known in the 2410Sstevel@tonic-gate * device table. Second, it gets the attributes to list just in 2420Sstevel@tonic-gate * case no attributes were specified. Then, set a pointer to the 2430Sstevel@tonic-gate * list of attributes to be extracted and listed... 2440Sstevel@tonic-gate */ 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate device = argv[optind]; 2470Sstevel@tonic-gate if ((argptr = listdev(device)) == (char **) NULL) { 2480Sstevel@tonic-gate if (errno == ENODEV) { 2490Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_NODEV, device); 2500Sstevel@tonic-gate exitcode = EX_NODEV; 2510Sstevel@tonic-gate severity = MM_ERROR; 2520Sstevel@tonic-gate } else { 2530Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 2540Sstevel@tonic-gate exitcode = EX_ERROR; 2550Sstevel@tonic-gate severity = MM_HALT; 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, severity, txt); 2580Sstevel@tonic-gate exit(exitcode); 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate if (argc > (optind+1)) argptr = &argv[optind+1]; 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * List attributes. If a requested attribute is not defined, 2650Sstevel@tonic-gate * list the value of that attribute as null. (Using shell 2660Sstevel@tonic-gate * variables as the model for this.) 2670Sstevel@tonic-gate */ 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate exitcode = EX_OK; 2700Sstevel@tonic-gate noerr = TRUE; 2710Sstevel@tonic-gate while (noerr && ((attr = *argptr++) != (char *) NULL)) { 2720Sstevel@tonic-gate if (!(value = devattr(device, attr))) { 2730Sstevel@tonic-gate if (errno == EINVAL) { 2740Sstevel@tonic-gate value = ""; 2750Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_NOATTR, attr); 2760Sstevel@tonic-gate /* stdmsg(MM_RECOVER, lbl, MM_WARNING, txt); */ 2770Sstevel@tonic-gate exitcode = EX_NOATTR; 2780Sstevel@tonic-gate } else { 2790Sstevel@tonic-gate noerr = FALSE; 2800Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 2810Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, txt); 2820Sstevel@tonic-gate exitcode = EX_ERROR; 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate if (noerr && v_seen) { 2860Sstevel@tonic-gate (void) fputs(attr, stdout); 2870Sstevel@tonic-gate (void) fputs("='", stdout); 2880Sstevel@tonic-gate for (p = value ; *p ; p++) { 2890Sstevel@tonic-gate (void) putc(*p, stdout); 2900Sstevel@tonic-gate if (*p == '\'') (void) fputs("\"'\"'", stdout); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate (void) fputs("'\n", stdout); 2930Sstevel@tonic-gate } else if (noerr) { 2940Sstevel@tonic-gate (void) fputs(value, stdout); 2950Sstevel@tonic-gate (void) putc('\n', stdout); 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate return (exitcode); 3000Sstevel@tonic-gate } 301