Debian Codegraph @

conn.go

raw
1 package socket
2
3 import (
4 "context"
5 "errors"
6 "io"
7 "os"
8 "sync"
9 "sync/atomic"
10 "syscall"
11 "time"
12
13 "golang.org/x/sys/unix"
14 )
15
16 // Lock in an expected public interface for convenience.
17 var _ interface {
18 io.ReadWriteCloser
19 syscall.Conn
20 SetDeadline(t time.Time) error
21 SetReadDeadline(t time.Time) error
22 SetWriteDeadline(t time.Time) error
23 } = &Conn{}
24
25 // A Conn is a low-level network connection which integrates with Go's runtime
26 // network poller to provide asynchronous I/O and deadline support.
27 //
28 // Many of a Conn's blocking methods support net.Conn deadlines as well as
29 // cancelation via context. Note that passing a context with a deadline set will
30 // override any of the previous deadlines set by calls to the SetDeadline family
31 // of methods.
32 type Conn struct {
33 // Indicates whether or not Conn.Close has been called. Must be accessed
34 // atomically. Atomics definitions must come first in the Conn struct.
35 closed uint32
36
37 // A unique name for the Conn which is also associated with derived file
38 // descriptors such as those created by accept(2).
39 name string
40
41 // facts contains information we have determined about Conn to trigger
42 // alternate behavior in certain functions.
43 facts facts
44
45 // Provides access to the underlying file registered with the runtime
46 // network poller, and arbitrary raw I/O calls.
47 fd *os.File
48 rc syscall.RawConn
49 }
50
51 // facts contains facts about a Conn.
52 type facts struct {
53 // isStream reports whether this is a streaming descriptor, as opposed to a
54 // packet-based descriptor like a UDP socket.
55 isStream bool
56
57 // zeroReadIsEOF reports Whether a zero byte read indicates EOF. This is
58 // false for a message based socket connection.
59 zeroReadIsEOF bool
60 }
61
62 // A Config contains options for a Conn.
63 type Config struct {
64 // NetNS specifies the Linux network namespace the Conn will operate in.
65 // This option is unsupported on other operating systems.
66 //
67 // If set (non-zero), Conn will enter the specified network namespace and an
68 // error will occur in Socket if the operation fails.
69 //
70 // If not set (zero), a best-effort attempt will be made to enter the
71 // network namespace of the calling thread: this means that any changes made
72 // to the calling thread's network namespace will also be reflected in Conn.
73 // If this operation fails (due to lack of permissions or because network
74 // namespaces are disabled by kernel configuration), Socket will not return
75 // an error, and the Conn will operate in the default network namespace of
76 // the process. This enables non-privileged use of Conn in applications
77 // which do not require elevated privileges.
78 //
79 // Entering a network namespace is a privileged operation (root or
80 // CAP_SYS_ADMIN are required), and most applications should leave this set
81 // to 0.
82 NetNS int
83 }
84
85 // High-level methods which provide convenience over raw system calls.
86
87 // Close closes the underlying file descriptor for the Conn, which also causes
88 // all in-flight I/O operations to immediately unblock and return errors. Any
89 // subsequent uses of Conn will result in EBADF.
90 func (c *Conn) Close() error {
91 // The caller has expressed an intent to close the socket, so immediately
92 // increment s.closed to force further calls to result in EBADF before also
93 // closing the file descriptor to unblock any outstanding operations.
94 //
95 // Because other operations simply check for s.closed != 0, we will permit
96 // double Close, which would increment s.closed beyond 1.
97 if atomic.AddUint32(&c.closed, 1) != 1 {
98 // Multiple Close calls.
99 return nil
100 }
101
102 return os.NewSyscallError("close", c.fd.Close())
103 }
104
105 // CloseRead shuts down the reading side of the Conn. Most callers should just
106 // use Close.
107 func (c *Conn) CloseRead() error { return c.Shutdown(unix.SHUT_RD) }
108
109 // CloseWrite shuts down the writing side of the Conn. Most callers should just
110 // use Close.
111 func (c *Conn) CloseWrite() error { return c.Shutdown(unix.SHUT_WR) }
112
113 // Read reads directly from the underlying file descriptor.
114 func (c *Conn) Read(b []byte) (int, error) { return c.fd.Read(b) }
115
116 // ReadContext reads from the underlying file descriptor with added support for
117 // context cancelation.
118 func (c *Conn) ReadContext(ctx context.Context, b []byte) (int, error) {
119 if c.facts.isStream && len(b) > maxRW {
120 b = b[:maxRW]
121 }
122
123 n, err := readT(ctx, c, "read", func(fd int) (int, error) {
124 return unix.Read(fd, b)
125 })
126 if n == 0 && err == nil && c.facts.zeroReadIsEOF {
127 return 0, io.EOF
128 }
129
130 return n, os.NewSyscallError("read", err)
131 }
132
133 // Write writes directly to the underlying file descriptor.
134 func (c *Conn) Write(b []byte) (int, error) { return c.fd.Write(b) }
135
136 // WriteContext writes to the underlying file descriptor with added support for
137 // context cancelation.
138 func (c *Conn) WriteContext(ctx context.Context, b []byte) (int, error) {
139 var (
140 n, nn int
141 err error
142 )
143
144 doErr := c.write(ctx, "write", func(fd int) error {
145 lenb := len(b)
146 if c.facts.isStream && lenb-nn > maxRW {
147 lenb = nn + maxRW
148 }
149
150 n, err = unix.Write(fd, b[nn:lenb])
151 if n > 0 {
152 nn += n
153 }
154 if nn == len(b) {
155 return err
156 }
157 if n == 0 && err == nil {
158 err = io.ErrUnexpectedEOF
159 return nil
160 }
161
162 return err
163 })
164 if doErr != nil {
165 return 0, doErr
166 }
167
168 return nn, os.NewSyscallError("write", err)
169 }
170
171 // SetDeadline sets both the read and write deadlines associated with the Conn.
172 func (c *Conn) SetDeadline(t time.Time) error { return c.fd.SetDeadline(t) }
173
174 // SetReadDeadline sets the read deadline associated with the Conn.
175 func (c *Conn) SetReadDeadline(t time.Time) error { return c.fd.SetReadDeadline(t) }
176
177 // SetWriteDeadline sets the write deadline associated with the Conn.
178 func (c *Conn) SetWriteDeadline(t time.Time) error { return c.fd.SetWriteDeadline(t) }
179
180 // ReadBuffer gets the size of the operating system's receive buffer associated
181 // with the Conn.
182 func (c *Conn) ReadBuffer() (int, error) {
183 return c.GetsockoptInt(unix.SOL_SOCKET, unix.SO_RCVBUF)
184 }
185
186 // WriteBuffer gets the size of the operating system's transmit buffer
187 // associated with the Conn.
188 func (c *Conn) WriteBuffer() (int, error) {
189 return c.GetsockoptInt(unix.SOL_SOCKET, unix.SO_SNDBUF)
190 }
191
192 // SetReadBuffer sets the size of the operating system's receive buffer
193 // associated with the Conn.
194 //
195 // When called with elevated privileges on Linux, the SO_RCVBUFFORCE option will
196 // be used to override operating system limits. Otherwise SO_RCVBUF is used
197 // (which obeys operating system limits).
198 func (c *Conn) SetReadBuffer(bytes int) error { return c.setReadBuffer(bytes) }
199
200 // SetWriteBuffer sets the size of the operating system's transmit buffer
201 // associated with the Conn.
202 //
203 // When called with elevated privileges on Linux, the SO_SNDBUFFORCE option will
204 // be used to override operating system limits. Otherwise SO_SNDBUF is used
205 // (which obeys operating system limits).
206 func (c *Conn) SetWriteBuffer(bytes int) error { return c.setWriteBuffer(bytes) }
207
208 // SyscallConn returns a raw network connection. This implements the
209 // syscall.Conn interface.
210 //
211 // SyscallConn is intended for advanced use cases, such as getting and setting
212 // arbitrary socket options using the socket's file descriptor. If possible,
213 // those operations should be performed using methods on Conn instead.
214 //
215 // Once invoked, it is the caller's responsibility to ensure that operations
216 // performed using Conn and the syscall.RawConn do not conflict with each other.
217 func (c *Conn) SyscallConn() (syscall.RawConn, error) {
218 if atomic.LoadUint32(&c.closed) != 0 {
219 return nil, os.NewSyscallError("syscallconn", unix.EBADF)
220 }
221
222 // TODO(mdlayher): mutex or similar to enforce syscall.RawConn contract of
223 // FD remaining valid for duration of calls?
224 return c.rc, nil
225 }
226
227 // Socket wraps the socket(2) system call to produce a Conn. domain, typ, and
228 // proto are passed directly to socket(2), and name should be a unique name for
229 // the socket type such as "netlink" or "vsock".
230 //
231 // The cfg parameter specifies optional configuration for the Conn. If nil, no
232 // additional configuration will be applied.
233 //
234 // If the operating system supports SOCK_CLOEXEC and SOCK_NONBLOCK, they are
235 // automatically applied to typ to mirror the standard library's socket flag
236 // behaviors.
237 func Socket(domain, typ, proto int, name string, cfg *Config) (*Conn, error) {
238 if cfg == nil {
239 cfg = &Config{}
240 }
241
242 if cfg.NetNS == 0 {
243 // Non-Linux or no network namespace.
244 return socket(domain, typ, proto, name)
245 }
246
247 // Linux only: create Conn in the specified network namespace.
248 return withNetNS(cfg.NetNS, func() (*Conn, error) {
249 return socket(domain, typ, proto, name)
250 })
251 }
252
253 // socket is the internal, cross-platform entry point for socket(2).
254 func socket(domain, typ, proto int, name string) (*Conn, error) {
255 var (
256 fd int
257 err error
258 )
259
260 for {
261 fd, err = unix.Socket(domain, typ|socketFlags, proto)
262 switch {
263 case err == nil:
264 // Some OSes already set CLOEXEC with typ.
265 if !flagCLOEXEC {
266 unix.CloseOnExec(fd)
267 }
268
269 // No error, prepare the Conn.
270 return New(fd, name)
271 case !ready(err):
272 // System call interrupted or not ready, try again.
273 continue
274 case err == unix.EINVAL, err == unix.EPROTONOSUPPORT:
275 // On Linux, SOCK_NONBLOCK and SOCK_CLOEXEC were introduced in
276 // 2.6.27. On FreeBSD, both flags were introduced in FreeBSD 10.
277 // EINVAL and EPROTONOSUPPORT check for earlier versions of these
278 // OSes respectively.
279 //
280 // Mirror what the standard library does when creating file
281 // descriptors: avoid racing a fork/exec with the creation of new
282 // file descriptors, so that child processes do not inherit socket
283 // file descriptors unexpectedly.
284 //
285 // For a more thorough explanation, see similar work in the Go tree:
286 // func sysSocket in net/sock_cloexec.go, as well as the detailed
287 // comment in syscall/exec_unix.go.
288 syscall.ForkLock.RLock()
289 fd, err = unix.Socket(domain, typ, proto)
290 if err != nil {
291 syscall.ForkLock.RUnlock()
292 return nil, os.NewSyscallError("socket", err)
293 }
294 unix.CloseOnExec(fd)
295 syscall.ForkLock.RUnlock()
296
297 return New(fd, name)
298 default:
299 // Unhandled error.
300 return nil, os.NewSyscallError("socket", err)
301 }
302 }
303 }
304
305 // FileConn returns a copy of the network connection corresponding to the open
306 // file. It is the caller's responsibility to close the file when finished.
307 // Closing the Conn does not affect the File, and closing the File does not
308 // affect the Conn.
309 func FileConn(f *os.File, name string) (*Conn, error) {
310 // First we'll try to do fctnl(2) with F_DUPFD_CLOEXEC because we can dup
311 // the file descriptor and set the flag in one syscall.
312 fd, err := unix.FcntlInt(f.Fd(), unix.F_DUPFD_CLOEXEC, 0)
313 switch err {
314 case nil:
315 // OK, ready to set up non-blocking I/O.
316 return New(fd, name)
317 case unix.EINVAL:
318 // The kernel rejected our fcntl(2), fall back to separate dup(2) and
319 // setting close on exec.
320 //
321 // Mirror what the standard library does when creating file descriptors:
322 // avoid racing a fork/exec with the creation of new file descriptors,
323 // so that child processes do not inherit socket file descriptors
324 // unexpectedly.
325 syscall.ForkLock.RLock()
326 fd, err := unix.Dup(fd)
327 if err != nil {
328 syscall.ForkLock.RUnlock()
329 return nil, os.NewSyscallError("dup", err)
330 }
331 unix.CloseOnExec(fd)
332 syscall.ForkLock.RUnlock()
333
334 return New(fd, name)
335 default:
336 // Any other errors.
337 return nil, os.NewSyscallError("fcntl", err)
338 }
339 }
340
341 // New wraps an existing file descriptor to create a Conn. name should be a
342 // unique name for the socket type such as "netlink" or "vsock".
343 //
344 // Most callers should use Socket or FileConn to construct a Conn. New is
345 // intended for integrating with specific system calls which provide a file
346 // descriptor that supports asynchronous I/O. The file descriptor is immediately
347 // set to nonblocking mode and registered with Go's runtime network poller for
348 // future I/O operations.
349 //
350 // Unlike FileConn, New does not duplicate the existing file descriptor in any
351 // way. The returned Conn takes ownership of the underlying file descriptor.
352 func New(fd int, name string) (*Conn, error) {
353 // All Conn I/O is nonblocking for integration with Go's runtime network
354 // poller. Depending on the OS this might already be set but it can't hurt
355 // to set it again.
356 if err := unix.SetNonblock(fd, true); err != nil {
357 return nil, os.NewSyscallError("setnonblock", err)
358 }
359
360 // os.NewFile registers the non-blocking file descriptor with the runtime
361 // poller, which is then used for most subsequent operations except those
362 // that require raw I/O via SyscallConn.
363 //
364 // See also: https://golang.org/pkg/os/#NewFile
365 f := os.NewFile(uintptr(fd), name)
366 rc, err := f.SyscallConn()
367 if err != nil {
368 return nil, err
369 }
370
371 c := &Conn{
372 name: name,
373 fd: f,
374 rc: rc,
375 }
376
377 // Probe the file descriptor for socket settings.
378 sotype, err := c.GetsockoptInt(unix.SOL_SOCKET, unix.SO_TYPE)
379 switch {
380 case err == nil:
381 // File is a socket, check its properties.
382 c.facts = facts{
383 isStream: sotype == unix.SOCK_STREAM,
384 zeroReadIsEOF: sotype != unix.SOCK_DGRAM && sotype != unix.SOCK_RAW,
385 }
386 case errors.Is(err, unix.ENOTSOCK):
387 // File is not a socket, treat it as a regular file.
388 c.facts = facts{
389 isStream: true,
390 zeroReadIsEOF: true,
391 }
392 default:
393 return nil, err
394 }
395
396 return c, nil
397 }
398
399 // Low-level methods which provide raw system call access.
400
401 // Accept wraps accept(2) or accept4(2) depending on the operating system, but
402 // returns a Conn for the accepted connection rather than a raw file descriptor.
403 //
404 // If the operating system supports accept4(2) (which allows flags),
405 // SOCK_CLOEXEC and SOCK_NONBLOCK are automatically applied to flags to mirror
406 // the standard library's socket flag behaviors.
407 //
408 // If the operating system only supports accept(2) (which does not allow flags)
409 // and flags is not zero, an error will be returned.
410 //
411 // Accept obeys context cancelation and uses the deadline set on the context to
412 // cancel accepting the next connection. If a deadline is set on ctx, this
413 // deadline will override any previous deadlines set using SetDeadline or
414 // SetReadDeadline. Upon return, the read deadline is cleared.
415 func (c *Conn) Accept(ctx context.Context, flags int) (*Conn, unix.Sockaddr, error) {
416 type ret struct {
417 nfd int
418 sa unix.Sockaddr
419 }
420
421 r, err := readT(ctx, c, sysAccept, func(fd int) (ret, error) {
422 // Either accept(2) or accept4(2) depending on the OS.
423 nfd, sa, err := accept(fd, flags|socketFlags)
424 return ret{nfd, sa}, err
425 })
426 if err != nil {
427 // internal/poll, context error, or user function error.
428 return nil, nil, err
429 }
430
431 // Successfully accepted a connection, wrap it in a Conn for use by the
432 // caller.
433 ac, err := New(r.nfd, c.name)
434 if err != nil {
435 return nil, nil, err
436 }
437
438 return ac, r.sa, nil
439 }
440
441 // Bind wraps bind(2).
442 func (c *Conn) Bind(sa unix.Sockaddr) error {
443 return c.control("bind", func(fd int) error { return unix.Bind(fd, sa) })
444 }
445
446 // Connect wraps connect(2). In order to verify that the underlying socket is
447 // connected to a remote peer, Connect calls getpeername(2) and returns the
448 // unix.Sockaddr from that call.
449 //
450 // Connect obeys context cancelation and uses the deadline set on the context to
451 // cancel connecting to a remote peer. If a deadline is set on ctx, this
452 // deadline will override any previous deadlines set using SetDeadline or
453 // SetWriteDeadline. Upon return, the write deadline is cleared.
454 func (c *Conn) Connect(ctx context.Context, sa unix.Sockaddr) (unix.Sockaddr, error) {
455 const op = "connect"
456
457 // TODO(mdlayher): it would seem that trying to connect to unbound vsock
458 // listeners by calling Connect multiple times results in ECONNRESET for the
459 // first and nil error for subsequent calls. Do we need to memoize the
460 // error? Check what the stdlib behavior is.
461
462 var (
463 // Track progress between invocations of the write closure. We don't
464 // have an explicit WaitWrite call like internal/poll does, so we have
465 // to wait until the runtime calls the closure again to indicate we can
466 // write.
467 progress atomic.Uint32
468
469 // Capture closure sockaddr and error.
470 rsa unix.Sockaddr
471 err error
472 )
473
474 doErr := c.write(ctx, op, func(fd int) error {
475 if progress.Add(1) == 1 {
476 // First call: initiate connect.
477 return unix.Connect(fd, sa)
478 }
479
480 // Subsequent calls: the runtime network poller indicates fd is
481 // writable. Check for errno.
482 errno, gerr := c.GetsockoptInt(unix.SOL_SOCKET, unix.SO_ERROR)
483 if gerr != nil {
484 return gerr
485 }
486 if errno != 0 {
487 // Connection is still not ready or failed. If errno indicates
488 // the socket is not ready, we will wait for the next write
489 // event. Otherwise we propagate this errno back to the as a
490 // permanent error.
491 uerr := unix.Errno(errno)
492 err = uerr
493 return uerr
494 }
495
496 // According to internal/poll, it's possible for the runtime network
497 // poller to spuriously wake us and return errno 0 for SO_ERROR.
498 // Make sure we are actually connected to a peer.
499 peer, err := c.Getpeername()
500 if err != nil {
501 // internal/poll unconditionally goes back to WaitWrite.
502 // Synthesize an error that will do the same for us.
503 return unix.EAGAIN
504 }
505
506 // Connection complete.
507 rsa = peer
508 return nil
509 })
510 if doErr != nil {
511 // internal/poll or context error.
512 return nil, doErr
513 }
514
515 if err == unix.EISCONN {
516 // TODO(mdlayher): is this block obsolete with the addition of the
517 // getsockopt SO_ERROR check above?
518 //
519 // EISCONN is reported if the socket is already established and should
520 // not be treated as an error.
521 // - Darwin reports this for at least TCP sockets
522 // - Linux reports this for at least AF_VSOCK sockets
523 return rsa, nil
524 }
525
526 return rsa, os.NewSyscallError(op, err)
527 }
528
529 // Getsockname wraps getsockname(2).
530 func (c *Conn) Getsockname() (unix.Sockaddr, error) {
531 return controlT(c, "getsockname", unix.Getsockname)
532 }
533
534 // Getpeername wraps getpeername(2).
535 func (c *Conn) Getpeername() (unix.Sockaddr, error) {
536 return controlT(c, "getpeername", unix.Getpeername)
537 }
538
539 // GetsockoptICMPv6Filter wraps getsockopt(2) for *unix.ICMPv6Filter values.
540 func (c *Conn) GetsockoptICMPv6Filter(level, opt int) (*unix.ICMPv6Filter, error) {
541 return controlT(c, "getsockopt", func(fd int) (*unix.ICMPv6Filter, error) {
542 return unix.GetsockoptICMPv6Filter(fd, level, opt)
543 })
544 }
545
546 // GetsockoptInt wraps getsockopt(2) for integer values.
547 func (c *Conn) GetsockoptInt(level, opt int) (int, error) {
548 return controlT(c, "getsockopt", func(fd int) (int, error) {
549 return unix.GetsockoptInt(fd, level, opt)
550 })
551 }
552
553 // GetsockoptString wraps getsockopt(2) for string values.
554 func (c *Conn) GetsockoptString(level, opt int) (string, error) {
555 return controlT(c, "getsockopt", func(fd int) (string, error) {
556 return unix.GetsockoptString(fd, level, opt)
557 })
558 }
559
560 // Listen wraps listen(2).
561 func (c *Conn) Listen(n int) error {
562 return c.control("listen", func(fd int) error { return unix.Listen(fd, n) })
563 }
564
565 // Recvmsg wraps recvmsg(2).
566 func (c *Conn) Recvmsg(ctx context.Context, p, oob []byte, flags int) (int, int, int, unix.Sockaddr, error) {
567 type ret struct {
568 n, oobn, recvflags int
569 from unix.Sockaddr
570 }
571
572 r, err := readT(ctx, c, "recvmsg", func(fd int) (ret, error) {
573 n, oobn, recvflags, from, err := unix.Recvmsg(fd, p, oob, flags)
574 return ret{n, oobn, recvflags, from}, err
575 })
576 if r.n == 0 && err == nil && c.facts.zeroReadIsEOF {
577 return 0, 0, 0, nil, io.EOF
578 }
579
580 return r.n, r.oobn, r.recvflags, r.from, err
581 }
582
583 // Recvfrom wraps recvfrom(2).
584 func (c *Conn) Recvfrom(ctx context.Context, p []byte, flags int) (int, unix.Sockaddr, error) {
585 type ret struct {
586 n int
587 addr unix.Sockaddr
588 }
589
590 out, err := readT(ctx, c, "recvfrom", func(fd int) (ret, error) {
591 n, addr, err := unix.Recvfrom(fd, p, flags)
592 return ret{n, addr}, err
593 })
594 if out.n == 0 && err == nil && c.facts.zeroReadIsEOF {
595 return 0, nil, io.EOF
596 }
597
598 return out.n, out.addr, err
599 }
600
601 // Sendmsg wraps sendmsg(2).
602 func (c *Conn) Sendmsg(ctx context.Context, p, oob []byte, to unix.Sockaddr, flags int) (int, error) {
603 return writeT(ctx, c, "sendmsg", func(fd int) (int, error) {
604 return unix.SendmsgN(fd, p, oob, to, flags)
605 })
606 }
607
608 // SendmsgBuffers wraps sendmsg(2) with scatter-gather I/O support.
609 func (c *Conn) SendmsgBuffers(ctx context.Context, buffers [][]byte, oob []byte, to unix.Sockaddr, flags int) (int, error) {
610 return writeT(ctx, c, "sendmsg", func(fd int) (int, error) {
611 return unix.SendmsgBuffers(fd, buffers, oob, to, flags)
612 })
613 }
614
615 // Sendto wraps sendto(2).
616 func (c *Conn) Sendto(ctx context.Context, p []byte, flags int, to unix.Sockaddr) error {
617 return c.write(ctx, "sendto", func(fd int) error {
618 return unix.Sendto(fd, p, flags, to)
619 })
620 }
621
622 // SetsockoptICMPv6Filter wraps setsockopt(2) for *unix.ICMPv6Filter values.
623 func (c *Conn) SetsockoptICMPv6Filter(level, opt int, filter *unix.ICMPv6Filter) error {
624 return c.control("setsockopt", func(fd int) error {
625 return unix.SetsockoptICMPv6Filter(fd, level, opt, filter)
626 })
627 }
628
629 // SetsockoptInt wraps setsockopt(2) for integer values.
630 func (c *Conn) SetsockoptInt(level, opt, value int) error {
631 return c.control("setsockopt", func(fd int) error {
632 return unix.SetsockoptInt(fd, level, opt, value)
633 })
634 }
635
636 // SetsockoptString wraps setsockopt(2) for string values.
637 func (c *Conn) SetsockoptString(level, opt int, value string) error {
638 return c.control("setsockopt", func(fd int) error {
639 return unix.SetsockoptString(fd, level, opt, value)
640 })
641 }
642
643 // Shutdown wraps shutdown(2).
644 func (c *Conn) Shutdown(how int) error {
645 return c.control("shutdown", func(fd int) error { return unix.Shutdown(fd, how) })
646 }
647
648 // Conn low-level read/write/control functions. These functions mirror the
649 // syscall.RawConn APIs but the input closures return errors rather than
650 // booleans.
651
652 // read wraps readT to execute a function and capture its error result. This is
653 // a convenience wrapper for functions which don't return any extra values.
654 func (c *Conn) read(ctx context.Context, op string, f func(fd int) error) error {
655 _, err := readT(ctx, c, op, func(fd int) (struct{}, error) {
656 return struct{}{}, f(fd)
657 })
658 return err
659 }
660
661 // write executes f, a write function, against the associated file descriptor.
662 // op is used to create an *os.SyscallError if the file descriptor is closed.
663 func (c *Conn) write(ctx context.Context, op string, f func(fd int) error) error {
664 _, err := writeT(ctx, c, op, func(fd int) (struct{}, error) {
665 return struct{}{}, f(fd)
666 })
667 return err
668 }
669
670 // readT executes c.rc.Read for op using the input function, returning a newly
671 // allocated result T.
672 func readT[T any](ctx context.Context, c *Conn, op string, f func(fd int) (T, error)) (T, error) {
673 return rwT(c, rwContext[T]{
674 Context: ctx,
675 Type: read,
676 Op: op,
677 Do: f,
678 })
679 }
680
681 // writeT executes c.rc.Write for op using the input function, returning a newly
682 // allocated result T.
683 func writeT[T any](ctx context.Context, c *Conn, op string, f func(fd int) (T, error)) (T, error) {
684 return rwT(c, rwContext[T]{
685 Context: ctx,
686 Type: write,
687 Op: op,
688 Do: f,
689 })
690 }
691
692 // readWrite indicates if an operation intends to read or write.
693 type readWrite bool
694
695 // Possible readWrite values.
696 const (
697 read readWrite = false
698 write readWrite = true
699 )
700
701 // An rwContext provides arguments to rwT.
702 type rwContext[T any] struct {
703 // The caller's context passed for cancelation.
704 Context context.Context
705
706 // The type of an operation: read or write.
707 Type readWrite
708
709 // The name of the operation used in errors.
710 Op string
711
712 // The actual function to perform.
713 Do func(fd int) (T, error)
714 }
715
716 // rwT executes c.rc.Read or c.rc.Write (depending on the value of rw.Type) for
717 // rw.Op using the input function, returning a newly allocated result T.
718 //
719 // It obeys context cancelation and the rw.Context must not be nil.
720 func rwT[T any](c *Conn, rw rwContext[T]) (T, error) {
721 if atomic.LoadUint32(&c.closed) != 0 {
722 // If the file descriptor is already closed, do nothing.
723 return *new(T), os.NewSyscallError(rw.Op, unix.EBADF)
724 }
725
726 if err := rw.Context.Err(); err != nil {
727 // Early exit due to context cancel.
728 return *new(T), os.NewSyscallError(rw.Op, err)
729 }
730
731 var (
732 // The read or write function used to access the runtime network poller.
733 poll func(func(uintptr) bool) error
734
735 // The read or write function used to set the matching deadline.
736 deadline func(time.Time) error
737 )
738
739 if rw.Type == write {
740 poll = c.rc.Write
741 deadline = c.SetWriteDeadline
742 } else {
743 poll = c.rc.Read
744 deadline = c.SetReadDeadline
745 }
746
747 var (
748 // Whether or not the context carried a deadline we are actively using
749 // for cancelation.
750 setDeadline bool
751
752 // Signals for the cancelation watcher goroutine.
753 wg sync.WaitGroup
754 doneC = make(chan struct{})
755
756 // Atomic: reports whether we have to disarm the deadline.
757 needDisarm atomic.Bool
758 )
759
760 if d, ok := rw.Context.Deadline(); ok {
761 // The context has an explicit deadline. We will use it for cancelation
762 // but disarm it after poll for the next call.
763 if err := deadline(d); err != nil {
764 return *new(T), err
765 }
766 setDeadline = true
767 needDisarm.Store(true)
768 }
769
770 if rw.Context.Done() != nil {
771 // The context can be canceled, regardless of whether or not it also
772 // carries a deadline. We have to watch for cancelation so we can
773 // propagate that signal to immediately unblock the runtime network
774 // poller rather than waiting for a possibly distant deadline to expire.
775 //
776 // A nil Done channel means the context can never be canceled (such as
777 // context.Background), so no watcher is necessary.
778 wg.Go(func() {
779 select {
780 case <-rw.Context.Done():
781 // Cancel the operation. Make the caller disarm after poll
782 // returns.
783 needDisarm.Store(true)
784 _ = deadline(time.Unix(0, 1))
785 case <-doneC:
786 // Nothing to do.
787 }
788 })
789 }
790
791 var (
792 t T
793 err error
794 )
795
796 pollErr := poll(func(fd uintptr) bool {
797 t, err = rw.Do(int(fd))
798 return ready(err)
799 })
800
801 // Stop the watcher and wait for it to exit before checking whether we
802 // must disarm the deadline. Otherwise the watcher could observe
803 // cancelation and arm a deadline in the past after we have already
804 // disarmed, leaving the next call to fail immediately.
805 close(doneC)
806 wg.Wait()
807
808 if needDisarm.Load() {
809 _ = deadline(time.Time{})
810 }
811
812 if pollErr != nil {
813 if rw.Context.Err() != nil || (setDeadline && errors.Is(pollErr, os.ErrDeadlineExceeded)) {
814 // The caller canceled the operation or we set a deadline internally
815 // and it was reached.
816 //
817 // Unpack a plain context error. We wait for the context to be done
818 // to synchronize state externally. Otherwise we have noticed I/O
819 // timeout wakeups when we set a deadline but the context was not
820 // yet marked done.
821 <-rw.Context.Done()
822 return *new(T), os.NewSyscallError(rw.Op, rw.Context.Err())
823 }
824
825 // Error from syscall.RawConn methods. Conventionally the standard
826 // library does not wrap internal/poll errors in os.NewSyscallError.
827 return *new(T), pollErr
828 }
829
830 // Result from user function.
831 return t, os.NewSyscallError(rw.Op, err)
832 }
833
834 // control executes Conn.control for op using the input function.
835 func (c *Conn) control(op string, f func(fd int) error) error {
836 _, err := controlT(c, op, func(fd int) (struct{}, error) {
837 return struct{}{}, f(fd)
838 })
839 return err
840 }
841
842 // controlT executes c.rc.Control for op using the input function, returning a
843 // newly allocated result T.
844 func controlT[T any](c *Conn, op string, f func(fd int) (T, error)) (T, error) {
845 if atomic.LoadUint32(&c.closed) != 0 {
846 // If the file descriptor is already closed, do nothing.
847 return *new(T), os.NewSyscallError(op, unix.EBADF)
848 }
849
850 var (
851 t T
852 err error
853 )
854
855 doErr := c.rc.Control(func(fd uintptr) {
856 // Repeatedly attempt the syscall(s) invoked by f until completion is
857 // indicated by the return value of ready or the context is canceled.
858 //
859 // The last values for t and err are captured outside of the closure for
860 // use when the loop breaks.
861 for {
862 t, err = f(int(fd))
863 if ready(err) {
864 return
865 }
866 }
867 })
868 if doErr != nil {
869 // Error from syscall.RawConn methods. Conventionally the standard
870 // library does not wrap internal/poll errors in os.NewSyscallError.
871 return *new(T), doErr
872 }
873
874 // Result from user function.
875 return t, os.NewSyscallError(op, err)
876 }
877
878 // ready indicates readiness based on the value of err.
879 func ready(err error) bool {
880 switch err {
881 case unix.EAGAIN, unix.EINPROGRESS, unix.EINTR:
882 // When a socket is in non-blocking mode, we might see a variety of errors:
883 // - EAGAIN: most common case for a socket read not being ready
884 // - EINPROGRESS: reported by some sockets when first calling connect
885 // - EINTR: system call interrupted, more frequently occurs in Go 1.14+
886 // because goroutines can be asynchronously preempted
887 //
888 // Return false to let the poller wait for readiness. See the source code
889 // for internal/poll.FD.RawRead for more details.
890 return false
891 default:
892 // Ready regardless of whether there was an error or no error.
893 return true
894 }
895 }
896
897 // Darwin and FreeBSD can't read or write 2GB+ files at a time,
898 // even on 64-bit systems.
899 // The same is true of socket implementations on many systems.
900 // See golang.org/issue/7812 and golang.org/issue/16266.
901 // Use 1GB instead of, say, 2GB-1, to keep subsequent reads aligned.
902 const maxRW = 1 << 30