#include <getopt.h>
#include <limits.h>
#include <math.h>
-#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define OPTUTILS_EXIT_CODE EX_USAGE
+#define XALLOC_EXIT_CODE EX_OSERR
#include "c.h"
#include "closestream.h"
new_broken_time.tm_sec, (long)newtime);
if (ctl->testing)
- printf(_("Clock not changed - testing only.\n"));
- else {
+ printf(_("Test mode: clock was not changed\n"));
+ else
ur->set_hardware_clock(ctl, &new_broken_time);
- }
}
/*
}
if (ctl->testing) {
printf(_
- ("Not setting system clock because running in test mode.\n"));
+ ("Test mode: clock was not changed\n"));
retcode = 0;
} else {
const struct timezone tz = { minuteswest, 0 };
}
if (ctl->testing) {
printf(_
- ("Not setting system clock because running in test mode.\n"));
+ ("Test mode: clock was not changed\n"));
retcode = 0;
} else {
const struct timezone tz_utc = { 0, 0 };
(adjtime->local_utc == LOCAL) ? "LOCAL" : "UTC");
if (ctl->testing) {
- printf(_
- ("Not updating adjtime file because of testing mode.\n"));
- printf(_("Would have written the following to %s:\n%s"),
- ctl->adj_file_name, content);
+ if (ctl->debug){
+ printf(_("Test mode: %s was not updated with:\n%s"),
+ ctl->adj_file_name, content);
+ }
free(content);
return;
}
return 0;
}
-/*
- * Get or set the Hardware Clock epoch value in the kernel, as appropriate.
- * <getepoch>, <setepoch>, and <epoch> are hwclock invocation options.
- *
- * <epoch> == -1 if the user did not specify an "epoch" option.
+/**
+ * Get or set the kernel RTC driver's epoch on Alpha machines.
+ * ISA machines are hard coded for 1900.
*/
-#ifdef __linux__
-/*
- * Maintenance note: This should work on non-Alpha machines, but the
- * evidence today (98.03.04) indicates that the kernel only keeps the epoch
- * value on Alphas. If that is ever fixed, this function should be changed.
- */
-# ifndef __alpha__
-static void
-manipulate_epoch(const struct hwclock_control *ctl __attribute__((__unused__)))
-{
- warnx(_("The kernel keeps an epoch value for the Hardware Clock "
- "only on an Alpha machine.\nThis copy of hwclock was built for "
- "a machine other than Alpha\n(and thus is presumably not running "
- "on an Alpha now). No action taken."));
-}
-# else
+#if defined(__linux__) && defined(__alpha__)
static void
manipulate_epoch(const struct hwclock_control *ctl)
{
("Unable to set the epoch value in the kernel.\n"));
}
}
-# endif /* __alpha__ */
-#endif /* __linux__ */
+#endif /* __linux__ __alpha__ */
static void out_version(void)
{
printf(UTIL_LINUX_VERSION);
}
-/*
- * usage - Output (error and) usage information
- *
- * This function is called both directly from main to show usage information
- * and as fatal function from shhopt if some argument is not understood. In
- * case of normal usage info FMT should be NULL. In that case the info is
- * printed to stdout. If FMT is given usage will act like fprintf( stderr,
- * fmt, ... ), show a usage information and terminate the program
- * afterwards.
- */
-static void usage(const struct hwclock_control *ctl, const char *fmt, ...)
+static void __attribute__((__noreturn__))
+usage(const struct hwclock_control *ctl)
{
- FILE *usageto;
- va_list ap;
-
- usageto = fmt ? stderr : stdout;
-
- fputs(USAGE_HEADER, usageto);
- fputs(_(" hwclock [function] [option...]\n"), usageto);
-
- fputs(USAGE_SEPARATOR, usageto);
- fputs(_("Query or set the hardware clock.\n"), usageto);
-
- fputs(_("\nFunctions:\n"), usageto);
- fputs(_(" -h, --help show this help text and exit\n"
- " -r, --show read hardware clock and print result\n"
- " --get read hardware clock and print drift corrected result\n"
- " --set set the RTC to the time given with --date\n"), usageto);
- fputs(_(" -s, --hctosys set the system time from the hardware clock\n"
- " -w, --systohc set the hardware clock from the current system time\n"
- " --systz set the system time based on the current timezone\n"
- " --adjust adjust the RTC to account for systematic drift since\n"
- " the clock was last set or adjusted\n"), usageto);
-#ifdef __linux__
- fputs(_(" --getepoch print out the kernel's hardware clock epoch value\n"
- " --setepoch set the kernel's hardware clock epoch value to the \n"
- " value given with --epoch\n"), usageto);
+ FILE *out = stdout;
+ fputs(USAGE_HEADER, out);
+ fputs(_(" hwclock [function] [option...]\n"), out);
+
+ fputs(USAGE_SEPARATOR, out);
+ fputs(_("Query or set the RTC (Real Time Clock / Hardware Clock)\n"), out);
+
+ fputs(USAGE_FUNCTIONS, out);
+ fputs(_(" -r, --show display the RTC time\n"), out);
+ fputs(_(" --get display drift corrected RTC time\n"), out);
+ fputs(_(" --set set the RTC according to --date\n"), out);
+ fputs(_(" -s, --hctosys set the system time from the RTC\n"), out);
+ fputs(_(" -w, --systohc set the RTC from the system time\n"), out);
+ fputs(_(" --systz send timescale configurations to the kernel\n"), out);
+ fputs(_(" --adjust adjust the RTC to account for systematic drift\n"), out);
+#if defined(__linux__) && defined(__alpha__)
+ fputs(_(" --getepoch display the RTC epoch\n"), out);
+ fputs(_(" --setepoch set the RTC epoch according to --epoch\n"), out);
#endif
- fputs(_(" --predict predict RTC reading at time given with --date\n"
- " -V, --version display version information and exit\n"), usageto);
-
- fputs(USAGE_OPTIONS, usageto);
- fputs(_(" -u, --utc the hardware clock is kept in UTC\n"
- " --localtime the hardware clock is kept in local time\n"), usageto);
+ fputs(_(" --predict predict the drifted RTC time according to --date\n"), out);
+ fputs(USAGE_OPTIONS, out);
+ fputs(_(" -u, --utc inform hwclock the RTC timescale is UTC\n"), out);
+ fputs(_(" -l, --localtime inform hwclock the RTC timescale is Local\n"), out);
+ fprintf(out, _(
#ifdef __linux__
- fputs(_(" -f, --rtc <file> special /dev/... file to use instead of default\n"), usageto);
+ " -f, --rtc <file> use an alternate file to %1$s\n"
#endif
- fprintf(usageto, _(
- " --directisa access the ISA bus directly instead of %s\n"
- " --date <time> specifies the time to which to set the hardware clock\n"
- " --epoch <year> specifies the year which is the beginning of the\n"
- " hardware clock's epoch value\n"), _PATH_RTC_DEV);
- fprintf(usageto, _(
- " --update-drift update drift factor in %1$s (requires\n"
- " --set or --systohc)\n"
- " --noadjfile do not access %1$s; this requires the use of\n"
- " either --utc or --localtime\n"
- " --adjfile <file> specifies the path to the adjust file;\n"
- " the default is %1$s\n"), _PATH_ADJTIME);
- fputs(_(" --test do not update anything, just show what would happen\n"
- " -D, --debug debugging mode\n" "\n"), usageto);
-
- if (fmt) {
- va_start(ap, fmt);
- vfprintf(usageto, fmt, ap);
- va_end(ap);
- }
-
- fflush(usageto);
- hwclock_exit(ctl, fmt ? EX_USAGE : EX_OK);
+ " --directisa use the ISA bus instead of %1$s access\n"), _PATH_RTC_DEV);
+ fputs(_(" --date <time> date/time input for --set and --predict\n"), out);
+#if defined(__linux__) && defined(__alpha__)
+ fputs(_(" --epoch <year> epoch input for --setepoch\n"), out);
+#endif
+ fputs(_(" --update-drift update the RTC drift factor\n"), out);
+ fprintf(out, _(
+ " --noadjfile do not use %1$s\n"
+ " --adjfile <file> use an alternate file to %1$s\n"), _PATH_ADJTIME);
+ fputs(_(" --test dry run; use -D to view what would have happened\n"), out);
+ fputs(_(" -D, --debug use debug mode\n"), out);
+ fputs(USAGE_SEPARATOR, out);
+ fputs(USAGE_HELP, out);
+ fputs(USAGE_VERSION, out);
+ fprintf(out, USAGE_MAN_TAIL("hwclock(8)"));
+ hwclock_exit(ctl, EXIT_SUCCESS);
}
/*
*/
int main(int argc, char **argv)
{
- struct hwclock_control ctl = { NULL };
+ struct hwclock_control ctl = { .show = 1 }; /* default op is show */
struct timeval startup_time;
struct adjtime adjtime = { 0 };
struct timespec when = { 0 };
OPT_EPOCH,
OPT_GET,
OPT_GETEPOCH,
- OPT_LOCALTIME,
OPT_NOADJFILE,
- OPT_PREDICT_HC,
+ OPT_PREDICT,
OPT_SET,
OPT_SETEPOCH,
OPT_SYSTZ,
static const struct option longopts[] = {
{ "adjust", no_argument, NULL, 'a' },
{ "help", no_argument, NULL, 'h' },
+ { "localtime", no_argument, NULL, 'l' },
{ "show", no_argument, NULL, 'r' },
{ "hctosys", no_argument, NULL, 's' },
{ "utc", no_argument, NULL, 'u' },
{ "systohc", no_argument, NULL, 'w' },
{ "debug", no_argument, NULL, 'D' },
{ "set", no_argument, NULL, OPT_SET },
-#ifdef __linux__
+#if defined(__linux__) && defined(__alpha__)
{ "getepoch", no_argument, NULL, OPT_GETEPOCH },
{ "setepoch", no_argument, NULL, OPT_SETEPOCH },
+ { "epoch", required_argument, NULL, OPT_EPOCH },
#endif
{ "noadjfile", no_argument, NULL, OPT_NOADJFILE },
- { "localtime", no_argument, NULL, OPT_LOCALTIME },
{ "directisa", no_argument, NULL, OPT_DIRECTISA },
{ "test", no_argument, NULL, OPT_TEST },
{ "date", required_argument, NULL, OPT_DATE },
- { "epoch", required_argument, NULL, OPT_EPOCH },
#ifdef __linux__
{ "rtc", required_argument, NULL, 'f' },
#endif
{ "adjfile", required_argument, NULL, OPT_ADJFILE },
{ "systz", no_argument, NULL, OPT_SYSTZ },
- { "predict-hc", no_argument, NULL, OPT_PREDICT_HC },
+ { "predict", no_argument, NULL, OPT_PREDICT },
{ "get", no_argument, NULL, OPT_GET },
{ "update-drift", no_argument, NULL, OPT_UPDATE },
{ NULL, 0, NULL, 0 }
static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
{ 'a','r','s','w',
- OPT_GET, OPT_GETEPOCH, OPT_PREDICT_HC,
+ OPT_GET, OPT_GETEPOCH, OPT_PREDICT,
OPT_SET, OPT_SETEPOCH, OPT_SYSTZ },
- { 'u', OPT_LOCALTIME},
+ { 'l', 'u' },
{ OPT_ADJFILE, OPT_NOADJFILE },
{ OPT_NOADJFILE, OPT_UPDATE },
{ 0 }
};
int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
+ strutils_set_exitcode(EX_USAGE);
+
/* Remember what time we were invoked */
gettimeofday(&startup_time, NULL);
atexit(close_stdout);
while ((c = getopt_long(argc, argv,
- "?hvVDarsuwAJSFf:", longopts, NULL)) != -1) {
+ "hvVDalrsuwf:", longopts, NULL)) != -1) {
err_exclusive_options(c, longopts, excl, excl_st);
break;
case 'a':
ctl.adjust = 1;
+ ctl.show = 0;
+ ctl.hwaudit_on = 1;
+ break;
+ case 'l':
+ ctl.local_opt = 1; /* --localtime */
break;
case 'r':
ctl.show = 1;
break;
case 's':
ctl.hctosys = 1;
+ ctl.show = 0;
+ ctl.hwaudit_on = 1;
break;
case 'u':
ctl.utc = 1;
break;
case 'w':
ctl.systohc = 1;
+ ctl.show = 0;
+ ctl.hwaudit_on = 1;
break;
case OPT_SET:
ctl.set = 1;
+ ctl.show = 0;
+ ctl.hwaudit_on = 1;
break;
-#ifdef __linux__
+#if defined(__linux__) && defined(__alpha__)
case OPT_GETEPOCH:
ctl.getepoch = 1;
+ ctl.show = 0;
break;
case OPT_SETEPOCH:
ctl.setepoch = 1;
+ ctl.show = 0;
+ ctl.hwaudit_on = 1;
+ break;
+ case OPT_EPOCH:
+ ctl.epoch_option = /* --epoch */
+ strtoul_or_err(optarg, _("invalid epoch argument"));
break;
#endif
case OPT_NOADJFILE:
ctl.noadjfile = 1;
break;
- case OPT_LOCALTIME:
- ctl.local_opt = 1; /* --localtime */
- break;
case OPT_DIRECTISA:
ctl.directisa = 1;
break;
case OPT_DATE:
ctl.date_opt = optarg; /* --date */
break;
- case OPT_EPOCH:
- ctl.epoch_option = /* --epoch */
- strtoul_or_err(optarg, _("invalid epoch argument"));
- break;
case OPT_ADJFILE:
ctl.adj_file_name = optarg; /* --adjfile */
break;
case OPT_SYSTZ:
ctl.systz = 1; /* --systz */
+ ctl.show = 0;
+ ctl.hwaudit_on = 1;
break;
- case OPT_PREDICT_HC:
- ctl.predict = 1; /* --predict-hc */
+ case OPT_PREDICT:
+ ctl.predict = 1; /* --predict */
+ ctl.show = 0;
break;
case OPT_GET:
ctl.get = 1; /* --get */
+ ctl.show = 0;
break;
case OPT_UPDATE:
ctl.update = 1; /* --update-drift */
out_version();
return 0;
case 'h': /* --help */
- case '?':
+ usage(&ctl);
default:
- usage(&ctl, NULL);
+ errtryhelp(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
-#ifdef HAVE_LIBAUDIT
- if (!ctl.testing) {
- if (ctl.adjust || ctl.hctosys || ctl.systohc ||
- ctl.set || ctl.setepoch) {
- ctl.hwaudit_on = 1;
- }
- }
-#endif
if (argc > 0) {
- usage(&ctl, _("%s takes no non-option arguments. "
- "You supplied %d.\n"), program_invocation_short_name,
- argc);
+ warnx(_("%d too many arguments given"), argc);
+ errtryhelp(EXIT_FAILURE);
}
if (!ctl.adj_file_name)
ctl.adj_file_name = _PATH_ADJTIME;
+ if (ctl.update && !ctl.set && !ctl.systohc) {
+ warnx(_("--update-drift requires --set or --systohc"));
+ hwclock_exit(&ctl, EX_USAGE);
+ }
+
if (ctl.noadjfile && !ctl.utc && !ctl.local_opt) {
warnx(_("With --noadjfile, you must specify "
"either --utc or --localtime"));
}
if (ctl.set || ctl.predict) {
+ if (!ctl.date_opt){
+ warnx(_("--date is required for --set or --predict"));
+ hwclock_exit(&ctl, EX_USAGE);
+ }
if (parse_date(&when, ctl.date_opt, NULL))
set_time = when.tv_sec;
else {
}
}
- if (!(ctl.show | ctl.set | ctl.systohc | ctl.hctosys |
- ctl.systz | ctl.adjust | ctl.getepoch | ctl.setepoch |
- ctl.predict | ctl.get))
- ctl.show = 1; /* default to show */
-
-#ifdef __linux__
+#if defined(__linux__) && defined(__alpha__)
if (ctl.getepoch || ctl.setepoch) {
manipulate_epoch(&ctl);
hwclock_exit(&ctl, EX_OK);
return rc; /* Not reached */
}
-void __attribute__((__noreturn__))
+void
hwclock_exit(const struct hwclock_control *ctl
#ifndef HAVE_LIBAUDIT
__attribute__((__unused__))
, int status)
{
#ifdef HAVE_LIBAUDIT
- if (ctl->hwaudit_on) {
+ if (ctl->hwaudit_on && !ctl->testing) {
audit_log_user_message(hwaudit_fd, AUDIT_USYS_CONFIG,
"op=change-system-time", NULL, NULL, NULL,
status ? 0 : 1);