Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp4046484ybg; Fri, 25 Oct 2019 12:32:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqySXKhTpfbzCrQ85EWrgLJWoqzz4teMDD6fBaXmdUt+SQfXnOAGfCXfZBcWznkg/b8Vl3Pk X-Received: by 2002:a50:eb42:: with SMTP id z2mr5792223edp.291.1572031964909; Fri, 25 Oct 2019 12:32:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572031964; cv=none; d=google.com; s=arc-20160816; b=xBgReXcniygerqyw2UU4sAmRg8wI4xb2oabD/NFJ04utkZl4sXdEOUOYfXfpn0vxDb NFRSDXBkJets8L4wypWp1cjiMI81wFYL3NPnYh8b3Lp9e6h0K1PU8PfvJvBGdmSQvj/F KuHmHWpz22mi6FJfI0Dmslhl5Ddbz5FHaUclhicBZ+tm2TqfQpeBHjUWGgZJ36Osta05 sER/sOIsvxIvRRgNOkbSIVifVO7egsCxVQihnrdHUVrPo5whfLd+nBM9Rx9tDLhbkLUK UR7MkC1zm8zZmhiBh9F4oNh9cBBPYIbwJXKQeyvC7FKh0WnnVV3DivIpV8IgpmtmYYnZ FV1Q== 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=5WPPyCn7kFd3Q7ik27pls1VplUuYoO79PiZJhOl1osE=; b=QTuiZyf6BcLVSW1Vuop8ogihT4McVbSfJ5u5oFoqIG8n/C5L5qbQVRvlFGQ6qYnCQ7 PEErvpnCq640YyrUdLo8U2Bf0wiCu8PbV9dLB86yyJTsrmgE0KtNIaIErPWSn0Fgd9Rb w57Hh0nt3WUBOhFRfOHUZ8+95wEz7ZLNVFPglWVEkywkUMBHxhwoi79eRnhvwAKOQmZ4 bsjpBzIouAdkjjTl25d3QqzWpbN5WOocqucNgx0bWtx594GDNdFozAvfjFb8H54NhXDA Cfk6PF72zD0C3Wcx/Q7fIWYBZyFwagW9rlrRRjtah8AQktE9BeIpdw3zW/KWW+uYaZoh ms/g== 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 s21si1928238edm.311.2019.10.25.12.32.20; Fri, 25 Oct 2019 12:32:44 -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 S2408370AbfJYIlq (ORCPT + 99 others); Fri, 25 Oct 2019 04:41:46 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:38624 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404179AbfJYIlq (ORCPT ); Fri, 25 Oct 2019 04:41:46 -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 1iNvAF-0001cC-V2; Fri, 25 Oct 2019 08:41:44 +0000 Date: Fri, 25 Oct 2019 10:41:43 +0200 From: Christian Brauner To: "Michael Kerrisk (man-pages)" Cc: Christian Brauner , Linux API , lkml Subject: Re: clone3() example code Message-ID: <20191025084142.jpwypkyczehylhgv@wittgenstein> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: 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 10:13:23AM +0200, Michael Kerrisk (man-pages) wrote: > Hello Christian, Hey Michael, > > Do you have some example user-space code somewhere that illustrates > calling clone3(). I'm looking at how we might document that system > call in the manual page, and some example code to play with would be Excellent! > useful at this point. (I already have a simple working test program, > but probably you have something better.) Not sure about something better but one simple thing I used (a more extensive test-suite is coming): #define _GNU_SOURCE #include #include #include #include #include #include #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 #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 = {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); 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; } Thanks! Christian