diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-09-11 09:54:23 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-09-11 09:54:23 +0000 |
| commit | 9725daa03a7806b1c9c5a3c511dfe2ff9f97dd26 (patch) | |
| tree | 8086e36b8ba88db3d2bedfc2a13c1f57b0aa1f31 /init | |
| parent | 8d89bed8401bfbca9c5ef18f201439b3502e733b (diff) | |
| download | busybox-w32-9725daa03a7806b1c9c5a3c511dfe2ff9f97dd26.tar.gz busybox-w32-9725daa03a7806b1c9c5a3c511dfe2ff9f97dd26.tar.bz2 busybox-w32-9725daa03a7806b1c9c5a3c511dfe2ff9f97dd26.zip | |
halt: reinstate -w even if !FEATURE_WTMP; beautify code in halt.c
Diffstat (limited to 'init')
| -rw-r--r-- | init/halt.c | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/init/halt.c b/init/halt.c index 42b9edc08..cbb325eb9 100644 --- a/init/halt.c +++ b/init/halt.c | |||
| @@ -13,60 +13,66 @@ | |||
| 13 | #if ENABLE_FEATURE_WTMP | 13 | #if ENABLE_FEATURE_WTMP |
| 14 | #include <sys/utsname.h> | 14 | #include <sys/utsname.h> |
| 15 | #include <utmp.h> | 15 | #include <utmp.h> |
| 16 | |||
| 17 | static void write_wtmp(void) | ||
| 18 | { | ||
| 19 | struct utmp utmp; | ||
| 20 | struct utsname uts; | ||
| 21 | if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) { | ||
| 22 | close(creat(bb_path_wtmp_file, 0664)); | ||
| 23 | } | ||
| 24 | memset(&utmp, 0, sizeof(utmp)); | ||
| 25 | utmp.ut_tv.tv_sec = time(NULL); | ||
| 26 | safe_strncpy(utmp.ut_user, "shutdown", UT_NAMESIZE); | ||
| 27 | utmp.ut_type = RUN_LVL; | ||
| 28 | safe_strncpy(utmp.ut_id, "~~", sizeof(utmp.ut_id)); | ||
| 29 | safe_strncpy(utmp.ut_line, "~~", UT_LINESIZE); | ||
| 30 | if (uname(&uts) == 0) | ||
| 31 | safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host)); | ||
| 32 | updwtmp(bb_path_wtmp_file, &utmp); | ||
| 33 | |||
| 34 | } | ||
| 35 | #else | ||
| 36 | #define write_wtmp() ((void)0) | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #ifndef RB_HALT_SYSTEM | ||
| 40 | #define RB_HALT_SYSTEM RB_HALT | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #ifndef RB_POWER_OFF | ||
| 44 | #define RB_POWER_OFF RB_POWERDOWN | ||
| 16 | #endif | 45 | #endif |
| 17 | 46 | ||
| 18 | int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 47 | int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 19 | int halt_main(int argc UNUSED_PARAM, char **argv) | 48 | int halt_main(int argc UNUSED_PARAM, char **argv) |
| 20 | { | 49 | { |
| 21 | static const int magic[] = { | 50 | static const int magic[] = { |
| 22 | #ifdef RB_HALT_SYSTEM | ||
| 23 | RB_HALT_SYSTEM, | 51 | RB_HALT_SYSTEM, |
| 24 | #elif defined RB_HALT | ||
| 25 | RB_HALT, | ||
| 26 | #endif | ||
| 27 | #ifdef RB_POWER_OFF | ||
| 28 | RB_POWER_OFF, | 52 | RB_POWER_OFF, |
| 29 | #elif defined RB_POWERDOWN | 53 | RB_AUTOBOOT |
| 30 | RB_POWERDOWN, | ||
| 31 | #endif | ||
| 32 | RB_AUTOBOOT | ||
| 33 | }; | 54 | }; |
| 34 | static const smallint signals[] = { SIGUSR1, SIGUSR2, SIGTERM }; | 55 | static const smallint signals[] = { SIGUSR1, SIGUSR2, SIGTERM }; |
| 35 | 56 | ||
| 36 | int delay = 0; | 57 | int delay = 0; |
| 37 | int which, flags, rc; | 58 | int which, flags, rc; |
| 38 | #if ENABLE_FEATURE_WTMP | ||
| 39 | struct utmp utmp; | ||
| 40 | struct utsname uts; | ||
| 41 | #endif | ||
| 42 | 59 | ||
| 43 | /* Figure out which applet we're running */ | 60 | /* Figure out which applet we're running */ |
| 44 | for (which = 0; "hpr"[which] != *applet_name; which++) | 61 | for (which = 0; "hpr"[which] != applet_name[0]; which++) |
| 45 | continue; | 62 | continue; |
| 46 | 63 | ||
| 47 | /* Parse and handle arguments */ | 64 | /* Parse and handle arguments */ |
| 48 | opt_complementary = "d+"; /* -d N */ | 65 | opt_complementary = "d+"; /* -d N */ |
| 49 | flags = getopt32(argv, "d:nf" USE_FEATURE_WTMP("w"), &delay); | 66 | /* We support -w even if !ENABLE_FEATURE_WTMP, in order |
| 67 | * to not break scripts */ | ||
| 68 | flags = getopt32(argv, "d:nfw", &delay); | ||
| 50 | 69 | ||
| 51 | sleep(delay); | 70 | sleep(delay); |
| 52 | 71 | ||
| 53 | #if ENABLE_FEATURE_WTMP | 72 | write_wtmp(); |
| 54 | if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) { | ||
| 55 | close(creat(bb_path_wtmp_file, 0664)); | ||
| 56 | } | ||
| 57 | memset(&utmp, 0, sizeof(utmp)); | ||
| 58 | utmp.ut_tv.tv_sec = time(NULL); | ||
| 59 | safe_strncpy(utmp.ut_user, "shutdown", UT_NAMESIZE); | ||
| 60 | utmp.ut_type = RUN_LVL; | ||
| 61 | safe_strncpy(utmp.ut_id, "~~", sizeof(utmp.ut_id)); | ||
| 62 | safe_strncpy(utmp.ut_line, "~~", UT_LINESIZE); | ||
| 63 | if (uname(&uts) == 0) | ||
| 64 | safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host)); | ||
| 65 | updwtmp(bb_path_wtmp_file, &utmp); | ||
| 66 | 73 | ||
| 67 | if (flags & 8) /* -w */ | 74 | if (flags & 8) /* -w */ |
| 68 | return EXIT_SUCCESS; | 75 | return EXIT_SUCCESS; |
| 69 | #endif /* !ENABLE_FEATURE_WTMP */ | ||
| 70 | 76 | ||
| 71 | if (!(flags & 2)) /* no -n */ | 77 | if (!(flags & 2)) /* no -n */ |
| 72 | sync(); | 78 | sync(); |
