summaryrefslogtreecommitdiff
path: root/nslcd_systemd
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2017-12-18 23:08:41 -0500
committerLuke Shumaker <lukeshu@lukeshu.com>2017-12-18 23:08:41 -0500
commit02a94b4d90531bca217d6913bd3f1325f3742ff6 (patch)
tree79d3e2b4bb96592354000deefae6d383fd6fa9ba /nslcd_systemd
parent055b10405ea02fb7adb3867141dc56464fda9a90 (diff)
nslcd_systemd: give each connection a unique ID prefix for logging
Diffstat (limited to 'nslcd_systemd')
-rw-r--r--nslcd_systemd/log.go45
-rw-r--r--nslcd_systemd/nslcd_systemd.go25
2 files changed, 54 insertions, 16 deletions
diff --git a/nslcd_systemd/log.go b/nslcd_systemd/log.go
new file mode 100644
index 0000000..8a646f8
--- /dev/null
+++ b/nslcd_systemd/log.go
@@ -0,0 +1,45 @@
+// Copyright (C) 2017 Luke Shumaker <lukeshu@sbcglobal.net>
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+// 02110-1301 USA
+
+package nslcd_systemd
+
+import (
+ "io"
+
+ "git.lukeshu.com/go/libnslcd/nslcd_server"
+)
+
+type PrefixLogger struct {
+ Prefix string
+ Logger nslcd_server.Logger
+}
+
+func (pl PrefixLogger) Emerg(m string) error { return pl.Logger.Emerg(pl.Prefix + m) }
+func (pl PrefixLogger) Alert(m string) error { return pl.Logger.Alert(pl.Prefix + m) }
+func (pl PrefixLogger) Crit(m string) error { return pl.Logger.Crit(pl.Prefix + m) }
+func (pl PrefixLogger) Err(m string) error { return pl.Logger.Err(pl.Prefix + m) }
+func (pl PrefixLogger) Warning(m string) error { return pl.Logger.Warning(pl.Prefix + m) }
+func (pl PrefixLogger) Notice(m string) error { return pl.Logger.Notice(pl.Prefix + m) }
+func (pl PrefixLogger) Info(m string) error { return pl.Logger.Info(pl.Prefix + m) }
+func (pl PrefixLogger) Debug(m string) error { return pl.Logger.Debug(pl.Prefix + m) }
+
+func (pl PrefixLogger) Write(m []byte) (int, error) {
+ return pl.Logger.Write(append([]byte(pl.Prefix), m...))
+}
+func (pl PrefixLogger) WriteString(m string) (int, error) {
+ return io.WriteString(pl.Logger, pl.Prefix+m)
+}
diff --git a/nslcd_systemd/nslcd_systemd.go b/nslcd_systemd/nslcd_systemd.go
index 29d49d6..91177a7 100644
--- a/nslcd_systemd/nslcd_systemd.go
+++ b/nslcd_systemd/nslcd_systemd.go
@@ -61,15 +61,6 @@ type Backend interface {
Close()
}
-type contextKey struct {
- name string
-}
-
-var (
- // ConnectionIDKey is a context key.
- ConnectionIDKey = &contextKey{"connection-id"}
-)
-
func get_socket() (socket net.Listener, err error) {
fds := sd_daemon.ListenFds(true)
if fds == nil {
@@ -106,13 +97,16 @@ func getpeercred(conn *net.UnixConn) (cred unix.Ucred, err error) {
return
}
-func handler(backend nslcd_server.Backend, limits nslcd_server.Limits, conn *net.UnixConn, ctx context.Context) {
+func handler(backend nslcd_server.Backend, limits nslcd_server.Limits, conn *net.UnixConn, cid int, ctx context.Context) {
defer conn.Close()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
- // TODO: override nslcd_server.LoggerKey with a logger that includes ConnectionIDKey
- log := nslcd_server.LoggerFromContext(ctx)
+ log := PrefixLogger{
+ Prefix: fmt.Sprintf("[%v] ", cid),
+ Logger: nslcd_server.LoggerFromContext(ctx),
+ }
+ ctx = context.WithValue(ctx, nslcd_server.LoggerKey, log)
cred, err := getpeercred(conn)
if err != nil {
@@ -195,12 +189,11 @@ func Main(backend Backend, limits nslcd_server.Limits, ctx context.Context) uint
if conn != nil {
wg.Add(1)
id++
- hctx := context.WithValue(ctx, ConnectionIDKey, id)
- go func() {
+ go func(id int) {
defer sd_daemon.Recover()
defer wg.Done()
- handler(backend, limits, conn.(*net.UnixConn), hctx)
- }()
+ handler(backend, limits, conn.(*net.UnixConn), id, ctx)
+ }(id)
}
}
}()