linux_version.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "linux_version.h"
  2. #include <util/generic/yexception.h>
  3. #include <util/system/platform.h>
  4. #ifdef _linux_
  5. # include <sys/utsname.h>
  6. #endif
  7. namespace NYql {
  8. std::tuple<int, int, int> DetectLinuxKernelVersion3() {
  9. #ifdef _linux_
  10. // see https://github.com/torvalds/linux/blob/master/Makefile
  11. // version is composed as follows:
  12. // VERSION = 4
  13. // PATCHLEVEL = 18
  14. // SUBLEVEL = 0
  15. // EXTRAVERSION = -rc4
  16. // KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
  17. utsname buf = {};
  18. if (uname(&buf)) {
  19. ythrow TSystemError() << "uname call failed";
  20. }
  21. int v = 0;
  22. int p = 0;
  23. int s = 0;
  24. if (sscanf(buf.release, "%d.%d.%d", &v, &p, &s) != 3) {
  25. ythrow yexception() << "Failed to parse linux kernel version " << buf.release;
  26. }
  27. return std::make_tuple(v, p, s);
  28. #else
  29. return {};
  30. #endif
  31. }
  32. std::pair<int, int> DetectLinuxKernelVersion2() {
  33. auto v = DetectLinuxKernelVersion3();
  34. return std::make_pair(std::get<0>(v), std::get<1>(v));
  35. }
  36. bool IsLinuxKernelBelow4_3() {
  37. return DetectLinuxKernelVersion2() < std::make_pair(4, 3);
  38. }
  39. }