From bb2c2aaee584a50eeec66e7f8a28f13c9c808a20 Mon Sep 17 00:00:00 2001 From: assaf Date: Sun, 8 Oct 2023 17:48:32 -0700 Subject: [PATCH] initial import --- .gitignore | 6 ++++++ Makefile | 32 +++++++++++++++++++++++++++++++ docker/Dockerfile | 9 +++++++++ scripts/build.sh | 3 +++ tryboot.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 docker/Dockerfile create mode 100755 scripts/build.sh create mode 100644 tryboot.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..caa7c46 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +qemu-arm +extension +tryboot +tryboot.tcz +tryboot.tcz.list +tryboot.tcz.md5.txt diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5a0699e --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +SRC := tryboot.c +BIN := tryboot +EXTDIR := extension +TCZ := $(BIN).tcz +LIST := $(TCZ).list +MD5 := $(TCZ).md5.txt + +all: tryboot.tcz.list tryboot.tcz.md5.txt + + +$(BIN): $(SRC) + gcc $(STATIC) -I/usr/include -o $(BIN) $(SRC) + + +$(EXTDIR): $(BIN) + install -D $(BIN) $(EXTDIR)/usr/local/bin/$(BIN) + + +$(TCZ): $(EXTDIR) + mksquashfs $(EXTDIR) $(TCZ) -all-root -noappend + + +$(LIST): $(TCZ) + unsquashfs -lc $(TCZ) | sed 's|^squashfs-root/||' > $(LIST) + + +$(MD5): $(TCZ) + md5sum $(TCZ) > $(MD5) + + +clean: + rm -r $(BIN) $(EXTDIR) $(TCZ) $(LIST) $(MD5) diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..1fc9f3e --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,9 @@ +FROM --platform=linux/arm/v5 debian:stable + +# a static-user qemu binary that allows running the container on x86 +# (the binary is not included in this git repository, build or download your own) +COPY qemu-arm /usr/bin + +RUN apt-get update \ + && apt-get install -y build-essential squashfs-tools \ + && apt-get clean diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..f4953f4 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,3 @@ +docker build -t builder -f docker/Dockerfile . + +docker run --rm -it -v $PWD:/mnt -w /mnt -u $(id -u):$(id -g) builder make diff --git a/tryboot.c b/tryboot.c new file mode 100644 index 0000000..6d0188c --- /dev/null +++ b/tryboot.c @@ -0,0 +1,48 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + char *param = NULL; + int cmd; + + if(argc == 2) { + param = argv[1]; + cmd = LINUX_REBOOT_CMD_RESTART2; + } + else { + cmd = LINUX_REBOOT_CMD_RESTART; + } + + // prevent being killed when parent dies + int rc = daemon(0, 1); + if (rc == -1) { + perror("daemon() failed"); + return 1; + } + + // write current pid to the killall5 skip file + pid_t pid = getpid(); + + FILE *k5skip = fopen("/var/tmp/k5_skip", "a"); + fprintf(k5skip, "%d\n", pid); + fclose(k5skip); + + system("/etc/init.d/rc.shutdown"); + + sync(); + printf("Calling reboot syscall...\n"); + + // calling reboot() doesn't work, need to use syscall() + //int rc = reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, param); + //printf("After reboot: rc=%d, errno=%d\n", rc, errno); + syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, param); + + return 0; +}