1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Implements the "putdgrp" command.
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fmtmsg.h>
#include <devmgmt.h>
#include <devtab.h>
/*
* General Purpose Constants
* TRUE Boolean TRUE (if not already defined)
* FALSE Boolean FALSE (if not already defined)
*/
#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
/*
* Exit codes
* EX_OK All went well
* EX_ERROR Usage or internal error
* EX_DGROUP Had trouble accessing/reading/writing the
* device-group table
* EX_NODGRP The specified device-group does not exist
* EX_NOMEM One or more device-group members requested for
* removal was not defined for the device
*/
#define EX_OK 0
#define EX_ERROR 1
#define EX_DGROUP 2
#define EX_NODGRP 3
#define EX_NOMEM 4
/*
* Error messages
*/
#define E_USAGE "usage: putdgrp [-d] dgroup [device [...]]"
#define E_NODGRP "Device-group does not exist in table: %s"
#define E_NOTAMEM "Device-group member not found: %s"
#define E_NODGRPTAB "Cannot open the device-group table: %s"
#define E_NOMKTAB "Cannot create a new device-group table: %s"
#define E_INTERNAL "Internal error, errno=%d"
/*
* Macros
* stdmsg(r,l,s,t) Using fmtmsg(), write a standard message to the
* standard error stream.
* Where:
* r The recoverability of the error
* l The label-component
* s The severity-component
* t The text-component
*/
#define stdmsg(r,l,s,t) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
/*
* Static data
* msg Space for message's text-component
*/
static char msg[256]; /* Space for text of message */
/*
* char *mklbl(cmd)
* char *cmd
*
* This function builds a standard label from the command used to invoke
* this process and the standard label prefix ("UX:")
*
* Arguments:
* char *cmd The command used to invoke this process.
*
* Returns: char *
* Pointer to malloc()ed space containing the standard label,
* or (char *) NULL if an error occurred.
*/
static char *
mklbl(cmd)
char *cmd;
{
/* Automatic data */
char *rtn; /* Value to return */
char *p; /* Temporary */
/* Find the 1st char of the basename of the command */
if (p = strrchr(cmd, '/')) p++;
else p = cmd;
/* Allocate and build the string value to return */
if (rtn = (char *) malloc(strlen("UX:")+strlen(p)+1)) {
(void) strcpy(rtn, "UX:");
(void) strcat(rtn, p);
}
/* Now that we've done all of this work, set up the environemnt
* so that only the text-component is written (some requirements
* say that standard messages are to be non-standard in SVR4.0,
* this is supposed to change in SVR4.1)
*/
(void) putenv("MSGVERB=text");
/* Done */
return(rtn);
}
/*
* putdgrp [-d] dgroup [device [...]]
*
* Options:
* -d
*
* Arguments:
* dgroup
* device
*
* Exit values:
*/
int
main(int argc, char *argv[])
{
/* Automatic data */
char **plist; /* Ptr to list of nonmembers */
char *lbl; /* Ptr to label for messages */
char *dgroup; /* Ptr to <dgroup> on command-line */
char *p; /* Temp ptr to char */
int noerr; /* FLAG, TRUE if all's well */
int d_seen; /* TRUE if -a seen on command-line */
int optchar; /* Option extracted */
int exitcd; /* Value to return at exit */
int nmems; /* Number of members on the cmd */
/* Generate the label for messages */
lbl = mklbl(argv[0]);
/* Extract arguments - validate usage */
noerr = TRUE;
d_seen = FALSE;
opterr = FALSE;
while ((optchar = getopt(argc, argv, "d:")) != EOF) switch (optchar) {
case 'd':
if (!d_seen)
{
d_seen = TRUE;
dgroup = optarg;
}
else noerr = FALSE;
break;
case '?':
default:
noerr = FALSE;
}
/* Write a usage message if we've seen a blatant error */
if (!noerr || (!d_seen && ((nmems = argc - optind - 1) < 0)) ||
(d_seen && ((nmems = argc - optind) < 0))) {
stdmsg(MM_NRECOV, lbl, MM_ERROR, E_USAGE);
exit(EX_ERROR);
}
/* Set up */
exitcd = EX_OK;
/* -d on the command line ? */
if (d_seen) {
/*
* Determine case (removing a device group or members
* of that device group.
*/
if (nmems == 0) {
/* putdgrp -d dgroup */
/* Attempt to remove the specified device */
if (!(_rmdgrptabrec(dgroup))) switch(errno) {
/*
* EINVAL indicates that the named device-group was
* not found in the device-group table.
*/
case EINVAL:
(void) snprintf(msg, sizeof(msg), E_NODGRP, dgroup);
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_NODGRP;
break;
/*
* ENOENT indicates that the device-group table can't
* be found.
*/
case ENOENT:
(void) snprintf(msg, sizeof(msg), E_NODGRPTAB, _dgrptabpath());
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_DGROUP;
break;
/*
* EACCES indicates that there was a problem reading the
* old device-group table or creating the new table. If the
* old table is readable, assume that we can't create the
* new table. Otherwise, assume that the old table isn't
* accessible.
*/
case EACCES:
p = _dgrptabpath();
if (access(p, R_OK) == 0)
(void) snprintf(msg, sizeof(msg), E_NOMKTAB, p);
else
(void) snprintf(msg, sizeof(msg), E_NODGRPTAB, p);
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_DGROUP;
break;
/*
* Some strange problem...
*/
default:
(void) snprintf(msg, sizeof(msg), E_INTERNAL, errno);
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_ERROR;
} /* End switch */
}
else {
/* putdgrp -d dgroup device [device [...]] */
/*
* Attempt to remove the specified devices from the
* specified device-group.
*/
if (!(_rmdgrpmems(dgroup, &argv[optind], &plist))) switch(errno) {
/*
* ENODEV indicates that a named device was not part
* of the specified device group.
*/
case ENODEV:
exitcd = EX_NOMEM;
for (; *plist; plist++) {
(void) snprintf(msg, sizeof(msg), E_NOTAMEM, *plist);
stdmsg(MM_RECOVER, lbl, MM_WARNING, msg);
}
break;
/*
* EINVAL indicates that the named device-group is not
* defined in the device-group table.
*/
case EINVAL:
(void) snprintf(msg, sizeof(msg), E_NODGRP, dgroup);
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_NODGRP;
break;
/*
* ENOENT indicates that the device table can't
* be found.
*/
case ENOENT:
(void) snprintf(msg, sizeof(msg), E_NODGRPTAB, _dgrptabpath());
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_DGROUP;
break;
/*
* EACCES indicates that there was a problem reading the
* old device table or creating the new table. If the
* old table is readable, assume that we can't create the
* new table. Otherwise, assume that the old table isn't
* accessible.
*/
case EACCES:
p = _dgrptabpath();
if (access(p, R_OK) == 0)
(void) snprintf(msg, sizeof(msg), E_NOMKTAB, p);
else
(void) snprintf(msg, sizeof(msg), E_NODGRPTAB, p);
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_DGROUP;
break;
/*
* Some strange problem...
*/
default:
(void) sprintf(msg, E_INTERNAL, errno);
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_ERROR;
} /* End switch */
} /* End "putdgrp -d device attr [...]" case */
} /* End -d case */
else {
/* Standard case (no -d on the command) */
if (!(_adddgrptabrec(argv[optind], &argv[optind+1]))) switch(errno) {
/*
* ENOENT indicates that the device-group table does not exist.
*/
case ENOENT:
(void) snprintf(msg, sizeof(msg), E_NODGRPTAB, _dgrptabpath());
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_DGROUP;
break;
/*
* EACCES indicates that the device-group table could not be
* opened or the new device-group table could not be created.
*/
case EACCES:
p = _dgrptabpath();
if (access(p, R_OK) == 0)
(void) snprintf(msg, sizeof(msg), E_NOMKTAB, p);
else
(void) snprintf(msg, sizeof(msg), E_NODGRPTAB, p);
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_DGROUP;
break;
/*
* Some strange error (memory?)
*/
default:
(void) sprintf(msg, E_INTERNAL, errno);
stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
exitcd = EX_ERROR;
}
}
/* Done. Return exit code (determined above) */
return(exitcd);
} /* main() */
|