Skip to content

Commit 55ac4b7

Browse files
tasks improvements
- fixed some race conditions. - added more errors logging.
1 parent 7224acc commit 55ac4b7

File tree

7 files changed

+34
-10
lines changed

7 files changed

+34
-10
lines changed

daemon/tasks/iocscanner/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package iocscanner
2-
31
// Copyright 2025 The OpenSnitch Authors. All rights reserved.
42
// Use of this source code is governed by the GPLv3
53
// license that can be found in the LICENSE file.
64

5+
package iocscanner
6+
77
import (
88
"context"
99
//"encoding/json"

daemon/tasks/iocscanner/tools/base/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (t *ToolBase) TransformLogline(line string) string {
5252
}
5353

5454
func (t *ToolBase) Done() <-chan struct{} {
55-
return t.Ctx.Done()
55+
return t.Executer.Ctx.Done()
5656
}
5757

5858
func (t *ToolBase) Stdout() chan string {

daemon/tasks/iocscanner/tools/dpkg/dpkg.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
//"path/filepath"
1212
"strings"
13+
"sync"
1314
"time"
1415

1516
"github.com/evilsocket/opensnitch/daemon/core"
@@ -120,6 +121,7 @@ func New(opts config.ToolOpts) *DpkgTool {
120121
Cancel: cancel,
121122
Stdout: make(chan string, opts.ReadBuffer),
122123
Stderr: make(chan string, 0),
124+
Mu: &sync.RWMutex{},
123125
},
124126
},
125127
}

daemon/tasks/iocscanner/tools/executer/executer.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1718
type 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.
3741
func (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+
95104
func (e *Executer) Running() bool {
105+
e.Mu.RLock()
106+
defer e.Mu.RUnlock()
96107
return e.isRunning
97108
}
98109

99110
func (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
}

daemon/tasks/iocscanner/tools/generic/generic.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
//"path/filepath"
1212
"strings"
13+
"sync"
1314
"time"
1415

1516
"github.com/evilsocket/opensnitch/daemon/core"
@@ -93,6 +94,7 @@ func New(opts config.ToolOpts) *GenericTool {
9394
Cancel: cancel,
9495
Stdout: make(chan string, opts.ReadBuffer),
9596
Stderr: make(chan string, 0),
97+
Mu: &sync.RWMutex{},
9698
},
9799
},
98100
}

daemon/tasks/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ func (tm *TaskManager) ResumeAll() {
136136
func (tm *TaskManager) StopAll() {
137137
for name := range tm.tasks {
138138
log.Debug("[tasks] Stopping task %s", name)
139-
tm.RemoveTask(name)
139+
if err := tm.RemoveTask(name); err != nil {
140+
log.Debug("[tasks] Remove task error: %s", err)
141+
}
140142
}
141143
}
142144

@@ -158,7 +160,9 @@ func (tm *TaskManager) StopTempTasks() {
158160

159161
// GetTask ...
160162
func (tm *TaskManager) GetTask(name string) (tk base.Task, found bool) {
163+
tm.mu.Lock()
161164
tk, found = tm.tasks[name]
165+
tm.mu.Unlock()
162166
return
163167
}
164168

daemon/tasks/scheduler/scheduler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type Scheduler struct {
4444
TickChan chan time.Time
4545
ticky chan time.Time
4646
Config Config
47+
48+
mu *sync.RWMutex
4749
}
4850

4951
func New(ctx context.Context, cancel context.CancelFunc, config Config) *Scheduler {
@@ -53,6 +55,7 @@ func New(ctx context.Context, cancel context.CancelFunc, config Config) *Schedul
5355
TickChan: make(chan time.Time),
5456
ticky: make(chan time.Time),
5557
Config: config,
58+
mu: &sync.RWMutex{},
5659
}
5760

5861
return sched
@@ -103,7 +106,9 @@ func (s *Scheduler) SetupDailyTimers() {
103106
return
104107
}
105108
// save tickers to stop them later when stopping the scheduler.
109+
s.mu.Lock()
106110
s.Tickers = append(s.Tickers, tck)
111+
s.mu.Unlock()
107112

108113
// wait for ticks while the tickers are active.
109114
go func() {

0 commit comments

Comments
 (0)