From 1172c1e817d26d5f9bb0ec289dd9a71aadf2759c Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Tue, 20 Apr 2021 00:00:22 +0000 Subject: Add portability infrastructure --- Makefile | 3 +- package/deps.mak | 23 +++++++++++---- package/targets.mak | 3 ++ src/include-local/os.h | 11 ++++++++ src/init/deps-exe/s6-linux-init | 1 + src/init/s6-linux-init.c | 50 ++++----------------------------- src/os/deps-lib/os-freebsd | 3 ++ src/os/deps-lib/os-linux | 4 +++ src/os/linux-os_kbspecials.c | 33 ++++++++++++++++++++++ src/os/linux-os_mount_devtmpfs.c | 10 +++++++ src/os/linux-os_mount_tmpfs.c | 31 ++++++++++++++++++++ src/os/linux-os_reboot.c | 10 +++++++ src/shutdown/deps-exe/s6-linux-init-hpr | 1 + src/shutdown/s6-linux-init-hpr.c | 6 ++-- tools/gen-deps.sh | 5 ---- 15 files changed, 134 insertions(+), 60 deletions(-) create mode 100644 src/include-local/os.h create mode 100644 src/os/deps-lib/os-freebsd create mode 100644 src/os/deps-lib/os-linux create mode 100644 src/os/linux-os_kbspecials.c create mode 100644 src/os/linux-os_mount_devtmpfs.c create mode 100644 src/os/linux-os_mount_tmpfs.c create mode 100644 src/os/linux-os_reboot.c diff --git a/Makefile b/Makefile index f39965d..c0cd40c 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,6 @@ CC = $(error Please use ./configure first) STATIC_LIBS := SHARED_LIBS := -INTERNAL_LIBS := EXTRA_TARGETS := LIB_DEFS := @@ -54,7 +53,7 @@ STRIP := $(CROSS_COMPILE)strip INSTALL := ./tools/install.sh ALL_BINS := $(LIBEXEC_TARGETS) $(BIN_TARGETS) -ALL_LIBS := $(SHARED_LIBS) $(STATIC_LIBS) $(INTERNAL_LIBS) +ALL_LIBS := $(SHARED_LIBS) $(STATIC_LIBS) ALL_INCLUDES := $(wildcard src/include/$(package)/*.h) all: $(ALL_LIBS) $(ALL_BINS) $(ALL_INCLUDES) diff --git a/package/deps.mak b/package/deps.mak index 1f0bd20..bd6a5e0 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -6,22 +6,26 @@ src/include-local/initctl.h: src/include/s6-linux-init/config.h src/shutdown/hpr.h: src/include-local/initctl.h src/init/s6-linux-init-maker.o src/init/s6-linux-init-maker.lo: src/init/s6-linux-init-maker.c src/include-local/defaults.h src/include-local/initctl.h src/include/s6-linux-init/config.h src/init/s6-linux-init-telinit.o src/init/s6-linux-init-telinit.lo: src/init/s6-linux-init-telinit.c src/include-local/initctl.h src/include/s6-linux-init/config.h -src/init/s6-linux-init.o src/init/s6-linux-init.lo: src/init/s6-linux-init.c src/include-local/defaults.h src/include-local/initctl.h src/include/s6-linux-init/config.h +src/init/s6-linux-init.o src/init/s6-linux-init.lo: src/init/s6-linux-init.c src/include-local/defaults.h src/include-local/initctl.h src/include-local/os.h src/include/s6-linux-init/config.h src/lib/s6_linux_init_logouthook.o src/lib/s6_linux_init_logouthook.lo: src/lib/s6_linux_init_logouthook.c src/include-local/initctl.h src/include/s6-linux-init/s6-linux-init.h src/misc/s6-linux-init-echo.o src/misc/s6-linux-init-echo.lo: src/misc/s6-linux-init-echo.c src/misc/s6-linux-init-logouthookd.o src/misc/s6-linux-init-logouthookd.lo: src/misc/s6-linux-init-logouthookd.c src/misc/s6-linux-init-nuke.o src/misc/s6-linux-init-nuke.lo: src/misc/s6-linux-init-nuke.c src/misc/s6-linux-init-umountall.o src/misc/s6-linux-init-umountall.lo: src/misc/s6-linux-init-umountall.c src/include/s6-linux-init/config.h +src/os/linux-os_kbspecials.o src/os/linux-os_kbspecials.lo: src/os/linux-os_kbspecials.c src/include-local/os.h +src/os/linux-os_mount_devtmpfs.o src/os/linux-os_mount_devtmpfs.lo: src/os/linux-os_mount_devtmpfs.c src/include-local/os.h +src/os/linux-os_mount_tmpfs.o src/os/linux-os_mount_tmpfs.lo: src/os/linux-os_mount_tmpfs.c src/include-local/os.h +src/os/linux-os_reboot.o src/os/linux-os_reboot.lo: src/os/linux-os_reboot.c src/include-local/os.h src/shutdown/hpr_confirm_hostname.o src/shutdown/hpr_confirm_hostname.lo: src/shutdown/hpr_confirm_hostname.c src/shutdown/hpr.h src/shutdown/hpr_shutdown.o src/shutdown/hpr_shutdown.lo: src/shutdown/hpr_shutdown.c src/shutdown/hpr.h src/shutdown/hpr_wall.o src/shutdown/hpr_wall.lo: src/shutdown/hpr_wall.c src/shutdown/hpr.h src/shutdown/hpr_wallv.o src/shutdown/hpr_wallv.lo: src/shutdown/hpr_wallv.c src/shutdown/hpr.h -src/shutdown/s6-linux-init-hpr.o src/shutdown/s6-linux-init-hpr.lo: src/shutdown/s6-linux-init-hpr.c src/include-local/defaults.h src/shutdown/hpr.h +src/shutdown/s6-linux-init-hpr.o src/shutdown/s6-linux-init-hpr.lo: src/shutdown/s6-linux-init-hpr.c src/include-local/defaults.h src/shutdown/hpr.h src/include-local/os.h src/shutdown/s6-linux-init-shutdown.o src/shutdown/s6-linux-init-shutdown.lo: src/shutdown/s6-linux-init-shutdown.c src/include-local/defaults.h src/shutdown/hpr.h src/include-local/initctl.h src/shutdown/s6-linux-init-shutdownd.o src/shutdown/s6-linux-init-shutdownd.lo: src/shutdown/s6-linux-init-shutdownd.c src/include-local/defaults.h src/shutdown/hpr.h src/include-local/initctl.h src/include/s6-linux-init/config.h s6-linux-init: EXTRA_LIBS := -lskarnet -s6-linux-init: src/init/s6-linux-init.o +s6-linux-init: src/init/s6-linux-init.o libos-${OS}.a.xyzzy s6-linux-init-maker: EXTRA_LIBS := -lskarnet ${MAYBEPTHREAD_LIB} s6-linux-init-maker: src/init/s6-linux-init-maker.o ${LIBNSSS} s6-linux-init-telinit: EXTRA_LIBS := -lskarnet @@ -42,14 +46,23 @@ s6-linux-init-nuke: src/misc/s6-linux-init-nuke.o s6-linux-init-umountall: EXTRA_LIBS := -lskarnet s6-linux-init-umountall: src/misc/s6-linux-init-umountall.o ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),) +libos-freebsd.a.xyzzy: src/os/freebsd-os_reboot.o src/os/freebsd-os_kbspecials.o src/os/freebsd-os_mount_tmpfs.o +else +libos-freebsd.a.xyzzy: src/os/freebsd-os_reboot.lo src/os/freebsd-os_kbspecials.lo src/os/freebsd-os_mount_tmpfs.lo +endif +ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),) +libos-linux.a.xyzzy: src/os/linux-os_reboot.o src/os/linux-os_kbspecials.o src/os/linux-os_mount_devtmpfs.o src/os/linux-os_mount_tmpfs.o +else +libos-linux.a.xyzzy: src/os/linux-os_reboot.lo src/os/linux-os_kbspecials.lo src/os/linux-os_mount_devtmpfs.lo src/os/linux-os_mount_tmpfs.lo +endif +ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),) libhpr.a.xyzzy: src/shutdown/hpr_shutdown.o src/shutdown/hpr_wall.o src/shutdown/hpr_wallv.o src/shutdown/hpr_confirm_hostname.o else libhpr.a.xyzzy: src/shutdown/hpr_shutdown.lo src/shutdown/hpr_wall.lo src/shutdown/hpr_wallv.lo src/shutdown/hpr_confirm_hostname.lo endif s6-linux-init-hpr: EXTRA_LIBS := -lskarnet ${SYSCLOCK_LIB} ${SOCKET_LIB} -s6-linux-init-hpr: src/shutdown/s6-linux-init-hpr.o libhpr.a.xyzzy ${LIBUTMPS} +s6-linux-init-hpr: src/shutdown/s6-linux-init-hpr.o libhpr.a.xyzzy libos-${OS}.a.xyzzy ${LIBUTMPS} s6-linux-init-shutdown: EXTRA_LIBS := -lskarnet ${SYSCLOCK_LIB} ${SOCKET_LIB} s6-linux-init-shutdown: src/shutdown/s6-linux-init-shutdown.o libhpr.a.xyzzy ${LIBUTMPS} s6-linux-init-shutdownd: EXTRA_LIBS := -ls6 -lskarnet ${SYSCLOCK_LIB} ${SOCKET_LIB} s6-linux-init-shutdownd: src/shutdown/s6-linux-init-shutdownd.o ${LIBUTMPS} -INTERNAL_LIBS := libhpr.a.xyzzy diff --git a/package/targets.mak b/package/targets.mak index 04d940f..f78cd32 100644 --- a/package/targets.mak +++ b/package/targets.mak @@ -10,6 +10,9 @@ s6-linux-init-nuke \ s6-linux-init-umountall \ s6-linux-init-maker +OS := $(shell uname | tr A-Z a-z) +EXTRA_TARGETS := libhpr.a.xyzzy libos-$(OS).a.xyzzy + LIB_DEFS := S6_LINUX_INIT=s6_linux_init SKELFILES := $(wildcard skel/*) diff --git a/src/include-local/os.h b/src/include-local/os.h new file mode 100644 index 0000000..72062e9 --- /dev/null +++ b/src/include-local/os.h @@ -0,0 +1,11 @@ +/* ISC license. */ + +#ifndef S6_LINUX_INIT_OS_H +#define S6_LINUX_INIT_OS_H + +extern void os_reboot (int) ; +extern void os_kbspecials (int) ; +extern void os_mount_tmpfs (char const *, unsigned int) ; +extern int os_mount_devtmpfs (char const *) ; + +#endif diff --git a/src/init/deps-exe/s6-linux-init b/src/init/deps-exe/s6-linux-init index e7187fe..78c08eb 100644 --- a/src/init/deps-exe/s6-linux-init +++ b/src/init/deps-exe/s6-linux-init @@ -1 +1,2 @@ +libos-${OS}.a.xyzzy -lskarnet diff --git a/src/init/s6-linux-init.c b/src/init/s6-linux-init.c index 9e2d86e..c868884 100644 --- a/src/init/s6-linux-init.c +++ b/src/init/s6-linux-init.c @@ -7,11 +7,6 @@ #include #include #include -#include -#include -#include - -#include #include #include @@ -26,6 +21,7 @@ #include #include +#include "os.h" #include "defaults.h" #include "initctl.h" @@ -67,25 +63,6 @@ static inline void wait_for_notif (int fd) close(fd) ; } -static void kbspecials (void) -{ - int fd ; - if (inns) return ; - fd = open("/dev/tty0", O_RDONLY | O_NOCTTY) ; - if (fd < 0) - strerr_warnwu2sys("open /dev/", "tty0 (kbrequest will not be handled)") ; - else - { - if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0) - strerr_warnwu2sys("ioctl KDSIGACCEPT on ", "tty0 (kbrequest will not be handled)") ; - close(fd) ; - } - - sig_block(SIGINT) ; /* don't panic on early cad before s6-svscan catches it */ - if (reboot(RB_DISABLE_CAD) == -1) - strerr_warnwu1sys("trap ctrl-alt-del") ; -} - static inline void run_stage2 (char const *basedir, char const **argv, unsigned int argc, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen, char const *initdefault) { size_t dirlen = strlen(basedir) ; @@ -185,7 +162,7 @@ int main (int argc, char const **argv, char const *const *envp) close(1) ; close(2) ; /* at this point we're totally in the dark, hoping /dev/console will work */ - nope = mount("dev", slashdev, "devtmpfs", MS_NOSUID | MS_NOEXEC, "") < 0 ; + nope = os_mount_devtmpfs(slashdev) < 0 ; e = errno ; if (open("/dev/console", O_WRONLY) || fd_move(2, 0) < 0 @@ -206,24 +183,7 @@ int main (int argc, char const **argv, char const *const *envp) if (fd_move(0, p[0]) < 0) strerr_diefu1sys(111, "fd_move to stdin") ; } - if (mounttype) - { - if (mounttype == 2) - { - if (mount("tmpfs", S6_LINUX_INIT_TMPFS, "tmpfs", MS_REMOUNT | MS_NODEV | MS_NOSUID, "mode=0755") == -1) - strerr_diefu1sys(111, "remount " S6_LINUX_INIT_TMPFS) ; - } - else - { - if (umount(S6_LINUX_INIT_TMPFS) == -1) - { - if (errno != EINVAL) - strerr_warnwu1sys("umount " S6_LINUX_INIT_TMPFS) ; - } - if (mount("tmpfs", S6_LINUX_INIT_TMPFS, "tmpfs", MS_NODEV | MS_NOSUID, "mode=0755") == -1) - strerr_diefu1sys(111, "mount tmpfs on " S6_LINUX_INIT_TMPFS) ; - } - } + os_mount_tmpfs(S6_LINUX_INIT_TMPFS, mounttype) ; { size_t dirlen = strlen(basedir) ; @@ -273,7 +233,7 @@ int main (int argc, char const **argv, char const *const *envp) close(notifpipe[0]) ; fmtfd[1] = 'd' ; fmtfd[2 + uint_fmt(fmtfd + 2, notifpipe[1])] = 0 ; - kbspecials() ; + os_kbspecials(inns) ; } else { @@ -281,7 +241,7 @@ int main (int argc, char const **argv, char const *const *envp) if (fd < 0) strerr_diefu1sys(111, "dup stderr") ; fmtfd[1] = 'X' ; fmtfd[2 + uint_fmt(fmtfd + 2, (unsigned int)fd)] = 0 ; - kbspecials() ; + os_kbspecials(inns) ; if (fd_copy(2, 1) == -1) strerr_diefu1sys(111, "redirect output file descriptor") ; } diff --git a/src/os/deps-lib/os-freebsd b/src/os/deps-lib/os-freebsd new file mode 100644 index 0000000..a8c2a99 --- /dev/null +++ b/src/os/deps-lib/os-freebsd @@ -0,0 +1,3 @@ +freebsd-os_reboot.o +freebsd-os_kbspecials.o +freebsd-os_mount_tmpfs.o diff --git a/src/os/deps-lib/os-linux b/src/os/deps-lib/os-linux new file mode 100644 index 0000000..56fe3fc --- /dev/null +++ b/src/os/deps-lib/os-linux @@ -0,0 +1,4 @@ +linux-os_reboot.o +linux-os_kbspecials.o +linux-os_mount_devtmpfs.o +linux-os_mount_tmpfs.o diff --git a/src/os/linux-os_kbspecials.c b/src/os/linux-os_kbspecials.c new file mode 100644 index 0000000..0d407ab --- /dev/null +++ b/src/os/linux-os_kbspecials.c @@ -0,0 +1,33 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include "os.h" + +void os_kbspecials (int inns) +{ + int fd ; + if (inns) return ; + fd = open("/dev/tty0", O_RDONLY | O_NOCTTY) ; + if (fd < 0) + strerr_warnwu2sys("open /dev/", "tty0 (kbrequest will not be handled)") ; + else + { + if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0) + strerr_warnwu2sys("ioctl KDSIGACCEPT on ", "tty0 (kbrequest will not be handled)") ; + close(fd) ; + } + + sig_block(SIGINT) ; /* don't panic on early cad before s6-svscan catches it */ + if (reboot(RB_DISABLE_CAD) == -1) + strerr_warnwu1sys("trap ctrl-alt-del") ; +} diff --git a/src/os/linux-os_mount_devtmpfs.c b/src/os/linux-os_mount_devtmpfs.c new file mode 100644 index 0000000..b3f34a1 --- /dev/null +++ b/src/os/linux-os_mount_devtmpfs.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include + +#include "os.h" + +int os_mount_devtmpfs (char const *point) +{ + return mount("dev", point, "devtmpfs", MS_NOSUID | MS_NOEXEC, "") ; +} diff --git a/src/os/linux-os_mount_tmpfs.c b/src/os/linux-os_mount_tmpfs.c new file mode 100644 index 0000000..0e173d0 --- /dev/null +++ b/src/os/linux-os_mount_tmpfs.c @@ -0,0 +1,31 @@ +/* ISC license. */ + +#include + +#include + +#include + +#include "os.h" + +void os_mount_tmpfs (char const *point, unsigned int mounttype) +{ + if (mounttype) + { + if (mounttype == 2) + { + if (mount("tmpfs", point, "tmpfs", MS_REMOUNT | MS_NODEV | MS_NOSUID, "mode=0755") == -1) + strerr_diefu2sys(111, "remount ", point) ; + } + else + { + if (umount(point) == -1) + { + if (errno != EINVAL) + strerr_warnwu2sys("umount ", point) ; + } + if (mount("tmpfs", point, "tmpfs", MS_NODEV | MS_NOSUID, "mode=0755") == -1) + strerr_diefu2sys(111, "mount tmpfs on ", point) ; + } + } +} diff --git a/src/os/linux-os_reboot.c b/src/os/linux-os_reboot.c new file mode 100644 index 0000000..fb41584 --- /dev/null +++ b/src/os/linux-os_reboot.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include + +#include "os.h" + +void os_reboot (int what) +{ + reboot(what == 3 ? RB_AUTOBOOT : what == 2 ? RB_POWER_OFF : RB_HALT_SYSTEM) ; +} diff --git a/src/shutdown/deps-exe/s6-linux-init-hpr b/src/shutdown/deps-exe/s6-linux-init-hpr index 454a027..e556641 100644 --- a/src/shutdown/deps-exe/s6-linux-init-hpr +++ b/src/shutdown/deps-exe/s6-linux-init-hpr @@ -1,4 +1,5 @@ libhpr.a.xyzzy +libos-${OS}.a.xyzzy ${LIBUTMPS} -lskarnet ${SYSCLOCK_LIB} diff --git a/src/shutdown/s6-linux-init-hpr.c b/src/shutdown/s6-linux-init-hpr.c index a874ac3..c92717c 100644 --- a/src/shutdown/s6-linux-init-hpr.c +++ b/src/shutdown/s6-linux-init-hpr.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -14,6 +13,7 @@ #include #include +#include "os.h" #include "defaults.h" #include "hpr.h" @@ -82,8 +82,8 @@ int main (int argc, char const *const *argv) if (force) { if (dosync) sync() ; - reboot(what == 3 ? RB_AUTOBOOT : what == 2 ? RB_POWER_OFF : RB_HALT_SYSTEM) ; - strerr_diefu1sys(111, "reboot()") ; + os_reboot(what) ; + strerr_diefu1sys(111, "os_reboot") ; } if (!tain_now_g()) strerr_warnw1sys("get current time") ; diff --git a/tools/gen-deps.sh b/tools/gen-deps.sh index cff90ad..d6a3c40 100755 --- a/tools/gen-deps.sh +++ b/tools/gen-deps.sh @@ -7,8 +7,6 @@ echo '# This file has been generated by tools/gen-deps.sh' echo '#' echo -internal_libs= - for dir in src/include/${package} src/* ; do for file in $(ls -1 $dir | grep -- \\.h$) ; do { @@ -75,8 +73,6 @@ for dir in $(ls -1 src | grep -v ^include) ; do if grep -F "LIB_DEFS :=" package/targets.mak | grep -qF "$file" ; then echo "lib${file}.so.xyzzy: EXTRA_LIBS :=$libs" echo "lib${file}.so.xyzzy:$(echo "$deps" | sed 's/\.o/.lo/g')" - else - internal_libs="$internal_libs lib${file}.a.xyzzy" fi done @@ -97,4 +93,3 @@ for dir in $(ls -1 src | grep -v ^include) ; do echo "$file: src/$dir/$file.o$deps" done done -echo "INTERNAL_LIBS :=$internal_libs" -- cgit v1.2.3