syscall_linux_test.go 13 KB

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