From 91381d10a0a50ea8893509e6005024e913f469d6 Mon Sep 17 00:00:00 2001 From: Dominic Breuker Date: Tue, 13 Feb 2018 21:41:41 +0100 Subject: [PATCH] pass through arguments from command line --- Makefile | 16 ++++++++++++++++ cmd/root.go | 20 ++++++++++++++++---- internal/inotify/event.go | 13 +++++++++++-- internal/inotify/inotify.go | 12 ++++-------- internal/process/process.go | 6 ++++-- internal/pspy/pspy.go | 16 +++++++++------- 6 files changed, 60 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 3cd5b19..6ecc96c 100644 --- a/Makefile +++ b/Makefile @@ -6,3 +6,19 @@ build-dev: dev: docker run -it --rm -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy $(DEV_IMAGE) + +release: + docker run -it \ + --rm \ + -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ + --env CGO_ENABLED=0 \ + --env GOOS=linux \ + --env GOARCH=386 \ + $(DEV_IMAGE) go build -a -ldflags '-extldflags "-static"' -o pspy/bin/pspy32 pspy/main.go + docker run -it \ + --rm \ + -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ + --env CGO_ENABLED=0 \ + --env GOOS=linux \ + --env GOARCH=amd64 \ + $(DEV_IMAGE) go build -a -ldflags '-extldflags "-static"' -o pspy/bin/pspy64 pspy/main.go diff --git a/cmd/root.go b/cmd/root.go index ca636e1..e9f4a7d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "log" "os" "strings" @@ -39,18 +40,29 @@ var rootCmd = &cobra.Command{ var logPS, logFS bool var rDirs, dirs []string +var defaultRDirs = []string{ + "/usr", + "/tmp", + "/etc", + "/home", + "/var", + "/opt", +} +var defaultDirs = []string{} func init() { rootCmd.PersistentFlags().BoolVarP(&logPS, "procevents", "p", true, "print new processes to stdout") - rootCmd.PersistentFlags().BoolVarP(&logFS, "fsevents", "f", true, "print file system events to stdout") - rootCmd.PersistentFlags().StringArrayVarP(&rDirs, "recursive_dirs", "r", []string{"/tmp"}, "watch these dirs recursively") - rootCmd.PersistentFlags().StringArrayVarP(&dirs, "dirs", "d", []string{}, "watch these dirs") + rootCmd.PersistentFlags().BoolVarP(&logFS, "fsevents", "f", false, "print file system events to stdout") + rootCmd.PersistentFlags().StringArrayVarP(&rDirs, "recursive_dirs", "r", defaultRDirs, "watch these dirs recursively") + rootCmd.PersistentFlags().StringArrayVarP(&dirs, "dirs", "d", defaultDirs, "watch these dirs") + + log.SetOutput(os.Stdout) } func root(cmd *cobra.Command, args []string) { fmt.Printf("Watching recursively : %+v (%d)\n", rDirs, len(rDirs)) fmt.Printf("Watching non-recursively: %+v (%d)\n", dirs, len(dirs)) - fmt.Printf("Printing: processes=%t file-system events:%t\n", logPS, logFS) + fmt.Printf("Printing: processes=%t file-system events=%t\n", logPS, logFS) pspy.Watch(rDirs, dirs, logPS, logFS) } diff --git a/internal/inotify/event.go b/internal/inotify/event.go index ee7d252..62e8ad4 100644 --- a/internal/inotify/event.go +++ b/internal/inotify/event.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "strconv" + "time" "unsafe" "golang.org/x/sys/unix" @@ -55,7 +56,13 @@ func newEvent(name string, mask uint32) Event { return e } -func eventLogger(i *Inotify, buffers chan bufRead) { +func eventLogger(i *Inotify, buffers chan bufRead, print bool) { + // enable printing only after delay since setting up watchers causes flood of events + printEnabled := false + go func() { + <-time.After(1 * time.Second) + printEnabled = print + }() for bf := range buffers { n := bf.n buf := bf.buf @@ -82,7 +89,9 @@ func eventLogger(i *Inotify, buffers chan bufRead) { } ev := newEvent(name, sys.Mask) - log.Printf("\x1b[32;1mFS: %+v\x1b[0m", ev) + if printEnabled { + log.Printf("\x1b[32;1mFS: %+v\x1b[0m", ev) + } } } } diff --git a/internal/inotify/inotify.go b/internal/inotify/inotify.go index fca1f62..9fbcec8 100644 --- a/internal/inotify/inotify.go +++ b/internal/inotify/inotify.go @@ -13,7 +13,7 @@ type Inotify struct { paused bool } -func NewInotify(ping chan struct{}) (*Inotify, error) { +func NewInotify(ping chan struct{}, print bool) (*Inotify, error) { fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC) if fd == -1 { return nil, fmt.Errorf("Can't init inotify: %d", errno) @@ -25,15 +25,11 @@ func NewInotify(ping chan struct{}) (*Inotify, error) { ping: ping, paused: false, } - go watch(i) + go watch(i, print) return i, nil } -func (i *Inotify) Start() { - go watch(i) -} - func (i *Inotify) Watch(dir string) error { w, err := newWatcher(i.fd, dir, i.ping) if err != nil { @@ -79,10 +75,10 @@ type bufRead struct { buf []byte } -func watch(i *Inotify) { +func watch(i *Inotify, print bool) { buf := make([]byte, 5*unix.SizeofInotifyEvent) buffers := make(chan bufRead) - go eventLogger(i, buffers) + go eventLogger(i, buffers, print) for { n, _ := unix.Read(i.fd, buf) if !i.paused { diff --git a/internal/process/process.go b/internal/process/process.go index 3bd8265..75077cb 100644 --- a/internal/process/process.go +++ b/internal/process/process.go @@ -15,7 +15,7 @@ func NewProcList() *ProcList { return &pl } -func (pl ProcList) Refresh() error { +func (pl ProcList) Refresh(print bool) error { pids, err := getPIDs() if err != nil { return err @@ -33,7 +33,9 @@ func (pl ProcList) Refresh() error { if err != nil { uid = "???" } - log.Printf("\x1b[31;1mCMD: UID=%-4s PID=%-6d | %s\x1b[0m\n", uid, pid, cmd) + if print { + log.Printf("\x1b[31;1mCMD: UID=%-4s PID=%-6d | %s\x1b[0m\n", uid, pid, cmd) + } pl[pid] = cmd } } diff --git a/internal/pspy/pspy.go b/internal/pspy/pspy.go index 2789e5d..07649d3 100644 --- a/internal/pspy/pspy.go +++ b/internal/pspy/pspy.go @@ -1,7 +1,9 @@ package pspy import ( + "fmt" "log" + "os" "time" "github.com/dominicbreuker/pspy/internal/inotify" @@ -23,7 +25,7 @@ func Watch(rdirs, dirs []string, logPS, logFS bool) { log.Printf("Inotify watcher limit: %d (/proc/sys/fs/inotify/max_user_watches)\n", maxWatchers) ping := make(chan struct{}) - in, err := inotify.NewInotify(ping) + in, err := inotify.NewInotify(ping, logFS) if err != nil { log.Fatalf("Can't init inotify: %v", err) } @@ -45,9 +47,9 @@ func Watch(rdirs, dirs []string, logPS, logFS bool) { for { select { case <-ticker.C: - refresh(in, procList) + refresh(in, procList, logPS) case <-ping: - refresh(in, procList) + refresh(in, procList, logPS) } } } @@ -66,17 +68,17 @@ loop: break loop } if err := in.Watch(dir); err != nil { - log.Printf("Can't create watcher: %v", err) + fmt.Fprintf(os.Stderr, "Can't create watcher: %v", err) } case err := <-errCh: - log.Printf("Error walking filesystem: %v", err) + fmt.Fprintf(os.Stderr, "Error walking filesystem: %v", err) } } } -func refresh(in *inotify.Inotify, pl *process.ProcList) { +func refresh(in *inotify.Inotify, pl *process.ProcList, print bool) { in.Pause() - if err := pl.Refresh(); err != nil { + if err := pl.Refresh(print); err != nil { log.Printf("ERROR refreshing process list: %v", err) } time.Sleep(5 * time.Millisecond)