/* default or user defined dev (by hwclock --rtc=<path>) */
char *rtc_dev_name;
+static int rtc_dev_fd = -1;
+
+static void
+close_rtc(void) {
+ if (rtc_dev_fd != -1)
+ close(rtc_dev_fd);
+ rtc_dev_fd = -1;
+}
+
static int
open_rtc(void) {
char *fls[] = {
};
char **p;
+ if (rtc_dev_fd != -1)
+ return rtc_dev_fd;
+
/* --rtc option has been given */
if (rtc_dev_name)
- return open(rtc_dev_name, O_RDONLY);
-
- for (p=fls; *p; ++p) {
- int fd = open(*p, O_RDONLY);
-
- if (fd < 0 && (errno == ENOENT || errno == ENODEV))
- continue;
- rtc_dev_name = *p;
- return fd;
+ rtc_dev_fd = open(rtc_dev_name, O_RDONLY);
+ else {
+ for (p=fls; *p; ++p) {
+ rtc_dev_fd = open(*p, O_RDONLY);
+
+ if (rtc_dev_fd < 0 && (errno == ENOENT || errno == ENODEV))
+ continue;
+ rtc_dev_name = *p;
+ break;
+ }
+ if (rtc_dev_fd < 0)
+ rtc_dev_name = *fls; /* default for error messages */
}
- rtc_dev_name = *fls; /* default */
- return -1;
+
+ if (rtc_dev_fd != 1)
+ atexit(close_rtc);
+ return rtc_dev_fd;
}
static int
int rtc_fd; /* File descriptor of /dev/rtc */
int ret;
- rtc_fd = open(rtc_dev_name, O_RDONLY);
+ rtc_fd = open_rtc();
if (rtc_fd == -1) {
outsyserr(_("open() of %s failed"), rtc_dev_name);
ret = 1;
"failed unexpectedly"), rtc_dev_name);
ret = 1;
}
- close(rtc_fd);
}
return ret;
}
/* Read the RTC time/date, return answer via tm */
rc = do_rtc_read_ioctl(rtc_fd, tm);
- close(rtc_fd);
return rc;
}
if (debug)
printf(_("ioctl(%s) was successful.\n"), ioctlname);
- close(rtc_fd);
return 0;
}
struct clock_ops *
probe_for_rtc_clock(){
int rtc_fd = open_rtc();
- if (rtc_fd >= 0) {
- close(rtc_fd);
+ if (rtc_fd >= 0)
return &rtc;
- }
if (debug)
outsyserr(_("Open of %s failed"), rtc_dev_name);
return NULL;
if (ioctl(rtc_fd, RTC_EPOCH_READ, epoch_p) == -1) {
if (!silent)
outsyserr(_("ioctl(RTC_EPOCH_READ) to %s failed"), rtc_dev_name);
- close(rtc_fd);
return 1;
}
printf(_("we have read epoch %ld from %s "
"with RTC_EPOCH_READ ioctl.\n"), *epoch_p, rtc_dev_name);
- close(rtc_fd);
return 0;
}
"does not have the RTC_EPOCH_SET ioctl.\n"), rtc_dev_name);
else
outsyserr(_("ioctl(RTC_EPOCH_SET) to %s failed"), rtc_dev_name);
- close(rtc_fd);
return 1;
}
- close(rtc_fd);
return 0;
}