Don't shell out to obtain process and system stats

create-reload-action
Dan Kortschak 4 years ago committed by Jakub
parent f40f002bf9
commit 5c28a3eda7
  1. 14
      internal/frontend/clientconfig/config_applemail.go
  2. 5
      internal/frontend/theme/detect_darwin.go
  3. 48
      internal/store/ulimit.go

@ -23,16 +23,16 @@ package clientconfig
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/ProtonMail/proton-bridge/v2/internal/bridge" "github.com/ProtonMail/proton-bridge/internal/bridge"
"github.com/ProtonMail/proton-bridge/v2/internal/config/useragent" "github.com/ProtonMail/proton-bridge/internal/config/useragent"
"github.com/ProtonMail/proton-bridge/v2/internal/frontend/types" "github.com/ProtonMail/proton-bridge/internal/frontend/types"
"github.com/ProtonMail/proton-bridge/v2/pkg/mobileconfig" "github.com/ProtonMail/proton-bridge/pkg/mobileconfig"
"golang.org/x/sys/execabs"
) )
const ( const (
@ -56,10 +56,10 @@ func (c *appleMail) Configure(imapPort, smtpPort int, imapSSL, smtpSSL bool, use
} }
if useragent.IsBigSurOrNewer() { 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 { func prepareMobileConfig(imapPort, smtpPort int, imapSSL, smtpSSL bool, user types.User, address string) *mobileconfig.Config {

@ -21,12 +21,13 @@
package theme package theme
import ( import (
"os/exec"
"strings" "strings"
"golang.org/x/sys/execabs"
) )
func detectSystemTheme() Theme { 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" { if err == nil && strings.TrimSpace(string(out)) == "Dark" {
return Dark return Dark
} }

@ -18,47 +18,43 @@
package store package store
import ( import (
"fmt"
"os" "os"
"os/exec"
"runtime" "runtime"
"strconv"
"strings" "golang.org/x/sys/unix"
) )
func uLimit() int { func isFdCloseToULimit() bool {
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
return 0 return false
} }
out, err := exec.Command("bash", "-c", "ulimit -n").Output()
if err != nil { var fdPath string
log.Print(err) switch runtime.GOOS {
return 0 case "darwin":
fdPath = "/dev/fd"
case "linux":
fdPath = "/proc/self/fd"
} }
outStr := strings.Trim(string(out), " \n") f, err := os.Open(fdPath)
num, err := strconv.Atoi(outStr)
if err != nil { if err != nil {
log.Print(err) log.Warn("isFdCloseToULimit: ", err)
return 0
}
return num
}
func isFdCloseToULimit() bool {
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
return false return false
} }
d, err := f.ReadDir(-1)
pid := fmt.Sprint(os.Getpid())
out, err := exec.Command("lsof", "-p", pid).Output() //nolint:gosec
if err != nil { if err != nil {
log.Warn("isFdCloseToULimit: ", err) log.Warn("isFdCloseToULimit: ", err)
return false 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) log.Info("File descriptor check: num goroutines ", runtime.NumGoroutine(), " fd ", fd, " ulimit ", ulimit)
return fd >= int(0.95*float64(ulimit)) return fd >= int(0.95*float64(ulimit))
} }

Loading…
Cancel
Save