1#!/bin/sh 2# 3# CDDL HEADER START 4# 5# The contents of this file are subject to the terms of the 6# Common Development and Distribution License (the "License"). 7# You may not use this file except in compliance with the License. 8# 9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10# or http://www.opensolaris.org/os/licensing. 11# See the License for the specific language governing permissions 12# and limitations under the License. 13# 14# When distributing Covered Code, include this CDDL HEADER in each 15# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16# If applicable, add the following below this CDDL HEADER, with the 17# fields enclosed by brackets "[]" replaced with your own identifying 18# information: Portions Copyright [yyyy] [name of copyright owner] 19# 20# CDDL HEADER END 21# 22# 23# Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24# Use is subject to license terms. 25# Copyright 2015 Nexenta Systems, Inc. All rights reserved. 26# Copyright 2012 Joyent, Inc. All rights reserved. 27# 28 29smf_present () { 30 [ -r /etc/svc/volatile/repository_door ] && \ 31 [ ! -f /etc/svc/volatile/repository_door ] 32} 33 34smf_clear_env () { 35 unset \ 36 SMF_FMRI \ 37 SMF_METHOD \ 38 SMF_RESTARTER \ 39 SMF_ZONENAME 40} 41 42# smf_console 43# 44# Use as "echo message 2>&1 | smf_console". If SMF_MSGLOG_REDIRECT is 45# unset, message will be displayed to console. SMF_MSGLOG_REDIRECT is 46# reserved for future use. 47# 48smf_console () { 49 /usr/bin/tee ${SMF_MSGLOG_REDIRECT:-/dev/msglog} 50} 51 52# smf_zonename 53# 54# Prints the name of this zone. 55 56smf_zonename() { 57 echo "${SMF_ZONENAME:=`/sbin/zonename`}" 58} 59 60# smf_is_globalzone 61# 62# Returns zero (success) if this is the global zone. 1 otherwise. 63# 64smf_is_globalzone() { 65 [ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" ] && return 0 66 return 1 67} 68 69# smf_is_nonglobalzone 70# 71# Returns zero (success) if this is not the global zone. 1 otherwise. 72# 73smf_is_nonglobalzone() { 74 [ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0 75 return 1 76} 77 78# smf_configure_ip 79# 80# Returns zero (success) if this zone needs IP to be configured i.e. 81# the global zone or has an exclusive stack. 1 otherwise. 82# 83smf_configure_ip() { 84 [ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" -o \ 85 `/sbin/zonename -t` = exclusive ] && return 0 86 return 1 87} 88 89# smf_dont_configure_ip 90# 91# Inverse of smf_configure_ip 92# 93smf_dont_configure_ip() { 94 [ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" -a \ 95 `/sbin/zonename -t` = shared ] && return 0 96 return 1 97} 98 99# smf_dont_configure_vt 100# 101# Returns zero (success) if vt functionality is not to be configured, 102# 1 otherwise. 103# 104smf_dont_configure_vt() { 105 [ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0 106 /usr/lib/vtinfo > /dev/null 2>&1 107 return $? 108} 109 110# smf_is_system_labeled 111# 112# Returns zero (success) if system is labeled (aka Trusted Extensions). 113# 1 otherwise. 114# 115smf_is_system_labeled() { 116 [ ! -x /bin/plabel ] && return 1 117 /bin/plabel > /dev/null 2>&1 118 return $? 119} 120 121# smf_netstrategy 122# -> (_INIT_NET_IF, _INIT_NET_STRATEGY) 123# 124# Sets _INIT_NET_IF to the name for the network-booted 125# interface if we are booting from the network. _INIT_NET_STRATEGY is 126# assigned the value of the current network configuration strategy. 127# Valid values for _INIT_NET_STRATEGY are "none", "dhcp", and "rarp". 128# 129# The network boot strategy for a zone is always "none". 130# 131smf_netstrategy () { 132 if smf_is_nonglobalzone; then 133 _INIT_NET_STRATEGY="none" export _INIT_NET_STRATEGY 134 return 0 135 fi 136 137 set -- `/sbin/netstrategy` 138 if [ $? -eq 0 ]; then 139 [ "$1" = "nfs" ] && \ 140 _INIT_NET_IF="$2" export _INIT_NET_IF 141 _INIT_NET_STRATEGY="$3" export _INIT_NET_STRATEGY 142 else 143 return 1 144 fi 145} 146 147# 148# smf_kill_contract CONTRACT SIGNAL WAIT TIMEOUT 149# 150# To be called from stop methods of non-transient services. 151# Sends SIGNAL to the service contract CONTRACT. If the 152# WAIT argument is non-zero, smf_kill_contract will wait 153# until the contract is empty before returning, or until 154# TIMEOUT expires. 155# 156# Example, send SIGTERM to contract 200: 157# 158# smf_kill_contract 200 TERM 159# 160# Since killing a contract with pkill(1) is not atomic, 161# smf_kill_contract will continue to send SIGNAL to CONTRACT 162# every second until the contract is empty. This will catch 163# races between fork(2) and pkill(1). 164# 165# Note that time in this routine is tracked (after being input 166# via TIMEOUT) in 10ths of a second. This is because we want 167# to sleep for short periods of time, and expr(1) is too dumb 168# to do non-integer math. 169# 170# Returns 1 if the contract is invalid. 171# Returns 2 if WAIT is "1", TIMEOUT is > 0, and TIMEOUT expires. 172# Returns 0 on success. 173# 174smf_kill_contract() { 175 176 time_waited=0 177 time_to_wait=$4 178 179 [ -z "$time_to_wait" ] && time_to_wait=0 180 181 # convert to 10ths. 182 time_to_wait=`/usr/bin/expr $time_to_wait '*' 10` 183 184 # Verify contract id is valid using pgrep 185 /usr/bin/pgrep -c $1 > /dev/null 2>&1 186 ret=$? 187 if [ $ret -gt 1 ] ; then 188 echo "Error, invalid contract \"$1\"" >&2 189 return 1 190 fi 191 192 # Return if contract is already empty. 193 [ $ret -eq 1 ] && return 0 194 195 # Kill contract. 196 /usr/bin/pkill -$2 -c $1 197 if [ $? -gt 1 ] ; then 198 echo "Error, could not kill contract \"$1\"" >&2 199 return 1 200 fi 201 202 # Return if WAIT is not set or is "0" 203 [ -z "$3" ] && return 0 204 [ "$3" -eq 0 ] && return 0 205 206 # If contract does not empty, keep killing the contract to catch 207 # any child processes missed because they were forking 208 /usr/bin/pgrep -c $1 > /dev/null 2>&1 209 while [ $? -eq 0 ] ; do 210 # Return 2 if TIMEOUT was passed, and it has expired 211 [ "$time_to_wait" -gt 0 -a $time_waited -ge $time_to_wait ] && \ 212 return 2 213 214 # 215 # At five second intervals, issue the kill again. Note that 216 # the sleep time constant (in tenths) must be a factor of 50 217 # for the remainder trick to work. i.e. sleeping 2 tenths is 218 # fine, but 27 tenths is not. 219 # 220 remainder=`/usr/bin/expr $time_waited % 50` 221 if [ $time_waited -gt 0 -a $remainder -eq 0 ]; then 222 /usr/bin/pkill -$2 -c $1 223 fi 224 225 # Wait two tenths, and go again. 226 /usr/bin/sleep 0.2 227 time_waited=`/usr/bin/expr $time_waited + 2` 228 /usr/bin/pgrep -c $1 > /dev/null 2>&1 229 done 230 231 return 0 232} 233 234# 235# smf(5) method and monitor exit status definitions 236# SMF_EXIT_ERR_OTHER, although not defined, encompasses all non-zero 237# exit status values. 238# 239# The SMF_EXIT_NODAEMON exit status should be used when a method does not 240# need to run any persistent process. This indicates success, abandons the 241# contract, and allows dependencies to be met. 242# 243SMF_EXIT_OK=0 244SMF_EXIT_NODAEMON=94 245SMF_EXIT_ERR_FATAL=95 246SMF_EXIT_ERR_CONFIG=96 247SMF_EXIT_MON_DEGRADE=97 248SMF_EXIT_MON_OFFLINE=98 249SMF_EXIT_ERR_NOSMF=99 250SMF_EXIT_ERR_PERM=100 251