123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #include "libbb.h"
- #include "reboot.h"
- #if ENABLE_FEATURE_WTMP
- #include <sys/utsname.h>
- static void write_wtmp(void)
- {
- struct utmpx utmp;
- struct utsname uts;
-
-
- memset(&utmp, 0, sizeof(utmp));
- utmp.ut_tv.tv_sec = time(NULL);
- strcpy(utmp.ut_user, "shutdown");
- utmp.ut_type = RUN_LVL;
- utmp.ut_id[0] = '~'; utmp.ut_id[1] = '~';
- utmp.ut_line[0] = '~'; utmp.ut_line[1] = '~';
- uname(&uts);
- safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host));
- updwtmpx(bb_path_wtmp_file, &utmp);
- }
- #else
- #define write_wtmp() ((void)0)
- #endif
- int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int halt_main(int argc UNUSED_PARAM, char **argv)
- {
- static const int magic[] = {
- RB_HALT_SYSTEM,
- RB_POWER_OFF,
- RB_AUTOBOOT
- };
- static const smallint signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
- int delay = 0;
- int which, flags, rc;
-
- if (ENABLE_HALT && !ENABLE_POWEROFF && !ENABLE_REBOOT)
- which = 0;
- else
- if (!ENABLE_HALT && ENABLE_POWEROFF && !ENABLE_REBOOT)
- which = 1;
- else
- if (!ENABLE_HALT && !ENABLE_POWEROFF && ENABLE_REBOOT)
- which = 2;
- else
- for (which = 0; "hpr"[which] != applet_name[0]; which++)
- continue;
-
-
- flags = getopt32(argv, "d:+nfwi", &delay);
- sleep(delay);
- write_wtmp();
- if (flags & 8)
- return EXIT_SUCCESS;
- if (!(flags & 2))
- sync();
-
- rc = 1;
- if (!(flags & 4)) {
- if (ENABLE_LINUXRC) {
-
-
- pid_t *pidlist = find_pid_by_name("linuxrc");
- if (pidlist[0] > 0)
- rc = kill(pidlist[0], signals[which]);
- if (ENABLE_FEATURE_CLEAN_UP)
- free(pidlist);
- }
- if (rc) {
-
- if (!ENABLE_FEATURE_CALL_TELINIT) {
-
- rc = kill(1, signals[which]);
- } else {
-
-
- execlp(CONFIG_TELINIT_PATH,
- CONFIG_TELINIT_PATH,
- which == 2 ? "6" : "0",
- (char *)NULL
- );
- bb_perror_msg_and_die("can't execute '%s'",
- CONFIG_TELINIT_PATH);
- }
- }
- } else {
- rc = reboot(magic[which]);
- }
- if (rc)
- bb_perror_nomsg_and_die();
- return rc;
- }
|