2021-09-09 14:25:25 +02:00
|
|
|
//go:build linux || freebsd
|
2021-01-03 01:05:52 +01:00
|
|
|
// +build linux freebsd
|
|
|
|
|
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-06-11 00:46:36 +02:00
|
|
|
|
|
|
|
"github.com/rclone/rclone/lib/file"
|
2021-01-03 01:05:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func newUnixListener(path string, gid int) (net.Listener, string, error) {
|
|
|
|
// try systemd socket activation
|
|
|
|
fds := systemdActivationFiles()
|
|
|
|
switch len(fds) {
|
|
|
|
case 0:
|
|
|
|
// fall thru
|
|
|
|
case 1:
|
|
|
|
listener, err := net.FileListener(fds[0])
|
|
|
|
return listener, "", err
|
|
|
|
default:
|
|
|
|
return nil, "", fmt.Errorf("expected only one socket from systemd, got %d", len(fds))
|
|
|
|
}
|
|
|
|
|
2022-08-14 04:56:32 +02:00
|
|
|
// create socket ourselves
|
2021-01-03 01:05:52 +01:00
|
|
|
if filepath.Ext(path) == "" {
|
|
|
|
path += ".sock"
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
path = filepath.Join(sockDir, path)
|
|
|
|
}
|
|
|
|
|
2021-06-11 00:46:36 +02:00
|
|
|
if err := file.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
2021-01-03 01:05:52 +01:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
listener, err := net.Listen("unix", path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Chmod(path, 0660); err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
if os.Geteuid() == 0 {
|
|
|
|
if err = os.Chown(path, 0, gid); err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we don't use spec file with unix sockets
|
|
|
|
return listener, path, nil
|
|
|
|
}
|