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 * Implements the "putdev" command.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <fmtmsg.h>
407c478bd9Sstevel@tonic-gate #include <devmgmt.h>
417c478bd9Sstevel@tonic-gate #include <devtab.h>
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate * General Purpose Constants
467c478bd9Sstevel@tonic-gate * TRUE Boolean TRUE (if not already defined)
477c478bd9Sstevel@tonic-gate * FALSE Boolean FALSE (if not already defined)
487c478bd9Sstevel@tonic-gate * NULL Null address (if not already defined)
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #ifndef TRUE
527c478bd9Sstevel@tonic-gate #define TRUE (1)
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #ifndef FALSE
567c478bd9Sstevel@tonic-gate #define FALSE (0)
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate * Exit codes
617c478bd9Sstevel@tonic-gate * EX_OK All went well
627c478bd9Sstevel@tonic-gate * EX_ERROR Usage or internal error
637c478bd9Sstevel@tonic-gate * EX_DEVTAB Had trouble accessing/reading/writing the device table
647c478bd9Sstevel@tonic-gate * EX_EXISTS The specified alias already exists
657c478bd9Sstevel@tonic-gate * EX_ATTRIB One or more attributes requested for removal was not
667c478bd9Sstevel@tonic-gate * defined for the device
677c478bd9Sstevel@tonic-gate * EX_RELPATH Pathname supplied for cdevice, bdevice or pathname
687c478bd9Sstevel@tonic-gate * attributes was not a full pathname
697c478bd9Sstevel@tonic-gate */
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate #define EX_OK 0
727c478bd9Sstevel@tonic-gate #define EX_ERROR 1
737c478bd9Sstevel@tonic-gate #define EX_DEVTAB 2
747c478bd9Sstevel@tonic-gate #define EX_EXISTS 3
757c478bd9Sstevel@tonic-gate #define EX_ATTRIB 4
767c478bd9Sstevel@tonic-gate #define EX_RELPATH 4
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * Error messages
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate #define E_USAGE "usage: putdev -a alias [attribute=value [...]]\n putdev -m device attribute=value [attribute=value [...]]\n putdev -d device [attribute [...]]"
847c478bd9Sstevel@tonic-gate #define E_ALIASIS "Alias already exists in table: %s"
857c478bd9Sstevel@tonic-gate #define E_NODEV "Device does not exist in table: %s"
867c478bd9Sstevel@tonic-gate #define E_NOALIAS "Cannot use \"alias\" as an attribute"
877c478bd9Sstevel@tonic-gate #define E_NOATTR "Attribute not found: %s"
887c478bd9Sstevel@tonic-gate #define E_NODEVTAB "Cannot open the device table: %s"
897c478bd9Sstevel@tonic-gate #define E_NOMKDTAB "Cannot create a new device table: %s"
907c478bd9Sstevel@tonic-gate #define E_INVALIAS "Not a valid device alias: %s"
917c478bd9Sstevel@tonic-gate #define E_MULTIPLE "Multiple definitions of an attribute are not allowed."
927c478bd9Sstevel@tonic-gate #define E_INTERNAL "Internal error, errno=%d"
937c478bd9Sstevel@tonic-gate #define E_RELPATH "Full pathname required for cdevice,bdevice and pathname attributes."
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate * Macros
987c478bd9Sstevel@tonic-gate * stdmsg(r,l,s,t) Using fmtmsg(), write a standard message to the
997c478bd9Sstevel@tonic-gate * standard error stream.
1007c478bd9Sstevel@tonic-gate * Where:
1017c478bd9Sstevel@tonic-gate * r The recoverability of the error
1027c478bd9Sstevel@tonic-gate * l The label-component
1037c478bd9Sstevel@tonic-gate * s The severity-component
1047c478bd9Sstevel@tonic-gate * t The text-component
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@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
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate * Static data
1127c478bd9Sstevel@tonic-gate * msg Space for message's text-component
1137c478bd9Sstevel@tonic-gate */
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate static char msg[256]; /* Space for text of message */
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate * char *mklbl(cmd)
1197c478bd9Sstevel@tonic-gate * char *cmd
1207c478bd9Sstevel@tonic-gate *
1217c478bd9Sstevel@tonic-gate * This function builds a standard label from the command used to invoke
1227c478bd9Sstevel@tonic-gate * this process and the standard label prefix ("UX:")
1237c478bd9Sstevel@tonic-gate *
1247c478bd9Sstevel@tonic-gate * Arguments:
1257c478bd9Sstevel@tonic-gate * char *cmd The command used to invoke this process.
1267c478bd9Sstevel@tonic-gate *
1277c478bd9Sstevel@tonic-gate * Returns: char *
1287c478bd9Sstevel@tonic-gate * Pointer to malloc()ed space containing the standard label,
1297c478bd9Sstevel@tonic-gate * or (char *) NULL if an error occurred.
1307c478bd9Sstevel@tonic-gate */
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate static char *
mklbl(cmd)1337c478bd9Sstevel@tonic-gate mklbl(cmd)
1347c478bd9Sstevel@tonic-gate char *cmd;
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate /* Automatic data */
1377c478bd9Sstevel@tonic-gate char *rtn; /* Value to return */
1387c478bd9Sstevel@tonic-gate char *p; /* Temporary */
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /* Find the 1st char of the basename of the command */
1417c478bd9Sstevel@tonic-gate if (p = strrchr(cmd, '/')) p++;
1427c478bd9Sstevel@tonic-gate else p = cmd;
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate /* Allocate and build the string value to return */
1457c478bd9Sstevel@tonic-gate if (rtn = (char *) malloc(strlen("UX:")+strlen(p)+1)) {
1467c478bd9Sstevel@tonic-gate (void) strcpy(rtn, "UX:");
1477c478bd9Sstevel@tonic-gate (void) strcat(rtn, p);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate /* Now that we've done all of that work, change the environment
1527c478bd9Sstevel@tonic-gate * so that only the text-component is written by fmtmsg().
1537c478bd9Sstevel@tonic-gate * (This should go away in SVR4.1)
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate (void) putenv("MSGVERB=text");
1577c478bd9Sstevel@tonic-gate
158*2a8bcb4eSToomas Soome
1597c478bd9Sstevel@tonic-gate /* Done */
1607c478bd9Sstevel@tonic-gate return(rtn);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate * putdev -a alias [attribute=value [...]]
1657c478bd9Sstevel@tonic-gate * putdev -m alias attribute=value [attribute=value [...]]
1667c478bd9Sstevel@tonic-gate * putdev -d alias [attribute [...]]
1677c478bd9Sstevel@tonic-gate *
1687c478bd9Sstevel@tonic-gate * Modify the device-table. If -a specified, add a record for <alias>
1697c478bd9Sstevel@tonic-gate * to the table. If -m specified, modify the attributes specified for
1707c478bd9Sstevel@tonic-gate * the <device> specified. If -d specified, remove the specified
1717c478bd9Sstevel@tonic-gate * attributes from the specified device or remove the specified device.
1727c478bd9Sstevel@tonic-gate *
1737c478bd9Sstevel@tonic-gate * Options:
1747c478bd9Sstevel@tonic-gate * -a Add an alias description to the device table
1757c478bd9Sstevel@tonic-gate * -m Modify an existing device description
1767c478bd9Sstevel@tonic-gate * -d (if no attributes specified) remove the specified
1777c478bd9Sstevel@tonic-gate * device from the device table, or (if attributes
1787c478bd9Sstevel@tonic-gate * specified) remove the specified attributes from
1797c478bd9Sstevel@tonic-gate * the specified device.
1807c478bd9Sstevel@tonic-gate *
1817c478bd9Sstevel@tonic-gate * Exit values:
1827c478bd9Sstevel@tonic-gate * 0 All went well
1837c478bd9Sstevel@tonic-gate * 1 Usage error (includes specifying "alias" as an
1847c478bd9Sstevel@tonic-gate * <attribute>)
1857c478bd9Sstevel@tonic-gate * 2 The device table file could not be opened, read
1867c478bd9Sstevel@tonic-gate * or modified
1877c478bd9Sstevel@tonic-gate * 3 If -a, the alias already exists. Otherwise, the
1887c478bd9Sstevel@tonic-gate * specified device does not exist in the table
1897c478bd9Sstevel@tonic-gate * 4 One of the specified attributes did not exist
1907c478bd9Sstevel@tonic-gate * for the device and therefore wasn't removed
1917c478bd9Sstevel@tonic-gate */
1927c478bd9Sstevel@tonic-gate
193113f4232Sakaplan int
main(int argc,char * argv[])194113f4232Sakaplan main(int argc, char *argv[])
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate /* Automatic data */
1977c478bd9Sstevel@tonic-gate char **plist; /* Ptr to list of undef'nd attrs */
1987c478bd9Sstevel@tonic-gate char *lbl; /* Ptr to label for messages */
1997c478bd9Sstevel@tonic-gate char *alias; /* Ptr to <alias> on command-line */
2007c478bd9Sstevel@tonic-gate char *device; /* Ptr to <device> on command-line */
2017c478bd9Sstevel@tonic-gate char *p; /* Temp ptr to char */
2027c478bd9Sstevel@tonic-gate int noerr; /* FLAG, TRUE if all's well */
2037c478bd9Sstevel@tonic-gate int a_seen; /* TRUE if -a seen on command-line */
2047c478bd9Sstevel@tonic-gate int m_seen; /* TRUE if -m seen on command-line */
2057c478bd9Sstevel@tonic-gate int d_seen; /* TRUE if -a seen on command-line */
2067c478bd9Sstevel@tonic-gate int optchar; /* Option extracted */
2077c478bd9Sstevel@tonic-gate int exitcd; /* Value to return at exit */
2087c478bd9Sstevel@tonic-gate int nattrs; /* Number of attributes on command */
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate /* Generate the label for messages */
2127c478bd9Sstevel@tonic-gate lbl = mklbl(argv[0]);
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /* Extract arguments - validate usage */
2157c478bd9Sstevel@tonic-gate noerr = TRUE;
2167c478bd9Sstevel@tonic-gate a_seen = FALSE;
2177c478bd9Sstevel@tonic-gate m_seen = FALSE;
2187c478bd9Sstevel@tonic-gate d_seen = FALSE;
2197c478bd9Sstevel@tonic-gate opterr = FALSE;
2207c478bd9Sstevel@tonic-gate while ((optchar = getopt(argc, argv, "a:d:m:")) != EOF) switch (optchar) {
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate case 'a':
2237c478bd9Sstevel@tonic-gate if (!(a_seen || m_seen || d_seen)) {
2247c478bd9Sstevel@tonic-gate a_seen = TRUE;
2257c478bd9Sstevel@tonic-gate alias = optarg;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate else noerr = FALSE;
2287c478bd9Sstevel@tonic-gate break;
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate case 'd':
2317c478bd9Sstevel@tonic-gate if (!(a_seen || m_seen || d_seen)) {
2327c478bd9Sstevel@tonic-gate d_seen = TRUE;
2337c478bd9Sstevel@tonic-gate device = optarg;
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate else noerr = FALSE;
2367c478bd9Sstevel@tonic-gate break;
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate case 'm':
2397c478bd9Sstevel@tonic-gate if (!(a_seen || m_seen || d_seen)) {
2407c478bd9Sstevel@tonic-gate m_seen = TRUE;
2417c478bd9Sstevel@tonic-gate device = optarg;
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate else noerr = FALSE;
2447c478bd9Sstevel@tonic-gate break;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate case '?':
2477c478bd9Sstevel@tonic-gate default:
2487c478bd9Sstevel@tonic-gate noerr = FALSE;
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate /* Write a usage message if we've seen a blatant error */
2537c478bd9Sstevel@tonic-gate if (!(a_seen || m_seen || d_seen) || !noerr) {
2547c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_USAGE);
2557c478bd9Sstevel@tonic-gate exit(EX_ERROR);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate /* Set up */
2607c478bd9Sstevel@tonic-gate exitcd = EX_OK;
2617c478bd9Sstevel@tonic-gate nattrs = argc - optind;
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate /* putdev -a alias [attr=value [...]] */
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate if (a_seen) {
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate /* Syntax check */
2697c478bd9Sstevel@tonic-gate if (nattrs < 0) {
2707c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_USAGE);
2717c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
2727c478bd9Sstevel@tonic-gate } else {
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /* Attempt to add the new alias */
2757c478bd9Sstevel@tonic-gate if (!(_adddevtabrec(alias, &argv[optind]))) {
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate /* Attempt failed. Write appropriate error message. */
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate switch(errno) {
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate * EINVAL indicates that <alias> is not valid or "alias"
2837c478bd9Sstevel@tonic-gate * was mentioned as <attr> in <attr>=<value> pair. If the
2847c478bd9Sstevel@tonic-gate * alias is a valid alias, assume that's the problem.
2857c478bd9Sstevel@tonic-gate */
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate case EINVAL:
2887c478bd9Sstevel@tonic-gate if (_validalias(alias))
2897c478bd9Sstevel@tonic-gate p = E_NOALIAS;
2907c478bd9Sstevel@tonic-gate else (void) snprintf(p=msg, sizeof(msg), E_INVALIAS, alias);
2917c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, p);
2927c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
2937c478bd9Sstevel@tonic-gate break;
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate * EEXIST indicates that the alias <alias> already exists
2977c478bd9Sstevel@tonic-gate * in the device table.
2987c478bd9Sstevel@tonic-gate */
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate case EEXIST:
3017c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_ALIASIS, alias);
3027c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
3037c478bd9Sstevel@tonic-gate exitcd = EX_EXISTS;
3047c478bd9Sstevel@tonic-gate break;
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate * EACCES and ENOENT indicate problems reading or writing
3087c478bd9Sstevel@tonic-gate * the device table.
3097c478bd9Sstevel@tonic-gate */
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate case EACCES:
3127c478bd9Sstevel@tonic-gate case ENOENT:
3137c478bd9Sstevel@tonic-gate p = _devtabpath();
3147c478bd9Sstevel@tonic-gate if (access(p, R_OK) == 0)
3157c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NOMKDTAB, p);
3167c478bd9Sstevel@tonic-gate else
3177c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NODEVTAB, p);
3187c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
3197c478bd9Sstevel@tonic-gate exitcd = EX_DEVTAB;
3207c478bd9Sstevel@tonic-gate break;
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate * EAGAIN indicates that an attribute was defined on the
3247c478bd9Sstevel@tonic-gate * command line more than once.
3257c478bd9Sstevel@tonic-gate */
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate case EAGAIN:
3287c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_MULTIPLE);
3297c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
3307c478bd9Sstevel@tonic-gate break;
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate * ENXIO indicates that a relative pathname was supplied
3347c478bd9Sstevel@tonic-gate * for the cdevice, bdevice or pathname attributes. Full
3357c478bd9Sstevel@tonic-gate * pathnames are required for these attributes.
3367c478bd9Sstevel@tonic-gate */
3377c478bd9Sstevel@tonic-gate case ENXIO:
3387c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_RELPATH);
3397c478bd9Sstevel@tonic-gate exitcd = EX_RELPATH;
3407c478bd9Sstevel@tonic-gate break;
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate * Some other problem (odd?)
3447c478bd9Sstevel@tonic-gate */
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate default:
3477c478bd9Sstevel@tonic-gate (void) sprintf(msg, E_INTERNAL, errno);
3487c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
3497c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate } /* End -a case */
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate /* putdev -m device attr=value [...] */
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate else if (m_seen) {
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate /* Check usage */
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate if (nattrs <= 0) {
3637c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_USAGE);
3647c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
3657c478bd9Sstevel@tonic-gate } else {
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate /* Attempt to modify a device's record */
3687c478bd9Sstevel@tonic-gate if (!(_moddevtabrec(device, &argv[optind]))) {
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate /* Modification attempt failed */
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate switch(errno) {
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate * EINVAL indicates that "alias" was used as an attribute
3767c478bd9Sstevel@tonic-gate * in an <attr>=<value> pair.
3777c478bd9Sstevel@tonic-gate */
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate case EINVAL:
3807c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOALIAS);
3817c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
3827c478bd9Sstevel@tonic-gate break;
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate * ENODEV indicates that the device that was to
3867c478bd9Sstevel@tonic-gate * be modified doesn't exist.
3877c478bd9Sstevel@tonic-gate */
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate case ENODEV:
3907c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NODEV, device);
3917c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
3927c478bd9Sstevel@tonic-gate exitcd = EX_EXISTS;
3937c478bd9Sstevel@tonic-gate break;
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate * ENOENT indicates that the device-table doesn't exist.
3977c478bd9Sstevel@tonic-gate */
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate case ENOENT:
4007c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NODEVTAB, _devtabpath());
4017c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
4027c478bd9Sstevel@tonic-gate exitcd = EX_DEVTAB;
4037c478bd9Sstevel@tonic-gate break;
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate * EACCES indicates that there was a problem reading the
4077c478bd9Sstevel@tonic-gate * old device table or creating the new table. If the
4087c478bd9Sstevel@tonic-gate * old table is readable, assume that we can't create the
4097c478bd9Sstevel@tonic-gate * new table. Otherwise, assume that the old table isn't
4107c478bd9Sstevel@tonic-gate * accessible.
4117c478bd9Sstevel@tonic-gate */
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate case EACCES:
4147c478bd9Sstevel@tonic-gate p = _devtabpath();
4157c478bd9Sstevel@tonic-gate if (access(p, R_OK) == 0)
4167c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NOMKDTAB, p);
4177c478bd9Sstevel@tonic-gate else
4187c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NODEVTAB, p);
4197c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
4207c478bd9Sstevel@tonic-gate exitcd = EX_DEVTAB;
4217c478bd9Sstevel@tonic-gate break;
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate * EAGAIN indicates that an attribute was specified more than
4257c478bd9Sstevel@tonic-gate * once on the command line.
4267c478bd9Sstevel@tonic-gate */
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate case EAGAIN:
4297c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_MULTIPLE);
4307c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
4317c478bd9Sstevel@tonic-gate break;
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate * ENXIO indicates that a relative pathname was supplied
4357c478bd9Sstevel@tonic-gate * for the cdevice, bdevice or pathname attributes. Full
4367c478bd9Sstevel@tonic-gate * pathnames are required for these attributes.
4377c478bd9Sstevel@tonic-gate */
4387c478bd9Sstevel@tonic-gate case ENXIO:
4397c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_RELPATH);
4407c478bd9Sstevel@tonic-gate exitcd = EX_RELPATH;
4417c478bd9Sstevel@tonic-gate break;
4427c478bd9Sstevel@tonic-gate
4437c478bd9Sstevel@tonic-gate /*
4447c478bd9Sstevel@tonic-gate * Some strange problem...
4457c478bd9Sstevel@tonic-gate */
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate default:
4487c478bd9Sstevel@tonic-gate (void) sprintf(msg, E_INTERNAL, errno);
4497c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
4507c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate } /* End -m case */
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate else if (d_seen) {
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate /* putdev -d device [attr [...]] */
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate /* Check usage */
4617c478bd9Sstevel@tonic-gate if (nattrs < 0) {
4627c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_USAGE);
4637c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
4647c478bd9Sstevel@tonic-gate } else {
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate * Determine case (removing a device or attributes
4687c478bd9Sstevel@tonic-gate * to a device.
4697c478bd9Sstevel@tonic-gate */
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate if (nattrs == 0) {
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate /* putdev -d device */
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate /* Attempt to remove the specified device */
4767c478bd9Sstevel@tonic-gate if (!(_rmdevtabrec(device))) switch(errno) {
4777c478bd9Sstevel@tonic-gate
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate * ENODEV indicates that the named device is not
4807c478bd9Sstevel@tonic-gate * defined in the device table.
4817c478bd9Sstevel@tonic-gate */
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate case ENODEV:
4847c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NODEV, device);
4857c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
4867c478bd9Sstevel@tonic-gate exitcd = EX_EXISTS;
4877c478bd9Sstevel@tonic-gate break;
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate * ENOENT indicates that the device table can't
4917c478bd9Sstevel@tonic-gate * be found.
4927c478bd9Sstevel@tonic-gate */
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate case ENOENT:
4957c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NODEVTAB, _devtabpath());
4967c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
4977c478bd9Sstevel@tonic-gate exitcd = EX_DEVTAB;
4987c478bd9Sstevel@tonic-gate break;
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate * EACCES indicates that there was a problem reading the
5027c478bd9Sstevel@tonic-gate * old device table or creating the new table. If the
5037c478bd9Sstevel@tonic-gate * old table is readable, assume that we can't create the
5047c478bd9Sstevel@tonic-gate * new table. Otherwise, assume that the old table isn't
5057c478bd9Sstevel@tonic-gate * accessible.
5067c478bd9Sstevel@tonic-gate */
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate case EACCES:
5097c478bd9Sstevel@tonic-gate p = _devtabpath();
5107c478bd9Sstevel@tonic-gate if (access(p, R_OK) == 0)
5117c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NOMKDTAB, p);
5127c478bd9Sstevel@tonic-gate else
5137c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NODEVTAB, p);
5147c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
5157c478bd9Sstevel@tonic-gate exitcd = EX_DEVTAB;
5167c478bd9Sstevel@tonic-gate break;
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate /*
5197c478bd9Sstevel@tonic-gate * Some strange problem...
5207c478bd9Sstevel@tonic-gate */
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate default:
5237c478bd9Sstevel@tonic-gate (void) sprintf(msg, E_INTERNAL, errno);
5247c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
5257c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate } /* End switch */
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate else {
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate /* putdev -d device attr [attr [...]] */
5327c478bd9Sstevel@tonic-gate
5337c478bd9Sstevel@tonic-gate /*
5347c478bd9Sstevel@tonic-gate * Attempt to remove the specified attributes from the
5357c478bd9Sstevel@tonic-gate * specified device.
5367c478bd9Sstevel@tonic-gate */
5377c478bd9Sstevel@tonic-gate if (!(_rmdevtabattrs(device, &argv[optind], &plist))) switch(errno) {
5387c478bd9Sstevel@tonic-gate
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate * EINVAL indicates that a named attribute was not
5417c478bd9Sstevel@tonic-gate * defined for the specified device or "alias" was
5427c478bd9Sstevel@tonic-gate * requested. If "plist" points to a list of attrs,
5437c478bd9Sstevel@tonic-gate * the former is the problem. Otherwise, the latter
5447c478bd9Sstevel@tonic-gate * is the problem.
5457c478bd9Sstevel@tonic-gate */
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate case EINVAL:
5487c478bd9Sstevel@tonic-gate if (plist) {
5497c478bd9Sstevel@tonic-gate exitcd = EX_ATTRIB;
5507c478bd9Sstevel@tonic-gate for (; *plist; plist++) {
5517c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NOATTR, *plist);
5527c478bd9Sstevel@tonic-gate stdmsg(MM_RECOVER, lbl, MM_WARNING, msg);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate } else {
5557c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOALIAS);
5567c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate break;
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate * ENODEV indicates that the named device is not
5627c478bd9Sstevel@tonic-gate * defined in the device table.
5637c478bd9Sstevel@tonic-gate */
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate case ENODEV:
5667c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NODEV, device);
5677c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
5687c478bd9Sstevel@tonic-gate exitcd = EX_EXISTS;
5697c478bd9Sstevel@tonic-gate break;
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate * ENOENT indicates that the device table can't
5737c478bd9Sstevel@tonic-gate * be found.
5747c478bd9Sstevel@tonic-gate */
5757c478bd9Sstevel@tonic-gate
5767c478bd9Sstevel@tonic-gate case ENOENT:
5777c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NODEVTAB, _devtabpath());
5787c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
5797c478bd9Sstevel@tonic-gate exitcd = EX_DEVTAB;
5807c478bd9Sstevel@tonic-gate break;
5817c478bd9Sstevel@tonic-gate
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate * EACCES indicates that there was a problem reading the
5847c478bd9Sstevel@tonic-gate * old device table or creating the new table. If the
5857c478bd9Sstevel@tonic-gate * old table is readable, assume that we can't create the
5867c478bd9Sstevel@tonic-gate * new table. Otherwise, assume that the old table isn't
5877c478bd9Sstevel@tonic-gate * accessible.
5887c478bd9Sstevel@tonic-gate */
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate case EACCES:
5917c478bd9Sstevel@tonic-gate p = _devtabpath();
5927c478bd9Sstevel@tonic-gate if (access(p, R_OK) == 0)
5937c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NOMKDTAB, p);
5947c478bd9Sstevel@tonic-gate else
5957c478bd9Sstevel@tonic-gate (void) snprintf(msg, sizeof(msg), E_NODEVTAB, p);
5967c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
5977c478bd9Sstevel@tonic-gate exitcd = EX_DEVTAB;
5987c478bd9Sstevel@tonic-gate break;
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate /*
6017c478bd9Sstevel@tonic-gate * Some strange problem...
6027c478bd9Sstevel@tonic-gate */
6037c478bd9Sstevel@tonic-gate
6047c478bd9Sstevel@tonic-gate default:
6057c478bd9Sstevel@tonic-gate (void) sprintf(msg, E_INTERNAL, errno);
6067c478bd9Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
6077c478bd9Sstevel@tonic-gate exitcd = EX_ERROR;
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate } /* End switch */
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate } /* End "putdev -d device attr [...]" case */
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate } /* End passes usage-check case */
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate } /* End -d case */
6167c478bd9Sstevel@tonic-gate
6177c478bd9Sstevel@tonic-gate
6187c478bd9Sstevel@tonic-gate /* Done. Return exit code (determined above) */
6197c478bd9Sstevel@tonic-gate return(exitcd);
6207c478bd9Sstevel@tonic-gate } /* main() */
621