]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - hwclock/rtc.c
Imported from util-linux-2.12 tarball.
[thirdparty/util-linux.git] / hwclock / rtc.c
index 06ac4360f4da954919e21769ccc8515e0bca3e1a..bc9733b801a0300415de2ab636d0b71d9aecf54a 100644 (file)
@@ -1,6 +1,7 @@
 /* rtc.c - Use /dev/rtc for clock access */
 #include <unistd.h>            /* for close() */
 #include <fcntl.h>             /* for O_RDONLY */
+#include <sysexits.h>
 #include <sys/ioctl.h>
 
 #include "clock.h"
  * used a struct rtc_time different from that used in mc146818rtc.h.
  */
 
-/* ia64 uses /dev/efirtc (char 10,136) */
-#if __ia64__
-#define RTC_DEV "/dev/efirtc"
-#else
-#define RTC_DEV "/dev/rtc"
-#endif
-
 /* On Sparcs, there is a <asm/rtc.h> that defines different ioctls
    (that are required on my machine). However, this include file
    does not exist on other architectures. */
@@ -87,38 +81,78 @@ struct linux_rtc_time {
 #endif
 
 
+/* ia64 uses /dev/efirtc (char 10,136) */
+/* devfs uses /dev/misc/rtc */
+#if __ia64__
+#define RTC_DEVN       "efirtc"
+#else
+#define RTC_DEVN       "rtc"
+#endif
+
+static char *rtc_dev_name;
+
+static int
+open_rtc(void) {
+       int rtc_fd;
+
+       rtc_dev_name = "/dev/" RTC_DEVN;
+       rtc_fd = open(rtc_dev_name, O_RDONLY);
+       if (rtc_fd < 0 && errno == ENOENT) {
+               rtc_dev_name = "/dev/misc/" RTC_DEVN;
+               rtc_fd = open(rtc_dev_name, O_RDONLY);
+               if (rtc_fd < 0 && errno == ENOENT)
+                       rtc_dev_name = "/dev/" RTC_DEVN;
+       }
+       return rtc_fd;
+}
+
+static int
+open_rtc_or_exit(void) {
+       int rtc_fd = open_rtc();
+
+       if (rtc_fd < 0) {
+               outsyserr(_("open() of %s failed"), rtc_dev_name);
+               exit(EX_OSFILE);
+       }
+       return rtc_fd;
+}
+
 static int
 do_rtc_read_ioctl(int rtc_fd, struct tm *tm) {
-  int rc;
-  char *ioctlname;
-#ifdef __sparc__
-  struct sparc_rtc_time stm;
+       int rc = -1;
+       char *ioctlname;
 
-  ioctlname = "RTCGET";
-  rc = ioctl(rtc_fd, RTCGET, &stm);
-#else
-  ioctlname = "RTC_RD_TIME";
-  rc = ioctl(rtc_fd, RTC_RD_TIME, tm);
-#endif
-  if (rc == -1) {
-    perror(ioctlname);
-    fprintf(stderr, _("ioctl() to %s to read the time failed.\n"),RTC_DEV);
-    exit(5);
-  }
 #ifdef __sparc__
-  tm->tm_sec = stm.sec;
-  tm->tm_min = stm.min;
-  tm->tm_hour = stm.hour;
-  tm->tm_mday = stm.dom;
-  tm->tm_mon = stm.month - 1;
-  tm->tm_year = stm.year - 1900;
-  tm->tm_wday = stm.dow - 1;
-  tm->tm_yday = -1;            /* day in the year */
+       /* some but not all sparcs use a different ioctl and struct */
+       struct sparc_rtc_time stm;
+
+       ioctlname = "RTCGET";
+       rc = ioctl(rtc_fd, RTCGET, &stm);
+       if (rc == 0) {
+               tm->tm_sec = stm.sec;
+               tm->tm_min = stm.min;
+               tm->tm_hour = stm.hour;
+               tm->tm_mday = stm.dom;
+               tm->tm_mon = stm.month - 1;
+               tm->tm_year = stm.year - 1900;
+               tm->tm_wday = stm.dow - 1;
+               tm->tm_yday = -1;               /* day in the year */
+       }
 #endif
-  tm->tm_isdst = -1;          /* don't know whether it's daylight */
-  return 0;
-}  
-
+       if (rc == -1) {         /* no sparc, or RTCGET failed */
+               ioctlname = "RTC_RD_TIME";
+               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);
+               exit(EX_IOERR);
+       }
+
+       tm->tm_isdst = -1;          /* don't know whether it's dst */
+       return 0;
+}
 
 static int
 busywait_for_rtc_clock_tick(const int rtc_fd) {
@@ -133,7 +167,8 @@ busywait_for_rtc_clock_tick(const int rtc_fd) {
   int rc;
 
   if (debug)
-    printf(_("Waiting in loop for time from %s to change\n"),RTC_DEV);
+    printf(_("Waiting in loop for time from %s to change\n"),
+          rtc_dev_name);
 
   rc = do_rtc_read_ioctl(rtc_fd, &start_time);
   if (rc)
@@ -167,27 +202,28 @@ synchronize_to_clock_tick_rtc(void) {
 int rtc_fd;  /* File descriptor of /dev/rtc */
 int ret;
 
-  rtc_fd = open(RTC_DEV,O_RDONLY);
+  rtc_fd = open(rtc_dev_name, O_RDONLY);
   if (rtc_fd == -1) {
-    outsyserr(_("open() of %s failed"),RTC_DEV);
+    outsyserr(_("open() of %s failed"), rtc_dev_name);
     ret = 1;
   } else {
     int rc;  /* Return code from ioctl */
     /* Turn on update interrupts (one per second) */
-#if defined(__alpha__) || defined(__sparc__)
+#if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
     /* Not all alpha kernels reject RTC_UIE_ON, but probably they should. */
     rc = -1;
     errno = EINVAL;
 #else
     rc = ioctl(rtc_fd, RTC_UIE_ON, 0);
 #endif
-    if (rc == -1 && errno == EINVAL) {
+    if (rc == -1 && (errno == ENOTTY || errno == EINVAL)) {
       /* This rtc device doesn't have interrupt functions.  This is typical
          on an Alpha, where the Hardware Clock interrupts are used by the
          kernel for the system clock, so aren't at the user's disposal.
          */
       if (debug)
-             printf(_("%s does not have interrupt functions. "),RTC_DEV);
+             printf(_("%s does not have interrupt functions. "),
+                    rtc_dev_name);
       ret = busywait_for_rtc_clock_tick(rtc_fd);
     } else if (rc == 0) {
       unsigned long dummy;
@@ -195,7 +231,8 @@ int ret;
       /* this blocks until the next update interrupt */
       rc = read(rtc_fd, &dummy, sizeof(dummy));
       if (rc == -1) {
-        outsyserr(_("read() to %s to wait for clock tick failed"),RTC_DEV);
+        outsyserr(_("read() to %s to wait for clock tick failed"),
+                 rtc_dev_name);
         ret = 1;
       } else {
         ret = 0;
@@ -203,11 +240,11 @@ int ret;
       /* 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);
+        outsyserr(_("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);
+               "failed unexpectedly"), rtc_dev_name);
       ret = 1;
     }
     close(rtc_fd);
@@ -218,69 +255,63 @@ int ret;
 
 static int
 read_hardware_clock_rtc(struct tm *tm) {
-/*----------------------------------------------------------------------------
-  Read the hardware clock and return the current time via <tm>
-  argument.  Use ioctls to "rtc" device /dev/rtc.
------------------------------------------------------------------------------*/
-  int rtc_fd;  /* File descriptor of /dev/rtc */
+       int rtc_fd;
 
-  rtc_fd = open(RTC_DEV,O_RDONLY);
-  if (rtc_fd == -1) {
-    outsyserr(_("open() of %s failed"),RTC_DEV);
-    exit(5);
-  }
+       rtc_fd = open_rtc_or_exit();
 
-  /* Read the RTC time/date */
-  do_rtc_read_ioctl(rtc_fd, tm);
+       /* Read the RTC time/date, return answer via tm */
+       do_rtc_read_ioctl(rtc_fd, tm);
 
-  close(rtc_fd);
-  return 0;
+       close(rtc_fd);
+       return 0;
 }
 
 
 static int
 set_hardware_clock_rtc(const struct tm *new_broken_time) {
-/*----------------------------------------------------------------------------
+/*-------------------------------------------------------------------------
   Set the Hardware Clock to the broken down time <new_broken_time>.
   Use ioctls to "rtc" device /dev/rtc.
-----------------------------------------------------------------------------*/
-  int rc;
-  int rtc_fd;
+  -------------------------------------------------------------------------*/
+       int rc = -1;
+       int rtc_fd;
+       char *ioctlname;
+
+       rtc_fd = open_rtc_or_exit();
 
-  rtc_fd = open(RTC_DEV, O_RDONLY);
-  if (rtc_fd < 0) {
-    outsyserr(_("Unable to open %s"),RTC_DEV);
-    exit(5);
-  } else {
-    char *ioctlname;
 #ifdef __sparc__
-    struct sparc_rtc_time stm;
-
-    stm.sec = new_broken_time->tm_sec;
-    stm.min = new_broken_time->tm_min;
-    stm.hour = new_broken_time->tm_hour;
-    stm.dom = new_broken_time->tm_mday;
-    stm.month = new_broken_time->tm_mon + 1;
-    stm.year = new_broken_time->tm_year + 1900;
-    stm.dow = new_broken_time->tm_wday + 1;
-
-    ioctlname = "RTCSET";
-    rc = ioctl(rtc_fd, RTCSET, &stm);
-#else
-    ioctlname = "RTC_SET_TIME";
-    rc = ioctl(rtc_fd, RTC_SET_TIME, new_broken_time);
+       {
+               struct sparc_rtc_time stm;
+
+               stm.sec = new_broken_time->tm_sec;
+               stm.min = new_broken_time->tm_min;
+               stm.hour = new_broken_time->tm_hour;
+               stm.dom = new_broken_time->tm_mday;
+               stm.month = new_broken_time->tm_mon + 1;
+               stm.year = new_broken_time->tm_year + 1900;
+               stm.dow = new_broken_time->tm_wday + 1;
+
+               ioctlname = "RTCSET";
+               rc = ioctl(rtc_fd, RTCSET, &stm);
+       }
 #endif
-    if (rc == -1) {
-      perror(ioctlname);
-      fprintf(stderr, _("ioctl() to %s to set the time failed.\n"),RTC_DEV);
-      exit(5);
-    } else {
-      if (debug)
-        printf(_("ioctl(%s) was successful.\n"), ioctlname);
-    }
-    close(rtc_fd);
-  }
-  return 0;
+       if (rc == -1) {         /* no sparc, or RTCSET failed */
+               ioctlname = "RTC_SET_TIME";
+               rc = ioctl(rtc_fd, RTC_SET_TIME, new_broken_time);
+       }
+
+       if (rc == -1) {
+               perror(ioctlname);
+               fprintf(stderr, _("ioctl() to %s to set the time failed.\n"),
+                       rtc_dev_name);
+               exit(EX_IOERR);
+       }
+
+       if (debug)
+               printf(_("ioctl(%s) was successful.\n"), ioctlname);
+
+       close(rtc_fd);
+       return 0;
 }
 
 
@@ -290,7 +321,7 @@ get_permissions_rtc(void) {
 }
 
 static struct clock_ops rtc = {
-       RTC_DEV " interface to clock",
+       "/dev/" RTC_DEVN " interface to clock",
        get_permissions_rtc,
        read_hardware_clock_rtc,
        set_hardware_clock_rtc,
@@ -300,14 +331,14 @@ static struct clock_ops rtc = {
 /* return &rtc if /dev/rtc can be opened, NULL otherwise */
 struct clock_ops *
 probe_for_rtc_clock(){
-    int rtc_fd = open(RTC_DEV, O_RDONLY);
-    if (rtc_fd > 0) {
-      close(rtc_fd);
-      return &rtc;
-    }
-    if (debug)
-      outsyserr(_("Open of %s failed"),RTC_DEV);
-    return NULL;
+       int rtc_fd = open_rtc();
+       if (rtc_fd >= 0) {
+               close(rtc_fd);
+               return &rtc;
+       }
+       if (debug)
+               outsyserr(_("Open of %s failed"), rtc_dev_name);
+       return NULL;
 }
 
 
@@ -319,30 +350,31 @@ get_epoch_rtc(unsigned long *epoch_p, int silent) {
 ----------------------------------------------------------------------------*/
   int rtc_fd;
 
-  rtc_fd = open(RTC_DEV, O_RDONLY);
+  rtc_fd = open_rtc();
   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);
+                "file %s.  This file does not exist on this system.\n"),
+               rtc_dev_name);
       else 
-        outsyserr(_("Unable to open %s"),RTC_DEV);
+        outsyserr(_("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"),RTC_DEV);
+      outsyserr(_("ioctl(RTC_EPOCH_READ) to %s failed"), rtc_dev_name);
     close(rtc_fd);
     return 1;
   }
 
   if (debug)
          printf(_("we have read epoch %ld from %s "
-                "with RTC_EPOCH_READ ioctl.\n"), *epoch_p,RTC_DEV);
+                "with RTC_EPOCH_READ ioctl.\n"), *epoch_p, rtc_dev_name);
 
   close(rtc_fd);
   return 0;
@@ -366,27 +398,28 @@ set_epoch_rtc(unsigned long epoch) {
     return 1;
   }
 
-  rtc_fd = open(RTC_DEV, O_RDONLY);
+  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);
+              "file %s.  This file does not exist on this system.\n"),
+             rtc_dev_name);
     else
-      outsyserr(_("Unable to open %s"),RTC_DEV);
+      outsyserr(_("Unable to open %s"), rtc_dev_name);
     return 1;
   }
 
   if (debug)
     printf(_("setting epoch to %ld "
-          "with RTC_EPOCH_SET ioctl to %s.\n"), epoch, RTC_DEV);
+          "with RTC_EPOCH_SET ioctl to %s.\n"), epoch, rtc_dev_name);
 
   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);
+             "does not have the RTC_EPOCH_SET ioctl.\n"), rtc_dev_name);
     else 
-      outsyserr(_("ioctl(RTC_EPOCH_SET) to %s failed"),RTC_DEV);
+      outsyserr(_("ioctl(RTC_EPOCH_SET) to %s failed"), rtc_dev_name);
     close(rtc_fd);
     return 1;
   }