xref: /titanic_52/usr/src/cmd/svc/shell/smf_include.sh (revision bd93c05dbd9b8f1e8d2edf48c777bc881f927608)
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.
25*bd93c05dSAlexander Eremin# Copyright 2015 Nexenta Systems, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate#
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gatesmf_present () {
297c478bd9Sstevel@tonic-gate	[ -r /etc/svc/volatile/repository_door ] && \
307c478bd9Sstevel@tonic-gate	    [ ! -f /etc/svc/volatile/repository_door ]
317c478bd9Sstevel@tonic-gate}
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gatesmf_clear_env () {
347c478bd9Sstevel@tonic-gate	unset \
357c478bd9Sstevel@tonic-gate		SMF_FMRI \
367c478bd9Sstevel@tonic-gate		SMF_METHOD \
376927f468Sdp		SMF_RESTARTER \
386927f468Sdp		SMF_ZONENAME
397c478bd9Sstevel@tonic-gate}
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate# smf_console
427c478bd9Sstevel@tonic-gate#
437c478bd9Sstevel@tonic-gate#   Use as "echo message 2>&1 | smf_console".  If SMF_MSGLOG_REDIRECT is
447c478bd9Sstevel@tonic-gate#   unset, message will be displayed to console.  SMF_MSGLOG_REDIRECT is
457c478bd9Sstevel@tonic-gate#   reserved for future use.
467c478bd9Sstevel@tonic-gate#
477c478bd9Sstevel@tonic-gatesmf_console () {
487c478bd9Sstevel@tonic-gate	/usr/bin/tee ${SMF_MSGLOG_REDIRECT:-/dev/msglog}
497c478bd9Sstevel@tonic-gate}
507c478bd9Sstevel@tonic-gate
516927f468Sdp# smf_zonename
527c478bd9Sstevel@tonic-gate#
536927f468Sdp#  Prints the name of this zone.
546927f468Sdp
556927f468Sdpsmf_zonename() {
566927f468Sdp	echo "${SMF_ZONENAME:=`/sbin/zonename`}"
576927f468Sdp}
586927f468Sdp
596927f468Sdp# smf_is_globalzone
606927f468Sdp#
616927f468Sdp#  Returns zero (success) if this is the global zone.  1 otherwise.
626927f468Sdp#
636927f468Sdpsmf_is_globalzone() {
646927f468Sdp	[ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" ] && return 0
656927f468Sdp	return 1
666927f468Sdp}
676927f468Sdp
686927f468Sdp# smf_is_nonglobalzone
696927f468Sdp#
706927f468Sdp#  Returns zero (success) if this is not the global zone.  1 otherwise.
716927f468Sdp#
726927f468Sdpsmf_is_nonglobalzone() {
736927f468Sdp	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0
746927f468Sdp	return 1
756927f468Sdp}
766927f468Sdp
77f4b3ec61Sdh155122# smf_configure_ip
78f4b3ec61Sdh155122#
79f4b3ec61Sdh155122#  Returns zero (success) if this zone needs IP to be configured i.e.
80f4b3ec61Sdh155122#  the global zone or has an exclusive stack.  1 otherwise.
81f4b3ec61Sdh155122#
82f4b3ec61Sdh155122smf_configure_ip() {
83f4b3ec61Sdh155122	[ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" -o \
84f4b3ec61Sdh155122	 `/sbin/zonename -t` = exclusive ] && return 0
85f4b3ec61Sdh155122	return 1
86f4b3ec61Sdh155122}
87f4b3ec61Sdh155122
88f4b3ec61Sdh155122# smf_dont_configure_ip
89f4b3ec61Sdh155122#
90f4b3ec61Sdh155122#  Inverse of smf_configure_ip
91f4b3ec61Sdh155122#
92f4b3ec61Sdh155122smf_dont_configure_ip() {
93f4b3ec61Sdh155122	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" -a \
94f4b3ec61Sdh155122	 `/sbin/zonename -t` = shared ] && return 0
95f4b3ec61Sdh155122	return 1
96f4b3ec61Sdh155122}
97f4b3ec61Sdh155122
98aecfc01dSrui zang - Sun Microsystems - Beijing China# smf_dont_configure_vt
99aecfc01dSrui zang - Sun Microsystems - Beijing China#
100aecfc01dSrui zang - Sun Microsystems - Beijing China#  Returns zero (success) if vt functionality is not to be configured,
101aecfc01dSrui zang - Sun Microsystems - Beijing China#  1 otherwise.
102aecfc01dSrui zang - Sun Microsystems - Beijing China#
103aecfc01dSrui zang - Sun Microsystems - Beijing Chinasmf_dont_configure_vt() {
104aecfc01dSrui zang - Sun Microsystems - Beijing China	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0
105aecfc01dSrui zang - Sun Microsystems - Beijing China	/usr/lib/vtinfo > /dev/null 2>&1
106aecfc01dSrui zang - Sun Microsystems - Beijing China	return $?
107aecfc01dSrui zang - Sun Microsystems - Beijing China}
108aecfc01dSrui zang - Sun Microsystems - Beijing China
10945916cd2Sjpk# smf_is_system_labeled
11045916cd2Sjpk#
11145916cd2Sjpk#  Returns zero (success) if system is labeled (aka Trusted Extensions).
11245916cd2Sjpk#  1 otherwise.
11345916cd2Sjpk#
11445916cd2Sjpksmf_is_system_labeled() {
11545916cd2Sjpk	[ ! -x /bin/plabel ] && return 1
11645916cd2Sjpk	/bin/plabel > /dev/null 2>&1
11745916cd2Sjpk	return $?
11845916cd2Sjpk}
11945916cd2Sjpk
1207c478bd9Sstevel@tonic-gate# smf_netstrategy
1217c478bd9Sstevel@tonic-gate#   -> (_INIT_NET_IF, _INIT_NET_STRATEGY)
1227c478bd9Sstevel@tonic-gate#
1237c478bd9Sstevel@tonic-gate#   Sets _INIT_NET_IF to the name for the network-booted
1247c478bd9Sstevel@tonic-gate#   interface if we are booting from the network.  _INIT_NET_STRATEGY is
1257c478bd9Sstevel@tonic-gate#   assigned the value of the current network configuration strategy.
1267c478bd9Sstevel@tonic-gate#   Valid values for _INIT_NET_STRATEGY are "none", "dhcp", and "rarp".
1277c478bd9Sstevel@tonic-gate#
1287c478bd9Sstevel@tonic-gate#   The network boot strategy for a zone is always "none".
1297c478bd9Sstevel@tonic-gate#
1307c478bd9Sstevel@tonic-gatesmf_netstrategy () {
1316927f468Sdp	if smf_is_nonglobalzone; then
1327c478bd9Sstevel@tonic-gate		_INIT_NET_STRATEGY="none" export _INIT_NET_STRATEGY
1337c478bd9Sstevel@tonic-gate		return 0
1347c478bd9Sstevel@tonic-gate	fi
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate	set -- `/sbin/netstrategy`
1377c478bd9Sstevel@tonic-gate	if [ $? -eq 0 ]; then
138*bd93c05dSAlexander Eremin		[ "$1" = "nfs" ] && \
1397c478bd9Sstevel@tonic-gate			_INIT_NET_IF="$2" export _INIT_NET_IF
1407c478bd9Sstevel@tonic-gate		_INIT_NET_STRATEGY="$3" export _INIT_NET_STRATEGY
1417c478bd9Sstevel@tonic-gate	else
1427c478bd9Sstevel@tonic-gate		return 1
1437c478bd9Sstevel@tonic-gate	fi
1447c478bd9Sstevel@tonic-gate}
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate#
1477c478bd9Sstevel@tonic-gate# smf_kill_contract CONTRACT SIGNAL WAIT TIMEOUT
1487c478bd9Sstevel@tonic-gate#
1497c478bd9Sstevel@tonic-gate#   To be called from stop methods of non-transient services.
1507c478bd9Sstevel@tonic-gate#   Sends SIGNAL to the service contract CONTRACT.  If the
1517c478bd9Sstevel@tonic-gate#   WAIT argument is non-zero, smf_kill_contract will wait
1527c478bd9Sstevel@tonic-gate#   until the contract is empty before returning, or until
1537c478bd9Sstevel@tonic-gate#   TIMEOUT expires.
1547c478bd9Sstevel@tonic-gate#
1557c478bd9Sstevel@tonic-gate#   Example, send SIGTERM to contract 200:
1567c478bd9Sstevel@tonic-gate#
1577c478bd9Sstevel@tonic-gate#       smf_kill_contract 200 TERM
1587c478bd9Sstevel@tonic-gate#
1597c478bd9Sstevel@tonic-gate#   Since killing a contract with pkill(1) is not atomic,
1607c478bd9Sstevel@tonic-gate#   smf_kill_contract will continue to send SIGNAL to CONTRACT
1617c478bd9Sstevel@tonic-gate#   every second until the contract is empty.  This will catch
1627c478bd9Sstevel@tonic-gate#   races between fork(2) and pkill(1).
1637c478bd9Sstevel@tonic-gate#
1644d53c7adSDan Price#   Note that time in this routine is tracked (after being input
1654d53c7adSDan Price#   via TIMEOUT) in 10ths of a second.  This is because we want
1664d53c7adSDan Price#   to sleep for short periods of time, and expr(1) is too dumb
1674d53c7adSDan Price#   to do non-integer math.
1684d53c7adSDan Price#
1697c478bd9Sstevel@tonic-gate#   Returns 1 if the contract is invalid.
1707c478bd9Sstevel@tonic-gate#   Returns 2 if WAIT is "1", TIMEOUT is > 0, and TIMEOUT expires.
1717c478bd9Sstevel@tonic-gate#   Returns 0 on success.
1727c478bd9Sstevel@tonic-gate#
1737c478bd9Sstevel@tonic-gatesmf_kill_contract() {
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate	time_waited=0
1767c478bd9Sstevel@tonic-gate	time_to_wait=$4
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate	[ -z "$time_to_wait" ] && time_to_wait=0
1797c478bd9Sstevel@tonic-gate
1804d53c7adSDan Price	# convert to 10ths.
1811f78048cSDan Price	time_to_wait=`/usr/bin/expr $time_to_wait '*' 10`
1824d53c7adSDan Price
1837c478bd9Sstevel@tonic-gate	# Verify contract id is valid using pgrep
1847c478bd9Sstevel@tonic-gate	/usr/bin/pgrep -c $1 > /dev/null 2>&1
1857c478bd9Sstevel@tonic-gate	ret=$?
1867c478bd9Sstevel@tonic-gate	if [ $ret -gt 1 ] ; then
1877c478bd9Sstevel@tonic-gate		echo "Error, invalid contract \"$1\"" >&2
1887c478bd9Sstevel@tonic-gate		return 1
1897c478bd9Sstevel@tonic-gate	fi
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate	# Return if contract is already empty.
1927c478bd9Sstevel@tonic-gate	[ $ret -eq 1 ] && return 0
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate	# Kill contract.
1957c478bd9Sstevel@tonic-gate	/usr/bin/pkill -$2 -c $1
1967c478bd9Sstevel@tonic-gate	if [ $? -gt 1 ] ; then
1977c478bd9Sstevel@tonic-gate		echo "Error, could not kill contract \"$1\"" >&2
1987c478bd9Sstevel@tonic-gate		return 1
1997c478bd9Sstevel@tonic-gate	fi
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate	# Return if WAIT is not set or is "0"
2027c478bd9Sstevel@tonic-gate	[ -z "$3" ] && return 0
2037c478bd9Sstevel@tonic-gate	[ "$3" -eq 0 ] && return 0
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate	# If contract does not empty, keep killing the contract to catch
2067c478bd9Sstevel@tonic-gate	# any child processes missed because they were forking
2077c478bd9Sstevel@tonic-gate	/usr/bin/pgrep -c $1 > /dev/null 2>&1
2087c478bd9Sstevel@tonic-gate	while [ $? -eq 0 ] ; do
2094d53c7adSDan Price		# Return 2 if TIMEOUT was passed, and it has expired
2107c478bd9Sstevel@tonic-gate		[ "$time_to_wait" -gt 0 -a $time_waited -ge $time_to_wait ] && \
2117c478bd9Sstevel@tonic-gate		    return 2
2124d53c7adSDan Price
2134d53c7adSDan Price		#
2144d53c7adSDan Price		# At five second intervals, issue the kill again.  Note that
2154d53c7adSDan Price		# the sleep time constant (in tenths) must be a factor of 50
2164d53c7adSDan Price		# for the remainder trick to work.  i.e. sleeping 2 tenths is
2174d53c7adSDan Price		# fine, but 27 tenths is not.
2184d53c7adSDan Price		#
2194d53c7adSDan Price		remainder=`/usr/bin/expr $time_waited % 50`
2204d53c7adSDan Price		if [ $time_waited -gt 0 -a $remainder -eq 0 ]; then
2217c478bd9Sstevel@tonic-gate			/usr/bin/pkill -$2 -c $1
2224d53c7adSDan Price		fi
2234d53c7adSDan Price
2244d53c7adSDan Price		# Wait two tenths, and go again.
2254d53c7adSDan Price		/usr/bin/sleep 0.2
2264d53c7adSDan Price		time_waited=`/usr/bin/expr $time_waited + 2`
2277c478bd9Sstevel@tonic-gate		/usr/bin/pgrep -c $1 > /dev/null 2>&1
2287c478bd9Sstevel@tonic-gate	done
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate	return 0
2317c478bd9Sstevel@tonic-gate}
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate#
2347c478bd9Sstevel@tonic-gate# smf(5) method and monitor exit status definitions
2357c478bd9Sstevel@tonic-gate#   SMF_EXIT_ERR_OTHER, although not defined, encompasses all non-zero
2367c478bd9Sstevel@tonic-gate#   exit status values.
2377c478bd9Sstevel@tonic-gate#
2387c478bd9Sstevel@tonic-gateSMF_EXIT_OK=0
2397c478bd9Sstevel@tonic-gateSMF_EXIT_ERR_FATAL=95
2407c478bd9Sstevel@tonic-gateSMF_EXIT_ERR_CONFIG=96
2417c478bd9Sstevel@tonic-gateSMF_EXIT_MON_DEGRADE=97
2427c478bd9Sstevel@tonic-gateSMF_EXIT_MON_OFFLINE=98
2437c478bd9Sstevel@tonic-gateSMF_EXIT_ERR_NOSMF=99
2447c478bd9Sstevel@tonic-gateSMF_EXIT_ERR_PERM=100
245