Skip to content

Commit

Permalink
Introduce sync.Mutex to deal with data races
Browse files Browse the repository at this point in the history
Ran the program using go -race and found that there were
several inconsistencies. Thus, a very simple approach was disallow
accessing the state concurrently.
  • Loading branch information
blasrodri committed May 13, 2020
1 parent 0d3809e commit 39804e5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
"fmt"
"github.com/blasrodri/frown/stats"
"github.com/blasrodri/frown/ui"
"strconv"
)

func debug(reportChan <-chan *stats.Report, closeChan chan <- bool) {
func debug(config *ui.UIConfig, reportChan <-chan *stats.Report, closeChan chan<- bool) {
for {

report := <-reportChan
Expand Down
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"github.com/blasrodri/frown/ui"
)


func main() {

var uiConfig = &ui.UIConfig {
FilterSecurityLevel: 1,

var uiConfig = &ui.UIConfig{
FilterSecurityLevel: 1,
}
manageState(uiConfig, ui.TerminalUI)
//manageState(uiConfig, debug)
}
19 changes: 16 additions & 3 deletions state.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"github.com/blasrodri/frown/ui"
"github.com/blasrodri/frown/lsof"
"github.com/blasrodri/frown/stats"
"github.com/blasrodri/frown/ui"
"log"
"sync"
"time"
)

Expand All @@ -13,6 +14,7 @@ type connectionsState struct {
processes map[int]*lsof.Process
listOpenSockets map[int]map[lsof.SocketId]bool
socketIdToPid map[lsof.SocketId]int
mux sync.Mutex
}

func newConnectionState() *connectionsState {
Expand Down Expand Up @@ -92,7 +94,7 @@ func manageState(config *ui.UIConfig, uiFunc func(*ui.UIConfig, <-chan *stats.Re
reportChan := make(chan *stats.Report)
closeChan := make(chan bool)
go manageProcceses(processesChan)
go manageConnections(connectionsChan)
go manageConnections(state, connectionsChan)
go reportSats(state, reportChan)
go uiFunc(config, reportChan, closeChan)

Expand All @@ -105,9 +107,12 @@ func manageState(config *ui.UIConfig, uiFunc func(*ui.UIConfig, <-chan *stats.Re
time.Sleep(100 * time.Duration(time.Millisecond))
select {
case listProcesses := <-processesChan:
state.mux.Lock()
state.setProcesses(listProcesses)
state.mux.Unlock()
// remove state associated to dead processes
go func() {
state.mux.Lock()
for pid, _ := range state.processes {
p := &lsof.Process{
Pid: pid,
Expand All @@ -129,9 +134,12 @@ func manageState(config *ui.UIConfig, uiFunc func(*ui.UIConfig, <-chan *stats.Re
state.socketIdToPid[socketId] = pid
}
}
state.mux.Unlock()
}()
case connDeets := <-connectionsChan:
state.mux.Lock()
state.setConnDetails(connDeets)
state.mux.Unlock()
default:
// Not much to do
}
Expand All @@ -149,7 +157,7 @@ func manageProcceses(processChan chan<- []*lsof.Process) {
}
}

func manageConnections(connectionsChan chan<- []*lsof.ConnectionDetails) {
func manageConnections(c *connectionsState, connectionsChan chan<- []*lsof.ConnectionDetails) {
for {
time.Sleep(200 * time.Duration(time.Millisecond))
connDeets, err := lsof.MonitorUserConnections()
Expand All @@ -164,7 +172,11 @@ func reportSats(c *connectionsState, reportChan chan<- *stats.Report) {
for {
report := stats.NewReport()
time.Sleep(200 * time.Duration(time.Millisecond))
c.mux.Lock()
for pid, sockIdToConnDeets := range c.connDeets {
if c.processes[pid] == nil {
continue
}
processName := c.processes[pid].Name
for socketId, connDeets := range sockIdToConnDeets {
connectionReport, err := stats.AnalyzeSecurity(connDeets)
Expand All @@ -174,6 +186,7 @@ func reportSats(c *connectionsState, reportChan chan<- *stats.Report) {
report.AddConnectionReport(processName, pid, socketId, connectionReport)
}
}
c.mux.Unlock()
if len(report.ProcessInfo) > 0 {
reportChan <- report
}
Expand Down

0 comments on commit 39804e5

Please sign in to comment.