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" 36*10054SJan.Setje-Eilers@Sun.COM #include "mtlib.h" 370Sstevel@tonic-gate #include <fcntl.h> 380Sstevel@tonic-gate #include <ctype.h> 390Sstevel@tonic-gate #include <stdio.h> 400Sstevel@tonic-gate #include <stdlib.h> 410Sstevel@tonic-gate #include <sys/types.h> 420Sstevel@tonic-gate #include <sys/stat.h> 430Sstevel@tonic-gate #include <sys/uadmin.h> 440Sstevel@tonic-gate #include <unistd.h> 457656SSherry.Moore@Sun.COM #include <strings.h> 465891Sraf #include <pthread.h> 472267Sdp #include <zone.h> 4810052SGangadhar.M@Sun.COM #include <libscf.h> 49*10054SJan.Setje-Eilers@Sun.COM #include <thread.h> 50*10054SJan.Setje-Eilers@Sun.COM #include <dlfcn.h> 51*10054SJan.Setje-Eilers@Sun.COM #include <atomic.h> 520Sstevel@tonic-gate 53*10054SJan.Setje-Eilers@Sun.COM /* 54*10054SJan.Setje-Eilers@Sun.COM * Pull in the following three interfaces from libscf without introducing 55*10054SJan.Setje-Eilers@Sun.COM * a dependency on it, which since libscf depends on libc would be circular: 56*10054SJan.Setje-Eilers@Sun.COM * 57*10054SJan.Setje-Eilers@Sun.COM * scf_simple_prop_get 58*10054SJan.Setje-Eilers@Sun.COM * scf_simple_prop_next_boolean 59*10054SJan.Setje-Eilers@Sun.COM * scf_simple_prop_free 60*10054SJan.Setje-Eilers@Sun.COM */ 61*10054SJan.Setje-Eilers@Sun.COM typedef scf_simple_prop_t *(*scf_simple_prop_get_t)(scf_handle_t *, 62*10054SJan.Setje-Eilers@Sun.COM const char *, const char *, const char *); 63*10054SJan.Setje-Eilers@Sun.COM static scf_simple_prop_get_t real_scf_simple_prop_get = NULL; 64*10054SJan.Setje-Eilers@Sun.COM typedef uint8_t *(*scf_simple_prop_next_boolean_t)(scf_simple_prop_t *); 65*10054SJan.Setje-Eilers@Sun.COM static scf_simple_prop_next_boolean_t real_scf_simple_prop_next_boolean = NULL; 66*10054SJan.Setje-Eilers@Sun.COM typedef void (*scf_simple_prop_free_t)(scf_simple_prop_t *); 67*10054SJan.Setje-Eilers@Sun.COM static scf_simple_prop_free_t real_scf_simple_prop_free = NULL; 68*10054SJan.Setje-Eilers@Sun.COM static mutex_t scf_lock = DEFAULTMUTEX; 69*10054SJan.Setje-Eilers@Sun.COM 70*10054SJan.Setje-Eilers@Sun.COM static void 71*10054SJan.Setje-Eilers@Sun.COM load_scf(void) 72*10054SJan.Setje-Eilers@Sun.COM { 73*10054SJan.Setje-Eilers@Sun.COM void *scf_handle = dlopen("libscf.so.1", RTLD_LAZY); 74*10054SJan.Setje-Eilers@Sun.COM scf_simple_prop_get_t scf_simple_prop_get = (scf_handle == NULL)? NULL : 75*10054SJan.Setje-Eilers@Sun.COM (scf_simple_prop_get_t)dlsym(scf_handle, "scf_simple_prop_get"); 76*10054SJan.Setje-Eilers@Sun.COM scf_simple_prop_next_boolean_t scf_simple_prop_next_boolean = 77*10054SJan.Setje-Eilers@Sun.COM (scf_handle == NULL)? NULL : 78*10054SJan.Setje-Eilers@Sun.COM (scf_simple_prop_next_boolean_t)dlsym(scf_handle, 79*10054SJan.Setje-Eilers@Sun.COM "scf_simple_prop_next_boolean"); 80*10054SJan.Setje-Eilers@Sun.COM scf_simple_prop_free_t scf_simple_prop_free = 81*10054SJan.Setje-Eilers@Sun.COM (scf_handle == NULL)? NULL : 82*10054SJan.Setje-Eilers@Sun.COM (scf_simple_prop_free_t)dlsym(scf_handle, "scf_simple_prop_free"); 83*10054SJan.Setje-Eilers@Sun.COM 84*10054SJan.Setje-Eilers@Sun.COM lmutex_lock(&scf_lock); 85*10054SJan.Setje-Eilers@Sun.COM if (real_scf_simple_prop_get == NULL || 86*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_next_boolean == NULL || 87*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_free == NULL) { 88*10054SJan.Setje-Eilers@Sun.COM if (scf_simple_prop_get == NULL) 89*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_get = (scf_simple_prop_get_t)(-1); 90*10054SJan.Setje-Eilers@Sun.COM else { 91*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_get = scf_simple_prop_get; 92*10054SJan.Setje-Eilers@Sun.COM scf_handle = NULL; /* don't dlclose it */ 93*10054SJan.Setje-Eilers@Sun.COM } 94*10054SJan.Setje-Eilers@Sun.COM if (scf_simple_prop_next_boolean == NULL) 95*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_next_boolean = 96*10054SJan.Setje-Eilers@Sun.COM (scf_simple_prop_next_boolean_t)(-1); 97*10054SJan.Setje-Eilers@Sun.COM else { 98*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_next_boolean = 99*10054SJan.Setje-Eilers@Sun.COM scf_simple_prop_next_boolean; 100*10054SJan.Setje-Eilers@Sun.COM scf_handle = NULL; /* don't dlclose it */ 101*10054SJan.Setje-Eilers@Sun.COM } 102*10054SJan.Setje-Eilers@Sun.COM if (scf_simple_prop_free == NULL) 103*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_free = 104*10054SJan.Setje-Eilers@Sun.COM (scf_simple_prop_free_t)(-1); 105*10054SJan.Setje-Eilers@Sun.COM else { 106*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_free = scf_simple_prop_free; 107*10054SJan.Setje-Eilers@Sun.COM scf_handle = NULL; /* don't dlclose it */ 108*10054SJan.Setje-Eilers@Sun.COM } 109*10054SJan.Setje-Eilers@Sun.COM membar_producer(); 110*10054SJan.Setje-Eilers@Sun.COM } 111*10054SJan.Setje-Eilers@Sun.COM lmutex_unlock(&scf_lock); 112*10054SJan.Setje-Eilers@Sun.COM 113*10054SJan.Setje-Eilers@Sun.COM if (scf_handle) 114*10054SJan.Setje-Eilers@Sun.COM (void) dlclose(scf_handle); 115*10054SJan.Setje-Eilers@Sun.COM } 116*10054SJan.Setje-Eilers@Sun.COM 117*10054SJan.Setje-Eilers@Sun.COM static void 118*10054SJan.Setje-Eilers@Sun.COM check_archive_update(void) 119*10054SJan.Setje-Eilers@Sun.COM { 120*10054SJan.Setje-Eilers@Sun.COM scf_simple_prop_t *prop = NULL; 121*10054SJan.Setje-Eilers@Sun.COM boolean_t update_flag = B_FALSE; 122*10054SJan.Setje-Eilers@Sun.COM char *fmri = "svc:/system/boot-config:default"; 123*10054SJan.Setje-Eilers@Sun.COM uint8_t *ret_val = NULL; 124*10054SJan.Setje-Eilers@Sun.COM 125*10054SJan.Setje-Eilers@Sun.COM if (real_scf_simple_prop_get == NULL || 126*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_next_boolean == NULL || 127*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_free == NULL) { 128*10054SJan.Setje-Eilers@Sun.COM load_scf(); 129*10054SJan.Setje-Eilers@Sun.COM } 130*10054SJan.Setje-Eilers@Sun.COM if (real_scf_simple_prop_get == (scf_simple_prop_get_t)(-1) || 131*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_next_boolean == 132*10054SJan.Setje-Eilers@Sun.COM (scf_simple_prop_next_boolean_t)(-1) || 133*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_free == (scf_simple_prop_free_t)(-1)) { 134*10054SJan.Setje-Eilers@Sun.COM return; 135*10054SJan.Setje-Eilers@Sun.COM } 136*10054SJan.Setje-Eilers@Sun.COM 137*10054SJan.Setje-Eilers@Sun.COM prop = real_scf_simple_prop_get(NULL, fmri, "config", 138*10054SJan.Setje-Eilers@Sun.COM "uadmin_boot_archive_sync"); 139*10054SJan.Setje-Eilers@Sun.COM if (prop) { 140*10054SJan.Setje-Eilers@Sun.COM if ((ret_val = real_scf_simple_prop_next_boolean(prop)) != 141*10054SJan.Setje-Eilers@Sun.COM NULL) 142*10054SJan.Setje-Eilers@Sun.COM update_flag = (*ret_val == 0) ? B_FALSE : 143*10054SJan.Setje-Eilers@Sun.COM B_TRUE; 144*10054SJan.Setje-Eilers@Sun.COM real_scf_simple_prop_free(prop); 145*10054SJan.Setje-Eilers@Sun.COM } 146*10054SJan.Setje-Eilers@Sun.COM 147*10054SJan.Setje-Eilers@Sun.COM if (update_flag == B_TRUE) 148*10054SJan.Setje-Eilers@Sun.COM (void) system("/sbin/bootadm update-archive"); 149*10054SJan.Setje-Eilers@Sun.COM } 1500Sstevel@tonic-gate static int 1510Sstevel@tonic-gate legal_arg(char *bargs) 1520Sstevel@tonic-gate { 1530Sstevel@tonic-gate int i; 1540Sstevel@tonic-gate 1552267Sdp for (i = 0; i < BOOTARGS_MAX; i++, bargs++) { 1560Sstevel@tonic-gate if (*bargs == 0 && i > 0) 1570Sstevel@tonic-gate return (i); 1580Sstevel@tonic-gate if (!isprint(*bargs)) 1590Sstevel@tonic-gate break; 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate return (-1); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate static char quote[] = "\'"; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate int 1670Sstevel@tonic-gate uadmin(int cmd, int fcn, uintptr_t mdep) 1680Sstevel@tonic-gate { 1690Sstevel@tonic-gate extern int __uadmin(int cmd, int fcn, uintptr_t mdep); 1700Sstevel@tonic-gate char *bargs, cmdbuf[256]; 1710Sstevel@tonic-gate struct stat sbuf; 1720Sstevel@tonic-gate char *altroot; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate bargs = (char *)mdep; 1757656SSherry.Moore@Sun.COM 1762267Sdp if (geteuid() == 0 && getzoneid() == GLOBAL_ZONEID && 1772267Sdp (cmd == A_SHUTDOWN || cmd == A_REBOOT)) { 1787656SSherry.Moore@Sun.COM int off = 0; 1797656SSherry.Moore@Sun.COM 1800Sstevel@tonic-gate switch (fcn) { 1810Sstevel@tonic-gate case AD_IBOOT: 1820Sstevel@tonic-gate case AD_SBOOT: 1830Sstevel@tonic-gate case AD_SIBOOT: 1840Sstevel@tonic-gate /* 1850Sstevel@tonic-gate * These functions fabricate appropriate bootargs. 1860Sstevel@tonic-gate * If bootargs are passed in, map these functions 1870Sstevel@tonic-gate * to AD_BOOT. 1880Sstevel@tonic-gate */ 1890Sstevel@tonic-gate if (bargs == 0) { 1900Sstevel@tonic-gate switch (fcn) { 1910Sstevel@tonic-gate case AD_IBOOT: 1920Sstevel@tonic-gate bargs = "-a"; 1930Sstevel@tonic-gate break; 1940Sstevel@tonic-gate case AD_SBOOT: 1950Sstevel@tonic-gate bargs = "-s"; 1960Sstevel@tonic-gate break; 1970Sstevel@tonic-gate case AD_SIBOOT: 1980Sstevel@tonic-gate bargs = "-sa"; 1990Sstevel@tonic-gate break; 2000Sstevel@tonic-gate } 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate /*FALLTHROUGH*/ 2030Sstevel@tonic-gate case AD_BOOT: 2047656SSherry.Moore@Sun.COM case AD_FASTREBOOT: 2050Sstevel@tonic-gate if (bargs == 0) 2060Sstevel@tonic-gate break; /* no args */ 2070Sstevel@tonic-gate if (legal_arg(bargs) < 0) 2080Sstevel@tonic-gate break; /* bad args */ 2090Sstevel@tonic-gate 2105891Sraf /* avoid cancellation in system() */ 2115891Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 2125891Sraf NULL); 2135891Sraf 2140Sstevel@tonic-gate /* check for /stubboot */ 2150Sstevel@tonic-gate if (stat("/stubboot/boot/grub/menu.lst", &sbuf) == 0) { 2160Sstevel@tonic-gate altroot = "-R /stubboot "; 2170Sstevel@tonic-gate } else { 2180Sstevel@tonic-gate altroot = ""; 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate 2217656SSherry.Moore@Sun.COM if (fcn == AD_FASTREBOOT) { 2227656SSherry.Moore@Sun.COM char *newarg, *head; 2237656SSherry.Moore@Sun.COM char bargs_scratch[BOOTARGS_MAX]; 2247656SSherry.Moore@Sun.COM 2257656SSherry.Moore@Sun.COM bzero(bargs_scratch, BOOTARGS_MAX); 2267656SSherry.Moore@Sun.COM 2277656SSherry.Moore@Sun.COM bcopy(bargs, bargs_scratch, strlen(bargs)); 2287656SSherry.Moore@Sun.COM head = bargs_scratch; 2297656SSherry.Moore@Sun.COM newarg = strtok(bargs_scratch, " "); 2307656SSherry.Moore@Sun.COM 2319160SSherry.Moore@Sun.COM if (newarg == NULL || newarg[0] == '-') 2327656SSherry.Moore@Sun.COM break; 2337656SSherry.Moore@Sun.COM 2347656SSherry.Moore@Sun.COM /* First argument is rootdir */ 2359160SSherry.Moore@Sun.COM if (strncmp(&newarg[strlen(newarg)-4], 2367656SSherry.Moore@Sun.COM "unix", 4) != 0) { 2377656SSherry.Moore@Sun.COM newarg = strtok(NULL, " "); 2387656SSherry.Moore@Sun.COM off = newarg - head; 2397656SSherry.Moore@Sun.COM } 2407656SSherry.Moore@Sun.COM 2417656SSherry.Moore@Sun.COM /* 2427656SSherry.Moore@Sun.COM * If we are using alternate root via 2437656SSherry.Moore@Sun.COM * mountpoint or a different BE, don't 2447656SSherry.Moore@Sun.COM * bother to update the temp menu entry. 2457656SSherry.Moore@Sun.COM */ 2467656SSherry.Moore@Sun.COM if (off > 0) 2477656SSherry.Moore@Sun.COM break; 2487656SSherry.Moore@Sun.COM } 2497656SSherry.Moore@Sun.COM 2500Sstevel@tonic-gate /* are we rebooting to a GRUB menu entry? */ 2510Sstevel@tonic-gate if (isdigit(bargs[0])) { 2520Sstevel@tonic-gate int entry = strtol(bargs, NULL, 10); 2530Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), 2540Sstevel@tonic-gate "/sbin/bootadm set-menu %sdefault=%d", 2550Sstevel@tonic-gate altroot, entry); 2560Sstevel@tonic-gate } else { 2570Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), 2580Sstevel@tonic-gate "/sbin/bootadm -m update_temp %s" 2597656SSherry.Moore@Sun.COM "-o %s%s%s", altroot, quote, 2607656SSherry.Moore@Sun.COM &bargs[off], quote); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate (void) system(cmdbuf); 2630Sstevel@tonic-gate } 264*10054SJan.Setje-Eilers@Sun.COM check_archive_update(); 2650Sstevel@tonic-gate } 2667656SSherry.Moore@Sun.COM 2670Sstevel@tonic-gate return (__uadmin(cmd, fcn, mdep)); 2680Sstevel@tonic-gate } 269