Skip to content

Commit

Permalink
simplify files organization
Browse files Browse the repository at this point in the history
  • Loading branch information
hekmon committed Sep 20, 2020
1 parent 55253e5 commit 2675a57
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 86 deletions.
66 changes: 51 additions & 15 deletions notify.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,70 @@
// +build linux

package systemd

import (
"fmt"
"net"
"os"
)

var socket *net.UnixAddr
var notifySocket *net.UnixAddr

func init() {
notifySocket := os.Getenv("NOTIFY_SOCKET")
if notifySocket != "" {
return
}
socket = &net.UnixAddr{
Name: notifySocket,
Net: "unixgram",
}
// IsNotifyEnabled tells if systemd notify socket has been detected or not.
func IsNotifyEnabled() bool {
return notifySocket != nil
}

// NotifyReady sends systemd notify READY=1
func NotifyReady() error {
return NotifyRaw("READY=1")
}

// NotifyReloading sends systemd notify RELOADING=1
func NotifyReloading() error {
return NotifyRaw("RELOADING=1")
}

// NotifyStopping sends systemd notify STOPPING=1
func NotifyStopping() error {
return NotifyRaw("STOPPING=1")
}

// NotifyStatus sends systemd notify STATUS=%s{status}
func NotifyStatus(status string) error {
return NotifyRaw(fmt.Sprintf("STATUS=%s", status))
}

// NotifyErrNo sends systemd notify ERRNO=%d{errno}
func NotifyErrNo(errno int) error {
return NotifyRaw(fmt.Sprintf("ERRNO=%d", errno))
}

// NotifyBusError sends systemd notify BUSERROR=%s{buserror}
func NotifyBusError(buserror string) error {
return NotifyRaw(fmt.Sprintf("BUSERROR=%s", buserror))
}

// NotifyMainPID sends systemd notify MAINPID=%d{mainpid}
func NotifyMainPID(mainpid int) error {
return NotifyRaw(fmt.Sprintf("MAINPID=%d", mainpid))
}

// NotifyWatchDog sends systemd notify WATCHDOG=1
func NotifyWatchDog() error {
return NotifyRaw("WATCHDOG=1")
}

// NotifyWatchDogUSec sends systemd notify WATCHDOG_USEC=%d{µsec}
func NotifyWatchDogUSec(usec int64) error {
return NotifyRaw(fmt.Sprintf("WATCHDOG_USEC=%d", usec))
}

// NotifyRaw send state thru the notify socket if any.
// If the notify socket was not detected, it is a noop call.
// Use IsNotifyEnabled() to determine if the notify socket has been detected.
func NotifyRaw(state string) error {
if socket == nil {
if notifySocket == nil {
return nil
}
conn, err := net.DialUnix(socket.Net, nil, socket)
conn, err := net.DialUnix(notifySocket.Net, nil, notifySocket)
if err != nil {
return fmt.Errorf("can't open unix socket: %v", err)
}
Expand Down
53 changes: 0 additions & 53 deletions notify_helpers.go

This file was deleted.

17 changes: 17 additions & 0 deletions notify_init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build linux

package systemd

import (
"net"
"os"
)

func init() {
if notifySocketName := os.Getenv("NOTIFY_SOCKET"); notifySocketName != "" {
notifySocket = &net.UnixAddr{
Name: notifySocketName,
Net: "unixgram",
}
}
}
17 changes: 0 additions & 17 deletions notify_noop.go

This file was deleted.

2 changes: 1 addition & 1 deletion watchdog.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func getWatchDogInterval() (interval time.Duration, err error) {

// SendHeartbeat sends a keepalive notification to systemd watchdog
func (c *WatchDog) SendHeartbeat() error {
if socket == nil {
if notifySocket == nil {
return errors.New("failed to notify watchdog: systemd notify is diabled")
}
return NotifyWatchDog()
Expand Down

0 comments on commit 2675a57

Please sign in to comment.