aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--linux-bin/zzz.c18
-rw-r--r--linux-bin/zzz.nim69
2 files changed, 84 insertions, 3 deletions
diff --git a/linux-bin/zzz.c b/linux-bin/zzz.c
index 8bc3c22..479dd25 100644
--- a/linux-bin/zzz.c
+++ b/linux-bin/zzz.c
@@ -4,8 +4,10 @@
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
+#include <sys/wait.h>
#define POWER_STATE_F "/sys/power/state"
+#define SLEEP_TIMEOUT 30
void
die(const char *msg)
@@ -34,8 +36,6 @@ suspend()
n = write(fh, cmd, 3);
if (n == -1)
err("Writing to "POWER_STATE_F);
-
- exit(0);
}
int
@@ -84,7 +84,19 @@ main(int argc, char **argv)
execlp(lockProg, NULL);
err("execlp");
default:
- suspend();
+ for(;;) {
+ ret = 0;
+ ret = waitpid(pid, NULL, WNOHANG);
+
+ if (ret == 0) {
+ suspend();
+ sleep(SLEEP_TIMEOUT);
+ continue;
+ }
+
+ puts("exiting");
+ exit(0);
+ }
}
}
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)