xref: /titanic_44/usr/src/cmd/svc/shell/smf_include.sh (revision 9dd0f810214fdc8e1af881a9a5c4b6927629ff9e)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26#ident	"%Z%%M%	%I%	%E% SMI"
27
28smf_present () {
29	[ -r /etc/svc/volatile/repository_door ] && \
30	    [ ! -f /etc/svc/volatile/repository_door ]
31}
32
33smf_clear_env () {
34	unset \
35		SMF_FMRI \
36		SMF_METHOD \
37		SMF_RESTARTER \
38		SMF_ZONENAME
39}
40
41# smf_console
42#
43#   Use as "echo message 2>&1 | smf_console".  If SMF_MSGLOG_REDIRECT is
44#   unset, message will be displayed to console.  SMF_MSGLOG_REDIRECT is
45#   reserved for future use.
46#
47smf_console () {
48	/usr/bin/tee ${SMF_MSGLOG_REDIRECT:-/dev/msglog}
49}
50
51# smf_zonename
52#
53#  Prints the name of this zone.
54
55smf_zonename() {
56	echo "${SMF_ZONENAME:=`/sbin/zonename`}"
57}
58
59# smf_is_globalzone
60#
61#  Returns zero (success) if this is the global zone.  1 otherwise.
62#
63smf_is_globalzone() {
64	[ "${SMF_ZONENAME:=`/sbin/zonename`}" = "global" ] && return 0
65	return 1
66}
67
68# smf_is_nonglobalzone
69#
70#  Returns zero (success) if this is not the global zone.  1 otherwise.
71#
72smf_is_nonglobalzone() {
73	[ "${SMF_ZONENAME:=`/sbin/zonename`}" != "global" ] && return 0
74	return 1
75}
76
77# smf_is_system_labeled
78#
79#  Returns zero (success) if system is labeled (aka Trusted Extensions).
80#  1 otherwise.
81#
82smf_is_system_labeled() {
83	[ ! -x /bin/plabel ] && return 1
84	/bin/plabel > /dev/null 2>&1
85	return $?
86}
87
88# smf_netstrategy
89#   -> (_INIT_NET_IF, _INIT_NET_STRATEGY)
90#
91#   Sets _INIT_NET_IF to the name for the network-booted
92#   interface if we are booting from the network.  _INIT_NET_STRATEGY is
93#   assigned the value of the current network configuration strategy.
94#   Valid values for _INIT_NET_STRATEGY are "none", "dhcp", and "rarp".
95#
96#   The network boot strategy for a zone is always "none".
97#
98smf_netstrategy () {
99	if smf_is_nonglobalzone; then
100		_INIT_NET_STRATEGY="none" export _INIT_NET_STRATEGY
101		return 0
102	fi
103
104	set -- `/sbin/netstrategy`
105	if [ $? -eq 0 ]; then
106		[ "$1" = "nfs" -o "$1" = "cachefs" ] && \
107			_INIT_NET_IF="$2" export _INIT_NET_IF
108		_INIT_NET_STRATEGY="$3" export _INIT_NET_STRATEGY
109	else
110		return 1
111	fi
112}
113
114#
115# smf_kill_contract CONTRACT SIGNAL WAIT TIMEOUT
116#
117#   To be called from stop methods of non-transient services.
118#   Sends SIGNAL to the service contract CONTRACT.  If the
119#   WAIT argument is non-zero, smf_kill_contract will wait
120#   until the contract is empty before returning, or until
121#   TIMEOUT expires.
122#
123#   Example, send SIGTERM to contract 200:
124#
125#       smf_kill_contract 200 TERM
126#
127#   Since killing a contract with pkill(1) is not atomic,
128#   smf_kill_contract will continue to send SIGNAL to CONTRACT
129#   every second until the contract is empty.  This will catch
130#   races between fork(2) and pkill(1).
131#
132#   Returns 1 if the contract is invalid.
133#   Returns 2 if WAIT is "1", TIMEOUT is > 0, and TIMEOUT expires.
134#   Returns 0 on success.
135#
136smf_kill_contract() {
137
138	time_waited=0
139	time_to_wait=$4
140
141	[ -z "$time_to_wait" ] && time_to_wait=0
142
143	# Verify contract id is valid using pgrep
144	/usr/bin/pgrep -c $1 > /dev/null 2>&1
145	ret=$?
146	if [ $ret -gt 1 ] ; then
147		echo "Error, invalid contract \"$1\"" >&2
148		return 1
149	fi
150
151	# Return if contract is already empty.
152	[ $ret -eq 1 ] && return 0
153
154	# Kill contract.
155	/usr/bin/pkill -$2 -c $1
156	if [ $? -gt 1 ] ; then
157		echo "Error, could not kill contract \"$1\"" >&2
158		return 1
159	fi
160
161	# Return if WAIT is not set or is "0"
162	[ -z "$3" ] && return 0
163	[ "$3" -eq 0 ] && return 0
164
165	# If contract does not empty, keep killing the contract to catch
166	# any child processes missed because they were forking
167	/usr/bin/sleep 5
168	/usr/bin/pgrep -c $1 > /dev/null 2>&1
169	while [ $? -eq 0 ] ; do
170
171		time_waited=`/usr/bin/expr $time_waited + 5`
172
173		# Return  if TIMEOUT was passed, and it has expired
174		[ "$time_to_wait" -gt 0 -a $time_waited -ge $time_to_wait ] && \
175		    return 2
176		/usr/bin/pkill -$2 -c $1
177		/usr/bin/sleep 5
178		/usr/bin/pgrep -c $1 > /dev/null 2>&1
179	done
180
181	return 0
182}
183
184#
185# smf(5) method and monitor exit status definitions
186#   SMF_EXIT_ERR_OTHER, although not defined, encompasses all non-zero
187#   exit status values.
188#
189SMF_EXIT_OK=0
190SMF_EXIT_ERR_FATAL=95
191SMF_EXIT_ERR_CONFIG=96
192SMF_EXIT_MON_DEGRADE=97
193SMF_EXIT_MON_OFFLINE=98
194SMF_EXIT_ERR_NOSMF=99
195SMF_EXIT_ERR_PERM=100
196