Debian Codegraph @

listen_reuseUnixSocket_windows.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //go:build windows
16
17 package caddy
18
19 import (
20 "errors"
21 "fmt"
22 "io/fs"
23 "net"
24 "os"
25 "strings"
26 "syscall"
27 "time"
28 )
29
30 var errUnixSocketAlreadyInUse = errors.New("unix socket is already in use by another process")
31
32 func reuseUnixSocket(network, addr string) (any, error) {
33 if !IsUnixNetwork(network) {
34 return nil, nil
35 }
36
37 // Note: This is here mainly for proper compatibility, because Unix sockets with abstract names are in an interesting limbo state on Windows:
38 // Go already translates `@` characters to `\0` for Windows: https://github.com/golang/go/blob/65d5c5f6dd8aa7b221cff6ec3f5101ea2e5f3efa/src/syscall/syscall_windows.go#L910
39 // ...but there still is an open issue about the fact that this is not properly supported: https://github.com/microsoft/WSL/issues/4240#issuecomment-620805115
40 // The main issue is that the original announcement proclaimed support for this feature, but it was (apparently) never implemented: https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/
41 isAbstractUnixSocket := strings.HasPrefix(addr, "@")
42
43 if isAbstractUnixSocket {
44 // Abstract Unix sockets do not require us to remove stale socket files.
45 return nil, nil
46 }
47
48 // On Windows, we're using the `fakeCloseListener` wrappers around a single, ever-living listener.
49 // So, if there's an active listener entry in the pool, we're the current owner of the Unix socket file.
50 _, socketBelongsToCurrentProcess := listenerPool.References(listenerKey(network, addr))
51
52 if socketBelongsToCurrentProcess {
53 // Reuse/cleanup is entirely handled by the refcounting mechanism in `listenerPool`.
54 return nil, nil
55 }
56
57 // If the socket file does not exist or has no backing server process, this will fail instantly.
58 connection, err := net.DialTimeout("unix", addr, 10*time.Millisecond)
59
60 if err == nil {
61 connection.Close()
62 return nil, fmt.Errorf("cannot reuse socket %v: %w", addr, errUnixSocketAlreadyInUse)
63 }
64
65 // Windows returns this error code both if the socket file does not exist and if it isn't backed by a server process anymore.
66 // See: https://learn.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2#wsaeconnrefused
67 const WSAECONNREFUSED syscall.Errno = 10061
68
69 var errno syscall.Errno
70 hasNoListeningServerProcess := errors.As(err, &errno) && errno == WSAECONNREFUSED
71
72 if !hasNoListeningServerProcess {
73 return nil, fmt.Errorf("cannot reuse socket %v: %w", addr, errUnixSocketAlreadyInUse)
74 }
75
76 // If the socket file exists, it hasn't been created by our process, and it seemingly
77 // isn't backed by a server process anymore. Try to delete it so we can bind to it later.
78 err = os.Remove(addr)
79
80 if err == nil {
81 return nil, nil
82 } else if errors.Is(err, fs.ErrNotExist) {
83 // Either the file didn't exist in the first place, or it was deleted before we were able to.
84 return nil, nil
85 } else {
86 // We failed to delete the file. Likely, it belongs to another (active) process.
87 return nil, err
88 }
89 }