From nobody Wed Jan 07 15:06:59 2026 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4dmWbl3NYbz6MyHY for ; Wed, 07 Jan 2026 15:07:15 +0000 (UTC) (envelope-from asomers@gmail.com) Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "WR4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dmWbl1M69z3jlP for ; Wed, 07 Jan 2026 15:07:15 +0000 (UTC) (envelope-from asomers@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-b79f8f7ea43so439525866b.2 for ; Wed, 07 Jan 2026 07:07:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767798433; x=1768403233; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=2DYVZjynEU5DBgTu2I6ful6TO67Bvv0s2bhqXd05z70=; b=REdbHtQwvVDm+AArReshd7SIuNowzbXmECD/mrwVNkkkIohZRktNCYEch5CMaJXmDP bulJrDurIr3enbLeVhBsBQWoOTiht7Zpo9KjyCeOoZ2elrTzRLx0mNt156W2GeWQgyD7 8Ktjj5bpWQejZZbjRgfEAynIXGp2H+Wso82uZZkYz0ei7BSPVyA4vcM8hFxHZWHi5+G8 tDGwcGKnufe4MpHktZRsVs2RlTVt5dkqtoHODRAB7TdgIN75b2jzD7LHDpZ40MMRWst1 X+Bh/bPQz94+nW6ihu+k6cI0FFCJP0WXsdlj4Zg4q+/W08RhG6B8xI4YcRLPo/bxoeh1 Z5Rg== X-Forwarded-Encrypted: i=1; AJvYcCWvb5FtmIBjioSrags0exLqfa87AVfOI+jGsDcd0YXYuSLb6SRDq3CaaHxmIrvRTvQInkBgX+24I+T1BV471D4=@freebsd.org X-Gm-Message-State: AOJu0YwR6hDrhbid+wvOAvEe0owasbseNyTgGT3LCkg2xprR2r1dXFDx haNiyl4xFvEEJlV5wTjJTnm/RJixL7Aj8FfQEFbbqgcOSSBOt840LaRq9bVVVpQbkNGw8w5585i jLt0a4Vg1hIk8jWO0rf9LGgQaxOhYyhE= X-Gm-Gg: AY/fxX73u5RjL1gxIUBZhHiXGM3SYjw/+1Js5bDNhaSaN5MI6rQbpIQ1AkUnDQHVVao HumGiQw5w3EZ3TR6RGMupkXMsbjLwHXqoIOqaP4vFfoO1AjC9uio6whiSdgS/SYuO7bQMgan8lR gwHTI4h84srS6+/Ft+zpttXnwVoJXTWE8AfpcCZS63wxHYPRern+3tqk6I9biAcRXrI/lgXX8I5 /XdNfTWza17Wyzrx+5kpTaePxvLJjg4UsJg3sFpLlEiiWHHsT8ZmqBlTMFHl8TNeEvKVWk= X-Google-Smtp-Source: AGHT+IHf3R+Ver0rtLtYszMqlbvufayCJYT6VGUdjO82Y1LOs5TjJn0aCMhs8bKeA1ztfmmlpzwjQZWxjUly4z50D7Q= X-Received: by 2002:a17:907:3c86:b0:b79:a827:4bc5 with SMTP id a640c23a62f3a-b8444c68296mr292667266b.15.1767798432989; Wed, 07 Jan 2026 07:07:12 -0800 (PST) List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@FreeBSD.org MIME-Version: 1.0 References: <01AC8C85-DE8E-44F9-85FD-6E2514DDE96B@FreeBSD.org> In-Reply-To: <01AC8C85-DE8E-44F9-85FD-6E2514DDE96B@FreeBSD.org> From: Alan Somers Date: Wed, 7 Jan 2026 08:06:59 -0700 X-Gm-Features: AQt7F2rvkqMQDV7uxlsEtFYgBsxkA1RlcskioO_0sWobDnxOzGLnyYHA--88oj8 Message-ID: Subject: Re: Is anybody working on extending procdesc support to rfork? To: David Chisnall Cc: Konstantin Belousov , FreeBSD Hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15169, ipnet:209.85.128.0/17, country:US] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Queue-Id: 4dmWbl1M69z3jlP On Wed, Jan 7, 2026 at 4:22=E2=80=AFAM David Chisnall wrote: > > On 7 Jan 2026, at 03:40, Konstantin Belousov wrote: > > > > On Tue, Jan 06, 2026 at 11:23:10AM -0700, Alan Somers wrote: > >> The procdesc(4) interface is a Capsicum-friendly, race-free > >> alternative to fork and kill. It's a good fit for a new feature going > >> into the Rust standard library. However, it's not quite complete > >> enough to do a good job. Is anybody working on extending it? Or even > >> interested in doing so? > >> > >> The functionality that Rust wants is: > >> > >> * kill - we can use pdkill() > >> * wait - we can use poll() or select() > >> * spawn - we must use pdfork/exec . posix_spawn would be better, but > >> we can't do such a thing without procdesc support in rfork. > > > > So what exactly is missing? Can you propose a scetch of the desired AP= I? > > The minimum is an rfork variant that returns a process descriptor and a P= ID, rather than just a PID (or returns a just a process descriptor, since p= dgetpid exists). > > But if we=E2=80=99re designing new process-creation APIs there are two th= ings I=E2=80=99d really like: > > An `rfork` (or replacement) flag that gives me something like vfork but w= ithout the returns-twice behaviour and requires an explicit execve or exit = call to return to the previous kernel state for the thread. After the call= , I have the child=E2=80=99s file descriptor table, credentials, and so on.= I can do whatever I want to set up the child=E2=80=99s environment, and t= hen `execve` returns either failure (couldn=E2=80=99t create the new proces= s, sorry) and cleans up all of the child-process state, or success. Or, if= I call `_exit` then the child process=E2=80=99s state is discarded. But i= n both cases the memory management is just like any other memory management= . > > This is useful for Rust, C++, and other languages with RAII. You have to= be very careful using RAII to manage memory in a vfork context: any newly = allocated memory must be owned by something with a scope that *isn=E2=80=99= t* between the `vfork` and `execve`. With an API like this, you can write = code that looks a bit like this: > > ```c++ > int child =3D pdrfork(...); > if (child < 0) > { > throw ProcessCreationFailed(errno); > } > std::vector owningArguments; > std::vector argv; > std::vector owningEnvironment; > std::vector envp; > > auto pushArgument(auto &arg) > { > owningArguments.push_back(arg); > arvv.push_back(owningArguments.back().c_str()); > }; > auto pushEnvironment(auto &envvar) > { > owningEnvironment.push_back(arg); > envp.push_back(owningEnvironment.back().c_str()); > }; > // Argv[0] is the program name > pushArgument(binaryName); > // Set up other arguments and environment variables. > pushArgument(...); > argv.push_back(nullptr); > > pushEnvironment(...); > envp.push_back(nullptr); > > // Set up any inherited file descriptors we need, including making sure t= hat the program binary is close on exec and near the end. > ... > > // If something went wrong, exit the child process scope and throw an exc= eption. > if (somethingWentWrong) > { > _exit(0); > throw ProcessSetupFailed(someExplanation); > } > > // Do any setuid, jattach, whatever things. > ... > // Enter capability mode > cap_enter(); > // Execute the new program in this context > int execReturn =3D fexecve(programFd, argv.get(), envp.get()); > if (execReturn < 0) > { > throw ProcessCreationFailed(errno); > } > return child; > ``` > > All memory for the argument / environment vectors is cleaned up on either= the throw or return. With normal `vfork`, you have to make sure that the = declaration of the vectors is *before* the `vfork` call. This is very easy= to get wrong (and to break in minor refactorings), and getting it wrong le= aks memory (so doesn=E2=80=99t crash and is not the kind of bug that gets h= igh-priority fixes). > > And this is easy to wrap in a process-creation API that takes a lambda th= at runs in the child-process setup scope, giving an ergonomic way of doing = process creation in higher-level languages. > > The other things I=E2=80=99d really like is a way of passing a list of fi= le descriptors to inherit and the numbers that they go in. Something like: > > ``` > struct inherited_file_descriptor > { > int parent_fd; > int child_fd; > }; > > int pdrfork(int flags, const struct inherited_file_descriptor[]); > ``` > > So I could write: > > ``` > struct inherited_file_descriptor fds[] =3D { > {0, 0}, {1, 1}, {2, 2}, {RPCSocket, 3}, > /* Gap for things opened after dropping privileges, in the child = context */ > { tempFD, 8} }; > int child =3D pdrfork(..., fds); > ... > ``` > > This requires a flag that is part way between RFCFDG and RFFDG: only the = named fds are inherited. This avoids the complicated `dup2` + `closefrom` = dance that I always end up doing to ensure that the child inherits the expe= cted FDs, in the correct places, but only the expected ones (the simplest w= ay of doing this is to `dup` all file descriptors that you want to inherit = until they=E2=80=99re above the number of the highest one, then `dup2` them= into the right place, then `closefrom`, you can make this more efficient b= y doing a shuffle pass but it=E2=80=99s complex and error prone. Doing thi= s in the kernel would be simpler). > > So my desired version is close to the pdrfork outlined above. That said,= it would be nice if the API were extensible, which probably means taking a= structure with a size where the structure contains the pointer to the inhe= rited file descriptor list. > > David David's pdrfork proposal tackles a problem I hadn't even considered. I was only thinking about process descriptors. As I said, those work currently, but to make them more efficient would require something like this: /* * A procdesc-enabled version of posix_spawn. * fdp is return storage for the process descriptor. * pdflags is PD_DAEMON or PD_CLOEXEC, as described in pdfork(2). * Other arguments are as described in posix_spawn(3) */ int pd_spawn(int *fdp, int pdflags, const char *restrict path, const posix_spawn_file_actions_t *file_actio= ns, const posix_spawnattr_t *restrict attrp, char *const argv[restrict], char *const envp[restrict]); /* * pdrfork - a procdesc-enabled version of rfork, to support pd_spawn * fdp is return storage for the process descriptor * pdflags are as described in pdfork(2) * rflags are as described in rfork(2) */ int pdrfork(int *fdp, int pdflags, int rflags);