reboot linux from a program

2019-06-20 17:19发布

问题:

This question has been asked before: How to restart Linux from inside a C++ program?

However, none of the answers seem to work for me.

Here's my problem:

I have a program on a piece of hardware. I can log in as root and run it without any issues. However, if I login as a different user with root priveleges, the program executes fine but does not reboot as it should. It only shuts down the current Telnet session.

To reboot, I've used system(reboot). So far I have tried:

sync();
reboot(RB_POWER_OFF);

And

execl("/sbin/reboot","reboot",NULL,NULL);

to no avail.

(I'm not sure, if I've used execl correctly. It is the first time I use it. Please correct me if I messed up).

Any help would be appreciated.

Edit:

/data/local/sbin # cat /etc/passwd
root:x:0:0:root:/root:/bin/sh
service:54zljpSAe:0:0:root:/root:/data/local/sbin/script

if I login as service, "script" will be run. The user has a list options to choose from. One of the options is to run the above mentioned program.

Edit:

Also worth mentioning: One of the options in the script is to reboot the system. If called from inside the script, it works. But I would like to call reboot from the program.

回答1:

first of all, login to your system as "a different user with root priveleges", and execute 'init 6', if system had been restarted ?

yes: use system("init 6"); in your program.

no: you have security issue, first you have to fix that.



标签: c linux reboot