aboutsummaryrefslogtreecommitdiff
path: root/linux-bin/zzz.nim
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2024-02-01 22:01:54 -0500
committerMitchell Riedstra <mitch@riedstra.dev>2024-02-01 22:46:00 -0500
commit8931c5f9a5eec7b91899f0c1e08f5d33f4432caf (patch)
tree1b3e491d8f1a12e47b92491c60de2cbcf0e795df /linux-bin/zzz.nim
parentd47e29c2f4be62ee250a054f4e6473604b84059c (diff)
downloaddotfiles-8931c5f9a5eec7b91899f0c1e08f5d33f4432caf.tar.gz
dotfiles-8931c5f9a5eec7b91899f0c1e08f5d33f4432caf.tar.xz
Auto suspend after 30 seconds
Also add a nim version.
Diffstat (limited to 'linux-bin/zzz.nim')
-rw-r--r--linux-bin/zzz.nim69
1 files changed, 69 insertions, 0 deletions
diff --git a/linux-bin/zzz.nim b/linux-bin/zzz.nim
new file mode 100644
index 0000000..eac2797
--- /dev/null
+++ b/linux-bin/zzz.nim
@@ -0,0 +1,69 @@
+# Just a copy of the C version.
+# I don't actually use this at the moment, but it was a nice small
+# introduction of using nim for something.
+import std/strformat
+import std/envvars
+import std/os
+import posix
+import posix_utils as putils
+
+let power_state_f = "/sys/power/state"
+
+proc suspend() =
+ writeFile(power_state_f, "mem")
+
+let uid = posix.getuid()
+let gid = posix.getgid()
+let euid = posix.geteuid()
+
+var lockProg = "slock"
+let lockProgWl = "swaylock"
+
+if euid != 0:
+ echo "This program must be run with setuid"
+ system.quit(1)
+
+if getEnv("XDG_SESSION_TYPE") == "wayland":
+ lockProg = lockProgWl
+
+for i in 1..os.paramCount():
+ if paramStr(i) == "-l":
+ if i+1 <= os.paramCount():
+ lockProg = paramStr(i + 1)
+ else:
+ echo "-l requires an argument"
+ system.quit(1)
+ else:
+ let s = paramStr(i)
+ echo &"unknown argument {s}"
+ system.quit(1)
+
+var pid = posix.fork()
+if pid == -1:
+ echo "unknown error"
+ system.quit(1)
+elif pid == 0:
+ var ret = posix.setuid(uid)
+ if ret == -1:
+ echo "Failed to setuid"
+ system.quit(1)
+ ret = posix.setgid(gid)
+ if ret == -1:
+ echo "Failed to setuid"
+ system.quit(1)
+
+ ret = execlp(lockprog, "")
+ echo &"excelp: {ret}"
+else:
+ suspend()
+ while true:
+ var info : cint
+ let ret = waitpid(pid, info, WNOHANG)
+
+ if ret == 0:
+ suspend()
+ sleep(10000)
+ continue
+
+ echo "exiting"
+ system.quit(0)