From a1d375a67209e0288a363bef107d100aa476b5af Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Tue, 28 Mar 2017 15:52:37 +0200 Subject: [PATCH 1/2] Fix error checking and add a recover --- fsm.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/fsm.go b/fsm.go index 045c656d8..a795514b2 100644 --- a/fsm.go +++ b/fsm.go @@ -129,29 +129,31 @@ func (r *fsmS) announceSensor(e *f.Event) { log.debug("announcing sensor to the agent") go func(cb func(b bool, from *FromS)) { + defer func() { + if r := recover(); r != nil { + log.debug("Announce recovered:", r) + } + }() d := &Discovery{PID: os.Getpid()} - d.Name, d.Args = getCommandLine() var socket net.Conn if _, err := os.Stat("/proc"); err == nil { - socket, err := net.Dial("tcp", r.agent.host+":42699") - if err != nil { - log.error(err) - } + if socket, err := net.Dial("tcp", r.agent.host+":42699"); err == nil { - tcpConn := socket.(*net.TCPConn) - f, err := tcpConn.File() + tcpConn := socket.(*net.TCPConn) + f, err := tcpConn.File() - if err != nil { - log.error(err) - } else { - d.Fd = fmt.Sprintf("%v", f.Fd()) + if err != nil { + log.error(err) + } else { + d.Fd = fmt.Sprintf("%v", f.Fd()) - link := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), f.Fd()) - if _, err := os.Stat(link); err == nil { - d.Inode, _ = os.Readlink(link) + link := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), f.Fd()) + if _, err := os.Stat(link); err == nil { + d.Inode, _ = os.Readlink(link) + } } } } From cc534f70ee758030b76282fb0ea39ba0f49452cc Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Tue, 28 Mar 2017 18:09:34 +0200 Subject: [PATCH 2/2] Use DialTCP instead to avoid type assertion --- fsm.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/fsm.go b/fsm.go index a795514b2..72e7f64fa 100644 --- a/fsm.go +++ b/fsm.go @@ -138,21 +138,22 @@ func (r *fsmS) announceSensor(e *f.Event) { d := &Discovery{PID: os.Getpid()} d.Name, d.Args = getCommandLine() - var socket net.Conn if _, err := os.Stat("/proc"); err == nil { - if socket, err := net.Dial("tcp", r.agent.host+":42699"); err == nil { + if addr, err := net.ResolveTCPAddr("tcp", r.agent.host+":42699"); err == nil { + if tcpConn, err := net.DialTCP("tcp", nil, addr); err == nil { + defer tcpConn.Close() - tcpConn := socket.(*net.TCPConn) - f, err := tcpConn.File() + f, err := tcpConn.File() - if err != nil { - log.error(err) - } else { - d.Fd = fmt.Sprintf("%v", f.Fd()) + if err != nil { + log.error(err) + } else { + d.Fd = fmt.Sprintf("%v", f.Fd()) - link := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), f.Fd()) - if _, err := os.Stat(link); err == nil { - d.Inode, _ = os.Readlink(link) + link := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), f.Fd()) + if _, err := os.Stat(link); err == nil { + d.Inode, _ = os.Readlink(link) + } } } } @@ -164,10 +165,6 @@ func (r *fsmS) announceSensor(e *f.Event) { &FromS{ PID: strconv.Itoa(int(ret.Pid)), HostID: ret.HostID}) - - if socket != nil { - socket.Close() - } }(cb) }