xref: /onnv-gate/usr/src/lib/libc/i386/sys/uadmin.c (revision 2267)
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
5*2267Sdp  * Common Development and Distribution License (the "License").
6*2267Sdp  * 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  */
210Sstevel@tonic-gate /*
22*2267Sdp  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma weak uadmin = _uadmin
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Wrapper function to implement reboot w/ arguments on x86
320Sstevel@tonic-gate  * platforms. Extract reboot arguments and place them in
330Sstevel@tonic-gate  * in a transient entry in /[stub]boot/grub/menu.lst
340Sstevel@tonic-gate  * All other commands are passed through.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include "synonyms.h"
380Sstevel@tonic-gate #include <fcntl.h>
390Sstevel@tonic-gate #include <ctype.h>
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/uadmin.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <string.h>
47*2267Sdp #include <zone.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 
54*2267Sdp 	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;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	bargs = (char *)mdep;
74*2267Sdp 	if (geteuid() == 0 && getzoneid() == GLOBAL_ZONEID &&
75*2267Sdp 	    (cmd == A_SHUTDOWN || cmd == A_REBOOT)) {
760Sstevel@tonic-gate 		switch (fcn) {
770Sstevel@tonic-gate 		case AD_IBOOT:
780Sstevel@tonic-gate 		case AD_SBOOT:
790Sstevel@tonic-gate 		case AD_SIBOOT:
800Sstevel@tonic-gate 			/*
810Sstevel@tonic-gate 			 * These functions fabricate appropriate bootargs.
820Sstevel@tonic-gate 			 * If bootargs are passed in, map these functions
830Sstevel@tonic-gate 			 * to AD_BOOT.
840Sstevel@tonic-gate 			 */
850Sstevel@tonic-gate 			if (bargs == 0) {
860Sstevel@tonic-gate 				switch (fcn) {
870Sstevel@tonic-gate 				case AD_IBOOT:
880Sstevel@tonic-gate 					bargs = "-a";
890Sstevel@tonic-gate 					break;
900Sstevel@tonic-gate 				case AD_SBOOT:
910Sstevel@tonic-gate 					bargs = "-s";
920Sstevel@tonic-gate 					break;
930Sstevel@tonic-gate 				case AD_SIBOOT:
940Sstevel@tonic-gate 					bargs = "-sa";
950Sstevel@tonic-gate 					break;
960Sstevel@tonic-gate 				}
970Sstevel@tonic-gate 			}
980Sstevel@tonic-gate 			/*FALLTHROUGH*/
990Sstevel@tonic-gate 		case AD_BOOT:
1000Sstevel@tonic-gate 			if (bargs == 0)
1010Sstevel@tonic-gate 				break;	/* no args */
1020Sstevel@tonic-gate 			if (legal_arg(bargs) < 0)
1030Sstevel@tonic-gate 				break;	/* bad args */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 			/* check for /stubboot */
1060Sstevel@tonic-gate 			if (stat("/stubboot/boot/grub/menu.lst", &sbuf) == 0) {
1070Sstevel@tonic-gate 				altroot = "-R /stubboot ";
1080Sstevel@tonic-gate 			} else {
1090Sstevel@tonic-gate 				altroot = "";
1100Sstevel@tonic-gate 			}
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 			/* are we rebooting to a GRUB menu entry? */
1130Sstevel@tonic-gate 			if (isdigit(bargs[0])) {
1140Sstevel@tonic-gate 				int entry = strtol(bargs, NULL, 10);
1150Sstevel@tonic-gate 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
1160Sstevel@tonic-gate 				    "/sbin/bootadm set-menu %sdefault=%d",
1170Sstevel@tonic-gate 				    altroot, entry);
1180Sstevel@tonic-gate 			} else {
1190Sstevel@tonic-gate 				(void) snprintf(cmdbuf, sizeof (cmdbuf),
1200Sstevel@tonic-gate 				    "/sbin/bootadm -m update_temp %s"
1210Sstevel@tonic-gate 				    "-o %s%s%s", altroot, quote, bargs, quote);
1220Sstevel@tonic-gate 			}
1230Sstevel@tonic-gate 			(void) system(cmdbuf);
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 	return (__uadmin(cmd, fcn, mdep));
1270Sstevel@tonic-gate }
128