libbb: code shrink by factoring out common update_utmp_DEAD_PROCESS

function                                             old     new   delta
update_utmp_DEAD_PROCESS                               -      17     +17
telnetd_main                                        1685    1674     -11
mark_terminated                                       56      45     -11
handle_sigchld                                        74      63     -11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2015-01-05 15:37:58 +01:00
parent 08b90a9d10
commit da9212667c
5 changed files with 20 additions and 16 deletions

View File

@ -921,9 +921,11 @@ void die_if_bad_username(const char* name) FAST_FUNC;
#if ENABLE_FEATURE_UTMP #if ENABLE_FEATURE_UTMP
void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname); void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname);
void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname); void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname);
void FAST_FUNC update_utmp_DEAD_PROCESS(pid_t pid);
#else #else
# define write_new_utmp(pid, new_type, tty_name, username, hostname) ((void)0) # define write_new_utmp(pid, new_type, tty_name, username, hostname) ((void)0)
# define update_utmp(pid, new_type, tty_name, username, hostname) ((void)0) # define update_utmp(pid, new_type, tty_name, username, hostname) ((void)0)
# define update_utmp_DEAD_PROCESS(pid) ((void)0)
#endif #endif

View File

@ -538,11 +538,7 @@ static struct init_action *mark_terminated(pid_t pid)
struct init_action *a; struct init_action *a;
if (pid > 0) { if (pid > 0) {
update_utmp(pid, DEAD_PROCESS, update_utmp_DEAD_PROCESS(pid);
/*tty_name:*/ NULL,
/*username:*/ NULL,
/*hostname:*/ NULL
);
for (a = init_action_list; a; a = a->next) { for (a = init_action_list; a; a = a->next) {
if (a->pid == pid) { if (a->pid == pid) {
a->pid = 0; a->pid = 0;

View File

@ -130,3 +130,17 @@ void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const
updwtmp(bb_path_wtmp_file, &utent); updwtmp(bb_path_wtmp_file, &utent);
#endif #endif
} }
/* man utmp:
* When init(8) finds that a process has exited, it locates its utmp entry
* by ut_pid, sets ut_type to DEAD_PROCESS, and clears ut_user, ut_host
* and ut_time with null bytes.
* [same applies to other processes which maintain utmp entries, like telnetd]
*
* We do not bother actually clearing fields:
* it might be interesting to know who was logged in and from where
*/
void FAST_FUNC update_utmp_DEAD_PROCESS(pid_t pid)
{
update_utmp(pid, DEAD_PROCESS, NULL, NULL, NULL);
}

View File

@ -454,7 +454,7 @@ int login_main(int argc UNUSED_PARAM, char **argv)
else { else {
if (safe_waitpid(child_pid, NULL, 0) == -1) if (safe_waitpid(child_pid, NULL, 0) == -1)
bb_perror_msg("waitpid"); bb_perror_msg("waitpid");
update_utmp(child_pid, DEAD_PROCESS, NULL, NULL, NULL); update_utmp_DEAD_PROCESS(child_pid);
} }
IF_PAM(login_pam_end(pamh);) IF_PAM(login_pam_end(pamh);)
return 0; return 0;

View File

@ -462,15 +462,7 @@ static void handle_sigchld(int sig UNUSED_PARAM)
while (ts) { while (ts) {
if (ts->shell_pid == pid) { if (ts->shell_pid == pid) {
ts->shell_pid = -1; ts->shell_pid = -1;
// man utmp: update_utmp_DEAD_PROCESS(pid);
// When init(8) finds that a process has exited, it locates its utmp entry
// by ut_pid, sets ut_type to DEAD_PROCESS, and clears ut_user, ut_host
// and ut_time with null bytes.
// [same applies to other processes which maintain utmp entries, like telnetd]
//
// We do not bother actually clearing fields:
// it might be interesting to know who was logged in and from where
update_utmp(pid, DEAD_PROCESS, /*tty_name:*/ NULL, /*username:*/ NULL, /*hostname:*/ NULL);
break; break;
} }
ts = ts->next; ts = ts->next;
@ -739,7 +731,7 @@ int telnetd_main(int argc UNUSED_PARAM, char **argv)
continue; continue;
kill_session: kill_session:
if (ts->shell_pid > 0) if (ts->shell_pid > 0)
update_utmp(ts->shell_pid, DEAD_PROCESS, /*tty_name:*/ NULL, /*username:*/ NULL, /*hostname:*/ NULL); update_utmp_DEAD_PROCESS(ts->shell_pid);
free_session(ts); free_session(ts);
ts = next; ts = next;
} }