aboutsummaryrefslogtreecommitdiffstats
path: root/http/systemd_linux.go
diff options
context:
space:
mode:
authorMarc Pervaz Boocha <mboocha@sudomsg.com>2025-08-07 22:51:34 +0530
committerMarc Pervaz Boocha <mboocha@sudomsg.com>2025-08-07 22:51:34 +0530
commit1326bb4103694d7ceac23b23329997ea2207a3f6 (patch)
tree72eb0065b597121c4e54518d303f5d15de40336d /http/systemd_linux.go
parentFixed missing signals (diff)
downloadkit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar.gz
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar.bz2
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar.lz
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar.xz
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar.zst
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.zip
Added File Mode to sockets and socket activation
Diffstat (limited to 'http/systemd_linux.go')
-rw-r--r--http/systemd_linux.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/http/systemd_linux.go b/http/systemd_linux.go
new file mode 100644
index 0000000..817b421
--- /dev/null
+++ b/http/systemd_linux.go
@@ -0,0 +1,61 @@
+//go:build linux
+
+package http
+
+import (
+ "fmt"
+
+ "net"
+ "os"
+ "strconv"
+)
+
+func getSystemdListeners() (Listeners, error) {
+ pidStr := os.Getenv("LISTEN_PID")
+ fdStr := os.Getenv("LISTEN_FDS")
+
+ if pidStr == "" || fdStr == "" {
+ return nil, nil // Not running under systemd
+ }
+
+ pid, err := strconv.Atoi(pidStr)
+ if err != nil {
+ // Not our activation — another process might have inherited the env.
+ return nil, nil
+ }
+ if pid != os.Getpid() {
+ return nil, fmt.Errorf("LISTEN_PID %d does not match current PID %d", pid, os.Getpid())
+ }
+
+ defer func() {
+ _ = os.Unsetenv("LISTEN_PID")
+ _ = os.Unsetenv("LISTEN_FDS")
+ }()
+
+ nfds, err := strconv.ParseUint(fdStr, 10, 64)
+ if err != nil {
+ return nil, fmt.Errorf("invalid LISTEN_FDS: %w", err)
+ }
+ if nfds == 0 {
+ return nil, nil // Nothing to do
+ }
+
+ lns := make(Listeners, 0, nfds)
+ for i := range nfds {
+ fd := i + 3
+ file := os.NewFile(uintptr(fd), fmt.Sprintf("fd_%d", fd))
+ if file == nil {
+ return nil, fmt.Errorf("fd %d was nil", fd)
+ }
+ ln, err := net.FileListener(file)
+ if err != nil {
+ file.Close()
+ return nil, fmt.Errorf("failed to create listener from fd %d: %w", fd, err)
+ }
+ lns = append(lns, ln)
+ }
+
+ return lns, nil
+}
+
+