├── LICENSE.md ├── Makefile ├── README.md ├── init.c └── respawn.c /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright © 2013-2020 Rich Felker 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | SRCS = $(wildcard *.c) 3 | OBJS = $(SRCS:%.c=%.o) 4 | 5 | CFLAGS = -O2 6 | LDFLAGS = -s 7 | 8 | all: init respawn 9 | 10 | clean: 11 | rm -f init respawn $(OBJS) 12 | 13 | $(OBJS): 14 | 15 | %: %.o 16 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< 17 | 18 | %.o: %.c 19 | $(CC) $(CFLAGS) -c -o $@ $< 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Minimal init 2 | 3 | This is an updated, nommu-compatible version of my minimal init, 4 | adapted from the version originally published on ewontfix.com. 5 | 6 | All it does it run `/etc/rc` on startup and reap orhaned processes 7 | as they exit. It does not do any process supervision. 8 | 9 | The `respawn` utility also included is a minimal process supervisor. 10 | It's not intended for any elaborate supervision, mainly running gettys 11 | for login, started from `/etc/rc` in place of a traditional `inittab`. 12 | The entire command line `argv[1]` to `argv[argc]` is the command that 13 | is executed repeatedly, so `argv[1]` should be an absolute pathname. 14 | Bad Things will happen if it's a program which daemonizes itself. 15 | -------------------------------------------------------------------------------- /init.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | if (getpid() != 1) return 1; 9 | 10 | sigset_t set, old; 11 | sigfillset(&set); 12 | sigprocmask(SIG_BLOCK, &set, &old); 13 | 14 | posix_spawnattr_t attr; 15 | posix_spawnattr_init(&attr); 16 | posix_spawnattr_setsigmask(&attr, &old); 17 | posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSID|POSIX_SPAWN_SETSIGMASK); 18 | 19 | posix_spawn(0, "/etc/rc", 0, &attr, 20 | ((char *[]){ "rc", 0 }), 21 | ((char *[]){ 0 })); 22 | 23 | for (;;) wait(0); 24 | } 25 | -------------------------------------------------------------------------------- /respawn.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char **argv) 9 | { 10 | sigset_t set, old; 11 | sigfillset(&set); 12 | sigprocmask(SIG_BLOCK, &set, &old); 13 | 14 | posix_spawnattr_t attr; 15 | posix_spawnattr_init(&attr); 16 | posix_spawnattr_setsigmask(&attr, &old); 17 | posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSID|POSIX_SPAWN_SETSIGMASK); 18 | 19 | for (;;) { 20 | extern char **environ; 21 | pid_t pid; 22 | if (posix_spawn(&pid, argv[1], 0, &attr, argv+1, environ)) 23 | nanosleep(&(struct timespec){.tv_nsec=100000000}, 0); 24 | waitpid(pid, 0, 0); 25 | } 26 | } 27 | --------------------------------------------------------------------------------