xref: /onnv-gate/usr/src/lib/libc/amd64/sys/uadmin.c (revision 5891)
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  */
21*5891Sraf 
220Sstevel@tonic-gate /*
23*5891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma weak uadmin = _uadmin
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Wrapper function to implement reboot w/ arguments on x86
330Sstevel@tonic-gate  * platforms. Extract reboot arguments and place them in
340Sstevel@tonic-gate  * in a transient entry in /[stub]boot/grub/menu.lst
350Sstevel@tonic-gate  * All other commands are passed through.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "synonyms.h"
390Sstevel@tonic-gate #include <fcntl.h>
400Sstevel@tonic-gate #include <ctype.h>
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <sys/uadmin.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <string.h>
48*5891Sraf #include <pthread.h>
492267Sdp #include <zone.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static int
520Sstevel@tonic-gate legal_arg(char *bargs)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	int i;
550Sstevel@tonic-gate 
562267Sdp 	for (i = 0; i < BOOTARGS_MAX; i++, bargs++) {
570Sstevel@tonic-gate 		if (*bargs == 0 && i > 0)
580Sstevel@tonic-gate 			return (i);
590Sstevel@tonic-gate 		if (!isprint(*bargs))
600Sstevel@tonic-gate 			break;
610Sstevel@tonic-gate 	}
620Sstevel@tonic-gate 	return (-1);
630Sstevel@tonic-gate }
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static char quote[] = "\'";
660Sstevel@tonic-gate 
670Sstevel@tonic-gate int
680Sstevel@tonic-gate uadmin(int cmd, int fcn, uintptr_t mdep)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	extern int __uadmin(int cmd, int fcn, uintptr_t mdep);
710Sstevel@tonic-gate 	char *bargs, cmdbuf[256];
720Sstevel@tonic-gate 	struct stat sbuf;
730Sstevel@tonic-gate 	char *altroot;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	bargs = (char *)mdep;
762267Sdp 	if (geteuid() == 0 && getzoneid() == GLOBAL_ZONEID &&
772267Sdp 	    (cmd == A_SHUTDOWN || cmd == A_REBOOT)) {
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:
1020Sstevel@tonic-gate 			if (bargs == 0)
1030Sstevel@tonic-gate 				break;	/* no args */
1040Sstevel@tonic-gate 			if (legal_arg(bargs) < 0)
1050Sstevel@tonic-gate 				break;	/* bad args */
1060Sstevel@tonic-gate 
107*5891Sraf 			/* avoid cancellation in system() */
108*5891Sraf 			(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
109*5891Sraf 			    NULL);
110*5891Sraf 
1110Sstevel@tonic-gate 			/* check for /stubboot */
1120Sstevel@tonic-gate 			if (stat("/stubboot/boot/grub/menu.lst", &sbuf) == 0) {
1130Sstevel@tonic-gate 				altroot = "-R /stubboot ";
1140Sstevel@tonic-gate 			} else {
1150Sstevel@tonic-gate 				altroot = "";
1160Sstevel@tonic-gate 			}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 			/* are we rebooting to a GRUB menu entry? */
1190Sstevel@tonic-gate 			if (isdigit(bargs[0])) {
1200Sstevel@tonic-gate 				int entry = strtol(bargs, NULL, 10);
1210Sstevel@tonic-gate 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
1220Sstevel@tonic-gate 				    "/sbin/bootadm set-menu %sdefault=%d",
1230Sstevel@tonic-gate 				    altroot, entry);
1240Sstevel@tonic-gate 			} else {
1250Sstevel@tonic-gate 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
1260Sstevel@tonic-gate 				    "/sbin/bootadm -m update_temp %s"
1270Sstevel@tonic-gate 				    "-o %s%s%s", altroot, quote, bargs, quote);
1280Sstevel@tonic-gate 			}
1290Sstevel@tonic-gate 			(void) system(cmdbuf);
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	return (__uadmin(cmd, fcn, mdep));
1330Sstevel@tonic-gate }
134