diff options
Diffstat (limited to 'sd_daemon')
-rw-r--r-- | sd_daemon/Makefile | 2 | ||||
-rw-r--r-- | sd_daemon/booted.go | 2 | ||||
-rw-r--r-- | sd_daemon/exit-status.go | 3 | ||||
-rw-r--r-- | sd_daemon/listen_fds.go | 2 | ||||
-rwxr-xr-x | sd_daemon/log_util.go.gen | 2 | ||||
-rw-r--r-- | sd_daemon/notify.go | 100 | ||||
-rw-r--r-- | sd_daemon/notify_linux.go | 123 | ||||
-rw-r--r-- | sd_daemon/notify_nonlinux.go | 63 | ||||
-rw-r--r-- | sd_daemon/watchdog.go | 2 |
9 files changed, 155 insertions, 144 deletions
diff --git a/sd_daemon/Makefile b/sd_daemon/Makefile index c834dda..90d221c 100644 --- a/sd_daemon/Makefile +++ b/sd_daemon/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2017 Luke Shumaker <lukeshu@sbcglobal.net> +# Copyright (C) 2016-2017 Luke Shumaker <lukeshu@lukeshu.com> # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/sd_daemon/booted.go b/sd_daemon/booted.go index 18fd4fb..9a0bdf1 100644 --- a/sd_daemon/booted.go +++ b/sd_daemon/booted.go @@ -1,3 +1,5 @@ +// Incorporates: git://github.com/coreos/go-systemd.git 7f0723f2757beb369312e795c56cb681a928d7c7 util/util.go:IsRunningSystemd() +// // Copyright 2015 CoreOS, Inc. // Copyright 2016 Luke Shumaker // diff --git a/sd_daemon/exit-status.go b/sd_daemon/exit-status.go index 7993dbc..5b86205 100644 --- a/sd_daemon/exit-status.go +++ b/sd_daemon/exit-status.go @@ -1,4 +1,4 @@ -// Copyright 2015-2016, 2018 Luke Shumaker +// Copyright 2015-2016, 2018-2019 Luke Shumaker // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -75,6 +75,7 @@ const ( // 200-254 are reserved (for init system use). // So, take codes 200+ from systemd's // `src/basic/exit-status.h` + // (last updated for SD v242) EXIT_CHDIR uint8 = 200 // SD v8+ EXIT_NICE uint8 = 201 // SD v8+ EXIT_FDS uint8 = 202 // SD v8+ diff --git a/sd_daemon/listen_fds.go b/sd_daemon/listen_fds.go index cdcb3cd..575e2ab 100644 --- a/sd_daemon/listen_fds.go +++ b/sd_daemon/listen_fds.go @@ -1,3 +1,5 @@ +// Incorporates: git://github.com/coreos/go-systemd.git 8843929dce14ef03e3f9b78c1707d347cbf661b9 activation/files.go +// // Copyright 2015 CoreOS, Inc. // Copyright 2015-2016 Luke Shumaker // diff --git a/sd_daemon/log_util.go.gen b/sd_daemon/log_util.go.gen index 0ad4ea2..6bf98b1 100755 --- a/sd_daemon/log_util.go.gen +++ b/sd_daemon/log_util.go.gen @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Copyright (C) 2016, 2018 Luke Shumaker <lukeshu@sbcglobal.net> +# Copyright (C) 2016, 2018 Luke Shumaker <lukeshu@lukeshu.com> # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/sd_daemon/notify.go b/sd_daemon/notify.go index b0a0d72..9591613 100644 --- a/sd_daemon/notify.go +++ b/sd_daemon/notify.go @@ -1,6 +1,9 @@ -// Copyright 2013-2015 Docker, Inc. +// Incorporates: git://github.com/docker/docker.git 18c7c67308bd4a24a41028e63c2603bb74eac85e pkg/systemd/sd_notify.go +// Incorporates: git://github.com/coreos/go-systemd.git a606a1e936df81b70d85448221c7b1c6d8a74ef1 daemon/sdnotify.go +// +// Copyright 2013, 2015 Docker, Inc. // Copyright 2014 CoreOS, Inc. -// Copyright 2015-2018 Luke Shumaker +// Copyright 2015-2019 Luke Shumaker // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,16 +17,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -// +build linux - package sd_daemon import ( - "bytes" - "net" "os" - - "golang.org/x/sys/unix" ) // Notification is a message to be sent to the service manager about @@ -33,6 +30,11 @@ type Notification struct { // If PID <= 0, or if the current process does not have // privileges to send messages on behalf of other processes, // then the message is simply sent from the current process. + // + // BUG(lukeshu): Spoofing the PID is not implemented on + // non-Linux kernels. If you are knowledgable about how to do + // this on other kernels, please let me know at + // <lukeshu@lukeshu.com>! PID int // State should contain a newline-separated list of variable @@ -72,85 +74,5 @@ type Notification struct { // support both service managers that support these notifications and // those that do not. func (msg Notification) Send(unsetEnv bool) error { - if unsetEnv { - defer func() { _ = os.Unsetenv("NOTIFY_SOCKET") }() - } - - socketAddr := &net.UnixAddr{ - Name: os.Getenv("NOTIFY_SOCKET"), - Net: "unixgram", - } - - if socketAddr.Name == "" { - return ErrDisabled - } - - conn, err := socketUnixgram(socketAddr.Name) - if err != nil { - return err - } - defer func() { _ = conn.Close() }() - - var cmsgs [][]byte - - if len(msg.Files) > 0 { - fds := make([]int, len(msg.Files)) - for i := range msg.Files { - fds[i] = int(msg.Files[i].Fd()) - } - cmsg := unix.UnixRights(fds...) - cmsgs = append(cmsgs, cmsg) - } - - havePid := msg.PID > 0 && msg.PID != os.Getpid() - if havePid { - cmsg := unix.UnixCredentials(&unix.Ucred{ - Pid: int32(msg.PID), - Uid: uint32(os.Getuid()), - Gid: uint32(os.Getgid()), - }) - cmsgs = append(cmsgs, cmsg) - } - - // If the 2nd argument is empty, this is equivalent to - // - // conn, _ := net.DialUnix(socketAddr.Net, nil, socketAddr) - // conn.Write([]byte(msg.State)) - _, _, err = conn.WriteMsgUnix([]byte(msg.State), bytes.Join(cmsgs, nil), socketAddr) - - if err != nil && havePid { - // Maybe it failed because we don't have privileges to - // spoof our pid; retry without spoofing the pid. - // - // I'm not too happy that we do this silently without - // notifying the caller, but that's what - // sd_pid_notify_with_fds does. - cmsgs = cmsgs[:len(cmsgs)-1] - _, _, err = conn.WriteMsgUnix([]byte(msg.State), bytes.Join(cmsgs, nil), socketAddr) - } - - return err -} - -// socketUnixgram wraps socket(2), but doesn't bind(2) or connect(2) -// the socket to anything. This is an ugly hack because none of the -// functions in "net" actually allow you to get a AF_UNIX socket not -// bound/connected to anything. -// -// At some point you begin to question if it is worth it to keep up -// the high-level interface of "net", and messing around with FileConn -// and UnixConn. Maybe we just drop to using unix.Socket and -// unix.SendmsgN directly. -func socketUnixgram(name string) (*net.UnixConn, error) { - fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_DGRAM|unix.SOCK_CLOEXEC, 0) - if err != nil { - return nil, os.NewSyscallError("socket", err) - } - defer unix.Close(fd) - conn, err := net.FileConn(os.NewFile(uintptr(fd), name)) - if err != nil { - return nil, err - } - unixConn := conn.(*net.UnixConn) - return unixConn, nil + return msg.send(unsetEnv) } diff --git a/sd_daemon/notify_linux.go b/sd_daemon/notify_linux.go new file mode 100644 index 0000000..085b25c --- /dev/null +++ b/sd_daemon/notify_linux.go @@ -0,0 +1,123 @@ +// Incorporates: git://github.com/docker/docker.git 18c7c67308bd4a24a41028e63c2603bb74eac85e pkg/systemd/sd_notify.go +// Incorporates: git://github.com/coreos/go-systemd.git a606a1e936df81b70d85448221c7b1c6d8a74ef1 daemon/sdnotify.go +// +// Copyright 2013, 2015 Docker, Inc. +// Copyright 2014 CoreOS, Inc. +// Copyright 2015-2019 Luke Shumaker +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build linux + +package sd_daemon + +import ( + "bytes" + "net" + "os" + + "golang.org/x/sys/unix" +) + +func (msg Notification) send(unsetEnv bool) error { + if unsetEnv { + defer func() { _ = os.Unsetenv("NOTIFY_SOCKET") }() + } + + socketAddr := &net.UnixAddr{ + Name: os.Getenv("NOTIFY_SOCKET"), + Net: "unixgram", + } + + if socketAddr.Name == "" { + return ErrDisabled + } + + conn, err := socketUnixgram(socketAddr.Name) + if err != nil { + return err + } + defer func() { _ = conn.Close() }() + + var cmsgs [][]byte + + if len(msg.Files) > 0 { + fds := make([]int, len(msg.Files)) + for i := range msg.Files { + fds[i] = int(msg.Files[i].Fd()) + } + cmsg := unix.UnixRights(fds...) + cmsgs = append(cmsgs, cmsg) + } + + havePid := msg.PID > 0 && msg.PID != os.Getpid() + if havePid { + cmsg := unix.UnixCredentials(&unix.Ucred{ + Pid: int32(msg.PID), + Uid: uint32(os.Getuid()), + Gid: uint32(os.Getgid()), + }) + cmsgs = append(cmsgs, cmsg) + } + + // If the 2nd argument is empty, this is equivalent to + // + // conn, _ := net.DialUnix(socketAddr.Net, nil, socketAddr) + // conn.Write([]byte(msg.State)) + _, _, err = conn.WriteMsgUnix([]byte(msg.State), bytes.Join(cmsgs, nil), socketAddr) + + if err != nil && havePid { + // Maybe it failed because we don't have privileges to + // spoof our pid; retry without spoofing the pid. + // + // I'm not too happy that we do this silently without + // notifying the caller, but that's what + // sd_pid_notify_with_fds does. + cmsgs = cmsgs[:len(cmsgs)-1] + _, _, err = conn.WriteMsgUnix([]byte(msg.State), bytes.Join(cmsgs, nil), socketAddr) + } + + return err +} + +// socketUnixgram wraps socket(2), but doesn't bind(2) or connect(2) +// the socket to anything. This is an ugly hack because none of the +// functions in "net" actually allow you to get a AF_UNIX socket not +// bound/connected to anything. +// +// At some point you begin to question if it is worth it to keep up +// the high-level interface of "net", and messing around with FileConn +// and UnixConn. Maybe we just drop to using unix.Socket and +// unix.SendmsgN directly. +// +// See: net/sock_cloexec.go:sysSocket() +func socketUnixgram(name string) (*net.UnixConn, error) { + // Don't bother with SOCK_NONBLOCK, net.FileConn() will call + // syscall.SetNonblock(). + // + // BUG(lukeshu): On Linux, Notification.Send() depends on + // SOCK_CLOEXEC in Linux 2.6.27 (2008-10-09), which is + // slightly newer than Go itself depends on, 2.6.23 + // (2007-10-09). + fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_DGRAM|unix.SOCK_CLOEXEC, 0) + if err != nil { + return nil, os.NewSyscallError("socket", err) + } + defer unix.Close(fd) + conn, err := net.FileConn(os.NewFile(uintptr(fd), name)) + if err != nil { + return nil, err + } + unixConn := conn.(*net.UnixConn) + return unixConn, nil +} diff --git a/sd_daemon/notify_nonlinux.go b/sd_daemon/notify_nonlinux.go index 5acc5d7..dc268d3 100644 --- a/sd_daemon/notify_nonlinux.go +++ b/sd_daemon/notify_nonlinux.go @@ -1,6 +1,9 @@ -// Copyright 2013-2015 Docker, Inc. +// Incorporates: git://github.com/docker/docker.git 18c7c67308bd4a24a41028e63c2603bb74eac85e pkg/systemd/sd_notify.go +// Incorporates: git://github.com/coreos/go-systemd.git a606a1e936df81b70d85448221c7b1c6d8a74ef1 daemon/sdnotify.go +// +// Copyright 2013, 2015 Docker, Inc. // Copyright 2014 CoreOS, Inc. -// Copyright 2015-2018 Luke Shumaker +// Copyright 2015-2019 Luke Shumaker // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -27,52 +30,7 @@ import ( "golang.org/x/sys/unix" ) -// Notification is a message to be sent to the service manager about -// state changes. -type Notification struct { - // PID specifies which process to send a notification about. - // If PID <= 0, or if the current process does not have - // privileges to send messages on behalf of other processes, - // then the message is simply sent from the current process. - PID int - - // State should contain a newline-separated list of variable - // assignments. See the documentation for sd_notify(3) for - // well-known variable assignments. - // - // https://www.freedesktop.org/software/systemd/man/sd_notify.html - State string - - // Files is a list of file descriptors to send to the service - // manager with the message. This is useful for keeping files - // open across restarts, as it enables the service manager to - // pass those files to the new process when it is restarted - // (see ListenFds). - // - // Note: The service manager will only actually store the file - // descriptors if you include "FDSTORE=1" in the state (again, - // see sd_notify(3) for well-known variable assignments). - Files []*os.File -} - -// Send sends the Notification to the service manager. -// -// If unsetEnv is true, then (regardless of whether the function call -// itself succeeds or not) it will unset the environmental variable -// NOTIFY_SOCKET, which will cause further notify operations to fail. -// -// If the service manager is not listening for notifications from this -// process tree (or a Notification has has already been send with -// unsetEnv=true), then ErrDisabled is returned. If the service -// manager appears to be listening, but there is an error sending the -// message, then that error is returned. -// -// It is generally recommended that you ignore the return value: if -// there is an error, then this is function no-op; meaning that by -// calling the function but ignoring the return value, you can easily -// support both service managers that support these notifications and -// those that do not. -func (msg Notification) Send(unsetEnv bool) error { +func (msg Notification) send(unsetEnv bool) error { if unsetEnv { defer func() { _ = os.Unsetenv("NOTIFY_SOCKET") }() } @@ -108,7 +66,7 @@ func (msg Notification) Send(unsetEnv bool) error { // BUG(lukeshu): Spoofing the socket credentials is // not implemnted on non-Linux kernels. If you are // knowledgable about how to do this on other kernels, - // please let me know at lukeshu@lukeshu.com! + // please let me know at <lukeshu@lukeshu.com>! havePid = false } @@ -141,6 +99,8 @@ func (msg Notification) Send(unsetEnv bool) error { // the high-level interface of "net", and messing around with FileConn // and UnixConn. Maybe we just drop to using unix.Socket and // unix.SendmsgN directly. +// +// See: net/sys_cloexec.go:sysSocket() func socketUnixgram(name string) (*net.UnixConn, error) { syscall.ForkLock.RLock() fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_DGRAM, 0) @@ -152,9 +112,8 @@ func socketUnixgram(name string) (*net.UnixConn, error) { return nil, os.NewSyscallError("socket", err) } defer unix.Close(fd) - if err = unix.SetNonblock(fd, true); err != nil { - return nil, os.NewSyscallError("setnonblock", err) - } + // Don't bother calling unix.SetNonblock(), net.FileConn() + // will call syscall.SetNonblock(). conn, err := net.FileConn(os.NewFile(uintptr(fd), name)) if err != nil { return nil, err diff --git a/sd_daemon/watchdog.go b/sd_daemon/watchdog.go index 05dc9f3..7ebac15 100644 --- a/sd_daemon/watchdog.go +++ b/sd_daemon/watchdog.go @@ -1,3 +1,5 @@ +// Incorporates: git://github.com/coreos/go-systemd.git 0c088eaedf4396216a47ca971d4630f1697186bf daemon/watchdog.go +// // Copyright 2016 CoreOS, Inc. // Copyright 2016, 2018 Luke Shumaker // |