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