I have a small cpp application which will reboot the system. This works very well so far.
sync(); //need for data safety
reboot(RB_AUTOBOOT);
Unless you are connected via SSH and run this program on the connected device. Then the SSH connection hangs.
If you are connected via SSH and use the CLI commands
sudo reboot
or
sudo shutdown -r now
The SSH connection will be terminated with the following message
Connection to xxx.xxx.xxx.xxx closed by remote host.
Connection to xxx.xxx.xxx.xxx closed.
How do I get the same behaviour with the cpp reboot method?
I read https://man7.org/linux/man-pages/man2/reboot.2.html and search the internet, but didn't found something about this topic.
The sudo reboot
command will notify init
that you want a reboot, init will kill all user-space processes and then do the actual kernel reboot.
The reboot
syscall does the equivalent of sudo reboot -f
(immediate reboot).
You could try to kill everything yourself and then invoke the syscall.
Or you could ask init, which by convention lives at PID=1. Reboot is signalled by sending it a SIGINT.
So:
#include <sys/types.h>
#include <signal.h>
int main() {
kill(1, SIGINT);
}
should do it.