summarylogtreecommitdiffstats
diff options
context:
space:
mode:
authoradrien10182019-10-10 09:53:05 +0800
committeradrien10182019-10-10 09:53:05 +0800
commit10099c8849b784b271f41efcfa647a0f83d0f105 (patch)
tree5e968bee3021a6cef8533b2b33f7e066e6a6c25b
downloadaur-10099c8849b784b271f41efcfa647a0f83d0f105.tar.gz
Initial commit
-rw-r--r--.SRCINFO15
-rw-r--r--Makefile2
-rw-r--r--PKGBUILD20
-rw-r--r--README.md9
-rw-r--r--linger-switch.cpp48
5 files changed, 94 insertions, 0 deletions
diff --git a/.SRCINFO b/.SRCINFO
new file mode 100644
index 00000000000..f6788a8eff2
--- /dev/null
+++ b/.SRCINFO
@@ -0,0 +1,15 @@
+pkgbase = linger-switch
+ pkgdesc = A tool to let user to enable / disable lingering
+ pkgver = 1.0
+ pkgrel = 2
+ url = https://github.com/adrien1018/linger-switch
+ arch = i686
+ arch = x86_64
+ provides = linger-switch
+ source = Makefile
+ source = linger-switch.cpp
+ sha256sums = ade71a4c53d521a18c37aff2274e7e902deaad411958b3194879ca37c294ceb3
+ sha256sums = de7453dce18dc64a4db9b2e9cb77f27bde8a4480d476aa8e45c8ca88166c346a
+
+pkgname = linger-switch
+
diff --git a/Makefile b/Makefile
new file mode 100644
index 00000000000..4a0fa606a45
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,2 @@
+all: linger-switch
+linger-switch: linger-switch.cpp
diff --git a/PKGBUILD b/PKGBUILD
new file mode 100644
index 00000000000..a2c59d120a6
--- /dev/null
+++ b/PKGBUILD
@@ -0,0 +1,20 @@
+# Author: Adrien Wu <adrien.sf.wu@gmail.com>
+pkgname=linger-switch
+pkgver=1.0
+pkgrel=2
+pkgdesc="A tool to let user to enable / disable lingering"
+url="https://github.com/adrien1018/linger-switch"
+arch=('i686' 'x86_64')
+provides=('linger-switch')
+source=('Makefile' 'linger-switch.cpp')
+sha256sums=('ade71a4c53d521a18c37aff2274e7e902deaad411958b3194879ca37c294ceb3' 'de7453dce18dc64a4db9b2e9cb77f27bde8a4480d476aa8e45c8ca88166c346a')
+
+build() {
+ cd "$srcdir"
+ make
+}
+
+package() {
+ install -d -m0711 "${pkgdir}/var/lib/systemd/linger"
+ install -D -m4755 "$srcdir/linger-switch" "${pkgdir}/usr/bin/linger-switch"
+}
diff --git a/README.md b/README.md
new file mode 100644
index 00000000000..8e537150ec6
--- /dev/null
+++ b/README.md
@@ -0,0 +1,9 @@
+linger-switch
+=======
+
+A simple tool to let user to enable / disable lingering in Arch Linux.
+
+```
+$ linger-switch enable # enable lingering for the current user
+$ linger-switch disable # disable
+```
diff --git a/linger-switch.cpp b/linger-switch.cpp
new file mode 100644
index 00000000000..674201a633e
--- /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]); }
+}