mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-05-14 22:45:17 +08:00
使用自定义 executils.LookPath() 代替 exec.LookPath() 避免因$PATH环境变量被破坏而无法工作
This commit is contained in:
@@ -13,7 +13,6 @@ import (
|
||||
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"net"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
@@ -86,7 +85,7 @@ func (this *ClockManager) Sync() error {
|
||||
|
||||
// check chrony
|
||||
if config.CheckChrony {
|
||||
chronycExe, err := exec.LookPath("chronyc")
|
||||
chronycExe, err := executils.LookPath("chronyc")
|
||||
if err == nil && len(chronycExe) > 0 {
|
||||
var chronyCmd = executils.NewTimeoutCmd(3*time.Second, chronycExe, "tracking")
|
||||
err = chronyCmd.Run()
|
||||
@@ -101,11 +100,11 @@ func (this *ClockManager) Sync() error {
|
||||
server = "pool.ntp.org"
|
||||
}
|
||||
|
||||
ntpdate, err := exec.LookPath("ntpdate")
|
||||
ntpdate, err := executils.LookPath("ntpdate")
|
||||
if err != nil {
|
||||
// 使用 date 命令设置
|
||||
// date --set TIME
|
||||
dateExe, err := exec.LookPath("date")
|
||||
dateExe, err := executils.LookPath("date")
|
||||
if err == nil {
|
||||
currentTime, err := this.ReadServer(server)
|
||||
if err != nil {
|
||||
|
||||
59
internal/utils/exec/look_linux.go
Normal file
59
internal/utils/exec/look_linux.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build linux
|
||||
|
||||
package executils
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/unix"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// LookPath customize our LookPath() function, to work in broken $PATH environment variable
|
||||
func LookPath(file string) (string, error) {
|
||||
result, err := exec.LookPath(file)
|
||||
if err == nil && len(result) > 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// add common dirs contains executable files these may be excluded in $PATH environment variable
|
||||
var binPaths = []string{
|
||||
"/usr/sbin",
|
||||
"/usr/bin",
|
||||
"/usr/local/sbin",
|
||||
"/usr/local/bin",
|
||||
}
|
||||
|
||||
for _, binPath := range binPaths {
|
||||
var fullPath = binPath + string(os.PathSeparator) + file
|
||||
|
||||
stat, err := os.Stat(fullPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if stat.IsDir() {
|
||||
return "", syscall.EISDIR
|
||||
}
|
||||
|
||||
var mode = stat.Mode()
|
||||
if mode.IsDir() {
|
||||
return "", syscall.EISDIR
|
||||
}
|
||||
err = syscall.Faccessat(unix.AT_FDCWD, fullPath, unix.X_OK, unix.AT_EACCESS)
|
||||
if err == nil || (err != syscall.ENOSYS && err != syscall.EPERM) {
|
||||
return fullPath, err
|
||||
}
|
||||
if mode&0111 != 0 {
|
||||
return fullPath, nil
|
||||
}
|
||||
return "", fs.ErrPermission
|
||||
}
|
||||
|
||||
return "", &exec.Error{
|
||||
Name: file,
|
||||
Err: exec.ErrNotFound,
|
||||
}
|
||||
}
|
||||
|
||||
10
internal/utils/exec/look_others.go
Normal file
10
internal/utils/exec/look_others.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !linux
|
||||
|
||||
package executils
|
||||
|
||||
import "os/exec"
|
||||
|
||||
func LookPath(file string) (string, error) {
|
||||
return exec.LookPath(file)
|
||||
}
|
||||
@@ -24,7 +24,7 @@ func (this *ServiceManager) Install(exePath string, args []string) error {
|
||||
return errors.New("only root users can install the service")
|
||||
}
|
||||
|
||||
systemd, err := exec.LookPath("systemctl")
|
||||
systemd, err := executils.LookPath("systemctl")
|
||||
if err != nil {
|
||||
return this.installInitService(exePath, args)
|
||||
}
|
||||
@@ -39,7 +39,7 @@ func (this *ServiceManager) Start() error {
|
||||
}
|
||||
|
||||
if files.NewFile(systemdServiceFile).Exists() {
|
||||
systemd, err := exec.LookPath("systemctl")
|
||||
systemd, err := executils.LookPath("systemctl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -56,7 +56,7 @@ func (this *ServiceManager) Uninstall() error {
|
||||
}
|
||||
|
||||
if files.NewFile(systemdServiceFile).Exists() {
|
||||
systemd, err := exec.LookPath("systemctl")
|
||||
systemd, err := executils.LookPath("systemctl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -96,7 +96,7 @@ func (this *ServiceManager) installInitService(exePath string, args []string) er
|
||||
return err
|
||||
}
|
||||
|
||||
chkCmd, err := exec.LookPath("chkconfig")
|
||||
chkCmd, err := executils.LookPath("chkconfig")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user