xref: /titanic_41/usr/src/cmd/svc/shell/routing_include.sh (revision e11c3f44f531fdff80941ce57c065d2ae861cefc)
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
28#
29# These functions are used to help map daemon arguments to appropriate
30# routing properties and back, allowing legacy specifications of daemon
31# arguments to be reflected in SMF property values for daemon services.
32#
33
34#
35# set_routeadm_property inst_fmri propname propvalue
36#
37# Functions sets appropriate property value in routeadm property group
38# (via routeadm -m) for inst_fmri to propvalue.
39#
40set_routeadm_property()
41{
42	/sbin/routeadm -m $1 ${2}="${3}"
43}
44
45#
46# The functions below are used to map from daemon arguments to appropriate
47# routeadm properties (properties that the service user can manipulate
48# to control daemon functionality. getopts is used extensively to
49# retrieve options/values from argument list, and these option values
50# are used to set properties appropriately.
51#
52
53#
54# set_daemon_value_property inst_fmri optstring options option prop
55#	default_value
56#
57# Function looks for option/value in argument string, and sets associated
58# property if found. If a default is specified, and the option is not
59# in the argument string, it will be used.
60#
61set_daemon_value_property()
62{
63	OPTIND=1
64	value_set=""
65	while getopts $3 opt $2; do
66		case $opt in
67			"$4" )	set_routeadm_property $1 $5 $OPTARG
68				value_set="true"
69				;;
70			? )
71		esac
72	done
73	# No value set - use default if specified.
74	if [ -z "$value_set" -a -n "$6" ]; then
75		set_routeadm_property $1 $5 $6
76	fi
77}
78
79#
80# set_daemon_ordered_multivalue_property inst_fmri optstring options option prop
81#       default_value
82#
83# Function looks for option/values in argument string, and sets associated
84# property if found. If a default is specified, and the option is not
85# in the argument string, it will be used.  Use ";" as delimiter for
86# multiple values.
87#
88set_daemon_ordered_multivalue_property()
89{
90	OPTIND=1
91	value_set=""
92	while getopts $3 opt $2; do
93		case $opt in
94			"$4" )  if [ -z "$value_set" ]; then
95					value_set="${OPTARG}"
96				else
97					value_set="$value_set;${OPTARG}"
98				fi
99                                ;;
100			? )
101		esac
102	done
103	if [ -n "$value_set" ]; then
104		set_routeadm_property $1 $5 "$value_set"
105	fi
106	# No value set - use default if specified.
107	if [ -z "$value_set" -a -n "$6" ]; then
108		set_routeadm_property $1 $5 $6
109	fi
110}
111
112#
113# set_daemon_boolean_property inst_fmri optstring options option
114#       prop value_if_found default
115#
116# Function looks for option in argument string, and sets associated
117# property, if found, to value_if_found. If a default is specified, and
118# the option is not found, it will be used.
119#
120set_daemon_boolean_property()
121{
122	OPTIND=1
123	value_set=""
124	while getopts $3 opt $2; do
125		case $opt in
126			"$4" )	set_routeadm_property $1 $5 $6
127				value_set="true"
128				;;
129			? )
130		esac
131	done
132	# No value set - use default if specified.
133	if [ -z "$value_set" -a -n "$7" ]; then
134		set_routeadm_property $1 $5 $7
135	fi
136}
137
138#
139# set_daemon_nonoption_properties inst_fmri optstring options propnames
140#       default
141#
142# Function looks past option list for addition values, and sets properties
143# specified in propnames to additional positional values. If no value
144# is found for additional property, default is used.
145#
146set_daemon_nonoption_properties()
147{
148	OPTIND=1
149	# Skip options
150	while getopts $3 opt $2; do
151		case $opt in
152			? )
153		esac
154	done
155	pos=$OPTIND
156	for prop in $4
157	do
158		val=`/usr/bin/echo $2 | /usr/bin/nawk -v POS=$pos \
159		    '{ print $POS }'`
160		if [ -z "$val" ]; then
161			val="$5"
162		fi
163		set_routeadm_property $1 $prop $val
164		pos=`expr $pos + 1`
165	done
166}
167
168#
169# get_daemon_args $inst_fmri
170#
171# Retrieves routeadm/daemon-args property values, if any.  Removes
172# quotes around values including spaces.
173#
174get_daemon_args()
175{
176	args=`/usr/sbin/svccfg -s $1 listprop routeadm/daemon-args | \
177	    /usr/bin/nawk '{ for (i = 3; i <= NF; i++) printf "%s ", $i }' | \
178	    /usr/bin/nawk '{sub(/^\"/, ""); sub(/\"[ \t]*$/,""); print}'`
179	echo "$args"
180}
181
182#
183# clear_daemon_args $inst_fmri
184#
185# Blanks routeadm/daemon-args property used in upgrade.
186#
187clear_daemon_args()
188{
189	/usr/sbin/svccfg -s $1 delprop routeadm/daemon-args 2>/dev/null
190}
191
192#
193# The functions below are used to map back from property settings to
194# commandline arguments to launch daemons.
195#
196
197get_routeadm_property()
198{
199	propval=`/sbin/routeadm -l $1 | /usr/bin/nawk -v PROP=$2 \
200	    '($1 == PROP) { for (i = 3; i < NF; i++) printf $i" "; \
201	    if (NF >= 3) {printf $NF}}'`
202	echo "$propval"
203}
204
205#
206# get_daemon_option_from_boolean_property inst_fmri prop option value_set
207#
208# Returns appropriate daemon option for boolean property prop - if current
209# value matches value_set.
210#
211get_daemon_option_from_boolean_property()
212{
213	propval=`get_routeadm_property $1 $2`
214	if [ "$propval" = "$4" ]; then
215		echo "${3}"
216	fi
217}
218
219#
220# get_daemon_option_from_property inst_fmri prop option ignore_value
221#
222# Returns appropriate daemon option and associated value (unless value
223# matches ignore_value, in which case nothing is returned).
224#
225get_daemon_option_from_property()
226{
227	propval=`get_routeadm_property $1 $2`
228	if [ "$propval" != "$4" ]; then
229		echo "-${3} \"$propval\""
230	fi
231}
232
233#
234# get_daemon_ordered_multivalue_option_from_property_quoted inst_fmri prop
235# option
236#
237# Returns appropriate daemon option and associated values. Values are
238# quoted, i.e. -A "value1 has spaces" -A "value2 has spaces"
239#
240get_daemon_ordered_multivalue_option_from_property_quoted()
241{
242	# get property values, removing trailing delimiter.
243	propvals=`get_routeadm_property $1 $2 | \
244	    /usr/bin/nawk '{sub(/;[ \t]*$/, ""); print }'`
245	# Substitute switch for internal delimiters, quoting values.
246	fixed_propvals=`/usr/bin/echo $propvals | \
247	    /usr/bin/nawk -v SWITCH="\" -${3} \"" \
248	    '{sub(/;/, SWITCH); print }'`
249	if [ -n "$fixed_propvals" ]; then
250		echo "-${3} \"$fixed_propvals\""
251	fi
252}
253
254#
255# get_daemon_ordered_multivalue_option_from_property inst_fmri prop
256# option
257#
258# Returns appropriate daemon option and associated values. Values are
259# unquoted, i.e. -A value1 -A value2
260#
261get_daemon_ordered_multivalue_option_from_property()
262{
263	# get property values, removing trailing delimiter.
264	propvals=`get_routeadm_property $1 $2 | \
265	    /usr/bin/nawk '{sub(/;[ \t]*$/, ""); print }'`
266	# Substitute switch for internal delimiters.
267	fixed_propvals=`/usr/bin/echo $propvals | \
268	    /usr/bin/nawk -v SWITCH=" -${3} " \
269	    '{sub(/;/, SWITCH); print }'`
270	if [ -n "$fixed_propvals" ]; then
271		echo "-${3} $fixed_propvals"
272	fi
273}
274
275#
276# get_nonoption_property inst_fmri prop ignore_value
277#
278# Returns appropriate non-option property (at end of option list), unless
279# value matches ignore value, in which case nothing is returned.
280#
281get_daemon_nonoption_property()
282{
283	propval=`get_routeadm_property $1 $2`
284	if [ -n "$propval" -a "$propval" != "$3" ]; then
285		echo "$propval"
286	fi
287}
288