diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/doc.go | 5 | ||||
-rw-r--r-- | util/isolate.go | 56 | ||||
-rw-r--r-- | util/isolate_win.go | 17 | ||||
-rw-r--r-- | util/state.go | 62 | ||||
-rw-r--r-- | util/state_test.go | 36 |
5 files changed, 78 insertions, 98 deletions
diff --git a/util/doc.go b/util/doc.go new file mode 100644 index 00000000..c6006de4 --- /dev/null +++ b/util/doc.go @@ -0,0 +1,5 @@ +package util + +/* +This package should not contain roadrunner dependencies, only system or third-party + */ diff --git a/util/isolate.go b/util/isolate.go new file mode 100644 index 00000000..005c430e --- /dev/null +++ b/util/isolate.go @@ -0,0 +1,56 @@ +// +build !windows + +package util + +import ( + "fmt" + "os" + "os/exec" + "os/user" + "strconv" + "syscall" +) + +// 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 { + usr, err := user.Lookup(u) + if err != nil { + return err + } + + usrI32, err := strconv.Atoi(usr.Uid) + if err != nil { + return err + } + + grI32, err := strconv.Atoi(usr.Gid) + if err != nil { + return 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 fmt.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 new file mode 100644 index 00000000..77674b3b --- /dev/null +++ b/util/isolate_win.go @@ -0,0 +1,17 @@ +// +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 +}
\ No newline at end of file diff --git a/util/state.go b/util/state.go deleted file mode 100644 index 29fca945..00000000 --- a/util/state.go +++ /dev/null @@ -1,62 +0,0 @@ -package util - -import ( - "errors" - "github.com/shirou/gopsutil/process" - "github.com/spiral/roadrunner" -) - -// State provides information about specific worker. -type State struct { - // Pid contains process id. - Pid int `json:"pid"` - - // Status of the worker. - Status string `json:"status"` - - // Number of worker executions. - NumJobs int64 `json:"numExecs"` - - // Created is unix nano timestamp of worker creation time. - Created int64 `json:"created"` - - // MemoryUsage holds the information about worker memory usage in bytes. - // Values might vary for different operating systems and based on RSS. - MemoryUsage uint64 `json:"memoryUsage"` -} - -// WorkerState creates new worker state definition. -func WorkerState(w *roadrunner.Worker) (*State, error) { - p, _ := process.NewProcess(int32(*w.Pid)) - i, err := p.MemoryInfo() - if err != nil { - return nil, err - } - - return &State{ - Pid: *w.Pid, - Status: w.State().String(), - NumJobs: w.State().NumExecs(), - Created: w.Created.UnixNano(), - MemoryUsage: i.RSS, - }, nil -} - -// ServerState returns list of all worker states of a given rr server. -func ServerState(rr *roadrunner.Server) ([]*State, error) { - if rr == nil { - return nil, errors.New("rr server is not running") - } - - result := make([]*State, 0) - for _, w := range rr.Workers() { - state, err := WorkerState(w) - if err != nil { - return nil, err - } - - result = append(result, state) - } - - return result, nil -} diff --git a/util/state_test.go b/util/state_test.go deleted file mode 100644 index 2afe682e..00000000 --- a/util/state_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package util - -import ( - "github.com/spiral/roadrunner" - "github.com/stretchr/testify/assert" - "runtime" - "testing" - "time" -) - -func TestServerState(t *testing.T) { - rr := roadrunner.NewServer( - &roadrunner.ServerConfig{ - Command: "php ../tests/client.php echo tcp", - Relay: "tcp://:9007", - RelayTimeout: 10 * time.Second, - Pool: &roadrunner.Config{ - NumWorkers: int64(runtime.NumCPU()), - AllocateTimeout: time.Second, - DestroyTimeout: time.Second, - }, - }) - defer rr.Stop() - - assert.NoError(t, rr.Start()) - - state, err := ServerState(rr) - assert.NoError(t, err) - - assert.Len(t, state, runtime.NumCPU()) -} - -func TestServerState_Err(t *testing.T) { - _, err := ServerState(nil) - assert.Error(t, err) -} |