2020-08-18 11:59:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-08 14:24:27 +00:00
|
|
|
"kdelsd/afterlock"
|
2021-10-15 20:22:00 +00:00
|
|
|
"kdelsd/config"
|
|
|
|
"kdelsd/lockfile"
|
2020-09-08 14:24:27 +00:00
|
|
|
"kdelsd/xdg/display"
|
|
|
|
"kdelsd/xdg/keypressdetector"
|
|
|
|
"kdelsd/xdg/lockscreen"
|
2021-10-15 20:22:00 +00:00
|
|
|
"log"
|
2020-08-20 13:57:17 +00:00
|
|
|
"os"
|
2021-10-15 20:22:00 +00:00
|
|
|
"os/signal"
|
2020-08-20 14:57:57 +00:00
|
|
|
"runtime/debug"
|
2021-10-15 20:22:00 +00:00
|
|
|
"syscall"
|
2020-08-18 11:59:20 +00:00
|
|
|
)
|
|
|
|
|
2021-10-15 20:22:00 +00:00
|
|
|
|
|
|
|
type afterLockConfig struct {
|
|
|
|
INITIAL_DELAY int
|
|
|
|
LOOP_DELAY int
|
|
|
|
DEV_NODE string
|
|
|
|
LOCK_PATH string
|
|
|
|
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)
|
|
|
|
}
|
2020-08-18 11:59:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
2020-08-20 13:57:17 +00:00
|
|
|
defer handleErrors()
|
2021-10-15 20:22:00 +00:00
|
|
|
err := config.Build("AFL", &configuration)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-09-08 13:27:52 +00:00
|
|
|
}
|
2021-10-15 20:22:00 +00:00
|
|
|
f := initLogging(configuration.LOG_PATH)
|
|
|
|
if f == nil {
|
|
|
|
panic("Failed to open logfile")
|
|
|
|
}
|
|
|
|
initCleanup(grabExclusiveProcessLock(configuration.LOCK_PATH), f)
|
2020-08-20 13:57:17 +00:00
|
|
|
|
2021-10-15 20:22:00 +00:00
|
|
|
al := buildAfterlock()
|
2020-08-18 11:59:20 +00:00
|
|
|
al.Start()
|
2021-10-15 20:22:00 +00:00
|
|
|
|
|
|
|
mainDone<-true
|
|
|
|
<-cleanupDone
|
2020-08-20 13:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleErrors() {
|
|
|
|
if err := recover(); err != nil {
|
2020-09-08 13:27:52 +00:00
|
|
|
log.Printf("Crashed with unexpected error: %v!\n", err)
|
2021-10-15 20:22:00 +00:00
|
|
|
if configuration.PRINT_STACKTRACES {
|
2020-09-08 13:27:52 +00:00
|
|
|
log.Printf("Stack trace:\n\n")
|
2020-08-20 13:57:17 +00:00
|
|
|
debug.PrintStack()
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 20:22:00 +00:00
|
|
|
func grabExclusiveProcessLock(lockfile_path string) *lockfile.Lockfile {
|
|
|
|
lf := lockfile.New(lockfile_path)
|
2020-08-20 13:57:17 +00:00
|
|
|
err := lf.Lock()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return lf
|
2020-09-08 14:34:10 +00:00
|
|
|
}
|
2021-10-15 20:22:00 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|