From b58ea042394c66eabe67c3f58906c5d76b1e119d Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 7 Sep 2017 23:28:47 -0400 Subject: nslcd_{server,systemd}: FIX, BREAKING CHANGE: add limits Added types: nslcd_server: type Limits struct { ...} nslcd_server: type Conn interface{ ... } // a subset of net.Conn nslcd_server.HandleRequest() signature change: -func HandleRequest(backend Backend, in io.Reader, out io.Writer, cred unix.Ucred) (err error) { +func HandleRequest(backend Backend, limits Limits, conn Conn, cred unix.Ucred) (err error) { The `limits Limits` argument is added, and `conn Conn` replaces `in io.Reader` and `out io.Writer`. nslcd_systemd.Main() signature change: -func Main(backend Backend) uint8 { +func Main(backend Backend, limits nslcd_server.Limits) uint8 { The `limits Limits` argument is added. --- nslcd_server/func_handlerequest.go.gen | 64 ++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 10 deletions(-) (limited to 'nslcd_server') diff --git a/nslcd_server/func_handlerequest.go.gen b/nslcd_server/func_handlerequest.go.gen index d34db88..af36e84 100755 --- a/nslcd_server/func_handlerequest.go.gen +++ b/nslcd_server/func_handlerequest.go.gen @@ -26,8 +26,8 @@ package nslcd_server import ( "fmt" - "io" "os" + "time" "golang.org/x/sys/unix" p "git.lukeshu.com/go/libnslcd/nslcd_proto" @@ -41,8 +41,32 @@ func maybePanic(err error) { } } +type Limits struct { + // What is the maximum total amount of time that we spend + // handling a single request. This includes both the time + // reading the request and the time creating and writing the + // response. + Timeout time.Duration + + // How long can we spend reading a request? + ReadTimeout time.Duration + + // How long can we spend writing a response? + WriteTimeout time.Duration +} + +type Conn interface { + // This is a subset of net.Conn; semantics are the same. + + Read(b []byte) (n int, err error) + Write(b []byte) (n int, err error) + SetDeadline(t time.Time) error + SetReadDeadline(t time.Time) error + SetWriteDeadline(t time.Time) error +} + // Handle a request to nslcd -func HandleRequest(backend Backend, in io.Reader, out io.Writer, cred unix.Ucred) (err error) { +func HandleRequest(backend Backend, limits Limits, conn Conn, cred unix.Ucred) (err error) { defer func() { if r := recover(); r != nil { switch r := r.(type) { @@ -54,13 +78,27 @@ func HandleRequest(backend Backend, in io.Reader, out io.Writer, cred unix.Ucred } }() + now := time.Now() + if limits.Timeout != 0 { + err = conn.SetDeadline(now.Add(limits.Timeout)) + if err != nil { + return err + } + } + if limits.ReadTimeout != 0 { + err = conn.SetReadDeadline(now.Add(limits.ReadTimeout)) + if err != nil { + return err + } + } + var version int32 - maybePanic(p.Read(in, &version)) + maybePanic(p.Read(conn, &version)) if version != p.NSLCD_VERSION { return p.NslcdError(fmt.Sprintf("Version mismatch: server=%#08x client=%#08x", p.NSLCD_VERSION, version)) } var action int32 - maybePanic(p.Read(in, &action)) + maybePanic(p.Read(conn, &action)) switch action { $( @@ -68,7 +106,7 @@ while read -r request; do cat <