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
|
/*
* 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 (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <wait.h>
#include <zone.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/priocntl.h>
#include "dispadmin.h"
/*
* This file contains the code implementing the class independent part
* of the dispadmin command. Most of the functionality of the dispadmin
* command is provided by the class specific sub-commands, the code for
* which is elsewhere. The class independent part of the command is
* responsible for switching out to the appropriate class specific
* sub-command based on the user supplied class argument.
* Code in this file should never assume any knowledge of any specific
* scheduler class (other than the SYS class).
*/
#define BASENMSZ 16
#define BUFSZ (PATH_MAX + 80)
#define CLASSPATH "/usr/lib/class"
#define CONFIGPATH "/etc/dispadmin.conf"
#define CONFIGOWNER 0 /* uid 0 (root) */
#define CONFIGGROUP 1 /* gid 1 (other) */
#define CONFIGPERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) /* 0644 */
#define TOKENNAME "DEFAULT_SCHEDULER"
extern char *basename();
static char usage[] =
"usage: dispadmin -l\n\
dispadmin -c class [class-specific options]\n\
dispadmin -d [class]\n\
dispadmin -u\n";
static char basenm[BASENMSZ];
static char cmdpath[PATH_MAX];
static void print_classlist();
static void exec_cscmd(char *, char **);
static void set_scheduler(char *);
static void class_info(pcinfo_t *);
static void set_default_class();
int
main(int argc, char **argv)
{
extern char *optarg;
extern int optind, opterr;
int c;
int uflag, cflag, dflag, lflag, csoptsflag;
char *clname;
(void) strncpy(cmdpath, argv[0], PATH_MAX);
(void) strncpy(basenm, basename(argv[0]), BASENMSZ);
cflag = dflag = lflag = uflag = csoptsflag = 0;
opterr = 0;
while ((c = getopt(argc, argv, "c:dlu")) != -1) {
switch (c) {
case 'c':
cflag++;
clname = optarg;
break;
case 'd':
dflag++;
clname = argv[optind];
break;
case 'l':
lflag++;
break;
case 'u':
uflag++;
break;
case '?':
/*
* We assume for now that any option that
* getopt() doesn't recognize is intended for a
* class specific subcommand.
*/
csoptsflag++;
if (argv[optind] && argv[optind][0] != '-') {
/*
* Class specific option takes an
* argument which we skip over for now.
*/
optind++;
}
break;
default:
break;
}
}
if (lflag) {
if (uflag || cflag || dflag || csoptsflag)
fatalerr(usage);
print_classlist();
exit(0);
} else if (uflag) {
if (lflag || dflag || csoptsflag)
fatalerr(usage);
set_default_class();
} else if (cflag) {
if (lflag || dflag)
fatalerr(usage);
exec_cscmd(clname, argv);
} else if (dflag) {
if (cflag || lflag || csoptsflag)
fatalerr(usage);
set_scheduler(clname);
exit(0);
} else {
fatalerr(usage);
}
return (1);
}
/*
* Print the heading for the class list and execute the
* class specific sub-command with the -l option for each
* configured class.
*/
static void
print_classlist()
{
id_t cid;
int nclass;
pcinfo_t pcinfo;
if ((nclass = priocntl(0, 0, PC_GETCLINFO, NULL)) == -1)
fatalerr("%s: Can't get number of configured classes\n",
cmdpath);
(void) printf("CONFIGURED CLASSES\n==================\n\n");
(void) printf("SYS\t(System Class)\n");
(void) fflush(stdout);
for (cid = 1; cid < nclass; cid++) {
pcinfo.pc_cid = cid;
if (priocntl(0, 0, PC_GETCLINFO, (caddr_t)&pcinfo) == -1)
fatalerr("%s: Can't get class name (class ID = %d)\n",
cmdpath, cid);
class_info(&pcinfo);
}
}
/*
* Execute the appropriate class specific sub-command for the class
* specified by clname, passing it the arguments in subcmdargv.
*/
static void
exec_cscmd(char *clname, char **subcmdargv)
{
pcinfo_t pcinfo;
char subcmdpath[PATH_MAX];
/*
* Do a quick check to make sure clname is valid.
* We could just wait and see if the exec below
* succeeds but we wouldn't know much about the reason.
* This way we can give the user a more meaningful error
* message.
*/
(void) strncpy(pcinfo.pc_clname, clname, PC_CLNMSZ);
if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
fatalerr("%s: Invalid or unconfigured class %s\n", cmdpath,
clname);
(void) snprintf(subcmdpath, PATH_MAX, "%s/%s/%s%s", CLASSPATH,
clname, clname, basenm);
subcmdargv[0] = subcmdpath;
(void) execv(subcmdpath, subcmdargv);
fatalerr("%s: Can't execute %s sub-command\n", cmdpath, clname);
}
static void
class_info(pcinfo_t *pcinfo)
{
int pid;
char subcmdpath[PATH_MAX];
(void) snprintf(subcmdpath, PATH_MAX, "%s/%s/%s%s", CLASSPATH,
pcinfo->pc_clname, pcinfo->pc_clname, basenm);
if ((pid = fork()) == 0) {
(void) execl(subcmdpath, subcmdpath, "-l", (char *)0);
fatalerr("%s\n\tCan't execute %s specific subcommand\n",
pcinfo->pc_clname, pcinfo->pc_clname);
} else if (pid == (pid_t)-1) {
(void) fprintf(stderr,
"%s\nCan't execute %s specific subcommand)\n",
pcinfo->pc_clname, pcinfo->pc_clname);
} else {
(void) wait(NULL);
}
}
/*
* Return the current default scheduling class as specified in
* /etc/dispadmin.conf.
*/
static char *
read_default_file(FILE *fp)
{
char buf[BUFSZ];
int line;
for (line = 1; fgets(buf, BUFSZ, fp) != NULL; line++) {
char name[BUFSZ], value[BUFSZ];
int len;
if (buf[0] == '#' || buf[0] == '\n')
continue;
/* LINTED - unbounded string specifier */
if (sscanf(buf, " %[^=]=%s \n%n", name, value, &len) == 2 &&
name[0] != '\0' && value[0] != '\0' && len == strlen(buf)) {
if (strcmp(name, TOKENNAME) != 0)
fatalerr("\"%s\", line %d: invalid "
"token: %s\n", CONFIGPATH, line, name);
(void) fclose(fp);
return (strdup(value));
} else {
fatalerr("\"%s\", line %d: syntax error\n", CONFIGPATH,
line);
(void) fclose(fp);
}
}
if (line == 1)
fatalerr("%s: %s is empty\n", cmdpath, CONFIGPATH);
return (NULL);
}
/*
* Set the default scheduling class for the system.
* Update /etc/dispadmin.conf if necessary.
*/
static void
set_scheduler(char *clname)
{
pcinfo_t pcinfo;
FILE *fp;
int fd;
if (getzoneid() != GLOBAL_ZONEID)
fatalerr("%s: Operation not supported in non-global zones\n",
cmdpath);
if (clname == NULL) {
if ((fd = open(CONFIGPATH, O_RDONLY, CONFIGPERM)) == -1) {
if (errno == ENOENT)
fatalerr("%s: Default scheduling class "
"is not set\n", cmdpath);
else
fatalerr("%s: Failed to open %s (%s)\n",
cmdpath, CONFIGPATH, strerror(errno));
}
if ((fp = fdopen(fd, "r")) == NULL)
fatalerr("%s: Failed to open stream for %s (%s)\n",
cmdpath, CONFIGPATH, strerror(errno));
clname = read_default_file(fp);
(void) strncpy(pcinfo.pc_clname, clname, PC_CLNMSZ);
if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
fatalerr("\"%s\", scheduling class %s is not "
"available\n", CONFIGPATH, clname);
else
class_info(&pcinfo);
return;
}
/*
* Do a quick check to make sure clname is valid class name.
*/
(void) strncpy(pcinfo.pc_clname, clname, PC_CLNMSZ);
if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
fatalerr("%s: Invalid or unconfigured class %s\n", cmdpath,
clname);
if ((fd = open(CONFIGPATH, O_RDWR | O_CREAT, CONFIGPERM)) == -1)
fatalerr("%s: Failed to open %s (%s)\n", cmdpath, CONFIGPATH,
strerror(errno));
if ((fp = fdopen(fd, "w")) == NULL)
fatalerr("%s: Failed to open stream for %s\n", CONFIGPATH);
if (ftruncate(fd, (off_t)0) == -1)
fatalerr("%s: Failed to truncate %s\n", cmdpath, CONFIGPATH);
(void) fputs("#\n# /etc/dispadmin.conf\n#\n"
"# Do NOT edit this file by hand -- use dispadmin(1m) instead.\n"
"#\n", fp);
if ((fprintf(fp, "%s=%s\n", TOKENNAME, clname)) == -1)
fatalerr("%s: Failed to write to %s\n", cmdpath, CONFIGPATH);
if (fflush(fp) != 0)
(void) fprintf(stderr,
"%s: warning: failed to flush config file\n",
cmdpath);
if (fsync(fd) == -1)
(void) fprintf(stderr,
"%s: warning: failed to sync config file to disk\n",
cmdpath);
if (fchmod(fd, CONFIGPERM) == -1)
(void) fprintf(stderr,
"%s: warning: failed to reset config file mode\n",
cmdpath);
if (fchown(fd, CONFIGOWNER, CONFIGGROUP) == -1)
(void) fprintf(stderr,
"%s: warning: failed to reset config file owner\n",
cmdpath);
(void) fclose(fp);
if (priocntl(0, 0, PC_SETDFLCL, clname) == -1)
fatalerr("%s: failed to set default class %s in kernel: %s\n",
cmdpath, clname, strerror(errno));
}
static void
set_default_class()
{
char *clname;
FILE *fp;
int fd;
if ((fd = open(CONFIGPATH, O_RDONLY, CONFIGPERM)) == -1) {
/* silently succeed, there is nothing to do */
if (errno == ENOENT)
return;
else
fatalerr("%s: Failed to open %s (%s)\n",
cmdpath, CONFIGPATH, strerror(errno));
}
if ((fp = fdopen(fd, "r")) == NULL)
fatalerr("%s: Failed to open stream for %s (%s)\n",
cmdpath, CONFIGPATH, strerror(errno));
if ((clname = read_default_file(fp)) != NULL) {
if (priocntl(0, 0, PC_SETDFLCL, clname) == -1)
fatalerr("%s: failed to set default class %s in "
"kernel: %s\n", cmdpath, clname, strerror(errno));
}
}
|