2 * Copyright 2002-2010, Haiku.
3 * Distributed under the terms of the MIT License.
6 * Francois Revol (mmu_man@users.sf.net)
10 /*! Shuts down the system, either halting or rebooting. */
17 #include <RosterPrivate.h>
27 uint32 gTimeToSleep
= 0;
31 /*! We get here when shutdown is cancelled.
42 parseTime(char *arg
, char *argv
, int32
*_i
)
46 if (isdigit(arg
[0])) {
47 gTimeToSleep
= strtoul(arg
, &unit
, 10);
48 } else if (argv
&& isdigit(argv
[0])) {
50 gTimeToSleep
= strtoul(argv
, &unit
, 10);
54 if (unit
[0] == '\0' || strcmp(unit
, "s") == 0)
56 if (strcmp(unit
, "m") == 0) {
66 usage(const char *program
)
68 fprintf(stderr
, "usage: %s [-rqca] [-d time]\n"
70 "\t-q quick shutdown (don't broadcast apps),\n"
71 "\t-a ask user to confirm the shutdown (ignored when -q is given),\n"
72 "\t-c cancel a running shutdown,\n"
73 "\t-s run shutdown synchronously (only returns if shutdown is cancelled)\n"
74 "\t-d delay shutdown by <time> seconds.\n", program
);
80 main(int argc
, char **argv
)
86 const char *program
= strrchr(argv
[0], '/');
92 // handle 'halt' and 'reboot' symlinks
93 if (strcmp(program
, "reboot") == 0)
95 if (strcmp(program
, "shutdown") != 0)
98 for (int32 i
= 1; i
< argc
; i
++) {
101 if (!isalpha(arg
[1]))
104 while (arg
&& isalpha((++arg
)[0])) {
120 // find all running shutdown commands and signal their
123 thread_info threadInfo
;
124 get_thread_info(find_thread(NULL
), &threadInfo
);
126 team_id thisTeam
= threadInfo
.team
;
128 int32 team_cookie
= 0;
130 while (get_next_team_info(&team_cookie
, &teamInfo
)
132 if (strstr(teamInfo
.args
, "shutdown") != NULL
133 && teamInfo
.team
!= thisTeam
) {
134 int32 thread_cookie
= 0;
135 while (get_next_thread_info(teamInfo
.team
,
136 &thread_cookie
, &threadInfo
) == B_OK
) {
137 if (strcmp(threadInfo
.name
, "shutdown") == 0)
138 kill(threadInfo
.thread
, SIGUSR1
);
146 if (parseTime(arg
+ 1, argv
[i
+ 1], &i
)) {
150 // supposed to fall through
160 if (gTimeToSleep
> 0) {
163 signal(SIGUSR1
, handle_usr1
);
165 printf("Delaying %s by %lu seconds...\n",
166 gReboot
? "reboot" : "shutdown", gTimeToSleep
);
168 left
= sleep(gTimeToSleep
);
171 fprintf(stderr
, "Shutdown cancelled.\n");
178 _kern_shutdown(gReboot
);
180 fprintf(stderr
, "Shutdown failed! (Do you have ACPI enabled?)\n");
184 BRoster::Private
rosterPrivate(roster
);
185 status_t error
= rosterPrivate
.ShutDown(gReboot
, askUser
, !async
);
187 fprintf(stderr
, "Shutdown failed: %s\n", strerror(error
));