Don't shell out to obtain process and system stats

This commit is contained in:
Dan Kortschak
2022-04-24 17:46:57 +09:30
committed by Jakub
parent f40f002bf9
commit 5c28a3eda7
3 changed files with 33 additions and 36 deletions

View File

@ -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 {

View File

@ -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
}

View File

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