2013-01-07 20:35:47 +01:00
|
|
|
/****************************************************************************
|
2023-07-05 15:43:50 +02:00
|
|
|
* libs/libc/unistd/lib_vfork.c
|
2013-01-07 16:20:21 +01:00
|
|
|
*
|
2020-05-01 03:20:29 +02:00
|
|
|
* 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
|
2013-01-07 16:20:21 +01:00
|
|
|
*
|
2020-05-01 03:20:29 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-01-07 16:20:21 +01:00
|
|
|
*
|
2020-05-01 03:20:29 +02:00
|
|
|
* 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.
|
2013-01-07 16:20:21 +01:00
|
|
|
*
|
2013-01-07 20:35:47 +01:00
|
|
|
****************************************************************************/
|
2013-01-07 16:20:21 +01:00
|
|
|
|
2013-01-07 20:35:47 +01:00
|
|
|
/****************************************************************************
|
2013-01-07 16:20:21 +01:00
|
|
|
* Included Files
|
2013-01-07 20:35:47 +01:00
|
|
|
****************************************************************************/
|
2013-01-07 16:20:21 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-07-05 15:43:50 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2013-01-07 16:20:21 +01:00
|
|
|
|
2023-07-05 15:43:50 +02:00
|
|
|
#if defined(CONFIG_ARCH_HAVE_FORK) && defined(CONFIG_SCHED_WAITPID)
|
2013-01-15 16:40:18 +01:00
|
|
|
|
2023-07-05 15:43:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2013-01-07 16:20:21 +01:00
|
|
|
|
2013-01-07 20:35:47 +01:00
|
|
|
/****************************************************************************
|
2023-07-05 15:43:50 +02:00
|
|
|
* Name: vfork
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The vfork() function is implemented based on fork() function, on
|
|
|
|
* vfork(), the parent task need to wait until the child task is performing
|
|
|
|
* exec or running finished.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Upon successful completion, vfork() returns 0 to the child process and
|
|
|
|
* returns the process ID of the child process to the parent process.
|
|
|
|
* Otherwise, -1 is returned to the parent, no child process is created,
|
|
|
|
* and errno is set to indicate the error.
|
|
|
|
*
|
2013-01-07 20:35:47 +01:00
|
|
|
****************************************************************************/
|
2013-01-07 16:20:21 +01:00
|
|
|
|
2023-07-05 15:43:50 +02:00
|
|
|
pid_t vfork(void)
|
2013-01-07 20:35:47 +01:00
|
|
|
{
|
2023-07-05 15:43:50 +02:00
|
|
|
int status = 0;
|
|
|
|
int ret;
|
|
|
|
pid_t pid = fork();
|
2013-01-15 16:40:18 +01:00
|
|
|
|
2023-07-05 15:43:50 +02:00
|
|
|
if (pid != 0)
|
|
|
|
{
|
|
|
|
/* we are in parent task, and we need to wait the child task
|
|
|
|
* until running finished or performing exec
|
|
|
|
*/
|
2013-01-15 16:40:18 +01:00
|
|
|
|
2023-08-16 05:45:04 +02:00
|
|
|
ret = waitpid(pid, &status, WNOWAIT);
|
2023-07-05 15:43:50 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-09-07 15:28:01 +02:00
|
|
|
serr("ERROR: waitpid failed: %d\n", get_errno());
|
2023-07-05 15:43:50 +02:00
|
|
|
}
|
|
|
|
}
|
2013-01-15 16:40:18 +01:00
|
|
|
|
2023-07-05 15:43:50 +02:00
|
|
|
return pid;
|
|
|
|
}
|
2013-01-07 16:20:21 +01:00
|
|
|
|
2023-07-05 15:43:50 +02:00
|
|
|
#endif /* CONFIG_ARCH_HAVE_FORK && CONFIG_SCHED_WAITPID */
|