-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
WIP: Implement systemd-notify protocol #21140
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
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -152,6 +152,54 @@ func (srv *Server) ListenAndServeTLSConfig(tlsConfig *tls.Config, serve ServeFun | |||||||||||||||||||||||||||||||||||||||||||||||||||||
return srv.Serve(serve) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
type NotifyMsg int8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
ReadyMsg NotifyMsg = iota | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
StoppingMsg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
func (msg *NotifyMsg) Bytes() []byte { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
switch msg { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
case ReadyMsg: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return []byte("READY=1") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
case StoppingMsg: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return []byte("STOPPING=1") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
// systemd notify protocol | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
func (srv *Server) Notify(msg NotifyMsg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
notifySocket := Os.Getenv("NOTIFY_SOCKET") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if notifySocket == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
log.Debug("no NOTIFY_SOCKET given") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
socketAddr := &net.UnixAddr{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Name: notifySocket, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Net: "unixgram", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
err := os.unsetenv("NOTIFY_SOCKET") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
log.Warn("failed to unsetenv NOTIFY_SOCKET: %v", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
log.Warn("failed to dial NOTIFY_SOCKET: %v", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
defer conn.close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
if _, err = conn.Write(msg.Bytes()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
log.Warn("failed to notify NOTIFY_SOCKET: %v", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+186
to
+198
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Serve accepts incoming HTTP connections on the wrapped listener l, creating a new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// service goroutine for each. The service goroutines read requests and then call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// handler to reply to them. Handler is typically nil, in which case the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -164,7 +212,9 @@ func (srv *Server) Serve(serve ServeFunction) error { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
defer log.Debug("Serve() returning... (PID: %d)", syscall.Getpid()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
srv.setState(stateRunning) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
GetManager().RegisterServer() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Notify(ReadyMsg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
err := serve(srv.listener) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Notify(StoppingMsg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
log.Debug("Waiting for connections to finish... (PID: %d)", syscall.Getpid()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
srv.wg.Wait() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
srv.setState(stateTerminate) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
As far as I can see, only the bytes are needed in your implementation.
So, why not skip the ints and make them strings/ byte arrays immediately?
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.
I was trying to pretend that golang has abstract data types but I probably should just give up and use byte strings.