pastebin - collaborative debugging tool
rovema.kpaste.net RSS


linux_test_for_realtime_kernel1.c - simple test whether the current Linux kernel is a realtime kernel
Posted by Anonymous on Mon 6th Mar 2023 13:04
raw | new post

  1. /*
  2.  * linux_test_for_realtime_kernel1.c - simple test whether the
  3.  * current Linux kernel is a realtime kernel
  4.  *
  5.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <string.h>
  15.  
  16. /*
  17.  * EINTR_REPEAT - make sure we repeat aborted syscalls since
  18.  * we cannot rely on |SA_RESTART| in library code
  19.  */
  20. #define EINTR_REPEAT(expr) \
  21.         { while((expr) && (errno == EINTR)) errno=0; };
  22.  
  23. int linux_is_realtime_kernel(void)
  24. {
  25.         int     fd;
  26.         /*
  27.          * Lazy buffer sizing. Instead of |fstat()| on fd or
  28.          * loop on |read()| we just assume that the contents
  29.          * of "/proc/sys/kernel/version" have always a size
  30.          * less than 1k.
  31.          */
  32.         char    buff[1024];
  33.         ssize_t s;
  34.         int     res = 0;
  35.  
  36.         EINTR_REPEAT(fd = open("/proc/sys/kernel/version",
  37.                 O_RDONLY|O_CLOEXEC));
  38.         if (fd == -1) {
  39.                 perror("Cannot open /proc/sys/kernel/version");
  40.                 return -1;
  41.         }
  42.  
  43.         EINTR_REPEAT(s = read(fd, buff, sizeof(buff)-1));
  44.  
  45.         if (s > 0) {
  46.                 buff[s] = '\0';
  47.                 if (strstr(buff, "PREEMPT_RT"))
  48.                         res = 1;
  49.         }
  50.  
  51.         (void)close(fd);
  52.  
  53.         return res;
  54. }
  55.  
  56. int main(int ac, char *av[])
  57. {
  58.         int x;
  59.  
  60.         (void)puts("#start.");
  61.  
  62.         x = linux_is_realtime_kernel();
  63.         (void)printf("kernel a realtime-kernel = %d\n", x);
  64.        
  65.         (void)puts("#done.");
  66.        
  67.         return EXIT_SUCCESS;
  68. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at