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 2008 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
25 #
26
27 smf_present () {
28 [ -r /etc/svc/volatile/repository_door ] && \
29 [ ! -f /etc/svc/volatile/repository_door ]
30 }
31
32 smf_clear_env () {
33 unset \
34 SMF_FMRI \
35 SMF_METHOD \
36 SMF_RESTARTER \
37 SMF_ZONENAME
38 }
39
40 # smf_console
41 #
42 # Use as "echo message 2>&1 | smf_console". If SMF_MSGLOG_REDIRECT is
43 # unset, message will be displayed to console. SMF_MSGLOG_REDIRECT is
143 }
144
145 #
146 # smf_kill_contract CONTRACT SIGNAL WAIT TIMEOUT
147 #
148 # To be called from stop methods of non-transient services.
149 # Sends SIGNAL to the service contract CONTRACT. If the
150 # WAIT argument is non-zero, smf_kill_contract will wait
151 # until the contract is empty before returning, or until
152 # TIMEOUT expires.
153 #
154 # Example, send SIGTERM to contract 200:
155 #
156 # smf_kill_contract 200 TERM
157 #
158 # Since killing a contract with pkill(1) is not atomic,
159 # smf_kill_contract will continue to send SIGNAL to CONTRACT
160 # every second until the contract is empty. This will catch
161 # races between fork(2) and pkill(1).
162 #
163 # Returns 1 if the contract is invalid.
164 # Returns 2 if WAIT is "1", TIMEOUT is > 0, and TIMEOUT expires.
165 # Returns 0 on success.
166 #
167 smf_kill_contract() {
168
169 time_waited=0
170 time_to_wait=$4
171
172 [ -z "$time_to_wait" ] && time_to_wait=0
173
174 # Verify contract id is valid using pgrep
175 /usr/bin/pgrep -c $1 > /dev/null 2>&1
176 ret=$?
177 if [ $ret -gt 1 ] ; then
178 echo "Error, invalid contract \"$1\"" >&2
179 return 1
180 fi
181
182 # Return if contract is already empty.
183 [ $ret -eq 1 ] && return 0
184
185 # Kill contract.
186 /usr/bin/pkill -$2 -c $1
187 if [ $? -gt 1 ] ; then
188 echo "Error, could not kill contract \"$1\"" >&2
189 return 1
190 fi
191
192 # Return if WAIT is not set or is "0"
193 [ -z "$3" ] && return 0
194 [ "$3" -eq 0 ] && return 0
195
196 # If contract does not empty, keep killing the contract to catch
197 # any child processes missed because they were forking
198 /usr/bin/sleep 5
199 /usr/bin/pgrep -c $1 > /dev/null 2>&1
200 while [ $? -eq 0 ] ; do
201
202 time_waited=`/usr/bin/expr $time_waited + 5`
203
204 # Return if TIMEOUT was passed, and it has expired
205 [ "$time_to_wait" -gt 0 -a $time_waited -ge $time_to_wait ] && \
206 return 2
207 /usr/bin/pkill -$2 -c $1
208 /usr/bin/sleep 5
209 /usr/bin/pgrep -c $1 > /dev/null 2>&1
210 done
211
212 return 0
213 }
214
215 #
216 # smf(5) method and monitor exit status definitions
217 # SMF_EXIT_ERR_OTHER, although not defined, encompasses all non-zero
218 # exit status values.
219 #
220 SMF_EXIT_OK=0
221 SMF_EXIT_ERR_FATAL=95
222 SMF_EXIT_ERR_CONFIG=96
223 SMF_EXIT_MON_DEGRADE=97
224 SMF_EXIT_MON_OFFLINE=98
225 SMF_EXIT_ERR_NOSMF=99
226 SMF_EXIT_ERR_PERM=100
|
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 #
26
27 smf_present () {
28 [ -r /etc/svc/volatile/repository_door ] && \
29 [ ! -f /etc/svc/volatile/repository_door ]
30 }
31
32 smf_clear_env () {
33 unset \
34 SMF_FMRI \
35 SMF_METHOD \
36 SMF_RESTARTER \
37 SMF_ZONENAME
38 }
39
40 # smf_console
41 #
42 # Use as "echo message 2>&1 | smf_console". If SMF_MSGLOG_REDIRECT is
43 # unset, message will be displayed to console. SMF_MSGLOG_REDIRECT is
143 }
144
145 #
146 # smf_kill_contract CONTRACT SIGNAL WAIT TIMEOUT
147 #
148 # To be called from stop methods of non-transient services.
149 # Sends SIGNAL to the service contract CONTRACT. If the
150 # WAIT argument is non-zero, smf_kill_contract will wait
151 # until the contract is empty before returning, or until
152 # TIMEOUT expires.
153 #
154 # Example, send SIGTERM to contract 200:
155 #
156 # smf_kill_contract 200 TERM
157 #
158 # Since killing a contract with pkill(1) is not atomic,
159 # smf_kill_contract will continue to send SIGNAL to CONTRACT
160 # every second until the contract is empty. This will catch
161 # races between fork(2) and pkill(1).
162 #
163 # Note that time in this routine is tracked (after being input
164 # via TIMEOUT) in 10ths of a second. This is because we want
165 # to sleep for short periods of time, and expr(1) is too dumb
166 # to do non-integer math.
167 #
168 # Returns 1 if the contract is invalid.
169 # Returns 2 if WAIT is "1", TIMEOUT is > 0, and TIMEOUT expires.
170 # Returns 0 on success.
171 #
172 smf_kill_contract() {
173
174 time_waited=0
175 time_to_wait=$4
176
177 [ -z "$time_to_wait" ] && time_to_wait=0
178
179 # convert to 10ths.
180 time_to_wait=`expr $time_to_wait * 10`
181
182 # Verify contract id is valid using pgrep
183 /usr/bin/pgrep -c $1 > /dev/null 2>&1
184 ret=$?
185 if [ $ret -gt 1 ] ; then
186 echo "Error, invalid contract \"$1\"" >&2
187 return 1
188 fi
189
190 # Return if contract is already empty.
191 [ $ret -eq 1 ] && return 0
192
193 # Kill contract.
194 /usr/bin/pkill -$2 -c $1
195 if [ $? -gt 1 ] ; then
196 echo "Error, could not kill contract \"$1\"" >&2
197 return 1
198 fi
199
200 # Return if WAIT is not set or is "0"
201 [ -z "$3" ] && return 0
202 [ "$3" -eq 0 ] && return 0
203
204 # If contract does not empty, keep killing the contract to catch
205 # any child processes missed because they were forking
206 /usr/bin/pgrep -c $1 > /dev/null 2>&1
207 while [ $? -eq 0 ] ; do
208 # Return 2 if TIMEOUT was passed, and it has expired
209 [ "$time_to_wait" -gt 0 -a $time_waited -ge $time_to_wait ] && \
210 return 2
211
212 #
213 # At five second intervals, issue the kill again. Note that
214 # the sleep time constant (in tenths) must be a factor of 50
215 # for the remainder trick to work. i.e. sleeping 2 tenths is
216 # fine, but 27 tenths is not.
217 #
218 remainder=`/usr/bin/expr $time_waited % 50`
219 if [ $time_waited -gt 0 -a $remainder -eq 0 ]; then
220 /usr/bin/pkill -$2 -c $1
221 fi
222
223 # Wait two tenths, and go again.
224 /usr/bin/sleep 0.2
225 time_waited=`/usr/bin/expr $time_waited + 2`
226 /usr/bin/pgrep -c $1 > /dev/null 2>&1
227 done
228
229 return 0
230 }
231
232 #
233 # smf(5) method and monitor exit status definitions
234 # SMF_EXIT_ERR_OTHER, although not defined, encompasses all non-zero
235 # exit status values.
236 #
237 SMF_EXIT_OK=0
238 SMF_EXIT_ERR_FATAL=95
239 SMF_EXIT_ERR_CONFIG=96
240 SMF_EXIT_MON_DEGRADE=97
241 SMF_EXIT_MON_OFFLINE=98
242 SMF_EXIT_ERR_NOSMF=99
243 SMF_EXIT_ERR_PERM=100
|