summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svc/shell/smf_include.sh
blob: e7285c42e750e360f748ea7363047223295f7d94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
# Copyright 2015 Nexenta Systems, Inc. All rights reserved.
# Copyright 2012 Joyent, Inc.  All rights reserved.
# Copyright 2021 Oxide Computer Company
#

smf_present () {
	[ -r /etc/svc/volatile/repository_door ] && \
	    [ ! -f /etc/svc/volatile/repository_door ]
}

smf_clear_env () {
	unset \
		SMF_FMRI \
		SMF_METHOD \
		SMF_RESTARTER \
		SMF_ZONENAME
}

# smf_console
#
#   Use as "echo message 2>&1 | smf_console".  If SMF_MSGLOG_REDIRECT is
#   unset, message will be displayed to console.  SMF_MSGLOG_REDIRECT is
#   reserved for future use.
#
smf_console () {
	/usr/bin/tee ${SMF_MSGLOG_REDIRECT:-/dev/msglog}
}

# smf_zonename
#
#  Prints the name of this zone.

smf_zonename() {
	echo "${SMF_ZONENAME:=`/sbin/zonename`}"
}

# smf_is_globalzone
#
#  Returns zero (success) if this is the global zone.  1 otherwise.
#
smf_is_globalzone() {
	[ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" ] && return 0
	return 1
}

# smf_is_nonglobalzone
#
#  Returns zero (success) if this is not the global zone.  1 otherwise.
#
smf_is_nonglobalzone() {
	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0
	return 1
}

# smf_root_is_ramdisk
#
# Returns zero (success) if the system root file system is mounted from a
# ramdisk, non-zero otherwise.
#
smf_root_is_ramdisk() {
	/lib/svc/bin/rootisramdisk
}

# smf_configure_ip
#
#  Returns zero (success) if this zone needs IP to be configured i.e.
#  the global zone or has an exclusive stack.  1 otherwise.
#
smf_configure_ip() {
	[ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" -o \
	 `/sbin/zonename -t` = exclusive ] && return 0
	return 1
}

# smf_dont_configure_ip
#
#  Inverse of smf_configure_ip
#
smf_dont_configure_ip() {
	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" -a \
	 `/sbin/zonename -t` = shared ] && return 0
	return 1
}

# smf_dont_configure_vt
#
#  Returns zero (success) if vt functionality is not to be configured,
#  1 otherwise.
#
smf_dont_configure_vt() {
	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0
	/usr/lib/vtinfo > /dev/null 2>&1
	return $?
}

# smf_is_system_labeled
#
#  Returns zero (success) if system is labeled (aka Trusted Extensions).
#  1 otherwise.
#
smf_is_system_labeled() {
	[ ! -x /bin/plabel ] && return 1
	/bin/plabel > /dev/null 2>&1
	return $?
}

# smf_netstrategy
#   -> (_INIT_NET_IF, _INIT_NET_STRATEGY)
#
#   Sets _INIT_NET_IF to the name for the network-booted
#   interface if we are booting from the network.  _INIT_NET_STRATEGY is
#   assigned the value of the current network configuration strategy.
#   Valid values for _INIT_NET_STRATEGY are "none", "dhcp", and "rarp".
#
#   The network boot strategy for a zone is always "none".
#
smf_netstrategy () {
	if smf_is_nonglobalzone; then
		_INIT_NET_STRATEGY="none" export _INIT_NET_STRATEGY
		return 0
	fi

	set -- `/sbin/netstrategy`
	if [ $? -eq 0 ]; then
		[ "$1" = "nfs" ] && \
			_INIT_NET_IF="$2" export _INIT_NET_IF
		_INIT_NET_STRATEGY="$3" export _INIT_NET_STRATEGY
	else
		return 1
	fi
}

#
# smf_kill_contract CONTRACT SIGNAL WAIT TIMEOUT
#
#   To be called from stop methods of non-transient services.
#   Sends SIGNAL to the service contract CONTRACT.  If the
#   WAIT argument is non-zero, smf_kill_contract will wait
#   until the contract is empty before returning, or until
#   TIMEOUT expires.
#
#   Example, send SIGTERM to contract 200:
#
#       smf_kill_contract 200 TERM
#
#   Since killing a contract with pkill(1) is not atomic,
#   smf_kill_contract will continue to send SIGNAL to CONTRACT
#   every second until the contract is empty.  This will catch
#   races between fork(2) and pkill(1).
#
#   Note that time in this routine is tracked (after being input
#   via TIMEOUT) in 10ths of a second.  This is because we want
#   to sleep for short periods of time, and expr(1) is too dumb
#   to do non-integer math.
#
#   Returns 1 if the contract is invalid.
#   Returns 2 if WAIT is "1", TIMEOUT is > 0, and TIMEOUT expires.
#   Returns 0 on success.
#
smf_kill_contract() {

	time_waited=0
	time_to_wait=$4

	[ -z "$time_to_wait" ] && time_to_wait=0

	# convert to 10ths.
	time_to_wait=`/usr/bin/expr $time_to_wait '*' 10`

	# Verify contract id is valid using pgrep
	/usr/bin/pgrep -c $1 > /dev/null 2>&1
	ret=$?
	if [ $ret -gt 1 ] ; then
		echo "Error, invalid contract \"$1\"" >&2
		return 1
	fi

	# Return if contract is already empty.
	[ $ret -eq 1 ] && return 0

	# Kill contract.
	/usr/bin/pkill -$2 -c $1
	if [ $? -gt 1 ] ; then
		echo "Error, could not kill contract \"$1\"" >&2
		return 1
	fi

	# Return if WAIT is not set or is "0"
	[ -z "$3" ] && return 0
	[ "$3" -eq 0 ] && return 0

	# If contract does not empty, keep killing the contract to catch
	# any child processes missed because they were forking
	/usr/bin/pgrep -c $1 > /dev/null 2>&1
	while [ $? -eq 0 ] ; do
		# Return 2 if TIMEOUT was passed, and it has expired
		[ "$time_to_wait" -gt 0 -a $time_waited -ge $time_to_wait ] && \
		    return 2

		#
		# At five second intervals, issue the kill again.  Note that
		# the sleep time constant (in tenths) must be a factor of 50
		# for the remainder trick to work.  i.e. sleeping 2 tenths is
		# fine, but 27 tenths is not.
		#
		remainder=`/usr/bin/expr $time_waited % 50`
		if [ $time_waited -gt 0 -a $remainder -eq 0 ]; then
			/usr/bin/pkill -$2 -c $1
		fi

		# Wait two tenths, and go again.
		/usr/bin/sleep 0.2
		time_waited=`/usr/bin/expr $time_waited + 2`
		/usr/bin/pgrep -c $1 > /dev/null 2>&1
	done

	return 0
}

#
# smf(7) method and monitor exit status definitions
#   SMF_EXIT_ERR_OTHER, although not defined, encompasses all non-zero
#   exit status values.
#
# The SMF_EXIT_NODAEMON exit status should be used when a method does not
# need to run any persistent process. This indicates success, abandons the
# contract, and allows dependencies to be met.
#
SMF_EXIT_OK=0
SMF_EXIT_NODAEMON=94
SMF_EXIT_ERR_FATAL=95
SMF_EXIT_ERR_CONFIG=96
SMF_EXIT_MON_DEGRADE=97
SMF_EXIT_MON_OFFLINE=98
SMF_EXIT_ERR_NOSMF=99
SMF_EXIT_ERR_PERM=100