Advertisement
AthabaskanCoreMiner

vmtimed_vmmci0.c

Aug 7th, 2017
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. // Originally from http://www.tedunangst.com/flak/post/vmtimed with minimal modification
  2. #include <sys/param.h>
  3. #include <sys/sysctl.h>
  4. #include <sys/sensors.h>
  5. #include <sys/time.h>
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <syslog.h>
  11. #include <unistd.h>
  12.  
  13. void
  14. error(const char *msg)
  15. {
  16.         syslog(LOG_DAEMON | LOG_ERR, "%s", msg);
  17.         exit(1);
  18. }
  19.  
  20. int
  21. findvmt0(void)
  22. {
  23.         struct sensordev sdev;
  24.         size_t slen;
  25.         int mib[5];
  26.         int i;
  27.  
  28.         mib[0] = CTL_HW;
  29.         mib[1] = HW_SENSORS;
  30.         for (i = 0; i < 20; i++) {
  31.                 mib[2] = i;
  32.                 slen = sizeof(sdev);
  33.                 if (sysctl(mib, 3, &sdev, &slen, NULL, 0) == -1)
  34.                         break;
  35.                 if (strcmp(sdev.xname, "vmmci0") == 0)
  36.                         return i;
  37.         }
  38.         return -1;
  39. }
  40.  
  41. void
  42. timeloop(int vmt0)
  43. {
  44.         struct sensor sensor;
  45.         size_t slen;
  46.         int mib[5];
  47.         struct timeval tv;
  48.         double delta;
  49.  
  50.         mib[0] = CTL_HW;
  51.         mib[1] = HW_SENSORS;
  52.         mib[2] = vmt0;
  53.         mib[3] = SENSOR_TIMEDELTA;
  54.         mib[4] = 0;
  55.         while (1) {
  56.                 slen = sizeof(sensor);
  57.                 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1)
  58.                         err(1, "sysctl");
  59.                 delta = sensor.value / 1000000000.0;
  60.                 if (delta < -30 || delta > 60) {
  61.                         syslog(LOG_DAEMON | LOG_NOTICE,
  62.                             "fixing time! %f\n", delta);
  63.                         if (gettimeofday(&tv, NULL) == -1)
  64.                                 error("gettimeofday");
  65.                         tv.tv_sec -= delta;
  66.                         if (settimeofday(&tv, NULL) == -1)
  67.                                 error("settimeofday");
  68.                 }
  69.                 sleep(15);
  70.         }
  71. }
  72.  
  73. int
  74. main(int argc, char **argv)
  75. {
  76.         int vmt0;
  77.  
  78.         vmt0 = findvmt0();
  79.         if (vmt0 == -1)
  80.                 error("can't find vmt0");
  81.  
  82.         timeloop(vmt0);
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement