简体   繁体   English

在C中安全重启Linux

[英]Safe reboot linux in C

how can i made a safe reboot like shell command reboot from C without exec? 我如何在没有exec的情况下从C重新启动像shell命令一样安全的重新reboot The reboot function in reboot.h is not safe. reboot.h中的重新启动功能不安全。 It make no sync and probaly no unmount und safe process termination. 它不进行同步,也可能不会进行卸载和安全的过程终止。 What function have the magic parameter? 哪些功能具有magic参数?

Bet regards 投注问候

The simplest way: 最简单的方法:

system('reboot')

Otherwise, you have Linux: Programatically shutdown or reboot computer from a user-level process 否则,您将拥有Linux:以编程方式从用户级进程关闭或重新启动计算机

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 ). 例如,Android仅强制文件系统挂载为只读(通过 sysrq-trigger 发出 “ u”命令)。 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. 您需要停止主init循环,因为您不希望init重新启动将在下一步开始杀死的进程。
  • Issue "stop" signal to all processes to let them finish their actions 向所有流程发出“停止”信号,以使它们完成操作
  • If "stop" takes too long time, issue "kill" signal to all processes. 如果“停止”时间太长,则向所有进程发出“ kill”信号。 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 . 您可以使用killumountreboot等调用从C代码执行上述所有步骤。

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 . 如前所述,就干净关机而言,Android并不是最佳示例,但是您可以在此处查看示例C代码关机实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM