static int hwaudit_on;
#endif
-#define MYNAME "hwclock"
-
-char *progname = MYNAME;
-
/* The struct that holds our hardware access routines */
struct clock_ops *ur;
tm->tm_year + 1900);
fclose(fp);
} else
- perror(LASTDATE);
+ warn(_("cannot write %s"), LASTDATE);
}
static void read_date_from_file(struct tm *tm)
adjfile = fopen(adj_file_name, "r"); /* open file for reading */
if (adjfile == NULL) {
- outsyserr("cannot open file %s", adj_file_name);
+ warn("cannot open file %s", adj_file_name);
return EX_OSFILE;
}
} else {
adjtime_p->local_utc = UNKNOWN;
if (line3[0]) {
- fprintf(stderr,
- _("%s: Warning: unrecognized third line in adjtime file\n"),
- MYNAME);
- fprintf(stderr,
- _("(Expected: `UTC' or `LOCAL' or nothing.)\n"));
+ warnx(_("Warning: unrecognized third line in adjtime file\n"
+ "(Expected: `UTC' or `LOCAL' or nothing.)"));
}
}
const double sync_duration)
{
if (!hclock_valid)
- fprintf(stderr,
- _
- ("The Hardware Clock registers contain values that are "
- "either invalid (e.g. 50th day of month) or beyond the range "
- "we can handle (e.g. Year 2095).\n"));
+ warnx(_
+ ("The Hardware Clock registers contain values that are "
+ "either invalid (e.g. 50th day of month) or beyond the range "
+ "we can handle (e.g. Year 2095)."));
else {
struct tm *lt;
char *format = "%c";
int rc; /* local return code */
if (date_opt == NULL) {
- fprintf(stderr, _("No --date option specified.\n"));
+ warnx(_("No --date option specified."));
return 14;
}
/* prevent overflow - a security risk */
if (strlen(date_opt) > sizeof(date_command) - 50) {
- fprintf(stderr, _("--date argument too long\n"));
+ warnx(_("--date argument too long"));
return 13;
}
/* Quotes in date_opt would ruin the date command we construct. */
if (strchr(date_opt, '"') != NULL) {
- fprintf(stderr,
- _
- ("The value of the --date option is not a valid date.\n"
- "In particular, it contains quotation marks.\n"));
+ warnx(_
+ ("The value of the --date option is not a valid date.\n"
+ "In particular, it contains quotation marks."));
return 12;
}
date_child_fp = popen(date_command, "r");
if (date_child_fp == NULL) {
- outsyserr(_("Unable to run 'date' program in /bin/sh shell. "
+ warn(_("Unable to run 'date' program in /bin/sh shell. "
"popen() failed"));
return 10;
}
if (debug)
printf(_("response from date command = %s\n"), date_resp);
if (strncmp(date_resp, magic, sizeof(magic) - 1) != 0) {
- fprintf(stderr, _("The date command issued by %s returned "
+ warnx(_("The date command issued by %s returned "
"unexpected results.\n"
"The command was:\n %s\n"
- "The response was:\n %s\n"),
- MYNAME, date_command, date_resp);
+ "The response was:\n %s"),
+ program_invocation_short_name, date_command, date_resp);
retcode = 8;
} else {
long seconds_since_epoch;
rc = sscanf(date_resp + sizeof(magic) - 1, "%ld",
&seconds_since_epoch);
if (rc < 1) {
- fprintf(stderr,
- _("The date command issued by %s returned "
- "something other than an integer where the "
- "converted time value was expected.\n"
- "The command was:\n %s\n"
- "The response was:\n %s\n"),
- MYNAME, date_command, date_resp);
+ warnx(_("The date command issued by %s returned "
+ "something other than an integer where the "
+ "converted time value was expected.\n"
+ "The command was:\n %s\n"
+ "The response was:\n %s\n"),
+ program_invocation_short_name, date_command,
+ date_resp);
retcode = 6;
} else {
retcode = 0;
int retcode;
if (!hclock_valid) {
- fprintf(stderr,
- _
- ("The Hardware Clock does not contain a valid time, so "
- "we cannot set the System Time from it.\n"));
+ warnx(_
+ ("The Hardware Clock does not contain a valid time, so "
+ "we cannot set the System Time from it."));
retcode = 1;
} else {
struct timeval tv;
rc = settimeofday(&tv, &tz);
if (rc) {
if (errno == EPERM) {
- fprintf(stderr,
- _
- ("Must be superuser to set system clock.\n"));
+ warnx(_
+ ("Must be superuser to set system clock."));
retcode = EX_NOPERM;
} else {
- outsyserr(_("settimeofday() failed"));
+ warn(_("settimeofday() failed"));
retcode = 1;
}
} else
rc = settimeofday(&tv, &tz);
if (rc) {
if (errno == EPERM) {
- fprintf(stderr,
- _
- ("Must be superuser to set system clock.\n"));
+ warnx(_
+ ("Must be superuser to set system clock."));
retcode = EX_NOPERM;
} else {
- outsyserr(_("settimeofday() failed"));
+ warn(_("settimeofday() failed"));
retcode = 1;
}
} else
adjfile = fopen(adj_file_name, "w");
if (adjfile == NULL) {
- outsyserr(_
- ("Could not open file with the clock adjustment parameters "
- "in it (%s) for writing"),
- adj_file_name);
+ warn(_
+ ("Could not open file with the clock adjustment parameters "
+ "in it (%s) for writing"), adj_file_name);
err = 1;
} else {
if (fputs(newfile, adjfile) < 0) {
- outsyserr(_
- ("Could not update file with the clock adjustment "
- "parameters (%s) in it"),
- adj_file_name);
+ warn(_
+ ("Could not update file with the clock adjustment "
+ "parameters (%s) in it"),
+ adj_file_name);
err = 1;
}
if (fclose(adjfile) < 0) {
- outsyserr(_
- ("Could not update file with the clock adjustment "
- "parameters (%s) in it"),
- adj_file_name);
+ warn(_
+ ("Could not update file with the clock adjustment "
+ "parameters (%s) in it"),
+ adj_file_name);
err = 1;
}
}
if (err)
- fprintf(stderr,
- _
- ("Drift adjustment parameters not updated.\n"));
+ warnx(_
+ ("Drift adjustment parameters not updated."));
}
}
}
const bool universal, const bool testing)
{
if (!hclock_valid) {
- fprintf(stderr,
- _("The Hardware Clock does not contain a valid time, "
- "so we cannot adjust it.\n"));
+ warnx(_("The Hardware Clock does not contain a valid time, "
+ "so we cannot adjust it."));
adjtime_p->last_calib_time = 0; /* calibration startover is required */
adjtime_p->last_adj_time = 0;
adjtime_p->not_adjusted = 0;
* should be changed.
*/
#ifndef __alpha__
- fprintf(stderr,
- _("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.\n"));
+ 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 (getepoch) {
unsigned long epoch;
if (get_epoch_rtc(&epoch, 0))
- fprintf(stderr,
- _
- ("Unable to get the epoch value from the kernel.\n"));
+ warnx(_
+ ("Unable to get the epoch value from the kernel."));
else
printf(_("Kernel is assuming an epoch value of %lu\n"),
epoch);
} else if (setepoch) {
if (epoch_opt == -1)
- fprintf(stderr,
- _
- ("To set the epoch value, you must use the 'epoch' "
- "option to tell to what value to set it.\n"));
+ warnx(_
+ ("To set the epoch value, you must use the 'epoch' "
+ "option to tell to what value to set it."));
else if (testing)
printf(_
("Not setting the epoch to %d - testing only.\n"),
static void out_version(void)
{
- printf(_("%s from %s\n"), MYNAME, PACKAGE_STRING);
+ printf(_("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING);
}
/*
"\n"));
#endif
- fflush(stdout);
+ fflush(usageto);
if (fmt) {
usageto = stderr;
va_start(ap, fmt);
va_end(ap);
}
- hwclock_exit(fmt ? EX_USAGE : 0);
+ hwclock_exit(fmt ? EX_USAGE : EX_OK);
}
static const struct option longopts[] = {
* You get these error codes only when the kernel doesn't
* have audit compiled in.
*/
- fprintf(stderr, _("%s: Unable to connect to audit system\n"),
- MYNAME);
+ warnx(_("Unable to connect to audit system"));
return EX_NOPERM;
}
#endif
#endif
if (argc > 0) {
usage(_("%s takes no non-option arguments. "
- "You supplied %d.\n"), MYNAME, argc);
+ "You supplied %d.\n"), program_invocation_short_name,
+ argc);
}
if (show + set + systohc + hctosys + systz + adjust + getepoch
+ setepoch + predict > 1) {
- fprintf(stderr, _("You have specified multiple functions.\n"
- "You can only perform one function "
- "at a time.\n"));
+ warnx(_("You have specified multiple functions.\n"
+ "You can only perform one function at a time."));
hwclock_exit(EX_USAGE);
}
if (utc && local_opt) {
- fprintf(stderr, _("%s: The --utc and --localtime options "
- "are mutually exclusive. You specified "
- "both.\n"), MYNAME);
+ warnx(_("The --utc and --localtime options "
+ "are mutually exclusive. You specified both."));
hwclock_exit(EX_USAGE);
}
if (adjust && noadjfile) {
- fprintf(stderr, _("%s: The --adjust and --noadjfile options "
- "are mutually exclusive. You specified "
- "both.\n"), MYNAME);
+ warnx(_("The --adjust and --noadjfile options "
+ "are mutually exclusive. You specified both."));
hwclock_exit(EX_USAGE);
}
if (adj_file_name && noadjfile) {
- fprintf(stderr, _("%s: The --adjfile and --noadjfile options "
- "are mutually exclusive. You specified "
- "both.\n"), MYNAME);
+ warnx(_("The --adjfile and --noadjfile options "
+ "are mutually exclusive. You specified both."));
hwclock_exit(EX_USAGE);
}
if (!adj_file_name)
adj_file_name = ADJPATH;
if (noadjfile && !(utc || local_opt)) {
- fprintf(stderr, _("%s: With --noadjfile, you must specify "
- "either --utc or --localtime\n"), MYNAME);
+ warnx(_("With --noadjfile, you must specify "
+ "either --utc or --localtime"));
hwclock_exit(EX_USAGE);
}
#ifdef __alpha__
rc = interpret_date_string(date_opt, &set_time);
/* (time-consuming) */
if (rc != 0) {
- fprintf(stderr, _("No usable set-to time. "
- "Cannot set clock.\n"));
+ warnx(_("No usable set-to time. "
+ "Cannot set clock."));
hwclock_exit(EX_USAGE);
}
}
else {
/* program is designed to run setuid (in some situations) */
if (set || systohc || adjust) {
- fprintf(stderr,
- _("Sorry, only the superuser can change "
- "the Hardware Clock.\n"));
+ warnx(_("Sorry, only the superuser can change "
+ "the Hardware Clock."));
permitted = FALSE;
} else if (systz || hctosys) {
- fprintf(stderr,
- _("Sorry, only the superuser can change "
- "the System Clock.\n"));
+ warnx(_("Sorry, only the superuser can change "
+ "the System Clock."));
permitted = FALSE;
} else if (setepoch) {
- fprintf(stderr,
- _("Sorry, only the superuser can change the "
- "Hardware Clock epoch in the kernel.\n"));
+ warnx(_("Sorry, only the superuser can change the "
+ "Hardware Clock epoch in the kernel."));
permitted = FALSE;
} else
permitted = TRUE;
#ifdef __linux__
if (getepoch || setepoch) {
manipulate_epoch(getepoch, setepoch, epoch_option, testing);
- hwclock_exit(0);
+ hwclock_exit(EX_OK);
}
#endif
if (debug)
out_version();
+
if (!systz && !predict) {
determine_clock_access_method(directisa);
if (!ur) {
- fprintf(stderr,
- _("Cannot access the Hardware Clock via "
- "any known method.\n"));
+ warnx(_("Cannot access the Hardware Clock via "
+ "any known method."));
if (!debug)
- fprintf(stderr,
- _("Use the --debug option to see the "
- "details of our search for an access "
- "method.\n"));
- hwclock_exit(1);
+ warnx(_("Use the --debug option to see the "
+ "details of our search for an access "
+ "method."));
+ hwclock_exit(EX_SOFTWARE);
}
}
return rc; /* Not reached */
}
-/* A single routine for greater uniformity */
-void outsyserr(char *msg, ...)
-{
- va_list args;
- int errsv = errno;
-
- fprintf(stderr, "%s: ", progname);
- va_start(args, msg);
- vfprintf(stderr, msg, args);
- va_end(args);
- fprintf(stderr, ", errno=%d: %s.\n", errsv, strerror(errsv));
-}
-
#ifdef HAVE_LIBAUDIT
-void hwaudit_exit(int status)
+/*
+ * hwclock_exit calls either this function or plain exit depending
+ * HAVE_LIBAUDIT, see also clock.h
+ */
+void __attribute__((__noreturn__)) hwaudit_exit(int status)
{
if (hwaudit_on) {
audit_log_user_message(hwaudit_fd, AUDIT_USYS_CONFIG,
int rtc_fd = open_rtc();
if (rtc_fd < 0) {
- outsyserr(_("open() of %s failed"), rtc_dev_name);
+ warn(_("open() of %s failed"), rtc_dev_name);
hwclock_exit(EX_OSFILE);
}
return rtc_fd;
rc = ioctl(rtc_fd, RTC_RD_TIME, tm);
}
if (rc == -1) {
- perror(ioctlname);
- fprintf(stderr, _("ioctl() to %s to read the time failed.\n"),
- rtc_dev_name);
+ warn(_("ioctl(%s) to %s to read the time failed"),
+ ioctlname, rtc_dev_name);
return -1;
}
break;
gettimeofday(&now, NULL);
if (time_diff(now, begin) > 1.5) {
- fprintf(stderr,
- _("Timed out waiting for time change.\n"));
+ warnx(_("Timed out waiting for time change."));
return 2;
}
} while (1);
rtc_fd = open_rtc();
if (rtc_fd == -1) {
- outsyserr(_("open() of %s failed"), rtc_dev_name);
+ warn(_("open() of %s failed"), rtc_dev_name);
ret = 1;
} else {
int rc; /* Return code from ioctl */
rc = read(rtc_fd, &dummy, sizeof(dummy));
ret = 1;
if (rc == -1)
- outsyserr(_
- ("read() to %s to wait for clock tick failed"),
- rtc_dev_name);
+ warn(_
+ ("read() to %s to wait for clock tick failed"),
+ rtc_dev_name);
else
ret = 0;
#else
rc = select(rtc_fd + 1, &rfds, NULL, NULL, &tv);
ret = 1;
if (rc == -1)
- outsyserr(_
- ("select() to %s to wait for clock tick failed"),
- rtc_dev_name);
+ warn(_
+ ("select() to %s to wait for clock tick failed"),
+ rtc_dev_name);
else if (rc == 0)
- fprintf(stderr,
- _
- ("select() to %s to wait for clock tick timed out\n"),
- rtc_dev_name);
+ warn(_
+ ("select() to %s to wait for clock tick timed out"),
+ rtc_dev_name);
else
ret = 0;
#endif
/* Turn off update interrupts */
rc = ioctl(rtc_fd, RTC_UIE_OFF, 0);
if (rc == -1)
- outsyserr(_
- ("ioctl() to %s to turn off update interrupts failed"),
- rtc_dev_name);
+ warn(_
+ ("ioctl() to %s to turn off update interrupts failed"),
+ rtc_dev_name);
} else {
- outsyserr(_
- ("ioctl() to %s to turn on update interrupts "
- "failed unexpectedly"), rtc_dev_name);
+ warn(_
+ ("ioctl() to %s to turn on update interrupts "
+ "failed unexpectedly"), rtc_dev_name);
ret = 1;
}
}
}
if (rc == -1) {
- perror(ioctlname);
- fprintf(stderr, _("ioctl() to %s to set the time failed.\n"),
- rtc_dev_name);
+ warn(_("ioctl(%s) to %s to set the time failed."),
+ ioctlname, rtc_dev_name);
hwclock_exit(EX_IOERR);
}
if (rtc_fd >= 0)
return &rtc;
if (debug)
- outsyserr(_("Open of %s failed"), rtc_dev_name);
+ warn(_("Open of %s failed"), rtc_dev_name);
return NULL;
}
if (rtc_fd < 0) {
if (!silent) {
if (errno == ENOENT)
- fprintf(stderr,
- _
- ("To manipulate the epoch value in the kernel, we must "
- "access the Linux 'rtc' device driver via the device special "
- "file %s. This file does not exist on this system.\n"),
- rtc_dev_name);
+ warnx(_
+ ("To manipulate the epoch value in the kernel, we must "
+ "access the Linux 'rtc' device driver via the device special "
+ "file %s. This file does not exist on this system."),
+ rtc_dev_name);
else
- outsyserr(_("Unable to open %s"), rtc_dev_name);
+ warn(_("Unable to open %s"), rtc_dev_name);
}
return 1;
}
if (ioctl(rtc_fd, RTC_EPOCH_READ, epoch_p) == -1) {
if (!silent)
- outsyserr(_("ioctl(RTC_EPOCH_READ) to %s failed"),
+ warn(_("ioctl(RTC_EPOCH_READ) to %s failed"),
rtc_dev_name);
return 1;
}
* Bad habit, deciding not to do what the user asks just
* because one believes that the kernel might not like it.
*/
- fprintf(stderr, _("The epoch value may not be less than 1900. "
- "You requested %ld\n"), epoch);
+ warnx(_("The epoch value may not be less than 1900. "
+ "You requested %ld"), epoch);
return 1;
}
rtc_fd = open_rtc();
if (rtc_fd < 0) {
if (errno == ENOENT)
- fprintf(stderr,
- _
- ("To manipulate the epoch value in the kernel, we must "
- "access the Linux 'rtc' device driver via the device special "
- "file %s. This file does not exist on this system.\n"),
- rtc_dev_name);
+ warnx(_
+ ("To manipulate the epoch value in the kernel, we must "
+ "access the Linux 'rtc' device driver via the device special "
+ "file %s. This file does not exist on this system."),
+ rtc_dev_name);
else
- outsyserr(_("Unable to open %s"), rtc_dev_name);
+ warn(_("Unable to open %s"), rtc_dev_name);
return 1;
}
if (ioctl(rtc_fd, RTC_EPOCH_SET, epoch) == -1) {
if (errno == EINVAL)
- fprintf(stderr, _("The kernel device driver for %s "
- "does not have the RTC_EPOCH_SET ioctl.\n"),
- rtc_dev_name);
+ warnx(_("The kernel device driver for %s "
+ "does not have the RTC_EPOCH_SET ioctl."),
+ rtc_dev_name);
else
- outsyserr(_("ioctl(RTC_EPOCH_SET) to %s failed"),
+ warn(_("ioctl(RTC_EPOCH_SET) to %s failed"),
rtc_dev_name);
return 1;
}