File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 99 "os"
1010 "os/signal"
1111 "strconv"
12+ "syscall"
1213 "text/tabwriter"
1314 "time"
1415
@@ -106,6 +107,11 @@ func (c *CLI) Run(args []string) int {
106107 return exitCodeErr
107108 }
108109
110+ if err := setRLimitNoFile (); err != nil {
111+ fmt .Fprintf (c .errStream , "%v" , err )
112+ return exitCodeErr
113+ }
114+
109115 if watch == 0 { // no watch option
110116 return c .run (processes , numeric , json , filter )
111117 }
@@ -191,6 +197,26 @@ func (c *CLI) PrintHostFlowsAsJSON(flows tcpflow.HostFlows) error {
191197 return nil
192198}
193199
200+ // setRLimitNoFile avoids too many open files error.
201+ func setRLimitNoFile () error {
202+ var rLimit syscall.Rlimit
203+
204+ err := syscall .Getrlimit (syscall .RLIMIT_NOFILE , & rLimit )
205+ if err != nil {
206+ return xerrors .Errorf ("could not get rlimit: %w" , err )
207+ }
208+
209+ if rLimit .Cur < rLimit .Max {
210+ rLimit .Cur = rLimit .Max
211+ err = syscall .Setrlimit (syscall .RLIMIT_NOFILE , & rLimit )
212+ if err != nil {
213+ return xerrors .Errorf ("could not set rlimit: %w" , err )
214+ }
215+ }
216+
217+ return nil
218+ }
219+
194220var helpText = `Usage: lstf [options]
195221
196222 Print TCP flows between localhost and other hosts
You can’t perform that action at this time.
0 commit comments