sched: add nx_wait, nx_waitid and nx_waitpid
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
parent
390f9a5fb7
commit
de2a9d8a77
@ -1260,6 +1260,16 @@ int nxsched_setaffinity(pid_t pid, size_t cpusetsize,
|
||||
|
||||
int sched_get_stackinfo(pid_t pid, FAR struct stackinfo_s *stackinfo);
|
||||
|
||||
/********************************************************************************
|
||||
* Name: nx_wait/nx_waitid/nx_waitpid
|
||||
********************************************************************************/
|
||||
|
||||
#ifdef CONFIG_SCHED_WAITPID
|
||||
pid_t nx_wait(FAR int *stat_loc);
|
||||
int nx_waitid(int idtype, id_t id, FAR siginfo_t *info, int options);
|
||||
pid_t nx_waitpid(pid_t pid, FAR int *stat_loc, int options);
|
||||
#endif
|
||||
|
||||
#undef EXTERN
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
|
@ -53,6 +53,15 @@
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nx_wait
|
||||
****************************************************************************/
|
||||
|
||||
pid_t nx_wait(FAR int *stat_loc)
|
||||
{
|
||||
return nx_waitpid((pid_t)-1, stat_loc, 0);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: wait
|
||||
*
|
||||
@ -85,7 +94,7 @@ pid_t wait(FAR int *stat_loc)
|
||||
* trivial case.
|
||||
*/
|
||||
|
||||
return waitpid((pid_t) - 1, stat_loc, 0);
|
||||
return waitpid((pid_t)-1, stat_loc, 0);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SCHED_WAITPID && CONFIG_SCHED_HAVE_PARENT */
|
||||
|
@ -93,69 +93,10 @@ static void exited_child(FAR struct tcb_s *rtcb,
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: waitid
|
||||
*
|
||||
* Description:
|
||||
* The waitid() function suspends the calling thread until one child of
|
||||
* the process containing the calling thread changes state. It records the
|
||||
* current state of a child in the structure pointed to by 'info'. If a
|
||||
* child process changed state prior to the call to waitid(), waitid()
|
||||
* returns immediately. If more than one thread is suspended in wait() or
|
||||
* waitpid() waiting termination of the same process, exactly one thread
|
||||
* will return the process status at the time of the target process
|
||||
* termination
|
||||
*
|
||||
* The idtype and id arguments are used to specify which children waitid()
|
||||
* will wait for.
|
||||
*
|
||||
* If idtype is P_PID, waitid() will wait for the child with a process
|
||||
* ID equal to (pid_t)id.
|
||||
*
|
||||
* If idtype is P_PGID, waitid() will wait for any child with a process
|
||||
* group ID equal to (pid_t)id.
|
||||
*
|
||||
* If idtype is P_ALL, waitid() will wait for any children and id is
|
||||
* ignored.
|
||||
*
|
||||
* The options argument is used to specify which state changes waitid()
|
||||
* will will wait for. It is formed by OR-ing together one or more of the
|
||||
* following flags:
|
||||
*
|
||||
* WEXITED - Wait for processes that have exited.
|
||||
* WSTOPPED - Status will be returned for any child that has stopped
|
||||
* upon receipt of a signal.
|
||||
* WCONTINUED - Status will be returned for any child that was stopped
|
||||
* and has been continued.
|
||||
* WNOHANG - Return immediately if there are no children to wait for.
|
||||
* WNOWAIT - Keep the process whose status is returned in 'info' in a
|
||||
* waitable state. This will not affect the state of the process; the
|
||||
* process may be waited for again after this call completes.
|
||||
*
|
||||
* The 'info' argument must point to a siginfo_t structure. If waitid()
|
||||
* returns because a child process was found that satisfied the conditions
|
||||
* indicated by the arguments idtype and options, then the structure
|
||||
* pointed to by 'info' will be filled in by the system with the status of
|
||||
* the process. The si_signo member will always be equal to SIGCHLD.
|
||||
*
|
||||
* Input Parameters:
|
||||
* See description.
|
||||
*
|
||||
* Returned Value:
|
||||
* If waitid() returns due to the change of state of one of its children,
|
||||
* 0 is returned. Otherwise, -1 is returned and errno is set to indicate
|
||||
* the error.
|
||||
*
|
||||
* The waitid() function will fail if:
|
||||
*
|
||||
* ECHILD - The calling process has no existing unwaited-for child
|
||||
* processes.
|
||||
* EINTR - The waitid() function was interrupted by a signal.
|
||||
* EINVAL - An invalid value was specified for options, or idtype and id
|
||||
* specify an invalid set of processes.
|
||||
*
|
||||
* Name: nx_waitid
|
||||
****************************************************************************/
|
||||
|
||||
int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
int nx_waitid(int idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
{
|
||||
FAR struct tcb_s *rtcb = this_task();
|
||||
FAR struct tcb_s *ctcb;
|
||||
@ -164,8 +105,7 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
bool retains;
|
||||
#endif
|
||||
sigset_t set;
|
||||
int errcode;
|
||||
int ret;
|
||||
int ret = OK;
|
||||
|
||||
/* MISSING LOGIC: If WNOHANG is provided in the options, then this
|
||||
* function should returned immediately. However, there is no mechanism
|
||||
@ -179,8 +119,7 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
|
||||
if (idtype != P_PID && idtype != P_ALL)
|
||||
{
|
||||
set_errno(ENOSYS);
|
||||
return ERROR;
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
/* None of the options are supported except for WEXITED (which must be
|
||||
@ -190,15 +129,10 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
|
||||
if (options != WEXITED)
|
||||
{
|
||||
set_errno(ENOSYS);
|
||||
return ERROR;
|
||||
return -ENOSYS;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* waitid() is a cancellation point */
|
||||
|
||||
enter_cancellation_point();
|
||||
|
||||
/* Create a signal set that contains only SIGCHLD */
|
||||
|
||||
sigemptyset(&set);
|
||||
@ -221,8 +155,8 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
{
|
||||
/* There are no children */
|
||||
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
else if (idtype == P_PID)
|
||||
{
|
||||
@ -238,8 +172,8 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
if (ctcb == NULL || ctcb->group->tg_ppid != rtcb->pid)
|
||||
#endif
|
||||
{
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Does this task retain child status? */
|
||||
@ -252,8 +186,8 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
{
|
||||
/* This specific pid is not a child */
|
||||
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -264,8 +198,8 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
{
|
||||
/* There are no children */
|
||||
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
else if (idtype == P_PID)
|
||||
{
|
||||
@ -281,8 +215,8 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
if (ctcb == NULL || ctcb->group->tg_ppid != rtcb->pid)
|
||||
#endif
|
||||
{
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -353,8 +287,8 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
* to reported ECHILD than bogus status.
|
||||
*/
|
||||
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
#else
|
||||
@ -372,8 +306,8 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
* Let's return ECHILD.. that is at least informative.
|
||||
*/
|
||||
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -382,8 +316,7 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
ret = nxsig_waitinfo(&set, info);
|
||||
if (ret < 0)
|
||||
{
|
||||
errcode = -ret;
|
||||
goto errout_with_errno;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Make there this was SIGCHLD */
|
||||
@ -417,22 +350,97 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
|
||||
else /* if (idtype == P_PGID) */
|
||||
{
|
||||
errcode = ENOSYS;
|
||||
goto errout_with_errno;
|
||||
ret = -ENOSYS;
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
leave_cancellation_point();
|
||||
errout:
|
||||
sched_unlock();
|
||||
return OK;
|
||||
return ret;
|
||||
}
|
||||
|
||||
errout_with_errno:
|
||||
set_errno(errcode);
|
||||
/****************************************************************************
|
||||
* Name: waitid
|
||||
*
|
||||
* Description:
|
||||
* The waitid() function suspends the calling thread until one child of
|
||||
* the process containing the calling thread changes state. It records the
|
||||
* current state of a child in the structure pointed to by 'info'. If a
|
||||
* child process changed state prior to the call to waitid(), waitid()
|
||||
* returns immediately. If more than one thread is suspended in wait() or
|
||||
* waitpid() waiting termination of the same process, exactly one thread
|
||||
* will return the process status at the time of the target process
|
||||
* termination
|
||||
*
|
||||
* The idtype and id arguments are used to specify which children waitid()
|
||||
* will wait for.
|
||||
*
|
||||
* If idtype is P_PID, waitid() will wait for the child with a process
|
||||
* ID equal to (pid_t)id.
|
||||
*
|
||||
* If idtype is P_PGID, waitid() will wait for any child with a process
|
||||
* group ID equal to (pid_t)id.
|
||||
*
|
||||
* If idtype is P_ALL, waitid() will wait for any children and id is
|
||||
* ignored.
|
||||
*
|
||||
* The options argument is used to specify which state changes waitid()
|
||||
* will will wait for. It is formed by OR-ing together one or more of the
|
||||
* following flags:
|
||||
*
|
||||
* WEXITED - Wait for processes that have exited.
|
||||
* WSTOPPED - Status will be returned for any child that has stopped
|
||||
* upon receipt of a signal.
|
||||
* WCONTINUED - Status will be returned for any child that was stopped
|
||||
* and has been continued.
|
||||
* WNOHANG - Return immediately if there are no children to wait for.
|
||||
* WNOWAIT - Keep the process whose status is returned in 'info' in a
|
||||
* waitable state. This will not affect the state of the process; the
|
||||
* process may be waited for again after this call completes.
|
||||
*
|
||||
* The 'info' argument must point to a siginfo_t structure. If waitid()
|
||||
* returns because a child process was found that satisfied the conditions
|
||||
* indicated by the arguments idtype and options, then the structure
|
||||
* pointed to by 'info' will be filled in by the system with the status of
|
||||
* the process. The si_signo member will always be equal to SIGCHLD.
|
||||
*
|
||||
* Input Parameters:
|
||||
* See description.
|
||||
*
|
||||
* Returned Value:
|
||||
* If waitid() returns due to the change of state of one of its children,
|
||||
* 0 is returned. Otherwise, -1 is returned and errno is set to indicate
|
||||
* the error.
|
||||
*
|
||||
* The waitid() function will fail if:
|
||||
*
|
||||
* ECHILD - The calling process has no existing unwaited-for child
|
||||
* processes.
|
||||
* EINTR - The waitid() function was interrupted by a signal.
|
||||
* EINVAL - An invalid value was specified for options, or idtype and id
|
||||
* specify an invalid set of processes.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* waitid() is a cancellation point */
|
||||
|
||||
enter_cancellation_point();
|
||||
|
||||
ret = nx_waitid(idtype, id, info, options);
|
||||
if (ret < 0)
|
||||
{
|
||||
set_errno(-ret);
|
||||
ret = ERROR;
|
||||
}
|
||||
|
||||
leave_cancellation_point();
|
||||
sched_unlock();
|
||||
return ERROR;
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SCHED_WAITPID && CONFIG_SCHED_HAVE_PARENT */
|
||||
|
@ -58,6 +58,389 @@
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nx_waitpid
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_SCHED_HAVE_PARENT
|
||||
pid_t nx_waitpid(pid_t pid, int *stat_loc, int options)
|
||||
{
|
||||
FAR struct tcb_s *ctcb;
|
||||
FAR struct task_group_s *group;
|
||||
bool mystat = false;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(stat_loc);
|
||||
|
||||
/* Disable pre-emption so that nothing changes in the following tests */
|
||||
|
||||
sched_lock();
|
||||
|
||||
/* Get the TCB corresponding to this PID */
|
||||
|
||||
ctcb = sched_gettcb(pid);
|
||||
if (ctcb == NULL)
|
||||
{
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Then the task group corresponding to this PID */
|
||||
|
||||
group = ctcb->group;
|
||||
DEBUGASSERT(group);
|
||||
|
||||
/* Lock this group so that it cannot be deleted until the wait completes */
|
||||
|
||||
group_addwaiter(group);
|
||||
|
||||
/* "If more than one thread is suspended in waitpid() awaiting termination
|
||||
* of the same process, exactly one thread will return the process status
|
||||
* at the time of the target process termination." Hmmm.. what do we
|
||||
* return to the others?
|
||||
*/
|
||||
|
||||
if (group->tg_waitflags == 0)
|
||||
{
|
||||
/* Save the waitpid() options, setting the non-standard WCLAIMED bit to
|
||||
* assure that tg_waitflags is non-zero.
|
||||
*/
|
||||
|
||||
group->tg_waitflags = (uint8_t)options | WCLAIMED;
|
||||
|
||||
/* Save the return status location (which may be NULL) */
|
||||
|
||||
group->tg_statloc = stat_loc;
|
||||
|
||||
/* We are the waipid() instance that gets the return status */
|
||||
|
||||
mystat = true;
|
||||
}
|
||||
|
||||
/* Then wait for the task to exit */
|
||||
|
||||
if ((options & WNOHANG) != 0)
|
||||
{
|
||||
/* Don't wait if status is not available */
|
||||
|
||||
ret = nxsem_trywait(&group->tg_exitsem);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Wait if necessary for status to become available */
|
||||
|
||||
ret = nxsem_wait(&group->tg_exitsem);
|
||||
}
|
||||
|
||||
group_delwaiter(group);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
/* Handle the awkward case of whether or not we
|
||||
* need to nullify the stat_loc value.
|
||||
*/
|
||||
|
||||
if (mystat)
|
||||
{
|
||||
group->tg_statloc = NULL;
|
||||
group->tg_waitflags = 0;
|
||||
}
|
||||
|
||||
if ((options & WNOHANG) != 0)
|
||||
{
|
||||
pid = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
/* On success, return the PID */
|
||||
|
||||
sched_unlock();
|
||||
return pid;
|
||||
|
||||
errout:
|
||||
sched_unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
*
|
||||
* If CONFIG_SCHED_HAVE_PARENT is defined, then waitpid will use the SIGHCLD
|
||||
* signal. It can also handle the pid == (pid_t)-1 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).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#else
|
||||
pid_t nx_waitpid(pid_t pid, int *stat_loc, int options)
|
||||
{
|
||||
FAR struct tcb_s *rtcb = this_task();
|
||||
FAR struct tcb_s *ctcb;
|
||||
#ifdef CONFIG_SCHED_CHILD_STATUS
|
||||
FAR struct child_status_s *child = NULL;
|
||||
bool retains;
|
||||
#endif
|
||||
FAR struct siginfo info;
|
||||
sigset_t set;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(stat_loc);
|
||||
|
||||
/* Create a signal set that contains only SIGCHLD */
|
||||
|
||||
sigemptyset(&set);
|
||||
nxsig_addset(&set, SIGCHLD);
|
||||
|
||||
/* Disable pre-emption so that nothing changes while the loop executes */
|
||||
|
||||
sched_lock();
|
||||
|
||||
/* 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)
|
||||
{
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
else if (pid != (pid_t)-1)
|
||||
{
|
||||
/* 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 = sched_gettcb(pid);
|
||||
if (ctcb != NULL)
|
||||
{
|
||||
/* Make sure that the thread it is our child. */
|
||||
|
||||
#ifdef HAVE_GROUP_MEMBERS
|
||||
if (ctcb->group->tg_pgrpid != rtcb->group->tg_grpid)
|
||||
#else
|
||||
if (ctcb->group->tg_ppid != rtcb->pid)
|
||||
#endif
|
||||
{
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
/* The child task is ours or it is no longer active. Does the parent
|
||||
* task retain child status?
|
||||
*/
|
||||
|
||||
if (retains)
|
||||
{
|
||||
/* Yes.. Check if this specific pid has allocated child status? */
|
||||
|
||||
if (group_findchild(rtcb->group, pid) == NULL)
|
||||
{
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
if (rtcb->group->tg_nchildren == 0)
|
||||
{
|
||||
/* There are no children */
|
||||
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
else if (pid != (pid_t)-1)
|
||||
{
|
||||
/* Get the TCB corresponding to this PID and make sure that the
|
||||
* thread it is our child.
|
||||
*/
|
||||
|
||||
ctcb = sched_gettcb(pid);
|
||||
|
||||
#ifdef HAVE_GROUP_MEMBERS
|
||||
if (ctcb == NULL || ctcb->group->tg_pgrpid != rtcb->group->tg_grpid)
|
||||
#else
|
||||
if (ctcb == NULL || ctcb->group->tg_ppid != rtcb->pid)
|
||||
#endif
|
||||
{
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
/* Loop until the child that we are waiting for dies */
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
#ifdef 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 (pid == (pid_t)-1)
|
||||
{
|
||||
/* We are waiting for any child, check if there are still
|
||||
* children.
|
||||
*/
|
||||
|
||||
DEBUGASSERT(!retains || rtcb->group->tg_children);
|
||||
if (retains && (child = group_exitchild(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 */
|
||||
|
||||
*stat_loc = child->ch_status << 8;
|
||||
|
||||
/* Discard the child entry and break out of the loop */
|
||||
|
||||
group_removechild(rtcb->group, child->ch_pid);
|
||||
group_freechild(child);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* We are waiting for a specific PID. Does this task retain child
|
||||
* status?
|
||||
*/
|
||||
|
||||
else if (retains)
|
||||
{
|
||||
/* Get the current status of the child task. */
|
||||
|
||||
child = group_findchild(rtcb->group, pid);
|
||||
DEBUGASSERT(child);
|
||||
|
||||
/* Did the child exit? */
|
||||
|
||||
if ((child->ch_flags & CHILD_FLAG_EXITED) != 0)
|
||||
{
|
||||
/* The child has exited. Return the saved exit status */
|
||||
|
||||
*stat_loc = child->ch_status << 8;
|
||||
|
||||
/* Discard the child entry and break out of the loop */
|
||||
|
||||
group_removechild(rtcb->group, pid);
|
||||
group_freechild(child);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* We can use nxsig_kill() with signal number 0 to determine if
|
||||
* that task is still alive.
|
||||
*/
|
||||
|
||||
ret = nxsig_kill(pid, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* It is no longer running. We know that the child task
|
||||
* was running okay when we started, so we must have lost
|
||||
* the signal. In this case, we know that the task exit'ed,
|
||||
* but we do not know its exit status. It would be better
|
||||
* to reported ECHILD than bogus status.
|
||||
*/
|
||||
|
||||
ret = -ECHILD;
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
#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 != (pid_t)-1 && (ret = nxsig_kill(pid, 0)) < 0))
|
||||
{
|
||||
/* 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;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
/* Wait for any death-of-child signal */
|
||||
|
||||
ret = nxsig_waitinfo(&set, &info);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Was this the death of the thread we were waiting for? In the of
|
||||
* pid == (pid_t)-1, we are waiting for any child thread.
|
||||
*/
|
||||
|
||||
if (info.si_signo == SIGCHLD &&
|
||||
(pid == (pid_t)-1 || info.si_pid == pid))
|
||||
{
|
||||
/* Yes... return the status and PID (in the event it was -1) */
|
||||
|
||||
*stat_loc = info.si_status << 8;
|
||||
pid = info.si_pid;
|
||||
|
||||
#ifdef CONFIG_SCHED_CHILD_STATUS
|
||||
if (retains)
|
||||
{
|
||||
/* Recover the exiting child */
|
||||
|
||||
child = group_exitchild(rtcb->group);
|
||||
DEBUGASSERT(child != NULL);
|
||||
|
||||
/* Discard the child entry, if we have one */
|
||||
|
||||
if (child != NULL)
|
||||
{
|
||||
group_removechild(rtcb->group, child->ch_pid);
|
||||
group_freechild(child);
|
||||
}
|
||||
}
|
||||
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sched_unlock();
|
||||
return pid;
|
||||
|
||||
errout:
|
||||
sched_unlock();
|
||||
return ret;
|
||||
}
|
||||
#endif /* CONFIG_SCHED_HAVE_PARENT */
|
||||
|
||||
/****************************************************************************
|
||||
* Name: waitpid
|
||||
*
|
||||
@ -182,402 +565,25 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_SCHED_HAVE_PARENT
|
||||
pid_t waitpid(pid_t pid, int *stat_loc, int options)
|
||||
{
|
||||
FAR struct tcb_s *ctcb;
|
||||
FAR struct task_group_s *group;
|
||||
bool mystat = false;
|
||||
int errcode;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(stat_loc);
|
||||
pid_t ret;
|
||||
|
||||
/* waitpid() is a cancellation point */
|
||||
|
||||
enter_cancellation_point();
|
||||
|
||||
/* Disable pre-emption so that nothing changes in the following tests */
|
||||
/* Let nx_waitpid() do the work. */
|
||||
|
||||
sched_lock();
|
||||
|
||||
/* Get the TCB corresponding to this PID */
|
||||
|
||||
ctcb = sched_gettcb(pid);
|
||||
if (ctcb == NULL)
|
||||
ret = nx_waitpid(pid, stat_loc, options);
|
||||
if (ret < 0)
|
||||
{
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
set_errno(-ret);
|
||||
ret = ERROR;
|
||||
}
|
||||
|
||||
/* Then the task group corresponding to this PID */
|
||||
|
||||
group = ctcb->group;
|
||||
DEBUGASSERT(group);
|
||||
|
||||
/* Lock this group so that it cannot be deleted until the wait completes */
|
||||
|
||||
group_addwaiter(group);
|
||||
|
||||
/* "If more than one thread is suspended in waitpid() awaiting termination
|
||||
* of the same process, exactly one thread will return the process status
|
||||
* at the time of the target process termination." Hmmm.. what do we
|
||||
* return to the others?
|
||||
*/
|
||||
|
||||
if (group->tg_waitflags == 0)
|
||||
{
|
||||
/* Save the waitpid() options, setting the non-standard WCLAIMED bit to
|
||||
* assure that tg_waitflags is non-zero.
|
||||
*/
|
||||
|
||||
group->tg_waitflags = (uint8_t)options | WCLAIMED;
|
||||
|
||||
/* Save the return status location (which may be NULL) */
|
||||
|
||||
group->tg_statloc = stat_loc;
|
||||
|
||||
/* We are the waipid() instance that gets the return status */
|
||||
|
||||
mystat = true;
|
||||
}
|
||||
|
||||
/* Then wait for the task to exit */
|
||||
|
||||
if ((options & WNOHANG) != 0)
|
||||
{
|
||||
/* Don't wait if status is not available */
|
||||
|
||||
ret = nxsem_trywait(&group->tg_exitsem);
|
||||
group_delwaiter(group);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
pid = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Wait if necessary for status to become available */
|
||||
|
||||
ret = nxsem_wait(&group->tg_exitsem);
|
||||
group_delwaiter(group);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
/* Unlock pre-emption and return the ERROR (nxsem_wait has already
|
||||
* set the errno). Handle the awkward case of whether or not we
|
||||
* need to nullify the stat_loc value.
|
||||
*/
|
||||
|
||||
if (mystat)
|
||||
{
|
||||
group->tg_statloc = NULL;
|
||||
group->tg_waitflags = 0;
|
||||
}
|
||||
|
||||
errcode = -ret;
|
||||
goto errout_with_errno;
|
||||
}
|
||||
}
|
||||
|
||||
/* On success, return the PID */
|
||||
|
||||
leave_cancellation_point();
|
||||
sched_unlock();
|
||||
return pid;
|
||||
|
||||
errout_with_errno:
|
||||
set_errno(errcode);
|
||||
|
||||
leave_cancellation_point();
|
||||
sched_unlock();
|
||||
return ERROR;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
*
|
||||
* If CONFIG_SCHED_HAVE_PARENT is defined, then waitpid will use the SIGHCLD
|
||||
* signal. It can also handle the pid == (pid_t)-1 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).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#else
|
||||
pid_t waitpid(pid_t pid, int *stat_loc, int options)
|
||||
{
|
||||
FAR struct tcb_s *rtcb = this_task();
|
||||
FAR struct tcb_s *ctcb;
|
||||
#ifdef CONFIG_SCHED_CHILD_STATUS
|
||||
FAR struct child_status_s *child = NULL;
|
||||
bool retains;
|
||||
#endif
|
||||
FAR struct siginfo info;
|
||||
sigset_t set;
|
||||
int errcode;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(stat_loc);
|
||||
|
||||
/* waitpid() is a cancellation point */
|
||||
|
||||
enter_cancellation_point();
|
||||
|
||||
/* Create a signal set that contains only SIGCHLD */
|
||||
|
||||
sigemptyset(&set);
|
||||
nxsig_addset(&set, SIGCHLD);
|
||||
|
||||
/* Disable pre-emption so that nothing changes while the loop executes */
|
||||
|
||||
sched_lock();
|
||||
|
||||
/* 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)
|
||||
{
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
}
|
||||
else if (pid != (pid_t)-1)
|
||||
{
|
||||
/* 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 = sched_gettcb(pid);
|
||||
if (ctcb != NULL)
|
||||
{
|
||||
/* Make sure that the thread it is our child. */
|
||||
|
||||
#ifdef HAVE_GROUP_MEMBERS
|
||||
if (ctcb->group->tg_pgrpid != rtcb->group->tg_grpid)
|
||||
#else
|
||||
if (ctcb->group->tg_ppid != rtcb->pid)
|
||||
#endif
|
||||
{
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
}
|
||||
}
|
||||
|
||||
/* The child task is ours or it is no longer active. Does the parent
|
||||
* task retain child status?
|
||||
*/
|
||||
|
||||
if (retains)
|
||||
{
|
||||
/* Yes.. Check if this specific pid has allocated child status? */
|
||||
|
||||
if (group_findchild(rtcb->group, pid) == NULL)
|
||||
{
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
if (rtcb->group->tg_nchildren == 0)
|
||||
{
|
||||
/* There are no children */
|
||||
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
}
|
||||
else if (pid != (pid_t)-1)
|
||||
{
|
||||
/* Get the TCB corresponding to this PID and make sure that the
|
||||
* thread it is our child.
|
||||
*/
|
||||
|
||||
ctcb = sched_gettcb(pid);
|
||||
|
||||
#ifdef HAVE_GROUP_MEMBERS
|
||||
if (ctcb == NULL || ctcb->group->tg_pgrpid != rtcb->group->tg_grpid)
|
||||
#else
|
||||
if (ctcb == NULL || ctcb->group->tg_ppid != rtcb->pid)
|
||||
#endif
|
||||
{
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
/* Loop until the child that we are waiting for dies */
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
#ifdef 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 (pid == (pid_t)-1)
|
||||
{
|
||||
/* We are waiting for any child, check if there are still
|
||||
* children.
|
||||
*/
|
||||
|
||||
DEBUGASSERT(!retains || rtcb->group->tg_children);
|
||||
if (retains && (child = group_exitchild(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 */
|
||||
|
||||
*stat_loc = child->ch_status << 8;
|
||||
|
||||
/* Discard the child entry and break out of the loop */
|
||||
|
||||
group_removechild(rtcb->group, child->ch_pid);
|
||||
group_freechild(child);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* We are waiting for a specific PID. Does this task retain child
|
||||
* status?
|
||||
*/
|
||||
|
||||
else if (retains)
|
||||
{
|
||||
/* Get the current status of the child task. */
|
||||
|
||||
child = group_findchild(rtcb->group, pid);
|
||||
DEBUGASSERT(child);
|
||||
|
||||
/* Did the child exit? */
|
||||
|
||||
if ((child->ch_flags & CHILD_FLAG_EXITED) != 0)
|
||||
{
|
||||
/* The child has exited. Return the saved exit status */
|
||||
|
||||
*stat_loc = child->ch_status << 8;
|
||||
|
||||
/* Discard the child entry and break out of the loop */
|
||||
|
||||
group_removechild(rtcb->group, pid);
|
||||
group_freechild(child);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* We can use nxsig_kill() with signal number 0 to determine if
|
||||
* that task is still alive.
|
||||
*/
|
||||
|
||||
ret = nxsig_kill(pid, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* It is no longer running. We know that the child task
|
||||
* was running okay when we started, so we must have lost
|
||||
* the signal. In this case, we know that the task exit'ed,
|
||||
* but we do not know its exit status. It would be better
|
||||
* to reported ECHILD than bogus status.
|
||||
*/
|
||||
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
}
|
||||
}
|
||||
|
||||
#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 != (pid_t)-1 && (ret = nxsig_kill(pid, 0)) < 0))
|
||||
{
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
errcode = ECHILD;
|
||||
goto errout_with_errno;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
/* Wait for any death-of-child signal */
|
||||
|
||||
ret = nxsig_waitinfo(&set, &info);
|
||||
if (ret < 0)
|
||||
{
|
||||
errcode = -ret;
|
||||
goto errout_with_errno;
|
||||
}
|
||||
|
||||
/* Was this the death of the thread we were waiting for? In the of
|
||||
* pid == (pid_t)-1, we are waiting for any child thread.
|
||||
*/
|
||||
|
||||
if (info.si_signo == SIGCHLD &&
|
||||
(pid == (pid_t)-1 || info.si_pid == pid))
|
||||
{
|
||||
/* Yes... return the status and PID (in the event it was -1) */
|
||||
|
||||
*stat_loc = info.si_status << 8;
|
||||
pid = info.si_pid;
|
||||
|
||||
#ifdef CONFIG_SCHED_CHILD_STATUS
|
||||
if (retains)
|
||||
{
|
||||
/* Recover the exiting child */
|
||||
|
||||
child = group_exitchild(rtcb->group);
|
||||
DEBUGASSERT(child != NULL);
|
||||
|
||||
/* Discard the child entry, if we have one */
|
||||
|
||||
if (child != NULL)
|
||||
{
|
||||
group_removechild(rtcb->group, child->ch_pid);
|
||||
group_freechild(child);
|
||||
}
|
||||
}
|
||||
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
leave_cancellation_point();
|
||||
sched_unlock();
|
||||
return (int)pid;
|
||||
|
||||
errout_with_errno:
|
||||
set_errno(errcode);
|
||||
|
||||
leave_cancellation_point();
|
||||
sched_unlock();
|
||||
return ERROR;
|
||||
}
|
||||
#endif /* CONFIG_SCHED_HAVE_PARENT */
|
||||
|
||||
#endif /* CONFIG_SCHED_WAITPID */
|
||||
|
Loading…
Reference in New Issue
Block a user