xref: /onnv-gate/usr/src/lib/libc/amd64/sys/uadmin.c (revision 10052)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52267Sdp  * Common Development and Distribution License (the "License").
62267Sdp  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215891Sraf 
220Sstevel@tonic-gate /*
239160SSherry.Moore@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Wrapper function to implement reboot w/ arguments on x86
300Sstevel@tonic-gate  * platforms. Extract reboot arguments and place them in
310Sstevel@tonic-gate  * in a transient entry in /[stub]boot/grub/menu.lst
320Sstevel@tonic-gate  * All other commands are passed through.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
356812Sraf #include "lint.h"
360Sstevel@tonic-gate #include <fcntl.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <stdio.h>
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/uadmin.h>
430Sstevel@tonic-gate #include <unistd.h>
447656SSherry.Moore@Sun.COM #include <strings.h>
455891Sraf #include <pthread.h>
462267Sdp #include <zone.h>
47*10052SGangadhar.M@Sun.COM #include <libscf.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static int
500Sstevel@tonic-gate legal_arg(char *bargs)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	int i;
530Sstevel@tonic-gate 
542267Sdp 	for (i = 0; i < BOOTARGS_MAX; i++, bargs++) {
550Sstevel@tonic-gate 		if (*bargs == 0 && i > 0)
560Sstevel@tonic-gate 			return (i);
570Sstevel@tonic-gate 		if (!isprint(*bargs))
580Sstevel@tonic-gate 			break;
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate 	return (-1);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static char quote[] = "\'";
640Sstevel@tonic-gate 
650Sstevel@tonic-gate int
660Sstevel@tonic-gate uadmin(int cmd, int fcn, uintptr_t mdep)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	extern int __uadmin(int cmd, int fcn, uintptr_t mdep);
690Sstevel@tonic-gate 	char *bargs, cmdbuf[256];
700Sstevel@tonic-gate 	struct stat sbuf;
710Sstevel@tonic-gate 	char *altroot;
72*10052SGangadhar.M@Sun.COM 	scf_simple_prop_t *prop = NULL;
73*10052SGangadhar.M@Sun.COM 	uint8_t *ret_val = NULL;
74*10052SGangadhar.M@Sun.COM 	boolean_t update_flag = B_FALSE;
75*10052SGangadhar.M@Sun.COM 	char *fmri = "svc:/system/boot-config:default";
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	bargs = (char *)mdep;
787656SSherry.Moore@Sun.COM 
792267Sdp 	if (geteuid() == 0 && getzoneid() == GLOBAL_ZONEID &&
802267Sdp 	    (cmd == A_SHUTDOWN || cmd == A_REBOOT)) {
817656SSherry.Moore@Sun.COM 		int off = 0;
827656SSherry.Moore@Sun.COM 
830Sstevel@tonic-gate 		switch (fcn) {
840Sstevel@tonic-gate 		case AD_IBOOT:
850Sstevel@tonic-gate 		case AD_SBOOT:
860Sstevel@tonic-gate 		case AD_SIBOOT:
870Sstevel@tonic-gate 			/*
880Sstevel@tonic-gate 			 * These functions fabricate appropriate bootargs.
890Sstevel@tonic-gate 			 * If bootargs are passed in, map these functions
900Sstevel@tonic-gate 			 * to AD_BOOT.
910Sstevel@tonic-gate 			 */
920Sstevel@tonic-gate 			if (bargs == 0) {
930Sstevel@tonic-gate 				switch (fcn) {
940Sstevel@tonic-gate 				case AD_IBOOT:
950Sstevel@tonic-gate 					bargs = "-a";
960Sstevel@tonic-gate 					break;
970Sstevel@tonic-gate 				case AD_SBOOT:
980Sstevel@tonic-gate 					bargs = "-s";
990Sstevel@tonic-gate 					break;
1000Sstevel@tonic-gate 				case AD_SIBOOT:
1010Sstevel@tonic-gate 					bargs = "-sa";
1020Sstevel@tonic-gate 					break;
1030Sstevel@tonic-gate 				}
1040Sstevel@tonic-gate 			}
1050Sstevel@tonic-gate 			/*FALLTHROUGH*/
1060Sstevel@tonic-gate 		case AD_BOOT:
1077656SSherry.Moore@Sun.COM 		case AD_FASTREBOOT:
1080Sstevel@tonic-gate 			if (bargs == 0)
1090Sstevel@tonic-gate 				break;	/* no args */
1100Sstevel@tonic-gate 			if (legal_arg(bargs) < 0)
1110Sstevel@tonic-gate 				break;	/* bad args */
1120Sstevel@tonic-gate 
1135891Sraf 			/* avoid cancellation in system() */
1145891Sraf 			(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
1155891Sraf 			    NULL);
1165891Sraf 
1170Sstevel@tonic-gate 			/* check for /stubboot */
1180Sstevel@tonic-gate 			if (stat("/stubboot/boot/grub/menu.lst", &sbuf) == 0) {
1190Sstevel@tonic-gate 				altroot = "-R /stubboot ";
1200Sstevel@tonic-gate 			} else {
1210Sstevel@tonic-gate 				altroot = "";
1220Sstevel@tonic-gate 			}
1230Sstevel@tonic-gate 
1247656SSherry.Moore@Sun.COM 			if (fcn == AD_FASTREBOOT) {
1257656SSherry.Moore@Sun.COM 				char *newarg, *head;
1267656SSherry.Moore@Sun.COM 				char bargs_scratch[BOOTARGS_MAX];
1277656SSherry.Moore@Sun.COM 
1287656SSherry.Moore@Sun.COM 				bzero(bargs_scratch, BOOTARGS_MAX);
1297656SSherry.Moore@Sun.COM 
1307656SSherry.Moore@Sun.COM 				bcopy(bargs, bargs_scratch, strlen(bargs));
1317656SSherry.Moore@Sun.COM 				head = bargs_scratch;
1327656SSherry.Moore@Sun.COM 				newarg = strtok(bargs_scratch, " ");
1337656SSherry.Moore@Sun.COM 
1349160SSherry.Moore@Sun.COM 				if (newarg == NULL || newarg[0] == '-')
1357656SSherry.Moore@Sun.COM 					break;
1367656SSherry.Moore@Sun.COM 
1377656SSherry.Moore@Sun.COM 				/* First argument is rootdir */
1389160SSherry.Moore@Sun.COM 				if (strncmp(&newarg[strlen(newarg)-4],
1397656SSherry.Moore@Sun.COM 				    "unix", 4) != 0) {
1407656SSherry.Moore@Sun.COM 					newarg = strtok(NULL, " ");
1417656SSherry.Moore@Sun.COM 					off = newarg - head;
1427656SSherry.Moore@Sun.COM 				}
1437656SSherry.Moore@Sun.COM 
1447656SSherry.Moore@Sun.COM 				/*
1457656SSherry.Moore@Sun.COM 				 * If we are using alternate root via
1467656SSherry.Moore@Sun.COM 				 * mountpoint or a different BE, don't
1477656SSherry.Moore@Sun.COM 				 * bother to update the temp menu entry.
1487656SSherry.Moore@Sun.COM 				 */
1497656SSherry.Moore@Sun.COM 				if (off > 0)
1507656SSherry.Moore@Sun.COM 					break;
1517656SSherry.Moore@Sun.COM 			}
1527656SSherry.Moore@Sun.COM 
1530Sstevel@tonic-gate 			/* are we rebooting to a GRUB menu entry? */
1540Sstevel@tonic-gate 			if (isdigit(bargs[0])) {
1550Sstevel@tonic-gate 				int entry = strtol(bargs, NULL, 10);
1560Sstevel@tonic-gate 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
1570Sstevel@tonic-gate 				    "/sbin/bootadm set-menu %sdefault=%d",
1580Sstevel@tonic-gate 				    altroot, entry);
1590Sstevel@tonic-gate 			} else {
1600Sstevel@tonic-gate 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
1610Sstevel@tonic-gate 				    "/sbin/bootadm -m update_temp %s"
1627656SSherry.Moore@Sun.COM 				    "-o %s%s%s", altroot, quote,
1637656SSherry.Moore@Sun.COM 				    &bargs[off], quote);
1640Sstevel@tonic-gate 			}
1650Sstevel@tonic-gate 			(void) system(cmdbuf);
1660Sstevel@tonic-gate 		}
167*10052SGangadhar.M@Sun.COM 
168*10052SGangadhar.M@Sun.COM 		prop = scf_simple_prop_get(NULL, fmri, "config",
169*10052SGangadhar.M@Sun.COM 		    "uadmin_boot_archive_sync");
170*10052SGangadhar.M@Sun.COM 		if (prop) {
171*10052SGangadhar.M@Sun.COM 			if ((ret_val = scf_simple_prop_next_boolean(prop)) !=
172*10052SGangadhar.M@Sun.COM 			    NULL)
173*10052SGangadhar.M@Sun.COM 				update_flag = (*ret_val == 0) ? B_FALSE :
174*10052SGangadhar.M@Sun.COM 				    B_TRUE;
175*10052SGangadhar.M@Sun.COM 			scf_simple_prop_free(prop);
176*10052SGangadhar.M@Sun.COM 		}
177*10052SGangadhar.M@Sun.COM 		if (update_flag == B_TRUE)
178*10052SGangadhar.M@Sun.COM 			(void) system("/sbin/bootadm update-archive");
1790Sstevel@tonic-gate 	}
1807656SSherry.Moore@Sun.COM 
1810Sstevel@tonic-gate 	return (__uadmin(cmd, fcn, mdep));
1820Sstevel@tonic-gate }
183