rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/syscall/syscall_unix_test.go (about) 1 // Copyright 2013 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 5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris 6 7 package syscall_test 8 9 import ( 10 "flag" 11 "fmt" 12 "io/ioutil" 13 "net" 14 "os" 15 "os/exec" 16 "path/filepath" 17 "runtime" 18 "syscall" 19 "testing" 20 "time" 21 ) 22 23 // Tests that below functions, structures and constants are consistent 24 // on all Unix-like systems. 25 func _() { 26 // program scheduling priority functions and constants 27 var ( 28 _ func(int, int, int) error = syscall.Setpriority 29 _ func(int, int) (int, error) = syscall.Getpriority 30 ) 31 const ( 32 _ int = syscall.PRIO_USER 33 _ int = syscall.PRIO_PROCESS 34 _ int = syscall.PRIO_PGRP 35 ) 36 37 // termios constants 38 const ( 39 _ int = syscall.TCIFLUSH 40 _ int = syscall.TCIOFLUSH 41 _ int = syscall.TCOFLUSH 42 ) 43 44 // fcntl file locking structure and constants 45 var ( 46 _ = syscall.Flock_t{ 47 Type: int16(0), 48 Whence: int16(0), 49 Start: int64(0), 50 Len: int64(0), 51 Pid: int32(0), 52 } 53 ) 54 const ( 55 _ = syscall.F_GETLK 56 _ = syscall.F_SETLK 57 _ = syscall.F_SETLKW 58 ) 59 } 60 61 // TestFcntlFlock tests whether the file locking structure matches 62 // the calling convention of each kernel. 63 func TestFcntlFlock(t *testing.T) { 64 name := filepath.Join(os.TempDir(), "TestFcntlFlock") 65 fd, err := syscall.Open(name, syscall.O_CREAT|syscall.O_RDWR|syscall.O_CLOEXEC, 0) 66 if err != nil { 67 t.Fatalf("Open failed: %v", err) 68 } 69 defer syscall.Unlink(name) 70 defer syscall.Close(fd) 71 flock := syscall.Flock_t{ 72 Type: syscall.F_RDLCK, 73 Start: 0, Len: 0, Whence: 1, 74 } 75 if err := syscall.FcntlFlock(uintptr(fd), syscall.F_GETLK, &flock); err != nil { 76 t.Fatalf("FcntlFlock failed: %v", err) 77 } 78 } 79 80 // TestPassFD tests passing a file descriptor over a Unix socket. 81 // 82 // This test involved both a parent and child process. The parent 83 // process is invoked as a normal test, with "go test", which then 84 // runs the child process by running the current test binary with args 85 // "-test.run=^TestPassFD$" and an environment variable used to signal 86 // that the test should become the child process instead. 87 func TestPassFD(t *testing.T) { 88 switch runtime.GOOS { 89 case "dragonfly": 90 // TODO(jsing): Figure out why sendmsg is returning EINVAL. 91 t.Skip("skipping test on dragonfly") 92 case "solaris": 93 // TODO(aram): Figure out why ReadMsgUnix is returning empty message. 94 t.Skip("skipping test on solaris, see issue 7402") 95 case "darwin": 96 switch runtime.GOARCH { 97 case "arm", "arm64": 98 t.Skipf("skipping test on %d/%s, no fork", runtime.GOOS, runtime.GOARCH) 99 } 100 } 101 if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" { 102 passFDChild() 103 return 104 } 105 106 tempDir, err := ioutil.TempDir("", "TestPassFD") 107 if err != nil { 108 t.Fatal(err) 109 } 110 defer os.RemoveAll(tempDir) 111 112 fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0) 113 if err != nil { 114 t.Fatalf("Socketpair: %v", err) 115 } 116 defer syscall.Close(fds[0]) 117 defer syscall.Close(fds[1]) 118 writeFile := os.NewFile(uintptr(fds[0]), "child-writes") 119 readFile := os.NewFile(uintptr(fds[1]), "parent-reads") 120 defer writeFile.Close() 121 defer readFile.Close() 122 123 cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir) 124 cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} 125 cmd.ExtraFiles = []*os.File{writeFile} 126 127 out, err := cmd.CombinedOutput() 128 if len(out) > 0 || err != nil { 129 t.Fatalf("child process: %q, %v", out, err) 130 } 131 132 c, err := net.FileConn(readFile) 133 if err != nil { 134 t.Fatalf("FileConn: %v", err) 135 } 136 defer c.Close() 137 138 uc, ok := c.(*net.UnixConn) 139 if !ok { 140 t.Fatalf("unexpected FileConn type; expected UnixConn, got %T", c) 141 } 142 143 buf := make([]byte, 32) // expect 1 byte 144 oob := make([]byte, 32) // expect 24 bytes 145 closeUnix := time.AfterFunc(5*time.Second, func() { 146 t.Logf("timeout reading from unix socket") 147 uc.Close() 148 }) 149 _, oobn, _, _, err := uc.ReadMsgUnix(buf, oob) 150 closeUnix.Stop() 151 152 scms, err := syscall.ParseSocketControlMessage(oob[:oobn]) 153 if err != nil { 154 t.Fatalf("ParseSocketControlMessage: %v", err) 155 } 156 if len(scms) != 1 { 157 t.Fatalf("expected 1 SocketControlMessage; got scms = %#v", scms) 158 } 159 scm := scms[0] 160 gotFds, err := syscall.ParseUnixRights(&scm) 161 if err != nil { 162 t.Fatalf("syscall.ParseUnixRights: %v", err) 163 } 164 if len(gotFds) != 1 { 165 t.Fatalf("wanted 1 fd; got %#v", gotFds) 166 } 167 168 f := os.NewFile(uintptr(gotFds[0]), "fd-from-child") 169 defer f.Close() 170 171 got, err := ioutil.ReadAll(f) 172 want := "Hello from child process!\n" 173 if string(got) != want { 174 t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want) 175 } 176 } 177 178 // passFDChild is the child process used by TestPassFD. 179 func passFDChild() { 180 defer os.Exit(0) 181 182 // Look for our fd. It should be fd 3, but we work around an fd leak 183 // bug here (http://golang.org/issue/2603) to let it be elsewhere. 184 var uc *net.UnixConn 185 for fd := uintptr(3); fd <= 10; fd++ { 186 f := os.NewFile(fd, "unix-conn") 187 var ok bool 188 netc, _ := net.FileConn(f) 189 uc, ok = netc.(*net.UnixConn) 190 if ok { 191 break 192 } 193 } 194 if uc == nil { 195 fmt.Println("failed to find unix fd") 196 return 197 } 198 199 // Make a file f to send to our parent process on uc. 200 // We make it in tempDir, which our parent will clean up. 201 flag.Parse() 202 tempDir := flag.Arg(0) 203 f, err := ioutil.TempFile(tempDir, "") 204 if err != nil { 205 fmt.Printf("TempFile: %v", err) 206 return 207 } 208 209 f.Write([]byte("Hello from child process!\n")) 210 f.Seek(0, 0) 211 212 rights := syscall.UnixRights(int(f.Fd())) 213 dummyByte := []byte("x") 214 n, oobn, err := uc.WriteMsgUnix(dummyByte, rights, nil) 215 if err != nil { 216 fmt.Printf("WriteMsgUnix: %v", err) 217 return 218 } 219 if n != 1 || oobn != len(rights) { 220 fmt.Printf("WriteMsgUnix = %d, %d; want 1, %d", n, oobn, len(rights)) 221 return 222 } 223 } 224 225 // TestUnixRightsRoundtrip tests that UnixRights, ParseSocketControlMessage, 226 // and ParseUnixRights are able to successfully round-trip lists of file descriptors. 227 func TestUnixRightsRoundtrip(t *testing.T) { 228 testCases := [...][][]int{ 229 {{42}}, 230 {{1, 2}}, 231 {{3, 4, 5}}, 232 {{}}, 233 {{1, 2}, {3, 4, 5}, {}, {7}}, 234 } 235 for _, testCase := range testCases { 236 b := []byte{} 237 var n int 238 for _, fds := range testCase { 239 // Last assignment to n wins 240 n = len(b) + syscall.CmsgLen(4*len(fds)) 241 b = append(b, syscall.UnixRights(fds...)...) 242 } 243 // Truncate b 244 b = b[:n] 245 246 scms, err := syscall.ParseSocketControlMessage(b) 247 if err != nil { 248 t.Fatalf("ParseSocketControlMessage: %v", err) 249 } 250 if len(scms) != len(testCase) { 251 t.Fatalf("expected %v SocketControlMessage; got scms = %#v", len(testCase), scms) 252 } 253 for i, scm := range scms { 254 gotFds, err := syscall.ParseUnixRights(&scm) 255 if err != nil { 256 t.Fatalf("ParseUnixRights: %v", err) 257 } 258 wantFds := testCase[i] 259 if len(gotFds) != len(wantFds) { 260 t.Fatalf("expected %v fds, got %#v", len(wantFds), gotFds) 261 } 262 for j, fd := range gotFds { 263 if fd != wantFds[j] { 264 t.Fatalf("expected fd %v, got %v", wantFds[j], fd) 265 } 266 } 267 } 268 } 269 } 270 271 func TestRlimit(t *testing.T) { 272 var rlimit, zero syscall.Rlimit 273 err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit) 274 if err != nil { 275 t.Fatalf("Getrlimit: save failed: %v", err) 276 } 277 if zero == rlimit { 278 t.Fatalf("Getrlimit: save failed: got zero value %#v", rlimit) 279 } 280 set := rlimit 281 set.Cur = set.Max - 1 282 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &set) 283 if err != nil { 284 t.Fatalf("Setrlimit: set failed: %#v %v", set, err) 285 } 286 var get syscall.Rlimit 287 err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &get) 288 if err != nil { 289 t.Fatalf("Getrlimit: get failed: %v", err) 290 } 291 set = rlimit 292 set.Cur = set.Max - 1 293 if set != get { 294 // Seems like Darwin requires some privilege to 295 // increase the soft limit of rlimit sandbox, though 296 // Setrlimit never reports an error. 297 switch runtime.GOOS { 298 case "darwin": 299 default: 300 t.Fatalf("Rlimit: change failed: wanted %#v got %#v", set, get) 301 } 302 } 303 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit) 304 if err != nil { 305 t.Fatalf("Setrlimit: restore failed: %#v %v", rlimit, err) 306 } 307 } 308 309 func TestSeekFailure(t *testing.T) { 310 _, err := syscall.Seek(-1, 0, 0) 311 if err == nil { 312 t.Fatalf("Seek(-1, 0, 0) did not fail") 313 } 314 str := err.Error() // used to crash on Linux 315 t.Logf("Seek: %v", str) 316 if str == "" { 317 t.Fatalf("Seek(-1, 0, 0) return error with empty message") 318 } 319 }