1
0

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

View File

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

View File

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