posix_utils.nim 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. {.deadCodeElim: on.} # dce option deprecated
  12. import posix
  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): (string, File) =
  66. ## Creates a unique temporary file from a prefix string. Adds a six chars suffix.
  67. ## The file is created with perms 0600.
  68. ## Returns the filename and a file opened in r/w mode.
  69. var tmpl = cstring(prefix & "XXXXXX")
  70. let fd = mkstemp(tmpl)
  71. var f: File
  72. if open(f, fd, fmReadWrite):
  73. return ($tmpl, f)
  74. raise newException(OSError, $strerror(errno))
  75. proc mkdtemp*(prefix: string): string =
  76. ## Creates a unique temporary directory from a prefix string. Adds a six chars suffix.
  77. ## The directory is created with permissions 0700. Returns the directory name.
  78. var tmpl = cstring(prefix & "XXXXXX")
  79. if mkdtemp(tmpl) == nil:
  80. raise newException(OSError, $strerror(errno))
  81. return $tmpl