posix_utils.nim 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. ## ```nim
  49. ## memoryLockAll(MCL_CURRENT or MCL_FUTURE)
  50. ## ```
  51. if mlockall(flags.cint) != 0:
  52. raise newException(OSError, $strerror(errno))
  53. proc memoryUnlock*(a1: pointer, a2: int) =
  54. ## Unlock pages starting from a1 for a1 bytes and allow them to be swapped.
  55. if munlock(a1, a2) != 0:
  56. raise newException(OSError, $strerror(errno))
  57. proc memoryUnlockAll*() =
  58. ## Unlocks all memory for the running process to allow swapping.
  59. if munlockall() != 0:
  60. raise newException(OSError, $strerror(errno))
  61. proc sendSignal*(pid: Pid, signal: int) =
  62. ## Sends a signal to a running process by calling `kill`.
  63. ## Raise exception in case of failure e.g. process not running.
  64. if kill(pid, signal.cint) != 0:
  65. raise newException(OSError, $strerror(errno))
  66. proc mkstemp*(prefix: string, suffix=""): (string, File) =
  67. ## Creates a unique temporary file from a prefix string. A six-character string
  68. ## will be added. If suffix is provided it will be added to the string
  69. ## The file is created with perms 0600.
  70. ## Returns the filename and a file opened in r/w mode.
  71. var tmpl = cstring(prefix & "XXXXXX" & suffix)
  72. let fd =
  73. if len(suffix)==0:
  74. when declared(mkostemp):
  75. mkostemp(tmpl, O_CLOEXEC)
  76. else:
  77. mkstemp(tmpl)
  78. else:
  79. when declared(mkostemps):
  80. mkostemps(tmpl, cint(len(suffix)), O_CLOEXEC)
  81. else:
  82. mkstemps(tmpl, cint(len(suffix)))
  83. var f: File
  84. if open(f, fd, fmReadWrite):
  85. return ($tmpl, f)
  86. raise newException(OSError, $strerror(errno))
  87. proc mkdtemp*(prefix: string): string =
  88. ## Creates a unique temporary directory from a prefix string. Adds a six chars suffix.
  89. ## The directory is created with permissions 0700. Returns the directory name.
  90. var tmpl = cstring(prefix & "XXXXXX")
  91. if mkdtemp(tmpl) == nil:
  92. raise newException(OSError, $strerror(errno))
  93. return $tmpl
  94. proc osReleaseFile*(): Config {.since: (1, 5).} =
  95. ## Gets system identification from `os-release` file and returns it as a `parsecfg.Config`.
  96. ## You also need to import the `parsecfg` module to gain access to this object.
  97. ## The `os-release` file is an official Freedesktop.org open standard.
  98. ## Available in Linux and BSD distributions, except Android and Android-based Linux.
  99. ## `os-release` file is not available on Windows and OS X by design.
  100. ## * https://www.freedesktop.org/software/systemd/man/os-release.html
  101. runnableExamples:
  102. import std/parsecfg
  103. when defined(linux):
  104. let data = osReleaseFile()
  105. echo "OS name: ", data.getSectionValue("", "NAME") ## the data is up to each distro.
  106. # We do not use a {.strdefine.} because Standard says it *must* be that path.
  107. for osReleaseFile in ["/etc/os-release", "/usr/lib/os-release"]:
  108. if fileExists(osReleaseFile):
  109. return loadConfig(osReleaseFile)
  110. raise newException(IOError, "File not found: /etc/os-release, /usr/lib/os-release")