-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Watch sysctl changes to ensure expected values
- Loading branch information
1 parent
32ea550
commit 8db2d79
Showing
6 changed files
with
102 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package control | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const SysctlPrefixPath = "/proc/sys/" | ||
|
||
var sysctl *SysctlManager | ||
|
||
type SysctlManager struct { | ||
mux sync.Mutex | ||
watcher *fsnotify.Watcher | ||
expectations map[string]string | ||
} | ||
|
||
func init() { | ||
var err error | ||
if sysctl, err = NewSysctlManager(); err != nil { | ||
logrus.Fatalf("failed to create sysctl manager: %v", err) | ||
} | ||
} | ||
|
||
func NewSysctlManager() (*SysctlManager, error) { | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
manager := &SysctlManager{ | ||
mux: sync.Mutex{}, | ||
watcher: watcher, | ||
expectations: map[string]string{}, | ||
} | ||
go manager.StartWatch() | ||
return manager, nil | ||
} | ||
|
||
func (s *SysctlManager) StartWatch() { | ||
for { | ||
select { | ||
case event, ok := <-s.watcher.Events: | ||
if !ok { | ||
return | ||
} | ||
if event.Has(fsnotify.Write) { | ||
logrus.Tracef("sysctl write event: %+v", event) | ||
s.mux.Lock() | ||
expected, ok := s.expectations[event.Name] | ||
s.mux.Unlock() | ||
if ok { | ||
raw, err := os.ReadFile(event.Name) | ||
if err != nil { | ||
logrus.Errorf("failed to read sysctl file %s: %v", event.Name, err) | ||
} | ||
value := strings.TrimSpace(string(raw)) | ||
if value != expected { | ||
logrus.Infof("sysctl %s has unexpected value %s, expected %s", event.Name, value, expected) | ||
if err := os.WriteFile(event.Name, []byte(expected), 0644); err != nil { | ||
logrus.Errorf("failed to write sysctl file %s: %v", event.Name, err) | ||
} | ||
} | ||
} | ||
} | ||
case err, ok := <-s.watcher.Errors: | ||
if !ok { | ||
return | ||
} | ||
logrus.Errorf("sysctl watcher error: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func (s *SysctlManager) Set(key string, value string, watch bool) (err error) { | ||
path := SysctlPrefixPath + strings.Replace(key, ".", "/", -1) | ||
if watch { | ||
s.mux.Lock() | ||
s.expectations[path] = value | ||
s.mux.Unlock() | ||
if err = s.watcher.Add(path); err != nil { | ||
return | ||
} | ||
} | ||
return os.WriteFile(path, []byte(value), 0644) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters