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