|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + //"path" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/evilsocket/opensnitch/daemon/log" |
| 9 | + "github.com/fsnotify/fsnotify" |
| 10 | +) |
| 11 | + |
| 12 | +func (l *Loader) AddWatch(path string) error { |
| 13 | + l.RLock() |
| 14 | + defer l.RUnlock() |
| 15 | + return l.watcher.Add(path) |
| 16 | +} |
| 17 | + |
| 18 | +func (l *Loader) RemoveWatch(path string) error { |
| 19 | + l.RLock() |
| 20 | + defer l.RUnlock() |
| 21 | + return l.watcher.Remove(path) |
| 22 | +} |
| 23 | + |
| 24 | +func (l *Loader) AddWatches() { |
| 25 | + if err := l.watcher.Add(l.CfgFile); err != nil { |
| 26 | + log.Error("[tasks] Could not watch path %s: %s", l.CfgFile, err) |
| 27 | + } |
| 28 | + |
| 29 | + for _, task := range l.Tasks { |
| 30 | + if task.ConfigFile == "" { |
| 31 | + log.Warning("[tasks] Loader watch, \"configfile\" field missing, skipping task %s: enabled: %v, %s", task.Name, task.Enabled, task.ConfigFile) |
| 32 | + continue |
| 33 | + } |
| 34 | + if !task.Enabled { |
| 35 | + continue |
| 36 | + } |
| 37 | + log.Debug("[tasks] Loader watching %s: %v, %s", task.Name, task.Enabled, task.ConfigFile) |
| 38 | + |
| 39 | + if err := l.AddWatch(task.ConfigFile); err != nil { |
| 40 | + log.Error("[tasks] Loader, could not watch path %s: %s", task.ConfigFile, err) |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (l *Loader) setLiveReloadRunning(running bool) { |
| 46 | + l.Lock() |
| 47 | + l.liveReloadRunning = running |
| 48 | + l.Unlock() |
| 49 | +} |
| 50 | + |
| 51 | +func (l *Loader) isLiveReloadRunning() bool { |
| 52 | + l.RLock() |
| 53 | + defer l.RUnlock() |
| 54 | + return l.liveReloadRunning |
| 55 | +} |
| 56 | + |
| 57 | +func (l *Loader) liveReloadWorker() { |
| 58 | + l.setLiveReloadRunning(true) |
| 59 | + defer l.setLiveReloadRunning(false) |
| 60 | + |
| 61 | + //log.Info("Tasks watcher started on path %v ...", l.Tasks) |
| 62 | + |
| 63 | + for { |
| 64 | + l.AddWatches() |
| 65 | + |
| 66 | + select { |
| 67 | + case <-l.stopLiveReload: |
| 68 | + goto Exit |
| 69 | + case event, ok := <-l.watcher.Events: |
| 70 | + if !ok { |
| 71 | + log.Error("[tasks] Loader.watcher events not ready, closed?") |
| 72 | + } |
| 73 | + if !strings.HasSuffix(event.Name, ".json") { |
| 74 | + continue |
| 75 | + } |
| 76 | + log.Trace("[tasks] watcher event. Write: %v, Create: %v, Removed: %v Renamed: %v, %+v, %s", |
| 77 | + event.Op&fsnotify.Write == fsnotify.Write, |
| 78 | + event.Op&fsnotify.Create == fsnotify.Create, |
| 79 | + event.Op&fsnotify.Remove == fsnotify.Remove, |
| 80 | + event.Op&fsnotify.Rename == fsnotify.Rename, |
| 81 | + event, event.Name) |
| 82 | + |
| 83 | + // a new rule json file has been created or updated |
| 84 | + if event.Op&fsnotify.Create == fsnotify.Create { |
| 85 | + log.Info("New task: %s", event.Name) |
| 86 | + |
| 87 | + } else if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Rename == fsnotify.Rename { |
| 88 | + log.Important("Tasks file changed %s, reloading ...", event.Name) |
| 89 | + // the events may occur too rapidly, and sometimes the file does not exist yet. |
| 90 | + time.Sleep(1 * time.Second) |
| 91 | + l.TaskChanged <- event.Name |
| 92 | + } |
| 93 | + |
| 94 | + case err := <-l.watcher.Errors: |
| 95 | + log.Warning("[tasks] watcher error: %s", err) |
| 96 | + } |
| 97 | + } |
| 98 | +Exit: |
| 99 | + log.Info("[tasks] liveReloadWorker() exited") |
| 100 | +} |
0 commit comments