From d1b1221e7f5c6bf74ee2ab58576d2da976671ce1 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Fri, 25 Apr 2014 18:05:02 +0900 Subject: [PATCH] implements Process.Send_signal() and other signal functions on posix. --- README.rst | 10 +++++----- process_posix.go | 38 ++++++++++++++++++++++++++++++++++++++ process_test.go | 12 ++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 1fd7677..75364ff 100644 --- a/README.rst +++ b/README.rst @@ -92,6 +92,11 @@ Current Status - memory_info_ex (linux) - Memory_maps() (linux) <- this is a function - open_files (linux) + - send_signal (linux, freebsd) + - suspend (linux, freebsd) + - resume (linux, freebsd) + - terminate (linux, freebsd) + - kill (linux, freebsd) - not yet @@ -125,11 +130,6 @@ Current Status - parent (use ppid instead) - as_dict - - send_signal() - - suspend() - - resume() - - terminate() - - kill() - wait diff --git a/process_posix.go b/process_posix.go index 3b448d3..12fc4a5 100644 --- a/process_posix.go +++ b/process_posix.go @@ -4,6 +4,8 @@ package gopsutil import ( "os" + "os/exec" + "strconv" "strings" "syscall" ) @@ -45,3 +47,39 @@ func getTerminalMap() (map[uint64]string, error) { } return ret, nil } + +func (p *Process) Send_signal(sig syscall.Signal) error { + sig_as_str := "INT" + switch sig { + case syscall.SIGSTOP: + sig_as_str = "STOP" + case syscall.SIGCONT: + sig_as_str = "CONT" + case syscall.SIGTERM: + sig_as_str = "TERM" + case syscall.SIGKILL: + sig_as_str = "KILL" + } + + cmd := exec.Command("kill", "-s", sig_as_str, strconv.Itoa(int(p.Pid))) + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + return err + } + + return nil +} + +func (p *Process) Suspend() error { + return p.Send_signal(syscall.SIGSTOP) +} +func (p *Process) Resume() error { + return p.Send_signal(syscall.SIGCONT) +} +func (p *Process) Terminate() error { + return p.Send_signal(syscall.SIGTERM) +} +func (p *Process) Kill() error { + return p.Send_signal(syscall.SIGKILL) +} diff --git a/process_test.go b/process_test.go index 1366f91..6be9165 100644 --- a/process_test.go +++ b/process_test.go @@ -6,6 +6,7 @@ import ( "os" "runtime" "testing" + "syscall" ) func Test_Pids(t *testing.T) { @@ -65,3 +66,14 @@ func Test_Process_memory_maps(t *testing.T) { } } + + +func Test_SendSignal(t *testing.T){ + check_pid := os.Getpid() + + p, _ := NewProcess(int32(check_pid)) + err := p.Send_signal(syscall.SIGCONT) + if err != nil{ + t.Errorf("send signal %v", err) + } +}