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