257
259
adjtime_p->not_adjusted = 0;
258
260
adjtime_p->last_calib_time = 0;
259
261
adjtime_p->local_utc = UNKNOWN;
262
adjtime_p->dirty = FALSE; /* don't create a zero adjfile */
264
adjfile = fopen(ADJPATH, "r"); /* open file for reading */
267
adjfile = fopen(adj_file_name, "r"); /* open file for reading */
265
268
if (adjfile == NULL) {
266
outsyserr("cannot open file " ADJPATH);
269
outsyserr("cannot open file %s", adj_file_name);
267
270
return EX_OSFILE;
515
527
time_t newhwtime;
516
528
struct timeval beginsystime, nowsystime;
519
532
gettimeofday(&beginsystime, NULL);
520
newhwtime = sethwtime + (int) time_diff(beginsystime, refsystime) + 1;
533
tdiff = time_diff(beginsystime, refsystime);
534
newhwtime = sethwtime + (int) (tdiff + 0.5);
522
536
printf(_("Time elapsed since reference time has been %.6f seconds.\n"
523
"Delaying further to reach the next full second.\n"),
524
time_diff(beginsystime, refsystime));
537
"Delaying further to reach the new time.\n"), tdiff);
527
* Now delay some more until Hardware Clock time newhwtime arrives. The -500
528
* ms is because the Hardware Clock always sets to your set time plus 500 ms
540
* Now delay some more until Hardware Clock time newhwtime arrives. The 0.5 s
541
* is because the Hardware Clock always sets to your set time plus 500 ms
529
542
* (because it is designed to update to the next second precisely 500 ms
530
543
* after you finish the setting).
534
546
gettimeofday(&nowsystime, NULL);
535
547
tdiff = time_diff(nowsystime, beginsystime);
537
goto time_resync; /* probably time was reset */
538
} while (time_diff(nowsystime, refsystime) - 0.5 < newhwtime - sethwtime);
549
goto time_resync; /* probably backward time reset */
551
goto time_resync; /* probably forward time reset */
552
beginsystime = nowsystime;
553
tdiff = time_diff(nowsystime, refsystime);
554
} while (newhwtime == sethwtime + (int) (tdiff + 0.5));
540
556
set_hardware_clock(newhwtime, universal, testing);
757
set_system_clock_timezone(const bool testing) {
758
/*----------------------------------------------------------------------------
759
Reset the System Clock from local time to UTC, based on its current
760
value and the timezone.
762
Also set the kernel time zone value to the value indicated by the
763
TZ environment variable and/or /usr/lib/zoneinfo/, interpreted as
764
tzset() would interpret them.
766
If 'testing' is true, don't actually update anything -- just say we
768
-----------------------------------------------------------------------------*/
775
gettimeofday(&tv, NULL);
777
struct tm broken_time;
780
broken_time = *gmtime(&tv.tv_sec);
781
strftime(ctime_now, sizeof(ctime_now), "%Y/%m/%d %H:%M:%S", &broken_time);
782
printf(_("Current system time: %ld = %s\n"), (long) tv.tv_sec, ctime_now);
785
broken = localtime(&tv.tv_sec);
786
#ifdef HAVE_TM_GMTOFF
787
minuteswest = -broken->tm_gmtoff/60; /* GNU extension */
789
minuteswest = timezone/60;
790
if (broken->tm_isdst)
794
gettimeofday(&tv, NULL);
795
tv.tv_sec += minuteswest * 60;
798
struct tm broken_time;
801
broken_time = *gmtime(&tv.tv_sec);
802
strftime(ctime_now, sizeof(ctime_now), "%Y/%m/%d %H:%M:%S", &broken_time);
804
printf(_("Calling settimeofday:\n"));
805
printf(_("\tUTC: %s\n"), ctime_now);
806
printf(_("\ttv.tv_sec = %ld, tv.tv_usec = %ld\n"),
807
(long) tv.tv_sec, (long) tv.tv_usec);
808
printf(_("\ttz.tz_minuteswest = %d\n"), minuteswest);
811
printf(_("Not setting system clock because running in test mode.\n"));
814
const struct timezone tz = { minuteswest, 0 };
816
rc = settimeofday(&tv, &tz);
818
if (errno == EPERM) {
820
_("Must be superuser to set system clock.\n"));
823
outsyserr(_("settimeofday() failed"));
741
833
adjust_drift_factor(struct adjtime *adjtime_p,
742
834
const time_t nowtime,
901
992
printf(_("Not updating adjtime file because of testing mode.\n"));
902
993
printf(_("Would have written the following to %s:\n%s"),
994
adj_file_name, newfile);
908
adjfile = fopen(ADJPATH, "w");
999
adjfile = fopen(adj_file_name, "w");
909
1000
if (adjfile == NULL) {
910
outsyserr("Could not open file with the clock adjustment parameters "
911
"in it (" ADJPATH ") for writing");
1001
outsyserr(_("Could not open file with the clock adjustment parameters "
1002
"in it (%s) for writing"), adj_file_name);
914
1005
if (fputs(newfile, adjfile) < 0) {
915
outsyserr("Could not update file with the clock adjustment "
916
"parameters (" ADJPATH ") in it");
1006
outsyserr(_("Could not update file with the clock adjustment "
1007
"parameters (%s) in it"), adj_file_name);
919
1010
if (fclose(adjfile) < 0) {
920
outsyserr("Could not update file with the clock adjustment "
921
"parameters (" ADJPATH ") in it");
1011
outsyserr(_("Could not update file with the clock adjustment "
1012
"parameters (%s) in it"), adj_file_name);
1018
1108
ur = probe_for_kd_clock();
1111
* This final clause is a really bad idea on x86/AT PCs. You run the
1112
* risk of a race condition with another copy of hwclock
1113
* that already has /dev/rtc open. The fallback case on
1114
* x86 is to then raise I/O priviledge level and access
1115
* the RTC CMOS directly using I/O instructions. Simultaneous
1116
* access like that can really hose the RTC.
1118
#if !defined(__i386__)
1020
1119
if (!ur && !user_requests_ISA)
1021
1120
ur = probe_for_cmos_clock();
1032
1132
manipulate_clock(const bool show, const bool adjust, const bool noadjfile,
1033
1133
const bool set, const time_t set_time,
1034
const bool hctosys, const bool systohc,
1134
const bool hctosys, const bool systohc, const bool systz,
1035
1135
const struct timeval startup_time,
1036
1136
const bool utc, const bool local_opt,
1037
1137
const bool testing) {
1070
1170
adjtime.dirty = TRUE;
1073
rc = synchronize_to_clock_tick(); /* this takes up to 1 second */
1078
1174
struct timeval read_time;
1079
1175
/* The time at which we read the Hardware Clock */
1177
bool hclock_valid = FALSE;
1082
1178
/* The Hardware Clock gives us a valid time, or at least something
1083
1179
close enough to fool mktime().
1182
time_t hclocktime = 0;
1087
1183
/* The time the hardware clock had just after we
1088
1184
synchronized to its next clock tick when we started up.
1089
1185
Defined only if hclock_valid is true.
1092
gettimeofday(&read_time, NULL);
1093
read_hardware_clock(universal, &hclock_valid, &hclocktime);
1188
if (show || adjust || hctosys || (!noadjfile && !systz)) {
1189
/* data from HW-clock are required */
1190
rc = synchronize_to_clock_tick();
1191
if (rc && rc != 2) /* 2= synchronization timeout */
1193
gettimeofday(&read_time, NULL);
1194
rc = read_hardware_clock(universal, &hclock_valid, &hclocktime);
1096
1200
display_time(hclock_valid, hclocktime,
1098
1202
} else if (set) {
1099
1203
set_hardware_clock_exact(set_time, startup_time,
1100
1204
universal, testing);
1101
adjust_drift_factor(&adjtime, set_time, hclock_valid, hclocktime,
1102
time_diff(read_time, startup_time));
1206
adjust_drift_factor(&adjtime, set_time, hclock_valid, hclocktime,
1207
time_diff(read_time, startup_time));
1103
1208
} else if (adjust) {
1104
1209
do_adjustment(&adjtime, hclock_valid, hclocktime,
1105
1210
read_time, universal, testing);
1116
1221
set_hardware_clock_exact((time_t) reftime.tv_sec, reftime,
1117
1222
universal, testing);
1118
adjust_drift_factor(&adjtime, (time_t) reftime.tv_sec, hclock_valid,
1119
hclocktime, (double) read_time.tv_usec / 1E6);
1224
adjust_drift_factor(&adjtime, (time_t) reftime.tv_sec, hclock_valid,
1225
hclocktime, (double) read_time.tv_usec / 1E6);
1120
1226
} else if (hctosys) {
1121
1227
rc = set_system_clock(hclock_valid, hclocktime, testing);
1123
1229
printf(_("Unable to set system clock.\n"));
1234
rc = set_system_clock_timezone(testing);
1236
printf(_("Unable to set system clock.\n"));
1240
printf(_("Clock in UTC, not changed.\n"));
1127
1243
if (!noadjfile)
1128
1244
save_adjtime(adjtime, testing);
1207
1323
"hwclock - query and set the hardware clock (RTC)\n\n"
1208
1324
"Usage: hwclock [function] [options...]\n\n"
1210
" --help show this help\n"
1211
" --show read hardware clock and print result\n"
1212
" --set set the rtc to the time given with --date\n"
1213
" --hctosys set the system time from the hardware clock\n"
1214
" --systohc set the hardware clock to the current system time\n"
1215
" --adjust adjust the rtc to account for systematic drift since \n"
1216
" the clock was last set or adjusted\n"
1217
" --getepoch print out the kernel's hardware clock epoch value\n"
1218
" --setepoch set the kernel's hardware clock epoch value to the \n"
1219
" value given with --epoch\n"
1220
" --version print out the version of hwclock to stdout\n"
1222
" --utc the hardware clock is kept in coordinated universal time\n"
1223
" --localtime the hardware clock is kept in local time\n"
1224
" --rtc=path special /dev/... file to use instead of default\n"
1225
" --directisa access the ISA bus directly instead of %s\n"
1226
" --badyear ignore rtc's year because the bios is broken\n"
1227
" --date specifies the time to which to set the hardware clock\n"
1228
" --epoch=year specifies the year which is the beginning of the \n"
1229
" hardware clock's epoch value\n"
1230
" --noadjfile do not access /etc/adjtime. Requires the use of\n"
1231
" either --utc or --localtime\n"
1326
" -h | --help show this help\n"
1327
" -r | --show read hardware clock and print result\n"
1328
" --set set the rtc to the time given with --date\n"
1329
" -s | --hctosys set the system time from the hardware clock\n"
1330
" -w | --systohc set the hardware clock to the current system time\n"
1331
" --systz set the system time based on the current timezone\n"
1332
" --adjust adjust the rtc to account for systematic drift since\n"
1333
" the clock was last set or adjusted\n"
1334
" --getepoch print out the kernel's hardware clock epoch value\n"
1335
" --setepoch set the kernel's hardware clock epoch value to the \n"
1336
" value given with --epoch\n"
1337
" -v | --version print out the version of hwclock to stdout\n"
1339
" -u | --utc the hardware clock is kept in UTC\n"
1340
" --localtime the hardware clock is kept in local time\n"
1341
" -f | --rtc=path special /dev/... file to use instead of default\n"
1342
" --directisa access the ISA bus directly instead of %s\n"
1343
" --badyear ignore rtc's year because the bios is broken\n"
1344
" --date specifies the time to which to set the hardware clock\n"
1345
" --epoch=year specifies the year which is the beginning of the \n"
1346
" hardware clock's epoch value\n"
1347
" --noadjfile do not access /etc/adjtime. Requires the use of\n"
1348
" either --utc or --localtime\n"
1349
" --adjfile=path specifies the path to the adjust file (default is\n"
1351
" --test do everything except actually updating the hardware\n"
1352
" clock or anything else\n"
1353
" -D | --debug debug mode\n"
1233
1356
#ifdef __alpha__
1234
1357
fprintf(usageto, _(
1235
" --jensen, --arc, --srm, --funky-toy\n"
1236
" tell hwclock the type of alpha you have (see hwclock(8))\n"
1358
" -J|--jensen, -A|--arc, -S|--srm, -F|--funky-toy\n"
1359
" tell hwclock the type of alpha you have (see hwclock(8))\n"
1241
1364
fflush(stdout);
1243
1366
usageto = stderr;
1304
1429
/* Variables set by various options; show may also be set later */
1305
1430
/* The options debug, badyear and epoch_option are global */
1306
bool show, set, systohc, hctosys, adjust, getepoch, setepoch;
1431
bool show, set, systohc, hctosys, systz, adjust, getepoch, setepoch;
1307
1432
bool utc, testing, local_opt, noadjfile, directisa;
1308
1433
bool ARCconsole, Jensen, SRM, funky_toy;
1309
1434
char *date_opt;
1528
1670
rc = manipulate_clock(show, adjust, noadjfile, set, set_time,
1529
hctosys, systohc, startup_time, utc,
1671
hctosys, systohc, systz, startup_time, utc,
1530
1672
local_opt, testing);
1531
1673
hwclock_exit(rc);
1532
1674
return rc; /* Not reached */