sysconf_openbsd.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright 2018 Tobias Klauser. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package sysconf
  5. import "golang.org/x/sys/unix"
  6. // sysconf implements sysconf(3) as in the OpenBSD 6.3 libc.
  7. func sysconf(name int) (int64, error) {
  8. switch name {
  9. case SC_AIO_LISTIO_MAX,
  10. SC_AIO_MAX,
  11. SC_AIO_PRIO_DELTA_MAX:
  12. return -1, nil
  13. case SC_ARG_MAX:
  14. return sysctl32("kern.argmax"), nil
  15. case SC_ATEXIT_MAX:
  16. return -1, nil
  17. case SC_CHILD_MAX:
  18. var rlim unix.Rlimit
  19. if err := unix.Getrlimit(unix.RLIMIT_NPROC, &rlim); err == nil {
  20. if rlim.Cur != unix.RLIM_INFINITY {
  21. return int64(rlim.Cur), nil
  22. }
  23. }
  24. return -1, nil
  25. case SC_CLK_TCK:
  26. return _CLK_TCK, nil
  27. case SC_DELAYTIMER_MAX:
  28. return -1, nil
  29. case SC_GETGR_R_SIZE_MAX:
  30. return _GR_BUF_LEN, nil
  31. case SC_GETPW_R_SIZE_MAX:
  32. return _PW_BUF_LEN, nil
  33. case SC_IOV_MAX:
  34. return _IOV_MAX, nil
  35. case SC_LOGIN_NAME_MAX:
  36. return _LOGIN_NAME_MAX, nil
  37. case SC_NGROUPS_MAX:
  38. return sysctl32("kern.ngroups"), nil
  39. case SC_OPEN_MAX:
  40. var rlim unix.Rlimit
  41. if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlim); err == nil {
  42. if rlim.Cur != unix.RLIM_INFINITY {
  43. return int64(rlim.Cur), nil
  44. }
  45. }
  46. return -1, nil
  47. case SC_SEM_NSEMS_MAX:
  48. return -1, nil
  49. case SC_SEM_VALUE_MAX:
  50. return _SEM_VALUE_MAX, nil
  51. case SC_SIGQUEUE_MAX:
  52. return -1, nil
  53. case SC_STREAM_MAX:
  54. var rlim unix.Rlimit
  55. if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlim); err == nil {
  56. if rlim.Cur != unix.RLIM_INFINITY {
  57. if rlim.Cur > _SHRT_MAX {
  58. return _SHRT_MAX, nil
  59. }
  60. return int64(rlim.Cur), nil
  61. }
  62. }
  63. return -1, nil
  64. case SC_THREAD_DESTRUCTOR_ITERATIONS:
  65. return _PTHREAD_DESTRUCTOR_ITERATIONS, nil
  66. case SC_THREAD_KEYS_MAX:
  67. return _PTHREAD_KEYS_MAX, nil
  68. case SC_THREAD_STACK_MIN:
  69. return _PTHREAD_STACK_MIN, nil
  70. case SC_THREAD_THREADS_MAX:
  71. return -1, nil
  72. case SC_TIMER_MAX:
  73. return -1, nil
  74. case SC_TTY_NAME_MAX:
  75. return _TTY_NAME_MAX, nil
  76. case SC_TZNAME_MAX:
  77. return _NAME_MAX, nil
  78. case SC_BARRIERS:
  79. return _POSIX_BARRIERS, nil
  80. case SC_FSYNC:
  81. return _POSIX_FSYNC, nil
  82. case SC_IPV6:
  83. if _POSIX_IPV6 == 0 {
  84. fd, err := unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, 0)
  85. if err == nil && fd >= 0 {
  86. unix.Close(fd)
  87. return int64(200112), nil
  88. }
  89. return 0, nil
  90. }
  91. return _POSIX_IPV6, nil
  92. case SC_JOB_CONTROL:
  93. return _POSIX_JOB_CONTROL, nil
  94. case SC_MAPPED_FILES:
  95. return _POSIX_MAPPED_FILES, nil
  96. case SC_MONOTONIC_CLOCK:
  97. return _POSIX_MONOTONIC_CLOCK, nil
  98. case SC_SAVED_IDS:
  99. return _POSIX_SAVED_IDS, nil
  100. case SC_SEMAPHORES:
  101. return _POSIX_SEMAPHORES, nil
  102. case SC_SPAWN:
  103. return _POSIX_SPAWN, nil
  104. case SC_SPIN_LOCKS:
  105. return _POSIX_SPIN_LOCKS, nil
  106. case SC_SPORADIC_SERVER:
  107. return _POSIX_SPORADIC_SERVER, nil
  108. case SC_SYNCHRONIZED_IO:
  109. return _POSIX_SYNCHRONIZED_IO, nil
  110. case SC_THREAD_ATTR_STACKADDR:
  111. return _POSIX_THREAD_ATTR_STACKADDR, nil
  112. case SC_THREAD_ATTR_STACKSIZE:
  113. return _POSIX_THREAD_ATTR_STACKSIZE, nil
  114. case SC_THREAD_CPUTIME:
  115. return _POSIX_THREAD_CPUTIME, nil
  116. case SC_THREAD_PRIO_INHERIT:
  117. return _POSIX_THREAD_PRIO_INHERIT, nil
  118. case SC_THREAD_PRIO_PROTECT:
  119. return _POSIX_THREAD_PRIO_PROTECT, nil
  120. case SC_THREAD_PRIORITY_SCHEDULING:
  121. return _POSIX_THREAD_PRIORITY_SCHEDULING, nil
  122. case SC_THREAD_PROCESS_SHARED:
  123. return _POSIX_THREAD_PROCESS_SHARED, nil
  124. case SC_THREAD_ROBUST_PRIO_INHERIT:
  125. return _POSIX_THREAD_ROBUST_PRIO_INHERIT, nil
  126. case SC_THREAD_ROBUST_PRIO_PROTECT:
  127. return _POSIX_THREAD_ROBUST_PRIO_PROTECT, nil
  128. case SC_THREAD_SAFE_FUNCTIONS:
  129. return _POSIX_THREAD_SAFE_FUNCTIONS, nil
  130. case SC_THREAD_SPORADIC_SERVER:
  131. return _POSIX_THREAD_SPORADIC_SERVER, nil
  132. case SC_THREADS:
  133. return _POSIX_THREADS, nil
  134. case SC_TIMEOUTS:
  135. return _POSIX_TIMEOUTS, nil
  136. case SC_TIMERS:
  137. return _POSIX_TIMERS, nil
  138. case SC_TRACE,
  139. SC_TRACE_EVENT_FILTER,
  140. SC_TRACE_EVENT_NAME_MAX,
  141. SC_TRACE_INHERIT,
  142. SC_TRACE_LOG:
  143. return _POSIX_TRACE, nil
  144. case SC_TYPED_MEMORY_OBJECTS:
  145. return _POSIX_TYPED_MEMORY_OBJECTS, nil
  146. case SC_V7_ILP32_OFF32:
  147. return _POSIX_V7_ILP32_OFF32, nil
  148. case SC_V7_ILP32_OFFBIG:
  149. if _POSIX_V7_ILP32_OFFBIG == 0 {
  150. if unix.SizeofInt*_CHAR_BIT == 32 &&
  151. unix.SizeofLong*_CHAR_BIT == 32 &&
  152. unix.SizeofPtr*_CHAR_BIT == 32 &&
  153. sizeofOffT*_CHAR_BIT >= 64 {
  154. return 1, nil
  155. }
  156. return -1, nil
  157. }
  158. return _POSIX_V7_ILP32_OFFBIG, nil
  159. case SC_V7_LP64_OFF64:
  160. if _POSIX_V7_LP64_OFF64 == 0 {
  161. if unix.SizeofInt*_CHAR_BIT == 32 &&
  162. unix.SizeofLong*_CHAR_BIT == 64 &&
  163. unix.SizeofPtr*_CHAR_BIT == 64 &&
  164. sizeofOffT*_CHAR_BIT == 64 {
  165. return 1, nil
  166. }
  167. return -1, nil
  168. }
  169. return _POSIX_V7_LP64_OFF64, nil
  170. case SC_V7_LPBIG_OFFBIG:
  171. if _POSIX_V7_LPBIG_OFFBIG == 0 {
  172. if unix.SizeofInt*_CHAR_BIT >= 32 &&
  173. unix.SizeofLong*_CHAR_BIT >= 64 &&
  174. unix.SizeofPtr*_CHAR_BIT >= 64 &&
  175. sizeofOffT*_CHAR_BIT >= 64 {
  176. return 1, nil
  177. }
  178. return -1, nil
  179. }
  180. return _POSIX_V7_LPBIG_OFFBIG, nil
  181. case SC_V6_ILP32_OFF32:
  182. return _POSIX_V6_ILP32_OFF32, nil
  183. case SC_V6_ILP32_OFFBIG:
  184. if _POSIX_V6_ILP32_OFFBIG == 0 {
  185. if unix.SizeofInt*_CHAR_BIT == 32 &&
  186. unix.SizeofLong*_CHAR_BIT == 32 &&
  187. unix.SizeofPtr*_CHAR_BIT == 32 &&
  188. sizeofOffT*_CHAR_BIT >= 64 {
  189. return 1, nil
  190. }
  191. return -1, nil
  192. }
  193. return _POSIX_V6_ILP32_OFFBIG, nil
  194. case SC_V6_LP64_OFF64:
  195. if _POSIX_V6_LP64_OFF64 == 0 {
  196. if unix.SizeofInt*_CHAR_BIT == 32 &&
  197. unix.SizeofLong*_CHAR_BIT == 64 &&
  198. unix.SizeofPtr*_CHAR_BIT == 64 &&
  199. sizeofOffT*_CHAR_BIT == 64 {
  200. return 1, nil
  201. }
  202. return -1, nil
  203. }
  204. return _POSIX_V6_LP64_OFF64, nil
  205. case SC_V6_LPBIG_OFFBIG:
  206. if _POSIX_V6_LPBIG_OFFBIG == 0 {
  207. if unix.SizeofInt*_CHAR_BIT >= 32 &&
  208. unix.SizeofLong*_CHAR_BIT >= 64 &&
  209. unix.SizeofPtr*_CHAR_BIT >= 64 &&
  210. sizeofOffT*_CHAR_BIT >= 64 {
  211. return 1, nil
  212. }
  213. return -1, nil
  214. }
  215. return _POSIX_V6_LPBIG_OFFBIG, nil
  216. case SC_2_CHAR_TERM:
  217. return _POSIX2_CHAR_TERM, nil
  218. case SC_2_PBS,
  219. SC_2_PBS_ACCOUNTING,
  220. SC_2_PBS_CHECKPOINT,
  221. SC_2_PBS_LOCATE,
  222. SC_2_PBS_MESSAGE,
  223. SC_2_PBS_TRACK:
  224. return _POSIX2_PBS, nil
  225. case SC_2_UPE:
  226. return _POSIX2_UPE, nil
  227. case SC_2_VERSION:
  228. return _POSIX2_VERSION, nil
  229. case SC_XOPEN_CRYPT:
  230. return _XOPEN_CRYPT, nil
  231. case SC_XOPEN_ENH_I18N:
  232. return _XOPEN_ENH_I18N, nil
  233. case SC_XOPEN_REALTIME:
  234. return _XOPEN_REALTIME, nil
  235. case SC_XOPEN_REALTIME_THREADS:
  236. return _XOPEN_REALTIME_THREADS, nil
  237. case SC_XOPEN_SHM:
  238. return _XOPEN_SHM, nil
  239. case SC_XOPEN_STREAMS:
  240. return _XOPEN_STREAMS, nil
  241. case SC_XOPEN_UNIX:
  242. return _XOPEN_UNIX, nil
  243. case SC_XOPEN_UUCP:
  244. return _XOPEN_UUCP, nil
  245. case SC_AVPHYS_PAGES:
  246. if uvm, err := unix.SysctlUvmexp("vm.uvmexp"); err == nil {
  247. return int64(uvm.Free), nil
  248. }
  249. return -1, nil
  250. case SC_PHYS_PAGES:
  251. return sysctl64("hw.physmem") / int64(unix.Getpagesize()), nil
  252. case SC_NPROCESSORS_CONF:
  253. return sysctl32("hw.ncpu"), nil
  254. case SC_NPROCESSORS_ONLN:
  255. if val, err := unix.SysctlUint32("hw.ncpuonline"); err == nil {
  256. return int64(val), nil
  257. }
  258. return sysctl32("hw.ncpu"), nil
  259. }
  260. return sysconfGeneric(name)
  261. }