diff options
author | Valery Piashchynski <[email protected]> | 2020-12-27 00:59:10 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-12-27 00:59:10 +0300 |
commit | 8df4896deabdab9a50a5ad3c6da6e1c7f05922af (patch) | |
tree | c2fa71733902b999c02e5abd608bff4bc7449c5c /util | |
parent | 1aaf6e6ffb015cd5a21d9d938ad84c18723973c5 (diff) |
Util -> Utils
Diffstat (limited to 'util')
-rwxr-xr-x | util/doc.go | 5 | ||||
-rwxr-xr-x | util/isolate.go | 59 | ||||
-rwxr-xr-x | util/isolate_win.go | 17 | ||||
-rwxr-xr-x | util/network.go | 59 | ||||
-rwxr-xr-x | util/network_test.go | 23 | ||||
-rwxr-xr-x | util/network_windows.go | 43 | ||||
-rwxr-xr-x | util/network_windows_test.go | 16 |
7 files changed, 0 insertions, 222 deletions
diff --git a/util/doc.go b/util/doc.go deleted file mode 100755 index a3798715..00000000 --- a/util/doc.go +++ /dev/null @@ -1,5 +0,0 @@ -package util - -/* -This package should not contain roadrunner dependencies, only system or third-party -*/ diff --git a/util/isolate.go b/util/isolate.go deleted file mode 100755 index 0ea1dff3..00000000 --- a/util/isolate.go +++ /dev/null @@ -1,59 +0,0 @@ -// +build !windows - -package util - -import ( - "fmt" - "os" - "os/exec" - "os/user" - "strconv" - "syscall" - - "github.com/spiral/errors" -) - -// IsolateProcess change gpid for the process to avoid bypassing signals to php processes. -func IsolateProcess(cmd *exec.Cmd) { - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} -} - -// ExecuteFromUser may work only if run RR under root user -func ExecuteFromUser(cmd *exec.Cmd, u string) error { - const op = errors.Op("execute from user") - usr, err := user.Lookup(u) - if err != nil { - return errors.E(op, err) - } - - usrI32, err := strconv.ParseInt(usr.Uid, 10, 32) - if err != nil { - return errors.E(op, err) - } - - grI32, err := strconv.ParseInt(usr.Gid, 10, 32) - if err != nil { - return errors.E(op, err) - } - - // For more information: - // https://www.man7.org/linux/man-pages/man7/user_namespaces.7.html - // https://www.man7.org/linux/man-pages/man7/namespaces.7.html - if _, err := os.Stat("/proc/self/ns/user"); err != nil { - if os.IsNotExist(err) { - return fmt.Errorf("kernel doesn't support user namespaces") - } - if os.IsPermission(err) { - return fmt.Errorf("unable to test user namespaces due to permissions") - } - - return errors.E(op, errors.Errorf("failed to stat /proc/self/ns/user: %v", err)) - } - - cmd.SysProcAttr.Credential = &syscall.Credential{ - Uid: uint32(usrI32), - Gid: uint32(grI32), - } - - return nil -} diff --git a/util/isolate_win.go b/util/isolate_win.go deleted file mode 100755 index 6756d59f..00000000 --- a/util/isolate_win.go +++ /dev/null @@ -1,17 +0,0 @@ -// +build windows - -package util - -import ( - "os/exec" - "syscall" -) - -// IsolateProcess change gpid for the process to avoid bypassing signals to php processes. -func IsolateProcess(cmd *exec.Cmd) { - cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP} -} - -func ExecuteFromUser(cmd *exec.Cmd, u string) error { - return nil -} diff --git a/util/network.go b/util/network.go deleted file mode 100755 index c2475f4a..00000000 --- a/util/network.go +++ /dev/null @@ -1,59 +0,0 @@ -// +build linux darwin freebsd - -package util - -import ( - "errors" - "fmt" - "net" - "os" - "strings" - "syscall" - - "github.com/valyala/tcplisten" -) - -// CreateListener crates socket listener based on DSN definition. -func CreateListener(address string) (net.Listener, error) { - dsn := strings.Split(address, "://") - if len(dsn) != 2 { - return nil, errors.New("invalid DSN (tcp://:6001, unix://file.sock)") - } - - if dsn[0] != "unix" && dsn[0] != "tcp" { - return nil, errors.New("invalid Protocol (tcp://:6001, unix://file.sock)") - } - - // create unix listener - if dsn[0] == "unix" { - // check if the file exist - if fileExists(dsn[1]) { - err := syscall.Unlink(dsn[1]) - if err != nil { - return nil, fmt.Errorf("error during the unlink syscall: error %v", err) - } - } - return net.Listen(dsn[0], dsn[1]) - } - - // configure and create tcp4 listener - cfg := tcplisten.Config{ - ReusePort: true, - DeferAccept: true, - FastOpen: true, - Backlog: 0, - } - - // only tcp4 is currently supported - return cfg.NewListener("tcp4", dsn[1]) -} - -// fileExists checks if a file exists and is not a directory before we -// try using it to prevent further errors. -func fileExists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return !info.IsDir() -} diff --git a/util/network_test.go b/util/network_test.go deleted file mode 100755 index 1dc16e94..00000000 --- a/util/network_test.go +++ /dev/null @@ -1,23 +0,0 @@ -// +build linux darwin freebsd - -package util - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestCreateListener(t *testing.T) { - _, err := CreateListener("unexpected dsn") - assert.Error(t, err, "Invalid DSN (tcp://:6001, unix://file.sock)") - - _, err = CreateListener("aaa://192.168.0.1") - assert.Error(t, err, "Invalid Protocol (tcp://:6001, unix://file.sock)") -} - -func TestUnixCreateListener(t *testing.T) { - l, err := CreateListener("unix://file.sock") - assert.NoError(t, err) - l.Close() -} diff --git a/util/network_windows.go b/util/network_windows.go deleted file mode 100755 index 843d5779..00000000 --- a/util/network_windows.go +++ /dev/null @@ -1,43 +0,0 @@ -// +build windows - -package util - -import ( - "errors" - "fmt" - "net" - "os" - "strings" - "syscall" -) - -// CreateListener crates socket listener based on DSN definition. -func CreateListener(address string) (net.Listener, error) { - dsn := strings.Split(address, "://") - if len(dsn) != 2 { - return nil, errors.New("invalid DSN (tcp://:6001, unix://file.sock)") - } - - if dsn[0] != "unix" && dsn[0] != "tcp" { - return nil, errors.New("invalid Protocol (tcp://:6001, unix://file.sock)") - } - - if dsn[0] == "unix" && fileExists(dsn[1]) { - err := syscall.Unlink(dsn[1]) - if err != nil { - return nil, fmt.Errorf("error during the unlink syscall: error %v", err) - } - } - - return net.Listen(dsn[0], dsn[1]) -} - -// fileExists checks if a file exists and is not a directory before we -// try using it to prevent further errors. -func fileExists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return !info.IsDir() -} diff --git a/util/network_windows_test.go b/util/network_windows_test.go deleted file mode 100755 index b6648ed0..00000000 --- a/util/network_windows_test.go +++ /dev/null @@ -1,16 +0,0 @@ -// +build windows - -package util - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestCreateListener(t *testing.T) { - _, err := CreateListener("unexpected dsn") - assert.Error(t, err, "Invalid DSN (tcp://:6001, unix://file.sock)") - - _, err = CreateListener("aaa://192.168.0.1") - assert.Error(t, err, "Invalid Protocol (tcp://:6001, unix://file.sock)") -} |