@@ -9,12 +9,15 @@ import (
99 "context"
1010 "io"
1111 "os/exec"
12+ "sync"
1213 "syscall"
1314
1415 "github.com/evilsocket/opensnitch/daemon/log"
1516)
1617
1718type Executer struct {
19+ Mu * sync.RWMutex
20+
1821 Ctx context.Context
1922 Cancel context.CancelFunc
2023
@@ -29,15 +32,15 @@ func New() *Executer {
2932 return & Executer {
3033 Stdout : make (chan string , 0 ),
3134 Stderr : make (chan string , 0 ),
35+ Mu : & sync.RWMutex {},
3236 }
3337}
3438
3539// Start launches the configured command.
3640// It's a blocking operation.
3741func (e * Executer ) Start (bin string , args []string ) {
3842 log .Debug ("[executer] Start() %s %v\n " , bin , args )
39- e .Ctx , e .Cancel = context .WithCancel (context .Background ())
40- e .isRunning = false
43+ e .setRunning (false )
4144
4245 cmd := exec .CommandContext (e .Ctx , bin , args ... )
4346 stdout , err := cmd .StdoutPipe ()
@@ -71,8 +74,8 @@ func (e *Executer) Start(bin string, args []string) {
7174 log .Error ("Executer.Start() %s" , err )
7275 return
7376 }
74- e .isRunning = true
75- defer func () { e .isRunning = false }()
77+ e .setRunning ( true )
78+ defer func () { e .setRunning ( false ) }()
7679
7780 if cmd .Process != nil {
7881 syscall .Setpriority (syscall .PRIO_PROCESS , cmd .Process .Pid , e .Priority )
@@ -92,14 +95,22 @@ func (e *Executer) SetPriority(prio int) {
9295 e .Priority = prio
9396}
9497
98+ func (e * Executer ) setRunning (running bool ) {
99+ e .Mu .Lock ()
100+ e .isRunning = running
101+ e .Mu .Unlock ()
102+ }
103+
95104func (e * Executer ) Running () bool {
105+ e .Mu .RLock ()
106+ defer e .Mu .RUnlock ()
96107 return e .isRunning
97108}
98109
99110func (e * Executer ) Stop () {
100- log .Debug ("[executer] Stop() running: %v" , e .isRunning )
111+ log .Debug ("[executer] Stop() running: %v" , e .Running () )
101112 if e .Running () && e .Cancel != nil {
102113 e .Cancel ()
103114 }
104- e .isRunning = false
115+ e .setRunning ( false )
105116}
0 commit comments