This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 44b956382d346ef58afb722c0966afaffd9f12ee Author: Marco Casaroli <[email protected]> AuthorDate: Tue Nov 3 19:46:50 2026 +0100 sched: Report stopped and continued children to the parent. A child process that stopped on SIGSTOP, SIGTSTP, SIGTTIN or SIGTTOU, or that continued on SIGCONT, was not reported to its parent: the parent got no SIGCHLD, and waitpid() with WUNTRACED or WCONTINUED waited for the exit. WIFSTOPPED() and WIFCONTINUED() were always 0. A shell with job control waits with WUNTRACED for its foreground job, so after Ctrl-Z it waited for ever. Also, waitpid() with a pid of 0 or less than -1 (a process group) failed with ECHILD, and waitid() with P_PGID failed with ENOSYS. With CONFIG_SCHED_HAVE_PARENT and CONFIG_SIG_SIGSTOP_ACTION: - The stop action sends SIGCHLD with CLD_STOPPED and the stop signal to the parent. SIGCONT to a stopped process sends CLD_CONTINUED. If the SIGCHLD action of the parent has SA_NOCLDSTOP, no SIGCHLD is sent, but the threads of the parent that wait in waitpid() or waitid() wake up. - With CONFIG_SCHED_CHILD_STATUS, the child status keeps the event until a wait reports it: waitpid() with WUNTRACED or WCONTINUED, or waitid() with WSTOPPED or WCONTINUED, reports it once, and WNOWAIT keeps it. Without the child status, the SIGCHLD tells the event, as for an exit. - SIGKILL also continues a stopped process, so that the process ends. For each build: - waitpid() is now waitid() with a status in place of the siginfo, so one wait loop does both. The status of a stopped child is (signal << 8) | 0x7f, and the status of a continued child is 0xffff. WIFSTOPPED(), WSTOPSIG() and WIFCONTINUED() decode them. The other macros and the values of the options do not change. - The loop keeps the rules of a parent that does not keep the child status (SA_NOCLDWAIT or SIGCHLD set to SIG_IGN): it waits until all the selected children exit, then fails with ECHILD. It still reports a stop or a continue. group_has_child() moves to sched_waitid.c as child_running(), which also selects a process group. - waitpid(0), waitpid(-pgid) and waitid(P_PGID) select the children in a process group: the process groups of setpgid() with CONFIG_SCHED_PROCESS_GROUPS, else a process group is one process. A child that exited is in the process group that it had at its exit. - waitid() fails with EINVAL for an unknown idtype or option, or if the options have no WEXITED, WSTOPPED or WCONTINUED. It checked this only with CONFIG_DEBUG_FEATURES. - Without group members (CONFIG_DISABLE_PTHREAD), the child status now records the exit of a child: the waits use only the child status. - Without CONFIG_SCHED_HAVE_PARENT, waitpid() with WUNTRACED gives the status of a stopped process, not 0. The change is in the common scheduler code, with no architecture code: the behavior is the same in the flat, protected and kernel builds. The flags of a task group get one bit, GROUP_FLAG_STOPPED, and a child status gets a signal number and, with process groups, a process group ID. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- include/nuttx/sched.h | 9 +- include/sys/wait.h | 12 +- sched/group/group.h | 12 +- sched/group/group_childstatus.c | 157 ++++++----- sched/sched/sched.h | 11 + sched/sched/sched_waitid.c | 588 +++++++++++++++++++++------------------- sched/sched/sched_waitpid.c | 327 +++------------------- sched/signal/sig_default.c | 132 ++++++++- sched/signal/sig_dispatch.c | 116 +++++++- sched/signal/signal.h | 7 + sched/task/task_exithook.c | 12 +- sched/task/task_setup.c | 12 +- 12 files changed, 758 insertions(+), 637 deletions(-) diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 77c8b993daa..d298cacbd17 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -127,6 +127,7 @@ #define GROUP_FLAG_DUMPABLE (1 << 6) /* Bit 6: Process may be traced / coredumped */ #define GROUP_FLAG_VFORK (1 << 7) /* Bit 7: In the address environment of the vfork() parent */ #define GROUP_FLAG_EXECED (1 << 8) /* Bit 8: The process image came from exec() */ +#define GROUP_FLAG_STOPPED (1 << 9) /* Bit 9: Stopped, the parent knows it */ /* Values for struct child_status_s ch_flags */ @@ -136,7 +137,9 @@ # define CHILD_FLAG_TTYPE_PTHREAD (1 << CHILD_FLAG_TTYPE_SHIFT) /* User pthread */ # define CHILD_FLAG_TTYPE_KERNEL (2 << CHILD_FLAG_TTYPE_SHIFT) /* Kernel thread */ #define CHILD_FLAG_EXITED (1 << 2) /* Bit 2: The child thread has exit'ed */ - /* Bits 3-7: Available */ +#define CHILD_FLAG_STOPPED (1 << 3) /* Bit 3: Stopped, not reported yet */ +#define CHILD_FLAG_CONTINUED (1 << 4) /* Bit 4: Continued, not reported yet */ + /* Bits 5-7: Available */ /* Sporadic scheduler flags */ @@ -375,7 +378,11 @@ struct child_status_s FAR struct child_status_s *flink; uint8_t ch_flags; /* Child status: See CHILD_FLAG_* defines */ + uint8_t ch_stopsig; /* Signal that stopped the child */ pid_t ch_pid; /* Child task ID */ +#ifdef CONFIG_SCHED_PROCESS_GROUPS + pid_t ch_pgid; /* Child process group ID at exit */ +#endif int ch_status; /* Child exit status */ #ifdef CONFIG_SCHED_USER_IDENTITY uid_t ch_uid; /* Child real user ID at exit */ diff --git a/include/sys/wait.h b/include/sys/wait.h index 5ae8f2b36e3..fae2fdc33e3 100644 --- a/include/sys/wait.h +++ b/include/sys/wait.h @@ -40,16 +40,22 @@ * Encoded is as follows as 2 bytes of _info(MS) then two bytes of code (LS). * Code: * 0 - Child has exited, info is the exit code. + * 0x7f - Child has stopped, info is the signal that stopped it. + * 0xffff (all of the 2 bytes) - Child has continued. * Other values - Not implemented */ #define WEXITSTATUS(s) (((s) >> 8) & 0xff) /* Return exit status */ #define WIFEXITED(s) (((s) & 0xff) == 0) /* True: Child exited normally */ -#define WIFCONTINUED(s) (0) /* True: Child has been continued */ +#define WIFCONTINUED(s) ((s) == 0xffff) /* True: Child has been continued */ #define WIFSIGNALED(s) (0) /* True: Child exited due to uncaught signal */ -#define WIFSTOPPED(s) (0) /* True: Child is currently stopped */ -#define WSTOPSIG(s) (0) /* Return signal number that caused process to stop */ + +/* True: Child is currently stopped */ + +#define WIFSTOPPED(s) (((s) & 0xff) == 0x7f) + +#define WSTOPSIG(s) (((s) >> 8) & 0xff) /* Return signal number that caused process to stop */ #define WTERMSIG(s) (((s) >> 8) & 0x7f) /* Return signal number that caused process to terminate */ #define WCOREDUMP(s) (((s) >> 8) & 0x80) /* True: Child has produced a core dump */ diff --git a/sched/group/group.h b/sched/group/group.h index 473277b5b35..73f503a489a 100644 --- a/sched/group/group.h +++ b/sched/group/group.h @@ -30,6 +30,7 @@ #include <nuttx/config.h> #include <sys/types.h> +#include <sys/wait.h> #include <stdbool.h> #include <sched.h> @@ -90,8 +91,11 @@ FAR struct task_group_s *task_getgroup(pid_t pid); /* Process groups and sessions */ #ifdef CONFIG_SCHED_PROCESS_GROUPS +# define group_getpgid(group) ((group)->tg_pgid) bool group_in_pgrp(FAR struct tcb_s *tcb, pid_t pgid); int group_setpgid(pid_t pid, pid_t pgid); +#else +# define group_getpgid(group) ((group)->tg_pid) #endif /* Signaling group members */ @@ -109,13 +113,17 @@ FAR struct child_status_s *group_alloc_child(void); void group_free_child(FAR struct child_status_s *status); void group_add_child(FAR struct task_group_s *group, FAR struct child_status_s *child); -FAR struct child_status_s *group_exit_child(FAR struct task_group_s *group); FAR struct child_status_s *group_find_child(FAR struct task_group_s *group, pid_t pid); FAR struct child_status_s *group_remove_child(FAR struct task_group_s *group, pid_t pid); void group_remove_children(FAR struct task_group_s *group); -bool group_has_child(FAR struct task_group_s *group, pid_t pid); +#ifdef CONFIG_SCHED_WAITPID +uint8_t group_child_event(FAR struct child_status_s *child, int options); +FAR struct child_status_s *group_wait_child(FAR struct task_group_s *group, + idtype_t idtype, pid_t id, + int options, FAR bool *found); +#endif #endif /* CONFIG_SCHED_CHILD_STATUS */ #endif /* CONFIG_SCHED_HAVE_PARENT */ diff --git a/sched/group/group_childstatus.c b/sched/group/group_childstatus.c index c71556c70b1..97efcd48011 100644 --- a/sched/group/group_childstatus.c +++ b/sched/group/group_childstatus.c @@ -61,15 +61,6 @@ struct child_pool_s FAR struct child_status_s *freelist; }; -/* The search state of group_has_child() */ - -struct child_search_s -{ - FAR struct task_group_s *group; - pid_t pid; - bool found; -}; - /**************************************************************************** * Private Data ****************************************************************************/ @@ -117,26 +108,35 @@ static void group_dump_children(FAR struct task_group_s *group, #endif /**************************************************************************** - * Name: group_child_handler + * Name: group_child_pgid * * Description: - * The nxsched_foreach() handler of group_has_child(). + * Return the process group ID of a child. A child that exited is in the + * process group that it had when its main thread exited. * ****************************************************************************/ -static void group_child_handler(FAR struct tcb_s *tcb, FAR void *arg) +#ifdef CONFIG_SCHED_WAITPID +static pid_t group_child_pgid(FAR struct child_status_s *child) { - FAR struct child_search_s *search = arg; - FAR struct task_group_s *group = tcb->group; +#ifdef CONFIG_SCHED_PROCESS_GROUPS + FAR struct task_group_s *chgrp; - if (group != NULL && group != search->group && - group->tg_ppid == search->group->tg_pid && - (search->pid == INVALID_PROCESS_ID || group->tg_pid == search->pid) && - (tcb->flags & TCB_FLAG_EXIT_PROCESSING) == 0) + if ((child->ch_flags & CHILD_FLAG_EXITED) == 0) { - search->found = true; + chgrp = task_getgroup(child->ch_pid); + if (chgrp != NULL) + { + return chgrp->tg_pgid; + } } + + return child->ch_pgid; +#else + return child->ch_pid; +#endif } +#endif /**************************************************************************** * Public Functions @@ -332,44 +332,107 @@ FAR struct child_status_s *group_find_child(FAR struct task_group_s *group, } /**************************************************************************** - * Name: group_exit_child + * Name: group_child_event + * + * Description: + * Return the event of a child that waitpid() or waitid() can report with + * these options: the child exited (WEXITED), stopped (WUNTRACED or + * WSTOPPED) or continued (WCONTINUED), and the event was not reported + * yet. An exit comes before the other events. + * + * Input Parameters: + * child - The child status structure. + * options - The options of waitpid() or waitid(). + * + * Returned Value: + * CHILD_FLAG_EXITED, CHILD_FLAG_STOPPED or CHILD_FLAG_CONTINUED, or 0 if + * the child has no event to report. + * + ****************************************************************************/ + +#ifdef CONFIG_SCHED_WAITPID +uint8_t group_child_event(FAR struct child_status_s *child, int options) +{ + if ((child->ch_flags & CHILD_FLAG_EXITED) != 0) + { + return (options & WEXITED) != 0 ? CHILD_FLAG_EXITED : 0; + } + + if ((child->ch_flags & CHILD_FLAG_STOPPED) != 0 && + (options & (WUNTRACED | WSTOPPED)) != 0) + { + return CHILD_FLAG_STOPPED; + } + + if ((child->ch_flags & CHILD_FLAG_CONTINUED) != 0 && + (options & WCONTINUED) != 0) + { + return CHILD_FLAG_CONTINUED; + } + + return 0; +} + +/**************************************************************************** + * Name: group_wait_child * * Description: - * Search for any child that has exited. + * Find a child that idtype and id select and that has an event to report + * (see group_child_event()). P_ALL selects all children, P_PID the child + * with the process ID id, and P_PGID the children in the process group + * id. A child that exited is in the process group that it had when its + * main thread exited. Without CONFIG_SCHED_PROCESS_GROUPS, a process + * group contains only one process. * * Input Parameters: - * tcb - The TCB of the parent task to containing the child status. + * group - The parent task group. + * idtype - P_ALL, P_PID or P_PGID. + * id - The process ID or the process group ID. + * options - The options of waitpid() or waitid(). + * found - Return true if idtype and id select a child. * * Returned Value: - * On success, a non-NULL pointer to a child status structure for the - * exited child. NULL is returned if not child has exited. + * The child status structure, or NULL if no child that idtype and id + * select has an event to report. * * Assumptions: - * Called during SIGCHLD processing in a safe context. No special - * precautions are required here. + * Called from a critical section. * ****************************************************************************/ -FAR struct child_status_s *group_exit_child(FAR struct task_group_s *group) +FAR struct child_status_s *group_wait_child(FAR struct task_group_s *group, + idtype_t idtype, pid_t id, + int options, FAR bool *found) { FAR struct child_status_s *child; irqstate_t flags; - /* Find the status structure of any child task that has exited. */ + *found = false; flags = spin_lock_irqsave(&group->tg_lock); for (child = group->tg_children; child; child = child->flink) { - if ((child->ch_flags & CHILD_FLAG_EXITED) != 0) + if (idtype == P_PID && child->ch_pid != id) { - spin_unlock_irqrestore(&group->tg_lock, flags); - return child; + continue; + } + + if (idtype == P_PGID && group_child_pgid(child) != id) + { + continue; + } + + *found = true; + if (group_child_event(child, options) != 0) + { + break; } } spin_unlock_irqrestore(&group->tg_lock, flags); - return NULL; + return child; } +#endif /* CONFIG_SCHED_WAITPID */ /**************************************************************************** * Name: group_remove_child @@ -481,34 +544,4 @@ void group_remove_children(FAR struct task_group_s *group) group_dump_children(group, "group_remove_children"); } -/**************************************************************************** - * Name: group_has_child - * - * Description: - * Check if a child of the task group is still running. This also finds - * the children that have no child status structure. - * - * Input Parameters: - * group - The parent task group. - * pid - The PID of the child, or INVALID_PROCESS_ID for any child. - * - * Returned Value: - * True if the child (or any child) has a thread that is not exiting. - * - * Assumptions: - * Called from a critical section. - * - ****************************************************************************/ - -bool group_has_child(FAR struct task_group_s *group, pid_t pid) -{ - struct child_search_s search; - - search.group = group; - search.pid = pid; - search.found = false; - nxsched_foreach(group_child_handler, &search); - return search.found; -} - #endif /* CONFIG_SCHED_HAVE_PARENT && CONFIG_SCHED_CHILD_STATUS */ diff --git a/sched/sched/sched.h b/sched/sched/sched.h index 3805fce8026..8a2e38ca9cb 100644 --- a/sched/sched/sched.h +++ b/sched/sched/sched.h @@ -30,6 +30,7 @@ #include <nuttx/config.h> #include <sys/types.h> +#include <sys/wait.h> #include <stdbool.h> #include <sched.h> @@ -44,6 +45,11 @@ #define PIDHASH(pid) ((pid) & (g_npidhash - 1)) +/* The wait() status of a stopped child and of a continued child */ + +#define WAIT_STOPPED(signo) ((((signo) & 0xff) << 8) | 0x7f) +#define WAIT_CONTINUED 0xffff + /* The state of a task is indicated both by the task_state field of the TCB * and by a series of task lists. All of these tasks lists are declared * below. Although it is not always necessary, most of these lists are @@ -384,6 +390,11 @@ void nxsched_sporadic_lowpriority(FAR struct tcb_s *tcb); void nxsched_suspend(FAR struct tcb_s *tcb); #endif +#if defined(CONFIG_SCHED_WAITPID) && defined(CONFIG_SCHED_HAVE_PARENT) +int nxsched_waitid(idtype_t idtype, pid_t id, FAR siginfo_t *info, + int options); +#endif + #if defined(up_this_task) # define this_task() up_this_task() #elif !defined(CONFIG_SMP) diff --git a/sched/sched/sched_waitid.c b/sched/sched/sched_waitid.c index d68a038baa7..80edc3f8c1c 100644 --- a/sched/sched/sched_waitid.c +++ b/sched/sched/sched_waitid.c @@ -40,159 +40,312 @@ #if defined(CONFIG_SCHED_WAITPID) && defined(CONFIG_SCHED_HAVE_PARENT) +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The search state of child_running() */ + +struct child_search_s +{ + FAR struct task_group_s *group; /* The parent task group */ + idtype_t idtype; /* P_ALL, P_PID or P_PGID */ + pid_t id; /* The process ID or process group ID */ + bool found; /* A child that idtype and id select */ +}; + /**************************************************************************** * Private Functions ****************************************************************************/ /**************************************************************************** - * Name: exited_child + * Name: report_child * * Description: - * Handle the case where a child exited properlay was we (apparently) lost - * the detch of child signal. + * Give the event of a child (see group_child_event()) in info. The + * event is then forgotten, and a child that exited is discarded, unless + * WNOWAIT keeps the child in a waitable state. * ****************************************************************************/ #ifdef CONFIG_SCHED_CHILD_STATUS -static void exited_child(FAR struct tcb_s *rtcb, +static void report_child(FAR struct tcb_s *rtcb, FAR struct child_status_s *child, FAR siginfo_t *info, int options) { - /* The child has exited. Return the saved exit status (and some fudged - * information). - */ + uint8_t event = group_child_event(child, options); - if (info) - { - info->si_signo = SIGCHLD; - info->si_code = CLD_EXITED; - info->si_errno = OK; - info->si_value.sival_ptr = NULL; - info->si_pid = child->ch_pid; + info->si_signo = SIGCHLD; + info->si_errno = OK; + info->si_value.sival_ptr = NULL; + info->si_pid = child->ch_pid; #ifdef CONFIG_SCHED_USER_IDENTITY - info->si_uid = child->ch_uid; + info->si_uid = child->ch_uid; #else - info->si_uid = 0; + info->si_uid = 0; #endif - info->si_status = child->ch_status; - } - /* Discard the child entry unless WNOWAIT is set (the caller wants the - * child to remain in a waitable state). - */ + if (event == CHILD_FLAG_STOPPED) + { + info->si_code = CLD_STOPPED; + info->si_status = child->ch_stopsig; + } + else if (event == CHILD_FLAG_CONTINUED) + { + info->si_code = CLD_CONTINUED; + info->si_status = SIGCONT; + } + else + { + info->si_code = CLD_EXITED; + info->si_status = child->ch_status; + } if ((options & WNOWAIT) == 0) { - group_remove_child(rtcb->group, child->ch_pid); - group_free_child(child); + if (event == CHILD_FLAG_EXITED) + { + group_remove_child(rtcb->group, child->ch_pid); + group_free_child(child); + } + else + { + child->ch_flags &= ~event; + } } } #endif -static inline_function -int waittcb(FAR struct tcb_s *rtcb, idtype_t idtype, int options, - id_t id, FAR siginfo_t *info, bool retains) +/**************************************************************************** + * Name: child_handler + * + * Description: + * The nxsched_foreach() handler of child_running(). + * + ****************************************************************************/ + +static void child_handler(FAR struct tcb_s *tcb, FAR void *arg) { + FAR struct child_search_s *search = arg; + FAR struct task_group_s *group = tcb->group; + + if (group == NULL || group == search->group || + group->tg_ppid != search->group->tg_pid) + { + return; + } + #ifdef CONFIG_SCHED_CHILD_STATUS - FAR struct child_status_s *child; + /* A child that sent SIGCHLD can still be found during its exit */ + + if ((tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + { + return; + } #endif - int errcode = OK; - sigset_t set; - int ret; - /* Create a signal set that contains only SIGCHLD */ + if (search->idtype == P_ALL || + (search->idtype == P_PID && group->tg_pid == search->id) || + (search->idtype == P_PGID && + group_getpgid(group) == search->id)) + { + search->found = true; + } +} - sigemptyset(&set); - nxsig_addset(&set, SIGCHLD); +/**************************************************************************** + * Name: child_running + * + * Description: + * Return true if a child that idtype and id select still runs. This + * also finds the children that have no child status structure. + * + ****************************************************************************/ - /* Loop until the child that we are waiting for dies */ +static bool child_running(FAR struct tcb_s *rtcb, idtype_t idtype, pid_t id) +{ + struct child_search_s search; - while (errcode == OK) +#ifndef CONFIG_SCHED_CHILD_STATUS + if (rtcb->group->tg_nchildren == 0) { -#ifdef CONFIG_SCHED_CHILD_STATUS - /* Another thread can change the action for SIGCHLD while we wait */ + return false; + } - retains = ((rtcb->group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0); + if (idtype == P_ALL) + { + return true; + } - /* Check if the task has already died. Signals are not queued in - * NuttX. So a possibility is that the child has died and we - * missed the death of child signal (we got some other signal - * instead). - */ + if (idtype == P_PID) + { + return nxsched_get_tcb(id) != NULL; + } +#endif - if (idtype == P_ALL) - { - /* We are waiting for any child to exit */ + search.group = rtcb->group; + search.idtype = idtype; + search.id = id; + search.found = false; + nxsched_foreach(child_handler, &search); + return search.found; +} - if (retains && (child = group_exit_child(rtcb->group)) != NULL) - { - /* A child has exited. Apparently we missed the signal. - * Return the exit status and break out of the loop. - */ +/**************************************************************************** + * Name: child_selected + * + * Description: + * Without a child status: return true if the SIGCHLD in info is an event + * that options wait for, of a child that idtype and id select. A child + * that is gone is taken to be in the process group. + * + ****************************************************************************/ - exited_child(rtcb, child, info, options); - break; - } +static bool child_selected(FAR siginfo_t *info, idtype_t idtype, pid_t id, + int options) +{ + FAR struct task_group_s *chgrp; - /* Without an exit status to wait for (XSI), wait until all - * children exit - */ + if (info->si_signo != SIGCHLD) + { + return false; + } - if ((!retains || rtcb->group->tg_children == NULL) && - !group_has_child(rtcb->group, INVALID_PROCESS_ID)) - { - errcode = ECHILD; - break; - } + if (info->si_code == CLD_STOPPED) + { + if ((options & (WUNTRACED | WSTOPPED)) == 0) + { + return false; } + } + else if (info->si_code == CLD_CONTINUED) + { + if ((options & WCONTINUED) == 0) + { + return false; + } + } + else + { +#ifdef CONFIG_SCHED_CHILD_STATUS + /* The exit status is not kept (XSI): wait for all children */ - /* We are waiting for a specific PID. Does this task retain child - * status? - */ - - else if (retains && - (child = group_find_child(rtcb->group, id)) != NULL) + return false; +#else + if ((options & WEXITED) == 0) { - /* Yes ... Did the child exit? */ + return false; + } +#endif + } - if ((child->ch_flags & CHILD_FLAG_EXITED) != 0) - { - /* The child has exited. Return the exit status and break out - * of the loop. - */ + if (idtype == P_PGID) + { + chgrp = task_getgroup(info->si_pid); + return chgrp == NULL || group_getpgid(chgrp) == id; + } - exited_child(rtcb, child, info, options); - break; - } + return idtype == P_ALL || info->si_pid == id; +} + +/**************************************************************************** + * Name: check_children + * + * Description: + * Return -ECHILD if idtype and id select no child of the caller. + * + ****************************************************************************/ + +static int check_children(FAR struct tcb_s *rtcb, idtype_t idtype, pid_t id) +{ +#ifdef CONFIG_SCHED_CHILD_STATUS + bool found; + + /* Without the child status, the wait loop checks the running children */ + + if ((rtcb->group->tg_flags & GROUP_FLAG_NOCLDWAIT) != 0) + { + return OK; + } + + group_wait_child(rtcb->group, idtype, id, 0, &found); + return found ? OK : -ECHILD; +#else + FAR struct tcb_s *ctcb; + + if (idtype == P_PID) + { + ctcb = nxsched_get_tcb(id); + if (ctcb == NULL || ctcb->group == NULL || + ctcb->group->tg_ppid != rtcb->group->tg_pid || + (ctcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + { + return -ECHILD; } - else if (!group_has_child(rtcb->group, id)) + } + + return child_running(rtcb, idtype, id) ? OK : -ECHILD; +#endif +} + +/**************************************************************************** + * Name: waittcb + * + * Description: + * Wait until a child that idtype and id select has an event that options + * wait for. + * + ****************************************************************************/ + +static int waittcb(FAR struct tcb_s *rtcb, idtype_t idtype, pid_t id, + FAR siginfo_t *info, int options) +{ +#ifdef CONFIG_SCHED_CHILD_STATUS + FAR struct child_status_s *child; +#endif + bool retains = false; + bool found = false; + sigset_t set; + int ret; + + /* Create a signal set that contains only SIGCHLD */ + + sigemptyset(&set); + nxsig_addset(&set, SIGCHLD); + + for (; ; ) + { +#ifdef CONFIG_SCHED_CHILD_STATUS + /* Another thread can change the action for SIGCHLD while we wait */ + + retains = (rtcb->group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0; + found = false; + if (retains) { - /* It is no longer running and there is no exit status (XSI, or - * another thread took it) + /* Signals are not queued in NuttX, so a SIGCHLD can be lost. + * The child status keeps each event: look there first. */ - errcode = ECHILD; - break; + child = group_wait_child(rtcb->group, idtype, id, options, + &found); + if (child != NULL) + { + report_child(rtcb, child, info, options); + return OK; + } } -#else - /* Check if the task has already died. Signals are not queued in - * NuttX. So a possibility is that the child has died and we - * missed the death of child signal (we got some other signal - * instead). +#endif + + /* Without a child status (XSI, or another thread took it), wait + * while a child runs. Without the child status option, the child + * ended and the SIGCHLD was lost: ECHILD is better than a wrong + * status. */ - if (rtcb->group->tg_nchildren == 0 || - (idtype == P_PID && nxsched_get_tcb((pid_t)id) == NULL)) + if (!found && !child_running(rtcb, idtype, id)) { - /* We know that the child task was running okay we started, - * so we must have lost the signal. What can we do? - * Let's return ECHILD.. that is at least informative. - */ - - errcode = ECHILD; - break; + return -ECHILD; } -#endif if ((options & WNOHANG) != 0) { @@ -206,97 +359,81 @@ int waittcb(FAR struct tcb_s *rtcb, idtype_t idtype, int options, */ info->si_signo = 0; - info->si_pid = 0; - break; + info->si_pid = 0; + return OK; } - /* Wait for any death-of-child signal */ + /* Wait for the next SIGCHLD */ ret = nxsig_timedwait(&set, info, NULL); if (ret < 0) { - errcode = -ret; - break; + return ret; } - /* Make there this was SIGCHLD */ + /* Without a child status, the SIGCHLD tells the event */ - if (info->si_signo == SIGCHLD) + if (!retains && child_selected(info, idtype, id, options)) { -#ifdef CONFIG_SCHED_CHILD_STATUS - if (!retains && idtype != P_PGID) - { - /* No exit status (XSI): check the children again */ - - continue; - } -#endif - - /* Yes.. Are we waiting for the death of a specific child? */ - - if (idtype == P_PID) - { - /* Was this the death of the thread we were waiting for? */ - - if (info->si_pid == (pid_t)id) - { - /* Yes... return success */ - -#ifdef CONFIG_SCHED_CHILD_STATUS - if (retains) - { - child = group_find_child(rtcb->group, info->si_pid); - if (child != NULL && - (child->ch_flags & CHILD_FLAG_EXITED) != 0) - { - exited_child(rtcb, child, NULL, options); - } - } -#endif + return OK; + } + } +} - break; - } - } +/**************************************************************************** + * Public Functions + ****************************************************************************/ - /* Are we waiting for any child to change state? */ +/**************************************************************************** + * Name: nxsched_waitid + * + * Description: + * The common part of waitid() and waitpid(): wait until a child that + * idtype and id select exited (WEXITED), stopped (WSTOPPED or WUNTRACED) + * or continued (WCONTINUED), and give the event in info as the SIGCHLD of + * the event. P_PGID with an id of 0 selects the process group of the + * caller. + * + * Input Parameters: + * idtype - P_PID, P_PGID or P_ALL + * id - The process ID or the process group ID + * info - The location for the event + * options - See waitid(); WUNTRACED is the same as WSTOPPED + * + * Returned Value: + * OK (0) on success; with WNOHANG and no event, si_pid in info is 0. + * A negated errno value on failure (ECHILD, EINTR). + * + ****************************************************************************/ - else if (idtype == P_ALL) - { - /* Return success */ +int nxsched_waitid(idtype_t idtype, pid_t id, FAR siginfo_t *info, + int options) +{ + FAR struct tcb_s *rtcb = this_task(); + irqstate_t flags; + int ret; -#ifdef CONFIG_SCHED_CHILD_STATUS - if (retains) - { - child = group_find_child(rtcb->group, info->si_pid); - - if (child && - (child->ch_flags & CHILD_FLAG_EXITED) != 0) - { - exited_child(rtcb, child, NULL, options); - } - } -#endif + if (idtype == P_PGID && id == 0) + { + id = group_getpgid(rtcb->group); + } - break; - } + /* NOTE: sched_lock() is not enough for SMP + * because the child task is running on another CPU + */ - /* Other ID types are not supported */ + flags = enter_critical_section(); - else /* if (idtype == P_PGID) */ - { - errcode = ENOSYS; - break; - } - } + ret = check_children(rtcb, idtype, id); + if (ret == OK) + { + ret = waittcb(rtcb, idtype, id, info, options); } - return errcode; + leave_critical_section(flags); + return ret; } -/**************************************************************************** - * Public Functions - ****************************************************************************/ - /**************************************************************************** * Name: waitid * @@ -362,125 +499,32 @@ int waittcb(FAR struct tcb_s *rtcb, idtype_t idtype, int options, int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options) { - FAR struct tcb_s *rtcb = this_task(); - FAR struct tcb_s *ctcb; - bool retains = false; - int errcode = ENOSYS; - irqstate_t flags; int ret; /* waitid() is a cancellation point */ enter_cancellation_point(); - flags = enter_critical_section(); - /* MISSING LOGIC: If WNOHANG is provided in the options, then this - * function should returned immediately. However, there is no mechanism - * available now know if the thread has child: The children remember - * their parents (if CONFIG_SCHED_HAVE_PARENT) but the parents do not - * remember their children. - */ - -#ifdef CONFIG_DEBUG_FEATURES - /* Only ID types P_PID and P_ALL are supported */ - - if ((idtype == P_PID || idtype == P_ALL) && - (options & WEXITED) != 0 && - (options & ~(WEXITED | WNOHANG | WNOWAIT)) == 0) -#endif + if ((idtype != P_PID && idtype != P_PGID && idtype != P_ALL) || + (options & ~(WEXITED | WSTOPPED | WCONTINUED | WNOHANG | + WNOWAIT)) != 0 || + (options & (WEXITED | WSTOPPED | WCONTINUED)) == 0) { - errcode = OK; - - /* Verify that this task actually has children and that the requested - * TCB is actually a child of this task. - */ - -#ifdef CONFIG_SCHED_CHILD_STATUS - /* Does this task retain child status? */ - - retains = ((rtcb->group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0); - - if (rtcb->group->tg_children == NULL && retains) - { - /* There are no children */ - - errcode = ECHILD; - } - else if (idtype == P_PID) - { - /* Get the TCB corresponding to this PID and make sure that the - * thread it is our child. - */ - - ctcb = nxsched_get_tcb((pid_t)id); - if (ctcb && ctcb->group) - { - /* Make sure that the thread it is our child. */ - - if (ctcb->group->tg_ppid != rtcb->group->tg_pid) - { - errcode = ECHILD; - } - } - - /* Does this task retain child status? */ - - if (retains && errcode == OK) - { - /* Check if this specific pid has allocated child status? */ - - if (group_find_child(rtcb->group, (pid_t)id) == NULL) - { - /* This specific pid is not a child */ - - errcode = ECHILD; - } - } - } -#else - /* Child status is not retained. */ - - if (rtcb->group->tg_nchildren == 0) - { - /* There are no children */ - - errcode = ECHILD; - } - else if (idtype == P_PID) - { - /* Get the TCB corresponding to this PID and make sure that the - * thread is our child. - */ - - ctcb = nxsched_get_tcb((pid_t)id); - - if (!ctcb || !ctcb->group || - ctcb->group->tg_ppid != rtcb->group->tg_pid) - { - errcode = ECHILD; - } - } -#endif + ret = -EINVAL; } - - if (errcode == OK) + else { - errcode = waittcb(rtcb, idtype, options, id, info, retains); + ret = nxsched_waitid(idtype, id, info, options); } - leave_critical_section(flags); leave_cancellation_point(); - if (errcode != OK) + if (ret < 0) { - ret = ERROR; - set_errno(errcode); - } - else - { - ret = OK; + set_errno(-ret); + return ERROR; } - return ret; + return OK; } #endif /* CONFIG_SCHED_WAITPID && CONFIG_SCHED_HAVE_PARENT */ diff --git a/sched/sched/sched_waitpid.c b/sched/sched/sched_waitpid.c index 9c8f877e5e9..ca94aee142e 100644 --- a/sched/sched/sched_waitpid.c +++ b/sched/sched/sched_waitpid.c @@ -41,212 +41,6 @@ #ifdef CONFIG_SCHED_WAITPID -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -#ifdef CONFIG_SCHED_HAVE_PARENT -static inline_function -int waittcb(FAR struct tcb_s *rtcb, pid_t pid, int options, - FAR int *stat_loc, bool retains) -{ -#ifdef CONFIG_SCHED_CHILD_STATUS - FAR struct child_status_s *child = NULL; -#endif - struct siginfo info; - sigset_t set; - int ret = OK; - - /* Create a signal set that contains only SIGCHLD */ - - sigemptyset(&set); - nxsig_addset(&set, SIGCHLD); - - while (ret >= 0) - { -#ifdef CONFIG_SCHED_CHILD_STATUS - /* Another thread can change the action for SIGCHLD while we wait */ - - retains = ((rtcb->group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0); - - /* Check if the task has already died. Signals are not queued in - * NuttX. So a possibility is that the child has died and we - * missed the death of child signal (we got some other signal - * instead). - */ - - if (pid == INVALID_PROCESS_ID) - { - /* We are waiting for any child, check if there are still - * children. - */ - - if (retains && (child = group_exit_child(rtcb->group)) != NULL) - { - /* A child has exited. Apparently we missed the signal. - * Return the saved exit status. - */ - - /* The child has exited. Return the saved exit status */ - - if (stat_loc != NULL) - { - *stat_loc = child->ch_status << 8; - } - - ret = child->ch_pid; - - /* Discard the child entry unless WNOWAIT is set */ - - if ((options & WNOWAIT) == 0) - { - group_remove_child(rtcb->group, child->ch_pid); - group_free_child(child); - } - - break; - } - - /* Without an exit status to wait for (XSI), wait until all - * children exit - */ - - if ((!retains || rtcb->group->tg_children == NULL) && - !group_has_child(rtcb->group, pid)) - { - ret = -ECHILD; - break; - } - } - - /* We are waiting for a specific PID. Does this task retain child - * status? - */ - - else if (retains && - (child = group_find_child(rtcb->group, pid)) != NULL) - { - /* Did the child exit? */ - - if ((child->ch_flags & CHILD_FLAG_EXITED) != 0) - { - /* The child has exited. Return the saved exit status */ - - if (stat_loc != NULL) - { - *stat_loc = child->ch_status << 8; - } - - /* Discard the child entry and break out of the loop */ - - if ((options & WNOWAIT) == 0) - { - group_remove_child(rtcb->group, pid); - group_free_child(child); - } - - ret = pid; - break; - } - } - else if (!group_has_child(rtcb->group, pid)) - { - /* It is no longer running and there is no exit status (XSI, or - * another thread took it) - */ - - ret = -ECHILD; - break; - } - -#else /* CONFIG_SCHED_CHILD_STATUS */ - - /* Check if the task has already died. Signals are not queued in - * NuttX. So a possibility is that the child has died and we - * missed the death of child signal (we got some other signal - * instead). - */ - - if (rtcb->group->tg_nchildren == 0 || - (pid != INVALID_PROCESS_ID && nxsched_get_tcb(pid) == NULL)) - { - /* We know that the child task was running okay when we started, - * so we must have lost the signal. What can we do? - * Let's return ECHILD.. that is at least informative. - */ - - ret = -ECHILD; - break; - } - -#endif /* CONFIG_SCHED_CHILD_STATUS */ - - if ((options & WNOHANG) != 0) - { - ret = 0; - break; - } - - /* Wait for any death-of-child signal */ - - ret = nxsig_timedwait(&set, &info, NULL); - - if (ret < 0) - { - break; - } - - /* Was this the death of the thread we were waiting for? In the of - * pid == INVALID_PROCESS_ID, we are waiting for any child thread. - */ - - if (info.si_signo == SIGCHLD && - (pid == INVALID_PROCESS_ID || info.si_pid == pid)) - { -#ifdef CONFIG_SCHED_CHILD_STATUS - if (!retains) - { - /* No exit status (XSI): check the children again */ - - continue; - } -#endif - - /* Yes... return the status and PID (in the event it was -1) */ - - if (stat_loc != NULL) - { - *stat_loc = info.si_status << 8; - } - - pid = info.si_pid; - -#ifdef CONFIG_SCHED_CHILD_STATUS - if (retains) - { - /* Recover the exiting child */ - - child = group_find_child(rtcb->group, info.si_pid); - - /* Discard the child entry, if we have one */ - - if (child != NULL && (options & WNOWAIT) == 0) - { - group_remove_child(rtcb->group, child->ch_pid); - group_free_child(child); - } - } -#endif /* CONFIG_SCHED_CHILD_STATUS */ - - ret = pid; - break; - } - } - - return ret; -} -#endif - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -300,108 +94,67 @@ int waittcb(FAR struct tcb_s *rtcb, pid_t pid, int options, #ifdef CONFIG_SCHED_HAVE_PARENT /**************************************************************************** * - * If CONFIG_SCHED_HAVE_PARENT is defined, then waitpid will use the SIGCHLD - * signal. It can also handle the pid == INVALID_PROCESS_ID argument. This - * is slightly more spec-compliant. - * - * But then I have to be concerned about the fact that NuttX does not queue - * signals. This means that a flurry of signals can cause signals to be - * lost (or to have the data in the struct siginfo to be overwritten by - * the next signal). + * If CONFIG_SCHED_HAVE_PARENT is defined, then waitpid() is waitid() with + * a status in place of the siginfo: the pid argument gives the idtype and + * the id, and WEXITED is always set. * ****************************************************************************/ pid_t nxsched_waitpid(pid_t pid, FAR int *stat_loc, int options) { - FAR struct tcb_s *rtcb = this_task(); - FAR struct tcb_s *ctcb; - bool retains = false; - irqstate_t flags; - int ret = OK; - - /* NOTE: sched_lock() is not enough for SMP - * because the child task is running on another CPU - */ + siginfo_t info; + idtype_t idtype; + pid_t id; + int ret; - flags = enter_critical_section(); - - /* Verify that this task actually has children and that the requested PID - * is actually a child of this task. - */ - -#ifdef CONFIG_SCHED_CHILD_STATUS - /* Does this task retain child status? */ - - retains = ((rtcb->group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0); - - if (rtcb->group->tg_children == NULL && retains) + if (pid == INVALID_PROCESS_ID) { - ret = -ECHILD; + idtype = P_ALL; + id = 0; } - else if (pid != INVALID_PROCESS_ID) + else if (pid > 0) { - /* Get the TCB corresponding to this PID. NOTE: If the child has - * already exited, then the PID will not map to a valid TCB. - */ - - ctcb = nxsched_get_tcb(pid); - if (ctcb && ctcb->group) - { - /* Make sure that the thread it is our child. */ - - if (ctcb->group->tg_ppid != rtcb->group->tg_pid) - { - ret = -ECHILD; - } - } - - /* The child task is ours or it is no longer active. Does the parent - * task retain child status? - */ + idtype = P_PID; + id = pid; + } + else + { + /* 0 is the process group of the caller */ - if (retains && ret == OK) - { - /* Yes.. Check if this specific pid has allocated child status? */ + idtype = P_PGID; + id = -pid; + } - if (group_find_child(rtcb->group, pid) == NULL) - { - ret = -ECHILD; - } - } + ret = nxsched_waitid(idtype, id, &info, options | WEXITED); + if (ret < 0) + { + return ret; } -#else /* CONFIG_SCHED_CHILD_STATUS */ + /* WNOHANG and no status */ - if (rtcb->group->tg_nchildren == 0) + if (info.si_pid == 0) { - /* There are no children */ - - ret = -ECHILD; + return 0; } - else if (pid != INVALID_PROCESS_ID) + + if (stat_loc != NULL) { - /* Get the TCB corresponding to this PID and make sure that the - * thread it is our child. - */ - - ctcb = nxsched_get_tcb(pid); - if (!ctcb || !ctcb->group || - ctcb->group->tg_ppid != rtcb->group->tg_pid || - (ctcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + if (info.si_code == CLD_STOPPED) { - ret = -ECHILD; + *stat_loc = WAIT_STOPPED(info.si_status); + } + else if (info.si_code == CLD_CONTINUED) + { + *stat_loc = WAIT_CONTINUED; + } + else + { + *stat_loc = info.si_status << 8; } } -#endif /* CONFIG_SCHED_CHILD_STATUS */ - - if (ret == OK) - { - ret = waittcb(rtcb, pid, options, stat_loc, retains); - } - - leave_critical_section(flags); - return ret; + return info.si_pid; } #else pid_t nxsched_waitpid(pid_t pid, FAR int *stat_loc, int options) diff --git a/sched/signal/sig_default.c b/sched/signal/sig_default.c index ea556541190..d93eaccd874 100644 --- a/sched/signal/sig_default.c +++ b/sched/signal/sig_default.c @@ -288,7 +288,7 @@ static void nxsig_abnormal_termination(int signo) static void nxsig_stop_task(int signo) { FAR struct tcb_s *rtcb = this_task(); -#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT) +#if defined(CONFIG_SCHED_WAITPID) || defined(CONFIG_SCHED_HAVE_PARENT) FAR struct task_group_s *group; DEBUGASSERT(rtcb != NULL && rtcb->group != NULL); @@ -314,6 +314,15 @@ static void nxsig_stop_task(int signo) sched_lock(); +#ifdef CONFIG_SCHED_HAVE_PARENT + /* Tell the parent that the process stopped: SIGCHLD with CLD_STOPPED, + * and waitpid() with WUNTRACED. SIGCONT tells it that the process + * continued (nxsig_tcbdispatch()). + */ + + nxsig_notify_parent(group->tg_pid, CLD_STOPPED, signo); +#endif + #if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT) /* Notify via waitpid if any parent is waiting for this task to EXIT * or STOP. This action is only performed if WUNTRACED is set in the @@ -324,11 +333,11 @@ static void nxsig_stop_task(int signo) { int semvalue; - /* Return zero for exit status (we are not exiting, however) */ + /* Return the status of a stopped process (WIFSTOPPED()) */ if (group->tg_statloc != NULL) { - *group->tg_statloc = 0; + *group->tg_statloc = WAIT_STOPPED(signo); group->tg_statloc = NULL; } @@ -614,3 +623,120 @@ int nxsig_default_initialize(FAR struct tcb_s *tcb) return OK; } + +/**************************************************************************** + * Name: nxsig_notify_parent + * + * Description: + * Tell the parent of a process that the process stopped (CLD_STOPPED) or + * continued (CLD_CONTINUED). The child status of the parent keeps the + * event for waitpid() and waitid(), and the parent gets SIGCHLD. If the + * SIGCHLD action of the parent has SA_NOCLDSTOP, the parent gets no + * SIGCHLD, but its threads that wait in waitpid() or waitid() wake up. + * CLD_STOPPED also sets GROUP_FLAG_STOPPED of the process. + * + * Input Parameters: + * pid - The process ID of the child + * code - CLD_STOPPED or CLD_CONTINUED + * signo - The signal that stopped the child, or SIGCONT + * + * Returned Value: + * None + * + * Assumptions: + * Not called from a critical section: the SIGCHLD handler of the calling + * thread can run before the function returns. + * + ****************************************************************************/ + +#if defined(CONFIG_SIG_SIGSTOP_ACTION) && defined(CONFIG_SCHED_HAVE_PARENT) +void nxsig_notify_parent(pid_t pid, int code, int signo) +{ + FAR struct task_group_s *chgrp; + FAR struct task_group_s *pgrp = NULL; +#ifdef CONFIG_SCHED_CHILD_STATUS + FAR struct child_status_s *child; +#endif +#ifdef CONFIG_ENABLE_ALL_SIGNALS + FAR sigactq_t *sigact; +#endif + bool nocldstop = false; + irqstate_t flags; + siginfo_t info; + pid_t ppid = INVALID_PROCESS_ID; + + flags = enter_critical_section(); + + chgrp = task_getgroup(pid); + if (chgrp != NULL) + { + /* SIGCONT reports only the continue of a stopped process */ + + if (code == CLD_STOPPED) + { + chgrp->tg_flags |= GROUP_FLAG_STOPPED; + } + + ppid = chgrp->tg_ppid; + pgrp = task_getgroup(ppid); + } + + if (pgrp == NULL) + { + /* The process ended, or it has no parent */ + + leave_critical_section(flags); + return; + } + +#ifdef CONFIG_SCHED_CHILD_STATUS + /* Keep the event for waitpid() and waitid(). It replaces an event of + * the other type that was not reported. + */ + + child = group_find_child(pgrp, pid); + if (child != NULL) + { + child->ch_flags &= ~(CHILD_FLAG_STOPPED | CHILD_FLAG_CONTINUED); + if (code == CLD_STOPPED) + { + child->ch_flags |= CHILD_FLAG_STOPPED; + child->ch_stopsig = signo; + } + else + { + child->ch_flags |= CHILD_FLAG_CONTINUED; + } + } +#endif + + info.si_signo = SIGCHLD; + info.si_code = code; + info.si_errno = OK; + info.si_value.sival_ptr = NULL; + info.si_pid = pid; +#ifdef CONFIG_SCHED_USER_IDENTITY + info.si_uid = chgrp->tg_uid; +#else + info.si_uid = 0; +#endif + info.si_status = signo; + +#ifdef CONFIG_ENABLE_ALL_SIGNALS + sigact = nxsig_find_action(pgrp, SIGCHLD); + nocldstop = sigact != NULL && (sigact->act.sa_flags & SA_NOCLDSTOP) != 0; +#endif + + if (nocldstop) + { + nxsig_wake_waiters(pgrp, &info); + } + + leave_critical_section(flags); + + if (!nocldstop) + { + nxsig_dispatch(ppid, &info, false, NULL, false); + } +} +#endif diff --git a/sched/signal/sig_dispatch.c b/sched/signal/sig_dispatch.c index 010d479a070..40cc3da280b 100644 --- a/sched/signal/sig_dispatch.c +++ b/sched/signal/sig_dispatch.c @@ -564,6 +564,58 @@ static bool nxsig_queue_full(FAR struct tcb_s *stcb) return count >= SIGQUEUE_MAX; } +/**************************************************************************** + * Name: nxsig_wake_waiter + * + * Description: + * Wake up the thread pid if it waits for the signal of arg (a siginfo), + * and give it the signal information. A function of + * nxsig_wake_waiters(). + * + * Assumptions: + * Called in a critical section with the scheduler locked. + * + ****************************************************************************/ + +#ifdef CONFIG_SIG_SIGSTOP_ACTION +static int nxsig_wake_waiter(pid_t pid, FAR void *arg) +{ + FAR siginfo_t *info = arg; + FAR struct tcb_s *rtcb = this_task(); + FAR struct tcb_s *tcb = nxsched_get_tcb(pid); + + if (tcb != NULL && tcb->task_state == TSTATE_WAIT_SIG && + nxsig_ismember(&tcb->sigwaitmask, info->si_signo) == 1) + { + if (tcb->sigunbinfo != NULL) + { + memcpy(tcb->sigunbinfo, info, sizeof(siginfo_t)); + } + + sigemptyset(&tcb->sigwaitmask); + if (WDOG_ISACTIVE(&tcb->waitdog)) + { + wd_cancel(&tcb->waitdog); + } + + /* Remove the task from waiting list */ + + dq_rem((FAR dq_entry_t *)tcb, list_waitingforsignal()); + + /* Add the task to ready-to-run task list and + * perform the context switch if one is needed + */ + + if (nxsched_add_readytorun(tcb)) + { + up_switch_context(this_task(), rtcb); + } + } + + return 0; +} +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -609,6 +661,9 @@ int nxsig_tcbdispatch(FAR struct tcb_s *stcb, FAR siginfo_t *info, #ifdef CONFIG_ENABLE_ALL_SIGNALS FAR sigactq_t *sigact; bool deliver = false; +#endif +#if defined(CONFIG_SIG_SIGSTOP_ACTION) && defined(CONFIG_SCHED_HAVE_PARENT) + pid_t contpid = INVALID_PROCESS_ID; #endif bool ignored = false; @@ -899,11 +954,12 @@ int nxsig_tcbdispatch(FAR struct tcb_s *stcb, FAR siginfo_t *info, #ifdef CONFIG_SIG_SIGSTOP_ACTION /* If the task was stopped by SIGSTOP or SIGTSTP, then unblock the task - * if SIGCONT is received. + * if SIGCONT is received. SIGKILL also unblocks it, so that it can + * end. */ else if (stcb->task_state == TSTATE_TASK_STOPPED && - info->si_signo == SIGCONT) + (info->si_signo == SIGCONT || info->si_signo == SIGKILL)) { #ifdef HAVE_GROUP_MEMBERS group_continue(stcb); @@ -923,6 +979,19 @@ int nxsig_tcbdispatch(FAR struct tcb_s *stcb, FAR siginfo_t *info, #endif } #endif + +#if defined(CONFIG_SIG_SIGSTOP_ACTION) && defined(CONFIG_SCHED_HAVE_PARENT) + /* SIGCONT continues a stopped process: tell the parent when the + * critical section is free (its SIGCHLD handler can run). + */ + + if (info->si_signo == SIGCONT && + (stcb->group->tg_flags & GROUP_FLAG_STOPPED) != 0) + { + stcb->group->tg_flags &= ~GROUP_FLAG_STOPPED; + contpid = stcb->group->tg_pid; + } +#endif } leave_critical_section(flags); @@ -948,6 +1017,13 @@ int nxsig_tcbdispatch(FAR struct tcb_s *stcb, FAR siginfo_t *info, } #endif +#if defined(CONFIG_SIG_SIGSTOP_ACTION) && defined(CONFIG_SCHED_HAVE_PARENT) + if (contpid != INVALID_PROCESS_ID) + { + nxsig_notify_parent(contpid, CLD_CONTINUED, SIGCONT); + } +#endif + /* In case nxsig_ismember failed due to an invalid signal number */ if (masked < 0) @@ -1084,3 +1160,39 @@ bool nxsig_source_queued(pid_t pid, FAR const void *source, int signo) leave_critical_section(flags); return found; } + +/**************************************************************************** + * Name: nxsig_wake_waiters + * + * Description: + * Wake up each thread of a task group that waits for the signal of info + * (sigwaitinfo(), sigtimedwait(), waitpid(), waitid()), and give it info. + * The signal is not sent: it does not become pending, and no handler + * runs. The parent of a child that stopped or continued uses it when + * the SIGCHLD action of the parent has SA_NOCLDSTOP: waitpid() must + * still see the event. + * + * Input Parameters: + * group - The task group + * info - The signal information + * + * Returned Value: + * None + * + * Assumptions: + * Called from a critical section. + * + ****************************************************************************/ + +#ifdef CONFIG_SIG_SIGSTOP_ACTION +void nxsig_wake_waiters(FAR struct task_group_s *group, FAR siginfo_t *info) +{ + sched_lock(); +#ifdef HAVE_GROUP_MEMBERS + group_foreachchild(group, nxsig_wake_waiter, info); +#else + nxsig_wake_waiter(group->tg_pid, info); +#endif + sched_unlock(); +} +#endif diff --git a/sched/signal/signal.h b/sched/signal/signal.h index 8041f315461..f17cdc8a5b9 100644 --- a/sched/signal/signal.h +++ b/sched/signal/signal.h @@ -190,6 +190,9 @@ _sa_handler_t nxsig_default(FAR struct tcb_s *tcb, int signo, bool defaction); int nxsig_default_initialize(FAR struct tcb_s *tcb); #endif +#if defined(CONFIG_SIG_SIGSTOP_ACTION) && defined(CONFIG_SCHED_HAVE_PARENT) +void nxsig_notify_parent(pid_t pid, int code, int signo); +#endif /* sig_dispatch.c */ @@ -202,6 +205,10 @@ int nxsig_dispatch(pid_t pid, FAR siginfo_t *info, bool limit); bool nxsig_source_queued(pid_t pid, FAR const void *source, int signo); +#ifdef CONFIG_SIG_SIGSTOP_ACTION +void nxsig_wake_waiters(FAR struct task_group_s *group, + FAR siginfo_t *info); +#endif /* sig_notification.c */ diff --git a/sched/task/task_exithook.c b/sched/task/task_exithook.c index e0805f10968..0f70bc39229 100644 --- a/sched/task/task_exithook.c +++ b/sched/task/task_exithook.c @@ -128,6 +128,13 @@ static inline void nxtask_exitstatus(FAR struct task_group_s *group, child->ch_status = status; #ifdef CONFIG_SCHED_USER_IDENTITY child->ch_uid = ctcb->group->tg_uid; +#endif +#ifdef CONFIG_SCHED_PROCESS_GROUPS + /* waitpid() with a process group finds the child after the + * exit. + */ + + child->ch_pgid = ctcb->group->tg_pgid; #endif } } @@ -282,9 +289,12 @@ static inline void nxtask_sigchild(FAR struct tcb_s *ptcb, #endif { #ifdef CONFIG_SCHED_CHILD_STATUS - /* Save the exit status now of the main thread */ + /* Save the exit status now of the main thread. Without group + * members, the main thread is the last thread of the task group. + */ nxtask_exitstatus(ptcb->group, ctcb, status); + nxtask_groupexit(ptcb->group, ctcb->group->tg_pid); #else /* CONFIG_SCHED_CHILD_STATUS */ /* Exit status is not retained. Just decrement the number of diff --git a/sched/task/task_setup.c b/sched/task/task_setup.c index 875a7dd5ad2..0203ad2ad4c 100644 --- a/sched/task/task_setup.c +++ b/sched/task/task_setup.c @@ -304,11 +304,15 @@ static inline void nxtask_save_parent(FAR struct tcb_s *tcb, uint8_t ttype) { /* Yes.. Initialize the structure */ - child->ch_flags = ttype; - child->ch_pid = tcb->pid; - child->ch_status = 0; + child->ch_flags = ttype; + child->ch_stopsig = 0; + child->ch_pid = tcb->pid; +#ifdef CONFIG_SCHED_PROCESS_GROUPS + child->ch_pgid = 0; +#endif + child->ch_status = 0; #ifdef CONFIG_SCHED_USER_IDENTITY - child->ch_uid = 0; + child->ch_uid = 0; #endif /* Add the entry into the group's list of children */
