user3612610
user3612610

Reputation: 252

Safe reboot linux in C

how can i made a safe reboot like shell command reboot from C without exec? The reboot function in reboot.h is not safe. It make no sync and probaly no unmount und safe process termination. What function have the magic parameter?

Bet regards

Upvotes: 0

Views: 3147

Answers (2)

mkmk88
mkmk88

Reputation: 271

In fact, there are systems in which reboot is done without properly unmounting partitions, causing filesystem errors. For instance Android is only forcing filesystem to mount to read-only (by issuing "u" command to sysrq-trigger). If you're not focused on performance and you rather want system to be shutdown cleanly, not quickly, then you need to perform following steps:

  • Stop main init loop. There is no single method to do this and it depends on what exactly init implementation your system is using. You need to stop the main init loop because you don't want init to restart processes that you will start killing in next step.
  • Issue "stop" signal to all processes to let them finish their actions
  • If "stop" takes too long time, issue "kill" signal to all processes. You don't want to have processes with open files before unmounting.
  • Unmount all partitions to read-only
  • Ask kernel to shutdown the machine by issuing reboot standard call.

All above steps you can do from C-code using calls like kill, umount, reboot.

As I said before, Android is not a best example in terms of clean shutdown, but you can have a look on example C-code shutdown implementation here.

Upvotes: 1

A. Gille
A. Gille

Reputation: 1048

The simplest way:

system('reboot')

Otherwise, you have Linux: Programatically shutdown or reboot computer from a user-level process

Upvotes: 1

Related Questions