libc: add wait4()

wait4() is BSD/Linux-standard (used by toybox's "time" applet) but NuttX
only had waitpid()+getrusage() separately. Add it to libs/libc/unistd/
built on top of those two existing primitives, so it needs no syscall
plumbing of its own and works unmodified across flat/protected/kernel
build separation. Prototype added to include/sys/wait.h.

Signed-off-by: Alan C. Assis <acassis@gmail.com>
Assisted-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Alan Carvalho de Assis
2026-08-14 10:20:52 +08:00
committed by Xiang Xiao
parent 3635004dcd
commit 3cbf6f2168
4 changed files with 81 additions and 0 deletions
+9
View File
@@ -96,6 +96,15 @@ pid_t wait(FAR int *stat_loc);
int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options); int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options);
pid_t waitpid(pid_t pid, FAR int *stat_loc, int options); pid_t waitpid(pid_t pid, FAR int *stat_loc, int options);
/* wait4() is waitpid() plus the reaped child's struct rusage in one call.
* struct rusage is forward-declared here (not #include <sys/resource.h>)
* since it is only ever used as a pointer type in this prototype.
*/
struct rusage;
pid_t wait4(pid_t pid, FAR int *stat_loc, int options,
FAR struct rusage *rusage);
#undef EXTERN #undef EXTERN
#if defined(__cplusplus) #if defined(__cplusplus)
} }
+4
View File
@@ -73,6 +73,10 @@ set(SRCS
lib_confstr.c lib_confstr.c
lib_ulimit.c) lib_ulimit.c)
if(CONFIG_SCHED_WAITPID)
list(APPEND SRCS lib_wait4.c)
endif()
if(NOT CONFIG_SCHED_USER_IDENTITY) if(NOT CONFIG_SCHED_USER_IDENTITY)
list( list(
APPEND APPEND
+4
View File
@@ -36,6 +36,10 @@ CSRCS += lib_getsid.c lib_setpgid.c lib_setsid.c
CSRCS += lib_lockf.c lib_flock.c lib_getpass.c CSRCS += lib_lockf.c lib_flock.c lib_getpass.c
CSRCS += lib_chdir.c lib_fchdir.c lib_confstr.c lib_ulimit.c CSRCS += lib_chdir.c lib_fchdir.c lib_confstr.c lib_ulimit.c
ifeq ($(CONFIG_SCHED_WAITPID),y)
CSRCS += lib_wait4.c
endif
ifneq ($(CONFIG_SCHED_USER_IDENTITY),y) ifneq ($(CONFIG_SCHED_USER_IDENTITY),y)
CSRCS += lib_setuid.c lib_setgid.c lib_getuid.c lib_getgid.c CSRCS += lib_setuid.c lib_setgid.c lib_getuid.c lib_getgid.c
CSRCS += lib_seteuid.c lib_setegid.c lib_geteuid.c lib_getegid.c CSRCS += lib_seteuid.c lib_setegid.c lib_geteuid.c lib_getegid.c
+64
View File
@@ -0,0 +1,64 @@
/****************************************************************************
* libs/libc/unistd/lib_wait4.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/wait.h>
#include <sys/resource.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: wait4
*
* Description:
* wait4() is waitpid() plus the reaped child's resource usage in one
* call. Built entirely on top of the existing waitpid()/getrusage()
* primitives (each already correctly proxied across build separation --
* waitpid() is a real syscall, getrusage() is plain libc), so this needs
* no syscall plumbing of its own and works unmodified in flat,
* protected, and kernel builds alike.
*
* NuttX's getrusage() only supports RUSAGE_SELF/RUSAGE_CHILDREN (not a
* specific pid), so the returned usage is exact only when the caller has
* a single outstanding child at a time.
*
****************************************************************************/
pid_t wait4(pid_t pid, FAR int *stat_loc, int options,
FAR struct rusage *rusage)
{
pid_t ret = waitpid(pid, stat_loc, options);
if (ret > 0 && rusage != NULL)
{
getrusage(RUSAGE_CHILDREN, rusage);
}
return ret;
}