posix_utils.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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
  12. type Uname* = object
  13. sysname*, nodename*, release*, version*, machine*: string
  14. template charArrayToString(input: typed): string =
  15. $cstring(addr input)
  16. proc uname*(): Uname =
  17. ## Provides system information in a `Uname` struct with sysname, nodename,
  18. ## release, version and machine attributes.
  19. when defined(posix):
  20. runnableExamples:
  21. echo uname().nodename, uname().release, uname().version
  22. doAssert uname().sysname.len != 0
  23. var u: Utsname
  24. if uname(u) != 0:
  25. raise newException(OSError, $strerror(errno))
  26. result.sysname = charArrayToString u.sysname
  27. result.nodename = charArrayToString u.nodename
  28. result.release = charArrayToString u.release
  29. result.version = charArrayToString u.version
  30. result.machine = charArrayToString u.machine
  31. proc fsync*(fd: int) =
  32. ## synchronize a file's buffer cache to the storage device
  33. if fsync(fd.cint) != 0:
  34. raise newException(OSError, $strerror(errno))
  35. proc stat*(path: string): Stat =
  36. ## Returns file status in a `Stat` structure
  37. if stat(path.cstring, result) != 0:
  38. raise newException(OSError, $strerror(errno))
  39. proc memoryLock*(a1: pointer, a2: int) =
  40. ## Locks pages starting from a1 for a1 bytes and prevent them from being swapped.
  41. if mlock(a1, a2) != 0:
  42. raise newException(OSError, $strerror(errno))
  43. proc memoryLockAll*(flags: int) =
  44. ## Locks all memory for the running process to prevent swapping.
  45. ##
  46. ## example::
  47. ##
  48. ## memoryLockAll(MCL_CURRENT or MCL_FUTURE)
  49. if mlockall(flags.cint) != 0:
  50. raise newException(OSError, $strerror(errno))
  51. proc memoryUnlock*(a1: pointer, a2: int) =
  52. ## Unlock pages starting from a1 for a1 bytes and allow them to be swapped.
  53. if munlock(a1, a2) != 0:
  54. raise newException(OSError, $strerror(errno))
  55. proc memoryUnlockAll*() =
  56. ## Unlocks all memory for the running process to allow swapping.
  57. if munlockall() != 0:
  58. raise newException(OSError, $strerror(errno))
  59. proc sendSignal*(pid: Pid, signal: int) =
  60. ## Sends a signal to a running process by calling `kill`.
  61. ## Raise exception in case of failure e.g. process not running.
  62. if kill(pid, signal.cint) != 0:
  63. raise newException(OSError, $strerror(errno))
  64. proc mkstemp*(prefix: string, suffix=""): (string, File) =
  65. ## Creates a unique temporary file from a prefix string. A six-character string
  66. ## will be added. If suffix is provided it will be added to the string
  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" & suffix)
  70. let fd =
  71. if len(suffix)==0:
  72. when declared(mkostemp):
  73. mkostemp(tmpl, O_CLOEXEC)
  74. else:
  75. mkstemp(tmpl)
  76. else:
  77. when declared(mkostemps):
  78. mkostemps(tmpl, cint(len(suffix)), O_CLOEXEC)
  79. else:
  80. mkstemps(tmpl, cint(len(suffix)))
  81. var f: File
  82. if open(f, fd, fmReadWrite):
  83. return ($tmpl, f)
  84. raise newException(OSError, $strerror(errno))
  85. proc mkdtemp*(prefix: string): string =
  86. ## Creates a unique temporary directory from a prefix string. Adds a six chars suffix.
  87. ## The directory is created with permissions 0700. Returns the directory name.
  88. var tmpl = cstring(prefix & "XXXXXX")
  89. if mkdtemp(tmpl) == nil:
  90. raise newException(OSError, $strerror(errno))
  91. return $tmpl