xref: /titanic_41/usr/src/lib/libc/amd64/sys/uadmin.c (revision ba2e4443695ee6a6f420a35cd4fc3d3346d22932)
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 2006 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 #pragma weak uadmin = _uadmin
29 
30 /*
31  * Wrapper function to implement reboot w/ arguments on x86
32  * platforms. Extract reboot arguments and place them in
33  * in a transient entry in /[stub]boot/grub/menu.lst
34  * All other commands are passed through.
35  */
36 
37 #include "synonyms.h"
38 #include <fcntl.h>
39 #include <ctype.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/uadmin.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <zone.h>
48 
49 static int
50 legal_arg(char *bargs)
51 {
52 	int i;
53 
54 	for (i = 0; i < BOOTARGS_MAX; i++, bargs++) {
55 		if (*bargs == 0 && i > 0)
56 			return (i);
57 		if (!isprint(*bargs))
58 			break;
59 	}
60 	return (-1);
61 }
62 
63 static char quote[] = "\'";
64 
65 int
66 uadmin(int cmd, int fcn, uintptr_t mdep)
67 {
68 	extern int __uadmin(int cmd, int fcn, uintptr_t mdep);
69 	char *bargs, cmdbuf[256];
70 	struct stat sbuf;
71 	char *altroot;
72 
73 	bargs = (char *)mdep;
74 	if (geteuid() == 0 && getzoneid() == GLOBAL_ZONEID &&
75 	    (cmd == A_SHUTDOWN || cmd == A_REBOOT)) {
76 		switch (fcn) {
77 		case AD_IBOOT:
78 		case AD_SBOOT:
79 		case AD_SIBOOT:
80 			/*
81 			 * These functions fabricate appropriate bootargs.
82 			 * If bootargs are passed in, map these functions
83 			 * to AD_BOOT.
84 			 */
85 			if (bargs == 0) {
86 				switch (fcn) {
87 				case AD_IBOOT:
88 					bargs = "-a";
89 					break;
90 				case AD_SBOOT:
91 					bargs = "-s";
92 					break;
93 				case AD_SIBOOT:
94 					bargs = "-sa";
95 					break;
96 				}
97 			}
98 			/*FALLTHROUGH*/
99 		case AD_BOOT:
100 			if (bargs == 0)
101 				break;	/* no args */
102 			if (legal_arg(bargs) < 0)
103 				break;	/* bad args */
104 
105 			/* check for /stubboot */
106 			if (stat("/stubboot/boot/grub/menu.lst", &sbuf) == 0) {
107 				altroot = "-R /stubboot ";
108 			} else {
109 				altroot = "";
110 			}
111 
112 			/* are we rebooting to a GRUB menu entry? */
113 			if (isdigit(bargs[0])) {
114 				int entry = strtol(bargs, NULL, 10);
115 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
116 				    "/sbin/bootadm set-menu %sdefault=%d",
117 				    altroot, entry);
118 			} else {
119 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
120 				    "/sbin/bootadm -m update_temp %s"
121 				    "-o %s%s%s", altroot, quote, bargs, quote);
122 			}
123 			(void) system(cmdbuf);
124 		}
125 	}
126 	return (__uadmin(cmd, fcn, mdep));
127 }
128