tposix.nim 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. disabled: windows
  4. """
  5. # Test Posix interface
  6. when not defined(windows):
  7. import posix
  8. import std/[assertions, syncio]
  9. var
  10. u: Utsname
  11. discard uname(u)
  12. writeLine(stdout, u.sysname)
  13. writeLine(stdout, u.nodename)
  14. writeLine(stdout, u.release)
  15. writeLine(stdout, u.machine)
  16. when not (defined(nintendoswitch) or defined(macos) or defined(macosx)):
  17. block:
  18. type Message = object
  19. value: int
  20. const MQ_PATH: cstring = "/top_level_file"
  21. const MQ_PRIORITY: cuint = 170
  22. const MQ_MESSAGE_SIZE: csize_t = csize_t(sizeof(Message))
  23. let mqd_a: posix.MqAttr = MqAttr(mq_maxmsg: 10, mq_msgsize: clong(MQ_MESSAGE_SIZE))
  24. let writable: posix.Mqd = posix.mq_open(
  25. MQ_PATH,
  26. posix.O_CREAT or posix.O_WRONLY or posix.O_NONBLOCK,
  27. posix.S_IRWXU,
  28. addr(mqd_a)
  29. )
  30. let readable: posix.Mqd = posix.mq_open(
  31. MQ_PATH,
  32. posix.O_RDONLY or posix.O_NONBLOCK,
  33. posix.S_IRWXU,
  34. addr(mqd_a)
  35. )
  36. let sent: Message = Message(value: 88)
  37. block:
  38. let success: int = writable.mq_send(
  39. cast[cstring](sent.addr),
  40. MQ_MESSAGE_SIZE,
  41. MQ_PRIORITY
  42. )
  43. doAssert success == 0, $success
  44. block:
  45. var buffer: Message
  46. var priority: cuint
  47. let bytesRead: int = readable.mq_receive(
  48. cast[cstring](buffer.addr),
  49. MQ_MESSAGE_SIZE,
  50. priority
  51. )
  52. doAssert buffer == sent
  53. doAssert bytesRead == int(MQ_MESSAGE_SIZE)
  54. block:
  55. var rl: RLimit
  56. var res = getrlimit(RLIMIT_STACK, rl)
  57. doAssert res == 0
  58. # save old value
  59. let oldrlim = rl.rlim_cur
  60. # set new value
  61. rl.rlim_cur = rl.rlim_max - 1
  62. res = setrlimit(RLIMIT_STACK, rl)
  63. doAssert res == 0
  64. # get new value
  65. var rl1: RLimit
  66. res = getrlimit(RLIMIT_STACK, rl1)
  67. doAssert res == 0
  68. doAssert rl1.rlim_cur == rl.rlim_max - 1
  69. # restore old value
  70. rl.rlim_cur = oldrlim
  71. res = setrlimit(RLIMIT_STACK, rl)
  72. doAssert res == 0