Skip to content

Commit

Permalink
[smart] fix process kill not working
Browse files Browse the repository at this point in the history
Add more debug log info.
Not deliver masked signal to thread.
Reset mask on timedwait.
Do terminate by compete of every thread in a process.

Signed-off-by: Shell <[email protected]>
  • Loading branch information
polarvid committed Jul 30, 2023
1 parent 58d06c6 commit c007357
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
34 changes: 20 additions & 14 deletions components/lwp/lwp_pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -983,26 +983,31 @@ void lwp_terminate(struct rt_lwp *lwp)
return;
}

LOG_D("%s(lwp=%p \"%s\")", __func__, lwp, lwp->cmd);

level = rt_hw_interrupt_disable();

/* stop the receiving of signals */
lwp->terminated = RT_TRUE;

/* broadcast exit request for sibling threads */
for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next)
if (!lwp->terminated)
{
rt_thread_t thread;
lwp->terminated = RT_TRUE;

thread = rt_list_entry(list, struct rt_thread, sibling);
if (thread->exit_request == LWP_EXIT_REQUEST_NONE)
{
thread->exit_request = LWP_EXIT_REQUEST_TRIGGERED;
}
if ((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
/* broadcast exit request for sibling threads */
for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next)
{
thread->error = RT_EINTR;
rt_hw_dsb();
rt_thread_wakeup(thread);
rt_thread_t thread;

thread = rt_list_entry(list, struct rt_thread, sibling);
if (thread->exit_request == LWP_EXIT_REQUEST_NONE)
{
thread->exit_request = LWP_EXIT_REQUEST_TRIGGERED;
}
if ((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
{
thread->error = RT_EINTR;
rt_hw_dsb();
rt_thread_wakeup(thread);
}
}
}
rt_hw_interrupt_enable(level);
Expand Down Expand Up @@ -1031,6 +1036,7 @@ void lwp_wait_subthread_exit(void)
while (1)
{
int subthread_is_terminated;
LOG_D("%s: wait for subthread exiting", __func__);

level = rt_hw_interrupt_disable();
subthread_is_terminated = (int)(thread->sibling.prev == &lwp->t_grp);
Expand Down
42 changes: 25 additions & 17 deletions components/lwp/lwp_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,32 +546,36 @@ void lwp_thread_signal_catch(void *exp_frame)
* @note that the p_usi is release before entering signal action by
* reseting the kernel sp.
*/
LOG_D("%s: enter signal handler(signo=%d) at %p", __func__, signo, handler);
arch_thread_signal_enter(signo, p_usi, exp_frame, handler, &save_sig_mask);
/* the arch_thread_signal_enter() never return */
RT_ASSERT(0);
}
}

static int _do_signal_wakeup(rt_thread_t thread, int sig)
{
int need_schedule;
if ((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
if (!_sigismember(&thread->signal.sigset_mask, sig))
{

if ((thread->stat & RT_SIGNAL_COMMON_WAKEUP_MASK) != RT_SIGNAL_COMMON_WAKEUP_MASK)
{
rt_thread_wakeup(thread);
need_schedule = 1;
}
else if ((sig == SIGKILL) && ((thread->stat & RT_SIGNAL_KILL_WAKEUP_MASK) != RT_SIGNAL_KILL_WAKEUP_MASK))
if ((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
{
rt_thread_wakeup(thread);
need_schedule = 1;
if ((thread->stat & RT_SIGNAL_COMMON_WAKEUP_MASK) != RT_SIGNAL_COMMON_WAKEUP_MASK)
{
rt_thread_wakeup(thread);
need_schedule = 1;
}
else if ((sig == SIGKILL) && ((thread->stat & RT_SIGNAL_KILL_WAKEUP_MASK) != RT_SIGNAL_KILL_WAKEUP_MASK))
{
rt_thread_wakeup(thread);
need_schedule = 1;
}
else
{
need_schedule = 0;
}
}
else
{
need_schedule = 0;
}
}
else
need_schedule = 0;
Expand Down Expand Up @@ -633,7 +637,7 @@ static int _siginfo_deliver_to_lwp(struct rt_lwp *lwp, lwp_siginfo_t siginfo)
return _do_signal_wakeup(catcher, siginfo->ksiginfo.signo);
}

static int _siginfo_deliver_to_thread(struct rt_lwp *lwp, rt_thread_t thread, lwp_siginfo_t siginfo)
static int _siginfo_deliver_to_thread(rt_thread_t thread, lwp_siginfo_t siginfo)
{
sigqueue_enqueue(_SIGQ(thread), siginfo);
return _do_signal_wakeup(thread, siginfo->ksiginfo.signo);
Expand Down Expand Up @@ -679,6 +683,9 @@ rt_err_t lwp_signal_kill(struct rt_lwp *lwp, long signo, long code, long value)
}
else
{
LOG_D("%s(lwp=%p \"%s\",signo=%ld,code=%ld,value=%ld)",
__func__, lwp, lwp->cmd, signo, code, value);

need_schedule = RT_FALSE;

/* FIXME: acquire READ lock to lwp */
Expand Down Expand Up @@ -848,7 +855,7 @@ rt_err_t lwp_thread_signal_kill(rt_thread_t thread, long signo, long code, long

if (siginfo)
{
need_schedule = _siginfo_deliver_to_thread(lwp, thread, siginfo);
need_schedule = _siginfo_deliver_to_thread(thread, siginfo);
ret = 0;
}
else
Expand Down Expand Up @@ -943,10 +950,9 @@ static int _dequeue_signal(rt_thread_t thread, lwp_sigset_t *mask, siginfo_t *us
rt_err_t lwp_thread_signal_timedwait(rt_thread_t thread, lwp_sigset_t *sigset,
siginfo_t *usi, struct timespec *timeout)
{
LOG_D("%s", __func__);

rt_base_t level;
rt_err_t ret;
lwp_sigset_t saved_sigset;
int sig;

/**
Expand Down Expand Up @@ -975,6 +981,7 @@ rt_err_t lwp_thread_signal_timedwait(rt_thread_t thread, lwp_sigset_t *sigset,
* wait for the time interval specified in the timespec structure referenced
* by timeout.
*/
_thread_signal_mask(thread, LWP_SIG_MASK_CMD_SET_MASK, sigset, &saved_sigset);
if (timeout)
{
/* TODO: verify timeout valid ? not overflow 32bits, nanosec valid, ... */
Expand Down Expand Up @@ -1009,6 +1016,7 @@ rt_err_t lwp_thread_signal_timedwait(rt_thread_t thread, lwp_sigset_t *sigset,
ret = -EAGAIN;
}
/* else ret == -EINTR */
_thread_signal_mask(thread, LWP_SIG_MASK_CMD_SET_MASK, &saved_sigset, RT_NULL);

/* FIXME: acquire READ lock to lwp */
level = rt_hw_interrupt_disable();
Expand Down
3 changes: 2 additions & 1 deletion components/lwp/lwp_syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,11 @@ void sys_exit(int value)
lwp_put_to_user(clear_child_tid, &t, sizeof t);
sys_futex(clear_child_tid, FUTEX_WAKE, 1, RT_NULL, RT_NULL, 0);
}
lwp_terminate(lwp);

main_thread = rt_list_entry(lwp->t_grp.prev, struct rt_thread, sibling);
if (main_thread == tid)
{
lwp_terminate(lwp);
lwp_wait_subthread_exit();
lwp->lwp_ret = value;
}
Expand Down

0 comments on commit c007357

Please sign in to comment.