aboutsummaryrefslogtreecommitdiffstats
path: root/net/systemd_linux.go
diff options
context:
space:
mode:
authorMarc Pervaz Boocha <mboocha@sudomsg.com>2026-02-24 23:43:18 +0530
committerMarc Pervaz Boocha <mboocha@sudomsg.com>2026-02-24 23:43:18 +0530
commit37809b8c855250b931ec592f12fd548ddfa1dabe (patch)
treee9fbb747cc8a29fc031dba3de77919c94edebc4a /net/systemd_linux.go
parentAdded more logging stuff (diff)
downloadkit-37809b8c855250b931ec592f12fd548ddfa1dabe.tar
kit-37809b8c855250b931ec592f12fd548ddfa1dabe.tar.gz
kit-37809b8c855250b931ec592f12fd548ddfa1dabe.tar.bz2
kit-37809b8c855250b931ec592f12fd548ddfa1dabe.tar.lz
kit-37809b8c855250b931ec592f12fd548ddfa1dabe.tar.xz
kit-37809b8c855250b931ec592f12fd548ddfa1dabe.tar.zst
kit-37809b8c855250b931ec592f12fd548ddfa1dabe.zip
Split net packages
Diffstat (limited to 'net/systemd_linux.go')
-rw-r--r--net/systemd_linux.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/net/systemd_linux.go b/net/systemd_linux.go
new file mode 100644
index 0000000..efccd27
--- /dev/null
+++ b/net/systemd_linux.go
@@ -0,0 +1,58 @@
+//go:build linux
+
+package net
+
+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
+}