1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <unistd.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/sysi86.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <time.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate static char *progname;
40*7c478bd9Sstevel@tonic-gate static char *zonefile = "/etc/rtc_config";
41*7c478bd9Sstevel@tonic-gate static FILE *zonefptr;
42*7c478bd9Sstevel@tonic-gate static char zone_info[256];
43*7c478bd9Sstevel@tonic-gate static char zone_lag[256];
44*7c478bd9Sstevel@tonic-gate static char tz[256] = "TZ=";
45*7c478bd9Sstevel@tonic-gate int debug = 0;
46*7c478bd9Sstevel@tonic-gate int lag;
47*7c478bd9Sstevel@tonic-gate int errors_ok = 0; /* allow "rtc no-args" to be quiet when not configured */
48*7c478bd9Sstevel@tonic-gate static time_t clock_val;
49*7c478bd9Sstevel@tonic-gate static char zone_comment[] =
50*7c478bd9Sstevel@tonic-gate "#\n"
51*7c478bd9Sstevel@tonic-gate "# This file (%s) contains information used to manage the\n"
52*7c478bd9Sstevel@tonic-gate "# x86 real time clock hardware. The hardware is kept in\n"
53*7c478bd9Sstevel@tonic-gate "# the machine's local time for compatibility with other x86\n"
54*7c478bd9Sstevel@tonic-gate "# operating systems. This file is read by the kernel at\n"
55*7c478bd9Sstevel@tonic-gate "# boot time. It is set and updated by the /usr/sbin/rtc\n"
56*7c478bd9Sstevel@tonic-gate "# command. The 'zone_info' field designates the local\n"
57*7c478bd9Sstevel@tonic-gate "# time zone. The 'zone_lag' field indicates the number\n"
58*7c478bd9Sstevel@tonic-gate "# of seconds between local time and Greenwich Mean Time.\n"
59*7c478bd9Sstevel@tonic-gate "#\n";
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate * Open the configuration file and extract the
63*7c478bd9Sstevel@tonic-gate * zone_info and the zone_lag. Return 0 if successful.
64*7c478bd9Sstevel@tonic-gate */
65*7c478bd9Sstevel@tonic-gate int
open_zonefile()66*7c478bd9Sstevel@tonic-gate open_zonefile()
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate char b[256], *s;
69*7c478bd9Sstevel@tonic-gate int lag_hrs;
70*7c478bd9Sstevel@tonic-gate
71*7c478bd9Sstevel@tonic-gate if ((zonefptr = fopen(zonefile, "r")) == NULL) {
72*7c478bd9Sstevel@tonic-gate if (errors_ok == 0)
73*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
74*7c478bd9Sstevel@tonic-gate "%s: cannot open %s: errno = %d\n",
75*7c478bd9Sstevel@tonic-gate progname, zonefile, errno);
76*7c478bd9Sstevel@tonic-gate return (1);
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate for (;;) {
80*7c478bd9Sstevel@tonic-gate if ((s = fgets(b, sizeof (b), zonefptr)) == NULL)
81*7c478bd9Sstevel@tonic-gate break;
82*7c478bd9Sstevel@tonic-gate if ((s = strchr(s, 'z')) == NULL)
83*7c478bd9Sstevel@tonic-gate continue;
84*7c478bd9Sstevel@tonic-gate if (strncmp(s, "zone_info", 9) == 0) {
85*7c478bd9Sstevel@tonic-gate s += 9;
86*7c478bd9Sstevel@tonic-gate while (*s != 0 && *s != '=')
87*7c478bd9Sstevel@tonic-gate s++;
88*7c478bd9Sstevel@tonic-gate if (*s == '=') {
89*7c478bd9Sstevel@tonic-gate s++;
90*7c478bd9Sstevel@tonic-gate while (*s != 0 && (*s == ' ' || *s == '\t'))
91*7c478bd9Sstevel@tonic-gate s++;
92*7c478bd9Sstevel@tonic-gate (void) strncpy(zone_info, s,
93*7c478bd9Sstevel@tonic-gate sizeof (zone_info));
94*7c478bd9Sstevel@tonic-gate s = zone_info;
95*7c478bd9Sstevel@tonic-gate while (*s != 0 && *s != '\n')
96*7c478bd9Sstevel@tonic-gate s++;
97*7c478bd9Sstevel@tonic-gate if (*s == '\n')
98*7c478bd9Sstevel@tonic-gate *s = 0;
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate } else if (strncmp(s, "zone_lag", 8) == 0) {
101*7c478bd9Sstevel@tonic-gate s += 8;
102*7c478bd9Sstevel@tonic-gate while (*s != 0 && *s != '=')
103*7c478bd9Sstevel@tonic-gate s++;
104*7c478bd9Sstevel@tonic-gate if (*s == '=') {
105*7c478bd9Sstevel@tonic-gate s++;
106*7c478bd9Sstevel@tonic-gate while (*s != 0 && (*s == ' ' || *s == '\t'))
107*7c478bd9Sstevel@tonic-gate s++;
108*7c478bd9Sstevel@tonic-gate (void) strncpy(zone_lag, s, sizeof (zone_lag));
109*7c478bd9Sstevel@tonic-gate s = zone_lag;
110*7c478bd9Sstevel@tonic-gate while (*s != 0 && *s != '\n')
111*7c478bd9Sstevel@tonic-gate s++;
112*7c478bd9Sstevel@tonic-gate if (*s == '\n')
113*7c478bd9Sstevel@tonic-gate *s = 0;
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate lag = atoi(zone_lag);
118*7c478bd9Sstevel@tonic-gate lag_hrs = lag / 3600;
119*7c478bd9Sstevel@tonic-gate if (zone_info[0] == 0) {
120*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: zone_info field is invalid\n",
121*7c478bd9Sstevel@tonic-gate progname);
122*7c478bd9Sstevel@tonic-gate zone_info[0] = 0;
123*7c478bd9Sstevel@tonic-gate zone_lag[0] = 0;
124*7c478bd9Sstevel@tonic-gate return (1);
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate if (zone_lag[0] == 0) {
127*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: zone_lag field is invalid\n",
128*7c478bd9Sstevel@tonic-gate progname);
129*7c478bd9Sstevel@tonic-gate zone_lag[0] = 0;
130*7c478bd9Sstevel@tonic-gate return (1);
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate if ((lag_hrs < -24) || (lag_hrs > 24)) {
133*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: a GMT lag of %d is out of range\n",
134*7c478bd9Sstevel@tonic-gate progname, lag_hrs);
135*7c478bd9Sstevel@tonic-gate zone_info[0] = 0;
136*7c478bd9Sstevel@tonic-gate zone_lag[0] = 0;
137*7c478bd9Sstevel@tonic-gate return (1);
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate if (debug)
140*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "zone_info = %s, zone_lag = %s\n",
141*7c478bd9Sstevel@tonic-gate zone_info, zone_lag);
142*7c478bd9Sstevel@tonic-gate if (debug)
143*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "lag (decimal) is %d\n", lag);
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate (void) fclose(zonefptr);
146*7c478bd9Sstevel@tonic-gate zonefptr = NULL;
147*7c478bd9Sstevel@tonic-gate return (0);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate void
display_zone_string(void)151*7c478bd9Sstevel@tonic-gate display_zone_string(void)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate if (open_zonefile() == 0)
154*7c478bd9Sstevel@tonic-gate (void) printf("%s\n", zone_info);
155*7c478bd9Sstevel@tonic-gate else
156*7c478bd9Sstevel@tonic-gate (void) printf("GMT\n");
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate long
set_zone(char * zone_string)160*7c478bd9Sstevel@tonic-gate set_zone(char *zone_string)
161*7c478bd9Sstevel@tonic-gate {
162*7c478bd9Sstevel@tonic-gate struct tm *tm;
163*7c478bd9Sstevel@tonic-gate long current_lag;
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate (void) umask(0022);
166*7c478bd9Sstevel@tonic-gate if ((zonefptr = fopen(zonefile, "w")) == NULL) {
167*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot open %s: errno = %d\n",
168*7c478bd9Sstevel@tonic-gate progname, zonefile, errno);
169*7c478bd9Sstevel@tonic-gate return (0);
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate
172*7c478bd9Sstevel@tonic-gate tz[3] = 0;
173*7c478bd9Sstevel@tonic-gate (void) strncat(tz, zone_string, 253);
174*7c478bd9Sstevel@tonic-gate if (debug)
175*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Time Zone string is '%s'\n", tz);
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate (void) putenv(tz);
178*7c478bd9Sstevel@tonic-gate if (debug)
179*7c478bd9Sstevel@tonic-gate (void) system("env | grep TZ");
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate (void) time(&clock_val);
182*7c478bd9Sstevel@tonic-gate
183*7c478bd9Sstevel@tonic-gate tm = localtime(&clock_val);
184*7c478bd9Sstevel@tonic-gate current_lag = tm->tm_isdst ? altzone : timezone;
185*7c478bd9Sstevel@tonic-gate if (debug)
186*7c478bd9Sstevel@tonic-gate (void) printf("%s DST. Lag is %ld.\n", tm->tm_isdst ? "Is" :
187*7c478bd9Sstevel@tonic-gate "Is NOT", tm->tm_isdst ? altzone : timezone);
188*7c478bd9Sstevel@tonic-gate
189*7c478bd9Sstevel@tonic-gate (void) fprintf(zonefptr, zone_comment, zonefile);
190*7c478bd9Sstevel@tonic-gate (void) fprintf(zonefptr, "zone_info=%s\n", zone_string);
191*7c478bd9Sstevel@tonic-gate (void) fprintf(zonefptr, "zone_lag=%ld\n",
192*7c478bd9Sstevel@tonic-gate tm->tm_isdst ? altzone : timezone);
193*7c478bd9Sstevel@tonic-gate (void) fclose(zonefptr);
194*7c478bd9Sstevel@tonic-gate zonefptr = NULL;
195*7c478bd9Sstevel@tonic-gate return (current_lag);
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate
198*7c478bd9Sstevel@tonic-gate void
correct_rtc_and_lag()199*7c478bd9Sstevel@tonic-gate correct_rtc_and_lag()
200*7c478bd9Sstevel@tonic-gate {
201*7c478bd9Sstevel@tonic-gate struct tm *tm;
202*7c478bd9Sstevel@tonic-gate long kernels_lag;
203*7c478bd9Sstevel@tonic-gate long current_lag;
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate if (open_zonefile())
206*7c478bd9Sstevel@tonic-gate return;
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate tz[3] = 0;
209*7c478bd9Sstevel@tonic-gate (void) strncat(tz, zone_info, 253);
210*7c478bd9Sstevel@tonic-gate if (debug)
211*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Time Zone string is '%s'\n", tz);
212*7c478bd9Sstevel@tonic-gate
213*7c478bd9Sstevel@tonic-gate (void) putenv(tz);
214*7c478bd9Sstevel@tonic-gate if (debug)
215*7c478bd9Sstevel@tonic-gate (void) system("env | grep TZ");
216*7c478bd9Sstevel@tonic-gate
217*7c478bd9Sstevel@tonic-gate (void) time(&clock_val);
218*7c478bd9Sstevel@tonic-gate tm = localtime(&clock_val);
219*7c478bd9Sstevel@tonic-gate current_lag = tm->tm_isdst ? altzone : timezone;
220*7c478bd9Sstevel@tonic-gate
221*7c478bd9Sstevel@tonic-gate if (current_lag != lag) { /* if file is wrong */
222*7c478bd9Sstevel@tonic-gate if (debug)
223*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "correcting file");
224*7c478bd9Sstevel@tonic-gate (void) set_zone(zone_info); /* then rewrite file */
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate
227*7c478bd9Sstevel@tonic-gate (void) sysi86(GGMTL, &kernels_lag);
228*7c478bd9Sstevel@tonic-gate if (current_lag != kernels_lag) {
229*7c478bd9Sstevel@tonic-gate if (debug)
230*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "correcting kernel's lag");
231*7c478bd9Sstevel@tonic-gate (void) sysi86(SGMTL, current_lag); /* correct the lag */
232*7c478bd9Sstevel@tonic-gate (void) sysi86(WTODC); /* set the rtc to */
233*7c478bd9Sstevel@tonic-gate /* new local time */
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate
237*7c478bd9Sstevel@tonic-gate void
initialize_zone(char * zone_string)238*7c478bd9Sstevel@tonic-gate initialize_zone(char *zone_string)
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate long current_lag;
241*7c478bd9Sstevel@tonic-gate
242*7c478bd9Sstevel@tonic-gate /* write the config file */
243*7c478bd9Sstevel@tonic-gate current_lag = set_zone(zone_string);
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate /* correct the lag */
246*7c478bd9Sstevel@tonic-gate (void) sysi86(SGMTL, current_lag);
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate /*
249*7c478bd9Sstevel@tonic-gate * set the unix time from the rtc,
250*7c478bd9Sstevel@tonic-gate * assuming the rtc was the correct
251*7c478bd9Sstevel@tonic-gate * local time.
252*7c478bd9Sstevel@tonic-gate */
253*7c478bd9Sstevel@tonic-gate (void) sysi86(RTCSYNC);
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate void
usage()257*7c478bd9Sstevel@tonic-gate usage()
258*7c478bd9Sstevel@tonic-gate {
259*7c478bd9Sstevel@tonic-gate static char Usage[] = "Usage:\n\
260*7c478bd9Sstevel@tonic-gate rtc [-c] [-z time_zone] [-?]\n";
261*7c478bd9Sstevel@tonic-gate
262*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Usage);
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate
265*7c478bd9Sstevel@tonic-gate void
verbose_usage()266*7c478bd9Sstevel@tonic-gate verbose_usage()
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate static char Usage1[] = "\
269*7c478bd9Sstevel@tonic-gate Options:\n\
270*7c478bd9Sstevel@tonic-gate -c\t\tCheck and correct for daylight savings time rollover.\n\
271*7c478bd9Sstevel@tonic-gate -z [zone]\tRecord the zone info in the config file.\n";
272*7c478bd9Sstevel@tonic-gate
273*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, Usage1);
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate
276*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])277*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
278*7c478bd9Sstevel@tonic-gate {
279*7c478bd9Sstevel@tonic-gate int c;
280*7c478bd9Sstevel@tonic-gate
281*7c478bd9Sstevel@tonic-gate progname = argv[0];
282*7c478bd9Sstevel@tonic-gate
283*7c478bd9Sstevel@tonic-gate if (argc == 1) {
284*7c478bd9Sstevel@tonic-gate errors_ok = 1;
285*7c478bd9Sstevel@tonic-gate display_zone_string();
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate
288*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "cz:d")) != EOF) {
289*7c478bd9Sstevel@tonic-gate switch (c) {
290*7c478bd9Sstevel@tonic-gate case 'c':
291*7c478bd9Sstevel@tonic-gate correct_rtc_and_lag();
292*7c478bd9Sstevel@tonic-gate continue;
293*7c478bd9Sstevel@tonic-gate case 'z':
294*7c478bd9Sstevel@tonic-gate initialize_zone(optarg);
295*7c478bd9Sstevel@tonic-gate continue;
296*7c478bd9Sstevel@tonic-gate case 'd':
297*7c478bd9Sstevel@tonic-gate debug = 1;
298*7c478bd9Sstevel@tonic-gate continue;
299*7c478bd9Sstevel@tonic-gate case '?':
300*7c478bd9Sstevel@tonic-gate verbose_usage();
301*7c478bd9Sstevel@tonic-gate return (0);
302*7c478bd9Sstevel@tonic-gate default:
303*7c478bd9Sstevel@tonic-gate usage();
304*7c478bd9Sstevel@tonic-gate return (1);
305*7c478bd9Sstevel@tonic-gate }
306*7c478bd9Sstevel@tonic-gate }
307*7c478bd9Sstevel@tonic-gate return (0);
308*7c478bd9Sstevel@tonic-gate }
309