syscall_linux_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. "testing"
  11. "time"
  12. "golang.org/x/sys/unix"
  13. )
  14. func TestFchmodat(t *testing.T) {
  15. defer chtmpdir(t)()
  16. touch(t, "file1")
  17. err := os.Symlink("file1", "symlink1")
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", 0444, 0)
  22. if err != nil {
  23. t.Fatalf("Fchmodat: unexpected error: %v", err)
  24. }
  25. fi, err := os.Stat("file1")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if fi.Mode() != 0444 {
  30. t.Errorf("Fchmodat: failed to change mode: expected %v, got %v", 0444, fi.Mode())
  31. }
  32. err = unix.Fchmodat(unix.AT_FDCWD, "symlink1", 0444, unix.AT_SYMLINK_NOFOLLOW)
  33. if err != unix.EOPNOTSUPP {
  34. t.Fatalf("Fchmodat: unexpected error: %v, expected EOPNOTSUPP", err)
  35. }
  36. }
  37. func TestIoctlGetInt(t *testing.T) {
  38. f, err := os.Open("/dev/random")
  39. if err != nil {
  40. t.Fatalf("failed to open device: %v", err)
  41. }
  42. defer f.Close()
  43. v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
  44. if err != nil {
  45. t.Fatalf("failed to perform ioctl: %v", err)
  46. }
  47. t.Logf("%d bits of entropy available", v)
  48. }
  49. func TestPpoll(t *testing.T) {
  50. f, cleanup := mktmpfifo(t)
  51. defer cleanup()
  52. const timeout = 100 * time.Millisecond
  53. ok := make(chan bool, 1)
  54. go func() {
  55. select {
  56. case <-time.After(10 * timeout):
  57. t.Errorf("Ppoll: failed to timeout after %d", 10*timeout)
  58. case <-ok:
  59. }
  60. }()
  61. fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
  62. timeoutTs := unix.NsecToTimespec(int64(timeout))
  63. n, err := unix.Ppoll(fds, &timeoutTs, nil)
  64. ok <- true
  65. if err != nil {
  66. t.Errorf("Ppoll: unexpected error: %v", err)
  67. return
  68. }
  69. if n != 0 {
  70. t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0)
  71. return
  72. }
  73. }
  74. func TestTime(t *testing.T) {
  75. var ut unix.Time_t
  76. ut2, err := unix.Time(&ut)
  77. if err != nil {
  78. t.Fatalf("Time: %v", err)
  79. }
  80. if ut != ut2 {
  81. t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
  82. }
  83. var now time.Time
  84. for i := 0; i < 10; i++ {
  85. ut, err = unix.Time(nil)
  86. if err != nil {
  87. t.Fatalf("Time: %v", err)
  88. }
  89. now = time.Now()
  90. if int64(ut) == now.Unix() {
  91. return
  92. }
  93. }
  94. t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
  95. }
  96. func TestUtime(t *testing.T) {
  97. defer chtmpdir(t)()
  98. touch(t, "file1")
  99. buf := &unix.Utimbuf{
  100. Modtime: 12345,
  101. }
  102. err := unix.Utime("file1", buf)
  103. if err != nil {
  104. t.Fatalf("Utime: %v", err)
  105. }
  106. fi, err := os.Stat("file1")
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. if fi.ModTime().Unix() != 12345 {
  111. t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
  112. }
  113. }
  114. func TestUtimesNanoAt(t *testing.T) {
  115. defer chtmpdir(t)()
  116. symlink := "symlink1"
  117. os.Remove(symlink)
  118. err := os.Symlink("nonexisting", symlink)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. ts := []unix.Timespec{
  123. {Sec: 1111, Nsec: 2222},
  124. {Sec: 3333, Nsec: 4444},
  125. }
  126. err = unix.UtimesNanoAt(unix.AT_FDCWD, symlink, ts, unix.AT_SYMLINK_NOFOLLOW)
  127. if err != nil {
  128. t.Fatalf("UtimesNanoAt: %v", err)
  129. }
  130. var st unix.Stat_t
  131. err = unix.Lstat(symlink, &st)
  132. if err != nil {
  133. t.Fatalf("Lstat: %v", err)
  134. }
  135. if st.Atim != ts[0] {
  136. t.Errorf("UtimesNanoAt: wrong atime: %v", st.Atim)
  137. }
  138. if st.Mtim != ts[1] {
  139. t.Errorf("UtimesNanoAt: wrong mtime: %v", st.Mtim)
  140. }
  141. }
  142. func TestGetrlimit(t *testing.T) {
  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. }
  149. func TestSelect(t *testing.T) {
  150. _, err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0})
  151. if err != nil {
  152. t.Fatalf("Select: %v", err)
  153. }
  154. dur := 150 * time.Millisecond
  155. tv := unix.NsecToTimeval(int64(dur))
  156. start := time.Now()
  157. _, err = unix.Select(0, nil, nil, nil, &tv)
  158. took := time.Since(start)
  159. if err != nil {
  160. t.Fatalf("Select: %v", err)
  161. }
  162. if took < dur {
  163. t.Errorf("Select: timeout should have been at least %v, got %v", dur, took)
  164. }
  165. }
  166. func TestPselect(t *testing.T) {
  167. _, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
  168. if err != nil {
  169. t.Fatalf("Pselect: %v", err)
  170. }
  171. dur := 2500 * time.Microsecond
  172. ts := unix.NsecToTimespec(int64(dur))
  173. start := time.Now()
  174. _, err = unix.Pselect(0, nil, nil, nil, &ts, nil)
  175. took := time.Since(start)
  176. if err != nil {
  177. t.Fatalf("Pselect: %v", err)
  178. }
  179. if took < dur {
  180. t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
  181. }
  182. }
  183. func TestFstatat(t *testing.T) {
  184. defer chtmpdir(t)()
  185. touch(t, "file1")
  186. var st1 unix.Stat_t
  187. err := unix.Stat("file1", &st1)
  188. if err != nil {
  189. t.Fatalf("Stat: %v", err)
  190. }
  191. var st2 unix.Stat_t
  192. err = unix.Fstatat(unix.AT_FDCWD, "file1", &st2, 0)
  193. if err != nil {
  194. t.Fatalf("Fstatat: %v", err)
  195. }
  196. if st1 != st2 {
  197. t.Errorf("Fstatat: returned stat does not match Stat")
  198. }
  199. err = os.Symlink("file1", "symlink1")
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. err = unix.Lstat("symlink1", &st1)
  204. if err != nil {
  205. t.Fatalf("Lstat: %v", err)
  206. }
  207. err = unix.Fstatat(unix.AT_FDCWD, "symlink1", &st2, unix.AT_SYMLINK_NOFOLLOW)
  208. if err != nil {
  209. t.Fatalf("Fstatat: %v", err)
  210. }
  211. if st1 != st2 {
  212. t.Errorf("Fstatat: returned stat does not match Lstat")
  213. }
  214. }
  215. func TestSchedSetaffinity(t *testing.T) {
  216. runtime.LockOSThread()
  217. defer runtime.UnlockOSThread()
  218. var oldMask unix.CPUSet
  219. err := unix.SchedGetaffinity(0, &oldMask)
  220. if err != nil {
  221. t.Fatalf("SchedGetaffinity: %v", err)
  222. }
  223. var newMask unix.CPUSet
  224. newMask.Zero()
  225. if newMask.Count() != 0 {
  226. t.Errorf("CpuZero: didn't zero CPU set: %v", newMask)
  227. }
  228. cpu := 1
  229. newMask.Set(cpu)
  230. if newMask.Count() != 1 || !newMask.IsSet(cpu) {
  231. t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
  232. }
  233. cpu = 5
  234. newMask.Set(cpu)
  235. if newMask.Count() != 2 || !newMask.IsSet(cpu) {
  236. t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
  237. }
  238. newMask.Clear(cpu)
  239. if newMask.Count() != 1 || newMask.IsSet(cpu) {
  240. t.Errorf("CpuClr: didn't clear CPU %d in set: %v", cpu, newMask)
  241. }
  242. if runtime.NumCPU() < 2 {
  243. t.Skip("skipping setaffinity tests on single CPU system")
  244. }
  245. err = unix.SchedSetaffinity(0, &newMask)
  246. if err != nil {
  247. t.Fatalf("SchedSetaffinity: %v", err)
  248. }
  249. var gotMask unix.CPUSet
  250. err = unix.SchedGetaffinity(0, &gotMask)
  251. if err != nil {
  252. t.Fatalf("SchedGetaffinity: %v", err)
  253. }
  254. if gotMask != newMask {
  255. t.Errorf("SchedSetaffinity: returned affinity mask does not match set affinity mask")
  256. }
  257. // Restore old mask so it doesn't affect successive tests
  258. err = unix.SchedSetaffinity(0, &oldMask)
  259. if err != nil {
  260. t.Fatalf("SchedSetaffinity: %v", err)
  261. }
  262. }
  263. func TestStatx(t *testing.T) {
  264. var stx unix.Statx_t
  265. err := unix.Statx(unix.AT_FDCWD, ".", 0, 0, &stx)
  266. if err == unix.ENOSYS {
  267. t.Skip("statx syscall is not available, skipping test")
  268. } else if err != nil {
  269. t.Fatalf("Statx: %v", err)
  270. }
  271. defer chtmpdir(t)()
  272. touch(t, "file1")
  273. var st unix.Stat_t
  274. err = unix.Stat("file1", &st)
  275. if err != nil {
  276. t.Fatalf("Stat: %v", err)
  277. }
  278. flags := unix.AT_STATX_SYNC_AS_STAT
  279. err = unix.Statx(unix.AT_FDCWD, "file1", flags, unix.STATX_ALL, &stx)
  280. if err != nil {
  281. t.Fatalf("Statx: %v", err)
  282. }
  283. if uint32(stx.Mode) != st.Mode {
  284. t.Errorf("Statx: returned stat mode does not match Stat")
  285. }
  286. atime := unix.StatxTimestamp{Sec: int64(st.Atim.Sec), Nsec: uint32(st.Atim.Nsec)}
  287. ctime := unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
  288. mtime := unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
  289. if stx.Atime != atime {
  290. t.Errorf("Statx: returned stat atime does not match Stat")
  291. }
  292. if stx.Ctime != ctime {
  293. t.Errorf("Statx: returned stat ctime does not match Stat")
  294. }
  295. if stx.Mtime != mtime {
  296. t.Errorf("Statx: returned stat mtime does not match Stat")
  297. }
  298. err = os.Symlink("file1", "symlink1")
  299. if err != nil {
  300. t.Fatal(err)
  301. }
  302. err = unix.Lstat("symlink1", &st)
  303. if err != nil {
  304. t.Fatalf("Lstat: %v", err)
  305. }
  306. err = unix.Statx(unix.AT_FDCWD, "symlink1", flags, unix.STATX_BASIC_STATS, &stx)
  307. if err != nil {
  308. t.Fatalf("Statx: %v", err)
  309. }
  310. // follow symlink, expect a regulat file
  311. if stx.Mode&unix.S_IFREG == 0 {
  312. t.Errorf("Statx: didn't follow symlink")
  313. }
  314. err = unix.Statx(unix.AT_FDCWD, "symlink1", flags|unix.AT_SYMLINK_NOFOLLOW, unix.STATX_ALL, &stx)
  315. if err != nil {
  316. t.Fatalf("Statx: %v", err)
  317. }
  318. // follow symlink, expect a symlink
  319. if stx.Mode&unix.S_IFLNK == 0 {
  320. t.Errorf("Statx: unexpectedly followed symlink")
  321. }
  322. if uint32(stx.Mode) != st.Mode {
  323. t.Errorf("Statx: returned stat mode does not match Lstat")
  324. }
  325. atime = unix.StatxTimestamp{Sec: int64(st.Atim.Sec), Nsec: uint32(st.Atim.Nsec)}
  326. ctime = unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
  327. mtime = unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
  328. if stx.Atime != atime {
  329. t.Errorf("Statx: returned stat atime does not match Lstat")
  330. }
  331. if stx.Ctime != ctime {
  332. t.Errorf("Statx: returned stat ctime does not match Lstat")
  333. }
  334. if stx.Mtime != mtime {
  335. t.Errorf("Statx: returned stat mtime does not match Lstat")
  336. }
  337. }
  338. // utilities taken from os/os_test.go
  339. func touch(t *testing.T, name string) {
  340. f, err := os.Create(name)
  341. if err != nil {
  342. t.Fatal(err)
  343. }
  344. if err := f.Close(); err != nil {
  345. t.Fatal(err)
  346. }
  347. }
  348. // chtmpdir changes the working directory to a new temporary directory and
  349. // provides a cleanup function. Used when PWD is read-only.
  350. func chtmpdir(t *testing.T) func() {
  351. oldwd, err := os.Getwd()
  352. if err != nil {
  353. t.Fatalf("chtmpdir: %v", err)
  354. }
  355. d, err := ioutil.TempDir("", "test")
  356. if err != nil {
  357. t.Fatalf("chtmpdir: %v", err)
  358. }
  359. if err := os.Chdir(d); err != nil {
  360. t.Fatalf("chtmpdir: %v", err)
  361. }
  362. return func() {
  363. if err := os.Chdir(oldwd); err != nil {
  364. t.Fatalf("chtmpdir: %v", err)
  365. }
  366. os.RemoveAll(d)
  367. }
  368. }