posix_utils.nim 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #
  2. # Nim's Runtime Library
  3. # (c) Copyright 2019 Federico Ceratto and other Nim contributors
  4. #
  5. # See the file "copying.txt", included in this
  6. # distribution, for details about the copyright.
  7. #
  8. ## A set of helpers for the POSIX module.
  9. ## Raw interfaces are in the other ``posix*.nim`` files.
  10. # Where possible, contribute OS-independent procs in `os <os.html>`_ instead.
  11. import posix, parsecfg, os
  12. import std/private/since
  13. type Uname* = object
  14. sysname*, nodename*, release*, version*, machine*: string
  15. template charArrayToString(input: typed): string =
  16. $cstring(addr input)
  17. proc uname*(): Uname =
  18. ## Provides system information in a `Uname` struct with sysname, nodename,
  19. ## release, version and machine attributes.
  20. when defined(posix):
  21. runnableExamples:
  22. echo uname().nodename, uname().release, uname().version
  23. doAssert uname().sysname.len != 0
  24. var u: Utsname
  25. if uname(u) != 0:
  26. raise newException(OSError, $strerror(errno))
  27. result.sysname = charArrayToString u.sysname
  28. result.nodename = charArrayToString u.nodename
  29. result.release = charArrayToString u.release
  30. result.version = charArrayToString u.version
  31. result.machine = charArrayToString u.machine
  32. proc fsync*(fd: int) =
  33. ## synchronize a file's buffer cache to the storage device
  34. if fsync(fd.cint) != 0:
  35. raise newException(OSError, $strerror(errno))
  36. proc stat*(path: string): Stat =
  37. ## Returns file status in a `Stat` structure
  38. if stat(path.cstring, result) != 0:
  39. raise newException(OSError, $strerror(errno))
  40. proc memoryLock*(a1: pointer, a2: int) =
  41. ## Locks pages starting from a1 for a1 bytes and prevent them from being swapped.
  42. if mlock(a1, a2) != 0:
  43. raise newException(OSError, $strerror(errno))
  44. proc memoryLockAll*(flags: int) =
  45. ## Locks all memory for the running process to prevent swapping.
  46. ##
  47. ## example::
  48. ##
  49. ## memoryLockAll(MCL_CURRENT or MCL_FUTURE)
  50. if mlockall(flags.cint) != 0:
  51. raise newException(OSError, $strerror(errno))
  52. proc memoryUnlock*(a1: pointer, a2: int) =
  53. ## Unlock pages starting from a1 for a1 bytes and allow them to be swapped.
  54. if munlock(a1, a2) != 0:
  55. raise newException(OSError, $strerror(errno))
  56. proc memoryUnlockAll*() =
  57. ## Unlocks all memory for the running process to allow swapping.
  58. if munlockall() != 0:
  59. raise newException(OSError, $strerror(errno))
  60. proc sendSignal*(pid: Pid, signal: int) =
  61. ## Sends a signal to a running process by calling `kill`.
  62. ## Raise exception in case of failure e.g. process not running.
  63. if kill(pid, signal.cint) != 0:
  64. raise newException(OSError, $strerror(errno))
  65. proc mkstemp*(prefix: string, suffix=""): (string, File) =
  66. ## Creates a unique temporary file from a prefix string. A six-character string
  67. ## will be added. If suffix is provided it will be added to the string
  68. ## The file is created with perms 0600.
  69. ## Returns the filename and a file opened in r/w mode.
  70. var tmpl = cstring(prefix & "XXXXXX" & suffix)
  71. let fd =
  72. if len(suffix)==0:
  73. when declared(mkostemp):
  74. mkostemp(tmpl, O_CLOEXEC)
  75. else:
  76. mkstemp(tmpl)
  77. else:
  78. when declared(mkostemps):
  79. mkostemps(tmpl, cint(len(suffix)), O_CLOEXEC)
  80. else:
  81. mkstemps(tmpl, cint(len(suffix)))
  82. var f: File
  83. if open(f, fd, fmReadWrite):
  84. return ($tmpl, f)
  85. raise newException(OSError, $strerror(errno))
  86. proc mkdtemp*(prefix: string): string =
  87. ## Creates a unique temporary directory from a prefix string. Adds a six chars suffix.
  88. ## The directory is created with permissions 0700. Returns the directory name.
  89. var tmpl = cstring(prefix & "XXXXXX")
  90. if mkdtemp(tmpl) == nil:
  91. raise newException(OSError, $strerror(errno))
  92. return $tmpl
  93. proc osReleaseFile*(): Config {.since: (1, 5).} =
  94. ## Gets system identification from `os-release` file and returns it as a `parsecfg.Config`.
  95. ## You also need to import the `parsecfg` module to gain access to this object.
  96. ## The `os-release` file is an official Freedesktop.org open standard.
  97. ## Available in Linux and BSD distributions, except Android and Android-based Linux.
  98. ## `os-release` file is not available on Windows and OS X by design.
  99. ## * https://www.freedesktop.org/software/systemd/man/os-release.html
  100. runnableExamples:
  101. import std/parsecfg
  102. when defined(linux):
  103. let data = osReleaseFile()
  104. echo "OS name: ", data.getSectionValue("", "NAME") ## the data is up to each distro.
  105. # We do not use a {.strdefine.} because Standard says it *must* be that path.
  106. for osReleaseFile in ["/etc/os-release", "/usr/lib/os-release"]:
  107. if fileExists(osReleaseFile):
  108. return loadConfig(osReleaseFile)
  109. raise newException(IOError, "File not found: /etc/os-release, /usr/lib/os-release")