xref: /titanic_44/usr/src/cmd/power/sysidpm.sh (revision 043728a3b744c81c506dc22d914fda48a547bec9)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26# ident	"%Z%%M%	%I%	%E% SMI"
27
28# The -c and -u options are used by system configruation.
29USAGE="$0 [-c|-u]"
30TEXTDOMAIN="SUNW_OST_OSCMD"
31export TEXTDOMAIN
32
33# As of Oct. 1, 1995, any new system shipped will have root
34# property "energystar-v2" defined in its prom.
35ESTAR_PROP="energystar-v2"
36
37# Power Management configuration file
38PWR_CONF=/etc/power.conf
39SHUTDOWN_PATTERN="autoshutdown[ 	]"
40TMP=/var/run/tmp1.$$
41
42# If this flag is present, user will be asked the autoshutdown
43# question again even when autoshutdown is already configured.
44ASK_AGAIN_FLAG=/etc/.PM_RECONFIGURE
45
46# This is provided for auto-install.
47# If either of the files is present, autoshutdown will be configured
48# accordingly silently.
49SHUTDOWN_ENABLE_FLAG=/autoshutdown
50SHUTDOWN_DISABLE_FLAG=/noautoshutdown
51
52# Autoshutdown is not supported on diskless systems.
53IS_DISKLESS=""
54
55# Default autoshutdown setup.
56DEFAULT_IDLE_TIME="30"
57DEFAULT_START_TIME="9:00"
58DEFAULT_FINISH_TIME="9:00"
59
60# Currently autoshutdown setup in the configuration file.
61CURRENT_IDLE_TIME=""
62CURRENT_START_TIME=""
63CURRENT_FINISH_TIME=""
64CURRENT_BEHAVIOR=""
65
66# Autoshutdown confirmation message to be prompted in the question1.
67# In the following message, each escape sequence requires three
68# backslashes to prevent the interpretation by the shell and gettext
69# before being passed to ckyorn.  In the message catalog, each message
70# will have two backslashes.
71MSG1=`gettext '\\\tAfter 30 minutes of idle time on this system, your system \
72state\\\n\\\twill automatically be saved to disk, and the system will \
73power-off.\\\n\\\n\\\tLater, when you want to use the system again, and \
74you turn the power\\\n\\\tback on, your system will be restored to its \
75previous state,\\\n\\\tincluding all the programs that you were running.\
76\\\n\\\n\\\tDo you want this automatic power-saving shutdown?\\\n\
77\\\t(If this system is used as a server, answer n)'`
78
79# Message to be prompted in question2.
80# In the following message, each escape sequence requires three
81# backslashes to prevent the interpretation by the shell and gettext
82# before being passed to ckyorn.  In the message catalog, each message
83# will have two backslashes.
84MSG2=`gettext '\\\tDo you want the system to ask about this again, when \
85you next reboot?\\\n\\\t(This gives you the chance to try it before deciding \
86whether\\\n\\\tto keep it.)'`
87
88# The autoshutdown comment to be put into the power management config file.
89SHUTDOWN_COMMENT="# Auto-Shutdown\t\tIdle(min)\tStart/Finish(hh:mm)\tBehavior"
90
91# Set up path.
92PATH="/usr/bin:/usr/sbin:${PATH}"
93export PATH
94
95#
96# Get current autoshutdown setup.
97#
98get_behavior() {
99	grep -s "$SHUTDOWN_PATTERN" $PWR_CONF > /dev/null
100	if [ $? = 0 ]; then
101		set - `grep "$SHUTDOWN_PATTERN" $PWR_CONF`
102		CURRENT_IDLE_TIME=$2
103		CURRENT_START_TIME=$3
104		CURRENT_FINISH_TIME=$4
105		CURRENT_BEHAVIOR=$5
106	fi
107}
108
109#
110# Set the autoshutdown behavior in the configuration file.
111# The autoshutdown token can be preceded by spaces.
112# The resulting configuration will be based on the first autoshutdown
113# line if there is more than one in the configuration file.
114#
115set_behavior() {
116	BEHAVIOR="$1"
117
118	grep -s "$SHUTDOWN_PATTERN" $PWR_CONF > /dev/null
119	if [ $? = 0 ]; then
120		set - `grep "$SHUTDOWN_PATTERN" $PWR_CONF`
121		CURRENT_IDLE_TIME=$2
122		CURRENT_START_TIME=$3
123		CURRENT_FINISH_TIME=$4
124		CURRENT_BEHAVIOR=$5
125	fi
126
127	if [ "$BEHAVIOR" = "unconfigured" ]; then
128		IDLE=$DEFAULT_IDLE_TIME
129		START=$DEFAULT_START_TIME
130		FINISH=$DEFAULT_FINISH_TIME
131	else {
132		if [ "$CURRENT_IDLE_TIME" = "" ]; then
133			IDLE="$DEFAULT_IDLE_TIME"
134		else
135			IDLE="$CURRENT_IDLE_TIME"
136		fi
137
138		if [ "$CURRENT_START_TIME" = "" ]; then
139			START="$DEFAULT_START_TIME"
140		else
141			START="$CURRENT_START_TIME"
142		fi
143
144		if [ "$CURRENT_FINISH_TIME" = "" ]; then
145			FINISH="$DEFAULT_FINISH_TIME"
146		else
147			FINISH="$CURRENT_FINISH_TIME"
148		fi
149	} fi
150
151	grep -v "# Auto-Shutdown[	]" $PWR_CONF | grep -v "$SHUTDOWN_PATTERN" > $TMP
152	echo $SHUTDOWN_COMMENT >> $TMP
153	echo "autoshutdown\t\t${IDLE}\t\t${START} ${FINISH}\t\t${BEHAVIOR}" >> \
154		$TMP
155	cp $TMP $PWR_CONF
156	rm $TMP
157}
158
159#
160# Print out the Energystar guidelines.
161#
162print_estar_guidelines() {
163	echo
164	echo "`gettext '\t================================================================'`"
165	echo "`gettext '\tThis system is configured to conserve energy.'`"
166        echo "`gettext '\t================================================================'`"
167}
168
169#
170# Ask user for autoshutdown confirmation.
171#
172question1() {
173        ans=`ckyorn -Q -d y -p "$1"`
174	case $ans in
175		y|yes|Y|Yes|YES)
176			set_behavior shutdown
177			echo
178			echo "`gettext '\tAutoshutdown remains enabled.'`"
179			break
180			;;
181		n|no|N|No|NO)
182			set_behavior noshutdown
183			echo
184			echo "`gettext '\tAutoshutdown has been disabled.'`"
185			break
186			;;
187	esac
188}
189
190#
191# Ask user whether they want to be asked about the question again during
192# next reboot.
193#
194question2() {
195        ans=`ckyorn -Q -d n -p "$1"`
196	case $ans in
197		y|yes|Y|Yes|YES)
198			touch $ASK_AGAIN_FLAG
199			echo "`gettext '\n\tThe system will ask you about automatic shutdown at the next reboot.'`"
200			break
201			;;
202		n|no|N|No|NO)
203			rm -f $ASK_AGAIN_FLAG
204			echo "`gettext '\n\tThe system will not ask you again about automatic shutdown.'`"
205			break
206			;;
207	esac
208}
209
210
211################
212#     Main     #
213################
214
215#
216# Exit if /etc/power.conf does not exist or is not writable.
217#
218if [ ! -f $PWR_CONF -o ! -w $PWR_CONF ]; then
219	exit 1
220fi
221
222
223#
224# Usage: sysidpm [-c|-u]
225#
226if [ $# -gt 1 ]; then
227	echo $USAGE
228	exit 1
229fi
230
231
232#
233# The postinstall script of some power management package should have
234# added this command into the application list in /etc/.sysidconfig.apps.
235# System configuration and unconfiguration will call those applications
236# with option -c and -u respectively.
237#
238if [ $# -eq 1 ]; then
239	case $1 in
240		-c)	# Does not need to do anything.
241			exit 0 ;;
242		-u)
243			# Reset the behavior back to unconfigured state.
244			set_behavior unconfigured
245
246			# Remove the statefile line too.
247			grep -v statefile $PWR_CONF > $TMP
248			cp $TMP $PWR_CONF
249			rm $TMP
250
251			exit 0 ;;
252		*)
253			echo $USAGE
254			exit 1 ;;
255	esac
256fi
257
258
259#
260# Get current autoshutdown setup.
261#
262get_behavior
263
264#
265# If this is a diskless system, silently disable autoshutdown and exit.
266#
267ROOT_FSTYPE=`df -n / | (read w1 w2 w3; echo $w3)`
268if [ $ROOT_FSTYPE != "ufs" ]; then
269	set_behavior noshutdown
270	exit 0
271fi
272
273
274#
275# If /autoshutdown is present, silently enable autoshutdown and exit.
276#
277if [ -f $SHUTDOWN_ENABLE_FLAG ]; then
278	set_behavior shutdown
279	rm $SHUTDOWN_ENABLE_FLAG
280	exit 0
281fi
282
283#
284# If /noautoshutdown is present, silently disable autoshutdown and
285# exit.
286#
287if [ -f $SHUTDOWN_DISABLE_FLAG ]; then
288	set_behavior noshutdown
289	rm $SHUTDOWN_DISABLE_FLAG
290	exit 0
291fi
292
293
294#
295# If this is an EnergyStar compliant system, the default should
296# have autoshutdown enabled. However we don't want to surprise
297# users, so let's confirm with the user.
298#
299prtconf -vp | grep -s -w ${ESTAR_PROP} > /dev/null
300if [ $? = 0 ]; then
301	if [ "$CURRENT_BEHAVIOR" = "unconfigured" -o -f $ASK_AGAIN_FLAG ]; then
302		print_estar_guidelines
303		question1 "$MSG1"
304		question2 "$MSG2"
305		# Currently, we do not have documentation for changing and
306		# setting workstation energy-saving features.  When we do,
307		# gettext messages should be emitting here. see CR6520924
308		#
309		echo
310	fi
311	exit 0
312fi
313
314#
315# The rest of the cases will have 'default' autoshutdown behavior.
316#
317if [ "$CURRENT_BEHAVIOR" = "unconfigured" ]; then
318	set_behavior default
319	exit 0
320fi
321
322#
323# We are here because either the autoshutdown line has been
324# removed or the behavior has been configured. It can be a result
325# of upgrade. In that case, the configuration file should not
326# be changed. Let's exit.
327exit 0
328