aboutsummaryrefslogtreecommitdiff
path: root/linux-bin/zzz.nim
diff options
context:
space:
mode:
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)