syscall_linux_test.go 16 KB

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