mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 14:15:06 +03:00
Add logic to notify service manager when start up is completed
This commit is contained in:
parent
fec96a38a4
commit
7878bb938e
5 changed files with 67 additions and 0 deletions
|
@ -272,6 +272,10 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, err = notifyStartupCompleted(); err != nil {
|
||||||
|
log.Warnln("Error while sending start up notification:", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Block until we are told to shut down.
|
// Block until we are told to shut down.
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
|
|
||||||
|
|
50
cmd/yggdrasil/notify_startup_linux.go
Normal file
50
cmd/yggdrasil/notify_startup_linux.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
//go:build linux
|
||||||
|
// +build linux
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Notify systemd daemon when start up is completed.
|
||||||
|
// Required to ensure that dependent services are started only after TUN interface is ready.
|
||||||
|
//
|
||||||
|
// One of the following is returned:
|
||||||
|
// (false, nil) - notification not supported (i.e. `notifySocketEnv` is unset)
|
||||||
|
// (false, err) - notification supported, but failure happened (e.g. error connecting to `notifySocketEnv` or while sending data)
|
||||||
|
// (true, nil) - notification supported, data has been sent
|
||||||
|
//
|
||||||
|
// Based on `SdNotify` from [`coreos/go-systemd`](https://github.com/coreos/go-systemd/blob/7d375ecc2b092916968b5601f74cca28a8de45dd/daemon/sdnotify.go#L56)
|
||||||
|
func notifyStartupCompleted() (bool, error) {
|
||||||
|
const (
|
||||||
|
notifyReady = "READY=1"
|
||||||
|
notifySocketEnv = "NOTIFY_SOCKET"
|
||||||
|
)
|
||||||
|
|
||||||
|
socketAddr := &net.UnixAddr{
|
||||||
|
Name: os.Getenv(notifySocketEnv),
|
||||||
|
Net: "unixgram",
|
||||||
|
}
|
||||||
|
|
||||||
|
// `notifySocketEnv` not set
|
||||||
|
if socketAddr.Name == "" {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Unsetenv(notifySocketEnv)
|
||||||
|
|
||||||
|
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
|
||||||
|
// Error connecting to `notifySocketEnv`
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
if _, err = conn.Write([]byte(notifyReady)); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
8
cmd/yggdrasil/notify_startup_other.go
Normal file
8
cmd/yggdrasil/notify_startup_other.go
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
//go:build !linux
|
||||||
|
// +build !linux
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
func notifyStartupCompleted() (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
|
@ -6,6 +6,10 @@ After=network-online.target
|
||||||
After=yggdrasil-default-config.service
|
After=yggdrasil-default-config.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
|
Type=notify
|
||||||
|
# Allow forked off processes to send notifications.
|
||||||
|
# Uncomment if e.g. yggdrasil is started by a script.
|
||||||
|
#NotifyAccess=all
|
||||||
Group=yggdrasil
|
Group=yggdrasil
|
||||||
ProtectHome=true
|
ProtectHome=true
|
||||||
ProtectSystem=true
|
ProtectSystem=true
|
||||||
|
|
|
@ -6,6 +6,7 @@ After=network-online.target
|
||||||
After=yggdrasil-default-config.service
|
After=yggdrasil-default-config.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
|
Type=notify
|
||||||
Group=yggdrasil
|
Group=yggdrasil
|
||||||
ProtectHome=true
|
ProtectHome=true
|
||||||
ProtectSystem=strict
|
ProtectSystem=strict
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue