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 --- 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 ++-- 11 files changed, 112 insertions(+), 48 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 (limited to 'src') 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") ; -- cgit v1.2.3