syscall_linux_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright 2016 The Go Authors. 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. // +build linux
  5. package unix_test
  6. import (
  7. "os"
  8. "runtime"
  9. "runtime/debug"
  10. "testing"
  11. "time"
  12. "golang.org/x/sys/unix"
  13. )
  14. func TestIoctlGetInt(t *testing.T) {
  15. f, err := os.Open("/dev/random")
  16. if err != nil {
  17. t.Fatalf("failed to open device: %v", err)
  18. }
  19. defer f.Close()
  20. v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
  21. if err != nil {
  22. t.Fatalf("failed to perform ioctl: %v", err)
  23. }
  24. t.Logf("%d bits of entropy available", v)
  25. }
  26. func TestPpoll(t *testing.T) {
  27. f, cleanup := mktmpfifo(t)
  28. defer cleanup()
  29. const timeout = 100 * time.Millisecond
  30. ok := make(chan bool, 1)
  31. go func() {
  32. select {
  33. case <-time.After(10 * timeout):
  34. t.Errorf("Ppoll: failed to timeout after %d", 10*timeout)
  35. case <-ok:
  36. }
  37. }()
  38. fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
  39. timeoutTs := unix.NsecToTimespec(int64(timeout))
  40. n, err := unix.Ppoll(fds, &timeoutTs, nil)
  41. ok <- true
  42. if err != nil {
  43. t.Errorf("Ppoll: unexpected error: %v", err)
  44. return
  45. }
  46. if n != 0 {
  47. t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0)
  48. return
  49. }
  50. }
  51. func TestTime(t *testing.T) {
  52. var ut unix.Time_t
  53. ut2, err := unix.Time(&ut)
  54. if err != nil {
  55. t.Fatalf("Time: %v", err)
  56. }
  57. if ut != ut2 {
  58. t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
  59. }
  60. var now time.Time
  61. for i := 0; i < 10; i++ {
  62. ut, err = unix.Time(nil)
  63. if err != nil {
  64. t.Fatalf("Time: %v", err)
  65. }
  66. now = time.Now()
  67. if int64(ut) == now.Unix() {
  68. return
  69. }
  70. }
  71. t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
  72. }
  73. func TestUtime(t *testing.T) {
  74. defer chtmpdir(t)()
  75. touch(t, "file1")
  76. buf := &unix.Utimbuf{
  77. Modtime: 12345,
  78. }
  79. err := unix.Utime("file1", buf)
  80. if err != nil {
  81. t.Fatalf("Utime: %v", err)
  82. }
  83. fi, err := os.Stat("file1")
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. if fi.ModTime().Unix() != 12345 {
  88. t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
  89. }
  90. }
  91. func TestUtimesNanoAt(t *testing.T) {
  92. defer chtmpdir(t)()
  93. symlink := "symlink1"
  94. os.Remove(symlink)
  95. err := os.Symlink("nonexisting", symlink)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. ts := []unix.Timespec{
  100. {Sec: 1111, Nsec: 2222},
  101. {Sec: 3333, Nsec: 4444},
  102. }
  103. err = unix.UtimesNanoAt(unix.AT_FDCWD, symlink, ts, unix.AT_SYMLINK_NOFOLLOW)
  104. if err != nil {
  105. t.Fatalf("UtimesNanoAt: %v", err)
  106. }
  107. var st unix.Stat_t
  108. err = unix.Lstat(symlink, &st)
  109. if err != nil {
  110. t.Fatalf("Lstat: %v", err)
  111. }
  112. if st.Atim != ts[0] {
  113. t.Errorf("UtimesNanoAt: wrong atime: %v", st.Atim)
  114. }
  115. if st.Mtim != ts[1] {
  116. t.Errorf("UtimesNanoAt: wrong mtime: %v", st.Mtim)
  117. }
  118. }
  119. func TestRlimitAs(t *testing.T) {
  120. // disable GC during to avoid flaky test
  121. defer debug.SetGCPercent(debug.SetGCPercent(-1))
  122. var rlim unix.Rlimit
  123. err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
  124. if err != nil {
  125. t.Fatalf("Getrlimit: %v", err)
  126. }
  127. var zero unix.Rlimit
  128. if zero == rlim {
  129. t.Fatalf("Getrlimit: got zero value %#v", rlim)
  130. }
  131. set := rlim
  132. set.Cur = uint64(unix.Getpagesize())
  133. err = unix.Setrlimit(unix.RLIMIT_AS, &set)
  134. if err != nil {
  135. t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
  136. }
  137. // RLIMIT_AS was set to the page size, so mmap()'ing twice the page size
  138. // should fail. See 'man 2 getrlimit'.
  139. _, err = unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
  140. if err == nil {
  141. t.Fatal("Mmap: unexpectedly suceeded after setting RLIMIT_AS")
  142. }
  143. err = unix.Setrlimit(unix.RLIMIT_AS, &rlim)
  144. if err != nil {
  145. t.Fatalf("Setrlimit: restore failed: %#v %v", rlim, err)
  146. }
  147. b, err := unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
  148. if err != nil {
  149. t.Fatalf("Mmap: %v", err)
  150. }
  151. err = unix.Munmap(b)
  152. if err != nil {
  153. t.Fatalf("Munmap: %v", err)
  154. }
  155. }
  156. func TestSelect(t *testing.T) {
  157. _, err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0})
  158. if err != nil {
  159. t.Fatalf("Select: %v", err)
  160. }
  161. dur := 150 * time.Millisecond
  162. tv := unix.NsecToTimeval(int64(dur))
  163. start := time.Now()
  164. _, err = unix.Select(0, nil, nil, nil, &tv)
  165. took := time.Since(start)
  166. if err != nil {
  167. t.Fatalf("Select: %v", err)
  168. }
  169. if took < dur {
  170. t.Errorf("Select: timeout should have been at least %v, got %v", dur, took)
  171. }
  172. }
  173. func TestPselect(t *testing.T) {
  174. _, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
  175. if err != nil {
  176. t.Fatalf("Pselect: %v", err)
  177. }
  178. dur := 2500 * time.Microsecond
  179. ts := unix.NsecToTimespec(int64(dur))
  180. start := time.Now()
  181. _, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
  182. took := time.Since(start)
  183. if err != nil {
  184. t.Fatalf("Pselect: %v", err)
  185. }
  186. if took < dur {
  187. t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
  188. }
  189. }
  190. func TestSchedSetaffinity(t *testing.T) {
  191. runtime.LockOSThread()
  192. defer runtime.UnlockOSThread()
  193. var oldMask unix.CPUSet
  194. err := unix.SchedGetaffinity(0, &oldMask)
  195. if err != nil {
  196. t.Fatalf("SchedGetaffinity: %v", err)
  197. }
  198. var newMask unix.CPUSet
  199. newMask.Zero()
  200. if newMask.Count() != 0 {
  201. t.Errorf("CpuZero: didn't zero CPU set: %v", newMask)
  202. }
  203. cpu := 1
  204. newMask.Set(cpu)
  205. if newMask.Count() != 1 || !newMask.IsSet(cpu) {
  206. t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
  207. }
  208. cpu = 5
  209. newMask.Set(cpu)
  210. if newMask.Count() != 2 || !newMask.IsSet(cpu) {
  211. t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
  212. }
  213. newMask.Clear(cpu)
  214. if newMask.Count() != 1 || newMask.IsSet(cpu) {
  215. t.Errorf("CpuClr: didn't clear CPU %d in set: %v", cpu, newMask)
  216. }
  217. if runtime.NumCPU() < 2 {
  218. t.Skip("skipping setaffinity tests on single CPU system")
  219. }
  220. err = unix.SchedSetaffinity(0, &newMask)
  221. if err != nil {
  222. t.Fatalf("SchedSetaffinity: %v", err)
  223. }
  224. var gotMask unix.CPUSet
  225. err = unix.SchedGetaffinity(0, &gotMask)
  226. if err != nil {
  227. t.Fatalf("SchedGetaffinity: %v", err)
  228. }
  229. if gotMask != newMask {
  230. t.Errorf("SchedSetaffinity: returned affinity mask does not match set affinity mask")
  231. }
  232. // Restore old mask so it doesn't affect successive tests
  233. err = unix.SchedSetaffinity(0, &oldMask)
  234. if err != nil {
  235. t.Fatalf("SchedSetaffinity: %v", err)
  236. }
  237. }
  238. func TestStatx(t *testing.T) {
  239. var stx unix.Statx_t
  240. err := unix.Statx(unix.AT_FDCWD, ".", 0, 0, &stx)
  241. if err == unix.ENOSYS {
  242. t.Skip("statx syscall is not available, skipping test")
  243. } else if err != nil {
  244. t.Fatalf("Statx: %v", err)
  245. }
  246. defer chtmpdir(t)()
  247. touch(t, "file1")
  248. var st unix.Stat_t
  249. err = unix.Stat("file1", &st)
  250. if err != nil {
  251. t.Fatalf("Stat: %v", err)
  252. }
  253. flags := unix.AT_STATX_SYNC_AS_STAT
  254. err = unix.Statx(unix.AT_FDCWD, "file1", flags, unix.STATX_ALL, &stx)
  255. if err != nil {
  256. t.Fatalf("Statx: %v", err)
  257. }
  258. if uint32(stx.Mode) != st.Mode {
  259. t.Errorf("Statx: returned stat mode does not match Stat")
  260. }
  261. atime := unix.StatxTimestamp{Sec: int64(st.Atim.Sec), Nsec: uint32(st.Atim.Nsec)}
  262. ctime := unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
  263. mtime := unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
  264. if stx.Atime != atime {
  265. t.Errorf("Statx: returned stat atime does not match Stat")
  266. }
  267. if stx.Ctime != ctime {
  268. t.Errorf("Statx: returned stat ctime does not match Stat")
  269. }
  270. if stx.Mtime != mtime {
  271. t.Errorf("Statx: returned stat mtime does not match Stat")
  272. }
  273. err = os.Symlink("file1", "symlink1")
  274. if err != nil {
  275. t.Fatal(err)
  276. }
  277. err = unix.Lstat("symlink1", &st)
  278. if err != nil {
  279. t.Fatalf("Lstat: %v", err)
  280. }
  281. err = unix.Statx(unix.AT_FDCWD, "symlink1", flags, unix.STATX_BASIC_STATS, &stx)
  282. if err != nil {
  283. t.Fatalf("Statx: %v", err)
  284. }
  285. // follow symlink, expect a regulat file
  286. if stx.Mode&unix.S_IFREG == 0 {
  287. t.Errorf("Statx: didn't follow symlink")
  288. }
  289. err = unix.Statx(unix.AT_FDCWD, "symlink1", flags|unix.AT_SYMLINK_NOFOLLOW, unix.STATX_ALL, &stx)
  290. if err != nil {
  291. t.Fatalf("Statx: %v", err)
  292. }
  293. // follow symlink, expect a symlink
  294. if stx.Mode&unix.S_IFLNK == 0 {
  295. t.Errorf("Statx: unexpectedly followed symlink")
  296. }
  297. if uint32(stx.Mode) != st.Mode {
  298. t.Errorf("Statx: returned stat mode does not match Lstat")
  299. }
  300. atime = unix.StatxTimestamp{Sec: int64(st.Atim.Sec), Nsec: uint32(st.Atim.Nsec)}
  301. ctime = unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
  302. mtime = unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
  303. if stx.Atime != atime {
  304. t.Errorf("Statx: returned stat atime does not match Lstat")
  305. }
  306. if stx.Ctime != ctime {
  307. t.Errorf("Statx: returned stat ctime does not match Lstat")
  308. }
  309. if stx.Mtime != mtime {
  310. t.Errorf("Statx: returned stat mtime does not match Lstat")
  311. }
  312. }
  313. // stringsFromByteSlice converts a sequence of attributes to a []string.
  314. // On Linux, each entry is a NULL-terminated string.
  315. func stringsFromByteSlice(buf []byte) []string {
  316. var result []string
  317. off := 0
  318. for i, b := range buf {
  319. if b == 0 {
  320. result = append(result, string(buf[off:i]))
  321. off = i + 1
  322. }
  323. }
  324. return result
  325. }