platform_linux.odin 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // +build linux
  2. package sysinfo
  3. import "core:intrinsics"
  4. import "core:runtime"
  5. import "core:strings"
  6. import "core:strconv"
  7. import "core:sys/linux"
  8. @(private)
  9. version_string_buf: [1024]u8
  10. @(init, private)
  11. init_os_version :: proc () {
  12. os_version.platform = .Linux
  13. // Try to parse `/etc/os-release` for `PRETTY_NAME="Ubuntu 20.04.3 LTS`
  14. fd, errno := linux.open("/etc/os-release", {.RDONLY}, {})
  15. assert(errno == .NONE, "Failed to read /etc/os-release")
  16. defer {
  17. errno := linux.close(fd)
  18. assert(errno == .NONE, "Failed to close the file descriptor")
  19. }
  20. os_release_buf: [2048]u8
  21. n, read_errno := linux.read(fd, os_release_buf[:])
  22. assert(read_errno == .NONE, "Failed to read data from /etc/os-release")
  23. release := string(os_release_buf[:n])
  24. // Search the line in the file until we find "PRETTY_NAME="
  25. NEEDLE :: "PRETTY_NAME=\""
  26. pretty_start := strings.index(release, NEEDLE)
  27. b := strings.builder_from_bytes(version_string_buf[:])
  28. if pretty_start > 0 {
  29. for r, i in release[pretty_start + len(NEEDLE):] {
  30. if r == '"' {
  31. strings.write_string(&b, release[pretty_start + len(NEEDLE):][:i])
  32. break
  33. } else if r == '\r' || r == '\n' {
  34. strings.write_string(&b, "Unknown Linux Distro")
  35. break
  36. }
  37. }
  38. }
  39. // Grab kernel info using `uname()` syscall, https://linux.die.net/man/2/uname
  40. uts: linux.UTS_Name
  41. uname_errno := linux.uname(&uts)
  42. assert(uname_errno == .NONE, "This should never happen!")
  43. // Append the system name (typically "Linux") and kernel release (looks like 6.5.2-arch1-1)
  44. strings.write_string(&b, ", ")
  45. strings.write_string(&b, string(cstring(&uts.sysname[0])))
  46. strings.write_rune(&b, ' ')
  47. l := strings.builder_len(b)
  48. strings.write_string(&b, string(cstring(&uts.release[0])))
  49. // Parse kernel version, as substrings of the version info in `version_string_buf`
  50. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  51. version_bits := strings.split_n(strings.to_string(b)[l:], "-", 2, context.temp_allocator)
  52. if len(version_bits) > 1 {
  53. os_version.version = version_bits[1]
  54. }
  55. // Parse major, minor, patch from release info
  56. triplet := strings.split(version_bits[0], ".", context.temp_allocator)
  57. if len(triplet) == 3 {
  58. major, major_ok := strconv.parse_int(triplet[0])
  59. minor, minor_ok := strconv.parse_int(triplet[1])
  60. patch, patch_ok := strconv.parse_int(triplet[2])
  61. if major_ok && minor_ok && patch_ok {
  62. os_version.major = major
  63. os_version.minor = minor
  64. os_version.patch = patch
  65. }
  66. }
  67. // Finish the string
  68. os_version.as_string = strings.to_string(b)
  69. }
  70. @(init)
  71. init_ram :: proc() {
  72. // Retrieve RAM info using `sysinfo`
  73. sys_info: linux.Sys_Info
  74. errno := linux.sysinfo(&sys_info)
  75. assert(errno == .NONE, "Good luck to whoever's debugging this, something's seriously cucked up!")
  76. ram = RAM{
  77. total_ram = int(sys_info.totalram) * int(sys_info.mem_unit),
  78. free_ram = int(sys_info.freeram) * int(sys_info.mem_unit),
  79. total_swap = int(sys_info.totalswap) * int(sys_info.mem_unit),
  80. free_swap = int(sys_info.freeswap) * int(sys_info.mem_unit),
  81. }
  82. }