mkrundir

minimal XDG_RUNTIME_DIR manager
git clone git://git.rr3.xyz/mkrundir
Log | Files | Refs | README

commit 3313f639d9e4984ae6e16115976ff6bac9fa4bdf
Author: Robert Russell <robertrussell.72001@gmail.com>
Date:   Sat,  9 Sep 2023 10:33:00 -0700

Initial commit

Diffstat:
A.gitignore | 2++
AMakefile | 20++++++++++++++++++++
AREADME | 7+++++++
Aconfig.mk | 5+++++
Amkrundir.c | 30++++++++++++++++++++++++++++++
5 files changed, 64 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +mkrundir +\ No newline at end of file diff --git a/Makefile b/Makefile @@ -0,0 +1,20 @@ +.POSIX: + +include config.mk + +mkrundir: mkrundir.c config.mk + $(CC) $(CFLAGS) -o $@ mkrundir.c + +install: mkrundir + mkdir -p $(DESTDIR)$(PREFIX)/bin + cp -f mkrundir $(DESTDIR)$(PREFIX)/bin/ + chown root:root $(DESTDIR)$(PREFIX)/bin/mkrundir + chmod a+rx,u+ws $(DESTDIR)$(PREFIX)/bin/mkrundir + +uninstall: + rm -f $(DESTDIR)$(PREFIX)/bin/mkrundir + +clean: + rm -f mkrundir + +.PHONY: install uninstall clean diff --git a/README b/README @@ -0,0 +1,7 @@ +mkrundir is a simple setuid program for creating XDG_RUNTIME_DIRs in /run/user. +Installation and usage: + # make install + # echo 'export XDG_RUNTIME_DIR=$(mkrundir)' >/etc/profile.d/xdg_runtime_dir.sh +Note that the directory won't be removed when a user fully logs out, contrary +to the XDG specification. Actually, it won't be removed at all, so your /run +directory should be a tmpfs. diff --git a/config.mk b/config.mk @@ -0,0 +1,5 @@ +PREFIX = /usr/local + +CFLAGS = -Wall + +CC = cc diff --git a/mkrundir.c b/mkrundir.c @@ -0,0 +1,30 @@ +#include <errno.h> +#include <limits.h> +#include <stdint.h> +#include <stdio.h> +#include <sys/stat.h> +#include <unistd.h> + +#define PARENT "/run/user/" // Must end with '/'. + +int +main(void) { + uintmax_t uid = getuid(); + uintmax_t gid = getgid(); + + // This should always be big enough. + char path[sizeof PARENT + sizeof(uintmax_t)*CHAR_BIT]; + int n = snprintf(path, sizeof path, PARENT"%ju", uid); + if (n < 0 || n >= sizeof path) return 1; + + if (mkdir(path, S_IRWXU) < 0) { + if (errno != EEXIST) return 1; + // If it already exists, make sure the mode is correct. + if (chmod(path, S_IRWXU) < 0) return 1; + } + if (chown(path, uid, gid) < 0) return 1; + + puts(path); + + return 0; +}