xref: /titanic_50/usr/src/lib/libc/i386/sys/uadmin.c (revision d29f5a711240f866521445b1656d114da090335e)
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 /*
30  * Wrapper function to implement reboot w/ arguments on x86
31  * platforms. Extract reboot arguments and place them in
32  * in a transient entry in /[stub]boot/grub/menu.lst
33  * All other commands are passed through.
34  */
35 
36 #include "lint.h"
37 #include <fcntl.h>
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/uadmin.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <pthread.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 			/* avoid cancellation in system() */
106 			(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
107 			    NULL);
108 
109 			/* check for /stubboot */
110 			if (stat("/stubboot/boot/grub/menu.lst", &sbuf) == 0) {
111 				altroot = "-R /stubboot ";
112 			} else {
113 				altroot = "";
114 			}
115 
116 			/* are we rebooting to a GRUB menu entry? */
117 			if (isdigit(bargs[0])) {
118 				int entry = strtol(bargs, NULL, 10);
119 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
120 				    "/sbin/bootadm set-menu %sdefault=%d",
121 				    altroot, entry);
122 			} else {
123 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
124 				    "/sbin/bootadm -m update_temp %s"
125 				    "-o %s%s%s", altroot, quote, bargs, quote);
126 			}
127 			(void) system(cmdbuf);
128 		}
129 	}
130 	return (__uadmin(cmd, fcn, mdep));
131 }
132