-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Notify service manager when start up is completed #1148
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,6 +272,10 @@ func main() { | |
} | ||
} | ||
|
||
if _, err = notifyStartupCompleted(); err != nil { | ||
log.Warnln("Error while sending start up notification:", err) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no guarantee at this point (in the main goroutine) that startup tasks in other goroutines have been finished, since they don't synchronise. So I think you are just racing against tun(4) setup just like |
||
// Block until we are told to shut down. | ||
<-ctx.Done() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package main | ||
|
||
func notifyStartupCompleted() (bool, error) { | ||
return false, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first
bool
return value is never used, so why have it at all?