#!/bin/sh # hwclock.sh Set and adjust the CMOS clock. # # Version: @(#)hwclock.sh 2.00 14-Dec-1998 miquels@cistron.nl # # Patches: # 2000-01-30 Henrique M. Holschuh # - Minor cosmetic changes in an attempt to help new # users notice something IS changing their clocks # during startup/shutdown. # - Added comments to alert users of hwclock issues # and discourage tampering without proper doc reading. # 2012-02-16 Roger Leigh # - Use the UTC/LOCAL setting in /etc/adjtime rather than # the UTC setting in /etc/default/rcS. Additionally # source /etc/default/hwclock to permit configuration. ### BEGIN INIT INFO # Provides: hwclock # Required-Start: mountdevsubfs # Required-Stop: mountdevsubfs # Should-Stop: umountfs # Default-Start: S # X-Start-Before: checkroot # Default-Stop: 0 6 # Short-Description: Sync hardware and system clock time. ### END INIT INFO # These defaults are user-overridable in /etc/default/hwclock global BADYEAR := 'no' global HWCLOCKACCESS := 'yes' global HWCLOCKPARS := '' global HCTOSYS_DEVICE := 'rtc0' # We only want to use the system timezone or else we'll get # potential inconsistency at startup. unset TZ proc hwclocksh { test ! -x /sbin/hwclock && return 0 test ! -r /etc/default/rcS || source /etc/default/rcS test ! -r /etc/default/hwclock || source /etc/default/hwclock source /lib/lsb/init-functions proc verbose_log_action_msg { test $VERBOSE = no || log_action_msg @Argv; } matchstr $BADYEAR { no|"" { global BADYEAR := ''"" } yes { global BADYEAR := '"--badyear'" } * { log_action_msg "unknown BADYEAR setting: \"$BADYEAR\""; return 1 } } matchstr $1 { start { # If the admin deleted the hwclock config, create a blank # template with the defaults. if test -w /etc && test ! -f /etc/adjtime && test ! -e /etc/adjtime { printf "0.0 0 0.0\n0\nUTC\n" > /etc/adjtime } if test -d /run/udev || test -d /dev/.udev { return 0 } if test $HWCLOCKACCESS != no { log_action_msg "Setting the system clock" # Just for reporting. if sed '3!d' /etc/adjtime | grep -q '^UTC$' { global UTC := '"--utc'" } else { global UTC := '' } # Copies Hardware Clock time to System Clock using the correct # timezone for hardware clocks in local time, and sets kernel # timezone. DO NOT REMOVE. if /sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --hctosys $HWCLOCKPARS $BADYEAR { # Announce the local time. verbose_log_action_msg "System Clock set to: $[date $UTC]" } else { log_warning_msg "Unable to set System Clock to: $[date $UTC]" } } else { verbose_log_action_msg "Not setting System Clock" } } stop|restart|reload|force-reload { # # Updates the Hardware Clock with the System Clock time. # This will *override* any changes made to the Hardware Clock. # # WARNING: If you disable this, any changes to the system # clock will not be carried across reboots. # if test $HWCLOCKACCESS != no { log_action_msg "Saving the system clock" if /sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --systohc $HWCLOCKPARS $BADYEAR { verbose_log_action_msg "Hardware Clock updated to $[date]" } } else { verbose_log_action_msg "Not saving System Clock" } } show { if test $HWCLOCKACCESS != no { /sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --show $HWCLOCKPARS $BADYEAR } } * { log_success_msg "Usage: hwclock.sh {start|stop|reload|force-reload|show}" log_success_msg " start sets kernel (system) clock from hardware (RTC) clock" log_success_msg " stop and reload set hardware (RTC) clock from kernel (system) clock" return 1 } } } hwclocksh @Argv (CommandList children: [ (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:BADYEAR) op:Equal rhs:{(no)} spids:[83])] spids: [83] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:HWCLOCKACCESS) op:Equal rhs:{(yes)} spids:[86])] spids: [86] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:HWCLOCKPARS) op:Equal rhs:{(SQ )} spids:[89])] spids: [89] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:HCTOSYS_DEVICE) op:Equal rhs:{(rtc0)} spids:[91])] spids: [91] ) (C {(unset)} {(TZ)}) (FuncDef name: hwclocksh body: (BraceGroup children: [ (AndOr children: [ (C {(Lit_Other "[")} {(KW_Bang "!")} {(-x)} {(/sbin/hwclock)} {(Lit_Other "]")}) (ControlFlow token: arg_word:{(0)}) ] op_id: Op_DAmp ) (AndOr children: [ (C {(Lit_Other "[")} {(KW_Bang "!")} {(-r)} {(/etc/default/rcS)} {(Lit_Other "]")}) (C {(.)} {(/etc/default/rcS)}) ] op_id: Op_DPipe ) (AndOr children: [ (C {(Lit_Other "[")} {(KW_Bang "!")} {(-r)} {(/etc/default/hwclock)} {(Lit_Other "]")}) (C {(.)} {(/etc/default/hwclock)}) ] op_id: Op_DPipe ) (C {(.)} {(/lib/lsb/init-functions)}) (FuncDef name: verbose_log_action_msg body: (BraceGroup children: [ (Sentence child: (AndOr children: [ (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$VERBOSE"))} {(Lit_Other "=")} {(no)} {(Lit_Other "]")} ) (C {(log_action_msg)} {(DQ ($ VSub_At "$@"))}) ] op_id: Op_DPipe ) terminator: ) ] spids: [174] ) spids: [170 173] ) (Case to_match: {(DQ ($ VSub_Name "$BADYEAR"))} arms: [ (case_arm pat_list: [{(no)} {(DQ )}] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:BADYEAR) op: Equal rhs: {(DQ )} spids: [216] ) ] spids: [216] ) ] spids: [210 214 220 -1] ) (case_arm pat_list: [{(yes)}] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:BADYEAR) op: Equal rhs: {(DQ (--badyear))} spids: [226] ) ] spids: [226] ) ] spids: [223 224 231 -1] ) (case_arm pat_list: [{(Lit_Other "*")}] action: [ (Sentence child: (C {(log_action_msg)} { (DQ ("unknown BADYEAR setting: ") (EscapedLiteralPart token: ) ($ VSub_Name "$BADYEAR") (EscapedLiteralPart token:) ) } ) terminator: ) (ControlFlow token: arg_word:{(1)}) ] spids: [234 235 251 -1] ) ] spids: [201 207 254] ) (Case to_match: {(DQ ($ VSub_Number "$1"))} arms: [ (case_arm pat_list: [{(start)}] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (AndOr children: [ (C {(Lit_Other "[")} {(-w)} {(/etc)} {(Lit_Other "]")}) (AndOr children: [ (C {(Lit_Other "[")} {(KW_Bang "!")} {(-f)} {(/etc/adjtime)} {(Lit_Other "]")} ) (C {(Lit_Other "[")} {(KW_Bang "!")} {(-e)} {(/etc/adjtime)} {(Lit_Other "]")} ) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) terminator: ) ] action: [ (SimpleCommand words: [ {(printf)} { (DQ ("0.0 0 0.0") (EscapedLiteralPart token:) (0) (EscapedLiteralPart token:) (UTC) (EscapedLiteralPart token: ) ) } ] redirects: [ (Redir op_id: Redir_Great fd: -1 arg_word: {(/etc/adjtime)} spids: [328] ) ] ) ] spids: [-1 314] ) ] spids: [-1 333] ) (If arms: [ (if_arm cond: [ (Sentence child: (AndOr children: [ (C {(Lit_Other "[")} {(-d)} {(/run/udev)} {(Lit_Other "]")}) (C {(Lit_Other "[")} {(-d)} {(/dev/.udev)} {(Lit_Other "]")}) ] op_id: Op_DPipe ) terminator: ) ] action: [(ControlFlow token: arg_word:{(0)})] spids: [-1 358] ) ] spids: [-1 366] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$HWCLOCKACCESS"))} {(KW_Bang "!") (Lit_Other "=")} {(no)} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(log_action_msg)} {(DQ ("Setting the system clock"))}) (If arms: [ (if_arm cond: [ (Sentence child: (Pipeline children: [ (C {(sed)} {(SQ <"3!d">)} {(/etc/adjtime)}) (C {(grep)} {(-q)} {(SQ <"^UTC$">)}) ] negated: False ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:UTC) op: Equal rhs: {(DQ (--utc))} spids: [425] ) ] spids: [425] ) ] spids: [-1 422] ) ] else_action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:UTC) op: Equal rhs: {(SQ )} spids: [434] ) ] spids: [434] ) ] spids: [431 437] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(/sbin/hwclock)} {(--rtc) (Lit_Other "=") (/dev/) ($ VSub_Name "$HCTOSYS_DEVICE") } {(--hctosys)} {($ VSub_Name "$HWCLOCKPARS")} {($ VSub_Name "$BADYEAR")} ) terminator: ) ] action: [ (C {(verbose_log_action_msg)} { (DQ ("System Clock set to: ") (CommandSubPart command_list: (CommandList children: [(C {(date)} {($ VSub_Name "$UTC")})] ) left_token: spids: [479 483] ) ) } ) ] spids: [-1 468] ) ] else_action: [ (C {(log_warning_msg)} { (DQ ("Unable to set System Clock to: ") (CommandSubPart command_list: (CommandList children: [(C {(date)} {($ VSub_Name "$UTC")})] ) left_token: spids: [494 498] ) ) } ) ] spids: [487 502] ) ] spids: [-1 386] ) ] else_action: [(C {(verbose_log_action_msg)} {(DQ ("Not setting System Clock"))})] spids: [505 515] ) ] spids: [267 268 518 -1] ) (case_arm pat_list: [{(stop)} {(restart)} {(reload)} {(force-reload)}] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$HWCLOCKACCESS"))} {(KW_Bang "!") (Lit_Other "=")} {(no)} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(log_action_msg)} {(DQ ("Saving the system clock"))}) (If arms: [ (if_arm cond: [ (Sentence child: (C {(/sbin/hwclock)} {(--rtc) (Lit_Other "=") (/dev/) ($ VSub_Name "$HCTOSYS_DEVICE") } {(--systohc)} {($ VSub_Name "$HWCLOCKPARS")} {($ VSub_Name "$BADYEAR")} ) terminator: ) ] action: [ (C {(verbose_log_action_msg)} { (DQ ("Hardware Clock updated to ") (CommandSubPart command_list: (CommandList children:[(C {(date)})]) left_token: spids: [609 611] ) ) } ) ] spids: [-1 602] ) ] spids: [-1 615] ) ] spids: [-1 576] ) ] else_action: [(C {(verbose_log_action_msg)} {(DQ ("Not saving System Clock"))})] spids: [618 628] ) ] spids: [521 528 631 -1] ) (case_arm pat_list: [{(show)}] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$HWCLOCKACCESS"))} {(KW_Bang "!") (Lit_Other "=")} {(no)} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(/sbin/hwclock)} {(--rtc) (Lit_Other "=") (/dev/) ($ VSub_Name "$HCTOSYS_DEVICE")} {(--show)} {($ VSub_Name "$HWCLOCKPARS")} {($ VSub_Name "$BADYEAR")} ) ] spids: [-1 654] ) ] spids: [-1 671] ) ] spids: [634 635 674 -1] ) (case_arm pat_list: [{(Lit_Other "*")}] action: [ (C {(log_success_msg)} {(DQ ("Usage: hwclock.sh {start|stop|reload|force-reload|show}"))} ) (C {(log_success_msg)} {(DQ (" start sets kernel (system) clock from hardware (RTC) clock"))} ) (C {(log_success_msg)} { (DQ ( " stop and reload set hardware (RTC) clock from kernel (system) clock" ) ) } ) (ControlFlow token: arg_word:{(1)}) ] spids: [677 678 707 -1] ) ] spids: [258 264 710] ) ] spids: [110] ) spids: [106 109] ) (C {(hwclocksh)} {(DQ ($ VSub_At "$@"))}) ] )