summaryrefslogtreecommitdiff
path: root/socket_linux.go
diff options
context:
space:
mode:
authorGrégoire Duchêne <gduchene@awhk.org>2021-06-27 19:31:23 +0100
committerGrégoire Duchêne <gduchene@awhk.org>2021-06-27 19:31:23 +0100
commitc62f3e8b39e7d1ff3d46cc4d3d599b2450b3c097 (patch)
tree8d0667e9176cdbf359530a9efe85b12eaf4559a2 /socket_linux.go
parent424618a7f2f253a0529e3462ceb69607ee3c8c88 (diff)
First draft
Diffstat (limited to 'socket_linux.go')
-rw-r--r--socket_linux.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/socket_linux.go b/socket_linux.go
new file mode 100644
index 0000000..8360318
--- /dev/null
+++ b/socket_linux.go
@@ -0,0 +1,59 @@
+// +build linux,!nosystemd
+
+package gosdd
+
+// #cgo LDFLAGS: -lsystemd
+// #include <stdlib.h>
+// #include <string.h>
+// #include <systemd/sd-daemon.h>
+import "C"
+
+import (
+ "fmt"
+ "os"
+ "unsafe"
+)
+
+func sdListenFDs(unsetenv bool) ([]*os.File, error) {
+ i := C.int(0)
+ if unsetenv {
+ i = C.int(1)
+ }
+ c := C.sd_listen_fds(i)
+ if c < 0 {
+ return nil, fmt.Errorf("sd_listen_fds: %s", C.GoString(C.strerror(-c)))
+ }
+ if c == 0 {
+ return nil, nil
+ }
+ fds := make([]*os.File, 0, c)
+ for fd := uintptr(C.SD_LISTEN_FDS_START); fd < uintptr(C.SD_LISTEN_FDS_START+c); fd++ {
+ fds = append(fds, os.NewFile(fd, ""))
+ }
+ return fds, nil
+}
+
+func sdListenFDsWithNames(unsetenv bool) (map[string]*os.File, error) {
+ i := C.int(0)
+ if unsetenv {
+ i = C.int(1)
+ }
+ var arr **C.char
+ c := C.sd_listen_fds_with_names(i, &arr)
+ if c < 0 {
+ return nil, fmt.Errorf("sd_listen_fds_with_names: %s", C.GoString(C.strerror(-c)))
+ }
+ if c == 0 {
+ return nil, nil
+ }
+ // See https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices.
+ names := (*[1 << 28]*C.char)(unsafe.Pointer(arr))[:c:c]
+ fds := make(map[string]*os.File)
+ for fd := uintptr(C.SD_LISTEN_FDS_START); fd < uintptr(C.SD_LISTEN_FDS_START+c); fd++ {
+ name := C.GoString(names[int(fd-C.SD_LISTEN_FDS_START)])
+ fds[name] = os.NewFile(fd, name)
+ C.free(unsafe.Pointer(names[int(fd-C.SD_LISTEN_FDS_START)]))
+ }
+ C.free(unsafe.Pointer(arr))
+ return fds, nil
+}