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