2005-04-21 16:53:53 +02:00
|
|
|
/* halt / reboot - halt or reboot system (depends on name)
|
|
|
|
|
|
|
|
halt - calling reboot() with RBT_HALT
|
|
|
|
reboot - calling reboot() with RBT_REBOOT
|
|
|
|
|
|
|
|
author: Edvard Tuinder v892231@si.hhs.NL
|
|
|
|
|
|
|
|
This program calls the library function reboot(2) which performs
|
|
|
|
the system-call do_reboot.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _POSIX_SOURCE 1
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2011-11-09 16:34:47 +01:00
|
|
|
#include "wtmp.h"
|
|
|
|
|
2012-03-25 20:38:37 +02:00
|
|
|
void write_log( char *fn );
|
|
|
|
void usage( void );
|
|
|
|
int main( int argc, char *argv[] );
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
char *prog;
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
2012-11-22 17:30:22 +01:00
|
|
|
fprintf(stderr, "Usage: %s [-hrRfpd]\n", prog);
|
2005-04-21 16:53:53 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(argc,argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
int flag = -1; /* default action unknown */
|
|
|
|
int fast = 0; /* fast halt/reboot, don't bother being nice. */
|
|
|
|
int i;
|
|
|
|
struct stat dummy;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if ((prog = strrchr(argv[0],'/')) == NULL) prog = argv[0]; else prog++;
|
|
|
|
|
|
|
|
if (strcmp(prog, "halt") == 0) flag = RBT_HALT;
|
|
|
|
if (strcmp(prog, "reboot") == 0) flag = RBT_REBOOT;
|
|
|
|
|
|
|
|
i = 1;
|
|
|
|
while (i < argc && argv[i][0] == '-') {
|
|
|
|
char *opt = argv[i++] + 1;
|
|
|
|
|
|
|
|
if (*opt == '-' && opt[1] == 0) break; /* -- */
|
|
|
|
|
|
|
|
while (*opt != 0) switch (*opt++) {
|
2006-03-13 11:33:50 +01:00
|
|
|
case 'h': flag = RBT_HALT; break;
|
|
|
|
case 'r': flag = RBT_REBOOT; break;
|
|
|
|
case 'R': flag = RBT_RESET; break;
|
2010-08-30 21:01:58 +02:00
|
|
|
case 'd': flag = RBT_DEFAULT; break;
|
2012-11-21 20:28:37 +01:00
|
|
|
case 'p': flag = RBT_POWEROFF; break;
|
2006-03-13 11:33:50 +01:00
|
|
|
case 'f': fast = 1; break;
|
2005-04-21 16:53:53 +02:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != argc) usage();
|
|
|
|
|
|
|
|
if (flag == -1) {
|
|
|
|
fprintf(stderr, "Don't know what to do when named '%s'\n", prog);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat("/usr/bin", &dummy) < 0) {
|
|
|
|
/* It seems that /usr isn't present, let's assume "-f." */
|
|
|
|
fast = 1;
|
|
|
|
}
|
|
|
|
|
2005-10-12 17:10:14 +02:00
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
signal(SIGTERM, SIG_IGN);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2005-10-12 17:10:14 +02:00
|
|
|
/* Skip this part for fast shut down. */
|
|
|
|
if (! fast) {
|
|
|
|
/* Run the shutdown scripts. */
|
2005-04-21 16:53:53 +02:00
|
|
|
switch ((pid = fork())) {
|
|
|
|
case -1:
|
|
|
|
fprintf(stderr, "%s: can't fork(): %s\n", prog, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
case 0:
|
|
|
|
execl("/bin/sh", "sh", "/etc/rc", "down", (char *) NULL);
|
|
|
|
fprintf(stderr, "%s: can't execute: /bin/sh: %s\n",
|
|
|
|
prog, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
default:
|
|
|
|
while (waitpid(pid, NULL, 0) != pid) {}
|
|
|
|
}
|
2005-10-12 17:10:14 +02:00
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2005-10-12 17:10:14 +02:00
|
|
|
/* Tell init to stop spawning getty's. */
|
|
|
|
kill(1, SIGTERM);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2010-06-24 16:56:03 +02:00
|
|
|
/* Extra sync for the case where SIGTERM causes deadlock */
|
|
|
|
sync();
|
|
|
|
|
2005-10-12 17:10:14 +02:00
|
|
|
/* Give everybody a chance to die peacefully. */
|
|
|
|
printf("Sending SIGTERM to all processes ...\n");
|
|
|
|
kill(-1, SIGTERM);
|
2006-03-13 11:33:50 +01:00
|
|
|
sleep(1);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2011-11-09 16:34:47 +01:00
|
|
|
write_log(STR_WTMP);
|
|
|
|
write_log(STR_ROOT_WTMP);
|
2006-04-03 16:50:07 +02:00
|
|
|
|
2005-10-05 17:07:08 +02:00
|
|
|
sync();
|
|
|
|
|
2012-11-22 17:30:22 +01:00
|
|
|
reboot(flag);
|
2007-07-02 13:16:27 +02:00
|
|
|
fprintf(stderr, "%s: reboot(): %s\n", prog, strerror(errno));
|
2005-04-21 16:53:53 +02:00
|
|
|
return 1;
|
|
|
|
}
|