11

Say I am writing my own init program running on a Linux kernel.

What happens when my init program exits with return value 0 ?

Additionally is the behaviour different if the return value is non-zero?

1 Answers1

7
What happens when my init program exits with return value 0?

This code, from the find_child_reaper function in kernel/exit.c, is run:

panic("Attempted to kill init! exitcode=0x%08x\n",
       father->signal->group_exit_code ?: father->exit_code);

And consequently this message appears on your console:

Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000000
JdeBP
  • 68,745
  • Thank you! I misunderstood the message, and was thinking this is the kernel panic you will get if you try to send a kill signal to init, not the action performed if init just returns. – Bjarke Freund-Hansen Apr 28 '15 at 09:13
  • 2
    @BjarkeFreund-Hansen There is no such thing as "just returns" for a process. When you return from main in a C program, you return to a stub function in the C library that then invokes a system call to end the process, just as if you called exit(). – DepressedDaniel Mar 21 '17 at 22:54
  • 1
    @DepressedDaniel: Thank you, that broadens my understanding. :) – Bjarke Freund-Hansen Mar 29 '17 at 08:04
  • 1
    Wow! The crap you learn just passing through is amazing. – Bruno Bronosky Mar 05 '18 at 18:57
  • 1
    If someone is wondering what the ?: means, look here: https://stackoverflow.com/a/3319144. In summary: the syntax x ?: y is a nonstandard GCC extension and is equivalent to x ? x : y. – jp48 Aug 06 '19 at 20:13