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