2022-09-21 13:55:23 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-03-02 23:00:18 -05:00
|
|
|
// Even though the filename ends in _unix.go, we still have to specify the
|
|
|
|
// build constraint here, because the filename convention only works for
|
|
|
|
// literal GOOS values, and "unix" is a shortcut unique to build tags.
|
|
|
|
//go:build unix
|
2022-09-21 13:55:23 -05:00
|
|
|
|
|
|
|
package caddy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io/fs"
|
|
|
|
"net"
|
2022-09-28 14:35:51 -05:00
|
|
|
"sync/atomic"
|
2022-09-21 13:55:23 -05:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2022-09-28 14:35:51 -05:00
|
|
|
// reuseUnixSocket copies and reuses the unix domain socket (UDS) if we already
|
|
|
|
// have it open; if not, unlink it so we can have it. No-op if not a unix network.
|
|
|
|
func reuseUnixSocket(network, addr string) (any, error) {
|
2023-02-08 12:05:09 -05:00
|
|
|
if !IsUnixNetwork(network) {
|
2022-09-28 14:35:51 -05:00
|
|
|
return nil, nil
|
2022-09-21 13:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
socketKey := listenerKey(network, addr)
|
|
|
|
|
2022-09-28 14:35:51 -05:00
|
|
|
socket, exists := unixSockets[socketKey]
|
|
|
|
if exists {
|
|
|
|
// make copy of file descriptor
|
|
|
|
socketFile, err := socket.File() // does dup() deep down
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// use copied fd to make new Listener or PacketConn, then replace
|
|
|
|
// it in the map so that future copies always come from the most
|
|
|
|
// recent fd (as the previous ones will be closed, and we'd get
|
|
|
|
// "use of closed network connection" errors) -- note that we
|
|
|
|
// preserve the *pointer* to the counter (not just the value) so
|
|
|
|
// that all socket wrappers will refer to the same value
|
|
|
|
switch unixSocket := socket.(type) {
|
|
|
|
case *unixListener:
|
|
|
|
ln, err := net.FileListener(socketFile)
|
2022-09-21 13:55:23 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-28 14:35:51 -05:00
|
|
|
atomic.AddInt32(unixSocket.count, 1)
|
|
|
|
unixSockets[socketKey] = &unixListener{ln.(*net.UnixListener), socketKey, unixSocket.count}
|
2022-09-21 13:55:23 -05:00
|
|
|
|
2022-09-28 14:35:51 -05:00
|
|
|
case *unixConn:
|
|
|
|
pc, err := net.FilePacketConn(socketFile)
|
2022-09-21 13:55:23 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-28 14:35:51 -05:00
|
|
|
atomic.AddInt32(unixSocket.count, 1)
|
|
|
|
unixSockets[socketKey] = &unixConn{pc.(*net.UnixConn), addr, socketKey, unixSocket.count}
|
2022-09-21 13:55:23 -05:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:35:51 -05:00
|
|
|
return unixSockets[socketKey], nil
|
2022-09-21 13:55:23 -05:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:35:51 -05:00
|
|
|
// from what I can tell after some quick research, it's quite common for programs to
|
|
|
|
// leave their socket file behind after they close, so the typical pattern is to
|
|
|
|
// unlink it before you bind to it -- this is often crucial if the last program using
|
|
|
|
// it was killed forcefully without a chance to clean up the socket, but there is a
|
|
|
|
// race, as the comment in net.UnixListener.close() explains... oh well, I guess?
|
|
|
|
if err := syscall.Unlink(addr); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2022-09-21 13:55:23 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-28 14:35:51 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
2022-09-21 13:55:23 -05:00
|
|
|
|
2022-09-28 14:35:51 -05:00
|
|
|
func listenTCPOrUnix(ctx context.Context, lnKey string, network, address string, config net.ListenConfig) (net.Listener, error) {
|
|
|
|
// wrap any Control function set by the user so we can also add our reusePort control without clobbering theirs
|
|
|
|
oldControl := config.Control
|
|
|
|
config.Control = func(network, address string, c syscall.RawConn) error {
|
|
|
|
if oldControl != nil {
|
|
|
|
if err := oldControl(network, address, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reusePort(network, address, c)
|
|
|
|
}
|
2023-03-02 23:00:18 -05:00
|
|
|
|
|
|
|
// even though SO_REUSEPORT lets us bind the socket multiple times,
|
|
|
|
// we still put it in the listenerPool so we can count how many
|
|
|
|
// configs are using this socket; necessary to ensure we can know
|
|
|
|
// whether to enforce shutdown delays, for example (see #5393).
|
|
|
|
ln, err := config.Listen(ctx, network, address)
|
|
|
|
if err == nil {
|
|
|
|
listenerPool.LoadOrStore(lnKey, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lightly wrap the listener so that when it is closed,
|
|
|
|
// we can decrement the usage pool counter
|
|
|
|
return deleteListener{ln, lnKey}, err
|
2022-09-21 13:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// reusePort sets SO_REUSEPORT. Ineffective for unix sockets.
|
|
|
|
func reusePort(network, address string, conn syscall.RawConn) error {
|
2023-02-08 12:05:09 -05:00
|
|
|
if IsUnixNetwork(network) {
|
2022-09-28 14:35:51 -05:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-21 13:55:23 -05:00
|
|
|
return conn.Control(func(descriptor uintptr) {
|
2023-05-23 11:56:00 -05:00
|
|
|
if err := unix.SetsockoptInt(int(descriptor), unix.SOL_SOCKET, unixSOREUSEPORT, 1); err != nil {
|
2022-09-21 13:55:23 -05:00
|
|
|
Log().Error("setting SO_REUSEPORT",
|
|
|
|
zap.String("network", network),
|
|
|
|
zap.String("address", address),
|
|
|
|
zap.Uintptr("descriptor", descriptor),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-03-02 23:00:18 -05:00
|
|
|
|
|
|
|
// deleteListener is a type that simply deletes itself
|
|
|
|
// from the listenerPool when it closes. It is used
|
|
|
|
// solely for the purpose of reference counting (i.e.
|
|
|
|
// counting how many configs are using a given socket).
|
|
|
|
type deleteListener struct {
|
|
|
|
net.Listener
|
|
|
|
lnKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl deleteListener) Close() error {
|
|
|
|
_, _ = listenerPool.Delete(dl.lnKey)
|
|
|
|
return dl.Listener.Close()
|
|
|
|
}
|