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