xref: /onnv-gate/usr/src/cmd/svc/shell/smf_include.sh (revision 1676:37f4a3e2bd99)
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#
231573Sdp# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate# Use is subject to license terms.
250Sstevel@tonic-gate#
260Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gatesmf_present () {
290Sstevel@tonic-gate	[ -r /etc/svc/volatile/repository_door ] && \
300Sstevel@tonic-gate	    [ ! -f /etc/svc/volatile/repository_door ]
310Sstevel@tonic-gate}
320Sstevel@tonic-gate
330Sstevel@tonic-gatesmf_clear_env () {
340Sstevel@tonic-gate	unset \
350Sstevel@tonic-gate		SMF_FMRI \
360Sstevel@tonic-gate		SMF_METHOD \
371573Sdp		SMF_RESTARTER \
381573Sdp		SMF_ZONENAME
390Sstevel@tonic-gate}
400Sstevel@tonic-gate
410Sstevel@tonic-gate# smf_console
420Sstevel@tonic-gate#
430Sstevel@tonic-gate#   Use as "echo message 2>&1 | smf_console".  If SMF_MSGLOG_REDIRECT is
440Sstevel@tonic-gate#   unset, message will be displayed to console.  SMF_MSGLOG_REDIRECT is
450Sstevel@tonic-gate#   reserved for future use.
460Sstevel@tonic-gate#
470Sstevel@tonic-gatesmf_console () {
480Sstevel@tonic-gate	/usr/bin/tee ${SMF_MSGLOG_REDIRECT:-/dev/msglog}
490Sstevel@tonic-gate}
500Sstevel@tonic-gate
511573Sdp# smf_zonename
520Sstevel@tonic-gate#
531573Sdp#  Prints the name of this zone.
541573Sdp
551573Sdpsmf_zonename() {
561573Sdp	echo "${SMF_ZONENAME:=`/sbin/zonename`}"
571573Sdp}
581573Sdp
591573Sdp# smf_is_globalzone
601573Sdp#
611573Sdp#  Returns zero (success) if this is the global zone.  1 otherwise.
621573Sdp#
631573Sdpsmf_is_globalzone() {
641573Sdp	[ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" ] && return 0
651573Sdp	return 1
661573Sdp}
671573Sdp
681573Sdp# smf_is_nonglobalzone
691573Sdp#
701573Sdp#  Returns zero (success) if this is not the global zone.  1 otherwise.
711573Sdp#
721573Sdpsmf_is_nonglobalzone() {
731573Sdp	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0
741573Sdp	return 1
751573Sdp}
761573Sdp
77*1676Sjpk# smf_is_system_labeled
78*1676Sjpk#
79*1676Sjpk#  Returns zero (success) if system is labeled (aka Trusted Extensions).
80*1676Sjpk#  1 otherwise.
81*1676Sjpk#
82*1676Sjpksmf_is_system_labeled() {
83*1676Sjpk	[ ! -x /bin/plabel ] && return 1
84*1676Sjpk	/bin/plabel > /dev/null 2>&1
85*1676Sjpk	return $?
86*1676Sjpk}
87*1676Sjpk
880Sstevel@tonic-gate# smf_netstrategy
890Sstevel@tonic-gate#   -> (_INIT_NET_IF, _INIT_NET_STRATEGY)
900Sstevel@tonic-gate#
910Sstevel@tonic-gate#   Sets _INIT_NET_IF to the name for the network-booted
920Sstevel@tonic-gate#   interface if we are booting from the network.  _INIT_NET_STRATEGY is
930Sstevel@tonic-gate#   assigned the value of the current network configuration strategy.
940Sstevel@tonic-gate#   Valid values for _INIT_NET_STRATEGY are "none", "dhcp", and "rarp".
950Sstevel@tonic-gate#
960Sstevel@tonic-gate#   The network boot strategy for a zone is always "none".
970Sstevel@tonic-gate#
980Sstevel@tonic-gatesmf_netstrategy () {
991573Sdp	if smf_is_nonglobalzone; then
1000Sstevel@tonic-gate		_INIT_NET_STRATEGY="none" export _INIT_NET_STRATEGY
1010Sstevel@tonic-gate		return 0
1020Sstevel@tonic-gate	fi
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate	set -- `/sbin/netstrategy`
1050Sstevel@tonic-gate	if [ $? -eq 0 ]; then
1060Sstevel@tonic-gate		[ "$1" = "nfs" -o "$1" = "cachefs" ] && \
1070Sstevel@tonic-gate			_INIT_NET_IF="$2" export _INIT_NET_IF
1080Sstevel@tonic-gate		_INIT_NET_STRATEGY="$3" export _INIT_NET_STRATEGY
1090Sstevel@tonic-gate	else
1100Sstevel@tonic-gate		return 1
1110Sstevel@tonic-gate	fi
1120Sstevel@tonic-gate}
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate#
1150Sstevel@tonic-gate# smf_kill_contract CONTRACT SIGNAL WAIT TIMEOUT
1160Sstevel@tonic-gate#
1170Sstevel@tonic-gate#   To be called from stop methods of non-transient services.
1180Sstevel@tonic-gate#   Sends SIGNAL to the service contract CONTRACT.  If the
1190Sstevel@tonic-gate#   WAIT argument is non-zero, smf_kill_contract will wait
1200Sstevel@tonic-gate#   until the contract is empty before returning, or until
1210Sstevel@tonic-gate#   TIMEOUT expires.
1220Sstevel@tonic-gate#
1230Sstevel@tonic-gate#   Example, send SIGTERM to contract 200:
1240Sstevel@tonic-gate#
1250Sstevel@tonic-gate#       smf_kill_contract 200 TERM
1260Sstevel@tonic-gate#
1270Sstevel@tonic-gate#   Since killing a contract with pkill(1) is not atomic,
1280Sstevel@tonic-gate#   smf_kill_contract will continue to send SIGNAL to CONTRACT
1290Sstevel@tonic-gate#   every second until the contract is empty.  This will catch
1300Sstevel@tonic-gate#   races between fork(2) and pkill(1).
1310Sstevel@tonic-gate#
1320Sstevel@tonic-gate#   Returns 1 if the contract is invalid.
1330Sstevel@tonic-gate#   Returns 2 if WAIT is "1", TIMEOUT is > 0, and TIMEOUT expires.
1340Sstevel@tonic-gate#   Returns 0 on success.
1350Sstevel@tonic-gate#
1360Sstevel@tonic-gatesmf_kill_contract() {
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate	time_waited=0
1390Sstevel@tonic-gate	time_to_wait=$4
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate	[ -z "$time_to_wait" ] && time_to_wait=0
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate	# Verify contract id is valid using pgrep
1440Sstevel@tonic-gate	/usr/bin/pgrep -c $1 > /dev/null 2>&1
1450Sstevel@tonic-gate	ret=$?
1460Sstevel@tonic-gate	if [ $ret -gt 1 ] ; then
1470Sstevel@tonic-gate		echo "Error, invalid contract \"$1\"" >&2
1480Sstevel@tonic-gate		return 1
1490Sstevel@tonic-gate	fi
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate	# Return if contract is already empty.
1520Sstevel@tonic-gate	[ $ret -eq 1 ] && return 0
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate	# Kill contract.
1550Sstevel@tonic-gate	/usr/bin/pkill -$2 -c $1
1560Sstevel@tonic-gate	if [ $? -gt 1 ] ; then
1570Sstevel@tonic-gate		echo "Error, could not kill contract \"$1\"" >&2
1580Sstevel@tonic-gate		return 1
1590Sstevel@tonic-gate	fi
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate	# Return if WAIT is not set or is "0"
1620Sstevel@tonic-gate	[ -z "$3" ] && return 0
1630Sstevel@tonic-gate	[ "$3" -eq 0 ] && return 0
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate	# If contract does not empty, keep killing the contract to catch
1660Sstevel@tonic-gate	# any child processes missed because they were forking
1670Sstevel@tonic-gate	/usr/bin/sleep 5
1680Sstevel@tonic-gate	/usr/bin/pgrep -c $1 > /dev/null 2>&1
1690Sstevel@tonic-gate	while [ $? -eq 0 ] ; do
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate		time_waited=`/usr/bin/expr $time_waited + 5`
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate		# Return  if TIMEOUT was passed, and it has expired
1740Sstevel@tonic-gate		[ "$time_to_wait" -gt 0 -a $time_waited -ge $time_to_wait ] && \
1750Sstevel@tonic-gate		    return 2
1760Sstevel@tonic-gate		/usr/bin/pkill -$2 -c $1
1770Sstevel@tonic-gate		/usr/bin/sleep 5
1780Sstevel@tonic-gate		/usr/bin/pgrep -c $1 > /dev/null 2>&1
1790Sstevel@tonic-gate	done
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate	return 0
1820Sstevel@tonic-gate}
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate#
1850Sstevel@tonic-gate# smf(5) method and monitor exit status definitions
1860Sstevel@tonic-gate#   SMF_EXIT_ERR_OTHER, although not defined, encompasses all non-zero
1870Sstevel@tonic-gate#   exit status values.
1880Sstevel@tonic-gate#
1890Sstevel@tonic-gateSMF_EXIT_OK=0
1900Sstevel@tonic-gateSMF_EXIT_ERR_FATAL=95
1910Sstevel@tonic-gateSMF_EXIT_ERR_CONFIG=96
1920Sstevel@tonic-gateSMF_EXIT_MON_DEGRADE=97
1930Sstevel@tonic-gateSMF_EXIT_MON_OFFLINE=98
1940Sstevel@tonic-gateSMF_EXIT_ERR_NOSMF=99
1950Sstevel@tonic-gateSMF_EXIT_ERR_PERM=100
196