mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-12 16:59:38 +07:00
optimize: lower kernel version requirement from 5.6 to 5.2
This commit is contained in:
43
cmd/internal/su.go
Normal file
43
cmd/internal/su.go
Normal file
@ -0,0 +1,43 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/sirupsen/logrus"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func AutoSu() {
|
||||
if os.Getuid() == 0 {
|
||||
return
|
||||
}
|
||||
program := filepath.Base(os.Args[0])
|
||||
pathSudo, err := exec.LookPath("sudo")
|
||||
if err != nil {
|
||||
// skip
|
||||
return
|
||||
}
|
||||
// https://github.com/WireGuard/wireguard-tools/blob/71799a8f6d1450b63071a21cad6ed434b348d3d5/src/wg-quick/linux.bash#L85
|
||||
p, err := os.StartProcess(pathSudo, append([]string{
|
||||
pathSudo,
|
||||
"-E",
|
||||
"-p",
|
||||
fmt.Sprintf("%v must be run as root. Please enter the password for %%u to continue: ", program),
|
||||
"--",
|
||||
}, os.Args...), &os.ProcAttr{
|
||||
Files: []*os.File{
|
||||
os.Stdin,
|
||||
os.Stdout,
|
||||
os.Stderr,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
stat, err := p.Wait()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(stat.ExitCode())
|
||||
}
|
Reference in New Issue
Block a user