3

I'm writing a custom reboot program in C and trying to decide if I should use reboot(2) directly or call system("/sbin/reboot").

Both reboot(8) and init 6 change the runlevel and gracefully shut down services, then unmount all filesystems. But, reboot(2) does neither of these things.

When should reboot(2) be used in preference to reboot(8)?

(I know from the man page to call sync(2) before reboot(2).)

Cuadue
  • 151
  • I recommend you read carefully man page of reboot, halt, shutdown, init and poweroff. – PersianGulf May 11 '15 at 22:24
  • However i recommend don't invent wheel – PersianGulf May 11 '15 at 22:25
  • I recommend not thinking in terms of run levels. If you are learning this stuff on a systemd operating system, forget all about run levels and don't begin learning from there in the first place. Your world comprises targets, not run levels. See http://unix.stackexchange.com/a/196014/5132 . – JdeBP May 11 '15 at 23:28
  • 4
    If you print out your question, hold it up to the light, and squint, you may find that the answer is contained within.  Do you want to reboot without gracefully shutting down services and unmounting all filesystems?  Use reboot(2).  Do you want to shut down services and unmount filesystems in a special way?  Use reboot(2).  Do you want the system to gracefully shut down services and unmount all filesystems for you?  Use reboot(8). – G-Man Says 'Reinstate Monica' May 12 '15 at 01:46

3 Answers3

3

If you want to restart the system now, don't do anything else, don't care about other running processes, don't care about any open files, then call the reboot system call (i.e. reboot(2)). It would be rare to not even call sync beforehand.

If you want to restart the system normally, leaving processes some time to exit cleanly, unmounting filesystems, etc. then call the reboot utility (i.e. reboot(8)).

If you need to ask, the reboot(8) is the right one. The reboot system call is pretty much there only because the reboot utility needs to have some way to say “yes, I've finished, the system is all but halted, now reset the processor”.

0

You should be aware that reboot(8) can be made to immediately reboot (skipping all interaction with init) by running it as reboot -f. This is how init actually takes down the system after being put in runlevel 6. The reboot(2) call is just how reboot(8) interacts with the kernel to tell it to physically reboot (or shut down) the system. It also does other things such as put the init process in control of reacting to the ctrl-alt-delete keystroke.

In general, the only reason you would want to do reboot/halt/poweroff -f as a user is if the init system is broken, for example if you are doing manual system recovery in single user mode (e.g. having booted with the kernel command line emergency or init=/bin/sh). And of course, you need to run sync(8) before doing this.

Many inits do provide an emergency or rescue mode that does not require this, instead you can simply use the normal shutdown/reboot tools, or exit the shell. The -f modes are in general only to be used by ordinary users as a last resort. Although, certainly, if you've bypassed init entirely with init=/bin/sh, the ordinary shutdown system isn't likely to do anything useful. And if you exit the shell in this case it will cause a kernel panic.

Random832
  • 10,666
  • Beware that this answer is dated. On systemd operating systems, single user mode has gone, as has the notion of being "in" run levels, and there is a subtle difference between the systemd emergency mode and rescue mode. However, neither needs the -f option, as ordinary reboot, shutdown, halt, and poweroff will still work in those modes because the init system is not broken. See http://unix.stackexchange.com/a/196471/5132 and http://unix.stackexchange.com/a/196014/5132 for more on how the world has changed. – JdeBP May 11 '15 at 23:51
  • A) Not all distros use systemd and B) it's certainly still possible for the init system to be broken; if nothing else it could be so badly broken you need init=/bin/sh. Anyone who knows enough to reasonably recover a system like this is probably able to judge for themselves how they should bring down the system. But I'll add an edit acknowledging this. – Random832 May 11 '15 at 23:53
  • If I'd wanted to imply that all distributions use systemd, I wouldn't have used the qualification "systemd operating systems". Similarly, if I'd wanted to say that init systems cannot be broken, I'd have written that instead of writing what I did write which was that in emergency and rescue modes systemd is not. Please read properly. That said, you'll notice from those answers and the manual pages that it's not solely systemd operating systems where the world has changed. The received wisdom about all of this stuff simply isn't true for a lot of operating systems (even BSDs) any more. – JdeBP May 12 '15 at 05:39
-1

reboot(2) is an API, you use it if you're implementing reboot(8) or an init system.

Learn more by reading the man page.

spuk
  • 368
  • 3
    This answer is technically correct but utterly devoid of content. The asker is evidently aware of the existence of man pages. reboot(2) is an API (two APIs, in fact: it's both a system call and a C API); so it reboot(8) (it's a shell API). – Gilles 'SO- stop being evil' May 12 '15 at 01:00
  • The asker is clearly confused, and/or didn't read enough docs. He's implementing a reboot command in C, thus he should use reboot(2) and read more on it to understand.

    If one is implementing a reboot in C and in doubt if should just call system("/sbin/reboot");, maybe one should write a shellscript instead. Or one needs better understanding of how the init system works.

    I agree with @G-Man: the answer is actually contained within the question.

    Sorry for the terseness anyway.

    – spuk May 12 '15 at 17:55