syscall_linux_test.go 9.6 KB

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