1 // Rule 9: the ABI-layout arrays in syscall survive a run.
2 //
3 // `var pp [2]_C_int`, `(*[2]Timespec)(unsafe.Pointer(&ts[0]))` and
4 // `*[2]Timeval` are not user containers dodging slice bookkeeping - they are the
5 // kernel's layout (`int fd[2]`, `struct timespec[2]`), written by the kernel at
6 // a fixed address. Converting them to slices would put an allocation and an
7 // indirection between the caller and the buffer the kernel writes, so the
8 // decision on this item is KEEP, and this fixture is the pin: it runs the three
9 // shapes and checks that the values the kernel wrote come back.
10 //
11 //:wasmtest skip
12 package main
13 14 import (
15 "fmt"
16 "os"
17 "syscall"
18 )
19 20 func fail(msg string) {
21 fmt.Printf("FAIL %s\n", msg)
22 os.Exit(1)
23 }
24 25 func main() {
26 // Termination: a batch program must still be stoppable, so the entry
27 // handles the channel the runtime closes on SIGINT/SIGTERM.
28 select {
29 case <-shutdown:
30 return
31 default:
32 }
33 34 // os.Pipe -> pipe2(p *[2]_C_int, flags), read back through the files.
35 r, w, perr := os.Pipe()
36 if perr != nil {
37 fail("os.Pipe")
38 }
39 msg := []byte("rule-nine")
40 if n, werr := w.Write(msg); werr != nil || n != 9 {
41 fail("pipe write")
42 }
43 got := []byte{:9}
44 if n, rerr := r.Read(got); rerr != nil || n != 9 {
45 fail("pipe read")
46 }
47 if string(got) != "rule-nine" {
48 fail("pipe bytes")
49 }
50 r.Close()
51 w.Close()
52 53 // syscall.Pipe2 on a []int32{:2}: the slice's first element is the array the
54 // kernel fills through a *[2]_C_int.
55 var fds []int32 = []int32{:2}
56 if err := syscall.Pipe2(fds, 0); err != nil {
57 fail("pipe2")
58 }
59 if fds[0] < 0 || fds[1] < 0 || fds[0] == fds[1] {
60 fail("pipe2 fds")
61 }
62 syscall.Close(fds[0])
63 syscall.Close(fds[1])
64 65 // syscall.UtimesNano -> utimensat(times *[2]Timespec): two timespecs by
66 // pointer, then stat and compare what the kernel stored.
67 f, cerr := os.CreateTemp("", "rule9")
68 if cerr != nil {
69 fail("temp file")
70 }
71 name := f.Name()
72 f.Close()
73 74 const want = int64(1234567890)
75 var ts []syscall.Timespec = []syscall.Timespec{:2}
76 ts[0] = syscall.NsecToTimespec(want * 1e9)
77 ts[1] = syscall.NsecToTimespec(want * 1e9)
78 if err := syscall.UtimesNano(name, ts); err != nil {
79 os.Remove(name)
80 fail("utimesnano")
81 }
82 st, serr := os.Stat(name)
83 if serr != nil {
84 os.Remove(name)
85 fail("stat")
86 }
87 if st.ModTime().Unix() != want {
88 os.Remove(name)
89 fail("mtime")
90 }
91 os.Remove(name)
92 93 fmt.Printf("ok abi arrays: pipe2 and utimensat through the kernel\n")
94 fmt.Printf("abi_arrays_pipe_times ok\n")
95 }
96