mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-15 16:26:48 +00:00
When calling clone() and clone3(), the user is allowed to specify a signal to be sent to the parent process on exit. Respect this value by storing it in the Process struct and sending the signal on exit. Add a test as well to verify that the signal is properly delivered to the parent.
43 lines
927 B
C
43 lines
927 B
C
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
#define _GNU_SOURCE
|
|
#include <err.h>
|
|
#include <linux/sched.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/syscall.h>
|
|
#include <sys/wait.h>
|
|
|
|
static pid_t sys_clone3(struct clone_args *args)
|
|
{
|
|
return syscall(SYS_clone3, args, sizeof(struct clone_args));
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
pid_t pid;
|
|
struct clone_args args = {
|
|
.exit_signal = 0,
|
|
};
|
|
|
|
pid = sys_clone3(&args);
|
|
if (pid < 0)
|
|
err(EXIT_FAILURE, "Failed to create new process");
|
|
|
|
if (pid == 0) {
|
|
printf("Child process with pid %d\n", getpid());
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
/*
|
|
* From the clone(2) manual:
|
|
* If [the exit signal] is specified as anything other than SIGCHLD,
|
|
* then the parent process must specify the __WALL or __WCLONE
|
|
* options when waiting for the child with wait(2).
|
|
*/
|
|
waitpid(pid, NULL, __WALL);
|
|
|
|
/* We should have gotten this far without receiving any signals */
|
|
return 0;
|
|
}
|