简体   繁体   English

从程序重启linux

[英]reboot linux from a program

This question has been asked before: How to restart Linux from inside a C++ program? 之前已经问过这个问题: 如何从C ++程序中重启Linux?

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. 我可以以root用户身份登录并运行它而不会出现任何问题。 However, if I login as a different user with root priveleges, the program executes fine but does not reboot as it should. 但是,如果我以root priveleges身份登录为其他用户,程序执行正常,但不会重新启动。 It only shuts down the current Telnet session. 它只关闭当前的Telnet会话。

To reboot, I've used system(reboot) . 要重新启动,我使用了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). (我不确定,如果我正确使用了execl 。这是我第一次使用它。如果我搞砸了,请纠正我)。

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. 如果我以服务身份登录,将运行“script”。 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. 但我想从程序中调用reboot。

first of all, login to your system as "a different user with root priveleges" , and execute 'init 6', if system had been restarted ? 首先,以“root priveleges的不同用户”身份登录系统,如果系统重启,则执行'init 6'?

yes: use system("init 6"); 是的:使用系统(“init 6”); in your program. 在你的程序中。

no: you have security issue, first you have to fix that. 不:你有安全问题,首先你必须解决这个问题。

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

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