syscall_linux_test.go 11 KB

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