From d4cd792950b68c968af09992000f4276bafa68ec Mon Sep 17 00:00:00 2001 From: mountain Date: Fri, 24 Nov 2023 16:02:31 +0900 Subject: [PATCH] =?UTF-8?q?setcap=20=EB=B6=80=ED=99=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/operation.go | 11 ++++---- client/platform_linux.go | 47 -------------------------------- client/platform_windows.go | 42 ---------------------------- client/set_permission_default.go | 7 +++++ client/set_permission_linux.go | 21 ++++++++++++++ 5 files changed, 33 insertions(+), 95 deletions(-) delete mode 100644 client/platform_linux.go delete mode 100644 client/platform_windows.go create mode 100644 client/set_permission_default.go create mode 100644 client/set_permission_linux.go diff --git a/client/operation.go b/client/operation.go index ebdacdd..046368e 100644 --- a/client/operation.go +++ b/client/operation.go @@ -153,16 +153,15 @@ func prepareProcessLaunch(storageRoot string, req *shared.StartProcessRequest) * if err == nil && fi.IsDir() { exefile := "./" + path.Clean(strings.TrimPrefix(req.Args[0], "/")) - os.Chmod(path.Join(verpath, exefile), 0777) + err = set_permission(path.Join(verpath, exefile)) + if err != nil { + logger.Println("set_permission failed :", err) + return nil + } exef, _ := os.Executable() cmd := exec.Command(path.Join(path.Dir(exef), verpath, exefile), req.Args[1:]...) - if err := run_prelaunch_script(cmd.Args[0]); err != nil { - logger.Println("run_prelaunch_script failed :", cmd.Args[0], err) - return nil - } - cmd.Dir = verpath stdin, _ := cmd.StdinPipe() diff --git a/client/platform_linux.go b/client/platform_linux.go deleted file mode 100644 index 4fc23fc..0000000 --- a/client/platform_linux.go +++ /dev/null @@ -1,47 +0,0 @@ -//go:build client && linux - -package client - -import ( - "errors" - "io/fs" - "os" - "os/exec" - "path" - - "repositories.action2quare.com/ayo/gocommon/logger" -) - -func run_prelaunch_script(exepath string) error { - scriptPath := path.Join(path.Dir(exepath), "prelaunch.sh") - fi, err := os.Stat(scriptPath) - if errors.Is(err, fs.ErrNotExist) { - logger.Println("prelaunch.sh not exists :", scriptPath, err) - return nil - } - - if fi == nil { - logger.Println("prelaunch.sh fi is nil :", scriptPath) - return nil - } - - os.Chmod(scriptPath, 0777) - - logger.Println("run_prelaunch_script :", "/bin/bash", scriptPath, exepath) - return exec.Command("/bin/bash", scriptPath, exepath).Run() -} - -func run_postlaunch_script(exepath string) error { - scriptPath := path.Join(path.Dir(exepath), "postlaunch.sh") - fi, err := os.Stat(scriptPath) - if errors.Is(err, fs.ErrNotExist) { - return nil - } - - if fi == nil { - return nil - } - - os.Chmod(scriptPath, 0777) - return exec.Command("/bin/bash", scriptPath, exepath).Run() -} diff --git a/client/platform_windows.go b/client/platform_windows.go deleted file mode 100644 index 89aa4f2..0000000 --- a/client/platform_windows.go +++ /dev/null @@ -1,42 +0,0 @@ -//go:build !(client && linux) - -package client - -import ( - "errors" - "io/fs" - "os" - "os/exec" - "path" -) - -func run_prelaunch_script(exepath string) error { - scriptPath := path.Join(path.Dir(exepath), "prelaunch.bat") - fi, err := os.Stat(scriptPath) - if errors.Is(err, fs.ErrNotExist) { - return nil - } - - if fi == nil { - return nil - } - - os.Chmod(scriptPath, 0777) - - return exec.Command("cmd", scriptPath).Run() -} - -func run_postlaunch_script(exepath string) error { - scriptPath := path.Join(path.Dir(exepath), "postlaunch.bat") - fi, err := os.Stat(scriptPath) - if errors.Is(err, fs.ErrNotExist) { - return nil - } - - if fi == nil { - return nil - } - - os.Chmod(scriptPath, 0777) - return exec.Command("cmd", scriptPath).Run() -} diff --git a/client/set_permission_default.go b/client/set_permission_default.go new file mode 100644 index 0000000..ef8f120 --- /dev/null +++ b/client/set_permission_default.go @@ -0,0 +1,7 @@ +//go:build client && windows + +package client + +func set_permission(path string) error { + return nil +} diff --git a/client/set_permission_linux.go b/client/set_permission_linux.go new file mode 100644 index 0000000..689b862 --- /dev/null +++ b/client/set_permission_linux.go @@ -0,0 +1,21 @@ +//go:build client && linux + +package client + +import ( + "os" + "os/exec" +) + +func init() { + file, _ := os.OpenFile("setcap.sh", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0777) + file.Write([]byte("sudo setcap 'cap_net_bind_service=+ep' $1")) + file.Sync() + file.Close() +} + +func set_permission(path string) error { + os.Chmod(path, 0777) + cmd := exec.Command("/bin/bash", "./setcap.sh", path) + return cmd.Run() +}