Magellan Linux

Contents of /tags/mkinitrd-6_3_3/klibc/usr/utils/halt.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1182 - (show annotations) (download)
Wed Dec 15 21:43:57 2010 UTC (13 years, 6 months ago) by niro
File MIME type: text/plain
File size: 1162 byte(s)
tagged 'mkinitrd-6_3_3'
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <signal.h>
4 #include <sys/reboot.h>
5 #include <klibc/compiler.h>
6
7 static __noreturn usage(void)
8 {
9 static char mesg[] = "Usage: {halt|reboot|poweroff} [-n]\n";
10 write(2, mesg, sizeof(mesg) - 1);
11 exit(1);
12 }
13
14 int main(int argc, char *argv[])
15 {
16 int cmd = 0; /* initalize to shut gcc up */
17 int do_sync = 1;
18 char *ptr, *ptr2;
19
20 /* Which action (program name)? */
21 ptr2 = ptr = argv[0];
22 while (*ptr2)
23 if (*ptr2++ == '/')
24 ptr = ptr2;
25 if (*ptr == 'r')
26 cmd = LINUX_REBOOT_CMD_RESTART;
27 else if (*ptr == 'h')
28 cmd = LINUX_REBOOT_CMD_HALT;
29 else if (*ptr == 'p')
30 cmd = LINUX_REBOOT_CMD_POWER_OFF;
31 else
32 usage();
33
34 /* Walk options */
35 while (*++argv && **argv == '-')
36 switch (*++*argv) {
37 case 'f':
38 break; /* -f assumed */
39 case 'n':
40 do_sync = 0;
41 break;
42 default:
43 usage();
44 }
45 if (*argv)
46 usage(); /* any args == error */
47
48 if (do_sync)
49 sync();
50 reboot(LINUX_REBOOT_CMD_CAD_OFF); /* Enable CTRL+ALT+DEL */
51 if (!reboot(cmd)) {
52 /* Success. Currently, CMD_HALT returns, so stop the world */
53 /* kill(-1, SIGSTOP); */
54 kill(getpid(), SIGSTOP);
55 }
56 write(2, "failed.\n", 8);
57 return 1;
58 }