Results 1 to 2 of 2
While in single user mode (run level S) I run the command "/sbin/reboot". Because I'm not at run-level 0 or 6 it execs "/sbin/shutdown -r now", but before that exec ...
- 08-12-2010 #1Just Joined!
- Join Date
- Aug 2010
- Posts
- 2
reboot command creates /halt
While in single user mode (run level S) I run the command "/sbin/reboot". Because I'm not at run-level 0 or 6 it execs "/sbin/shutdown -r now", but before that exec occurs the file "/halt" gets created.
I discovered this by replacing the "/sbin/shutdown" binary with a copy of "/bin/ls" and then invoking "/sbin/reboot" . First I made sure "/halt" didn't exist and afterwards looked and it was now there again.
I examined the code for reboot (halt.c -- also used for /sbin/halt and /sbin/poweroff via links). By invoking reboot with no arguments and at run level S there is almost no code, and only a couple innocuous function calls before the exec of /sbin/shutdown.
How did "/halt" get created?
I stumbled onto this because some users were complaining that after doing a few things in single user mode they issued the reboot command to restart the system. The reboot always works, but when they subsequently try to shutdown the system using "init 0", which should power off the hardware, it only does a halt and leaves the hardware powered on. The only way I know to do this is to create "/halt".
To deepen the mystery, I found that as the system is booting, several files, including "/halt", are always removed. See /etc/rc.sysinit. The user has no way to manually create the "/halt" file (no shells, only very restricted GUIs, that's why these users are doing admin stuff in single user to start with.)
So where does that "/halt" file come from?
Right now it looks like I'll have to change the /etc/init.d/halt script to ignore "/halt" so it always does a poweroff -- which is what we always want.
Our Linux version is RHEL4,
And INIT_VERSION is sysvinit-2.85
Here's a snippet of code from halt.c. As you can see there aint much there.
void do_shutdown(char *fl, char *tm)
{
char *args[8];
int i = 0;
args[i++] = "shutdown";
args[i++] = fl;
if (tm) {
args[i++] = "-t";
args[i++] = tm;
}
args[i++] = "now";
args[i++] = NULL;
execv("/sbin/shutdown", args);
execv("/etc/shutdown", args);
execv("/bin/shutdown", args);
perror("shutdown");
exit(1);
}
/*
* Main program.
* Write a wtmp entry and reboot cq. halt.
*/
int main(int argc, char **argv)
{
int do_reboot = 0;
int do_sync = 1;
int do_wtmp = 1;
int do_nothing = 0;
int do_hard = 0;
int do_ifdown = 0;
int do_hddown = 0;
int do_poweroff = 0;
int c;
char *tm = NULL;
/*
* Find out who we are
*/
if ((progname = strrchr(argv[0], '/')) != NULL)
progname++;
else
progname = argv[0];
if (geteuid() != 0) {
fprintf(stderr, "%s: must be superuser.\n", progname);
exit(1);
}
if (!strcmp(progname, "reboot")) do_reboot = 1;
if (!strcmp(progname, "poweroff")) do_poweroff = 1;
/*
* Get flags
*/
while((c = getopt(argc, argv, ":ihdfnpwt:")) != EOF) {
switch(c) {
case 'n':
do_sync = 0;
do_wtmp = 0;
break;
case 'w':
do_nothing = 1;
break;
case 'd':
do_wtmp = 0;
break;
case 'f':
do_hard = 1;
break;
case 'i':
do_ifdown = 1;
break;
case 'h':
do_hddown = 1;
break;
case 'p':
do_poweroff = 1;
break;
case 't':
tm = optarg;
break;
default:
usage();
}
}
if (argc != optind) usage();
(void)chdir("/");
if (!do_hard && !do_nothing) {
/*
* See if we are in runlevel 0 or 6.
*/
c = get_runlevel();
if (c != '0' && c != '6')
do_shutdown(do_reboot ? "-r" : "-h", tm);
}
- 08-13-2010 #2Just Joined!
- Join Date
- Aug 2010
- Posts
- 2
Mystery solved. /sbin/reboot really does create /halt. An strace pointed out that it was calling open() to create the file. And finding the most current patched code for halt.c had the open() call in plain sight.
Now onto the original mystery of why, sometimes, when the user requests a shutdown of the system via GUI menu, which runs "init 0", does the system halt instead of powering off.
The only thing I can find that causes that is the presence of the /halt file.
And that's why I was trying to figure out whether reboot was creating the /halt file. But that really doesn't matter, because /halt is ALWAYS removed when the system boots.


Reply With Quote