From nobody Wed Jan 07 11:22:15 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 4dmQcM2QJVz6MfMB for ; Wed, 07 Jan 2026 11:22:27 +0000 (UTC) (envelope-from theraven@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dmQcM0vWkz3CxB; Wed, 07 Jan 2026 11:22:27 +0000 (UTC) (envelope-from theraven@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1767784947; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tOKLOwpa3jnMELywcydtNNa+pYQoahGPIixuPS4arrQ=; b=Ea88Cx7f3UoYfQWCEOKYmPZ5PIhw7RUqTp8g+b2cnYp6huXzPYo2HCbtQnBqVgC0VcwBxN dGpqXmHMuomUYb0FYDgStABj3PaGYK1H4hxSPvp5PJ+0RgQ8nU4GELFQDMqs3kd89MTKcn Y0zMXbE+6jBiqwjlhFe9Aoj5oaIWMA6udxBOL4OKPaVoce6byugnEY8HQ7xqfjE2tpatSB eJyFXJWRueaNmqf5rzaZZ5b3zrRIZcw2LQqCcdyCMG5FlCX0rtU0/SWO/quzS2jTzjXSFe 0noJ7dA4r30h6ER29yCuMtlTtbYoygRBJIOr7Slox5A3XCE14cWAAXGceejKPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1767784947; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tOKLOwpa3jnMELywcydtNNa+pYQoahGPIixuPS4arrQ=; b=FoNbgsEwa2jMMpWnRbeb+JFkyVXr7s1jcJFIC5ic6QN2a02IX0uhgzZI9xbWW6x8/UJOmB w3gLsYov3qLqwDhJmHIrF7cxfh9fr4aQFWNaqZLDdDC0EHXw6XYcifxymwtV7VMQ3vc6Me /ceoHGkSPWOd3gM3sVf9Pbqjq+ejlZvOBNS2XhfJ6GA4o4ItQIUgubHJdxWB7KJOxEe3yC ruugCFOA5iOcMHBOLWs+BJw+VEqajsB4F6V3B8zwVe/BS8zwh6N7ntzg21uUb/1QR9M3T0 pFYcQb5FzbqfKSZmbQnZQWApFFcCrbb6udifGtPQdeT3EdjyuU33bmdlXStrAw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1767784947; a=rsa-sha256; cv=none; b=NtSpBzydINc9SeCQLibYHvqGgvBaEY7atI7iV46LAz4iO7znF9/Zkrg7nL7ff28cZxOlHD tMFk0VTGIeL0Yh9T+96Je28C9z4GVOth3FLm3m75ArnyXwtgzhgvsiIEoYKjuH/sRXEjGl 5o2z1C/LZM0b3AvpMpFwmWwAfVblFZ+tjysZOe4wVghJwfQ1wffQ25+L8aEkq940HtqFo6 VBrIF/H5JUOI9VXhO6Y457apOrks+yrqJIc73zAfGMOEtJhoHvcXgxm16qqyInf4KJBxVt dT1I3Z/Em5Qk1ni0IZuCOFk+gwD1XASlhb/4N0spJpByhN+fT5XeqSAb41t/dQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from smtp.theravensnest.org (smtp.theravensnest.org [45.77.103.195]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: theraven) by smtp.freebsd.org (Postfix) with ESMTPSA id 4dmQcM0QX9z1N7p; Wed, 07 Jan 2026 11:22:27 +0000 (UTC) (envelope-from theraven@FreeBSD.org) Received: from smtpclient.apple (host86-143-41-230.range86-143.btcentralplus.com [86.143.41.230]) by smtp.theravensnest.org (Postfix) with ESMTPSA id 351979ABD; Wed, 07 Jan 2026 11:22:26 +0000 (GMT) Content-Type: text/plain; charset=utf-8 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 (Mac OS X Mail 16.0 \(3776.700.51.11.4\)) Subject: Re: Is anybody working on extending procdesc support to rfork? From: David Chisnall In-Reply-To: Date: Wed, 7 Jan 2026 11:22:15 +0000 Cc: Alan Somers , FreeBSD Hackers Content-Transfer-Encoding: quoted-printable Message-Id: <01AC8C85-DE8E-44F9-85FD-6E2514DDE96B@FreeBSD.org> References: To: Konstantin Belousov X-Mailer: Apple Mail (2.3776.700.51.11.4) On 7 Jan 2026, at 03:40, Konstantin Belousov = wrote: >=20 > 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? >>=20 >> The functionality that Rust wants is: >>=20 >> * 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. >=20 > So what exactly is missing? Can you propose a scetch of the desired = API? The minimum is an rfork variant that returns a process descriptor and a = PID, rather than just a PID (or returns a just a process descriptor, = since pdgetpid exists). But if we=E2=80=99re designing new process-creation APIs there are two = things I=E2=80=99d really like: An `rfork` (or replacement) flag that gives me something like vfork but = without 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 then `execve` returns either failure (couldn=E2=80=99t = create the new process, 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 in 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=99t* 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 = that the program binary is close on exec and near the end. ... // If something went wrong, exit the child process scope and throw an = exception. 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 leaks memory (so doesn=E2=80=99t crash and is not the = kind of bug that gets high-priority fixes). =20 And this is easy to wrap in a process-creation API that takes a lambda = that 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 = file 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 expected FDs, in the correct places, but only the expected = ones (the simplest way 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 by doing a shuffle pass but it=E2=80=99s = complex and error prone. Doing this 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 inherited file descriptor list. David