xref: /titanic_44/usr/src/lib/libc/amd64/sys/uadmin.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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 
49 #define	MAX_BOOTARG	256
50 
51 static int
52 legal_arg(char *bargs)
53 {
54 	int i;
55 
56 	for (i = 0; i < MAX_BOOTARG; 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 && (cmd == A_SHUTDOWN || cmd == A_REBOOT)) {
77 		switch (fcn) {
78 		case AD_IBOOT:
79 		case AD_SBOOT:
80 		case AD_SIBOOT:
81 			/*
82 			 * These functions fabricate appropriate bootargs.
83 			 * If bootargs are passed in, map these functions
84 			 * to AD_BOOT.
85 			 */
86 			if (bargs == 0) {
87 				switch (fcn) {
88 				case AD_IBOOT:
89 					bargs = "-a";
90 					break;
91 				case AD_SBOOT:
92 					bargs = "-s";
93 					break;
94 				case AD_SIBOOT:
95 					bargs = "-sa";
96 					break;
97 				}
98 			}
99 			/*FALLTHROUGH*/
100 		case AD_BOOT:
101 			if (bargs == 0)
102 				break;	/* no args */
103 			if (legal_arg(bargs) < 0)
104 				break;	/* bad args */
105 
106 			/* check for /stubboot */
107 			if (stat("/stubboot/boot/grub/menu.lst", &sbuf) == 0) {
108 				altroot = "-R /stubboot ";
109 			} else {
110 				altroot = "";
111 			}
112 
113 			/* are we rebooting to a GRUB menu entry? */
114 			if (isdigit(bargs[0])) {
115 				int entry = strtol(bargs, NULL, 10);
116 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
117 				    "/sbin/bootadm set-menu %sdefault=%d",
118 				    altroot, entry);
119 			} else {
120 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
121 				    "/sbin/bootadm -m update_temp %s"
122 				    "-o %s%s%s", altroot, quote, bargs, quote);
123 			}
124 			(void) system(cmdbuf);
125 		}
126 	}
127 	return (__uadmin(cmd, fcn, mdep));
128 }
129