xref: /onnv-gate/usr/src/lib/libc/amd64/sys/uadmin.c (revision 9160)
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 /*
23*9160SSherry.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>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static int
490Sstevel@tonic-gate legal_arg(char *bargs)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	int i;
520Sstevel@tonic-gate 
532267Sdp 	for (i = 0; i < BOOTARGS_MAX; i++, bargs++) {
540Sstevel@tonic-gate 		if (*bargs == 0 && i > 0)
550Sstevel@tonic-gate 			return (i);
560Sstevel@tonic-gate 		if (!isprint(*bargs))
570Sstevel@tonic-gate 			break;
580Sstevel@tonic-gate 	}
590Sstevel@tonic-gate 	return (-1);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static char quote[] = "\'";
630Sstevel@tonic-gate 
640Sstevel@tonic-gate int
650Sstevel@tonic-gate uadmin(int cmd, int fcn, uintptr_t mdep)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	extern int __uadmin(int cmd, int fcn, uintptr_t mdep);
680Sstevel@tonic-gate 	char *bargs, cmdbuf[256];
690Sstevel@tonic-gate 	struct stat sbuf;
700Sstevel@tonic-gate 	char *altroot;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	bargs = (char *)mdep;
737656SSherry.Moore@Sun.COM 
742267Sdp 	if (geteuid() == 0 && getzoneid() == GLOBAL_ZONEID &&
752267Sdp 	    (cmd == A_SHUTDOWN || cmd == A_REBOOT)) {
767656SSherry.Moore@Sun.COM 		int off = 0;
777656SSherry.Moore@Sun.COM 
780Sstevel@tonic-gate 		switch (fcn) {
790Sstevel@tonic-gate 		case AD_IBOOT:
800Sstevel@tonic-gate 		case AD_SBOOT:
810Sstevel@tonic-gate 		case AD_SIBOOT:
820Sstevel@tonic-gate 			/*
830Sstevel@tonic-gate 			 * These functions fabricate appropriate bootargs.
840Sstevel@tonic-gate 			 * If bootargs are passed in, map these functions
850Sstevel@tonic-gate 			 * to AD_BOOT.
860Sstevel@tonic-gate 			 */
870Sstevel@tonic-gate 			if (bargs == 0) {
880Sstevel@tonic-gate 				switch (fcn) {
890Sstevel@tonic-gate 				case AD_IBOOT:
900Sstevel@tonic-gate 					bargs = "-a";
910Sstevel@tonic-gate 					break;
920Sstevel@tonic-gate 				case AD_SBOOT:
930Sstevel@tonic-gate 					bargs = "-s";
940Sstevel@tonic-gate 					break;
950Sstevel@tonic-gate 				case AD_SIBOOT:
960Sstevel@tonic-gate 					bargs = "-sa";
970Sstevel@tonic-gate 					break;
980Sstevel@tonic-gate 				}
990Sstevel@tonic-gate 			}
1000Sstevel@tonic-gate 			/*FALLTHROUGH*/
1010Sstevel@tonic-gate 		case AD_BOOT:
1027656SSherry.Moore@Sun.COM 		case AD_FASTREBOOT:
1030Sstevel@tonic-gate 			if (bargs == 0)
1040Sstevel@tonic-gate 				break;	/* no args */
1050Sstevel@tonic-gate 			if (legal_arg(bargs) < 0)
1060Sstevel@tonic-gate 				break;	/* bad args */
1070Sstevel@tonic-gate 
1085891Sraf 			/* avoid cancellation in system() */
1095891Sraf 			(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
1105891Sraf 			    NULL);
1115891Sraf 
1120Sstevel@tonic-gate 			/* check for /stubboot */
1130Sstevel@tonic-gate 			if (stat("/stubboot/boot/grub/menu.lst", &sbuf) == 0) {
1140Sstevel@tonic-gate 				altroot = "-R /stubboot ";
1150Sstevel@tonic-gate 			} else {
1160Sstevel@tonic-gate 				altroot = "";
1170Sstevel@tonic-gate 			}
1180Sstevel@tonic-gate 
1197656SSherry.Moore@Sun.COM 			if (fcn == AD_FASTREBOOT) {
1207656SSherry.Moore@Sun.COM 				char *newarg, *head;
1217656SSherry.Moore@Sun.COM 				char bargs_scratch[BOOTARGS_MAX];
1227656SSherry.Moore@Sun.COM 
1237656SSherry.Moore@Sun.COM 				bzero(bargs_scratch, BOOTARGS_MAX);
1247656SSherry.Moore@Sun.COM 
1257656SSherry.Moore@Sun.COM 				bcopy(bargs, bargs_scratch, strlen(bargs));
1267656SSherry.Moore@Sun.COM 				head = bargs_scratch;
1277656SSherry.Moore@Sun.COM 				newarg = strtok(bargs_scratch, " ");
1287656SSherry.Moore@Sun.COM 
129*9160SSherry.Moore@Sun.COM 				if (newarg == NULL || newarg[0] == '-')
1307656SSherry.Moore@Sun.COM 					break;
1317656SSherry.Moore@Sun.COM 
1327656SSherry.Moore@Sun.COM 				/* First argument is rootdir */
133*9160SSherry.Moore@Sun.COM 				if (strncmp(&newarg[strlen(newarg)-4],
1347656SSherry.Moore@Sun.COM 				    "unix", 4) != 0) {
1357656SSherry.Moore@Sun.COM 					newarg = strtok(NULL, " ");
1367656SSherry.Moore@Sun.COM 					off = newarg - head;
1377656SSherry.Moore@Sun.COM 				}
1387656SSherry.Moore@Sun.COM 
1397656SSherry.Moore@Sun.COM 				/*
1407656SSherry.Moore@Sun.COM 				 * If we are using alternate root via
1417656SSherry.Moore@Sun.COM 				 * mountpoint or a different BE, don't
1427656SSherry.Moore@Sun.COM 				 * bother to update the temp menu entry.
1437656SSherry.Moore@Sun.COM 				 */
1447656SSherry.Moore@Sun.COM 				if (off > 0)
1457656SSherry.Moore@Sun.COM 					break;
1467656SSherry.Moore@Sun.COM 			}
1477656SSherry.Moore@Sun.COM 
1480Sstevel@tonic-gate 			/* are we rebooting to a GRUB menu entry? */
1490Sstevel@tonic-gate 			if (isdigit(bargs[0])) {
1500Sstevel@tonic-gate 				int entry = strtol(bargs, NULL, 10);
1510Sstevel@tonic-gate 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
1520Sstevel@tonic-gate 				    "/sbin/bootadm set-menu %sdefault=%d",
1530Sstevel@tonic-gate 				    altroot, entry);
1540Sstevel@tonic-gate 			} else {
1550Sstevel@tonic-gate 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
1560Sstevel@tonic-gate 				    "/sbin/bootadm -m update_temp %s"
1577656SSherry.Moore@Sun.COM 				    "-o %s%s%s", altroot, quote,
1587656SSherry.Moore@Sun.COM 				    &bargs[off], quote);
1590Sstevel@tonic-gate 			}
1600Sstevel@tonic-gate 			(void) system(cmdbuf);
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 	}
1637656SSherry.Moore@Sun.COM 
1640Sstevel@tonic-gate 	return (__uadmin(cmd, fcn, mdep));
1650Sstevel@tonic-gate }
166