[PATCH 13/23] linux: Add fchroot

Collin Funk collin.funk1@gmail.com
Thu Nov 5 17:26:54 GMT 2026


Florian Weimer <fweimer@redhat.com> writes:

> * Andreas Schwab:
>
>> On Nov 05 2026, Florian Weimer wrote:
>>
>>> * Adhemerval Zanella:
>>>
>>>> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>> index a541b378d03..6597435ace7 100644
>>>> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
>>>>  # define CLOSE_RANGE_CLOEXEC (1U << 2)
>>>>  #endif
>>>>  
>>>> +/* Change the root directory of the calling process to the directory
>>>> +   referred to by the file descriptor FD.  FD can also be FD_FAILFS_ROOT,
>>>> +   in which case all subsequent absolute file name lookups fail.  FLAGS
>>>> +   must be zero.  */
>>>> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
>>>> +
>>>>  #endif /* __USE_GNU  */
>>>
>>> CRIU defines _GNU_SOURCE, and it has:
>>>
>>> criu/files.c:#include <unistd.h>
>>>
>>> criu/files.c:static int fchroot(int fd)
>>
>> BSD also has fchroot without the flags.
>
> So does Hurd.

And Solaris [1].

I have been wanting this for the 'chroot' command from GNU coreutils for
a while. If my memory serves me correctly, it is only available on
NetBSD and not the other BSDs. For that reason, I didn't bother making
use of it. I was not aware it was supported on Hurd, and only found out
about Solaris recently.

>> Should the one above be called fchroot2, or should the flags be
>> dropped for now?
>
> It won't fix the fchroot build failure.  It's surprisingly rare, though:
>
> <https://codesearch.debian.net/search?q=%28%5E%7Cint%5Cs*%29fchroot%5Cs*%5C%28&literal=0>
>
> I just got lucky, I didn't find it through Debian Code Search, just by
> consulting the CRIU source code.
>
> I don't think we should aim for compatibility with Hurd/BSD if Linux has
> something else.  On the other hand, if POSIX standardizes fchroot in the
> one-argument form, we'll have a problem.

Yes. I was pleasantly suprised to see the system call added in Linux
7.3. But I find it a bit annoying that a different function signature
than everyone else...

Collin

[1] https://docs.oracle.com/cd/E86824_01/html/E54765/fchroot-2.html


More information about the Libc-alpha mailing list