[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20191025094956.hvr44v2lbfxf7dfs@yavin.dot.cyphar.com>
Date: Fri, 25 Oct 2019 20:49:56 +1100
From: Aleksa Sarai <cyphar@...har.com>
To: Christian Brauner <christian.brauner@...ntu.com>
Cc: "Michael Kerrisk (man-pages)" <mtk.manpages@...il.com>,
Christian Brauner <christian@...uner.io>,
Linux API <linux-api@...r.kernel.org>,
lkml <linux-kernel@...r.kernel.org>
Subject: Re: clone3() example code
On 2019-10-25, Christian Brauner <christian.brauner@...ntu.com> wrote:
> #define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr)))
>
> int main(int argc, char *argv[])
> {
> int pidfd = -1;
> pid_t parent_tid = -1, pid = -1;
> struct clone_args args = {0};
>
> args.parent_tid = ptr_to_u64(&parent_tid); /* CLONE_PARENT_SETTID */
> args.pidfd = ptr_to_u64(&pidfd); /* CLONE_PIDFD */
> args.flags = CLONE_PIDFD | CLONE_PARENT_SETTID;
> args.exit_signal = SIGCHLD;
>
> pid = sys_clone3(&args);
I'd suggest that
struct clone_args args = {
.flags = CLONE_PIDFD | CLONE_PARENT_SETTID,
.parent_tid = ptr_to_u64(&parent_tid), /* CLONE_PARENT_SETTID */
.pidfd = ptr_to_u64(&pidfd), /* CLONE_PIDFD */
.exit_signal = SIGCHLD,
};
or alternatively
pid = sys_clone3(&(struct clone_args) {
.flags = CLONE_PIDFD | CLONE_PARENT_SETTID,
.parent_tid = ptr_to_u64(&parent_tid), /* CLONE_PARENT_SETTID */
.pidfd = ptr_to_u64(&pidfd), /* CLONE_PIDFD */
.exit_signal = SIGCHLD,
});
are easier to read.
> if (pid < 0) {
> fprintf(stderr, "%s - Failed to create new process\n", strerror(errno));
> exit(EXIT_FAILURE);
> }
>
> if (pid == 0) {
> printf("Child process with pid %d\n", getpid());
> exit(EXIT_SUCCESS);
> }
>
> printf("Parent process received child's pid %d as return value\n", pid);
> printf("Parent process received child's pidfd %d\n", *(int *)args.pidfd);
> printf("Parent process received child's pid %d as return argument\n",
> *(pid_t *)args.parent_tid);
>
> if (0) {
> if (waitid(P_ALL, pid, NULL, 0) == 0) {
> fprintf(stderr, "Managed to wait on CLONE_NO_WAITALL process with waitid(P_ALL)\n");
> exit(EXIT_FAILURE);
> }
> printf("Child process %d requested CLONE_NO_WAITALL\n", pid);
> } else {
> printf("Child process %d did not request CLONE_NO_WAITALL\n", pid);
> }
>
> if (wait_for_pid(pid))
> exit(EXIT_FAILURE);
>
> if (pid != *(pid_t *)args.parent_tid)
> exit(EXIT_FAILURE);
>
> close(pidfd);
>
> return 0;
> }
--
Aleksa Sarai
Senior Software Engineer (Containers)
SUSE Linux GmbH
<https://www.cyphar.com/>
Download attachment "signature.asc" of type "application/pgp-signature" (229 bytes)
Powered by blists - more mailing lists