xref: /titanic_44/usr/src/cmd/svc/shell/smf_include.sh (revision 1f78048c070f4d26b2a39b0b7a082172e437ebe7)
17c478bd9Sstevel@tonic-gate#!/bin/sh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
66927f468Sdp# Common Development and Distribution License (the "License").
76927f468Sdp# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#
234d53c7adSDan Price# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gatesmf_present () {
287c478bd9Sstevel@tonic-gate	[ -r /etc/svc/volatile/repository_door ] && \
297c478bd9Sstevel@tonic-gate	    [ ! -f /etc/svc/volatile/repository_door ]
307c478bd9Sstevel@tonic-gate}
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gatesmf_clear_env () {
337c478bd9Sstevel@tonic-gate	unset \
347c478bd9Sstevel@tonic-gate		SMF_FMRI \
357c478bd9Sstevel@tonic-gate		SMF_METHOD \
366927f468Sdp		SMF_RESTARTER \
376927f468Sdp		SMF_ZONENAME
387c478bd9Sstevel@tonic-gate}
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate# smf_console
417c478bd9Sstevel@tonic-gate#
427c478bd9Sstevel@tonic-gate#   Use as "echo message 2>&1 | smf_console".  If SMF_MSGLOG_REDIRECT is
437c478bd9Sstevel@tonic-gate#   unset, message will be displayed to console.  SMF_MSGLOG_REDIRECT is
447c478bd9Sstevel@tonic-gate#   reserved for future use.
457c478bd9Sstevel@tonic-gate#
467c478bd9Sstevel@tonic-gatesmf_console () {
477c478bd9Sstevel@tonic-gate	/usr/bin/tee ${SMF_MSGLOG_REDIRECT:-/dev/msglog}
487c478bd9Sstevel@tonic-gate}
497c478bd9Sstevel@tonic-gate
506927f468Sdp# smf_zonename
517c478bd9Sstevel@tonic-gate#
526927f468Sdp#  Prints the name of this zone.
536927f468Sdp
546927f468Sdpsmf_zonename() {
556927f468Sdp	echo "${SMF_ZONENAME:=`/sbin/zonename`}"
566927f468Sdp}
576927f468Sdp
586927f468Sdp# smf_is_globalzone
596927f468Sdp#
606927f468Sdp#  Returns zero (success) if this is the global zone.  1 otherwise.
616927f468Sdp#
626927f468Sdpsmf_is_globalzone() {
636927f468Sdp	[ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" ] && return 0
646927f468Sdp	return 1
656927f468Sdp}
666927f468Sdp
676927f468Sdp# smf_is_nonglobalzone
686927f468Sdp#
696927f468Sdp#  Returns zero (success) if this is not the global zone.  1 otherwise.
706927f468Sdp#
716927f468Sdpsmf_is_nonglobalzone() {
726927f468Sdp	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0
736927f468Sdp	return 1
746927f468Sdp}
756927f468Sdp
76f4b3ec61Sdh155122# smf_configure_ip
77f4b3ec61Sdh155122#
78f4b3ec61Sdh155122#  Returns zero (success) if this zone needs IP to be configured i.e.
79f4b3ec61Sdh155122#  the global zone or has an exclusive stack.  1 otherwise.
80f4b3ec61Sdh155122#
81f4b3ec61Sdh155122smf_configure_ip() {
82f4b3ec61Sdh155122	[ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" -o \
83f4b3ec61Sdh155122	 `/sbin/zonename -t` = exclusive ] && return 0
84f4b3ec61Sdh155122	return 1
85f4b3ec61Sdh155122}
86f4b3ec61Sdh155122
87f4b3ec61Sdh155122# smf_dont_configure_ip
88f4b3ec61Sdh155122#
89f4b3ec61Sdh155122#  Inverse of smf_configure_ip
90f4b3ec61Sdh155122#
91f4b3ec61Sdh155122smf_dont_configure_ip() {
92f4b3ec61Sdh155122	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" -a \
93f4b3ec61Sdh155122	 `/sbin/zonename -t` = shared ] && return 0
94f4b3ec61Sdh155122	return 1
95f4b3ec61Sdh155122}
96f4b3ec61Sdh155122
97aecfc01dSrui zang - Sun Microsystems - Beijing China# smf_dont_configure_vt
98aecfc01dSrui zang - Sun Microsystems - Beijing China#
99aecfc01dSrui zang - Sun Microsystems - Beijing China#  Returns zero (success) if vt functionality is not to be configured,
100aecfc01dSrui zang - Sun Microsystems - Beijing China#  1 otherwise.
101aecfc01dSrui zang - Sun Microsystems - Beijing China#
102aecfc01dSrui zang - Sun Microsystems - Beijing Chinasmf_dont_configure_vt() {
103aecfc01dSrui zang - Sun Microsystems - Beijing China	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0
104aecfc01dSrui zang - Sun Microsystems - Beijing China	/usr/lib/vtinfo > /dev/null 2>&1
105aecfc01dSrui zang - Sun Microsystems - Beijing China	return $?
106aecfc01dSrui zang - Sun Microsystems - Beijing China}
107aecfc01dSrui zang - Sun Microsystems - Beijing China
10845916cd2Sjpk# smf_is_system_labeled
10945916cd2Sjpk#
11045916cd2Sjpk#  Returns zero (success) if system is labeled (aka Trusted Extensions).
11145916cd2Sjpk#  1 otherwise.
11245916cd2Sjpk#
11345916cd2Sjpksmf_is_system_labeled() {
11445916cd2Sjpk	[ ! -x /bin/plabel ] && return 1
11545916cd2Sjpk	/bin/plabel > /dev/null 2>&1
11645916cd2Sjpk	return $?
11745916cd2Sjpk}
11845916cd2Sjpk
1197c478bd9Sstevel@tonic-gate# smf_netstrategy
1207c478bd9Sstevel@tonic-gate#   -> (_INIT_NET_IF, _INIT_NET_STRATEGY)
1217c478bd9Sstevel@tonic-gate#
1227c478bd9Sstevel@tonic-gate#   Sets _INIT_NET_IF to the name for the network-booted
1237c478bd9Sstevel@tonic-gate#   interface if we are booting from the network.  _INIT_NET_STRATEGY is
1247c478bd9Sstevel@tonic-gate#   assigned the value of the current network configuration strategy.
1257c478bd9Sstevel@tonic-gate#   Valid values for _INIT_NET_STRATEGY are "none", "dhcp", and "rarp".
1267c478bd9Sstevel@tonic-gate#
1277c478bd9Sstevel@tonic-gate#   The network boot strategy for a zone is always "none".
1287c478bd9Sstevel@tonic-gate#
1297c478bd9Sstevel@tonic-gatesmf_netstrategy () {
1306927f468Sdp	if smf_is_nonglobalzone; then
1317c478bd9Sstevel@tonic-gate		_INIT_NET_STRATEGY="none" export _INIT_NET_STRATEGY
1327c478bd9Sstevel@tonic-gate		return 0
1337c478bd9Sstevel@tonic-gate	fi
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate	set -- `/sbin/netstrategy`
1367c478bd9Sstevel@tonic-gate	if [ $? -eq 0 ]; then
1377c478bd9Sstevel@tonic-gate		[ "$1" = "nfs" -o "$1" = "cachefs" ] && \
1387c478bd9Sstevel@tonic-gate			_INIT_NET_IF="$2" export _INIT_NET_IF
1397c478bd9Sstevel@tonic-gate		_INIT_NET_STRATEGY="$3" export _INIT_NET_STRATEGY
1407c478bd9Sstevel@tonic-gate	else
1417c478bd9Sstevel@tonic-gate		return 1
1427c478bd9Sstevel@tonic-gate	fi
1437c478bd9Sstevel@tonic-gate}
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate#
1467c478bd9Sstevel@tonic-gate# smf_kill_contract CONTRACT SIGNAL WAIT TIMEOUT
1477c478bd9Sstevel@tonic-gate#
1487c478bd9Sstevel@tonic-gate#   To be called from stop methods of non-transient services.
1497c478bd9Sstevel@tonic-gate#   Sends SIGNAL to the service contract CONTRACT.  If the
1507c478bd9Sstevel@tonic-gate#   WAIT argument is non-zero, smf_kill_contract will wait
1517c478bd9Sstevel@tonic-gate#   until the contract is empty before returning, or until
1527c478bd9Sstevel@tonic-gate#   TIMEOUT expires.
1537c478bd9Sstevel@tonic-gate#
1547c478bd9Sstevel@tonic-gate#   Example, send SIGTERM to contract 200:
1557c478bd9Sstevel@tonic-gate#
1567c478bd9Sstevel@tonic-gate#       smf_kill_contract 200 TERM
1577c478bd9Sstevel@tonic-gate#
1587c478bd9Sstevel@tonic-gate#   Since killing a contract with pkill(1) is not atomic,
1597c478bd9Sstevel@tonic-gate#   smf_kill_contract will continue to send SIGNAL to CONTRACT
1607c478bd9Sstevel@tonic-gate#   every second until the contract is empty.  This will catch
1617c478bd9Sstevel@tonic-gate#   races between fork(2) and pkill(1).
1627c478bd9Sstevel@tonic-gate#
1634d53c7adSDan Price#   Note that time in this routine is tracked (after being input
1644d53c7adSDan Price#   via TIMEOUT) in 10ths of a second.  This is because we want
1654d53c7adSDan Price#   to sleep for short periods of time, and expr(1) is too dumb
1664d53c7adSDan Price#   to do non-integer math.
1674d53c7adSDan Price#
1687c478bd9Sstevel@tonic-gate#   Returns 1 if the contract is invalid.
1697c478bd9Sstevel@tonic-gate#   Returns 2 if WAIT is "1", TIMEOUT is > 0, and TIMEOUT expires.
1707c478bd9Sstevel@tonic-gate#   Returns 0 on success.
1717c478bd9Sstevel@tonic-gate#
1727c478bd9Sstevel@tonic-gatesmf_kill_contract() {
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate	time_waited=0
1757c478bd9Sstevel@tonic-gate	time_to_wait=$4
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate	[ -z "$time_to_wait" ] && time_to_wait=0
1787c478bd9Sstevel@tonic-gate
1794d53c7adSDan Price	# convert to 10ths.
180*1f78048cSDan Price	time_to_wait=`/usr/bin/expr $time_to_wait '*' 10`
1814d53c7adSDan Price
1827c478bd9Sstevel@tonic-gate	# Verify contract id is valid using pgrep
1837c478bd9Sstevel@tonic-gate	/usr/bin/pgrep -c $1 > /dev/null 2>&1
1847c478bd9Sstevel@tonic-gate	ret=$?
1857c478bd9Sstevel@tonic-gate	if [ $ret -gt 1 ] ; then
1867c478bd9Sstevel@tonic-gate		echo "Error, invalid contract \"$1\"" >&2
1877c478bd9Sstevel@tonic-gate		return 1
1887c478bd9Sstevel@tonic-gate	fi
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate	# Return if contract is already empty.
1917c478bd9Sstevel@tonic-gate	[ $ret -eq 1 ] && return 0
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate	# Kill contract.
1947c478bd9Sstevel@tonic-gate	/usr/bin/pkill -$2 -c $1
1957c478bd9Sstevel@tonic-gate	if [ $? -gt 1 ] ; then
1967c478bd9Sstevel@tonic-gate		echo "Error, could not kill contract \"$1\"" >&2
1977c478bd9Sstevel@tonic-gate		return 1
1987c478bd9Sstevel@tonic-gate	fi
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate	# Return if WAIT is not set or is "0"
2017c478bd9Sstevel@tonic-gate	[ -z "$3" ] && return 0
2027c478bd9Sstevel@tonic-gate	[ "$3" -eq 0 ] && return 0
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate	# If contract does not empty, keep killing the contract to catch
2057c478bd9Sstevel@tonic-gate	# any child processes missed because they were forking
2067c478bd9Sstevel@tonic-gate	/usr/bin/pgrep -c $1 > /dev/null 2>&1
2077c478bd9Sstevel@tonic-gate	while [ $? -eq 0 ] ; do
2084d53c7adSDan Price		# Return 2 if TIMEOUT was passed, and it has expired
2097c478bd9Sstevel@tonic-gate		[ "$time_to_wait" -gt 0 -a $time_waited -ge $time_to_wait ] && \
2107c478bd9Sstevel@tonic-gate		    return 2
2114d53c7adSDan Price
2124d53c7adSDan Price		#
2134d53c7adSDan Price		# At five second intervals, issue the kill again.  Note that
2144d53c7adSDan Price		# the sleep time constant (in tenths) must be a factor of 50
2154d53c7adSDan Price		# for the remainder trick to work.  i.e. sleeping 2 tenths is
2164d53c7adSDan Price		# fine, but 27 tenths is not.
2174d53c7adSDan Price		#
2184d53c7adSDan Price		remainder=`/usr/bin/expr $time_waited % 50`
2194d53c7adSDan Price		if [ $time_waited -gt 0 -a $remainder -eq 0 ]; then
2207c478bd9Sstevel@tonic-gate			/usr/bin/pkill -$2 -c $1
2214d53c7adSDan Price		fi
2224d53c7adSDan Price
2234d53c7adSDan Price		# Wait two tenths, and go again.
2244d53c7adSDan Price		/usr/bin/sleep 0.2
2254d53c7adSDan Price		time_waited=`/usr/bin/expr $time_waited + 2`
2267c478bd9Sstevel@tonic-gate		/usr/bin/pgrep -c $1 > /dev/null 2>&1
2277c478bd9Sstevel@tonic-gate	done
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate	return 0
2307c478bd9Sstevel@tonic-gate}
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate#
2337c478bd9Sstevel@tonic-gate# smf(5) method and monitor exit status definitions
2347c478bd9Sstevel@tonic-gate#   SMF_EXIT_ERR_OTHER, although not defined, encompasses all non-zero
2357c478bd9Sstevel@tonic-gate#   exit status values.
2367c478bd9Sstevel@tonic-gate#
2377c478bd9Sstevel@tonic-gateSMF_EXIT_OK=0
2387c478bd9Sstevel@tonic-gateSMF_EXIT_ERR_FATAL=95
2397c478bd9Sstevel@tonic-gateSMF_EXIT_ERR_CONFIG=96
2407c478bd9Sstevel@tonic-gateSMF_EXIT_MON_DEGRADE=97
2417c478bd9Sstevel@tonic-gateSMF_EXIT_MON_OFFLINE=98
2427c478bd9Sstevel@tonic-gateSMF_EXIT_ERR_NOSMF=99
2437c478bd9Sstevel@tonic-gateSMF_EXIT_ERR_PERM=100
244