annotate toys/pending/hwclock.c @ 1668:7e3372a47248 draft

fix hwclock's rtc selection For systems using /dev/rtcN, /dev/rtc0 isn't necessarily the RTC that's used to provide the system time at boot time. We need to search for the RTC whose /sys/class/rtc/rtcN/hctosys contains "1".
author Elliott Hughes <enh@google.com>
date Tue, 20 Jan 2015 16:03:29 -0600
parents f42dceed5071
children ae981311e232
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
1 /* hwclock.c - get and set the hwclock
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
2 *
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
3 * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com>
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
4 *
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
5 * No Standard.
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
6 *
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
7 USE_HWCLOCK(NEWTOY(hwclock, ">0(fast)f(rtc):u(utc)l(localtime)t(systz)w(systohc)s(hctosys)r(show)[!ul][!rsw]", TOYFLAG_USR|TOYFLAG_BIN))
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
8
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
9 config HWCLOCK
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
10 bool "hwclock"
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
11 default n
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
12 help
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
13 usage: hwclock [-rswtluf]
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
14
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
15 -f FILE Use specified device file instead of /dev/rtc (--show)
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
16 -l Hardware clock uses localtime (--localtime)
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
17 -r Show hardware clock time (--show)
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
18 -s Set system time from hardware clock (--hctosys)
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
19 -t Set the system time based on the current timezone (--systz)
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
20 -u Hardware clock uses UTC (--utc)
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
21 -w Set hardware clock from system time (--systohc)
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
22 */
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
23 #define FOR_hwclock
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
24 #include "toys.h"
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
25 #include <linux/rtc.h>
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
26
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
27 GLOBALS(
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
28 char *fname;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
29
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
30 int utc;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
31 )
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
32
1668
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
33 static int check_hctosys(struct dirtree* node)
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
34 {
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
35 FILE *fp;
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
36
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
37 if (!node->parent) return DIRTREE_RECURSE;
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
38
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
39 snprintf(toybuf, sizeof(toybuf), "/sys/class/rtc/%s/hctosys", node->name);
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
40 fp = fopen(toybuf, "r");
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
41 if (fp) {
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
42 int hctosys = 0;
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
43 int items = fscanf(fp, "%d", &hctosys);
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
44 fclose(fp);
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
45 if (items == 1 && hctosys == 1) {
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
46 snprintf(toybuf, sizeof(toybuf), "/dev/%s", node->name);
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
47 TT.fname = toybuf;
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
48 return DIRTREE_ABORT;
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
49 }
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
50 }
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
51 return 0;
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
52 }
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
53
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
54 // Search /sys/class/rtc for the RTC that the system clock is set from.
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
55 // See the kernel's Documentation/rtc.txt.
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
56 static int open_wall_clock_rtc(int flag)
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
57 {
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
58 TT.fname = NULL;
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
59 dirtree_read("/sys/class/rtc", check_hctosys);
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
60 return TT.fname ? xopen(TT.fname, flag) : -1;
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
61 }
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
62
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
63 static int rtc_open(int flag)
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
64 {
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
65 if (!TT.fname) {
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
66 int fd;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
67
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
68 if ((fd = open((TT.fname = "/dev/rtc"), flag)) != -1) return fd;
1668
7e3372a47248 fix hwclock's rtc selection
Elliott Hughes <enh@google.com>
parents: 1549
diff changeset
69 else if ((fd = open_wall_clock_rtc(flag)) != -1) return fd;
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
70 else TT.fname = "/dev/misc/rtc";
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
71 }
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
72 return xopen(TT.fname, flag);
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
73 }
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
74
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
75 static time_t get_rtc()
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
76 {
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
77 struct tm time;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
78 time_t tm;
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
79 char *ptz_old = 0;
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
80 int fd = rtc_open(O_RDONLY);
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
81
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
82 xioctl(fd, RTC_RD_TIME, &time);
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
83 close(fd);
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
84 if (TT.utc) {
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
85 ptz_old = getenv("TZ");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
86 if (putenv((char*)"TZ=UTC0")) perror_exit("putenv");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
87 tzset();
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
88 }
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
89 if ((tm = mktime(&time)) < 0) error_exit("mktime failed");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
90 if (TT.utc) {
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
91 if (unsetenv("TZ") < 0) perror_exit("unsetenv");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
92 if (ptz_old && putenv(ptz_old - 3)) perror_exit("putenv");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
93 tzset();
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
94 }
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
95 return tm;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
96 }
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
97
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
98 static void set_sysclock_from_hwclock()
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
99 {
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
100 struct timezone tmzone;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
101 struct timeval tmval;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
102
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
103 tmzone.tz_minuteswest = timezone / 60 - 60 * daylight;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
104 tmzone.tz_dsttime = 0;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
105 tmval.tv_sec = get_rtc();
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
106 tmval.tv_usec = 0;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
107 if (settimeofday(&tmval, &tmzone) < 0) perror_exit("settimeofday");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
108 }
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
109
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
110 static void set_hwclock_from_sysclock()
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
111 {
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
112 struct timeval tmval;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
113 struct tm time;
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
114 int fd = rtc_open(O_WRONLY);
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
115
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
116 if (gettimeofday(&tmval, NULL) < 0) perror_exit("gettimeofday");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
117 // converting a time value to broken-down UTC time
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
118 if (TT.utc && !gmtime_r((time_t*)&tmval.tv_sec, &time))
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
119 error_exit("gmtime_r failed");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
120 // converting a time value to a broken-down localtime
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
121 else if (!(localtime_r((time_t*)&tmval.tv_sec, &time)))
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
122 error_exit("localtime_r failed");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
123
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
124 /* The value of tm_isdst will positive if daylight saving time is in effect,
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
125 * zero if it is not and negative if the information is not available.
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
126 * */
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
127 time.tm_isdst = 0;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
128 xioctl(fd, RTC_SET_TIME, &time);
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
129 close(fd);
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
130 }
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
131
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
132 static void set_sysclock_timezone()
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
133 {
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
134 struct timezone tmzone;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
135 struct timeval tmval;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
136 struct tm *pb;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
137
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
138 if (gettimeofday(&tmval, NULL) < 0) perror_exit("gettimeofday");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
139 if (!(pb = localtime(&tmval.tv_sec))) error_exit("localtime failed");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
140 // extern long timezone => defined in header sys/time.h
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
141 tmzone.tz_minuteswest = timezone / 60;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
142 if (pb->tm_isdst) tmzone.tz_minuteswest -= 60;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
143 tmzone.tz_dsttime = 0; // daylight saving time is not in effect
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
144 if (gettimeofday(&tmval, NULL) < 0) perror_exit("gettimeofday");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
145 if (!TT.utc) tmval.tv_sec += tmzone.tz_minuteswest * 60;
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
146 if (settimeofday(&tmval, &tmzone) < 0) perror_exit("settimeofday");
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
147 }
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
148
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
149 void hwclock_main()
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
150 {
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
151 // check for UTC
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
152 if (!(toys.optflags & FLAG_u)) {
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
153 FILE *fp = fopen("/etc/adjtime", "r");
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
154
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
155 if (fp) {
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
156 char *line = NULL;
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
157 size_t st;
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
158
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
159 while (0 < getline(&line, &st, fp)) {
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
160 if (!strncmp(line, "UTC", 3)) {
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
161 TT.utc = 1;
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
162 break;
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
163 }
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
164 free(line);
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
165 }
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
166 fclose(fp);
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
167 }
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
168 } else TT.utc = 1;
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
169
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
170 if (toys.optflags & FLAG_w) set_hwclock_from_sysclock();
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
171 else if (toys.optflags & FLAG_s) set_sysclock_from_hwclock();
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
172 else if (toys.optflags & FLAG_t) set_sysclock_timezone();
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
173 else if ((toys.optflags & FLAG_r) || (toys.optflags & FLAG_l)
1549
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
174 || !*toys.optargs)
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
175 {
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
176 time_t tm = get_rtc();
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
177 char *s, *pctm = ctime(&tm);
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
178
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
179 // ctime() is defined as equivalent to asctime(localtime(t)),
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
180 // which is defined to overflow its buffer rather than return NULL.
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
181 // if (!pctm) error_exit("can't happen");
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
182 if ((s = strrchr(pctm, '\n'))) *s = '\0';
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
183 // TODO: implement this.
f42dceed5071 First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents: 1548
diff changeset
184 xprintf("%s 0.000000 seconds\n", pctm);
1548
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
185 }
e3216b4e6a0f hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
186 }