4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 * Implements the "putdgrp" command.
36 #include <sys/types.h>
48 * General Purpose Constants
49 * TRUE Boolean TRUE (if not already defined)
50 * FALSE Boolean FALSE (if not already defined)
64 * EX_ERROR Usage or internal error
65 * EX_DGROUP Had trouble accessing/reading/writing the
67 * EX_NODGRP The specified device-group does not exist
68 * EX_NOMEM One or more device-group members requested for
69 * removal was not defined for the device
83 #define E_USAGE "usage: putdgrp [-d] dgroup [device [...]]"
84 #define E_NODGRP "Device-group does not exist in table: %s"
85 #define E_NOTAMEM "Device-group member not found: %s"
86 #define E_NODGRPTAB "Cannot open the device-group table: %s"
87 #define E_NOMKTAB "Cannot create a new device-group table: %s"
88 #define E_INTERNAL "Internal error, errno=%d"
93 * stdmsg(r,l,s,t) Using fmtmsg(), write a standard message to the
94 * standard error stream.
96 * r The recoverability of the error
97 * l The label-component
98 * s The severity-component
99 * t The text-component
102 #define stdmsg(r,l,s,t) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
107 * msg Space for message's text-component
110 static char msg
[256]; /* Space for text of message */
116 * This function builds a standard label from the command used to invoke
117 * this process and the standard label prefix ("UX:")
120 * char *cmd The command used to invoke this process.
123 * Pointer to malloc()ed space containing the standard label,
124 * or (char *) NULL if an error occurred.
132 char *rtn
; /* Value to return */
133 char *p
; /* Temporary */
135 /* Find the 1st char of the basename of the command */
136 if (p
= strrchr(cmd
, '/')) p
++;
139 /* Allocate and build the string value to return */
140 if (rtn
= (char *) malloc(strlen("UX:")+strlen(p
)+1)) {
141 (void) strcpy(rtn
, "UX:");
142 (void) strcat(rtn
, p
);
145 /* Now that we've done all of this work, set up the environemnt
146 * so that only the text-component is written (some requirements
147 * say that standard messages are to be non-standard in SVR4.0,
148 * this is supposed to change in SVR4.1)
151 (void) putenv("MSGVERB=text");
158 * putdgrp [-d] dgroup [device [...]]
171 main(int argc
, char *argv
[])
174 char **plist
; /* Ptr to list of nonmembers */
175 char *lbl
; /* Ptr to label for messages */
176 char *dgroup
; /* Ptr to <dgroup> on command-line */
177 char *p
; /* Temp ptr to char */
178 int noerr
; /* FLAG, TRUE if all's well */
179 int d_seen
; /* TRUE if -a seen on command-line */
180 int optchar
; /* Option extracted */
181 int exitcd
; /* Value to return at exit */
182 int nmems
; /* Number of members on the cmd */
185 /* Generate the label for messages */
186 lbl
= mklbl(argv
[0]);
188 /* Extract arguments - validate usage */
192 while ((optchar
= getopt(argc
, argv
, "d:")) != EOF
) switch (optchar
) {
209 /* Write a usage message if we've seen a blatant error */
210 if (!noerr
|| (!d_seen
&& ((nmems
= argc
- optind
- 1) < 0)) ||
211 (d_seen
&& ((nmems
= argc
- optind
) < 0))) {
212 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, E_USAGE
);
221 /* -d on the command line ? */
225 * Determine case (removing a device group or members
226 * of that device group.
231 /* putdgrp -d dgroup */
233 /* Attempt to remove the specified device */
234 if (!(_rmdgrptabrec(dgroup
))) switch(errno
) {
237 * EINVAL indicates that the named device-group was
238 * not found in the device-group table.
242 (void) snprintf(msg
, sizeof(msg
), E_NODGRP
, dgroup
);
243 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
248 * ENOENT indicates that the device-group table can't
253 (void) snprintf(msg
, sizeof(msg
), E_NODGRPTAB
, _dgrptabpath());
254 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
259 * EACCES indicates that there was a problem reading the
260 * old device-group table or creating the new table. If the
261 * old table is readable, assume that we can't create the
262 * new table. Otherwise, assume that the old table isn't
268 if (access(p
, R_OK
) == 0)
269 (void) snprintf(msg
, sizeof(msg
), E_NOMKTAB
, p
);
271 (void) snprintf(msg
, sizeof(msg
), E_NODGRPTAB
, p
);
272 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
277 * Some strange problem...
281 (void) snprintf(msg
, sizeof(msg
), E_INTERNAL
, errno
);
282 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
289 /* putdgrp -d dgroup device [device [...]] */
292 * Attempt to remove the specified devices from the
293 * specified device-group.
295 if (!(_rmdgrpmems(dgroup
, &argv
[optind
], &plist
))) switch(errno
) {
298 * ENODEV indicates that a named device was not part
299 * of the specified device group.
304 for (; *plist
; plist
++) {
305 (void) snprintf(msg
, sizeof(msg
), E_NOTAMEM
, *plist
);
306 stdmsg(MM_RECOVER
, lbl
, MM_WARNING
, msg
);
311 * EINVAL indicates that the named device-group is not
312 * defined in the device-group table.
316 (void) snprintf(msg
, sizeof(msg
), E_NODGRP
, dgroup
);
317 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
322 * ENOENT indicates that the device table can't
327 (void) snprintf(msg
, sizeof(msg
), E_NODGRPTAB
, _dgrptabpath());
328 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
333 * EACCES indicates that there was a problem reading the
334 * old device table or creating the new table. If the
335 * old table is readable, assume that we can't create the
336 * new table. Otherwise, assume that the old table isn't
342 if (access(p
, R_OK
) == 0)
343 (void) snprintf(msg
, sizeof(msg
), E_NOMKTAB
, p
);
345 (void) snprintf(msg
, sizeof(msg
), E_NODGRPTAB
, p
);
346 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
351 * Some strange problem...
355 (void) sprintf(msg
, E_INTERNAL
, errno
);
356 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
361 } /* End "putdgrp -d device attr [...]" case */
367 /* Standard case (no -d on the command) */
368 if (!(_adddgrptabrec(argv
[optind
], &argv
[optind
+1]))) switch(errno
) {
371 * ENOENT indicates that the device-group table does not exist.
375 (void) snprintf(msg
, sizeof(msg
), E_NODGRPTAB
, _dgrptabpath());
376 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
381 * EACCES indicates that the device-group table could not be
382 * opened or the new device-group table could not be created.
387 if (access(p
, R_OK
) == 0)
388 (void) snprintf(msg
, sizeof(msg
), E_NOMKTAB
, p
);
390 (void) snprintf(msg
, sizeof(msg
), E_NODGRPTAB
, p
);
391 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
396 * Some strange error (memory?)
400 (void) sprintf(msg
, E_INTERNAL
, errno
);
401 stdmsg(MM_NRECOV
, lbl
, MM_ERROR
, msg
);
406 /* Done. Return exit code (determined above) */