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 /* 235891Sraf * 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 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> 44*7656SSherry.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; 73*7656SSherry.Moore@Sun.COM 742267Sdp if (geteuid() == 0 && getzoneid() == GLOBAL_ZONEID && 752267Sdp (cmd == A_SHUTDOWN || cmd == A_REBOOT)) { 76*7656SSherry.Moore@Sun.COM int off = 0; 77*7656SSherry.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: 102*7656SSherry.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 119*7656SSherry.Moore@Sun.COM if (fcn == AD_FASTREBOOT) { 120*7656SSherry.Moore@Sun.COM char *newarg, *head; 121*7656SSherry.Moore@Sun.COM char bargs_scratch[BOOTARGS_MAX]; 122*7656SSherry.Moore@Sun.COM 123*7656SSherry.Moore@Sun.COM bzero(bargs_scratch, BOOTARGS_MAX); 124*7656SSherry.Moore@Sun.COM 125*7656SSherry.Moore@Sun.COM bcopy(bargs, bargs_scratch, strlen(bargs)); 126*7656SSherry.Moore@Sun.COM head = bargs_scratch; 127*7656SSherry.Moore@Sun.COM newarg = strtok(bargs_scratch, " "); 128*7656SSherry.Moore@Sun.COM 129*7656SSherry.Moore@Sun.COM if (newarg == NULL) 130*7656SSherry.Moore@Sun.COM break; 131*7656SSherry.Moore@Sun.COM 132*7656SSherry.Moore@Sun.COM /* First argument is rootdir */ 133*7656SSherry.Moore@Sun.COM if (newarg[0] != '-' && 134*7656SSherry.Moore@Sun.COM strncmp(&newarg[strlen(newarg)-4], 135*7656SSherry.Moore@Sun.COM "unix", 4) != 0) { 136*7656SSherry.Moore@Sun.COM newarg = strtok(NULL, " "); 137*7656SSherry.Moore@Sun.COM off = newarg - head; 138*7656SSherry.Moore@Sun.COM } 139*7656SSherry.Moore@Sun.COM 140*7656SSherry.Moore@Sun.COM /* 141*7656SSherry.Moore@Sun.COM * If we are using alternate root via 142*7656SSherry.Moore@Sun.COM * mountpoint or a different BE, don't 143*7656SSherry.Moore@Sun.COM * bother to update the temp menu entry. 144*7656SSherry.Moore@Sun.COM */ 145*7656SSherry.Moore@Sun.COM if (off > 0) 146*7656SSherry.Moore@Sun.COM break; 147*7656SSherry.Moore@Sun.COM } 148*7656SSherry.Moore@Sun.COM 1490Sstevel@tonic-gate /* are we rebooting to a GRUB menu entry? */ 1500Sstevel@tonic-gate if (isdigit(bargs[0])) { 1510Sstevel@tonic-gate int entry = strtol(bargs, NULL, 10); 1520Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), 1530Sstevel@tonic-gate "/sbin/bootadm set-menu %sdefault=%d", 1540Sstevel@tonic-gate altroot, entry); 1550Sstevel@tonic-gate } else { 1560Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), 1570Sstevel@tonic-gate "/sbin/bootadm -m update_temp %s" 158*7656SSherry.Moore@Sun.COM "-o %s%s%s", altroot, quote, 159*7656SSherry.Moore@Sun.COM &bargs[off], quote); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate (void) system(cmdbuf); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate } 164*7656SSherry.Moore@Sun.COM 1650Sstevel@tonic-gate return (__uadmin(cmd, fcn, mdep)); 1660Sstevel@tonic-gate } 167