syscall_linux_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 succeeded 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. // On a system like ppc64x where some cores can be disabled using ppc64_cpu,
  232. // setaffinity should only be called with enabled cores. The valid cores
  233. // are found from the oldMask, but if none are found then the setaffinity
  234. // tests are skipped. Issue #27875.
  235. if !oldMask.IsSet(cpu) {
  236. newMask.Zero()
  237. for i := 0; i < len(oldMask); i++ {
  238. if oldMask.IsSet(i) {
  239. newMask.Set(i)
  240. break
  241. }
  242. }
  243. if newMask.Count() == 0 {
  244. t.Skip("skipping setaffinity tests if CPU not available")
  245. }
  246. }
  247. err = unix.SchedSetaffinity(0, &newMask)
  248. if err != nil {
  249. t.Fatalf("SchedSetaffinity: %v", err)
  250. }
  251. var gotMask unix.CPUSet
  252. err = unix.SchedGetaffinity(0, &gotMask)
  253. if err != nil {
  254. t.Fatalf("SchedGetaffinity: %v", err)
  255. }
  256. if gotMask != newMask {
  257. t.Errorf("SchedSetaffinity: returned affinity mask does not match set affinity mask")
  258. }
  259. // Restore old mask so it doesn't affect successive tests
  260. err = unix.SchedSetaffinity(0, &oldMask)
  261. if err != nil {
  262. t.Fatalf("SchedSetaffinity: %v", err)
  263. }
  264. }
  265. func TestStatx(t *testing.T) {
  266. var stx unix.Statx_t
  267. err := unix.Statx(unix.AT_FDCWD, ".", 0, 0, &stx)
  268. if err == unix.ENOSYS || err == unix.EPERM {
  269. t.Skip("statx syscall is not available, skipping test")
  270. } else if err != nil {
  271. t.Fatalf("Statx: %v", err)
  272. }
  273. defer chtmpdir(t)()
  274. touch(t, "file1")
  275. var st unix.Stat_t
  276. err = unix.Stat("file1", &st)
  277. if err != nil {
  278. t.Fatalf("Stat: %v", err)
  279. }
  280. flags := unix.AT_STATX_SYNC_AS_STAT
  281. err = unix.Statx(unix.AT_FDCWD, "file1", flags, unix.STATX_ALL, &stx)
  282. if err != nil {
  283. t.Fatalf("Statx: %v", err)
  284. }
  285. if uint32(stx.Mode) != st.Mode {
  286. t.Errorf("Statx: returned stat mode does not match Stat")
  287. }
  288. ctime := unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
  289. mtime := unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
  290. if stx.Ctime != ctime {
  291. t.Errorf("Statx: returned stat ctime does not match Stat")
  292. }
  293. if stx.Mtime != mtime {
  294. t.Errorf("Statx: returned stat mtime does not match Stat")
  295. }
  296. err = os.Symlink("file1", "symlink1")
  297. if err != nil {
  298. t.Fatal(err)
  299. }
  300. err = unix.Lstat("symlink1", &st)
  301. if err != nil {
  302. t.Fatalf("Lstat: %v", err)
  303. }
  304. err = unix.Statx(unix.AT_FDCWD, "symlink1", flags, unix.STATX_BASIC_STATS, &stx)
  305. if err != nil {
  306. t.Fatalf("Statx: %v", err)
  307. }
  308. // follow symlink, expect a regulat file
  309. if stx.Mode&unix.S_IFREG == 0 {
  310. t.Errorf("Statx: didn't follow symlink")
  311. }
  312. err = unix.Statx(unix.AT_FDCWD, "symlink1", flags|unix.AT_SYMLINK_NOFOLLOW, unix.STATX_ALL, &stx)
  313. if err != nil {
  314. t.Fatalf("Statx: %v", err)
  315. }
  316. // follow symlink, expect a symlink
  317. if stx.Mode&unix.S_IFLNK == 0 {
  318. t.Errorf("Statx: unexpectedly followed symlink")
  319. }
  320. if uint32(stx.Mode) != st.Mode {
  321. t.Errorf("Statx: returned stat mode does not match Lstat")
  322. }
  323. ctime = unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
  324. mtime = unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
  325. if stx.Ctime != ctime {
  326. t.Errorf("Statx: returned stat ctime does not match Lstat")
  327. }
  328. if stx.Mtime != mtime {
  329. t.Errorf("Statx: returned stat mtime does not match Lstat")
  330. }
  331. }
  332. // stringsFromByteSlice converts a sequence of attributes to a []string.
  333. // On Linux, each entry is a NULL-terminated string.
  334. func stringsFromByteSlice(buf []byte) []string {
  335. var result []string
  336. off := 0
  337. for i, b := range buf {
  338. if b == 0 {
  339. result = append(result, string(buf[off:i]))
  340. off = i + 1
  341. }
  342. }
  343. return result
  344. }
  345. func TestFaccessat(t *testing.T) {
  346. defer chtmpdir(t)()
  347. touch(t, "file1")
  348. err := unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, 0)
  349. if err != nil {
  350. t.Errorf("Faccessat: unexpected error: %v", err)
  351. }
  352. err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, 2)
  353. if err != unix.EINVAL {
  354. t.Errorf("Faccessat: unexpected error: %v, want EINVAL", err)
  355. }
  356. err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, unix.AT_EACCESS)
  357. if err != nil {
  358. t.Errorf("Faccessat: unexpected error: %v", err)
  359. }
  360. err = os.Symlink("file1", "symlink1")
  361. if err != nil {
  362. t.Fatal(err)
  363. }
  364. err = unix.Faccessat(unix.AT_FDCWD, "symlink1", unix.R_OK, unix.AT_SYMLINK_NOFOLLOW)
  365. if err != nil {
  366. t.Errorf("Faccessat SYMLINK_NOFOLLOW: unexpected error %v", err)
  367. }
  368. // We can't really test AT_SYMLINK_NOFOLLOW, because there
  369. // doesn't seem to be any way to change the mode of a symlink.
  370. // We don't test AT_EACCESS because such tests are only
  371. // meaningful if run as root.
  372. err = unix.Fchmodat(unix.AT_FDCWD, "file1", 0, 0)
  373. if err != nil {
  374. t.Errorf("Fchmodat: unexpected error %v", err)
  375. }
  376. err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.F_OK, unix.AT_SYMLINK_NOFOLLOW)
  377. if err != nil {
  378. t.Errorf("Faccessat: unexpected error: %v", err)
  379. }
  380. err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, unix.AT_SYMLINK_NOFOLLOW)
  381. if err != unix.EACCES {
  382. if unix.Getuid() != 0 {
  383. t.Errorf("Faccessat: unexpected error: %v, want EACCES", err)
  384. }
  385. }
  386. }
  387. func TestSyncFileRange(t *testing.T) {
  388. file, err := ioutil.TempFile("", "TestSyncFileRange")
  389. if err != nil {
  390. t.Fatal(err)
  391. }
  392. defer os.Remove(file.Name())
  393. defer file.Close()
  394. err = unix.SyncFileRange(int(file.Fd()), 0, 0, 0)
  395. if err == unix.ENOSYS || err == unix.EPERM {
  396. t.Skip("sync_file_range syscall is not available, skipping test")
  397. } else if err != nil {
  398. t.Fatalf("SyncFileRange: %v", err)
  399. }
  400. // invalid flags
  401. flags := 0xf00
  402. err = unix.SyncFileRange(int(file.Fd()), 0, 0, flags)
  403. if err != unix.EINVAL {
  404. t.Fatalf("SyncFileRange: unexpected error: %v, want EINVAL", err)
  405. }
  406. }
  407. func TestClockNanosleep(t *testing.T) {
  408. delay := 100 * time.Millisecond
  409. // Relative timespec.
  410. start := time.Now()
  411. rel := unix.NsecToTimespec(delay.Nanoseconds())
  412. err := unix.ClockNanosleep(unix.CLOCK_MONOTONIC, 0, &rel, nil)
  413. if err == unix.ENOSYS || err == unix.EPERM {
  414. t.Skip("clock_nanosleep syscall is not available, skipping test")
  415. } else if err != nil {
  416. t.Errorf("ClockNanosleep(CLOCK_MONOTONIC, 0, %#v, nil) = %v", &rel, err)
  417. } else if slept := time.Now().Sub(start); slept < delay {
  418. t.Errorf("ClockNanosleep(CLOCK_MONOTONIC, 0, %#v, nil) slept only %v", &rel, slept)
  419. }
  420. // Absolute timespec.
  421. start = time.Now()
  422. until := start.Add(delay)
  423. abs := unix.NsecToTimespec(until.UnixNano())
  424. err = unix.ClockNanosleep(unix.CLOCK_REALTIME, unix.TIMER_ABSTIME, &abs, nil)
  425. if err != nil {
  426. t.Errorf("ClockNanosleep(CLOCK_REALTIME, TIMER_ABSTIME, %#v (=%v), nil) = %v", &abs, until, err)
  427. } else if slept := time.Now().Sub(start); slept < delay {
  428. t.Errorf("ClockNanosleep(CLOCK_REALTIME, TIMER_ABSTIME, %#v (=%v), nil) slept only %v", &abs, until, slept)
  429. }
  430. // Invalid clock. clock_nanosleep(2) says EINVAL, but it’s actually EOPNOTSUPP.
  431. err = unix.ClockNanosleep(unix.CLOCK_THREAD_CPUTIME_ID, 0, &rel, nil)
  432. if err != unix.EINVAL && err != unix.EOPNOTSUPP {
  433. t.Errorf("ClockNanosleep(CLOCK_THREAD_CPUTIME_ID, 0, %#v, nil) = %v, want EINVAL or EOPNOTSUPP", &rel, err)
  434. }
  435. }