From be6319ab89eaaceca644665fce59d8c83be53c3c Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Mon, 9 Jul 2018 16:59:13 +0100 Subject: [PATCH] move doc for inits and sanity checks to README --- README.adoc | 45 ++++++++++++++++++++++++++++-- kernel_module/user/hello.c | 2 ++ kernel_module/user/hello_cpp.cpp | 4 ++- kernel_module/user/poweroff.c | 4 +-- kernel_module/user/sleep_forever.c | 2 +- 5 files changed, 50 insertions(+), 7 deletions(-) diff --git a/README.adoc b/README.adoc index 34bf922..6b98b61 100644 --- a/README.adoc +++ b/README.adoc @@ -2323,7 +2323,7 @@ Remember that if your init returns, the kernel will panic, there are just two no * run forever in a loop or long sleep * `poweroff` the machine -==== The kernel panics despite poweroff +==== poweroff.out Just using BusyBox' `poweroff` at the end of the `init` does not work and the kernel panics: @@ -2341,7 +2341,27 @@ But this fails when we are `init` itself! ./run -E 'poweroff -f' .... -but why not just use your super simple and effective `/poweroff.out` and be done with it? +but why not just use our minimal `/poweroff.out` and be done with it? + +.... +./run -E '/poweroff.out' +.... + +Source: link:kernel_module/user/poweroff.c[] + +This also illustrates how to shutdown the computer from C: https://stackoverflow.com/questions/28812514/how-to-shutdown-linux-using-c-or-qt-without-call-to-system + +==== sleep_forever.out + +I dare you to guess what this does: + +.... +./run -E '/sleep_forever.out' +.... + +Source: link:kernel_module/user/sleep_forever.c[] + +This executable is a convenient simple init that does not panic and sleeps instead. [[init-busybox]] === Run command at the end of BusyBox init @@ -8711,6 +8731,27 @@ Then proceed to do the following tests: * `/count.sh` and `b __x64_sys_write` * `insmod /timer.ko` and `b lkmc_timer_callback` +===== Sanity checks + +Basic C and C++ hello worlds: + +.... +/hello.out +/hello_cpp.out +.... + +Output: + +.... +hello +hello cpp +.... + +Sources: + +* link:kernel_module/user/hello.c[] +* link:kernel_module/user/hello_cpp.c[] + === About This project is for people who want to learn and modify low level system components: diff --git a/kernel_module/user/hello.c b/kernel_module/user/hello.c index 7a63575..81f6976 100644 --- a/kernel_module/user/hello.c +++ b/kernel_module/user/hello.c @@ -1,3 +1,5 @@ +/* https://github.com/cirosantilli/linux-kernel-module-cheat#sanity-checks */ + #include #include diff --git a/kernel_module/user/hello_cpp.cpp b/kernel_module/user/hello_cpp.cpp index 52e3b2e..df8669e 100644 --- a/kernel_module/user/hello_cpp.cpp +++ b/kernel_module/user/hello_cpp.cpp @@ -1,5 +1,7 @@ +/* https://github.com/cirosantilli/linux-kernel-module-cheat#sanity-checks */ + #include int main() { - std::cout << "hello world" << std::endl; + std::cout << "hello cpp" << std::endl; } diff --git a/kernel_module/user/poweroff.c b/kernel_module/user/poweroff.c index 92f56dd..bfa9540 100644 --- a/kernel_module/user/poweroff.c +++ b/kernel_module/user/poweroff.c @@ -1,6 +1,4 @@ -/* Userspace is for the weak. Die. - * https://stackoverflow.com/questions/28812514/how-to-shutdown-linux-using-c-or-qt-without-call-to-system - **/ +/* https://github.com/cirosantilli/linux-kernel-module-cheat#poweroff-out */ #define _XOPEN_SOURCE 700 #include diff --git a/kernel_module/user/sleep_forever.c b/kernel_module/user/sleep_forever.c index 4725548..4676458 100644 --- a/kernel_module/user/sleep_forever.c +++ b/kernel_module/user/sleep_forever.c @@ -1,4 +1,4 @@ -/* Sleeping beauty, your prince is called Ctrl + C. */ +/* https://github.com/cirosantilli/linux-kernel-module-cheat#sleep_forever-out */ #include #include