4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
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.
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]
23 * Copyright 2018 Gary Mills
24 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
31 #include <sys/types.h>
32 #include <sys/sysi86.h>
39 #define M_UNSET 0 /* Mode never set */
40 #define M_VAR 1 /* Tracks local time including DST */
41 #define M_UTC 2 /* Clock runs in UTC */
42 #define M_STD 3 /* Clock runs in local standard time */
44 static char *progname
;
45 static char *zonefile
= "/etc/rtc_config";
46 static FILE *zonefptr
;
47 static char zone_info
[256];
48 static char zone_lag
[256];
49 static char tz
[256] = "TZ=";
50 static char *utc_zone
= "UTC";
52 int rtc_mode
= M_UNSET
;
54 int errors_ok
= 0; /* allow "rtc no-args" to be quiet when not configured */
55 static time_t clock_val
;
56 static char zone_comment
[] =
58 "# This file (%s) contains information used to manage the\n"
59 "# x86 real time clock hardware. The hardware is kept in\n"
60 "# the machine's local time for compatibility with other x86\n"
61 "# operating systems. This file is read by the kernel at\n"
62 "# boot time. It is set and updated by the /usr/sbin/rtc\n"
63 "# command. The 'zone_info' field designates the local\n"
64 "# time zone. The 'zone_lag' field indicates the number\n"
65 "# of seconds between local time and Greenwich Mean Time.\n"
69 * Open the configuration file and extract the
70 * zone_info and the zone_lag. Return 0 if successful.
78 if ((zonefptr
= fopen(zonefile
, "r")) == NULL
) {
80 (void) fprintf(stderr
,
81 "%s: cannot open %s: errno = %d\n",
82 progname
, zonefile
, errno
);
87 if ((s
= fgets(b
, sizeof (b
), zonefptr
)) == NULL
)
89 if ((s
= strchr(s
, 'z')) == NULL
)
91 if (strncmp(s
, "zone_info", 9) == 0) {
93 while (*s
!= 0 && *s
!= '=')
97 while (*s
!= 0 && (*s
== ' ' || *s
== '\t'))
99 (void) strncpy(zone_info
, s
,
102 while (*s
!= 0 && *s
!= '\n')
107 } else if (strncmp(s
, "zone_lag", 8) == 0) {
109 while (*s
!= 0 && *s
!= '=')
113 while (*s
!= 0 && (*s
== ' ' || *s
== '\t'))
115 (void) strncpy(zone_lag
, s
, sizeof (zone_lag
));
117 while (*s
!= 0 && *s
!= '\n')
124 lag
= atoi(zone_lag
);
125 lag_hrs
= lag
/ 3600;
126 if (zone_info
[0] == 0) {
127 (void) fprintf(stderr
, "%s: zone_info field is invalid\n",
133 if (zone_lag
[0] == 0) {
134 (void) fprintf(stderr
, "%s: zone_lag field is invalid\n",
139 if ((lag_hrs
< -24) || (lag_hrs
> 24)) {
140 (void) fprintf(stderr
, "%s: a GMT lag of %d is out of range\n",
147 (void) fprintf(stderr
, "zone_info = %s, zone_lag = %s\n",
148 zone_info
, zone_lag
);
150 (void) fprintf(stderr
, "lag (decimal) is %d\n", lag
);
152 (void) fclose(zonefptr
);
158 display_zone_string(void)
160 if (open_zonefile() == 0)
161 (void) printf("%s\n", zone_info
);
163 (void) printf("GMT\n");
172 (void) strncat(tz
, z
, 253);
174 (void) fprintf(stderr
, "Time Zone string is '%s'\n", tz
);
178 (void) system("env | grep TZ");
180 (void) time(&clock_val
);
182 tm
= localtime(&clock_val
);
183 return (tm
->tm_isdst
);
187 set_zone(char *zone_string
)
193 if ((zonefptr
= fopen(zonefile
, "w")) == NULL
) {
194 (void) fprintf(stderr
, "%s: cannot open %s: errno = %d\n",
195 progname
, zonefile
, errno
);
201 isdst
= get_local(zone_string
);
202 current_lag
= isdst
? altzone
: timezone
;
205 isdst
= get_local(zone_string
);
206 current_lag
= timezone
;
208 default: /* Includes M_UTC */
211 zone_string
= utc_zone
;
215 (void) printf("%s DST. Lag is %ld.\n", isdst
? "Is" :
216 "Is NOT", current_lag
);
218 (void) fprintf(zonefptr
, zone_comment
, zonefile
);
219 (void) fprintf(zonefptr
, "zone_info=%s\n", zone_string
);
220 (void) fprintf(zonefptr
, "zone_lag=%ld\n", current_lag
);
221 (void) fclose(zonefptr
);
223 return (current_lag
);
227 correct_rtc_and_lag()
238 isdst
= get_local(zone_info
);
239 current_lag
= isdst
? altzone
: timezone
;
242 (void) get_local(zone_info
);
243 current_lag
= timezone
;
245 default: /* Includes M_UTC */
250 if (current_lag
!= lag
) { /* if file is wrong */
252 (void) fprintf(stderr
, "correcting file\n");
253 (void) set_zone(zone_info
); /* then rewrite file */
256 (void) sysi86(GGMTL
, &kernels_lag
);
257 if (current_lag
!= kernels_lag
) {
259 (void) fprintf(stderr
, "correcting kernel's lag\n");
260 (void) sysi86(SGMTL
, current_lag
); /* correct the lag */
261 (void) sysi86(WTODC
); /* set the rtc to */
267 initialize_zone(char *zone_string
)
271 /* write the config file */
272 current_lag
= set_zone(zone_string
);
274 /* correct the lag */
275 (void) sysi86(SGMTL
, current_lag
);
278 * set the unix time from the rtc,
279 * assuming the rtc was the correct
282 (void) sysi86(RTCSYNC
);
288 static char Usage
[] = "Usage:\n\
289 rtc [-w] [-s|-u|-v] [-c] [-z time_zone] [-?]\n";
291 (void) fprintf(stderr
, Usage
);
297 static char Usage1
[] = "\
299 -w\t\tDoes nothing.\n\
300 -s\t\tRTC runs in local standard time.\n\
301 -u\t\tRTC runs in UTC time.\n\
302 -v\t\tRTC tracks local time (with cron command).\n\
303 -c\t\tCheck and correct for daylight savings time rollover.\n\
304 -z [zone]\tRecord the zone info in the config file.\n";
306 (void) fprintf(stderr
, Usage1
);
313 default: /* Includes M_UNSET */
326 check_mode(int letter
, int mode
)
328 if (rtc_mode
== M_UNSET
|| rtc_mode
== mode
) {
332 (void) fprintf(stderr
, "%s: option -%c conflicts with other options\n",
339 main(int argc
, char *argv
[])
343 char *zone_name
= NULL
;
349 display_zone_string();
353 while ((c
= getopt(argc
, argv
, "suvwcz:d")) != EOF
) {
364 case 's': /* standard: RTC runs local standard time */
365 check_mode(c
, M_STD
);
367 case 'u': /* utc: RTC runs UTC time */
368 check_mode(c
, M_UTC
);
370 case 'v': /* varies: RTC tracks local time */
371 check_mode(c
, M_VAR
);
373 case 'w': /* Does nothing */
384 if (zone_name
!= NULL
)
385 initialize_zone(zone_name
);
387 correct_rtc_and_lag();