xref: /titanic_50/usr/src/cmd/power/parse.c (revision 87c584d28b917037834264beb248765bd95712f5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include "pmconfig.h"
29 #include <deflt.h>
30 #include <pwd.h>
31 
32 #ifdef sparc
33 #include <libdevinfo.h>
34 static char sf_cmt[] = "# Statefile\t\tPath\n";
35 #endif
36 
37 static char as_cmt[] =
38 	"# Auto-Shutdown\t\tIdle(min)\tStart/Finish(hh:mm)\tBehavior\n";
39 
40 char **line_args;
41 int lineno = 0;
42 
43 /*
44  * cpr and pm combined permission/update status
45  */
46 prmup_t cpr_status = { 0, OKUP, "cpr" };
47 prmup_t pm_status  = { 0, OKUP, "pm" };
48 
49 
50 /*
51  * For config file parsing to work correctly/efficiently, this table
52  * needs to be sorted by .keyword and any longer string like "device"
53  * must appear before a substring like "dev".
54  */
55 static cinfo_t conftab[] = {
56 	"S3-support",		S3sup,   &pm_status,	NULL,	2, 0, 1,
57 	"autoS3",		autoS3,  &pm_status,	NULL,	2, 0, 1,
58 	"autopm",		autopm,  &pm_status,	NULL,	2, 0, 1,
59 	"autoshutdown",		autosd,  &cpr_status,	as_cmt,	5, 0, 1,
60 	"cpu-threshold",	cputhr,  &pm_status,	NULL,	2, 0, 1,
61 	"cpupm",		cpupm,   &pm_status,	NULL,	2, 0, 1,
62 	"device-dependency-property",
63 				ddprop,  &pm_status,	NULL,	3, 1, 1,
64 	"device-dependency",	devdep,  &pm_status,	NULL,	3, 1, 1,
65 	"device-thresholds",	devthr,  &pm_status,	NULL,	3, 1, 1,
66 	"diskreads",		dreads,  &cpr_status,	NULL,	2, 0, 1,
67 	"idlecheck",		idlechk, &cpr_status,	NULL,	2, 0, 0,
68 	"loadaverage",		loadavg, &cpr_status,	NULL,	2, 0, 1,
69 	"nfsreqs",		nfsreq,  &cpr_status,	NULL,	2, 0, 1,
70 #ifdef  sparc
71 	"statefile",		sfpath,  &cpr_status,	sf_cmt,	2, 0, 0,
72 #endif
73 	"system-threshold",	systhr,  &pm_status,	NULL,	2, 0, 1,
74 	"ttychars",		tchars,  &cpr_status,	NULL,	2, 0, 1,
75 	NULL,			NULL,	 NULL,		NULL,	0, 0, 0,
76 };
77 
78 
79 /*
80  * Set cpr/pm permission from default file info.
81  */
82 static void
83 set_perm(char *defstr, char *user, int *perm, int cons)
84 {
85 	char *dinfo, *tk;
86 
87 	/*
88 	 * /etc/default/power entries are:
89 	 *	all			(all users + root)
90 	 *	-			(none + root)
91 	 *	<user1[, user2...>	(list users + root)
92 	 *	console-owner		(console onwer + root)
93 	 * Any error in reading/parsing the file limits the
94 	 * access requirement to root.
95 	 */
96 	dinfo = defread(defstr);
97 	mesg(MDEBUG, "set_perm: \"%s\", value \"%s\"\n",
98 	    defstr, dinfo ? dinfo : "NULL");
99 	if (dinfo == NULL)
100 		return;
101 	else if (strcmp(dinfo, "all") == 0)
102 		*perm = 1;
103 	else if (strcmp(dinfo, "console-owner") == 0)
104 		*perm = cons;
105 	else if (user != NULL &&
106 	    (*dinfo == '<') && (tk = strrchr(++dinfo, '>'))) {
107 		/* Scan dinfo for a matching user. */
108 		for (*tk = '\0'; tk = strtok(dinfo, ", "); dinfo = NULL) {
109 			mesg(MDEBUG, "match_user: cmp (\"%s\", \"%s\")\n",
110 			    tk, user);
111 			if (strcmp(tk, user) == 0) {
112 				*perm = 1;
113 				break;
114 			}
115 		}
116 	}
117 }
118 
119 
120 /*
121  * Lookup cpr/pm user permissions in "/etc/default/power".
122  */
123 void
124 lookup_perms(void)
125 {
126 	struct passwd *pent;
127 	struct stat stbuf;
128 	int cons_perm;
129 	char *user;
130 
131 	if ((ruid = getuid()) == 0) {
132 		cpr_status.perm = pm_status.perm = 1;
133 		return;
134 	} else if ((pent = getpwuid(ruid)) != NULL) {
135 		user = pent->pw_name;
136 	} else {
137 		user = NULL;
138 	}
139 
140 	if (defopen("/etc/default/power") == -1)
141 		return;
142 	if (stat("/dev/console", &stbuf) == -1)
143 		cons_perm = 0;
144 	else
145 		cons_perm = (ruid == stbuf.st_uid);
146 
147 	set_perm("PMCHANGEPERM=", user, &pm_status.perm, cons_perm);
148 	set_perm("CPRCHANGEPERM=", user, &cpr_status.perm, cons_perm);
149 
150 	(void) defopen(NULL);
151 }
152 
153 
154 #ifdef sparc
155 /*
156  * Lookup energystar-v[23] property and set estar_vers.
157  */
158 void
159 lookup_estar_vers(void)
160 {
161 	char es_prop[] = "energystar-v?", *fmt = "%s init/access error\n";
162 	di_prom_handle_t ph;
163 	di_node_t node;
164 	uchar_t *prop_data;
165 	int last;
166 	char ch;
167 
168 	if ((node = di_init("/", DINFOPROP)) == DI_NODE_NIL) {
169 		mesg(MERR, fmt, "di_init");
170 		return;
171 	} else if ((ph = di_prom_init()) == DI_PROM_HANDLE_NIL) {
172 		mesg(MERR, fmt, "di_prom_init");
173 		di_fini(node);
174 		return;
175 	}
176 	last = strlen(es_prop) - 1;
177 	for (ch = ESTAR_V2; ch <= ESTAR_V3; ch++) {
178 		es_prop[last] = ch;
179 		if (di_prom_prop_lookup_bytes(ph, node,
180 		    es_prop, &prop_data) == 0) {
181 			mesg(MDEBUG, "get_estar_vers: %s prop found\n",
182 			    es_prop);
183 			estar_vers = ch;
184 			break;
185 		}
186 	}
187 	di_prom_fini(ph);
188 	di_fini(node);
189 }
190 #endif /* sparc */
191 
192 
193 /*
194  * limit open() to the real user
195  */
196 static int
197 pmc_open(char *name, int oflag)
198 {
199 	uid_t euid;
200 	int fd;
201 
202 	euid = geteuid();
203 	if (seteuid(ruid) == -1)
204 		mesg(MEXIT, "cannot reset euid to %d, %s\n",
205 		    ruid, strerror(errno));
206 	fd = open(name, oflag);
207 	(void) seteuid(euid);
208 	return (fd);
209 }
210 
211 
212 /*
213  * Alloc space and read a config file; caller needs to free the space.
214  */
215 static char *
216 get_conf_data(char *name)
217 {
218 	struct stat stbuf;
219 	ssize_t nread;
220 	size_t size;
221 	char *buf;
222 	int fd;
223 
224 	if ((fd = pmc_open(name, O_RDONLY)) == -1)
225 		mesg(MEXIT, "cannot open %s\n", name);
226 	else if (fstat(fd, &stbuf) == -1)
227 		mesg(MEXIT, "cannot stat %s\n", name);
228 	size = (size_t)stbuf.st_size;
229 	def_src = (stbuf.st_ino == def_info.st_ino &&
230 	    stbuf.st_dev == def_info.st_dev);
231 	if ((buf = malloc(size + 1)) == NULL)
232 		mesg(MEXIT, "cannot allocate %u for \"%s\"\n", size + 1, name);
233 	nread = read(fd, buf, size);
234 	(void) close(fd);
235 	if (nread != (ssize_t)size)
236 		mesg(MEXIT, "read error, expect %u, got %d, file \"%s\"\n",
237 		    size, nread, name);
238 	*(buf + size) = '\0';
239 	return (buf);
240 }
241 
242 
243 /*
244  * Add an arg to line_args, adding space if needed.
245  */
246 static void
247 newarg(char *arg, int index)
248 {
249 	static int alcnt;
250 	size_t size;
251 
252 	if ((index + 1) > alcnt) {
253 		alcnt += 4;
254 		size = alcnt * sizeof (*line_args);
255 		if ((line_args = realloc(line_args, size)) == NULL)
256 			mesg(MEXIT, "cannot alloc %u for line args\n", size);
257 	}
258 	*(line_args + index) = arg;
259 }
260 
261 
262 /*
263  * Convert blank-delimited words into an arg vector and return
264  * the arg count; character strings get null-terminated in place.
265  */
266 static int
267 build_args(char *cline, char *tail)
268 {
269 	extern int debug;
270 	char **vec, *arg, *cp;
271 	int cnt = 0;
272 
273 	/*
274 	 * Search logic: look for "\\\n" as a continuation marker,
275 	 * treat any other "\\*" as ordinary arg data, scan until a
276 	 * white-space delimiter is found, and if the arg has length,
277 	 * null-terminate and save arg to line_args.  The scan includes
278 	 * tail so the last arg is found without any special-case code.
279 	 */
280 	for (arg = cp = cline; cp <= tail; cp++) {
281 		if (*cp == '\\') {
282 			if (*(cp + 1) && *(cp + 1) != '\n') {
283 				cp++;
284 				continue;
285 			}
286 		} else if (strchr(" \t\n", *cp) == NULL)
287 			continue;
288 		if (cp - arg) {
289 			*cp = '\0';
290 			newarg(arg, cnt++);
291 		}
292 		arg = cp + 1;
293 	}
294 	newarg(NULL, cnt);
295 
296 	if (debug) {
297 		mesg(MDEBUG, "\nline %d, found %d args:\n", lineno, cnt);
298 		for (vec = line_args; *vec; vec++)
299 			mesg(MDEBUG, "    \"%s\"\n", *vec);
300 	}
301 
302 	return (cnt);
303 }
304 
305 
306 /*
307  * Match leading keyword from a conf line and
308  * return a reference to a config info struct.
309  */
310 static cinfo_t *
311 get_cinfo(void)
312 {
313 	cinfo_t *cip, *info = NULL;
314 	char *keyword;
315 	int chr_diff;
316 
317 	/*
318 	 * Scan the config table for a matching keyword; since the table
319 	 * is sorted by keyword strings, a few optimizations can be done:
320 	 * first compare only the first byte of the keyword, skip any
321 	 * table string that starts with a lower ASCII value, compare the
322 	 * full string only when the first byte matches, and stop checking
323 	 * if the table string starts with a higher ASCII value.
324 	 */
325 	keyword = LINEARG(0);
326 	for (cip = conftab; cip->keyword; cip++) {
327 		chr_diff = (int)(*cip->keyword - *keyword);
328 #if 0
329 		mesg(MDEBUG, "line %d, ('%c' - '%c') = %d\n",
330 		    lineno, *cip->keyword, *line, chr_diff);
331 #endif
332 		if (chr_diff < 0)
333 			continue;
334 		else if (chr_diff == 0) {
335 			if (strcmp(keyword, cip->keyword) == 0) {
336 				info = cip;
337 				break;
338 			}
339 		} else
340 			break;
341 	}
342 	return (info);
343 }
344 
345 
346 /*
347  * Find the end of a [possibly continued] conf line
348  * and record the real/lf-delimited line count at *lcnt.
349  */
350 static char *
351 find_line_end(char *line, int *lcnt)
352 {
353 	char *next, *lf;
354 
355 	*lcnt = 0;
356 	next = line;
357 	while (lf = strchr(next, '\n')) {
358 		(*lcnt)++;
359 		if (lf == line || (*(lf - 1) != '\\') || *(lf + 1) == '\0')
360 			break;
361 		next = lf + 1;
362 	}
363 	return (lf);
364 }
365 
366 
367 /*
368  * Parse the named conf file and for each conf line
369  * call the action routine or conftab handler routine.
370  */
371 void
372 parse_conf_file(char *name, vact_t action)
373 {
374 	char *file_buf, *cline, *line, *lend;
375 	cinfo_t *cip;
376 	int linc, cnt;
377 	size_t llen;
378 	int dontcare;
379 
380 	/*
381 	 * Do the "implied default" for autoS3
382 	 */
383 	(void) S3_helper("S3-support-enable", "S3-support-disable",
384 	    PM_ENABLE_S3, PM_DISABLE_S3, "S3-support", "default",
385 	    &dontcare, -1);
386 
387 	file_buf = get_conf_data(name);
388 	mesg(MDEBUG, "\nnow parsing \"%s\"...\n", name);
389 
390 	lineno = 1;
391 	line = file_buf;
392 	while (lend = find_line_end(line, &linc)) {
393 		/*
394 		 * Each line should start with valid data
395 		 * but leading white-space can be ignored
396 		 */
397 		while (line < lend) {
398 			if (*line != ' ' && *line != '\t')
399 				break;
400 			line++;
401 		}
402 
403 		/*
404 		 * Copy line into allocated space and null-terminate
405 		 * without the trailing line-feed.
406 		 */
407 		if ((llen = (lend - line)) != 0) {
408 			if ((cline = malloc(llen + 1)) == NULL)
409 				mesg(MEXIT, "cannot alloc %u bytes "
410 				    "for line copy\n", llen);
411 			(void) memcpy(cline, line, llen);
412 			*(cline + llen) = '\0';
413 		} else
414 			cline = NULL;
415 
416 		/*
417 		 * For blank and comment lines: possibly show a debug
418 		 * message and otherwise ignore them.  For other lines:
419 		 * parse into an arg vector and try to match the first
420 		 * arg with conftab keywords.  When a match is found,
421 		 * check for exact or minimum arg count, and call the
422 		 * action or handler routine; if handler does not return
423 		 * OKUP, set the referenced update value to NOUP so that
424 		 * later CPR or PM updates are skipped.
425 		 */
426 		if (llen == 0)
427 			mesg(MDEBUG, "\nline %d, blank...\n", lineno);
428 		else if (*line == '#')
429 			mesg(MDEBUG, "\nline %d, comment...\n", lineno);
430 		else if (cnt = build_args(cline, cline + llen)) {
431 			if ((cip = get_cinfo()) == NULL) {
432 				mesg(MEXIT, "unrecognized keyword \"%s\"\n",
433 				    LINEARG(0));
434 			} else if (cnt != cip->argc &&
435 			    (cip->any == 0 || cnt < cip->argc)) {
436 				mesg(MEXIT, "found %d args, expect %d%s\n",
437 				    cnt, cip->argc, cip->any ? "+" : "");
438 			} else if (action)
439 				(*action)(line, llen + 1, cip);
440 			else if (cip->status->perm && (def_src || cip->alt)) {
441 				if ((*cip->handler)() != OKUP)
442 					cip->status->update = NOUP;
443 			} else {
444 				mesg(MDEBUG,
445 				    "==> handler skipped: %s_perm %d, "
446 				    "def_src %d, alt %d\n", cip->status->set,
447 				    cip->status->perm, def_src, cip->alt);
448 			}
449 		}
450 
451 		if (cline)
452 			free(cline);
453 		line = lend + 1;
454 		lineno += linc;
455 	}
456 	lineno = 0;
457 
458 	free(file_buf);
459 
460 	if (verify) {
461 		int ret = ioctl(pm_fd, PM_GET_PM_STATE, NULL);
462 		if (ret < 0) {
463 			mesg(MDEBUG, "Cannot get PM state: %s\n",
464 			    strerror(errno));
465 		}
466 		switch (ret) {
467 		case PM_SYSTEM_PM_ENABLED:
468 			mesg(MDEBUG, "Autopm Enabled\n");
469 			break;
470 		case PM_SYSTEM_PM_DISABLED:
471 			mesg(MDEBUG, "Autopm Disabled\n");
472 			break;
473 		}
474 		ret = ioctl(pm_fd, PM_GET_S3_SUPPORT_STATE, NULL);
475 		if (ret < 0) {
476 			mesg(MDEBUG, "Cannot get PM state: %s\n",
477 			    strerror(errno));
478 		}
479 		switch (ret) {
480 		case PM_S3_SUPPORT_ENABLED:
481 			mesg(MDEBUG, "S3 support Enabled\n");
482 			break;
483 		case PM_S3_SUPPORT_DISABLED:
484 			mesg(MDEBUG, "S3 support Disabled\n");
485 			break;
486 		}
487 		ret = ioctl(pm_fd, PM_GET_AUTOS3_STATE, NULL);
488 		if (ret < 0) {
489 			mesg(MDEBUG, "Cannot get PM state: %s\n",
490 			    strerror(errno));
491 		}
492 		switch (ret) {
493 		case PM_AUTOS3_ENABLED:
494 			mesg(MDEBUG, "AutoS3 Enabled\n");
495 			break;
496 		case PM_AUTOS3_DISABLED:
497 			mesg(MDEBUG, "AutoS3  Disabled\n");
498 			break;
499 		}
500 	}
501 }
502