Improve cleanup logic (handle singals gracefully) and use config magic

This commit is contained in:
Kristóf Tóth 2021-10-15 22:22:00 +02:00
parent 0177d349df
commit bae3b95b6c
2 changed files with 81 additions and 38 deletions

View File

@ -2,49 +2,70 @@ package main
import ( import (
"kdelsd/afterlock" "kdelsd/afterlock"
"kdelsd/config"
"kdelsd/lockfile"
"kdelsd/xdg/display" "kdelsd/xdg/display"
"kdelsd/xdg/keypressdetector" "kdelsd/xdg/keypressdetector"
"kdelsd/xdg/lockscreen" "kdelsd/xdg/lockscreen"
"kdelsd/lockfile"
"os"
"runtime/debug"
"log" "log"
"os"
"os/signal"
"runtime/debug"
"syscall"
) )
const (
initialDelay = 3 type afterLockConfig struct {
loopDelay = 7 INITIAL_DELAY int
keyboardDeviceNode = "/dev/input/by-id/usb-Dell_Dell_USB_Entry_Keyboard-event-kbd" LOOP_DELAY int
lockfilePath = "/tmp/after-lock.lock" DEV_NODE string
printStackTraces = false LOCK_PATH string
logFilePath = "/home/superuser/Desktop/after-lock.log" LOG_PATH string
) PRINT_STACKTRACES bool
}
var configuration = afterLockConfig{
INITIAL_DELAY: 3,
LOOP_DELAY: 7,
LOCK_PATH: "/tmp/after-lock.lock",
PRINT_STACKTRACES: true,
}
var mainDone chan bool
var cleanupDone chan bool
var sig chan os.Signal
func init() {
mainDone = make(chan bool)
cleanupDone = make(chan bool)
sig = make(chan os.Signal)
}
func main() { func main() {
defer handleErrors() defer handleErrors()
f := initLogging() err := config.Build("AFL", &configuration)
if f != nil { if err != nil {
defer teardownLogging(f) panic(err)
} }
f := initLogging(configuration.LOG_PATH)
if f == nil {
panic("Failed to open logfile")
}
initCleanup(grabExclusiveProcessLock(configuration.LOCK_PATH), f)
lock := grabExclusiveProcessLock() al := buildAfterlock()
defer lock.Unlock()
al := afterlock.New(keyboardDeviceNode)
al.InitialDelay = initialDelay
al.LoopDelay = loopDelay
al.Display = display.Xset{}
al.LockScreen = lockscreen.XDG{}
al.KeypressDetector = keypressdetector.Evdev{}
al.Start() al.Start()
mainDone<-true
<-cleanupDone
} }
func handleErrors() { func handleErrors() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Printf("Crashed with unexpected error: %v!\n", err) log.Printf("Crashed with unexpected error: %v!\n", err)
if printStackTraces { if configuration.PRINT_STACKTRACES {
log.Printf("Stack trace:\n\n") log.Printf("Stack trace:\n\n")
debug.PrintStack() debug.PrintStack()
} }
@ -52,11 +73,37 @@ func handleErrors() {
} }
} }
func grabExclusiveProcessLock() *lockfile.Lockfile { func grabExclusiveProcessLock(lockfile_path string) *lockfile.Lockfile {
lf := lockfile.New(lockfilePath) lf := lockfile.New(lockfile_path)
err := lf.Lock() err := lf.Lock()
if err != nil { if err != nil {
panic(err) panic(err)
} }
return lf return lf
} }
func initCleanup(lock *lockfile.Lockfile, logfile *os.File) {
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
select{
case <-mainDone:
case <-sig:
}
lock.Unlock()
teardownLogging(logfile)
cleanupDone<-true
}()
}
func buildAfterlock() *afterlock.AfterLock {
al := afterlock.New(configuration.DEV_NODE)
al.InitialDelay = uint(configuration.INITIAL_DELAY)
al.LoopDelay = uint(configuration.LOOP_DELAY)
al.Display = display.Xset{}
al.LockScreen = lockscreen.XDG{}
al.KeypressDetector = keypressdetector.Evdev{}
return al
}

View File

@ -5,11 +5,7 @@ import (
"log" "log"
) )
func initLogging() *os.File { func initLogging(logFilePath string) *os.File {
if len(logFilePath) == 0 {
return nil
}
f, err := os.OpenFile(logFilePath, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0600) f, err := os.OpenFile(logFilePath, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0600)
if err != nil { if err != nil {
panic(err) panic(err)
@ -22,16 +18,16 @@ func initLogging() *os.File {
func teardownLogging(f *os.File) { func teardownLogging(f *os.File) {
fi, err := f.Stat() fi, err := f.Stat()
if err != nil { if err != nil {
panic(err) panic(err)
} }
if fi.Size() == 0 { if fi.Size() == 0 {
err = os.Remove(f.Name()) err = os.Remove(f.Name())
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
err = f.Close() err = f.Close()
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }