-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for systemd Tweek systemd * Add exec start args * Fix data race
- Loading branch information
1 parent
b18de14
commit 8cf1d04
Showing
8 changed files
with
145 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2021 Wayback Archiver. All rights reserved. | ||
// Use of this source code is governed by the GNU GPL v3 | ||
// license that can be found in the LICENSE file. | ||
|
||
/* | ||
Package systemd provides a Go implementation of the sd_notify protocol. | ||
It can be used to inform systemd of service start-up completion. | ||
*/ | ||
package systemd // import "github.com/wabarc/wayback/systemd" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2021 Wayback Archiver. All rights reserved. | ||
// Use of this source code is governed by the GNU GPL v3 | ||
// license that can be found in the LICENSE file. | ||
|
||
package systemd // import "github.com/wabarc/wayback/systemd" | ||
|
||
import ( | ||
"net" | ||
"os" | ||
) | ||
|
||
// SdNotifyReady tells the service manager that service startup is | ||
// finished, or the service finished loading its configuration. | ||
// https://www.freedesktop.org/software/systemd/man/sd_notify.html#READY=1 | ||
const SdNotifyReady = "READY=1" | ||
|
||
// HasNotifySocket checks if the process is supervised by Systemd and has the notify socket. | ||
func HasNotifySocket() bool { | ||
return os.Getenv("NOTIFY_SOCKET") != "" | ||
} | ||
|
||
// SdNotify sends a message to systemd using the sd_notify protocol. | ||
// See https://www.freedesktop.org/software/systemd/man/sd_notify.html. | ||
func SdNotify(state string) error { | ||
addr := &net.UnixAddr{ | ||
Net: "unixgram", | ||
Name: os.Getenv("NOTIFY_SOCKET"), | ||
} | ||
|
||
// We're not running under systemd (NOTIFY_SOCKET is not set). | ||
if addr.Name == "" { | ||
return nil | ||
} | ||
|
||
conn, err := net.DialUnix(addr.Net, nil, addr) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
|
||
if _, err = conn.Write([]byte(state)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2021 Wayback Archiver. All rights reserved. | ||
// Use of this source code is governed by the GNU GPL v3 | ||
// license that can be found in the LICENSE file. | ||
|
||
package systemd // import "github.com/wabarc/wayback/systemd" | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestSdNotify(t *testing.T) { | ||
testDir, err := ioutil.TempDir("", "test-") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(testDir) | ||
|
||
notifySocket := testDir + "/notify-socket.sock" | ||
laddr := net.UnixAddr{ | ||
Name: notifySocket, | ||
Net: "unixgram", | ||
} | ||
if _, err := net.ListenUnixgram("unixgram", &laddr); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
tests := []struct { | ||
envSocket string | ||
|
||
werr bool | ||
}{ | ||
// (nil) - notification supported, data has been sent | ||
{notifySocket, false}, | ||
// (err) - notification supported, but failure happened | ||
{testDir + "/missing.sock", true}, | ||
// (nil) - notification not supported | ||
{"", false}, | ||
} | ||
|
||
for i, tt := range tests { | ||
os.Unsetenv("NOTIFY_SOCKET") | ||
|
||
if tt.envSocket != "" { | ||
os.Setenv("NOTIFY_SOCKET", tt.envSocket) | ||
} | ||
err := SdNotify(fmt.Sprintf("TestSdNotify test message #%d", i)) | ||
|
||
if tt.werr && err == nil { | ||
t.Errorf("#%d: want non-nil err, got nil", i) | ||
} else if !tt.werr && err != nil { | ||
t.Errorf("#%d: want nil err, got %v", i, err) | ||
} | ||
} | ||
} |