xref: /onnv-gate/usr/src/cmd/svc/shell/smf_include.sh (revision 8982:63356fcb9349)
10Sstevel@tonic-gate#!/bin/sh
20Sstevel@tonic-gate#
30Sstevel@tonic-gate# CDDL HEADER START
40Sstevel@tonic-gate#
50Sstevel@tonic-gate# The contents of this file are subject to the terms of the
61573Sdp# Common Development and Distribution License (the "License").
71573Sdp# You may not use this file except in compliance with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
220Sstevel@tonic-gate#
238944Sdp@eng.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-gatesmf_present () {
280Sstevel@tonic-gate	[ -r /etc/svc/volatile/repository_door ] && \
290Sstevel@tonic-gate	    [ ! -f /etc/svc/volatile/repository_door ]
300Sstevel@tonic-gate}
310Sstevel@tonic-gate
320Sstevel@tonic-gatesmf_clear_env () {
330Sstevel@tonic-gate	unset \
340Sstevel@tonic-gate		SMF_FMRI \
350Sstevel@tonic-gate		SMF_METHOD \
361573Sdp		SMF_RESTARTER \
371573Sdp		SMF_ZONENAME
380Sstevel@tonic-gate}
390Sstevel@tonic-gate
400Sstevel@tonic-gate# smf_console
410Sstevel@tonic-gate#
420Sstevel@tonic-gate#   Use as "echo message 2>&1 | smf_console".  If SMF_MSGLOG_REDIRECT is
430Sstevel@tonic-gate#   unset, message will be displayed to console.  SMF_MSGLOG_REDIRECT is
440Sstevel@tonic-gate#   reserved for future use.
450Sstevel@tonic-gate#
460Sstevel@tonic-gatesmf_console () {
470Sstevel@tonic-gate	/usr/bin/tee ${SMF_MSGLOG_REDIRECT:-/dev/msglog}
480Sstevel@tonic-gate}
490Sstevel@tonic-gate
501573Sdp# smf_zonename
510Sstevel@tonic-gate#
521573Sdp#  Prints the name of this zone.
531573Sdp
541573Sdpsmf_zonename() {
551573Sdp	echo "${SMF_ZONENAME:=`/sbin/zonename`}"
561573Sdp}
571573Sdp
581573Sdp# smf_is_globalzone
591573Sdp#
601573Sdp#  Returns zero (success) if this is the global zone.  1 otherwise.
611573Sdp#
621573Sdpsmf_is_globalzone() {
631573Sdp	[ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" ] && return 0
641573Sdp	return 1
651573Sdp}
661573Sdp
671573Sdp# smf_is_nonglobalzone
681573Sdp#
691573Sdp#  Returns zero (success) if this is not the global zone.  1 otherwise.
701573Sdp#
711573Sdpsmf_is_nonglobalzone() {
721573Sdp	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0
731573Sdp	return 1
741573Sdp}
751573Sdp
763448Sdh155122# smf_configure_ip
773448Sdh155122#
783448Sdh155122#  Returns zero (success) if this zone needs IP to be configured i.e.
793448Sdh155122#  the global zone or has an exclusive stack.  1 otherwise.
803448Sdh155122#
813448Sdh155122smf_configure_ip() {
823448Sdh155122	[ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" -o \
833448Sdh155122	 `/sbin/zonename -t` = exclusive ] && return 0
843448Sdh155122	return 1
853448Sdh155122}
863448Sdh155122
873448Sdh155122# smf_dont_configure_ip
883448Sdh155122#
893448Sdh155122#  Inverse of smf_configure_ip
903448Sdh155122#
913448Sdh155122smf_dont_configure_ip() {
923448Sdh155122	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" -a \
933448Sdh155122	 `/sbin/zonename -t` = shared ] && return 0
943448Sdh155122	return 1
953448Sdh155122}
963448Sdh155122
977688SAaron.Zang@Sun.COM# smf_dont_configure_vt
987688SAaron.Zang@Sun.COM#
997688SAaron.Zang@Sun.COM#  Returns zero (success) if vt functionality is not to be configured,
1007688SAaron.Zang@Sun.COM#  1 otherwise.
1017688SAaron.Zang@Sun.COM#
1027688SAaron.Zang@Sun.COMsmf_dont_configure_vt() {
1037688SAaron.Zang@Sun.COM	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0
1047688SAaron.Zang@Sun.COM	/usr/lib/vtinfo > /dev/null 2>&1
1057688SAaron.Zang@Sun.COM	return $?
1067688SAaron.Zang@Sun.COM}
1077688SAaron.Zang@Sun.COM
1081676Sjpk# smf_is_system_labeled
1091676Sjpk#
1101676Sjpk#  Returns zero (success) if system is labeled (aka Trusted Extensions).
1111676Sjpk#  1 otherwise.
1121676Sjpk#
1131676Sjpksmf_is_system_labeled() {
1141676Sjpk	[ ! -x /bin/plabel ] && return 1
1151676Sjpk	/bin/plabel > /dev/null 2>&1
1161676Sjpk	return $?
1171676Sjpk}
1181676Sjpk
1190Sstevel@tonic-gate# smf_netstrategy
1200Sstevel@tonic-gate#   -> (_INIT_NET_IF, _INIT_NET_STRATEGY)
1210Sstevel@tonic-gate#
1220Sstevel@tonic-gate#   Sets _INIT_NET_IF to the name for the network-booted
1230Sstevel@tonic-gate#   interface if we are booting from the network.  _INIT_NET_STRATEGY is
1240Sstevel@tonic-gate#   assigned the value of the current network configuration strategy.
1250Sstevel@tonic-gate#   Valid values for _INIT_NET_STRATEGY are "none", "dhcp", and "rarp".
1260Sstevel@tonic-gate#
1270Sstevel@tonic-gate#   The network boot strategy for a zone is always "none".
1280Sstevel@tonic-gate#
1290Sstevel@tonic-gatesmf_netstrategy () {
1301573Sdp	if smf_is_nonglobalzone; then
1310Sstevel@tonic-gate		_INIT_NET_STRATEGY="none" export _INIT_NET_STRATEGY
1320Sstevel@tonic-gate		return 0
1330Sstevel@tonic-gate	fi
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate	set -- `/sbin/netstrategy`
1360Sstevel@tonic-gate	if [ $? -eq 0 ]; then
1370Sstevel@tonic-gate		[ "$1" = "nfs" -o "$1" = "cachefs" ] && \
1380Sstevel@tonic-gate			_INIT_NET_IF="$2" export _INIT_NET_IF
1390Sstevel@tonic-gate		_INIT_NET_STRATEGY="$3" export _INIT_NET_STRATEGY
1400Sstevel@tonic-gate	else
1410Sstevel@tonic-gate		return 1
1420Sstevel@tonic-gate	fi
1430Sstevel@tonic-gate}
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate#
1460Sstevel@tonic-gate# smf_kill_contract CONTRACT SIGNAL WAIT TIMEOUT
1470Sstevel@tonic-gate#
1480Sstevel@tonic-gate#   To be called from stop methods of non-transient services.
1490Sstevel@tonic-gate#   Sends SIGNAL to the service contract CONTRACT.  If the
1500Sstevel@tonic-gate#   WAIT argument is non-zero, smf_kill_contract will wait
1510Sstevel@tonic-gate#   until the contract is empty before returning, or until
1520Sstevel@tonic-gate#   TIMEOUT expires.
1530Sstevel@tonic-gate#
1540Sstevel@tonic-gate#   Example, send SIGTERM to contract 200:
1550Sstevel@tonic-gate#
1560Sstevel@tonic-gate#       smf_kill_contract 200 TERM
1570Sstevel@tonic-gate#
1580Sstevel@tonic-gate#   Since killing a contract with pkill(1) is not atomic,
1590Sstevel@tonic-gate#   smf_kill_contract will continue to send SIGNAL to CONTRACT
1600Sstevel@tonic-gate#   every second until the contract is empty.  This will catch
1610Sstevel@tonic-gate#   races between fork(2) and pkill(1).
1620Sstevel@tonic-gate#
1638944Sdp@eng.sun.com#   Note that time in this routine is tracked (after being input
1648944Sdp@eng.sun.com#   via TIMEOUT) in 10ths of a second.  This is because we want
1658944Sdp@eng.sun.com#   to sleep for short periods of time, and expr(1) is too dumb
1668944Sdp@eng.sun.com#   to do non-integer math.
1678944Sdp@eng.sun.com#
1680Sstevel@tonic-gate#   Returns 1 if the contract is invalid.
1690Sstevel@tonic-gate#   Returns 2 if WAIT is "1", TIMEOUT is > 0, and TIMEOUT expires.
1700Sstevel@tonic-gate#   Returns 0 on success.
1710Sstevel@tonic-gate#
1720Sstevel@tonic-gatesmf_kill_contract() {
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate	time_waited=0
1750Sstevel@tonic-gate	time_to_wait=$4
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate	[ -z "$time_to_wait" ] && time_to_wait=0
1780Sstevel@tonic-gate
1798944Sdp@eng.sun.com	# convert to 10ths.
180*8982Sdp@eng.sun.com	time_to_wait=`/usr/bin/expr $time_to_wait '*' 10`
1818944Sdp@eng.sun.com
1820Sstevel@tonic-gate	# Verify contract id is valid using pgrep
1830Sstevel@tonic-gate	/usr/bin/pgrep -c $1 > /dev/null 2>&1
1840Sstevel@tonic-gate	ret=$?
1850Sstevel@tonic-gate	if [ $ret -gt 1 ] ; then
1860Sstevel@tonic-gate		echo "Error, invalid contract \"$1\"" >&2
1870Sstevel@tonic-gate		return 1
1880Sstevel@tonic-gate	fi
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate	# Return if contract is already empty.
1910Sstevel@tonic-gate	[ $ret -eq 1 ] && return 0
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate	# Kill contract.
1940Sstevel@tonic-gate	/usr/bin/pkill -$2 -c $1
1950Sstevel@tonic-gate	if [ $? -gt 1 ] ; then
1960Sstevel@tonic-gate		echo "Error, could not kill contract \"$1\"" >&2
1970Sstevel@tonic-gate		return 1
1980Sstevel@tonic-gate	fi
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate	# Return if WAIT is not set or is "0"
2010Sstevel@tonic-gate	[ -z "$3" ] && return 0
2020Sstevel@tonic-gate	[ "$3" -eq 0 ] && return 0
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate	# If contract does not empty, keep killing the contract to catch
2050Sstevel@tonic-gate	# any child processes missed because they were forking
2060Sstevel@tonic-gate	/usr/bin/pgrep -c $1 > /dev/null 2>&1
2070Sstevel@tonic-gate	while [ $? -eq 0 ] ; do
2088944Sdp@eng.sun.com		# Return 2 if TIMEOUT was passed, and it has expired
2090Sstevel@tonic-gate		[ "$time_to_wait" -gt 0 -a $time_waited -ge $time_to_wait ] && \
2100Sstevel@tonic-gate		    return 2
2118944Sdp@eng.sun.com
2128944Sdp@eng.sun.com		#
2138944Sdp@eng.sun.com		# At five second intervals, issue the kill again.  Note that
2148944Sdp@eng.sun.com		# the sleep time constant (in tenths) must be a factor of 50
2158944Sdp@eng.sun.com		# for the remainder trick to work.  i.e. sleeping 2 tenths is
2168944Sdp@eng.sun.com		# fine, but 27 tenths is not.
2178944Sdp@eng.sun.com		#
2188944Sdp@eng.sun.com		remainder=`/usr/bin/expr $time_waited % 50`
2198944Sdp@eng.sun.com		if [ $time_waited -gt 0 -a $remainder -eq 0 ]; then
2208944Sdp@eng.sun.com			/usr/bin/pkill -$2 -c $1
2218944Sdp@eng.sun.com		fi
2228944Sdp@eng.sun.com
2238944Sdp@eng.sun.com		# Wait two tenths, and go again.
2248944Sdp@eng.sun.com		/usr/bin/sleep 0.2
2258944Sdp@eng.sun.com		time_waited=`/usr/bin/expr $time_waited + 2`
2260Sstevel@tonic-gate		/usr/bin/pgrep -c $1 > /dev/null 2>&1
2270Sstevel@tonic-gate	done
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate	return 0
2300Sstevel@tonic-gate}
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate#
2330Sstevel@tonic-gate# smf(5) method and monitor exit status definitions
2340Sstevel@tonic-gate#   SMF_EXIT_ERR_OTHER, although not defined, encompasses all non-zero
2350Sstevel@tonic-gate#   exit status values.
2360Sstevel@tonic-gate#
2370Sstevel@tonic-gateSMF_EXIT_OK=0
2380Sstevel@tonic-gateSMF_EXIT_ERR_FATAL=95
2390Sstevel@tonic-gateSMF_EXIT_ERR_CONFIG=96
2400Sstevel@tonic-gateSMF_EXIT_MON_DEGRADE=97
2410Sstevel@tonic-gateSMF_EXIT_MON_OFFLINE=98
2420Sstevel@tonic-gateSMF_EXIT_ERR_NOSMF=99
2430Sstevel@tonic-gateSMF_EXIT_ERR_PERM=100
244