summaryrefslogtreecommitdiff
path: root/sd_daemon
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-12-19 13:45:53 -0500
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-12-19 13:45:53 -0500
commita04b84778871d77e539a675acbff05f7220e2f5e (patch)
tree4b6c54cdf6d6d5b3c3ef084f604615b2dc2b9c47 /sd_daemon
parent4a3a5525172afd69bc674a2a344065cd9e9a5888 (diff)
Improve documentation.
Diffstat (limited to 'sd_daemon')
-rw-r--r--sd_daemon/booted.go4
-rw-r--r--sd_daemon/doc.go2
-rw-r--r--sd_daemon/log.go39
-rw-r--r--sd_daemon/log_test.go12
-rwxr-xr-xsd_daemon/log_util.go.gen8
-rw-r--r--sd_daemon/lsb/exit-status.go2
-rw-r--r--sd_daemon/notify.go9
-rw-r--r--sd_daemon/watchdog.go11
8 files changed, 47 insertions, 40 deletions
diff --git a/sd_daemon/booted.go b/sd_daemon/booted.go
index bfbc685..b833b45 100644
--- a/sd_daemon/booted.go
+++ b/sd_daemon/booted.go
@@ -17,11 +17,11 @@ package sd_daemon
import "os"
-// Returns whether the operating system booted using the Systemd init
+// Returns whether the operating system booted using the systemd init
// system.
//
// Please do not use this function. All of the other functionality in
-// this package uses interfaces that are not Systemd-specific.
+// this package uses interfaces that are not systemd-specific.
func SdBooted() bool {
fi, err := os.Lstat("/run/systemd/system")
return err != nil && fi.IsDir()
diff --git a/sd_daemon/doc.go b/sd_daemon/doc.go
index 8f1fb00..90616f6 100644
--- a/sd_daemon/doc.go
+++ b/sd_daemon/doc.go
@@ -21,7 +21,7 @@
// be much simpler; modern versions of the daemon(7) page on GNU/Linux
// systems also describe "new-style" daemons. Though many of the
// mechanisms described there and implemented here originated with
-// Systemd, they are all very simple mechanisms which can easily be
+// systemd, they are all very simple mechanisms which can easily be
// implemented with a variety of service managers.
//
// [daemon(7)]: https://www.freedesktop.org/software/systemd/man/daemon.html
diff --git a/sd_daemon/log.go b/sd_daemon/log.go
index 9fa62d4..9b23f31 100644
--- a/sd_daemon/log.go
+++ b/sd_daemon/log.go
@@ -25,14 +25,21 @@ import (
"sync"
)
-// Logger writes "<N>"-prefixed lines to an io.Writer, where N is a
+// A Logger writes "<N>"-prefixed lines to an io.Writer, where N is a
// syslog priority number. It implements mostly the same interface as
-// "log/syslog".Writer.
+// syslog.Writer.
//
// You probably don't need any instance of this other than the
// constant "Log", which uses os.Stderr as the writer. It is
// implemented as a struct rather than a set of functions so that it
// can be passed around as an implementation of an interface.
+//
+// Each logging operation makes a single call to the underlying
+// Writer's Write method. A single logging operation may be multiple
+// lines; each line will have the prefix, and they will all be written
+// in the same Write. A Logger can be used simultaneously from
+// multiple goroutines; it guarantees to serialize access to the
+// Writer.
type Logger struct {
mu sync.Mutex
out io.Writer
@@ -44,9 +51,9 @@ func NewLogger(w io.Writer) *Logger {
return &Logger{out: w}
}
-// Log is a Logger that use used very similarly to
-// "log/syslog".Writer, but writes to os.Stderr under the assumption
-// that stderr is forwarded to syslog/journald.
+// Log is a Logger whose interface is very similar to syslog.Writer,
+// but writes to os.Stderr under the assumption that stderr is
+// forwarded to syslog (or journald).
//
// You are encouraged to use stderr unless you have a good reason to
// talk to syslog or journald directly.
@@ -56,7 +63,7 @@ var Log = NewLogger(os.Stderr)
//
// *buf = append(*buf, fmt.Sprintf("<%d>", n)...)
func appendPrefix(buf []byte, n syslog.Priority) []byte {
- var b [22]byte // 21 = ceil(log_10(2^64))+len("<>")
+ var b [22]byte // 22 = ceil(log_10(2^64))+len("<>")
b[len(b)-1] = '>'
i := len(b) - 2
for n >= 10 {
@@ -72,7 +79,7 @@ func appendPrefix(buf []byte, n syslog.Priority) []byte {
// WriteString writes a message with the specified priority to the
// log.
-func (l *Logger) WriteString(level syslog.Priority, msg string) (n int, err error) {
+func (l *Logger) WriteString(pri syslog.Priority, msg string) (n int, err error) {
l.mu.Lock()
defer l.mu.Unlock()
@@ -80,12 +87,12 @@ func (l *Logger) WriteString(level syslog.Priority, msg string) (n int, err erro
// The following is a cheap version of:
//
- // prefix := fmt.Sprintf("<%d>", level)
+ // prefix := fmt.Sprintf("<%d>", pri)
// buf := prefix + strings.Replace(msg, "\n", "\n"+prefix, -1)
// return io.WriteString(l.out, buf)
l.buf = l.buf[:0]
- l.buf = appendPrefix(l.buf, level) // possible allocation
+ l.buf = appendPrefix(l.buf, pri) // possible allocation
prefix := l.buf
nlines := strings.Count(msg, "\n") + 1
n = len(msg) + len(prefix)*nlines + 1
@@ -109,7 +116,7 @@ func (l *Logger) WriteString(level syslog.Priority, msg string) (n int, err erro
// WriteString writes a message with the specified priority to the
// log.
-func (l *Logger) WriteBytes(level syslog.Priority, msg []byte) (n int, err error) {
+func (l *Logger) WriteBytes(pri syslog.Priority, msg []byte) (n int, err error) {
// Copy/pasted from WriteString and
// * `strings.` -> `bytes.`
// * `"\n"` -> `[]byte{'\n'}`
@@ -119,7 +126,7 @@ func (l *Logger) WriteBytes(level syslog.Priority, msg []byte) (n int, err error
msg = bytes.TrimSuffix(msg, []byte{'\n'})
l.buf = l.buf[:0]
- l.buf = appendPrefix(l.buf, level) // possible allocation
+ l.buf = appendPrefix(l.buf, pri) // possible allocation
prefix := l.buf
nlines := bytes.Count(msg, []byte{'\n'}) + 1
n = len(msg) + len(prefix)*nlines + 1
@@ -142,16 +149,16 @@ func (l *Logger) WriteBytes(level syslog.Priority, msg []byte) (n int, err error
}
type loggerWriter struct {
- log *Logger
- level syslog.Priority
+ log *Logger
+ pri syslog.Priority
}
func (lw loggerWriter) Write(p []byte) (n int, err error) {
- return lw.log.WriteBytes(lw.level, p)
+ return lw.log.WriteBytes(lw.pri, p)
}
// Writer returns an io.Writer that writes messages with the specified
// priority to the log.
-func (l *Logger) Writer(level syslog.Priority) io.Writer {
- return loggerWriter{log: l, level: level}
+func (l *Logger) Writer(pri syslog.Priority) io.Writer {
+ return loggerWriter{log: l, pri: pri}
}
diff --git a/sd_daemon/log_test.go b/sd_daemon/log_test.go
index e2fdef9..257ccb9 100644
--- a/sd_daemon/log_test.go
+++ b/sd_daemon/log_test.go
@@ -33,8 +33,8 @@ func TestLog(t *testing.T) {
}))
type testcase struct {
- level syslog.Priority
- msg string
+ pri syslog.Priority
+ msg string
}
testcases := map[testcase]string{
@@ -52,18 +52,18 @@ func TestLog(t *testing.T) {
for in, out := range testcases {
written = nil
- n, err := log.WriteString(in.level, in.msg)
+ n, err := log.WriteString(in.pri, in.msg)
if n != len(out) || string(written) != out || err != nil {
t.Errorf("WriteString(%#v, %#v)\n -> expected:{%#v, %#v, %#v}\n -> got:{%#v, %#v, %#v}\n",
- in.level, in.msg,
+ in.pri, in.msg,
len(out), nil, out,
n, err, string(written))
}
written = nil
- n, err = log.WriteBytes(in.level, []byte(in.msg))
+ n, err = log.WriteBytes(in.pri, []byte(in.msg))
if n != len(out) || string(written) != out || err != nil {
t.Errorf("WriteBytes(%#v, %#v)\n -> expected:{%#v, %#v, %#v}\n -> got:{%#v, %#v, %#v}\n",
- in.level, in.msg,
+ in.pri, in.msg,
len(out), nil, out,
n, err, string(written))
}
diff --git a/sd_daemon/log_util.go.gen b/sd_daemon/log_util.go.gen
index 95d56b0..3ca63dc 100755
--- a/sd_daemon/log_util.go.gen
+++ b/sd_daemon/log_util.go.gen
@@ -24,11 +24,11 @@ import "log/syslog"
EOF
- for level in Emerg Alert Crit Err Warning Notice Info Debug; do
+ for pri in Emerg Alert Crit Err Warning Notice Info Debug; do
cat <<EOF
-// $level writes a message with priority syslog.LOG_${level^^} to the log.
-func (l *Logger) $level(msg string) error {
- _, err := l.WriteString(syslog.LOG_${level^^}, msg)
+// $pri writes a message with priority syslog.LOG_${pri^^} to the log.
+func (l *Logger) $pri(msg string) error {
+ _, err := l.WriteString(syslog.LOG_${pri^^}, msg)
return err
}
diff --git a/sd_daemon/lsb/exit-status.go b/sd_daemon/lsb/exit-status.go
index fbbb5e3..9004dc3 100644
--- a/sd_daemon/lsb/exit-status.go
+++ b/sd_daemon/lsb/exit-status.go
@@ -40,7 +40,7 @@ const (
// 100-149 are reserved for distribution use
// 150-199 are reserved for application use
// 200-254 are reserved for init system use
-
+ //
// Therefore, the following are taken from systemd's
// `src/basic/exit-status.h`
EXIT_CHDIR uint8 = 200
diff --git a/sd_daemon/notify.go b/sd_daemon/notify.go
index 76ffe81..1d72cc3 100644
--- a/sd_daemon/notify.go
+++ b/sd_daemon/notify.go
@@ -41,14 +41,15 @@ import (
//
// It is possible to include a set of file descriptors with the
// message. This is useful for keeping files open across restarts, as
-// it enables the service manager will pass those files to the new
+// it enables the service manager to pass those files to the new
// process when it is restarted (see ListenFds). Note: The service
// manager will only actually store the file descriptors if you
-// include "FDSTORE=1" in the state.
+// include "FDSTORE=1" in the state (again, see sd_notify(3) for
+// well-known variable assignments).
//
// If the service manager is not listening for notifications from this
-// process (or this has already been called with unsetEnv=true), then
-// ErrDisabled is returned. If the service manager appears to be
+// process tree (or this has already been called with unsetEnv=true),
+// then ErrDisabled is returned. If the service manager appears to be
// listening, but there is an error sending the message, then that
// error is returned. It is generally recommended that you ignore the
// return value: if there is an error, this is function no-op; meaning
diff --git a/sd_daemon/watchdog.go b/sd_daemon/watchdog.go
index b32f470..be96970 100644
--- a/sd_daemon/watchdog.go
+++ b/sd_daemon/watchdog.go
@@ -31,12 +31,11 @@ import (
// WATCHDOG_USEC and WATCHDOG_PID, which will cause further calls to
// this function to fail.
//
-// If an error is returned, then the duration is 0. If the service
-// manager is not expecting a keep-alive notification from this
-// process (or if this has already been called with unsetEnv=true),
-// then ErrDisabled is returned. If there is an error parsing the
-// service manager's watchdog request, then an appropriate other error
-// is returned.
+// If an error is not returned, then the duration returned is greater
+// than 0; if an error is returned, then the duration is 0. If the
+// service manager is not expecting a keep-alive notification from
+// this process (or if this has already been called with
+// unsetEnv=true), then the error is ErrDisabled.
func WatchdogEnabled(unsetEnv bool) (time.Duration, error) {
if unsetEnv {
defer func() {