kernel.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright 2007, 2008 by Grégoire Henry, Julien Cristau and Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <sys/utsname.h>
  20. #include <sys/time.h>
  21. #include <sys/param.h>
  22. #include <time.h>
  23. #include "babeld.h"
  24. #ifdef __linux
  25. #include "kernel_netlink.c"
  26. #else
  27. #include "kernel_socket.c"
  28. #endif
  29. /* Like gettimeofday, but returns monotonic time. If POSIX clocks are not
  30. available, falls back to gettimeofday but enforces monotonicity. */
  31. int
  32. gettime(struct timeval *tv)
  33. {
  34. int rc;
  35. static time_t offset = 0, previous = 0;
  36. #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC)
  37. static int have_posix_clocks = -1;
  38. if(UNLIKELY(have_posix_clocks < 0)) {
  39. struct timespec ts;
  40. rc = clock_gettime(CLOCK_MONOTONIC, &ts);
  41. if(rc < 0) {
  42. have_posix_clocks = 0;
  43. } else {
  44. have_posix_clocks = 1;
  45. }
  46. }
  47. if(have_posix_clocks) {
  48. struct timespec ts;
  49. int rc;
  50. rc = clock_gettime(CLOCK_MONOTONIC, &ts);
  51. if(rc < 0)
  52. return rc;
  53. tv->tv_sec = ts.tv_sec;
  54. tv->tv_usec = ts.tv_nsec / 1000;
  55. return rc;
  56. }
  57. #endif
  58. rc = gettimeofday(tv, NULL);
  59. if(rc < 0)
  60. return rc;
  61. tv->tv_sec += offset;
  62. if(UNLIKELY(previous > tv->tv_sec)) {
  63. offset += previous - tv->tv_sec;
  64. tv->tv_sec = previous;
  65. }
  66. previous = tv->tv_sec;
  67. return rc;
  68. }
  69. /* If /dev/urandom doesn't exist, this will fail with ENOENT, which the
  70. caller will deal with gracefully. */
  71. int
  72. read_random_bytes(void *buf, int len)
  73. {
  74. int fd, rc;
  75. fd = open("/dev/urandom", O_RDONLY);
  76. if(fd < 0) {
  77. errno = ENOSYS;
  78. return -1;
  79. }
  80. rc = read(fd, buf, len);
  81. if(rc < len)
  82. rc = -1;
  83. close(fd);
  84. return rc;
  85. }
  86. int
  87. add_import_table(int table)
  88. {
  89. if(table < 0 || table > 0xFFFF) return -1;
  90. if(import_table_count > MAX_IMPORT_TABLES - 1) return -2;
  91. import_tables[import_table_count++] = table;
  92. return 0;
  93. }
  94. int
  95. kernel_older_than(const char *sysname, int version, int sub_version)
  96. {
  97. struct utsname un;
  98. int rc;
  99. int v = 0;
  100. int sub_v = 0;
  101. rc = uname(&un);
  102. if(rc < 0)
  103. return -1;
  104. if(strcmp(sysname, un.sysname) != 0)
  105. return -1;
  106. rc = sscanf(un.release, "%d.%d", &v, &sub_v);
  107. if(rc < 2)
  108. return -1;
  109. return (v < version || (v == version && sub_v < sub_version));
  110. }