linux-qubasis

linux oasis port as a qubes template

git clone https://9o.is/git/linux-qubasis.git

commit 42d398c118c36fdad8a0bd53169df095c78bce4b
parent 13788e858a0e13b4463e011bdcf3f952d175d46e
Author: Jul <jul@9o.is>
Date:   Tue, 16 Sep 2025 19:45:42 +0800

add sinit

Diffstat:
M.gitmodules | 3+++
Mgen.sh | 1+
Apkg/sinit/config.h | 5+++++
Apkg/sinit/gen.sh | 17+++++++++++++++++
Apkg/sinit/repo | 1+
Apkg/sinit/shutdown.c | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 114 insertions(+), 0 deletions(-)

diff --git a/.gitmodules b/.gitmodules @@ -86,3 +86,6 @@ path = pkg/ubase/repo url = git://git.suckless.org/ubase ignore = all +[submodule "sinit"] + path = pkg/sinit/repo + url = git://git.suckless.org/sinit diff --git a/gen.sh b/gen.sh @@ -33,6 +33,7 @@ subgen pkg/perp subgen pkg/probe subgen pkg/rc subgen pkg/sbase +subgen pkg/sinit subgen pkg/ubase subgen pkg/util-linux subgen pkg/xz-embedded diff --git a/pkg/sinit/config.h b/pkg/sinit/config.h @@ -0,0 +1,5 @@ +/* See LICENSE file for copyright and license details. */ + +static char *const rcinitcmd[] = { PREFIX "/etc/rc.init", NULL }; +static char *const rcrebootcmd[] = { PREFIX "/etc/rc.shutdown", "reboot", NULL }; +static char *const rcpoweroffcmd[] = { PREFIX "/etc/rc.shutdown", "poweroff", NULL }; diff --git a/pkg/sinit/gen.sh b/pkg/sinit/gen.sh @@ -0,0 +1,17 @@ +fetch git + +cflags " + -std=c99 + -D _POSIX_C_SOURCE=200809L + -D PREFIX='"\"'/$PREFIX'\""' + -I $dir + -Wno-unused-parameter +" + +exe sinit sinit.c +exe shutdown $dir/shutdown.c + +bin sinit +sym sinit /bin/init +man sinit.8 +bin shutdown diff --git a/pkg/sinit/repo b/pkg/sinit/repo @@ -0,0 +1 @@ +Subproject commit 28c44b6b94a870f2942c37f9cfbae8b770595712 diff --git a/pkg/sinit/shutdown.c b/pkg/sinit/shutdown.c @@ -0,0 +1,87 @@ +/* See LICENSE file for copyright and license details. */ +#define _XOPEN_SOURCE 700 +#include <errno.h> +#include <mntent.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdnoreturn.h> +#include <string.h> +#include <sys/mount.h> +#include <sys/reboot.h> +#include <unistd.h> + +static noreturn void +usage(void) +{ + fprintf(stderr, "usage: shutdown [-hpr]\n"); + exit(2); +} + +int +main(int argc, char *argv[]) +{ + FILE *fp; + char **dirs = NULL; + size_t n = 0; + struct mntent *mnt; + int cmd = RB_POWER_OFF; + + while (*++argv && (*argv)[0] == '-' && (*argv)[1]) { + switch ((*argv)[1]) { + case 'h': + cmd = RB_HALT_SYSTEM; + break; + case 'p': + cmd = RB_POWER_OFF; + break; + case 'r': + cmd = RB_AUTOBOOT; + break; + default: + usage(); + } + } + if (*argv) + usage(); + + if (getsid(0) != getpid()) { + fprintf(stderr, "must be session leader\n"); + return 1; + } + + sync(); + kill(-1, SIGTERM); + sleep(2); + kill(-1, SIGKILL); + + sync(); + fp = setmntent("/proc/mounts", "r"); + if (!fp) { + perror("setmntent"); + goto reboot; + } + while ((mnt = getmntent(fp))) { + if (!(dirs = realloc(dirs, ++n * sizeof(*dirs)))) { + perror("realloc"); + break; + } + if (!(dirs[n - 1] = strdup(mnt->mnt_dir))) { + perror("strdup"); + break; + } + } + endmntent(fp); + while (n) { + if (umount(dirs[--n]) < 0) + fprintf(stderr, "umount %s: %s\n", dirs[n], strerror(errno)); + free(dirs[n]); + } + free(dirs); + +reboot: + if (reboot(cmd) < 0) { + perror("reboot"); + return 1; + } +}