1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:27:35 +00:00

Kernel+LibC: Add sys$disown() for disowning child processes

This syscall allows a parent process to disown a child process, setting
its parent PID to 0.

Unparented processes are automatically reaped by the kernel upon exit,
and no sys$waitid() is required. This will make it much nicer to do
spawn-and-forget which is common in the GUI environment.
This commit is contained in:
Andreas Kling 2020-08-04 13:51:11 +02:00
parent 83a4fbf548
commit 7de831efc6
7 changed files with 79 additions and 1 deletions

View file

@ -0,0 +1,25 @@
## Name
disown - disown a child process
## Synopsis
```**c++
#include <serenity.h>
int disown(pid_t pid);
```
## Description
`disown()` unparents a child process of the calling process. The child's parent PID is set to zero, which allows the kernel to automatically reap it upon death.
## Pledge
In pledged programs, the `proc` promise is required for this system call.
## Errors
* `ESRCH`: No process with PID `pid` was found.
* `ECHILD`: The target process is not a child of the calling process.

View file

@ -194,7 +194,8 @@ namespace Kernel {
S(sendfd) \
S(recvfd) \
S(sysconf) \
S(set_process_name)
S(set_process_name) \
S(disown)
namespace Syscall {

View file

@ -96,6 +96,7 @@ set(KERNEL_SOURCES
Syscalls/chroot.cpp
Syscalls/clock.cpp
Syscalls/debug.cpp
Syscalls/disown.cpp
Syscalls/dup.cpp
Syscalls/execve.cpp
Syscalls/exit.cpp

View file

@ -333,6 +333,7 @@ public:
int sys$sendfd(int sockfd, int fd);
int sys$recvfd(int sockfd);
long sys$sysconf(int name);
int sys$disown(pid_t);
template<bool sockname, typename Params>
int get_sock_or_peer_name(const Params&);

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Kernel/Process.h>
namespace Kernel {
int Process::sys$disown(pid_t pid)
{
REQUIRE_PROMISE(proc);
auto process = Process::from_pid(pid);
if (!process)
return -ESRCH;
if (process->ppid() != this->pid())
return -ECHILD;
process->m_ppid = 0;
return 0;
}
}

View file

@ -30,6 +30,12 @@
extern "C" {
int disown(pid_t pid)
{
int rc = syscall(SC_disown, pid);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int module_load(const char* path, size_t path_length)
{
int rc = syscall(SC_module_load, path, path_length);

View file

@ -31,6 +31,8 @@
__BEGIN_DECLS
int disown(pid_t);
int shbuf_create(int, void** buffer);
int shbuf_allow_pid(int, pid_t peer_pid);
int shbuf_allow_all(int);