xref: /illumos-gate/usr/src/cmd/svc/shell/smf_include.sh (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
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, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#
24# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27#ident	"%Z%%M%	%I%	%E% SMI"
28
29smf_present () {
30	[ -r /etc/svc/volatile/repository_door ] && \
31	    [ ! -f /etc/svc/volatile/repository_door ]
32}
33
34smf_clear_env () {
35	unset \
36		SMF_FMRI \
37		SMF_METHOD \
38		SMF_RESTARTER
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#
52# smf_netstrategy
53#   -> (_INIT_NET_IF, _INIT_NET_STRATEGY)
54#
55#   Sets _INIT_NET_IF to the name for the network-booted
56#   interface if we are booting from the network.  _INIT_NET_STRATEGY is
57#   assigned the value of the current network configuration strategy.
58#   Valid values for _INIT_NET_STRATEGY are "none", "dhcp", and "rarp".
59#
60#   The network boot strategy for a zone is always "none".
61#
62smf_netstrategy () {
63	if [ "${_INIT_ZONENAME:=`/sbin/zonename`}" != "global" ]; then
64		_INIT_NET_STRATEGY="none" export _INIT_NET_STRATEGY
65		return 0
66	fi
67
68	set -- `/sbin/netstrategy`
69	if [ $? -eq 0 ]; then
70		[ "$1" = "nfs" -o "$1" = "cachefs" ] && \
71			_INIT_NET_IF="$2" export _INIT_NET_IF
72		_INIT_NET_STRATEGY="$3" export _INIT_NET_STRATEGY
73	else
74		return 1
75	fi
76}
77
78#
79# smf_kill_contract CONTRACT SIGNAL WAIT TIMEOUT
80#
81#   To be called from stop methods of non-transient services.
82#   Sends SIGNAL to the service contract CONTRACT.  If the
83#   WAIT argument is non-zero, smf_kill_contract will wait
84#   until the contract is empty before returning, or until
85#   TIMEOUT expires.
86#
87#   Example, send SIGTERM to contract 200:
88#
89#       smf_kill_contract 200 TERM
90#
91#   Since killing a contract with pkill(1) is not atomic,
92#   smf_kill_contract will continue to send SIGNAL to CONTRACT
93#   every second until the contract is empty.  This will catch
94#   races between fork(2) and pkill(1).
95#
96#   Returns 1 if the contract is invalid.
97#   Returns 2 if WAIT is "1", TIMEOUT is > 0, and TIMEOUT expires.
98#   Returns 0 on success.
99#
100smf_kill_contract() {
101
102	time_waited=0
103	time_to_wait=$4
104
105	[ -z "$time_to_wait" ] && time_to_wait=0
106
107	# Verify contract id is valid using pgrep
108	/usr/bin/pgrep -c $1 > /dev/null 2>&1
109	ret=$?
110	if [ $ret -gt 1 ] ; then
111		echo "Error, invalid contract \"$1\"" >&2
112		return 1
113	fi
114
115	# Return if contract is already empty.
116	[ $ret -eq 1 ] && return 0
117
118	# Kill contract.
119	/usr/bin/pkill -$2 -c $1
120	if [ $? -gt 1 ] ; then
121		echo "Error, could not kill contract \"$1\"" >&2
122		return 1
123	fi
124
125	# Return if WAIT is not set or is "0"
126	[ -z "$3" ] && return 0
127	[ "$3" -eq 0 ] && return 0
128
129	# If contract does not empty, keep killing the contract to catch
130	# any child processes missed because they were forking
131	/usr/bin/sleep 5
132	/usr/bin/pgrep -c $1 > /dev/null 2>&1
133	while [ $? -eq 0 ] ; do
134
135		time_waited=`/usr/bin/expr $time_waited + 5`
136
137		# Return  if TIMEOUT was passed, and it has expired
138		[ "$time_to_wait" -gt 0 -a $time_waited -ge $time_to_wait ] && \
139		    return 2
140		/usr/bin/pkill -$2 -c $1
141		/usr/bin/sleep 5
142		/usr/bin/pgrep -c $1 > /dev/null 2>&1
143	done
144
145	return 0
146}
147
148#
149# smf(5) method and monitor exit status definitions
150#   SMF_EXIT_ERR_OTHER, although not defined, encompasses all non-zero
151#   exit status values.
152#
153SMF_EXIT_OK=0
154SMF_EXIT_ERR_FATAL=95
155SMF_EXIT_ERR_CONFIG=96
156SMF_EXIT_MON_DEGRADE=97
157SMF_EXIT_MON_OFFLINE=98
158SMF_EXIT_ERR_NOSMF=99
159SMF_EXIT_ERR_PERM=100
160