...

Source file src/golang.org/x/sys/unix/syscall_linux_test.go

Documentation: golang.org/x/sys/unix

     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  
     5  //go:build linux
     6  
     7  package unix_test
     8  
     9  import (
    10  	"bufio"
    11  	"bytes"
    12  	"encoding/hex"
    13  	"errors"
    14  	"fmt"
    15  	"io"
    16  	"net"
    17  	"os"
    18  	"os/exec"
    19  	"path/filepath"
    20  	"runtime"
    21  	"runtime/debug"
    22  	"strconv"
    23  	"strings"
    24  	"syscall"
    25  	"testing"
    26  	"time"
    27  	"unsafe"
    28  
    29  	"golang.org/x/sys/unix"
    30  )
    31  
    32  func TestIoctlGetEthtoolDrvinfo(t *testing.T) {
    33  	if runtime.GOOS == "android" {
    34  		t.Skip("ethtool driver info is not available on android, skipping test")
    35  	}
    36  
    37  	s, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
    38  	if err != nil {
    39  		t.Fatalf("failed to open socket: %v", err)
    40  	}
    41  	defer unix.Close(s)
    42  
    43  	ifis, err := net.Interfaces()
    44  	if err != nil {
    45  		t.Fatalf("failed to get network interfaces: %v", err)
    46  	}
    47  
    48  	// Print the interface name and associated driver information for each
    49  	// network interface supported by ethtool.
    50  	for _, ifi := range ifis {
    51  		drv, err := unix.IoctlGetEthtoolDrvinfo(s, ifi.Name)
    52  		if err != nil {
    53  			if err == unix.EOPNOTSUPP {
    54  				continue
    55  			}
    56  
    57  			if err == unix.EBUSY {
    58  				// See https://go.dev/issues/67350
    59  				t.Logf("%s: ethtool driver busy, possible kernel bug", ifi.Name)
    60  				continue
    61  			}
    62  
    63  			t.Fatalf("failed to get ethtool driver info for %q: %v", ifi.Name, err)
    64  		}
    65  
    66  		// Trim trailing NULLs.
    67  		t.Logf("%s: %q", ifi.Name, string(bytes.TrimRight(drv.Driver[:], "\x00")))
    68  	}
    69  }
    70  
    71  func TestIoctlGetEthtoolTsInfo(t *testing.T) {
    72  	if runtime.GOOS == "android" {
    73  		t.Skip("ethtool driver info is not available on android, skipping test")
    74  	}
    75  
    76  	s, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
    77  	if err != nil {
    78  		t.Fatalf("failed to open socket: %v", err)
    79  	}
    80  	defer unix.Close(s)
    81  
    82  	ifis, err := net.Interfaces()
    83  	if err != nil {
    84  		t.Fatalf("failed to get network interfaces: %v", err)
    85  	}
    86  
    87  	// Print the interface name and associated PHC information for each
    88  	// network interface supported by ethtool.
    89  	for _, ifi := range ifis {
    90  		tsi, err := unix.IoctlGetEthtoolTsInfo(s, ifi.Name)
    91  		if err != nil {
    92  			if err == unix.EOPNOTSUPP {
    93  				continue
    94  			}
    95  
    96  			if err == unix.EBUSY {
    97  				// See https://go.dev/issues/67350
    98  				t.Logf("%s: ethtool driver busy, possible kernel bug", ifi.Name)
    99  				continue
   100  			}
   101  
   102  			t.Fatalf("failed to get ethtool PHC info for %q: %v", ifi.Name, err)
   103  		}
   104  
   105  		t.Logf("%s: ptp%d", ifi.Name, tsi.Phc_index)
   106  	}
   107  }
   108  
   109  func TestIoctlGetInt(t *testing.T) {
   110  	f, err := os.Open("/dev/random")
   111  	if err != nil {
   112  		t.Fatalf("failed to open device: %v", err)
   113  	}
   114  	defer f.Close()
   115  
   116  	v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
   117  	if err != nil {
   118  		t.Fatalf("failed to perform ioctl: %v", err)
   119  	}
   120  
   121  	t.Logf("%d bits of entropy available", v)
   122  }
   123  
   124  func TestIoctlRetInt(t *testing.T) {
   125  	f, err := os.Open("/proc/self/ns/mnt")
   126  	if err != nil {
   127  		t.Skipf("skipping test, %v", err)
   128  	}
   129  	defer f.Close()
   130  
   131  	v, err := unix.IoctlRetInt(int(f.Fd()), unix.NS_GET_NSTYPE)
   132  	if err != nil {
   133  		if err == unix.ENOTTY {
   134  			t.Skipf("old kernel? (need Linux >= 4.11)")
   135  		}
   136  		t.Fatalf("failed to perform ioctl: %v", err)
   137  	}
   138  	if v != unix.CLONE_NEWNS {
   139  		t.Fatalf("unexpected return from ioctl; expected %v, got %v", v, unix.CLONE_NEWNS)
   140  	}
   141  }
   142  
   143  func TestIoctlGetRTCTime(t *testing.T) {
   144  	f, err := os.Open("/dev/rtc0")
   145  	if err != nil {
   146  		t.Skipf("skipping test, %v", err)
   147  	}
   148  	defer f.Close()
   149  
   150  	v, err := unix.IoctlGetRTCTime(int(f.Fd()))
   151  	if err != nil {
   152  		t.Fatalf("failed to perform ioctl: %v", err)
   153  	}
   154  
   155  	t.Logf("RTC time: %04d-%02d-%02d %02d:%02d:%02d", v.Year+1900, v.Mon+1, v.Mday, v.Hour, v.Min, v.Sec)
   156  }
   157  
   158  func TestIoctlGetRTCWkAlrm(t *testing.T) {
   159  	f, err := os.Open("/dev/rtc0")
   160  	if err != nil {
   161  		t.Skipf("skipping test, %v", err)
   162  	}
   163  	defer f.Close()
   164  
   165  	v, err := unix.IoctlGetRTCWkAlrm(int(f.Fd()))
   166  
   167  	// Not all RTC drivers support wakeup alarms, and will return EINVAL in such cases.
   168  	if err == unix.EINVAL {
   169  		t.Skip("RTC_WKALM_RD ioctl not supported on this rtc, skipping test")
   170  	}
   171  
   172  	if err != nil {
   173  		t.Fatalf("failed to perform ioctl: %v", err)
   174  	}
   175  
   176  	t.Logf("RTC wake alarm enabled '%d'; time: %04d-%02d-%02d %02d:%02d:%02d",
   177  		v.Enabled, v.Time.Year+1900, v.Time.Mon+1, v.Time.Mday, v.Time.Hour, v.Time.Min, v.Time.Sec)
   178  }
   179  
   180  func TestIoctlIfreq(t *testing.T) {
   181  	s, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
   182  	if err != nil {
   183  		t.Fatalf("failed to open socket: %v", err)
   184  	}
   185  	defer unix.Close(s)
   186  
   187  	ifis, err := net.Interfaces()
   188  	if err != nil {
   189  		t.Fatalf("failed to get network interfaces: %v", err)
   190  	}
   191  
   192  	// Compare the network interface fetched from rtnetlink with the data from
   193  	// the equivalent ioctl API.
   194  	for _, ifi := range ifis {
   195  		ifr, err := unix.NewIfreq(ifi.Name)
   196  		if err != nil {
   197  			t.Fatalf("failed to create ifreq for %q: %v", ifi.Name, err)
   198  		}
   199  
   200  		if err := unix.IoctlIfreq(s, unix.SIOCGIFINDEX, ifr); err != nil {
   201  			t.Fatalf("failed to get interface index for %q: %v", ifi.Name, err)
   202  		}
   203  
   204  		if want, got := ifi.Index, int(ifr.Uint32()); want != got {
   205  			t.Fatalf("unexpected interface index for %q: got: %d, want: %d",
   206  				ifi.Name, got, want)
   207  		}
   208  
   209  		if want, got := ifi.Name, ifr.Name(); want != got {
   210  			t.Fatalf("unexpected interface name for index %d: got: %q, want: %q",
   211  				ifi.Index, got, want)
   212  		}
   213  
   214  		wantIP, ok := firstIPv4(t, &ifi)
   215  		if err := unix.IoctlIfreq(s, unix.SIOCGIFADDR, ifr); err != nil {
   216  			// Interface may have no assigned IPv4 address.
   217  			if err != unix.EADDRNOTAVAIL {
   218  				t.Fatalf("failed to get IPv4 address for %q: %v", ifi.Name, err)
   219  			}
   220  
   221  			// But if we found an address via rtnetlink, we should expect the
   222  			// ioctl to return one.
   223  			if ok {
   224  				t.Fatalf("found IPv4 address %q for %q but ioctl returned none", wantIP, ifi.Name)
   225  			}
   226  
   227  			continue
   228  		}
   229  
   230  		// Found an address, compare it directly.
   231  		addr, err := ifr.Inet4Addr()
   232  		if err != nil {
   233  			t.Fatalf("failed to get ifreq IPv4 address: %v", err)
   234  		}
   235  
   236  		if want, got := wantIP, addr; !want.Equal(got) {
   237  			t.Fatalf("unexpected first IPv4 address for %q: got: %q, want: %q",
   238  				ifi.Name, got, want)
   239  		}
   240  	}
   241  }
   242  
   243  // firstIPv4 reports whether the interface has an IPv4 address assigned,
   244  // returning the first discovered address.
   245  func firstIPv4(t *testing.T, ifi *net.Interface) (net.IP, bool) {
   246  	t.Helper()
   247  
   248  	addrs, err := ifi.Addrs()
   249  	if err != nil {
   250  		t.Fatalf("failed to get interface %q addresses: %v", ifi.Name, err)
   251  	}
   252  
   253  	for _, a := range addrs {
   254  		// Only want valid IPv4 addresses.
   255  		ipn, ok := a.(*net.IPNet)
   256  		if !ok || ipn.IP.To4() == nil {
   257  			continue
   258  		}
   259  
   260  		return ipn.IP, true
   261  	}
   262  
   263  	return nil, false
   264  }
   265  
   266  func TestPidfd(t *testing.T) {
   267  	// Start a child process which will sleep for 1 hour; longer than the 10
   268  	// minute default Go test timeout.
   269  	cmd := exec.Command("sleep", "1h")
   270  	if err := cmd.Start(); err != nil {
   271  		t.Fatalf("failed to exec sleep: %v", err)
   272  	}
   273  
   274  	fd, err := unix.PidfdOpen(cmd.Process.Pid, 0)
   275  	if err != nil {
   276  		// GOARCH arm/arm64 and GOOS android builders do not support pidfds.
   277  		if errors.Is(err, unix.ENOSYS) {
   278  			t.Skipf("skipping, pidfd_open is not implemented: %v", err)
   279  		}
   280  
   281  		t.Fatalf("failed to open child pidfd: %v", err)
   282  	}
   283  	defer unix.Close(fd)
   284  
   285  	// Child is running but not terminated.
   286  	if err := unix.Waitid(unix.P_PIDFD, fd, nil, unix.WEXITED|unix.WNOHANG, nil); err != nil {
   287  		if errors.Is(err, unix.EINVAL) {
   288  			t.Skip("skipping due to waitid EINVAL, see https://go.dev/issues/52014")
   289  		}
   290  
   291  		t.Fatalf("failed to check for child exit: %v", err)
   292  	}
   293  
   294  	const want = unix.SIGHUP
   295  	if err := unix.PidfdSendSignal(fd, want, nil, 0); err != nil {
   296  		t.Fatalf("failed to signal child process: %v", err)
   297  	}
   298  
   299  	// Now verify that the child process received the expected signal.
   300  	var eerr *exec.ExitError
   301  	if err := cmd.Wait(); !errors.As(err, &eerr) {
   302  		t.Fatalf("child process terminated but did not return an exit error: %v", err)
   303  	}
   304  
   305  	if err := unix.Waitid(unix.P_PIDFD, fd, nil, unix.WEXITED, nil); !errors.Is(err, unix.ECHILD) {
   306  		t.Fatalf("expected ECHILD for final waitid, but got: %v", err)
   307  	}
   308  
   309  	ws, ok := eerr.Sys().(syscall.WaitStatus)
   310  	if !ok {
   311  		t.Fatalf("expected syscall.WaitStatus value, but got: %#T", eerr.Sys())
   312  	}
   313  
   314  	if got := ws.Signal(); got != want {
   315  		t.Fatalf("unexpected child exit signal, got: %s, want: %s", got, want)
   316  	}
   317  }
   318  
   319  func TestPpoll(t *testing.T) {
   320  	if runtime.GOOS == "android" {
   321  		t.Skip("mkfifo syscall is not available on android, skipping test")
   322  	}
   323  
   324  	chtmpdir(t)
   325  	f := mktmpfifo(t)
   326  
   327  	const timeout = 100 * time.Millisecond
   328  
   329  	ok := make(chan bool, 1)
   330  	go func() {
   331  		select {
   332  		case <-time.After(10 * timeout):
   333  			t.Errorf("Ppoll: failed to timeout after %d", 10*timeout)
   334  		case <-ok:
   335  		}
   336  	}()
   337  
   338  	fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
   339  	timeoutTs := unix.NsecToTimespec(int64(timeout))
   340  	for {
   341  		n, err := unix.Ppoll(fds, &timeoutTs, nil)
   342  		ok <- true
   343  		if err == unix.EINTR {
   344  			t.Log("Ppoll interrupted")
   345  			continue
   346  		} else if err != nil {
   347  			t.Errorf("Ppoll: unexpected error: %v", err)
   348  			return
   349  		}
   350  		if n != 0 {
   351  			t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0)
   352  			return
   353  		}
   354  		break
   355  	}
   356  }
   357  
   358  func TestTime(t *testing.T) {
   359  	var ut unix.Time_t
   360  	ut2, err := unix.Time(&ut)
   361  	if err != nil {
   362  		t.Fatalf("Time: %v", err)
   363  	}
   364  	if ut != ut2 {
   365  		t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
   366  	}
   367  
   368  	var now time.Time
   369  
   370  	for i := 0; i < 10; i++ {
   371  		ut, err = unix.Time(nil)
   372  		if err != nil {
   373  			t.Fatalf("Time: %v", err)
   374  		}
   375  
   376  		now = time.Now()
   377  		diff := int64(ut) - now.Unix()
   378  		if -1 <= diff && diff <= 1 {
   379  			return
   380  		}
   381  	}
   382  
   383  	t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v±1", ut, now.Unix())
   384  }
   385  
   386  func TestUtime(t *testing.T) {
   387  	chtmpdir(t)
   388  
   389  	touch(t, "file1")
   390  
   391  	buf := &unix.Utimbuf{
   392  		Modtime: 12345,
   393  	}
   394  
   395  	err := unix.Utime("file1", buf)
   396  	if err != nil {
   397  		t.Fatalf("Utime: %v", err)
   398  	}
   399  
   400  	fi, err := os.Stat("file1")
   401  	if err != nil {
   402  		t.Fatal(err)
   403  	}
   404  
   405  	if fi.ModTime().Unix() != 12345 {
   406  		t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
   407  	}
   408  }
   409  
   410  func TestRlimitAs(t *testing.T) {
   411  	// disable GC during to avoid flaky test
   412  	defer debug.SetGCPercent(debug.SetGCPercent(-1))
   413  
   414  	var rlim unix.Rlimit
   415  	err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
   416  	if err != nil {
   417  		t.Fatalf("Getrlimit: %v", err)
   418  	}
   419  	var zero unix.Rlimit
   420  	if zero == rlim {
   421  		t.Fatalf("Getrlimit: got zero value %#v", rlim)
   422  	}
   423  	set := rlim
   424  	set.Cur = uint64(unix.Getpagesize())
   425  	err = unix.Setrlimit(unix.RLIMIT_AS, &set)
   426  	if err != nil {
   427  		t.Fatalf("Setrlimit: set failed: %#v %v", set, err)
   428  	}
   429  
   430  	// RLIMIT_AS was set to the page size, so mmap()'ing twice the page size
   431  	// should fail. See 'man 2 getrlimit'.
   432  	_, err = unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
   433  	if err == nil {
   434  		t.Fatal("Mmap: unexpectedly succeeded after setting RLIMIT_AS")
   435  	}
   436  
   437  	err = unix.Setrlimit(unix.RLIMIT_AS, &rlim)
   438  	if err != nil {
   439  		t.Fatalf("Setrlimit: restore failed: %#v %v", rlim, err)
   440  	}
   441  
   442  	b, err := unix.Mmap(-1, 0, 2*unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
   443  	if err != nil {
   444  		t.Fatalf("Mmap: %v", err)
   445  	}
   446  	err = unix.Munmap(b)
   447  	if err != nil {
   448  		t.Fatalf("Munmap: %v", err)
   449  	}
   450  }
   451  
   452  func TestPselect(t *testing.T) {
   453  	for {
   454  		n, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, nil)
   455  		if err == unix.EINTR {
   456  			t.Logf("Pselect interrupted")
   457  			continue
   458  		} else if err != nil {
   459  			t.Fatalf("Pselect: %v", err)
   460  		}
   461  		if n != 0 {
   462  			t.Fatalf("Pselect: got %v ready file descriptors, expected 0", n)
   463  		}
   464  		break
   465  	}
   466  
   467  	dur := 2500 * time.Microsecond
   468  	var took time.Duration
   469  	for {
   470  		// On some platforms (e.g. Linux), the passed-in timespec is
   471  		// updated by pselect(2). Make sure to reset to the full
   472  		// duration in case of an EINTR.
   473  		ts := unix.NsecToTimespec(int64(dur))
   474  		start := time.Now()
   475  		n, err := unix.Pselect(0, nil, nil, nil, &ts, nil)
   476  		took = time.Since(start)
   477  		if err == unix.EINTR {
   478  			t.Logf("Pselect interrupted after %v", took)
   479  			continue
   480  		} else if err != nil {
   481  			t.Fatalf("Pselect: %v", err)
   482  		}
   483  		if n != 0 {
   484  			t.Fatalf("Pselect: got %v ready file descriptors, expected 0", n)
   485  		}
   486  		break
   487  	}
   488  
   489  	// On some builder the actual timeout might also be slightly less than the requested.
   490  	// Add an acceptable margin to avoid flaky tests.
   491  	if took < dur*2/3 {
   492  		t.Errorf("Pselect: got %v timeout, expected at least %v", took, dur)
   493  	}
   494  }
   495  
   496  func TestPselectWithSigmask(t *testing.T) {
   497  	var sigmask unix.Sigset_t
   498  	sigmask.Val[0] |= 1 << (uint(unix.SIGUSR1) - 1)
   499  	for {
   500  		n, err := unix.Pselect(0, nil, nil, nil, &unix.Timespec{Sec: 0, Nsec: 0}, &sigmask)
   501  		if err == unix.EINTR {
   502  			t.Logf("Pselect interrupted")
   503  			continue
   504  		} else if err != nil {
   505  			t.Fatalf("Pselect: %v", err)
   506  		}
   507  		if n != 0 {
   508  			t.Fatalf("Pselect: got %v ready file descriptors, expected 0", n)
   509  		}
   510  		break
   511  	}
   512  }
   513  
   514  func TestSchedSetaffinity(t *testing.T) {
   515  	var newMask unix.CPUSet
   516  	newMask.Zero()
   517  	if newMask.Count() != 0 {
   518  		t.Errorf("CpuZero: didn't zero CPU set: %v", newMask)
   519  	}
   520  	cpu := 1
   521  	newMask.Set(cpu)
   522  	if newMask.Count() != 1 || !newMask.IsSet(cpu) {
   523  		t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
   524  	}
   525  	cpu = 5
   526  	newMask.Set(cpu)
   527  	if newMask.Count() != 2 || !newMask.IsSet(cpu) {
   528  		t.Errorf("CpuSet: didn't set CPU %d in set: %v", cpu, newMask)
   529  	}
   530  	newMask.Clear(cpu)
   531  	if newMask.Count() != 1 || newMask.IsSet(cpu) {
   532  		t.Errorf("CpuClr: didn't clear CPU %d in set: %v", cpu, newMask)
   533  	}
   534  
   535  	runtime.LockOSThread()
   536  	defer runtime.UnlockOSThread()
   537  
   538  	var oldMask unix.CPUSet
   539  	err := unix.SchedGetaffinity(0, &oldMask)
   540  	if err != nil {
   541  		t.Fatalf("SchedGetaffinity: %v", err)
   542  	}
   543  
   544  	if runtime.NumCPU() < 2 {
   545  		t.Skip("skipping setaffinity tests on single CPU system")
   546  	}
   547  	if runtime.GOOS == "android" {
   548  		t.Skip("skipping setaffinity tests on android")
   549  	}
   550  
   551  	// On a system like ppc64x where some cores can be disabled using ppc64_cpu,
   552  	// setaffinity should only be called with enabled cores. The valid cores
   553  	// are found from the oldMask, but if none are found then the setaffinity
   554  	// tests are skipped. Issue #27875.
   555  	cpu = 1
   556  	if !oldMask.IsSet(cpu) {
   557  		newMask.Zero()
   558  		for i := 0; i < len(oldMask); i++ {
   559  			if oldMask.IsSet(i) {
   560  				newMask.Set(i)
   561  				break
   562  			}
   563  		}
   564  		if newMask.Count() == 0 {
   565  			t.Skip("skipping setaffinity tests if CPU not available")
   566  		}
   567  	}
   568  
   569  	err = unix.SchedSetaffinity(0, &newMask)
   570  	if err != nil {
   571  		t.Fatalf("SchedSetaffinity: %v", err)
   572  	}
   573  
   574  	var gotMask unix.CPUSet
   575  	err = unix.SchedGetaffinity(0, &gotMask)
   576  	if err != nil {
   577  		t.Fatalf("SchedGetaffinity: %v", err)
   578  	}
   579  
   580  	if gotMask != newMask {
   581  		t.Errorf("SchedSetaffinity: returned affinity mask does not match set affinity mask")
   582  	}
   583  
   584  	// Restore old mask so it doesn't affect successive tests
   585  	err = unix.SchedSetaffinity(0, &oldMask)
   586  	if err != nil {
   587  		t.Fatalf("SchedSetaffinity: %v", err)
   588  	}
   589  }
   590  
   591  func TestStatx(t *testing.T) {
   592  	var stx unix.Statx_t
   593  	err := unix.Statx(unix.AT_FDCWD, ".", 0, 0, &stx)
   594  	if err == unix.ENOSYS || err == unix.EPERM {
   595  		t.Skip("statx syscall is not available, skipping test")
   596  	} else if err != nil {
   597  		t.Fatalf("Statx: %v", err)
   598  	}
   599  
   600  	chtmpdir(t)
   601  	touch(t, "file1")
   602  
   603  	var st unix.Stat_t
   604  	err = unix.Stat("file1", &st)
   605  	if err != nil {
   606  		t.Fatalf("Stat: %v", err)
   607  	}
   608  
   609  	flags := unix.AT_STATX_SYNC_AS_STAT
   610  	err = unix.Statx(unix.AT_FDCWD, "file1", flags, unix.STATX_ALL, &stx)
   611  	if err != nil {
   612  		t.Fatalf("Statx: %v", err)
   613  	}
   614  
   615  	if uint32(stx.Mode) != st.Mode {
   616  		t.Errorf("Statx: returned stat mode does not match Stat")
   617  	}
   618  
   619  	ctime := unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
   620  	mtime := unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
   621  
   622  	if stx.Ctime != ctime {
   623  		t.Errorf("Statx: returned stat ctime does not match Stat")
   624  	}
   625  	if stx.Mtime != mtime {
   626  		t.Errorf("Statx: returned stat mtime does not match Stat")
   627  	}
   628  
   629  	err = os.Symlink("file1", "symlink1")
   630  	if err != nil {
   631  		t.Fatal(err)
   632  	}
   633  
   634  	err = unix.Lstat("symlink1", &st)
   635  	if err != nil {
   636  		t.Fatalf("Lstat: %v", err)
   637  	}
   638  
   639  	err = unix.Statx(unix.AT_FDCWD, "symlink1", flags, unix.STATX_BASIC_STATS, &stx)
   640  	if err != nil {
   641  		t.Fatalf("Statx: %v", err)
   642  	}
   643  
   644  	// follow symlink, expect a regulat file
   645  	if stx.Mode&unix.S_IFREG == 0 {
   646  		t.Errorf("Statx: didn't follow symlink")
   647  	}
   648  
   649  	err = unix.Statx(unix.AT_FDCWD, "symlink1", flags|unix.AT_SYMLINK_NOFOLLOW, unix.STATX_ALL, &stx)
   650  	if err != nil {
   651  		t.Fatalf("Statx: %v", err)
   652  	}
   653  
   654  	// follow symlink, expect a symlink
   655  	if stx.Mode&unix.S_IFLNK == 0 {
   656  		t.Errorf("Statx: unexpectedly followed symlink")
   657  	}
   658  	if uint32(stx.Mode) != st.Mode {
   659  		t.Errorf("Statx: returned stat mode does not match Lstat")
   660  	}
   661  
   662  	ctime = unix.StatxTimestamp{Sec: int64(st.Ctim.Sec), Nsec: uint32(st.Ctim.Nsec)}
   663  	mtime = unix.StatxTimestamp{Sec: int64(st.Mtim.Sec), Nsec: uint32(st.Mtim.Nsec)}
   664  
   665  	if stx.Ctime != ctime {
   666  		t.Errorf("Statx: returned stat ctime does not match Lstat")
   667  	}
   668  	if stx.Mtime != mtime {
   669  		t.Errorf("Statx: returned stat mtime does not match Lstat")
   670  	}
   671  }
   672  
   673  // stringsFromByteSlice converts a sequence of attributes to a []string.
   674  // On Linux, each entry is a NULL-terminated string.
   675  func stringsFromByteSlice(buf []byte) []string {
   676  	var result []string
   677  	off := 0
   678  	for i, b := range buf {
   679  		if b == 0 {
   680  			result = append(result, string(buf[off:i]))
   681  			off = i + 1
   682  		}
   683  	}
   684  	return result
   685  }
   686  
   687  func TestFaccessat(t *testing.T) {
   688  	chtmpdir(t)
   689  	touch(t, "file1")
   690  
   691  	err := unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, 0)
   692  	if err != nil {
   693  		t.Errorf("Faccessat: unexpected error: %v", err)
   694  	}
   695  
   696  	err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, 2)
   697  	if err != unix.EINVAL {
   698  		t.Errorf("Faccessat: unexpected error: %v, want EINVAL", err)
   699  	}
   700  
   701  	err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, unix.AT_EACCESS)
   702  	if err != nil {
   703  		t.Errorf("Faccessat: unexpected error: %v", err)
   704  	}
   705  
   706  	err = os.Symlink("file1", "symlink1")
   707  	if err != nil {
   708  		t.Fatal(err)
   709  	}
   710  
   711  	err = unix.Faccessat(unix.AT_FDCWD, "symlink1", unix.R_OK, unix.AT_SYMLINK_NOFOLLOW)
   712  	if err != nil {
   713  		t.Errorf("Faccessat SYMLINK_NOFOLLOW: unexpected error %v", err)
   714  	}
   715  
   716  	// We can't really test AT_SYMLINK_NOFOLLOW, because there
   717  	// doesn't seem to be any way to change the mode of a symlink.
   718  	// We don't test AT_EACCESS because such tests are only
   719  	// meaningful if run as root.
   720  
   721  	err = unix.Fchmodat(unix.AT_FDCWD, "file1", 0, 0)
   722  	if err != nil {
   723  		t.Errorf("Fchmodat: unexpected error %v", err)
   724  	}
   725  
   726  	err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.F_OK, unix.AT_SYMLINK_NOFOLLOW)
   727  	if err != nil {
   728  		t.Errorf("Faccessat: unexpected error: %v", err)
   729  	}
   730  
   731  	err = unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, unix.AT_SYMLINK_NOFOLLOW)
   732  	if err != unix.EACCES {
   733  		if unix.Getuid() != 0 {
   734  			t.Errorf("Faccessat: unexpected error: %v, want EACCES", err)
   735  		}
   736  	}
   737  }
   738  
   739  func TestSyncFileRange(t *testing.T) {
   740  	file, err := os.Create(filepath.Join(t.TempDir(), t.Name()))
   741  	if err != nil {
   742  		t.Fatal(err)
   743  	}
   744  	defer file.Close()
   745  
   746  	err = unix.SyncFileRange(int(file.Fd()), 0, 0, 0)
   747  	if err == unix.ENOSYS || err == unix.EPERM {
   748  		t.Skip("sync_file_range syscall is not available, skipping test")
   749  	} else if err != nil {
   750  		t.Fatalf("SyncFileRange: %v", err)
   751  	}
   752  
   753  	// invalid flags
   754  	flags := 0xf00
   755  	err = unix.SyncFileRange(int(file.Fd()), 0, 0, flags)
   756  	if err != unix.EINVAL {
   757  		t.Fatalf("SyncFileRange: unexpected error: %v, want EINVAL", err)
   758  	}
   759  }
   760  
   761  func TestClockNanosleep(t *testing.T) {
   762  	delay := 50 * time.Millisecond
   763  
   764  	// Relative timespec.
   765  	start := time.Now()
   766  	rel := unix.NsecToTimespec(delay.Nanoseconds())
   767  	remain := unix.Timespec{}
   768  	for {
   769  		err := unix.ClockNanosleep(unix.CLOCK_MONOTONIC, 0, &rel, &remain)
   770  		if err == unix.ENOSYS || err == unix.EPERM {
   771  			t.Skip("clock_nanosleep syscall is not available, skipping test")
   772  		} else if err == unix.EINTR {
   773  			t.Logf("ClockNanosleep interrupted after %v", time.Since(start))
   774  			rel = remain
   775  			continue
   776  		} else if err != nil {
   777  			t.Errorf("ClockNanosleep(CLOCK_MONOTONIC, 0, %#v, nil) = %v", &rel, err)
   778  		} else if slept := time.Since(start); slept < delay {
   779  			t.Errorf("ClockNanosleep(CLOCK_MONOTONIC, 0, %#v, nil) slept only %v", &rel, slept)
   780  		}
   781  		break
   782  	}
   783  
   784  	// Absolute timespec.
   785  	for {
   786  		start = time.Now()
   787  		until := start.Add(delay)
   788  		abs := unix.NsecToTimespec(until.UnixNano())
   789  		err := unix.ClockNanosleep(unix.CLOCK_REALTIME, unix.TIMER_ABSTIME, &abs, nil)
   790  		if err == unix.EINTR {
   791  			t.Logf("ClockNanosleep interrupted after %v", time.Since(start))
   792  			continue
   793  		} else if err != nil {
   794  			t.Errorf("ClockNanosleep(CLOCK_REALTIME, TIMER_ABSTIME, %#v (=%v), nil) = %v", &abs, until, err)
   795  		} else {
   796  			// We asked for CLOCK_REALTIME, but we have no way to know whether it
   797  			// jumped backward after ClockNanosleep returned. Compare both ways,
   798  			// and only fail if both the monotonic and wall clocks agree that
   799  			// the elapsed sleep was too short.
   800  			//
   801  			// This can still theoretically fail spuriously: if the clock jumps
   802  			// forward during ClockNanosleep and then backward again before we can
   803  			// call time.Now, then we could end up with a time that is too short on
   804  			// both the monotonic scale (because of the forward jump) and the
   805  			// real-time scale (because of the backward jump. However, it seems
   806  			// unlikely that two such contrary jumps will ever occur in the time it
   807  			// takes to execute this test.
   808  			if now := time.Now(); now.Before(until) && now.Round(0).Before(until) {
   809  				t.Errorf("ClockNanosleep(CLOCK_REALTIME, TIMER_ABSTIME, %#v (=%v), nil) slept only until %v", &abs, until, now)
   810  			}
   811  		}
   812  		break
   813  	}
   814  
   815  	// Invalid clock. clock_nanosleep(2) says EINVAL, but it’s actually EOPNOTSUPP.
   816  	err := unix.ClockNanosleep(unix.CLOCK_THREAD_CPUTIME_ID, 0, &rel, nil)
   817  	if err != unix.EINVAL && err != unix.EOPNOTSUPP {
   818  		t.Errorf("ClockNanosleep(CLOCK_THREAD_CPUTIME_ID, 0, %#v, nil) = %v, want EINVAL or EOPNOTSUPP", &rel, err)
   819  	}
   820  }
   821  
   822  func TestOpenByHandleAt(t *testing.T) {
   823  	skipIfNotSupported := func(t *testing.T, name string, err error) {
   824  		if err == unix.EPERM {
   825  			t.Skipf("skipping %s test without CAP_DAC_READ_SEARCH", name)
   826  		}
   827  		if err == unix.ENOSYS {
   828  			t.Skipf("%s system call not available", name)
   829  		}
   830  		if err == unix.EOPNOTSUPP {
   831  			t.Skipf("%s not supported on this filesystem", name)
   832  		}
   833  	}
   834  
   835  	h, mountID, err := unix.NameToHandleAt(unix.AT_FDCWD, "syscall_linux_test.go", 0)
   836  	if err != nil {
   837  		skipIfNotSupported(t, "name_to_handle_at", err)
   838  		t.Fatalf("NameToHandleAt: %v", err)
   839  	}
   840  	t.Logf("mountID: %v, handle: size=%d, type=%d, bytes=%q", mountID,
   841  		h.Size(), h.Type(), h.Bytes())
   842  	mount, err := openMountByID(mountID)
   843  	if err != nil {
   844  		t.Fatalf("openMountByID: %v", err)
   845  	}
   846  	defer mount.Close()
   847  
   848  	for _, clone := range []bool{false, true} {
   849  		t.Run("clone="+strconv.FormatBool(clone), func(t *testing.T) {
   850  			if clone {
   851  				h = unix.NewFileHandle(h.Type(), h.Bytes())
   852  			}
   853  			fd, err := unix.OpenByHandleAt(int(mount.Fd()), h, unix.O_RDONLY)
   854  			skipIfNotSupported(t, "open_by_handle_at", err)
   855  			if err != nil {
   856  				t.Fatalf("OpenByHandleAt: %v", err)
   857  			}
   858  			t.Logf("opened fd %v", fd)
   859  			f := os.NewFile(uintptr(fd), "")
   860  			defer f.Close()
   861  
   862  			slurp, err := io.ReadAll(f)
   863  			if err != nil {
   864  				t.Fatal(err)
   865  			}
   866  			const substr = "Some substring for a test."
   867  			if !strings.Contains(string(slurp), substr) {
   868  				t.Errorf("didn't find substring %q in opened file; read %d bytes", substr, len(slurp))
   869  			}
   870  		})
   871  	}
   872  }
   873  
   874  func openMountByID(mountID int) (f *os.File, err error) {
   875  	mi, err := os.Open("/proc/self/mountinfo")
   876  	if err != nil {
   877  		return nil, err
   878  	}
   879  	defer mi.Close()
   880  	bs := bufio.NewScanner(mi)
   881  	wantPrefix := []byte(fmt.Sprintf("%v ", mountID))
   882  	for bs.Scan() {
   883  		if !bytes.HasPrefix(bs.Bytes(), wantPrefix) {
   884  			continue
   885  		}
   886  		fields := strings.Fields(bs.Text())
   887  		dev := fields[4]
   888  		return os.Open(dev)
   889  	}
   890  	if err := bs.Err(); err != nil {
   891  		return nil, err
   892  	}
   893  	return nil, errors.New("mountID not found")
   894  }
   895  
   896  func TestEpoll(t *testing.T) {
   897  	efd, err := unix.EpollCreate1(unix.EPOLL_CLOEXEC)
   898  	if err != nil {
   899  		t.Fatalf("EpollCreate1: %v", err)
   900  	}
   901  	defer unix.Close(efd)
   902  
   903  	r, w, err := os.Pipe()
   904  	if err != nil {
   905  		t.Fatal(err)
   906  	}
   907  	defer r.Close()
   908  	defer w.Close()
   909  
   910  	fd := int(r.Fd())
   911  	ev := unix.EpollEvent{Events: unix.EPOLLIN, Fd: int32(fd)}
   912  
   913  	err = unix.EpollCtl(efd, unix.EPOLL_CTL_ADD, fd, &ev)
   914  	if err != nil {
   915  		t.Fatalf("EpollCtl: %v", err)
   916  	}
   917  
   918  	if _, err := w.Write([]byte("HELLO GOPHER")); err != nil {
   919  		t.Fatal(err)
   920  	}
   921  
   922  	events := make([]unix.EpollEvent, 128)
   923  	n, err := unix.EpollWait(efd, events, 1)
   924  	if err != nil {
   925  		t.Fatalf("EpollWait: %v", err)
   926  	}
   927  
   928  	if n != 1 {
   929  		t.Errorf("EpollWait: wrong number of events: got %v, expected 1", n)
   930  	}
   931  
   932  	got := int(events[0].Fd)
   933  	if got != fd {
   934  		t.Errorf("EpollWait: wrong Fd in event: got %v, expected %v", got, fd)
   935  	}
   936  }
   937  
   938  func TestPrctlRetInt(t *testing.T) {
   939  	skipc := make(chan bool, 1)
   940  	skip := func() {
   941  		skipc <- true
   942  		runtime.Goexit()
   943  	}
   944  
   945  	go func() {
   946  		// This test uses prctl to modify the calling thread, so run it on its own
   947  		// throwaway thread and do not unlock it when the goroutine exits.
   948  		runtime.LockOSThread()
   949  		defer close(skipc)
   950  
   951  		err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)
   952  		if err != nil {
   953  			t.Logf("Prctl: %v, skipping test", err)
   954  			skip()
   955  		}
   956  
   957  		v, err := unix.PrctlRetInt(unix.PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0)
   958  		if err != nil {
   959  			t.Errorf("failed to perform prctl: %v", err)
   960  		}
   961  		if v != 1 {
   962  			t.Errorf("unexpected return from prctl; got %v, expected %v", v, 1)
   963  		}
   964  	}()
   965  
   966  	if <-skipc {
   967  		t.SkipNow()
   968  	}
   969  }
   970  
   971  func TestTimerfd(t *testing.T) {
   972  	var now unix.Timespec
   973  	if err := unix.ClockGettime(unix.CLOCK_REALTIME, &now); err != nil {
   974  		t.Fatalf("ClockGettime: %v", err)
   975  	}
   976  
   977  	tfd, err := unix.TimerfdCreate(unix.CLOCK_REALTIME, 0)
   978  	if err == unix.ENOSYS {
   979  		t.Skip("timerfd_create system call not implemented")
   980  	} else if err != nil {
   981  		t.Fatalf("TimerfdCreate: %v", err)
   982  	}
   983  	defer unix.Close(tfd)
   984  
   985  	var timeSpec unix.ItimerSpec
   986  	if err := unix.TimerfdGettime(tfd, &timeSpec); err != nil {
   987  		t.Fatalf("TimerfdGettime: %v", err)
   988  	}
   989  
   990  	if timeSpec.Value.Nsec != 0 || timeSpec.Value.Sec != 0 {
   991  		t.Fatalf("TimerfdGettime: timer is already set, but shouldn't be")
   992  	}
   993  
   994  	timeSpec = unix.ItimerSpec{
   995  		Interval: unix.NsecToTimespec(int64(time.Millisecond)),
   996  		Value:    now,
   997  	}
   998  
   999  	if err := unix.TimerfdSettime(tfd, unix.TFD_TIMER_ABSTIME, &timeSpec, nil); err != nil {
  1000  		t.Fatalf("TimerfdSettime: %v", err)
  1001  	}
  1002  
  1003  	const totalTicks = 10
  1004  	const bufferLength = 8
  1005  
  1006  	buffer := make([]byte, bufferLength)
  1007  
  1008  	var count uint64 = 0
  1009  	for count < totalTicks {
  1010  		n, err := unix.Read(tfd, buffer)
  1011  		if err != nil {
  1012  			t.Fatalf("Timerfd: %v", err)
  1013  		} else if n != bufferLength {
  1014  			t.Fatalf("Timerfd: got %d bytes from timerfd, expected %d bytes", n, bufferLength)
  1015  		}
  1016  
  1017  		count += *(*uint64)(unsafe.Pointer(&buffer))
  1018  	}
  1019  }
  1020  
  1021  func TestOpenat2(t *testing.T) {
  1022  	how := &unix.OpenHow{
  1023  		Flags: unix.O_RDONLY,
  1024  	}
  1025  	fd, err := unix.Openat2(unix.AT_FDCWD, ".", how)
  1026  	if err != nil {
  1027  		if err == unix.ENOSYS || err == unix.EPERM {
  1028  			t.Skipf("openat2: %v (old kernel? need Linux >= 5.6)", err)
  1029  		}
  1030  		t.Fatalf("openat2: %v", err)
  1031  	}
  1032  	if err := unix.Close(fd); err != nil {
  1033  		t.Fatalf("close: %v", err)
  1034  	}
  1035  
  1036  	// prepare
  1037  	subdir := filepath.Join(t.TempDir(), "dir")
  1038  	if err := os.Mkdir(subdir, 0755); err != nil {
  1039  		t.Fatal(err)
  1040  	}
  1041  	symlink := filepath.Join(subdir, "symlink")
  1042  	if err := os.Symlink("../", symlink); err != nil {
  1043  		t.Fatal(err)
  1044  	}
  1045  
  1046  	dirfd, err := unix.Open(subdir, unix.O_RDONLY, 0)
  1047  	if err != nil {
  1048  		t.Fatalf("open(%q): %v", subdir, err)
  1049  	}
  1050  	defer unix.Close(dirfd)
  1051  
  1052  	// openat2 with no extra flags -- should succeed
  1053  	fd, err = unix.Openat2(dirfd, "symlink", how)
  1054  	if err != nil {
  1055  		t.Errorf("Openat2 should succeed, got %v", err)
  1056  	}
  1057  	if err := unix.Close(fd); err != nil {
  1058  		t.Fatalf("close: %v", err)
  1059  	}
  1060  
  1061  	// open with RESOLVE_BENEATH, should result in EXDEV
  1062  	how.Resolve = unix.RESOLVE_BENEATH
  1063  	fd, err = unix.Openat2(dirfd, "symlink", how)
  1064  	if err == nil {
  1065  		if err := unix.Close(fd); err != nil {
  1066  			t.Fatalf("close: %v", err)
  1067  		}
  1068  	}
  1069  	if err != unix.EXDEV {
  1070  		t.Errorf("Openat2 should fail with EXDEV, got %v", err)
  1071  	}
  1072  }
  1073  
  1074  func TestIoctlFileDedupeRange(t *testing.T) {
  1075  	dir := t.TempDir()
  1076  	f1, err := os.Create(filepath.Join(dir, "f1"))
  1077  	if err != nil {
  1078  		t.Fatal(err)
  1079  	}
  1080  	defer f1.Close()
  1081  
  1082  	// Test deduplication with two blocks of zeros
  1083  	data := make([]byte, 4096)
  1084  
  1085  	for i := 0; i < 2; i += 1 {
  1086  		_, err = f1.Write(data)
  1087  		if err != nil {
  1088  			t.Fatal(err)
  1089  		}
  1090  	}
  1091  
  1092  	f2, err := os.Create(filepath.Join(dir, "f2"))
  1093  	if err != nil {
  1094  		t.Fatal(err)
  1095  	}
  1096  	defer f2.Close()
  1097  
  1098  	for i := 0; i < 2; i += 1 {
  1099  		// Make the 2nd block different
  1100  		if i == 1 {
  1101  			data[1] = 1
  1102  		}
  1103  
  1104  		_, err = f2.Write(data)
  1105  		if err != nil {
  1106  			t.Fatal(err)
  1107  		}
  1108  	}
  1109  
  1110  	dedupe := unix.FileDedupeRange{
  1111  		Src_offset: uint64(0),
  1112  		Src_length: uint64(4096),
  1113  		Info: []unix.FileDedupeRangeInfo{
  1114  			unix.FileDedupeRangeInfo{
  1115  				Dest_fd:     int64(f2.Fd()),
  1116  				Dest_offset: uint64(0),
  1117  			},
  1118  			unix.FileDedupeRangeInfo{
  1119  				Dest_fd:     int64(f2.Fd()),
  1120  				Dest_offset: uint64(4096),
  1121  			},
  1122  		}}
  1123  
  1124  	err = unix.IoctlFileDedupeRange(int(f1.Fd()), &dedupe)
  1125  	if err == unix.EOPNOTSUPP || err == unix.EINVAL || err == unix.ENOTTY {
  1126  		t.Skip("deduplication not supported on this filesystem")
  1127  	} else if err != nil {
  1128  		t.Fatal(err)
  1129  	}
  1130  
  1131  	// The first Info should be equal
  1132  	if dedupe.Info[0].Status < 0 {
  1133  		errno := unix.Errno(-dedupe.Info[0].Status)
  1134  		if errno == unix.EINVAL || errno == unix.EOPNOTSUPP {
  1135  			t.Skip("deduplication not supported on this filesystem")
  1136  		}
  1137  		t.Errorf("Unexpected error in FileDedupeRange: %s", unix.ErrnoName(errno))
  1138  	} else if dedupe.Info[0].Status == unix.FILE_DEDUPE_RANGE_DIFFERS {
  1139  		t.Errorf("Unexpected different bytes in FileDedupeRange")
  1140  	}
  1141  	if dedupe.Info[0].Bytes_deduped != 4096 {
  1142  		t.Errorf("Unexpected amount of bytes deduped %v != %v",
  1143  			dedupe.Info[0].Bytes_deduped, 4096)
  1144  	}
  1145  
  1146  	// The second Info should be different
  1147  	if dedupe.Info[1].Status < 0 {
  1148  		errno := unix.Errno(-dedupe.Info[1].Status)
  1149  		if errno == unix.EINVAL || errno == unix.EOPNOTSUPP {
  1150  			t.Skip("deduplication not supported on this filesystem")
  1151  		}
  1152  		t.Errorf("Unexpected error in FileDedupeRange: %s", unix.ErrnoName(errno))
  1153  	} else if dedupe.Info[1].Status == unix.FILE_DEDUPE_RANGE_SAME {
  1154  		t.Errorf("Unexpected equal bytes in FileDedupeRange")
  1155  	}
  1156  	if dedupe.Info[1].Bytes_deduped != 0 {
  1157  		t.Errorf("Unexpected amount of bytes deduped %v != %v",
  1158  			dedupe.Info[1].Bytes_deduped, 0)
  1159  	}
  1160  }
  1161  
  1162  // TestPwritevOffsets tests golang.org/issues/57291 where
  1163  // offs2lohi was shifting by the size of long in bytes, not bits.
  1164  func TestPwritevOffsets(t *testing.T) {
  1165  	path := filepath.Join(t.TempDir(), "x.txt")
  1166  
  1167  	f, err := os.Create(path)
  1168  	if err != nil {
  1169  		t.Fatal(err)
  1170  	}
  1171  	t.Cleanup(func() { f.Close() })
  1172  
  1173  	const (
  1174  		off = 20
  1175  	)
  1176  	b := [][]byte{{byte(0)}}
  1177  	n, err := unix.Pwritev(int(f.Fd()), b, off)
  1178  	if err != nil {
  1179  		t.Fatal(err)
  1180  	}
  1181  	if n != len(b) {
  1182  		t.Fatalf("expected to write %d, wrote %d", len(b), n)
  1183  	}
  1184  
  1185  	info, err := f.Stat()
  1186  	if err != nil {
  1187  		t.Fatal(err)
  1188  	}
  1189  	want := off + int64(len(b))
  1190  	if info.Size() != want {
  1191  		t.Fatalf("expected size to be %d, got %d", want, info.Size())
  1192  	}
  1193  }
  1194  
  1195  func TestReadvAllocate(t *testing.T) {
  1196  	f, err := os.Create(filepath.Join(t.TempDir(), "test"))
  1197  	if err != nil {
  1198  		t.Fatal(err)
  1199  	}
  1200  	t.Cleanup(func() { f.Close() })
  1201  
  1202  	test := func(name string, fn func(fd int)) {
  1203  		n := int(testing.AllocsPerRun(100, func() {
  1204  			fn(int(f.Fd()))
  1205  		}))
  1206  		if n != 0 {
  1207  			t.Errorf("%q got %d allocations, want 0", name, n)
  1208  		}
  1209  	}
  1210  
  1211  	iovs := make([][]byte, 8)
  1212  	for i := range iovs {
  1213  		iovs[i] = []byte{'A'}
  1214  	}
  1215  
  1216  	test("Writev", func(fd int) {
  1217  		unix.Writev(fd, iovs)
  1218  	})
  1219  	test("Pwritev", func(fd int) {
  1220  		unix.Pwritev(fd, iovs, 0)
  1221  	})
  1222  	test("Pwritev2", func(fd int) {
  1223  		unix.Pwritev2(fd, iovs, 0, 0)
  1224  	})
  1225  	test("Readv", func(fd int) {
  1226  		unix.Readv(fd, iovs)
  1227  	})
  1228  	test("Preadv", func(fd int) {
  1229  		unix.Preadv(fd, iovs, 0)
  1230  	})
  1231  	test("Preadv2", func(fd int) {
  1232  		unix.Preadv2(fd, iovs, 0, 0)
  1233  	})
  1234  }
  1235  
  1236  func TestSockaddrALG(t *testing.T) {
  1237  	// Open a socket to perform SHA1 hashing.
  1238  	fd, err := unix.Socket(unix.AF_ALG, unix.SOCK_SEQPACKET, 0)
  1239  	if err != nil {
  1240  		t.Skip("socket(AF_ALG):", err)
  1241  	}
  1242  	defer unix.Close(fd)
  1243  	addr := &unix.SockaddrALG{Type: "hash", Name: "sha1"}
  1244  	if err := unix.Bind(fd, addr); err != nil {
  1245  		t.Fatal("bind:", err)
  1246  	}
  1247  	// Need to call accept(2) with the second and third arguments as 0,
  1248  	// which is not possible via unix.Accept, thus the use of unix.Syscall.
  1249  	hashfd, _, errno := unix.Syscall6(unix.SYS_ACCEPT4, uintptr(fd), 0, 0, 0, 0, 0)
  1250  	if errno != 0 {
  1251  		t.Fatal("accept:", errno)
  1252  	}
  1253  
  1254  	hash := os.NewFile(hashfd, "sha1")
  1255  	defer hash.Close()
  1256  
  1257  	// Hash an input string and read the results.
  1258  	const (
  1259  		input = "Hello, world."
  1260  		exp   = "2ae01472317d1935a84797ec1983ae243fc6aa28"
  1261  	)
  1262  	if _, err := hash.WriteString(input); err != nil {
  1263  		t.Fatal(err)
  1264  	}
  1265  	b := make([]byte, 20)
  1266  	if _, err := hash.Read(b); err != nil {
  1267  		t.Fatal(err)
  1268  	}
  1269  	got := hex.EncodeToString(b)
  1270  	if got != exp {
  1271  		t.Fatalf("got: %q, want: %q", got, exp)
  1272  	}
  1273  }
  1274  

View as plain text