From c62f3e8b39e7d1ff3d46cc4d3d599b2450b3c097 Mon Sep 17 00:00:00 2001 From: GrĂ©goire DuchĂȘne Date: Sun, 27 Jun 2021 19:31:23 +0100 Subject: First draft --- socket_linux.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 socket_linux.go (limited to 'socket_linux.go') 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 +// #include +// #include +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 +} -- cgit v1.2.3-70-g09d2