Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp4054272ybg; Fri, 25 Oct 2019 12:39:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqyiTkSkuAwOLHF2dOlLpwHpFFgSvVpeMqOfSZmY9oprT6MZITQGScDiiWHq7pEHQnwlbgXa X-Received: by 2002:a17:906:bfc6:: with SMTP id us6mr5090069ejb.51.1572032345415; Fri, 25 Oct 2019 12:39:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572032345; cv=none; d=google.com; s=arc-20160816; b=HueRebeR+pYS1zp5BSLC+ZKOU69AXKsy49S7D5ZWzynoBnvkX4NpCCuRzlWWLirD7e pHnNFSYimb+ZkWLUK9t5pWyYpFmwHKdIQO2j+AIPwb0aP3UiFHr2lxQTh2NMw6ilyirw RrjQQfie/M4lKOHfnHZfLjagNWEjEdERkfWFYcyrprVGNarH+ynoqz76wbmTzja0osJK ucIgOFRrPfgfu4PfFF/jQ7c+jDF2d2jvvW3C7TlDucPZIP9+l0uiuDjcgk61Oj33YJI0 W13TvVfZ3V6UsOK1b8IkEz+B+m3xqpTOxFvKu7aaSgCu2vr3O7fDJrJ8mE+YT1EBbJ7/ V7Jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=dvA5zmO2ds2tbplJsjRUDUQPIBWphAuMzLV7gJs+K+0=; b=RezzcyAIW33FZ2rcO2+BQ4iLkJ5L7WTrZALBGBLtSkXBRWHWJCpcSif1THmnbzoIyw YCywbbtxDcOdwV3wxugqFCCeoTE2a12PyrCOU95pRQDhTGxwiO7Gi6tCijC1qCRWfBs5 BLCOn/t6j+ms2Q0LS5CfInJUecA9hA+W3X3SBrwr6uUu5n3qrw7wEkkGXHhbmInOJCQP f5D77XigbIujyDyZiDtKWnUTFfHof3AdPT03L9s5FXbiNYtkCR29Ucohkp5X0K5ktSKw ntBPrUgTzzd7x4LCku02bjq9aRNfJga+aWD0+hsuuJyVcDmAv703aDXvzp+ET+VhxVWa OQ4w== 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 b18si1872536edc.390.2019.10.25.12.38.42; Fri, 25 Oct 2019 12:39:05 -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 S2408984AbfJYJ4y (ORCPT + 99 others); Fri, 25 Oct 2019 05:56:54 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:40638 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2408934AbfJYJ4y (ORCPT ); Fri, 25 Oct 2019 05:56:54 -0400 Received: from [185.240.52.243] (helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iNwKw-00077y-MR; Fri, 25 Oct 2019 09:56:50 +0000 Date: Fri, 25 Oct 2019 11:56:49 +0200 From: Christian Brauner To: Aleksa Sarai Cc: "Michael Kerrisk (man-pages)" , Christian Brauner , Linux API , lkml Subject: Re: clone3() example code Message-ID: <20191025095647.ga74uchqhflpliec@wittgenstein> References: <20191025084142.jpwypkyczehylhgv@wittgenstein> <20191025094956.hvr44v2lbfxf7dfs@yavin.dot.cyphar.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20191025094956.hvr44v2lbfxf7dfs@yavin.dot.cyphar.com> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 25, 2019 at 08:49:56PM +1100, Aleksa Sarai wrote: > On 2019-10-25, Christian Brauner 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. That was an accident. I posted from the wrong file. The correct code is: #ifndef _GNU_SOURCE #define _GNU_SOURCE 1 #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef CLONE_PIDFD #define CLONE_PIDFD 0x00001000 #endif #ifndef __NR_clone3 #define __NR_clone3 -1 struct clone_args { __aligned_u64 flags; __aligned_u64 pidfd; __aligned_u64 child_tid; __aligned_u64 parent_tid; __aligned_u64 exit_signal; __aligned_u64 stack; __aligned_u64 stack_size; __aligned_u64 tls; }; #endif static pid_t sys_clone3(struct clone_args *args) { return syscall(__NR_clone3, args, sizeof(struct clone_args)); } static int wait_for_pid(pid_t pid) { int status, ret; again: ret = waitpid(pid, &status, 0); if (ret == -1) { if (errno == EINTR) goto again; return -1; } if (ret != pid) goto again; if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) return -1; return 0; } #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 = { /* CLONE_PARENT_SETTID */ .parent_tid = ptr_to_u64(&parent_tid), /* CLONE_PIDFD */ .pidfd = ptr_to_u64(&pidfd), .flags = CLONE_PIDFD | CLONE_PARENT_SETTID, .exit_signal = SIGCHLD, }; pid = sys_clone3(&args); 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", pidfd); printf("Parent process received child's pid %d as return argument\n", *(pid_t *)args.parent_tid); if (wait_for_pid(pid)) { fprintf(stderr, "Failed to wait on child process\n"); exit(EXIT_FAILURE); } if (pid != parent_tid) exit(EXIT_FAILURE); close(pidfd); return 0; }