xref: /titanic_53/usr/src/cmd/dispadmin/dispadmin.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
24*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
29*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
35*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include	<string.h>
37*7c478bd9Sstevel@tonic-gate #include	<unistd.h>
38*7c478bd9Sstevel@tonic-gate #include	<fcntl.h>
39*7c478bd9Sstevel@tonic-gate #include	<errno.h>
40*7c478bd9Sstevel@tonic-gate #include	<string.h>
41*7c478bd9Sstevel@tonic-gate #include	<limits.h>
42*7c478bd9Sstevel@tonic-gate #include	<wait.h>
43*7c478bd9Sstevel@tonic-gate #include	<zone.h>
44*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
46*7c478bd9Sstevel@tonic-gate #include	<sys/priocntl.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #include	"dispadmin.h"
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * This file contains the code implementing the class independent part
52*7c478bd9Sstevel@tonic-gate  * of the dispadmin command.  Most of the functionality of the dispadmin
53*7c478bd9Sstevel@tonic-gate  * command is provided by the class specific sub-commands, the code for
54*7c478bd9Sstevel@tonic-gate  * which is elsewhere.  The class independent part of the command is
55*7c478bd9Sstevel@tonic-gate  * responsible for switching out to the appropriate class specific
56*7c478bd9Sstevel@tonic-gate  * sub-command based on the user supplied class argument.
57*7c478bd9Sstevel@tonic-gate  * Code in this file should never assume any knowledge of any specific
58*7c478bd9Sstevel@tonic-gate  * scheduler class (other than the SYS class).
59*7c478bd9Sstevel@tonic-gate  */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #define	BASENMSZ	16
62*7c478bd9Sstevel@tonic-gate #define	BUFSZ		(PATH_MAX + 80)
63*7c478bd9Sstevel@tonic-gate #define	CLASSPATH	"/usr/lib/class"
64*7c478bd9Sstevel@tonic-gate #define	CONFIGPATH	"/etc/dispadmin.conf"
65*7c478bd9Sstevel@tonic-gate #define	CONFIGOWNER	0	/* uid 0 (root) */
66*7c478bd9Sstevel@tonic-gate #define	CONFIGGROUP	1	/* gid 1 (other) */
67*7c478bd9Sstevel@tonic-gate #define	CONFIGPERM	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) /* 0644 */
68*7c478bd9Sstevel@tonic-gate #define	TOKENNAME	"DEFAULT_SCHEDULER"
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate extern char *basename();
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate static char usage[] =
73*7c478bd9Sstevel@tonic-gate "usage:	dispadmin -l\n\
74*7c478bd9Sstevel@tonic-gate 	dispadmin -c class [class-specific options]\n\
75*7c478bd9Sstevel@tonic-gate 	dispadmin -d [class]\n\
76*7c478bd9Sstevel@tonic-gate 	dispadmin -u\n";
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate static char basenm[BASENMSZ];
79*7c478bd9Sstevel@tonic-gate static char cmdpath[PATH_MAX];
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate static void print_classlist();
82*7c478bd9Sstevel@tonic-gate static void exec_cscmd(char *, char **);
83*7c478bd9Sstevel@tonic-gate static void set_scheduler(char *);
84*7c478bd9Sstevel@tonic-gate static void class_info(pcinfo_t *);
85*7c478bd9Sstevel@tonic-gate static void set_default_class();
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate int
88*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	extern char	*optarg;
91*7c478bd9Sstevel@tonic-gate 	extern int	optind, opterr;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	int		c;
94*7c478bd9Sstevel@tonic-gate 	int		uflag, cflag, dflag, lflag, csoptsflag;
95*7c478bd9Sstevel@tonic-gate 	char		*clname;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	(void) strncpy(cmdpath, argv[0], PATH_MAX);
98*7c478bd9Sstevel@tonic-gate 	(void) strncpy(basenm, basename(argv[0]), BASENMSZ);
99*7c478bd9Sstevel@tonic-gate 	cflag = dflag = lflag = uflag = csoptsflag = 0;
100*7c478bd9Sstevel@tonic-gate 	opterr = 0;
101*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "c:dlu")) != -1) {
102*7c478bd9Sstevel@tonic-gate 		switch (c) {
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 		case 'c':
105*7c478bd9Sstevel@tonic-gate 			cflag++;
106*7c478bd9Sstevel@tonic-gate 			clname = optarg;
107*7c478bd9Sstevel@tonic-gate 			break;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		case 'd':
110*7c478bd9Sstevel@tonic-gate 			dflag++;
111*7c478bd9Sstevel@tonic-gate 			clname = argv[optind];
112*7c478bd9Sstevel@tonic-gate 			break;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 		case 'l':
115*7c478bd9Sstevel@tonic-gate 			lflag++;
116*7c478bd9Sstevel@tonic-gate 			break;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 		case 'u':
119*7c478bd9Sstevel@tonic-gate 			uflag++;
120*7c478bd9Sstevel@tonic-gate 			break;
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 		case '?':
124*7c478bd9Sstevel@tonic-gate 			/*
125*7c478bd9Sstevel@tonic-gate 			 * We assume for now that any option that
126*7c478bd9Sstevel@tonic-gate 			 * getopt() doesn't recognize is intended for a
127*7c478bd9Sstevel@tonic-gate 			 * class specific subcommand.
128*7c478bd9Sstevel@tonic-gate 			 */
129*7c478bd9Sstevel@tonic-gate 			csoptsflag++;
130*7c478bd9Sstevel@tonic-gate 			if (argv[optind] && argv[optind][0] != '-') {
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 				/*
134*7c478bd9Sstevel@tonic-gate 				 * Class specific option takes an
135*7c478bd9Sstevel@tonic-gate 				 * argument which we skip over for now.
136*7c478bd9Sstevel@tonic-gate 				 */
137*7c478bd9Sstevel@tonic-gate 				optind++;
138*7c478bd9Sstevel@tonic-gate 			}
139*7c478bd9Sstevel@tonic-gate 			break;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 		default:
142*7c478bd9Sstevel@tonic-gate 			break;
143*7c478bd9Sstevel@tonic-gate 		}
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if (lflag) {
147*7c478bd9Sstevel@tonic-gate 		if (uflag || cflag || dflag || csoptsflag)
148*7c478bd9Sstevel@tonic-gate 			fatalerr(usage);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 		print_classlist();
151*7c478bd9Sstevel@tonic-gate 		exit(0);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	} else if (uflag) {
154*7c478bd9Sstevel@tonic-gate 		if (lflag || dflag || csoptsflag)
155*7c478bd9Sstevel@tonic-gate 			fatalerr(usage);
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 		set_default_class();
158*7c478bd9Sstevel@tonic-gate 	} else if (cflag) {
159*7c478bd9Sstevel@tonic-gate 		if (lflag || dflag)
160*7c478bd9Sstevel@tonic-gate 			fatalerr(usage);
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 		exec_cscmd(clname, argv);
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	} else if (dflag) {
165*7c478bd9Sstevel@tonic-gate 		if (cflag || lflag || csoptsflag)
166*7c478bd9Sstevel@tonic-gate 			fatalerr(usage);
167*7c478bd9Sstevel@tonic-gate 		set_scheduler(clname);
168*7c478bd9Sstevel@tonic-gate 		exit(0);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	} else {
171*7c478bd9Sstevel@tonic-gate 		fatalerr(usage);
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 	return (1);
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate /*
178*7c478bd9Sstevel@tonic-gate  * Print the heading for the class list and execute the
179*7c478bd9Sstevel@tonic-gate  * class specific sub-command with the -l option for each
180*7c478bd9Sstevel@tonic-gate  * configured class.
181*7c478bd9Sstevel@tonic-gate  */
182*7c478bd9Sstevel@tonic-gate static void
183*7c478bd9Sstevel@tonic-gate print_classlist()
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate 	id_t		cid;
186*7c478bd9Sstevel@tonic-gate 	int		nclass;
187*7c478bd9Sstevel@tonic-gate 	pcinfo_t	pcinfo;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	if ((nclass = priocntl(0, 0, PC_GETCLINFO, NULL)) == -1)
190*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: Can't get number of configured classes\n",
191*7c478bd9Sstevel@tonic-gate 		    cmdpath);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	(void) printf("CONFIGURED CLASSES\n==================\n\n");
194*7c478bd9Sstevel@tonic-gate 	(void) printf("SYS\t(System Class)\n");
195*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
196*7c478bd9Sstevel@tonic-gate 	for (cid = 1; cid < nclass; cid++) {
197*7c478bd9Sstevel@tonic-gate 		pcinfo.pc_cid = cid;
198*7c478bd9Sstevel@tonic-gate 		if (priocntl(0, 0, PC_GETCLINFO, (caddr_t)&pcinfo) == -1)
199*7c478bd9Sstevel@tonic-gate 			fatalerr("%s: Can't get class name (class ID = %d)\n",
200*7c478bd9Sstevel@tonic-gate 			    cmdpath, cid);
201*7c478bd9Sstevel@tonic-gate 		class_info(&pcinfo);
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate }
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate /*
207*7c478bd9Sstevel@tonic-gate  * Execute the appropriate class specific sub-command for the class
208*7c478bd9Sstevel@tonic-gate  * specified by clname, passing it the arguments in subcmdargv.
209*7c478bd9Sstevel@tonic-gate  */
210*7c478bd9Sstevel@tonic-gate static void
211*7c478bd9Sstevel@tonic-gate exec_cscmd(char *clname, char **subcmdargv)
212*7c478bd9Sstevel@tonic-gate {
213*7c478bd9Sstevel@tonic-gate 	pcinfo_t	pcinfo;
214*7c478bd9Sstevel@tonic-gate 	char		subcmdpath[PATH_MAX];
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	/*
217*7c478bd9Sstevel@tonic-gate 	 * Do a quick check to make sure clname is valid.
218*7c478bd9Sstevel@tonic-gate 	 * We could just wait and see if the exec below
219*7c478bd9Sstevel@tonic-gate 	 * succeeds but we wouldn't know much about the reason.
220*7c478bd9Sstevel@tonic-gate 	 * This way we can give the user a more meaningful error
221*7c478bd9Sstevel@tonic-gate 	 * message.
222*7c478bd9Sstevel@tonic-gate 	 */
223*7c478bd9Sstevel@tonic-gate 	(void) strncpy(pcinfo.pc_clname, clname, PC_CLNMSZ);
224*7c478bd9Sstevel@tonic-gate 	if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
225*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: Invalid or unconfigured class %s\n", cmdpath,
226*7c478bd9Sstevel@tonic-gate 		    clname);
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	(void) snprintf(subcmdpath, PATH_MAX, "%s/%s/%s%s", CLASSPATH,
229*7c478bd9Sstevel@tonic-gate 	    clname, clname, basenm);
230*7c478bd9Sstevel@tonic-gate 	subcmdargv[0] = subcmdpath;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	(void) execv(subcmdpath, subcmdargv);
233*7c478bd9Sstevel@tonic-gate 	fatalerr("%s: Can't execute %s sub-command\n", cmdpath, clname);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate static void
237*7c478bd9Sstevel@tonic-gate class_info(pcinfo_t *pcinfo)
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate 	int pid;
240*7c478bd9Sstevel@tonic-gate 	char subcmdpath[PATH_MAX];
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	(void) snprintf(subcmdpath, PATH_MAX, "%s/%s/%s%s", CLASSPATH,
243*7c478bd9Sstevel@tonic-gate 	    pcinfo->pc_clname, pcinfo->pc_clname, basenm);
244*7c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
245*7c478bd9Sstevel@tonic-gate 		(void) execl(subcmdpath, subcmdpath, "-l", (char *)0);
246*7c478bd9Sstevel@tonic-gate 		fatalerr("%s\n\tCan't execute %s specific subcommand\n",
247*7c478bd9Sstevel@tonic-gate 		    pcinfo->pc_clname, pcinfo->pc_clname);
248*7c478bd9Sstevel@tonic-gate 	} else if (pid == (pid_t)-1) {
249*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
250*7c478bd9Sstevel@tonic-gate 		    "%s\nCan't execute %s specific subcommand)\n",
251*7c478bd9Sstevel@tonic-gate 		    pcinfo->pc_clname, pcinfo->pc_clname);
252*7c478bd9Sstevel@tonic-gate 	} else {
253*7c478bd9Sstevel@tonic-gate 		(void) wait(NULL);
254*7c478bd9Sstevel@tonic-gate 	}
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate /*
258*7c478bd9Sstevel@tonic-gate  * Return the current default scheduling class as specified in
259*7c478bd9Sstevel@tonic-gate  * /etc/dispadmin.conf.
260*7c478bd9Sstevel@tonic-gate  */
261*7c478bd9Sstevel@tonic-gate static char *
262*7c478bd9Sstevel@tonic-gate read_default_file(FILE *fp)
263*7c478bd9Sstevel@tonic-gate {
264*7c478bd9Sstevel@tonic-gate 	char buf[BUFSZ];
265*7c478bd9Sstevel@tonic-gate 	int line;
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	for (line = 1; fgets(buf, BUFSZ, fp) != NULL; line++) {
268*7c478bd9Sstevel@tonic-gate 		char name[BUFSZ], value[BUFSZ];
269*7c478bd9Sstevel@tonic-gate 		int len;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 		if (buf[0] == '#' || buf[0] == '\n')
272*7c478bd9Sstevel@tonic-gate 			continue;
273*7c478bd9Sstevel@tonic-gate 		/* LINTED - unbounded string specifier */
274*7c478bd9Sstevel@tonic-gate 		if (sscanf(buf, " %[^=]=%s \n%n", name, value, &len) == 2 &&
275*7c478bd9Sstevel@tonic-gate 		    name[0] != '\0' && value[0] != '\0' && len == strlen(buf)) {
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 			if (strcmp(name, TOKENNAME) != 0)
278*7c478bd9Sstevel@tonic-gate 				fatalerr("\"%s\", line %d: invalid "
279*7c478bd9Sstevel@tonic-gate 				    "token: %s\n", CONFIGPATH, line, name);
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
282*7c478bd9Sstevel@tonic-gate 			return (strdup(value));
283*7c478bd9Sstevel@tonic-gate 		} else {
284*7c478bd9Sstevel@tonic-gate 			fatalerr("\"%s\", line %d: syntax error\n", CONFIGPATH,
285*7c478bd9Sstevel@tonic-gate 			    line);
286*7c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
287*7c478bd9Sstevel@tonic-gate 		}
288*7c478bd9Sstevel@tonic-gate 	}
289*7c478bd9Sstevel@tonic-gate 	if (line == 1)
290*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: %s is empty\n", cmdpath, CONFIGPATH);
291*7c478bd9Sstevel@tonic-gate 	return (NULL);
292*7c478bd9Sstevel@tonic-gate }
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate /*
295*7c478bd9Sstevel@tonic-gate  * Set the default scheduling class for the system.
296*7c478bd9Sstevel@tonic-gate  * Update /etc/dispadmin.conf if necessary.
297*7c478bd9Sstevel@tonic-gate  */
298*7c478bd9Sstevel@tonic-gate static void
299*7c478bd9Sstevel@tonic-gate set_scheduler(char *clname)
300*7c478bd9Sstevel@tonic-gate {
301*7c478bd9Sstevel@tonic-gate 	pcinfo_t pcinfo;
302*7c478bd9Sstevel@tonic-gate 	FILE *fp;
303*7c478bd9Sstevel@tonic-gate 	int fd;
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID)
306*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: Operation not supported in non-global zones\n",
307*7c478bd9Sstevel@tonic-gate 		    cmdpath);
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	if (clname == NULL) {
310*7c478bd9Sstevel@tonic-gate 		if ((fd = open(CONFIGPATH, O_RDONLY, CONFIGPERM)) == -1) {
311*7c478bd9Sstevel@tonic-gate 			if (errno == ENOENT)
312*7c478bd9Sstevel@tonic-gate 				fatalerr("%s: Default scheduling class "
313*7c478bd9Sstevel@tonic-gate 				    "is not set\n", cmdpath);
314*7c478bd9Sstevel@tonic-gate 			else
315*7c478bd9Sstevel@tonic-gate 				fatalerr("%s: Failed to open %s (%s)\n",
316*7c478bd9Sstevel@tonic-gate 				    cmdpath, CONFIGPATH, strerror(errno));
317*7c478bd9Sstevel@tonic-gate 		}
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 		if ((fp = fdopen(fd, "r")) == NULL)
320*7c478bd9Sstevel@tonic-gate 			fatalerr("%s: Failed to open stream for %s (%s)\n",
321*7c478bd9Sstevel@tonic-gate 			    cmdpath, CONFIGPATH, strerror(errno));
322*7c478bd9Sstevel@tonic-gate 		clname = read_default_file(fp);
323*7c478bd9Sstevel@tonic-gate 		(void) strncpy(pcinfo.pc_clname, clname, PC_CLNMSZ);
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 		if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
326*7c478bd9Sstevel@tonic-gate 			fatalerr("\"%s\", scheduling class %s is not "
327*7c478bd9Sstevel@tonic-gate 			    "available\n", CONFIGPATH, clname);
328*7c478bd9Sstevel@tonic-gate 		else
329*7c478bd9Sstevel@tonic-gate 			class_info(&pcinfo);
330*7c478bd9Sstevel@tonic-gate 		return;
331*7c478bd9Sstevel@tonic-gate 	}
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	/*
334*7c478bd9Sstevel@tonic-gate 	 * Do a quick check to make sure clname is valid class name.
335*7c478bd9Sstevel@tonic-gate 	 */
336*7c478bd9Sstevel@tonic-gate 	(void) strncpy(pcinfo.pc_clname, clname, PC_CLNMSZ);
337*7c478bd9Sstevel@tonic-gate 	if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
338*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: Invalid or unconfigured class %s\n", cmdpath,
339*7c478bd9Sstevel@tonic-gate 		    clname);
340*7c478bd9Sstevel@tonic-gate 	if ((fd = open(CONFIGPATH, O_RDWR | O_CREAT, CONFIGPERM)) == -1)
341*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: Failed to open %s (%s)\n", cmdpath, CONFIGPATH,
342*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
343*7c478bd9Sstevel@tonic-gate 	if ((fp = fdopen(fd, "w")) == NULL)
344*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: Failed to open stream for %s\n", CONFIGPATH);
345*7c478bd9Sstevel@tonic-gate 	if (ftruncate(fd, (off_t)0) == -1)
346*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: Failed to truncate %s\n", cmdpath, CONFIGPATH);
347*7c478bd9Sstevel@tonic-gate 	(void) fputs("#\n# /etc/dispadmin.conf\n#\n"
348*7c478bd9Sstevel@tonic-gate 	    "# Do NOT edit this file by hand -- use dispadmin(1m) instead.\n"
349*7c478bd9Sstevel@tonic-gate 	    "#\n", fp);
350*7c478bd9Sstevel@tonic-gate 	if ((fprintf(fp, "%s=%s\n", TOKENNAME, clname)) == -1)
351*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: Failed to write to %s\n", cmdpath, CONFIGPATH);
352*7c478bd9Sstevel@tonic-gate 	if (fflush(fp) != 0)
353*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
354*7c478bd9Sstevel@tonic-gate 		    "%s: warning: failed to flush config file\n",
355*7c478bd9Sstevel@tonic-gate 		    cmdpath);
356*7c478bd9Sstevel@tonic-gate 	if (fsync(fd) == -1)
357*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
358*7c478bd9Sstevel@tonic-gate 		    "%s: warning: failed to sync config file to disk\n",
359*7c478bd9Sstevel@tonic-gate 		    cmdpath);
360*7c478bd9Sstevel@tonic-gate 	if (fchmod(fd, CONFIGPERM) == -1)
361*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
362*7c478bd9Sstevel@tonic-gate 		    "%s: warning: failed to reset config file mode\n",
363*7c478bd9Sstevel@tonic-gate 		    cmdpath);
364*7c478bd9Sstevel@tonic-gate 	if (fchown(fd, CONFIGOWNER, CONFIGGROUP) == -1)
365*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
366*7c478bd9Sstevel@tonic-gate 		    "%s: warning: failed to reset config file owner\n",
367*7c478bd9Sstevel@tonic-gate 		    cmdpath);
368*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 	if (priocntl(0, 0, PC_SETDFLCL, clname) == -1)
371*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: failed to set default class %s in kernel: %s\n",
372*7c478bd9Sstevel@tonic-gate 		    cmdpath, clname, strerror(errno));
373*7c478bd9Sstevel@tonic-gate }
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate static void
376*7c478bd9Sstevel@tonic-gate set_default_class()
377*7c478bd9Sstevel@tonic-gate {
378*7c478bd9Sstevel@tonic-gate 	char *clname;
379*7c478bd9Sstevel@tonic-gate 	FILE *fp;
380*7c478bd9Sstevel@tonic-gate 	int fd;
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	if ((fd = open(CONFIGPATH, O_RDONLY, CONFIGPERM)) == -1) {
383*7c478bd9Sstevel@tonic-gate 		/* silently succeed, there is nothing to do */
384*7c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
385*7c478bd9Sstevel@tonic-gate 			return;
386*7c478bd9Sstevel@tonic-gate 		else
387*7c478bd9Sstevel@tonic-gate 			fatalerr("%s: Failed to open %s (%s)\n",
388*7c478bd9Sstevel@tonic-gate 			    cmdpath, CONFIGPATH, strerror(errno));
389*7c478bd9Sstevel@tonic-gate 	}
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 	if ((fp = fdopen(fd, "r")) == NULL)
392*7c478bd9Sstevel@tonic-gate 		fatalerr("%s: Failed to open stream for %s (%s)\n",
393*7c478bd9Sstevel@tonic-gate 		    cmdpath, CONFIGPATH, strerror(errno));
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	if ((clname = read_default_file(fp)) != NULL) {
396*7c478bd9Sstevel@tonic-gate 		if (priocntl(0, 0, PC_SETDFLCL, clname) == -1)
397*7c478bd9Sstevel@tonic-gate 			fatalerr("%s: failed to set default class %s in "
398*7c478bd9Sstevel@tonic-gate 			    "kernel: %s\n", cmdpath, clname, strerror(errno));
399*7c478bd9Sstevel@tonic-gate 	}
400*7c478bd9Sstevel@tonic-gate }
401