From 5c28a3eda7095808f13c28dabbdc0089f2d2df24 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sun, 24 Apr 2022 17:46:57 +0930 Subject: [PATCH] Don't shell out to obtain process and system stats --- .../frontend/clientconfig/config_applemail.go | 14 +++--- internal/frontend/theme/detect_darwin.go | 5 +- internal/store/ulimit.go | 48 +++++++++---------- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/internal/frontend/clientconfig/config_applemail.go b/internal/frontend/clientconfig/config_applemail.go index b50db72..f2e7c63 100644 --- a/internal/frontend/clientconfig/config_applemail.go +++ b/internal/frontend/clientconfig/config_applemail.go @@ -23,16 +23,16 @@ package clientconfig import ( "io/ioutil" "os" - "os/exec" "path/filepath" "strconv" "strings" "time" - "github.com/ProtonMail/proton-bridge/v2/internal/bridge" - "github.com/ProtonMail/proton-bridge/v2/internal/config/useragent" - "github.com/ProtonMail/proton-bridge/v2/internal/frontend/types" - "github.com/ProtonMail/proton-bridge/v2/pkg/mobileconfig" + "github.com/ProtonMail/proton-bridge/internal/bridge" + "github.com/ProtonMail/proton-bridge/internal/config/useragent" + "github.com/ProtonMail/proton-bridge/internal/frontend/types" + "github.com/ProtonMail/proton-bridge/pkg/mobileconfig" + "golang.org/x/sys/execabs" ) const ( @@ -56,10 +56,10 @@ func (c *appleMail) Configure(imapPort, smtpPort int, imapSSL, smtpSSL bool, use } if useragent.IsBigSurOrNewer() { - return exec.Command("open", bigSurPreferncesPane, confPath).Run() //nolint:gosec G204: open command is safe, mobileconfig is generated by us + return execabs.Command("open", bigSurPreferncesPane, confPath).Run() //nolint:gosec G204: open command is safe, mobileconfig is generated by us } - return exec.Command("open", confPath).Run() //nolint:gosec G204: open command is safe, mobileconfig is generated by us + return execabs.Command("open", confPath).Run() //nolint:gosec G204: open command is safe, mobileconfig is generated by us } func prepareMobileConfig(imapPort, smtpPort int, imapSSL, smtpSSL bool, user types.User, address string) *mobileconfig.Config { diff --git a/internal/frontend/theme/detect_darwin.go b/internal/frontend/theme/detect_darwin.go index f9a8e84..b7da2e9 100644 --- a/internal/frontend/theme/detect_darwin.go +++ b/internal/frontend/theme/detect_darwin.go @@ -21,12 +21,13 @@ package theme import ( - "os/exec" "strings" + + "golang.org/x/sys/execabs" ) func detectSystemTheme() Theme { - out, err := exec.Command("defaults", "read", "-g", "AppleInterfaceStyle").Output() //nolint:gosec + out, err := execabs.Command("defaults", "read", "-g", "AppleInterfaceStyle").Output() //nolint:gosec if err == nil && strings.TrimSpace(string(out)) == "Dark" { return Dark } diff --git a/internal/store/ulimit.go b/internal/store/ulimit.go index 3765773..76c8527 100644 --- a/internal/store/ulimit.go +++ b/internal/store/ulimit.go @@ -18,47 +18,43 @@ package store import ( - "fmt" "os" - "os/exec" "runtime" - "strconv" - "strings" + + "golang.org/x/sys/unix" ) -func uLimit() int { +func isFdCloseToULimit() bool { if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { - return 0 + return false } - out, err := exec.Command("bash", "-c", "ulimit -n").Output() - if err != nil { - log.Print(err) - return 0 + + var fdPath string + switch runtime.GOOS { + case "darwin": + fdPath = "/dev/fd" + case "linux": + fdPath = "/proc/self/fd" } - outStr := strings.Trim(string(out), " \n") - num, err := strconv.Atoi(outStr) + f, err := os.Open(fdPath) if err != nil { - log.Print(err) - return 0 - } - return num -} - -func isFdCloseToULimit() bool { - if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { + log.Warn("isFdCloseToULimit: ", err) return false } - - pid := fmt.Sprint(os.Getpid()) - out, err := exec.Command("lsof", "-p", pid).Output() //nolint:gosec + d, err := f.ReadDir(-1) if err != nil { log.Warn("isFdCloseToULimit: ", err) return false } - lines := strings.Split(string(out), "\n") + fd := len(d) - 1 + + var lim unix.Rlimit + err = unix.Getrlimit(unix.RLIMIT_NOFILE, &lim) + if err != nil { + log.Print(err) + } + ulimit := lim.Max - fd := len(lines) - 1 - ulimit := uLimit() log.Info("File descriptor check: num goroutines ", runtime.NumGoroutine(), " fd ", fd, " ulimit ", ulimit) return fd >= int(0.95*float64(ulimit)) }