diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-02-25 18:45:32 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-02-28 08:05:02 +0100 |
commit | 019ce9f2815cd21756be4f11702fcb02b5453fdc (patch) | |
tree | 43070181e30db403dfad69f3e67a566ba589df4e /conf/storewatcher_windows.go | |
parent | Initial scaffolding (diff) | |
download | wireguard-windows-019ce9f2815cd21756be4f11702fcb02b5453fdc.tar.xz wireguard-windows-019ce9f2815cd21756be4f11702fcb02b5453fdc.zip |
conf: introduce configuration management
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'conf/storewatcher_windows.go')
-rw-r--r-- | conf/storewatcher_windows.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/conf/storewatcher_windows.go b/conf/storewatcher_windows.go new file mode 100644 index 00000000..f3f38fef --- /dev/null +++ b/conf/storewatcher_windows.go @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package conf + +import ( + "golang.org/x/sys/windows" + "log" +) + +const ( + fncFILE_NAME uint32 = 0x00000001 + fncDIR_NAME uint32 = 0x00000002 + fncATTRIBUTES uint32 = 0x00000004 + fncSIZE uint32 = 0x00000008 + fncLAST_WRITE uint32 = 0x00000010 + fncLAST_ACCESS uint32 = 0x00000020 + fncCREATION uint32 = 0x00000040 + fncSECURITY uint32 = 0x00000100 +) + +//sys findFirstChangeNotification(path *uint16, watchSubtree bool, filter uint32) (handle windows.Handle, err error) = kernel32.FindFirstChangeNotificationW +//sys findNextChangeNotification(handle windows.Handle) (err error) = kernel32.FindNextChangeNotification + +var haveStartedWatchingConfigDir bool + +func startWatchingConfigDir() { + if haveStartedWatchingConfigDir { + return + } + haveStartedWatchingConfigDir = true + go func() { + configFileDir, err := resolveConfigFileDir() + if err != nil { + return + } + h, err := findFirstChangeNotification(windows.StringToUTF16Ptr(configFileDir), true, fncFILE_NAME|fncDIR_NAME|fncATTRIBUTES|fncSIZE|fncLAST_WRITE|fncLAST_ACCESS|fncCREATION|fncSECURITY) + if err != nil { + log.Fatalf("Unable to monitor config directory: %v", err) + } + for { + s, err := windows.WaitForSingleObject(h, windows.INFINITE) + if err != nil || s == windows.WAIT_FAILED { + log.Fatalf("Unable to wait on config directory watcher: %v", err) + } + + for _, cb := range storeCallbacks { + cb() + } + + err = findNextChangeNotification(h) + if err != nil { + log.Fatalf("Unable to monitor config directory again: %v", err) + } + } + }() +} |