Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp4053648ybg; Fri, 25 Oct 2019 12:38:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqxGuwxka/sAnrwm00ecwN+7ZJIEERNe6rKZnD04yTPKMDYJx5gTuvycU5ErCdx0N98zGtfl X-Received: by 2002:a17:906:22c8:: with SMTP id q8mr5069801eja.19.1572032310781; Fri, 25 Oct 2019 12:38:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572032310; cv=none; d=google.com; s=arc-20160816; b=v7KFNFFP8OUg9++FjYWJ3t7qKXUgzNZnsbaeVMYVw/Mre6L1w45Jk2YzsOR04mOwT/ 0+5zKA5TJxYPQ5+qCcrPBC+GpxBKL8JxPgDmxtFcd/6qM7lD6Y6wFT814i2rok4dsHA4 SE85LAHI5G1svW6cOXfLfEob9J89H1XDzj+endSkVt0TgTEq8XHZJoox6F998mne9/qL XP6/SqL2nRpbuRuD69Qq8M8j9Gs9SqYY8oJHGXroAfLCteXLO6LIjPnHGRgvJbBq/pzb P7Uq3rW7nTq3sfsBeY4ztXIGDTX2DdFO8h/oXxnLTnPED8SeBFY66KVfI8SeJXIoefcZ 9UBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=1hr/It7KBpMKK+hW97z8lbJIyl9ptG8WnPmalZv5iWU=; b=shw7meIkVrfTfnPlE2Q3M/KWfM4W1Su6aZnBq62wKv2LR+NZ32Fj/fzqDlV3HZ8RAs OKS/1VCPgXKmZqhjzneUIhTXAL+VZFEPlA0Z6xM9QTFO4dbSvptfCmcOSa+S1vFQ8xoG qqjyk07q7pyeLQTp8iOsYIrii5mi1+BlLkre5Kcl6YRHKKRa4YI+8xXFvObZjTZh2uQ2 4o7XEJT9BLyE0XR6C5HmCvHo40kc0OY+eHq8R/SfzHD5pi2APxaqfveAKO+g+IWQ2tUj jRw501pdEzcQK9+RQ3LMNXJEIl7h+BX+QopSKS+u6vR8PVE3407vpvyqLY9r7aLgSCTs GaNA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a8si1987195edm.240.2019.10.25.12.38.07; Fri, 25 Oct 2019 12:38:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2408858AbfJYJuM (ORCPT + 99 others); Fri, 25 Oct 2019 05:50:12 -0400 Received: from mout-p-102.mailbox.org ([80.241.56.152]:53530 "EHLO mout-p-102.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2407918AbfJYJuL (ORCPT ); Fri, 25 Oct 2019 05:50:11 -0400 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 46zzqj258lzKmZ4; Fri, 25 Oct 2019 11:50:09 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id TLtJwWbBoCxd; Fri, 25 Oct 2019 11:50:05 +0200 (CEST) Date: Fri, 25 Oct 2019 20:49:56 +1100 From: Aleksa Sarai To: Christian Brauner Cc: "Michael Kerrisk (man-pages)" , Christian Brauner , Linux API , lkml Subject: Re: clone3() example code Message-ID: <20191025094956.hvr44v2lbfxf7dfs@yavin.dot.cyphar.com> References: <20191025084142.jpwypkyczehylhgv@wittgenstein> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="qfxueoggiia3z6jh" Content-Disposition: inline In-Reply-To: <20191025084142.jpwypkyczehylhgv@wittgenstein> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --qfxueoggiia3z6jh Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2019-10-25, Christian Brauner wrote: > #define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr))) >=20 > int main(int argc, char *argv[]) > { > int pidfd =3D -1; > pid_t parent_tid =3D -1, pid =3D -1; > struct clone_args args =3D {0}; >=20 > args.parent_tid =3D ptr_to_u64(&parent_tid); /* CLONE_PARENT_SETTID */ > args.pidfd =3D ptr_to_u64(&pidfd); /* CLONE_PIDFD */ > args.flags =3D CLONE_PIDFD | CLONE_PARENT_SETTID; > args.exit_signal =3D SIGCHLD; >=20 > pid =3D sys_clone3(&args); I'd suggest that struct clone_args args =3D { .flags =3D CLONE_PIDFD | CLONE_PARENT_SETTID, .parent_tid =3D ptr_to_u64(&parent_tid), /* CLONE_PARENT_SETTID */ .pidfd =3D ptr_to_u64(&pidfd), /* CLONE_PIDFD */ .exit_signal =3D SIGCHLD, }; or alternatively pid =3D sys_clone3(&(struct clone_args) { .flags =3D CLONE_PIDFD | CLONE_PARENT_SETTID, .parent_tid =3D ptr_to_u64(&parent_tid), /* CLONE_PARENT_SETTID */ .pidfd =3D ptr_to_u64(&pidfd), /* CLONE_PIDFD */ .exit_signal =3D SIGCHLD, }); are easier to read. > if (pid < 0) { > fprintf(stderr, "%s - Failed to create new process\n", strerror(errno)); > exit(EXIT_FAILURE); > } >=20 > if (pid =3D=3D 0) { > printf("Child process with pid %d\n", getpid()); > exit(EXIT_SUCCESS); > } >=20 > 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); >=20 > if (0) { > if (waitid(P_ALL, pid, NULL, 0) =3D=3D 0) { > fprintf(stderr, "Managed to wait on CLONE_NO_WAITALL process with wait= id(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); > } >=20 > if (wait_for_pid(pid)) > exit(EXIT_FAILURE); >=20 > if (pid !=3D *(pid_t *)args.parent_tid) > exit(EXIT_FAILURE); >=20 > close(pidfd); >=20 > return 0; > } --=20 Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH --qfxueoggiia3z6jh Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQSxZm6dtfE8gxLLfYqdlLljIbnQEgUCXbLFQAAKCRCdlLljIbnQ El81AP0Zq9hqMvdLZ606cwcrvwrlF1Dz1SNfa3Qr2ixC8xKfigD+OBZQIMDUhECt fIXlzzzhuKElKrMxiI6ydgtkfRPGJgA= =Ka/u -----END PGP SIGNATURE----- --qfxueoggiia3z6jh--