5

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.

Community
  • 1
  • 1
H_squared
  • 1,251
  • 2
  • 15
  • 32
  • 1
    You gave `execl()` a relative path. THis only works if your working dir is `/`, which it probably isn't. Maybe try `/sbin/reboot`? – glglgl Feb 12 '14 at 09:10
  • 2
    what is a different user with root privilegies? I think that's false. However the correct answer for this is to setuid your program to root. So that the program always runs with root privilegies. Please be aware of the security issues with this. – iveqy Feb 12 '14 at 09:11
  • @glglgl Sorry that was a typo. Corrected – H_squared Feb 12 '14 at 09:13
  • 1
    @iveqy As the OP uses telnet(!), there can be no security issues at all. – glglgl Feb 12 '14 at 09:14
  • @iveqy I provided more information. Check my edits – H_squared Feb 12 '14 at 09:20
  • `execve` did not help. The outcome was the same as with `execl`. – H_squared Feb 12 '14 at 09:26
  • have you tried `reboot(RB_AUTOBOOT)` or `reboot(LINUX_REBOOT_CMD_RESTART);` ? – Jayesh Bhoi Feb 12 '14 at 09:36
  • @JKB No go for both. Same thing happens, telnet session shuts down and nothing happens. Also with all exec(), errno does not get set, i.e. perror() does not print anything! – H_squared Feb 12 '14 at 09:43
  • A normal user cannot reboot the machine. Your program needs to be run as root. (and not just run by a user with root privileges - whatever that means). – nos Feb 12 '14 at 09:53
  • @nos Correct. But if you look at my last edit, you can see that a service user can reboot the system from inside the script. – H_squared Feb 12 '14 at 09:59
  • What return value do you get from reboot( ) ? – harmic Feb 12 '14 at 09:59
  • @harmic if I add `printf("%d\n",reboot(RB_POWER_OFF));printf("done");`, it only prints "done". So apparantly the reboot call is being ignored. – H_squared Feb 12 '14 at 10:07
  • That doesn't make sense. The whole printf is being "ignored"? The only possibility would be for it not to return from reboot, but then it would not print done either. Might be worth trying to print it to a file in case the reason it is not being printed is to do with the telnet session closing – harmic Feb 12 '14 at 10:12
  • we've decided to call reboot again from the script and it has been working so far. That will have to do for now since we need to deliver a version of the program by the end of the week. However, this issue still puzzles me. I will post, once I find out what the problem is. Thanks for the help. – H_squared Feb 12 '14 at 10:22
  • @hhachem Your service user IS root. It has uid = 0, that's the definition of root, the username is irrellevant. – nos Feb 12 '14 at 10:24
  • @nos exactly, that's what I meant with "root priveleges". – H_squared Feb 12 '14 at 11:03
  • Try to run "sleep 2 && shutdown -r 0 &". It will schedule a reboot in 2 seconds, and allow your program to finish normally before it gets (possibly) killed, and your telnet session will also stay on for those 2 seconds. I do this on linux VMs sending this rpc: /bin/bash "/bin/sleep 1 && /sbin/shutdown -r 0 &". This is called from a PHP script, so I need to delay shutdown scheduling until the whole command goes on background, then schedule the shutdown immediately. – DNT Feb 12 '14 at 11:42

1 Answers1

2

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.

wiesniak
  • 558
  • 2
  • 11