Black Lives Matter. Support the Equal Justice Initiative.

Source file src/syscall/syscall_unix_test.go

Documentation: syscall

     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  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     6  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
     7  
     8  package syscall_test
     9  
    10  import (
    11  	"flag"
    12  	"fmt"
    13  	"internal/testenv"
    14  	"io"
    15  	"net"
    16  	"os"
    17  	"os/exec"
    18  	"path/filepath"
    19  	"runtime"
    20  	"strconv"
    21  	"syscall"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  // Tests that below functions, structures and constants are consistent
    27  // on all Unix-like systems.
    28  func _() {
    29  	// program scheduling priority functions and constants
    30  	var (
    31  		_ func(int, int, int) error   = syscall.Setpriority
    32  		_ func(int, int) (int, error) = syscall.Getpriority
    33  	)
    34  	const (
    35  		_ int = syscall.PRIO_USER
    36  		_ int = syscall.PRIO_PROCESS
    37  		_ int = syscall.PRIO_PGRP
    38  	)
    39  
    40  	// termios constants
    41  	const (
    42  		_ int = syscall.TCIFLUSH
    43  		_ int = syscall.TCIOFLUSH
    44  		_ int = syscall.TCOFLUSH
    45  	)
    46  
    47  	// fcntl file locking structure and constants
    48  	var (
    49  		_ = syscall.Flock_t{
    50  			Type:   int16(0),
    51  			Whence: int16(0),
    52  			Start:  int64(0),
    53  			Len:    int64(0),
    54  			Pid:    int32(0),
    55  		}
    56  	)
    57  	const (
    58  		_ = syscall.F_GETLK
    59  		_ = syscall.F_SETLK
    60  		_ = syscall.F_SETLKW
    61  	)
    62  }
    63  
    64  // TestFcntlFlock tests whether the file locking structure matches
    65  // the calling convention of each kernel.
    66  // On some Linux systems, glibc uses another set of values for the
    67  // commands and translates them to the correct value that the kernel
    68  // expects just before the actual fcntl syscall. As Go uses raw
    69  // syscalls directly, it must use the real value, not the glibc value.
    70  // Thus this test also verifies that the Flock_t structure can be
    71  // roundtripped with F_SETLK and F_GETLK.
    72  func TestFcntlFlock(t *testing.T) {
    73  	if runtime.GOOS == "ios" {
    74  		t.Skip("skipping; no child processes allowed on iOS")
    75  	}
    76  	flock := syscall.Flock_t{
    77  		Type:  syscall.F_WRLCK,
    78  		Start: 31415, Len: 271828, Whence: 1,
    79  	}
    80  	if os.Getenv("GO_WANT_HELPER_PROCESS") == "" {
    81  		// parent
    82  		tempDir := t.TempDir()
    83  		name := filepath.Join(tempDir, "TestFcntlFlock")
    84  		fd, err := syscall.Open(name, syscall.O_CREAT|syscall.O_RDWR|syscall.O_CLOEXEC, 0)
    85  		if err != nil {
    86  			t.Fatalf("Open failed: %v", err)
    87  		}
    88  		defer syscall.Close(fd)
    89  		if err := syscall.Ftruncate(fd, 1<<20); err != nil {
    90  			t.Fatalf("Ftruncate(1<<20) failed: %v", err)
    91  		}
    92  		if err := syscall.FcntlFlock(uintptr(fd), syscall.F_SETLK, &flock); err != nil {
    93  			t.Fatalf("FcntlFlock(F_SETLK) failed: %v", err)
    94  		}
    95  		cmd := exec.Command(os.Args[0], "-test.run=^TestFcntlFlock$")
    96  		cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
    97  		cmd.ExtraFiles = []*os.File{os.NewFile(uintptr(fd), name)}
    98  		out, err := cmd.CombinedOutput()
    99  		if len(out) > 0 || err != nil {
   100  			t.Fatalf("child process: %q, %v", out, err)
   101  		}
   102  	} else {
   103  		// child
   104  		got := flock
   105  		// make sure the child lock is conflicting with the parent lock
   106  		got.Start--
   107  		got.Len++
   108  		if err := syscall.FcntlFlock(3, syscall.F_GETLK, &got); err != nil {
   109  			t.Fatalf("FcntlFlock(F_GETLK) failed: %v", err)
   110  		}
   111  		flock.Pid = int32(syscall.Getppid())
   112  		// Linux kernel always set Whence to 0
   113  		flock.Whence = 0
   114  		if got.Type == flock.Type && got.Start == flock.Start && got.Len == flock.Len && got.Pid == flock.Pid && got.Whence == flock.Whence {
   115  			os.Exit(0)
   116  		}
   117  		t.Fatalf("FcntlFlock got %v, want %v", got, flock)
   118  	}
   119  }
   120  
   121  // TestPassFD tests passing a file descriptor over a Unix socket.
   122  //
   123  // This test involved both a parent and child process. The parent
   124  // process is invoked as a normal test, with "go test", which then
   125  // runs the child process by running the current test binary with args
   126  // "-test.run=^TestPassFD$" and an environment variable used to signal
   127  // that the test should become the child process instead.
   128  func TestPassFD(t *testing.T) {
   129  	testenv.MustHaveExec(t)
   130  
   131  	if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
   132  		passFDChild()
   133  		return
   134  	}
   135  
   136  	if runtime.GOOS == "aix" {
   137  		// Unix network isn't properly working on AIX 7.2 with Technical Level < 2
   138  		out, err := exec.Command("oslevel", "-s").Output()
   139  		if err != nil {
   140  			t.Skipf("skipping on AIX because oslevel -s failed: %v", err)
   141  		}
   142  		if len(out) < len("7200-XX-ZZ-YYMM") { // AIX 7.2, Tech Level XX, Service Pack ZZ, date YYMM
   143  			t.Skip("skipping on AIX because oslevel -s hasn't the right length")
   144  		}
   145  		aixVer := string(out[:4])
   146  		tl, err := strconv.Atoi(string(out[5:7]))
   147  		if err != nil {
   148  			t.Skipf("skipping on AIX because oslevel -s output cannot be parsed: %v", err)
   149  		}
   150  		if aixVer < "7200" || (aixVer == "7200" && tl < 2) {
   151  			t.Skip("skipped on AIX versions previous to 7.2 TL 2")
   152  		}
   153  
   154  	}
   155  
   156  	tempDir := t.TempDir()
   157  
   158  	fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
   159  	if err != nil {
   160  		t.Fatalf("Socketpair: %v", err)
   161  	}
   162  	writeFile := os.NewFile(uintptr(fds[0]), "child-writes")
   163  	readFile := os.NewFile(uintptr(fds[1]), "parent-reads")
   164  	defer writeFile.Close()
   165  	defer readFile.Close()
   166  
   167  	cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir)
   168  	cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
   169  	cmd.ExtraFiles = []*os.File{writeFile}
   170  
   171  	out, err := cmd.CombinedOutput()
   172  	if len(out) > 0 || err != nil {
   173  		t.Fatalf("child process: %q, %v", out, err)
   174  	}
   175  
   176  	c, err := net.FileConn(readFile)
   177  	if err != nil {
   178  		t.Fatalf("FileConn: %v", err)
   179  	}
   180  	defer c.Close()
   181  
   182  	uc, ok := c.(*net.UnixConn)
   183  	if !ok {
   184  		t.Fatalf("unexpected FileConn type; expected UnixConn, got %T", c)
   185  	}
   186  
   187  	buf := make([]byte, 32) // expect 1 byte
   188  	oob := make([]byte, 32) // expect 24 bytes
   189  	closeUnix := time.AfterFunc(5*time.Second, func() {
   190  		t.Logf("timeout reading from unix socket")
   191  		uc.Close()
   192  	})
   193  	_, oobn, _, _, err := uc.ReadMsgUnix(buf, oob)
   194  	if err != nil {
   195  		t.Fatalf("ReadMsgUnix: %v", err)
   196  	}
   197  	closeUnix.Stop()
   198  
   199  	scms, err := syscall.ParseSocketControlMessage(oob[:oobn])
   200  	if err != nil {
   201  		t.Fatalf("ParseSocketControlMessage: %v", err)
   202  	}
   203  	if len(scms) != 1 {
   204  		t.Fatalf("expected 1 SocketControlMessage; got scms = %#v", scms)
   205  	}
   206  	scm := scms[0]
   207  	gotFds, err := syscall.ParseUnixRights(&scm)
   208  	if err != nil {
   209  		t.Fatalf("syscall.ParseUnixRights: %v", err)
   210  	}
   211  	if len(gotFds) != 1 {
   212  		t.Fatalf("wanted 1 fd; got %#v", gotFds)
   213  	}
   214  
   215  	f := os.NewFile(uintptr(gotFds[0]), "fd-from-child")
   216  	defer f.Close()
   217  
   218  	got, err := io.ReadAll(f)
   219  	want := "Hello from child process!\n"
   220  	if string(got) != want {
   221  		t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want)
   222  	}
   223  }
   224  
   225  // passFDChild is the child process used by TestPassFD.
   226  func passFDChild() {
   227  	defer os.Exit(0)
   228  
   229  	// Look for our fd. It should be fd 3, but we work around an fd leak
   230  	// bug here (https://golang.org/issue/2603) to let it be elsewhere.
   231  	var uc *net.UnixConn
   232  	for fd := uintptr(3); fd <= 10; fd++ {
   233  		f := os.NewFile(fd, "unix-conn")
   234  		var ok bool
   235  		netc, _ := net.FileConn(f)
   236  		uc, ok = netc.(*net.UnixConn)
   237  		if ok {
   238  			break
   239  		}
   240  	}
   241  	if uc == nil {
   242  		fmt.Println("failed to find unix fd")
   243  		return
   244  	}
   245  
   246  	// Make a file f to send to our parent process on uc.
   247  	// We make it in tempDir, which our parent will clean up.
   248  	flag.Parse()
   249  	tempDir := flag.Arg(0)
   250  	f, err := os.CreateTemp(tempDir, "")
   251  	if err != nil {
   252  		fmt.Printf("TempFile: %v", err)
   253  		return
   254  	}
   255  
   256  	f.Write([]byte("Hello from child process!\n"))
   257  	f.Seek(0, io.SeekStart)
   258  
   259  	rights := syscall.UnixRights(int(f.Fd()))
   260  	dummyByte := []byte("x")
   261  	n, oobn, err := uc.WriteMsgUnix(dummyByte, rights, nil)
   262  	if err != nil {
   263  		fmt.Printf("WriteMsgUnix: %v", err)
   264  		return
   265  	}
   266  	if n != 1 || oobn != len(rights) {
   267  		fmt.Printf("WriteMsgUnix = %d, %d; want 1, %d", n, oobn, len(rights))
   268  		return
   269  	}
   270  }
   271  
   272  // TestUnixRightsRoundtrip tests that UnixRights, ParseSocketControlMessage,
   273  // and ParseUnixRights are able to successfully round-trip lists of file descriptors.
   274  func TestUnixRightsRoundtrip(t *testing.T) {
   275  	testCases := [...][][]int{
   276  		{{42}},
   277  		{{1, 2}},
   278  		{{3, 4, 5}},
   279  		{{}},
   280  		{{1, 2}, {3, 4, 5}, {}, {7}},
   281  	}
   282  	for _, testCase := range testCases {
   283  		b := []byte{}
   284  		var n int
   285  		for _, fds := range testCase {
   286  			// Last assignment to n wins
   287  			n = len(b) + syscall.CmsgLen(4*len(fds))
   288  			b = append(b, syscall.UnixRights(fds...)...)
   289  		}
   290  		// Truncate b
   291  		b = b[:n]
   292  
   293  		scms, err := syscall.ParseSocketControlMessage(b)
   294  		if err != nil {
   295  			t.Fatalf("ParseSocketControlMessage: %v", err)
   296  		}
   297  		if len(scms) != len(testCase) {
   298  			t.Fatalf("expected %v SocketControlMessage; got scms = %#v", len(testCase), scms)
   299  		}
   300  		for i, scm := range scms {
   301  			gotFds, err := syscall.ParseUnixRights(&scm)
   302  			if err != nil {
   303  				t.Fatalf("ParseUnixRights: %v", err)
   304  			}
   305  			wantFds := testCase[i]
   306  			if len(gotFds) != len(wantFds) {
   307  				t.Fatalf("expected %v fds, got %#v", len(wantFds), gotFds)
   308  			}
   309  			for j, fd := range gotFds {
   310  				if fd != wantFds[j] {
   311  					t.Fatalf("expected fd %v, got %v", wantFds[j], fd)
   312  				}
   313  			}
   314  		}
   315  	}
   316  }
   317  
   318  func TestRlimit(t *testing.T) {
   319  	var rlimit, zero syscall.Rlimit
   320  	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit)
   321  	if err != nil {
   322  		t.Fatalf("Getrlimit: save failed: %v", err)
   323  	}
   324  	if zero == rlimit {
   325  		t.Fatalf("Getrlimit: save failed: got zero value %#v", rlimit)
   326  	}
   327  	set := rlimit
   328  	set.Cur = set.Max - 1
   329  	if (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && set.Cur > 4096 {
   330  		// rlim_min for RLIMIT_NOFILE should be equal to
   331  		// or lower than kern.maxfilesperproc, which on
   332  		// some machines are 4096. See #40564.
   333  		set.Cur = 4096
   334  	}
   335  	err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &set)
   336  	if err != nil {
   337  		t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
   338  	}
   339  	var get syscall.Rlimit
   340  	err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &get)
   341  	if err != nil {
   342  		t.Fatalf("Getrlimit: get failed: %v", err)
   343  	}
   344  	set = rlimit
   345  	set.Cur = set.Max - 1
   346  	if (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && set.Cur > 4096 {
   347  		set.Cur = 4096
   348  	}
   349  	if set != get {
   350  		t.Fatalf("Rlimit: change failed: wanted %#v got %#v", set, get)
   351  	}
   352  	err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit)
   353  	if err != nil {
   354  		t.Fatalf("Setrlimit: restore failed: %#v %v", rlimit, err)
   355  	}
   356  }
   357  
   358  func TestSeekFailure(t *testing.T) {
   359  	_, err := syscall.Seek(-1, 0, io.SeekStart)
   360  	if err == nil {
   361  		t.Fatalf("Seek(-1, 0, 0) did not fail")
   362  	}
   363  	str := err.Error() // used to crash on Linux
   364  	t.Logf("Seek: %v", str)
   365  	if str == "" {
   366  		t.Fatalf("Seek(-1, 0, 0) return error with empty message")
   367  	}
   368  }
   369  
   370  func TestSetsockoptString(t *testing.T) {
   371  	// should not panic on empty string, see issue #31277
   372  	err := syscall.SetsockoptString(-1, 0, 0, "")
   373  	if err == nil {
   374  		t.Fatalf("SetsockoptString: did not fail")
   375  	}
   376  }
   377  
   378  func TestENFILETemporary(t *testing.T) {
   379  	if !syscall.ENFILE.Temporary() {
   380  		t.Error("ENFILE is not treated as a temporary error")
   381  	}
   382  }
   383  

View as plain text