| 1 |
// Copyright 2017 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 solaris |
| 6 |
|
| 7 |
package unix_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"net" |
| 12 |
"os" |
| 13 |
"os/exec" |
| 14 |
"path/filepath" |
| 15 |
"runtime" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"golang.org/x/sys/unix" |
| 20 |
) |
| 21 |
|
| 22 |
// getOneRetry wraps EventPort.GetOne which in turn wraps a syscall that can be |
| 23 |
// interrupted causing us to receive EINTR. |
| 24 |
// To prevent our tests from flaking, we retry the syscall until it works |
| 25 |
// rather than get unexpected results in our tests. |
| 26 |
func getOneRetry(t *testing.T, p *unix.EventPort, timeout *unix.Timespec) (e *unix.PortEvent, err error) { |
| 27 |
t.Helper() |
| 28 |
for { |
| 29 |
e, err = p.GetOne(timeout) |
| 30 |
if err != unix.EINTR { |
| 31 |
break |
| 32 |
} |
| 33 |
} |
| 34 |
return e, err |
| 35 |
} |
| 36 |
|
| 37 |
// getRetry wraps EventPort.Get which in turn wraps a syscall that can be |
| 38 |
// interrupted causing us to receive EINTR. |
| 39 |
// To prevent our tests from flaking, we retry the syscall until it works |
| 40 |
// rather than get unexpected results in our tests. |
| 41 |
func getRetry(t *testing.T, p *unix.EventPort, s []unix.PortEvent, min int, timeout *unix.Timespec) (n int, err error) { |
| 42 |
t.Helper() |
| 43 |
for { |
| 44 |
n, err = p.Get(s, min, timeout) |
| 45 |
if err != unix.EINTR { |
| 46 |
break |
| 47 |
} |
| 48 |
// If we did get EINTR, make sure we got 0 events |
| 49 |
if n != 0 { |
| 50 |
t.Fatalf("EventPort.Get returned events on EINTR.\ngot: %d\nexpected: 0", n) |
| 51 |
} |
| 52 |
} |
| 53 |
return n, err |
| 54 |
} |
| 55 |
|
| 56 |
func TestStatvfs(t *testing.T) { |
| 57 |
if err := unix.Statvfs("", nil); err == nil { |
| 58 |
t.Fatal(`Statvfs("") expected failure`) |
| 59 |
} |
| 60 |
|
| 61 |
statvfs := unix.Statvfs_t{} |
| 62 |
if err := unix.Statvfs("/", &statvfs); err != nil { |
| 63 |
t.Errorf(`Statvfs("/") failed: %v`, err) |
| 64 |
} |
| 65 |
|
| 66 |
if t.Failed() { |
| 67 |
mount, err := exec.Command("mount").CombinedOutput() |
| 68 |
if err != nil { |
| 69 |
t.Logf("mount: %v\n%s", err, mount) |
| 70 |
} else { |
| 71 |
t.Logf("mount: %s", mount) |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestSysconf(t *testing.T) { |
| 77 |
n, err := unix.Sysconf(3 /* SC_CLK_TCK */) |
| 78 |
if err != nil { |
| 79 |
t.Fatalf("Sysconf: %v", err) |
| 80 |
} |
| 81 |
t.Logf("Sysconf(SC_CLK_TCK) = %d", n) |
| 82 |
} |
| 83 |
|
| 84 |
// Event Ports |
| 85 |
|
| 86 |
func TestBasicEventPort(t *testing.T) { |
| 87 |
tmpfile, err := os.Create(filepath.Join(t.TempDir(), "eventport")) |
| 88 |
if err != nil { |
| 89 |
t.Fatal(err) |
| 90 |
} |
| 91 |
defer tmpfile.Close() |
| 92 |
path := tmpfile.Name() |
| 93 |
|
| 94 |
stat, err := os.Stat(path) |
| 95 |
if err != nil { |
| 96 |
t.Fatalf("Failed to stat %s: %v", path, err) |
| 97 |
} |
| 98 |
port, err := unix.NewEventPort() |
| 99 |
if err != nil { |
| 100 |
t.Fatalf("NewEventPort failed: %v", err) |
| 101 |
} |
| 102 |
defer port.Close() |
| 103 |
cookie := stat.Mode() |
| 104 |
err = port.AssociatePath(path, stat, unix.FILE_MODIFIED, cookie) |
| 105 |
if err != nil { |
| 106 |
t.Errorf("AssociatePath failed: %v", err) |
| 107 |
} |
| 108 |
if !port.PathIsWatched(path) { |
| 109 |
t.Errorf("PathIsWatched unexpectedly returned false") |
| 110 |
} |
| 111 |
err = port.DissociatePath(path) |
| 112 |
if err != nil { |
| 113 |
t.Errorf("DissociatePath failed: %v", err) |
| 114 |
} |
| 115 |
err = port.AssociatePath(path, stat, unix.FILE_MODIFIED, cookie) |
| 116 |
if err != nil { |
| 117 |
t.Errorf("AssociatePath failed: %v", err) |
| 118 |
} |
| 119 |
bs := []byte{42} |
| 120 |
tmpfile.Write(bs) |
| 121 |
timeout := new(unix.Timespec) |
| 122 |
timeout.Nsec = 100 |
| 123 |
pevent, err := getOneRetry(t, port, timeout) |
| 124 |
if err == unix.ETIME { |
| 125 |
t.Errorf("GetOne timed out: %v", err) |
| 126 |
} |
| 127 |
if err != nil { |
| 128 |
t.Fatalf("GetOne failed: %v", err) |
| 129 |
} |
| 130 |
if pevent.Path != path { |
| 131 |
t.Errorf("Path mismatch: %v != %v", pevent.Path, path) |
| 132 |
} |
| 133 |
err = port.AssociatePath(path, stat, unix.FILE_MODIFIED, cookie) |
| 134 |
if err != nil { |
| 135 |
t.Errorf("AssociatePath failed: %v", err) |
| 136 |
} |
| 137 |
err = port.AssociatePath(path, stat, unix.FILE_MODIFIED, cookie) |
| 138 |
if err == nil { |
| 139 |
t.Errorf("Unexpected success associating already associated path") |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestEventPortFds(t *testing.T) { |
| 144 |
_, path, _, _ := runtime.Caller(0) |
| 145 |
stat, err := os.Stat(path) |
| 146 |
cookie := stat.Mode() |
| 147 |
port, err := unix.NewEventPort() |
| 148 |
if err != nil { |
| 149 |
t.Errorf("NewEventPort failed: %v", err) |
| 150 |
} |
| 151 |
defer port.Close() |
| 152 |
r, w, err := os.Pipe() |
| 153 |
if err != nil { |
| 154 |
t.Errorf("unable to create a pipe: %v", err) |
| 155 |
} |
| 156 |
defer w.Close() |
| 157 |
defer r.Close() |
| 158 |
fd := r.Fd() |
| 159 |
|
| 160 |
port.AssociateFd(fd, unix.POLLIN, cookie) |
| 161 |
if !port.FdIsWatched(fd) { |
| 162 |
t.Errorf("FdIsWatched unexpectedly returned false") |
| 163 |
} |
| 164 |
err = port.DissociateFd(fd) |
| 165 |
err = port.AssociateFd(fd, unix.POLLIN, cookie) |
| 166 |
bs := []byte{42} |
| 167 |
w.Write(bs) |
| 168 |
n, err := port.Pending() |
| 169 |
if n != 1 { |
| 170 |
t.Errorf("Pending() failed: %v, %v", n, err) |
| 171 |
} |
| 172 |
timeout := new(unix.Timespec) |
| 173 |
timeout.Nsec = 100 |
| 174 |
pevent, err := getOneRetry(t, port, timeout) |
| 175 |
if err == unix.ETIME { |
| 176 |
t.Errorf("GetOne timed out: %v", err) |
| 177 |
} |
| 178 |
if err != nil { |
| 179 |
t.Fatalf("GetOne failed: %v", err) |
| 180 |
} |
| 181 |
if pevent.Fd != fd { |
| 182 |
t.Errorf("Fd mismatch: %v != %v", pevent.Fd, fd) |
| 183 |
} |
| 184 |
var c = pevent.Cookie |
| 185 |
if c == nil { |
| 186 |
t.Errorf("Cookie missing: %v != %v", cookie, c) |
| 187 |
return |
| 188 |
} |
| 189 |
if c != cookie { |
| 190 |
t.Errorf("Cookie mismatch: %v != %v", cookie, c) |
| 191 |
} |
| 192 |
port.AssociateFd(fd, unix.POLLIN, cookie) |
| 193 |
err = port.AssociateFd(fd, unix.POLLIN, cookie) |
| 194 |
if err == nil { |
| 195 |
t.Errorf("unexpected success associating already associated fd") |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func TestEventPortErrors(t *testing.T) { |
| 200 |
tmpfile, err := os.CreateTemp("", "eventport") |
| 201 |
if err != nil { |
| 202 |
t.Errorf("unable to create a tempfile: %v", err) |
| 203 |
} |
| 204 |
path := tmpfile.Name() |
| 205 |
stat, _ := os.Stat(path) |
| 206 |
os.Remove(path) |
| 207 |
port, _ := unix.NewEventPort() |
| 208 |
defer port.Close() |
| 209 |
err = port.AssociatePath(path, stat, unix.FILE_MODIFIED, nil) |
| 210 |
if err == nil { |
| 211 |
t.Errorf("unexpected success associating nonexistent file") |
| 212 |
} |
| 213 |
err = port.DissociatePath(path) |
| 214 |
if err == nil { |
| 215 |
t.Errorf("unexpected success dissociating unassociated path") |
| 216 |
} |
| 217 |
timeout := new(unix.Timespec) |
| 218 |
timeout.Nsec = 1 |
| 219 |
_, err = getOneRetry(t, port, timeout) |
| 220 |
if err != unix.ETIME { |
| 221 |
t.Errorf("port.GetOne(%v) returned error %v, want %v", timeout, err, unix.ETIME) |
| 222 |
} |
| 223 |
err = port.DissociateFd(uintptr(0)) |
| 224 |
if err == nil { |
| 225 |
t.Errorf("unexpected success dissociating unassociated fd") |
| 226 |
} |
| 227 |
events := make([]unix.PortEvent, 4) |
| 228 |
_, err = getRetry(t, port, events, 5, nil) |
| 229 |
if err == nil { |
| 230 |
t.Errorf("unexpected success calling Get with min greater than len of slice") |
| 231 |
} |
| 232 |
_, err = getRetry(t, port, nil, 1, nil) |
| 233 |
if err == nil { |
| 234 |
t.Errorf("unexpected success calling Get with nil slice") |
| 235 |
} |
| 236 |
_, err = getRetry(t, port, nil, 0, nil) |
| 237 |
if err == nil { |
| 238 |
t.Errorf("unexpected success calling Get with nil slice") |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
func ExamplePortEvent() { |
| 243 |
type MyCookie struct { |
| 244 |
Name string |
| 245 |
} |
| 246 |
cookie := MyCookie{"Cookie Monster"} |
| 247 |
port, err := unix.NewEventPort() |
| 248 |
if err != nil { |
| 249 |
fmt.Printf("NewEventPort failed: %v\n", err) |
| 250 |
return |
| 251 |
} |
| 252 |
defer port.Close() |
| 253 |
r, w, err := os.Pipe() |
| 254 |
if err != nil { |
| 255 |
fmt.Printf("os.Pipe() failed: %v\n", err) |
| 256 |
return |
| 257 |
} |
| 258 |
defer w.Close() |
| 259 |
defer r.Close() |
| 260 |
fd := r.Fd() |
| 261 |
|
| 262 |
port.AssociateFd(fd, unix.POLLIN, cookie) |
| 263 |
|
| 264 |
bs := []byte{42} |
| 265 |
w.Write(bs) |
| 266 |
timeout := new(unix.Timespec) |
| 267 |
timeout.Sec = 1 |
| 268 |
var pevent *unix.PortEvent |
| 269 |
for { |
| 270 |
pevent, err = port.GetOne(timeout) |
| 271 |
if err != unix.EINTR { |
| 272 |
break |
| 273 |
} |
| 274 |
} |
| 275 |
if err != nil { |
| 276 |
fmt.Printf("didn't get the expected event: %v\n", err) |
| 277 |
} |
| 278 |
|
| 279 |
// Use a type assertion to convert the received cookie back to its original type |
| 280 |
c := pevent.Cookie.(MyCookie) |
| 281 |
fmt.Printf("%s", c.Name) |
| 282 |
//Output: Cookie Monster |
| 283 |
} |
| 284 |
|
| 285 |
func TestPortEventSlices(t *testing.T) { |
| 286 |
port, err := unix.NewEventPort() |
| 287 |
if err != nil { |
| 288 |
t.Fatalf("NewEventPort failed: %v", err) |
| 289 |
} |
| 290 |
// Create, associate, and delete 6 files |
| 291 |
for i := 0; i < 6; i++ { |
| 292 |
tmpfile, err := os.CreateTemp("", "eventport") |
| 293 |
if err != nil { |
| 294 |
t.Fatalf("unable to create tempfile: %v", err) |
| 295 |
} |
| 296 |
path := tmpfile.Name() |
| 297 |
stat, err := os.Stat(path) |
| 298 |
if err != nil { |
| 299 |
t.Fatalf("unable to stat tempfile: %v", err) |
| 300 |
} |
| 301 |
err = port.AssociatePath(path, stat, unix.FILE_MODIFIED, nil) |
| 302 |
if err != nil { |
| 303 |
t.Fatalf("unable to AssociatePath tempfile: %v", err) |
| 304 |
} |
| 305 |
err = os.Remove(path) |
| 306 |
if err != nil { |
| 307 |
t.Fatalf("unable to Remove tempfile: %v", err) |
| 308 |
} |
| 309 |
} |
| 310 |
n, err := port.Pending() |
| 311 |
if err != nil { |
| 312 |
t.Errorf("Pending failed: %v", err) |
| 313 |
} |
| 314 |
if n != 6 { |
| 315 |
t.Errorf("expected 6 pending events, got %d", n) |
| 316 |
} |
| 317 |
timeout := new(unix.Timespec) |
| 318 |
timeout.Nsec = 1 |
| 319 |
events := make([]unix.PortEvent, 4) |
| 320 |
n, err = getRetry(t, port, events, 3, timeout) |
| 321 |
if err != nil { |
| 322 |
t.Errorf("Get failed: %v", err) |
| 323 |
} |
| 324 |
if n != 4 { |
| 325 |
t.Errorf("expected 4 events, got %d", n) |
| 326 |
} |
| 327 |
e := events[:n] |
| 328 |
for _, p := range e { |
| 329 |
if p.Events != unix.FILE_DELETE { |
| 330 |
t.Errorf("unexpected event. got %v, expected %v", p.Events, unix.FILE_DELETE) |
| 331 |
} |
| 332 |
} |
| 333 |
n, err = getRetry(t, port, events, 3, timeout) |
| 334 |
if err != unix.ETIME { |
| 335 |
t.Errorf("unexpected error. got %v, expected %v", err, unix.ETIME) |
| 336 |
} |
| 337 |
if n != 2 { |
| 338 |
t.Errorf("expected 2 events, got %d", n) |
| 339 |
} |
| 340 |
e = events[:n] |
| 341 |
for _, p := range e { |
| 342 |
if p.Events != unix.FILE_DELETE { |
| 343 |
t.Errorf("unexpected event. got %v, expected %v", p.Events, unix.FILE_DELETE) |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
r, w, err := os.Pipe() |
| 348 |
if err != nil { |
| 349 |
t.Fatalf("unable to create a pipe: %v", err) |
| 350 |
} |
| 351 |
port.AssociateFd(r.Fd(), unix.POLLIN, nil) |
| 352 |
port.AssociateFd(w.Fd(), unix.POLLOUT, nil) |
| 353 |
bs := []byte{41} |
| 354 |
w.Write(bs) |
| 355 |
|
| 356 |
n, err = getRetry(t, port, events, 1, timeout) |
| 357 |
if err != nil { |
| 358 |
t.Errorf("Get failed: %v", err) |
| 359 |
} |
| 360 |
if n != 2 { |
| 361 |
t.Errorf("expected 2 events, got %d", n) |
| 362 |
} |
| 363 |
err = w.Close() |
| 364 |
if err != nil { |
| 365 |
t.Errorf("w.Close() failed: %v", err) |
| 366 |
} |
| 367 |
err = r.Close() |
| 368 |
if err != nil { |
| 369 |
t.Errorf("r.Close() failed: %v", err) |
| 370 |
} |
| 371 |
err = port.Close() |
| 372 |
if err != nil { |
| 373 |
t.Errorf("port.Close() failed: %v", err) |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
func TestLifreqSetName(t *testing.T) { |
| 378 |
var l unix.Lifreq |
| 379 |
err := l.SetName("12345678901234356789012345678901234567890") |
| 380 |
if err == nil { |
| 381 |
t.Fatal(`Lifreq.SetName should reject names that are too long`) |
| 382 |
} |
| 383 |
err = l.SetName("tun0") |
| 384 |
if err != nil { |
| 385 |
t.Errorf(`Lifreq.SetName("tun0") failed: %v`, err) |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
func TestLifreqGetMTU(t *testing.T) { |
| 390 |
// Find links and their MTU using CLI tooling |
| 391 |
// $ dladm show-link -p -o link,mtu |
| 392 |
// net0:1500 |
| 393 |
out, err := exec.Command("dladm", "show-link", "-p", "-o", "link,mtu").Output() |
| 394 |
if err != nil { |
| 395 |
t.Fatalf("unable to use dladm to find data links: %v", err) |
| 396 |
} |
| 397 |
lines := strings.Split(string(out), "\n") |
| 398 |
tc := make(map[string]string) |
| 399 |
for _, line := range lines { |
| 400 |
v := strings.Split(line, ":") |
| 401 |
if len(v) == 2 { |
| 402 |
tc[v[0]] = v[1] |
| 403 |
} |
| 404 |
} |
| 405 |
ip_fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0) |
| 406 |
if err != nil { |
| 407 |
t.Fatalf("could not open udp socket: %v", err) |
| 408 |
} |
| 409 |
var l unix.Lifreq |
| 410 |
for link, mtu := range tc { |
| 411 |
err = l.SetName(link) |
| 412 |
if err != nil { |
| 413 |
t.Fatalf("Lifreq.SetName(%q) failed: %v", link, err) |
| 414 |
} |
| 415 |
if err = unix.IoctlLifreq(ip_fd, unix.SIOCGLIFMTU, &l); err != nil { |
| 416 |
t.Fatalf("unable to SIOCGLIFMTU: %v", err) |
| 417 |
} |
| 418 |
m := l.GetLifruUint() |
| 419 |
if fmt.Sprintf("%d", m) != mtu { |
| 420 |
t.Errorf("unable to read MTU correctly: expected %s, got %d", mtu, m) |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
func TestUcredGet(t *testing.T) { |
| 426 |
euid := unix.Geteuid() |
| 427 |
creds, err := unix.UcredGet(unix.Getpid()) |
| 428 |
if err != nil { |
| 429 |
t.Fatalf("unix.UcredGet failed: %v", err) |
| 430 |
} |
| 431 |
if euid != creds.Geteuid() { |
| 432 |
t.Fatalf("mismatched euid") |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
func TestGetPeerUcred(t *testing.T) { |
| 437 |
d := t.TempDir() |
| 438 |
path := filepath.Join(d, "foo.sock") |
| 439 |
sock, err := net.Listen("unix", path) |
| 440 |
if err != nil { |
| 441 |
t.Fatalf("net.Listen: %v", err) |
| 442 |
} |
| 443 |
defer sock.Close() |
| 444 |
|
| 445 |
c1, err := net.Dial("unix", path) |
| 446 |
if err != nil { |
| 447 |
t.Error(err) |
| 448 |
return |
| 449 |
} |
| 450 |
defer c1.Close() |
| 451 |
|
| 452 |
c2, err := sock.Accept() |
| 453 |
if err != nil { |
| 454 |
t.Fatalf("Accept: %v", err) |
| 455 |
} |
| 456 |
defer c2.Close() |
| 457 |
|
| 458 |
switch unixconn := c2.(type) { |
| 459 |
case *net.UnixConn: |
| 460 |
raw, err := unixconn.SyscallConn() |
| 461 |
if err != nil { |
| 462 |
t.Fatalf("SyscallConn failed: %v", err) |
| 463 |
} |
| 464 |
|
| 465 |
var creds *unix.Ucred |
| 466 |
cerr := raw.Control(func(fd uintptr) { |
| 467 |
creds, err = unix.GetPeerUcred(fd) |
| 468 |
if err != nil { |
| 469 |
err = fmt.Errorf("unix.GetPeerUcred: %w", err) |
| 470 |
return |
| 471 |
} |
| 472 |
}) |
| 473 |
if cerr != nil { |
| 474 |
t.Fatalf("raw.Control failed: %v", err) |
| 475 |
} |
| 476 |
if creds == nil { |
| 477 |
t.Fatalf("Got a nil Ucred response") |
| 478 |
} |
| 479 |
euid := unix.Geteuid() |
| 480 |
if euid != creds.Geteuid() { |
| 481 |
t.Fatalf("mismatched euid") |
| 482 |
} |
| 483 |
default: |
| 484 |
t.Fatalf("Somehow didn't get a UnixConn") |
| 485 |
} |
| 486 |
} |