diff options
-rw-r--r-- | LICENSE | 202 | ||||
-rw-r--r-- | NOTICE | 17 | ||||
-rw-r--r-- | go.mod | 5 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rwxr-xr-x | make-release | 12 | ||||
-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 | ||||
-rw-r--r-- | sd_messages/Makefile | 2 | ||||
-rw-r--r-- | sd_messages/messages.go | 7 | ||||
-rwxr-xr-x | sd_messages/messages.go.gen | 2 |
17 files changed, 401 insertions, 147 deletions
@@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. @@ -0,0 +1,17 @@ +The following notice pertains to the `sd_daemon/notify.go` file: + + Docker + Copyright 2012-2015 Docker, Inc. + + This product includes software developed at Docker, Inc. (https://www.docker.com). + + The following is courtesy of our legal counsel: + + Use and transfer of Docker may be subject to certain restrictions by the + United States and other governments. + It is your responsibility to ensure that your use and/or transfer does not + violate applicable laws. + + For more information, please see https://www.bis.doc.gov + + See also https://www.apache.org/dev/crypto.html and/or seek legal counsel. @@ -0,0 +1,5 @@ +module git.lukeshu.com/go/libsystemd + +go 1.12 + +require golang.org/x/sys v0.0.0-20180425194835-bb9c189858d9 @@ -0,0 +1,2 @@ +golang.org/x/sys v0.0.0-20180425194835-bb9c189858d9 h1:+wWbjU2lIhyVuf3l5jcSMKOCokMwx1eP/lpVWk1Ox0M= +golang.org/x/sys v0.0.0-20180425194835-bb9c189858d9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/make-release b/make-release index 8646bc1..fabfd02 100755 --- a/make-release +++ b/make-release @@ -1,5 +1,17 @@ #!/usr/bin/env bash # Copyright 2016-2018 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. set -e branch=$(git name-rev --name-only HEAD) 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 // diff --git a/sd_messages/Makefile b/sd_messages/Makefile index 4cf4640..312d20b 100644 --- a/sd_messages/Makefile +++ b/sd_messages/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_messages/messages.go b/sd_messages/messages.go index cf0294a..e0b3ba2 100644 --- a/sd_messages/messages.go +++ b/sd_messages/messages.go @@ -37,9 +37,9 @@ var ( UnitStarting = sd_id128.ID128{0x7d, 0x49, 0x58, 0xe8, 0x42, 0xda, 0x4a, 0x75, 0x8f, 0x6c, 0x1c, 0xdc, 0x7b, 0x36, 0xdc, 0xc5} UnitStarted = sd_id128.ID128{0x39, 0xf5, 0x34, 0x79, 0xd3, 0xa0, 0x45, 0xac, 0x8e, 0x11, 0x78, 0x62, 0x48, 0x23, 0x1f, 0xbf} + UnitFailed = sd_id128.ID128{0xbe, 0x02, 0xcf, 0x68, 0x55, 0xd2, 0x42, 0x8b, 0xa4, 0x0d, 0xf7, 0xe9, 0xd0, 0x22, 0xf0, 0x3d} UnitStopping = sd_id128.ID128{0xde, 0x5b, 0x42, 0x6a, 0x63, 0xbe, 0x47, 0xa7, 0xb6, 0xac, 0x3e, 0xaa, 0xc8, 0x2e, 0x2f, 0x6f} UnitStopped = sd_id128.ID128{0x9d, 0x1a, 0xaa, 0x27, 0xd6, 0x01, 0x40, 0xbd, 0x96, 0x36, 0x54, 0x38, 0xaa, 0xd2, 0x02, 0x86} - UnitFailed = sd_id128.ID128{0xbe, 0x02, 0xcf, 0x68, 0x55, 0xd2, 0x42, 0x8b, 0xa4, 0x0d, 0xf7, 0xe9, 0xd0, 0x22, 0xf0, 0x3d} UnitReloading = sd_id128.ID128{0xd3, 0x4d, 0x03, 0x7f, 0xff, 0x18, 0x47, 0xe6, 0xae, 0x66, 0x9a, 0x37, 0x0e, 0x69, 0x47, 0x25} UnitReloaded = sd_id128.ID128{0x7b, 0x05, 0xeb, 0xc6, 0x68, 0x38, 0x42, 0x22, 0xba, 0xa8, 0x88, 0x11, 0x79, 0xcf, 0xda, 0x54} @@ -47,8 +47,13 @@ var ( UnitResources = sd_id128.ID128{0xae, 0x8f, 0x7b, 0x86, 0x6b, 0x03, 0x47, 0xb9, 0xaf, 0x31, 0xfe, 0x1c, 0x80, 0xb1, 0x27, 0xc0} + UnitSuccess = sd_id128.ID128{0x7a, 0xd2, 0xd1, 0x89, 0xf7, 0xe9, 0x4e, 0x70, 0xa3, 0x8c, 0x78, 0x13, 0x54, 0x91, 0x24, 0x48} + UnitFailureResult = sd_id128.ID128{0xd9, 0xb3, 0x73, 0xed, 0x55, 0xa6, 0x4f, 0xeb, 0x82, 0x42, 0xe0, 0x2d, 0xbe, 0x79, 0xa4, 0x9c} + SpawnFailed = sd_id128.ID128{0x64, 0x12, 0x57, 0x65, 0x1c, 0x1b, 0x4e, 0xc9, 0xa8, 0x62, 0x4d, 0x7a, 0x40, 0xa9, 0xe1, 0xe7} + UnitProcessExit = sd_id128.ID128{0x98, 0xe3, 0x22, 0x20, 0x3f, 0x7a, 0x4e, 0xd2, 0x90, 0xd0, 0x9f, 0xe0, 0x3c, 0x09, 0xfe, 0x15} + ForwardSyslogMissed = sd_id128.ID128{0x00, 0x27, 0x22, 0x9c, 0xa0, 0x64, 0x41, 0x81, 0xa7, 0x6c, 0x4e, 0x92, 0x45, 0x8a, 0xfa, 0x2e} Overmounting = sd_id128.ID128{0x1d, 0xee, 0x03, 0x69, 0xc7, 0xfc, 0x47, 0x36, 0xb7, 0x09, 0x9b, 0x38, 0xec, 0xb4, 0x6e, 0xe7} diff --git a/sd_messages/messages.go.gen b/sd_messages/messages.go.gen index ecd2e2f..534213f 100755 --- a/sd_messages/messages.go.gen +++ b/sd_messages/messages.go.gen @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Copyright (C) 2017-2018 Luke Shumaker <lukeshu@sbcglobal.net> +# Copyright (C) 2017-2018 Luke Shumaker <lukeshu@lukeshu.cm> # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. |