Shutting down Linux from a C program designed to run as an init process

I write a C program to do various things on a Linux system, and then close it. This program will be launched with the command line option init=/path/to/programwith PID 1, and because of this I cannot use execl("/sbin/poweroff", "poweroff", NULL);it because the command poweroffdoes not turn off the system itself, it requests the process using PID 1 to do this. So, what code initdoes it use to shut down the system?

+4
source share
1 answer

Why doesn’t work poweroff?

, ​​ init - 1. init systemd; , poweroff systemctl.

systemd , init=/path/to/program. poweroff : systemctl systemd, .

systemd.

reboot Linux. glibc RB_POWER_OFF .

, reboot sync, .

glibc Linux:

#include <unistd.h>
#include <sys/reboot.h>

sync();
reboot(RB_POWER_OFF);

.

Linux ++?

+5

Source: https://habr.com/ru/post/1654057/


All Articles