aboutsummarylogtreecommitdiffstats
path: root/linger-switch.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'linger-switch.cpp')
-rw-r--r--linger-switch.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/linger-switch.cpp b/linger-switch.cpp
new file mode 100644
index 000000000000..674201a633eb
--- /dev/null
+++ b/linger-switch.cpp
@@ -0,0 +1,48 @@
+#include <pwd.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <cstdio>
+#include <cstdlib>
+#include <string>
+
+void Help(const char* name) {
+ fprintf(stderr, "Usage: %s [enable/disable]\n", name);
+ exit(1);
+}
+void Err(const char* str) {
+ perror(str);
+ exit(1);
+}
+
+int main(int argc, char** argv) {
+ if (setgid(0) < 0) Err("setgid");
+ uid_t uid = getuid();
+ if (setresuid(uid, uid, 0) < 0) Err("setuid");
+ umask(0022);
+ bool del = false, flag = false;
+ if (argv == 0) Help("linger");
+ else if (argc != 2) Help(argv[0]);
+ try {
+ std::string mode_str(argv[1]);
+ if (mode_str == "enable") {
+ del = false;
+ } else if (mode_str == "disable") {
+ del = true;
+ } else {
+ Help(argv[0]);
+ }
+ struct passwd* pwd = getpwuid(uid);
+ if (!pwd) Err("getpwuid");
+ std::string path = std::string("/var/lib/systemd/linger/") + getpwuid(uid)->pw_name;
+ if (setresuid(uid, 0, 0) < 0) Err("setuid");
+ if (del) {
+ unlink(path.c_str());
+ } else {
+ int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_NOCTTY | O_NONBLOCK, 0644);
+ if (fd >= 0) futimens(fd, nullptr);
+ close(fd);
+ }
+ } catch (...) { Err(argv[0]); }
+}