summaryrefslogtreecommitdiffstats
path: root/src/os/fifo_test.go
blob: df4b2ee757c6a58ef6f146b0ae37ee9bacf944a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build darwin || dragonfly || freebsd || (linux && !android) || netbsd || openbsd

package os_test

import (
	"errors"
	"internal/syscall/unix"
	"internal/testenv"
	"io/fs"
	"os"
	"path/filepath"
	"strconv"
	"sync"
	"syscall"
	"testing"
)

func TestFifoEOF(t *testing.T) {
	t.Parallel()

	dir := t.TempDir()
	fifoName := filepath.Join(dir, "fifo")
	if err := syscall.Mkfifo(fifoName, 0600); err != nil {
		t.Fatal(err)
	}

	// Per https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html#tag_16_357_03:
	//
	// - “If O_NONBLOCK is clear, an open() for reading-only shall block the
	//   calling thread until a thread opens the file for writing. An open() for
	//   writing-only shall block the calling thread until a thread opens the file
	//   for reading.”
	//
	// In order to unblock both open calls, we open the two ends of the FIFO
	// simultaneously in separate goroutines.

	rc := make(chan *os.File, 1)
	go func() {
		r, err := os.Open(fifoName)
		if err != nil {
			t.Error(err)
		}
		rc <- r
	}()

	w, err := os.OpenFile(fifoName, os.O_WRONLY, 0)
	if err != nil {
		t.Error(err)
	}

	r := <-rc
	if t.Failed() {
		if r != nil {
			r.Close()
		}
		if w != nil {
			w.Close()
		}
		return
	}

	testPipeEOF(t, r, w)
}

// Issue #59545.
func TestNonPollable(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test with tight loops in short mode")
	}

	// We need to open a non-pollable file.
	// This is almost certainly Linux-specific,
	// but if other systems have non-pollable files,
	// we can add them here.
	const nonPollable = "/dev/net/tun"

	f, err := os.OpenFile(nonPollable, os.O_RDWR, 0)
	if err != nil {
		if errors.Is(err, fs.ErrNotExist) || errors.Is(err, fs.ErrPermission) || testenv.SyscallIsNotSupported(err) {
			t.Skipf("can't open %q: %v", nonPollable, err)
		}
		t.Fatal(err)
	}
	f.Close()

	// On a Linux laptop, before the problem was fixed,
	// this test failed about 50% of the time with this
	// number of iterations.
	// It takes about 1/2 second when it passes.
	const attempts = 20000

	start := make(chan bool)
	var wg sync.WaitGroup
	wg.Add(1)
	defer wg.Wait()
	go func() {
		defer wg.Done()
		close(start)
		for i := 0; i < attempts; i++ {
			f, err := os.OpenFile(nonPollable, os.O_RDWR, 0)
			if err != nil {
				t.Error(err)
				return
			}
			if err := f.Close(); err != nil {
				t.Error(err)
				return
			}
		}
	}()

	dir := t.TempDir()
	<-start
	for i := 0; i < attempts; i++ {
		name := filepath.Join(dir, strconv.Itoa(i))
		if err := syscall.Mkfifo(name, 0o600); err != nil {
			t.Fatal(err)
		}
		// The problem only occurs if we use O_NONBLOCK here.
		rd, err := os.OpenFile(name, os.O_RDONLY|syscall.O_NONBLOCK, 0o600)
		if err != nil {
			t.Fatal(err)
		}
		wr, err := os.OpenFile(name, os.O_WRONLY|syscall.O_NONBLOCK, 0o600)
		if err != nil {
			t.Fatal(err)
		}
		const msg = "message"
		if _, err := wr.Write([]byte(msg)); err != nil {
			if errors.Is(err, syscall.EAGAIN) || errors.Is(err, syscall.ENOBUFS) {
				t.Logf("ignoring write error %v", err)
				rd.Close()
				wr.Close()
				continue
			}
			t.Fatalf("write to fifo %d failed: %v", i, err)
		}
		if _, err := rd.Read(make([]byte, len(msg))); err != nil {
			if errors.Is(err, syscall.EAGAIN) || errors.Is(err, syscall.ENOBUFS) {
				t.Logf("ignoring read error %v", err)
				rd.Close()
				wr.Close()
				continue
			}
			t.Fatalf("read from fifo %d failed; %v", i, err)
		}
		if err := rd.Close(); err != nil {
			t.Fatal(err)
		}
		if err := wr.Close(); err != nil {
			t.Fatal(err)
		}
	}
}

// Issue 60211.
func TestOpenFileNonBlocking(t *testing.T) {
	exe, err := os.Executable()
	if err != nil {
		t.Skipf("can't find executable: %v", err)
	}
	f, err := os.OpenFile(exe, os.O_RDONLY|syscall.O_NONBLOCK, 0666)
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	nonblock, err := unix.IsNonblock(int(f.Fd()))
	if err != nil {
		t.Fatal(err)
	}
	if !nonblock {
		t.Errorf("file opened with O_NONBLOCK but in blocking mode")
	}
}

func TestNewFileNonBlocking(t *testing.T) {
	var p [2]int
	if err := syscall.Pipe(p[:]); err != nil {
		t.Fatal(err)
	}
	if err := syscall.SetNonblock(p[0], true); err != nil {
		t.Fatal(err)
	}
	f := os.NewFile(uintptr(p[0]), "pipe")
	nonblock, err := unix.IsNonblock(p[0])
	if err != nil {
		t.Fatal(err)
	}
	if !nonblock {
		t.Error("pipe blocking after NewFile")
	}
	fd := f.Fd()
	if fd != uintptr(p[0]) {
		t.Errorf("Fd returned %d, want %d", fd, p[0])
	}
	nonblock, err = unix.IsNonblock(p[0])
	if err != nil {
		t.Fatal(err)
	}
	if !nonblock {
		t.Error("pipe blocking after Fd")
	}
}