misc: initial import

Wed, 28 Jul 2021 14:28:31 +0200

author
David Demelier <markand@malikania.fr>
date
Wed, 28 Jul 2021 14:28:31 +0200
changeset 0
810d59028aae
child 1
4662c96a5794

misc: initial import

.hgignore file | annotate | diff | comparison | revisions
LICENSE.md file | annotate | diff | comparison | revisions
Makefile file | annotate | diff | comparison | revisions
README.md file | annotate | diff | comparison | revisions
vinit.8 file | annotate | diff | comparison | revisions
vinit.c file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Wed Jul 28 14:28:31 2021 +0200
@@ -0,0 +1,10 @@
+# vim/emacs specific.
+^tags$
+^tags.lock$
+^tags.temp$
+\.swp$
+\.swo$
+
+# object files.
+\.o$
+^vinit$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LICENSE.md	Wed Jul 28 14:28:31 2021 +0200
@@ -0,0 +1,16 @@
+vinit ISC LICENSE
+=================
+
+Copyright (c) 2021 David Demelier <markand@malikania.fr>
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Wed Jul 28 14:28:31 2021 +0200
@@ -0,0 +1,51 @@
+#
+# Makefile -- basic Makefile for vinit
+#
+# Copyright (c) 2021 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+.POSIX:
+
+CC=     cc
+CFLAGS= -Wall -Wextra -O3 -DNDEBUG
+
+PREFIX= /usr/local
+BINDIR= ${PREFIX}/sbin
+MANDIR= ${PREFIX}/share/man
+
+SRCS=   vinit.c
+OBJS=   ${SRCS:.c=.o}
+
+.SUFFIXES:
+.SUFFIXES: .c .o
+
+all: vinit
+
+.c.o:
+	${CC} ${CFLAGS} -c $< -o $@
+
+vinit: ${OBJS}
+	${CC} ${CFLAGS} -o $@ ${OBJS} ${LDFLAGS}
+
+install:
+	mkdir -p ${DESTDIR}${BINDIR}
+	cp vinit ${DESTDIR}${BINDIR}
+	mkdir -p ${DESTDIR}${MANDIR}/man8
+	cp vinit.8 ${DESTDIR}${MANDIR}/man8
+
+clean:
+	rm -f vinit ${OBJS}
+
+.PHONY: all clean install
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Wed Jul 28 14:28:31 2021 +0200
@@ -0,0 +1,30 @@
+vinit
+=====
+
+Vanilla Linux init system.
+
+It's sole purpose is to run the following services files:
+
+- /etc/rc.init: starts the system,
+- /etc/rc.shutdown: shutdown the system.
+
+Features and non features
+-------------------------
+
+- Spawn startup and shutdown scripts,
+- Reap child processes,
+- Does not know anything related to services,
+- Does not your networking, DNS, DHCP, UEFI, journaling, etc.
+
+You may want to check vinit-scripts project for a set of default scripts for a
+better out of the box experience.
+
+Documentation
+-------------
+
+See vinit(8) manual page.
+
+Author
+------
+
+David Demelier <markand@malikania.fr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vinit.8	Wed Jul 28 14:28:31 2021 +0200
@@ -0,0 +1,61 @@
+.\"
+.\" vinit.8 -- vanilla init
+.\"
+.\" Copyright (c) 2021 David Demelier <markand@malikania.fr>
+.\"
+.\" Permission to use, copy, modify, and/or distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd July 28, 2021
+.Dt VINIT 8
+.Os
+.Sh NAME
+.Nm vinit
+.Nd vanilla init
+.Sh DESCRIPTION
+.Nm
+is the official default init system for Vanilla Linux. It's sole purpose is to
+start init scripts and shutdown the system.
+.Ss Startup
+When the system starts,
+.Nm
+invokes
+.Pa /etc/rc.init
+which setups the system and starts some services. There is not respawning
+process, an external tool should be used for that purpose.
+.Ss Shutdown
+When a signal is received the script
+.Pa /etc/rc.shutdown
+is invoked with a unique argument defined as follow:
+.Bl -tag
+.It USR1
+Pass
+.Dq halt
+as argument, halt the system without powering off. The
+.Xr halt 8
+command can be used.
+.It USR2
+Pass
+.Dq poweroff
+as argument, halt the system and powering it off. The
+.Xr poweroff 8
+command can be used.
+.It TERM
+Pass
+.Dq reboot
+as argument, kill services and reboot the system. The
+.Xr reboot 8
+command can be used.
+.Sh AUTHORS
+.Nm
+was written by
+.An David Demelier <markand@malikania.fr> .
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vinit.c	Wed Jul 28 14:28:31 2021 +0200
@@ -0,0 +1,81 @@
+#include <sys/wait.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdnoreturn.h>
+#include <string.h>
+#include <unistd.h>
+
+static char * const rcinit[]      = { "/etc/rc.init", NULL                        };
+static char * const rchalt[]      = { "/etc/rc.shutdown", "halt", NULL            };
+static char * const rcpoweroff[]  = { "/etc/rc.shutdown", "poweroff", NULL        };
+static char * const rcreboot[]    = { "/etc/rc.shutdown", "reboot", NULL          };
+
+static sigset_t set;
+
+noreturn static void
+die(const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+	exit(1);
+}
+
+static void
+spawn(char * const args[])
+{
+	switch (fork()) {
+	case 0:
+		sigprocmask(SIG_UNBLOCK, &set, NULL);
+		setsid();
+		execv(args[0], args);
+		perror("execv");
+		exit(1);
+	case 1:
+		perror("fork");
+	default:
+		break;
+	}
+}
+
+int
+main(void)
+{
+	int sig;
+
+	if (getpid() != 1)
+		die("abort: must be ran as PID 1\n");
+	if (chdir("/") < 0)
+		die("abort: %s\n", strerror(errno));
+
+	sigfillset(&set);
+	sigprocmask(SIG_BLOCK, &set, NULL);
+	spawn(rcinit);
+
+	for (;;) {
+		sigwait(&set, &sig);
+
+		/* Match busybox defaults. */
+		switch (sig) {
+		case SIGUSR1:
+			spawn(rchalt);
+			break;
+		case SIGUSR2:
+			spawn(rcpoweroff);
+			break;
+		case SIGTERM:
+			spawn(rcreboot);
+			break;
+		case SIGCHLD:
+			while (waitpid(-1, NULL, WNOHANG) > 0)
+				continue;
+		default:
+			break;
+		}
+	}
+}

mercurial