syscall_linux_test.go 13 KB

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