oscall 0.1.0

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! What Linux alone has, and what containing a process is made of there:
//! namespaces, the identifier maps of a user namespace, process
//! descriptors (pidfds), starting a child in a cgroup, and a few process
//! attributes (`prctl`).
//!
//! # Namespaces
//!
//! A namespace is the view a process has of one kind of system resource:
//! [`Namespaces`] names the kinds. A child is started in new ones with
//! [`CommandExt::namespaces`]; [`unshare`] gives new ones to the caller,
//! and [`setns`] moves it into those of another process.
//!
//! Creating a namespace needs `CAP_SYS_ADMIN`, except a **user namespace**,
//! which anyone may create where the system allows it, and whose creator
//! has every capability *inside* it: a process asks for
//! [`Namespaces::USER`] together with the others, and the kernel creates
//! the user namespace first. In a new user namespace no identifier means
//! anything until its maps are written ([`CommandExt::uid_map`],
//! [`write_uid_map`]): the process is `nobody` (65534) to itself, and loses
//! its capabilities when it runs a program.
//!
//! Systems restrict this, and answer
//! [`PermissionDenied`](crate::ErrorKind::PermissionDenied) unless said
//! otherwise:
//!
//! - `user.max_user_namespaces` may be 0: `ENOSPC`,
//!   [`StorageFull`](crate::ErrorKind::StorageFull).
//! - Debian's `kernel.unprivileged_userns_clone` switches user namespaces
//!   off for who has no privilege.
//! - Ubuntu's AppArmor restriction
//!   (`kernel.apparmor_restrict_unprivileged_userns`) lets a process
//!   without privilege create its namespaces, and
//!   its parent write maps of one identifier, but denies the process the
//!   *use* of the capabilities it has there: it is root of its namespace
//!   and may not set the host name, nor create another namespace later.
//!   What the `clone` that creates the child does is done; what the child
//!   would do itself is refused.
//! - The default system call filter of a container runtime (Docker)
//!   refuses every call that takes a namespace.
//!
//! # Memory and other limits: cgroups
//!
//! The limits of `unix::set_limit` are per process, and the one on memory
//! ([`AddressSpace`](super::unix::Resource::AddressSpace)) counts address
//! space, not memory in use. What limits the memory, the processor time or
//! the number of processes of a child *and everything it starts* is a
//! control group (cgroup v2): a directory under `/sys/fs/cgroup` whose
//! files are the settings (`memory.max`, `pids.max`, `cpu.max`...) and
//! which [`fs`](crate::fs) creates, reads and writes like any other.
//! [`CommandExt::cgroup`] starts a child in one, so that not one
//! instruction of the program runs outside it. oscall has no more than
//! that: no cgroup manager.
//!
//! # Process descriptors
//!
//! A pidfd names one process for as long as it is open, whatever becomes of
//! its identifier: [`pidfd_open`], [`pidfd_send_signal`], [`pidfd_wait`].
//! A [`Child`](super::Child) holds one and lends it
//! ([`AsHandle`]). A pidfd is readable once its process has exited:
//! [`event::unix::register`](crate::event::unix::register) takes it, with
//! [`Interest::READABLE`](crate::event::Interest::READABLE), for any
//! process (a [`Child`](super::Child) has
//! [`Poller::register_child`](crate::event::Poller::register_child)).
//!
//! # What is needed of the kernel
//!
//! oscall supports Linux 5.10 and later, which has everything here but the
//! time namespace (5.6) on kernels built without it. An emulator or a
//! system call filter may lack a call all the same, and it then fails with
//! [`Unsupported`](crate::ErrorKind::Unsupported) (`ENOSYS`): qemu-user
//! has no `clone3`, and refuses to create a child in new namespaces
//! (`EINVAL`).
//!
//! # Not covered
//!
//! Mounting (`mount`, `umount2`, `pivot_root`, `chroot`, the new mount
//! API), capabilities (`capset`), `seccomp`, and reading the host name:
//! [`raw::linux`](crate::raw::linux) makes those calls.

use crate::{AsHandle, Result, sys};

#[cfg(feature = "alloc")]
use super::{Command, ExitStatus};
#[cfg(feature = "alloc")]
use crate::BorrowedHandle;

crate::macros::flags! {
    /// Kinds of namespaces: `CLONE_NEW*`.
    pub struct Namespaces: u32 {
        /// Mount points (`CLONE_NEWNS`): the new namespace starts as a
        /// copy of the old one, and what is mounted or unmounted in it
        /// afterwards is its own.
        const MOUNT = 1;
        /// The host name and the domain name (`CLONE_NEWUTS`): see
        /// [`set_host_name`].
        const UTS = 1 << 1;
        /// System V IPC objects and POSIX message queues (`CLONE_NEWIPC`).
        const IPC = 1 << 2;
        /// User and group identifiers, and capabilities (`CLONE_NEWUSER`):
        /// see the [module documentation](self#namespaces).
        const USER = 1 << 3;
        /// Process identifiers (`CLONE_NEWPID`). The first process of the
        /// new namespace is its process 1: see
        /// [`CommandExt::namespaces`].
        const PID = 1 << 4;
        /// Network devices, addresses, routes, ports (`CLONE_NEWNET`): a
        /// new one has a loopback device, which is down, and nothing else.
        const NETWORK = 1 << 5;
        /// The root of the cgroup hierarchy the process sees
        /// (`CLONE_NEWCGROUP`).
        const CGROUP = 1 << 6;
        /// The offsets of the monotonic and boot-time clocks
        /// (`CLONE_NEWTIME`, Linux 5.6).
        const TIME = 1 << 7;
    }
}

/// Gives the calling **thread** new namespaces of the kinds in
/// `namespaces`, which it stops sharing with the rest of the system:
/// `unshare`.
///
/// Namespaces belong to threads, not to processes. The threads and
/// processes created afterwards by this thread are in its namespaces; the
/// other threads of the process stay where they were. Two kinds do not
/// take the caller along:
///
/// - [`PID`](Namespaces::PID): the caller keeps its identifier and its
///   namespace. The next process it creates is process 1 of the new one
///   (see [`CommandExt::namespaces`] for what that means), and when that
///   one has exited no other can be created: spawning then fails with
///   [`OutOfMemory`](crate::ErrorKind::OutOfMemory) (`ENOMEM`).
/// - [`TIME`](Namespaces::TIME): likewise, the processes created
///   afterwards are in the new namespace, as is the caller once it runs
///   another program.
///
/// # Errors
///
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied) without
/// `CAP_SYS_ADMIN` in the user namespace of the caller, which a new
/// [`USER`](Namespaces::USER) namespace asked for in the same call gives
/// (see the [module documentation](self#namespaces) for what may forbid
/// that one). [`InvalidInput`](crate::ErrorKind::InvalidInput) for
/// [`USER`](Namespaces::USER) in a process that has several threads: the
/// kernel refuses it, and a Rust test, which runs on a thread of its own,
/// is such a process.
pub fn unshare(namespaces: Namespaces) -> Result<()> {
    sys::process::contain::unshare(namespaces)
}

/// Moves the calling thread into existing namespaces: `setns`.
///
/// `handle` is either:
///
/// - a namespace: one of the files of `/proc/<pid>/ns` (`uts`, `net`,
///   `mnt`, `ipc`, `user`, `pid`, `cgroup`, `time`), opened for reading
///   with [`fs`](crate::fs). `namespaces` is then the kind it must be, or
///   empty for any;
/// - a pidfd ([`pidfd_open`], a [`Child`](super::Child)): the thread joins
///   the namespaces of that process, of all the kinds in `namespaces` or of
///   none (Linux 5.8).
///
/// As with [`unshare`], [`PID`](Namespaces::PID) and
/// [`TIME`](Namespaces::TIME) change where the processes created afterwards
/// are, not the caller.
///
/// # Errors
///
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied) without
/// `CAP_SYS_ADMIN` in the user namespace that owns the target, and in the
/// caller's own. [`InvalidInput`](crate::ErrorKind::InvalidInput) when the
/// handle is not of the kind asked for, for a pidfd with no kind, and to
/// join a [`USER`](Namespaces::USER) namespace from a process that has
/// several threads, or the one the caller is in already.
pub fn setns<H: AsHandle + ?Sized>(handle: &H, namespaces: Namespaces) -> Result<()> {
    sys::process::contain::setns(handle.as_handle().as_raw(), namespaces)
}

/// Sets the host name of the UTS namespace of the caller: `sethostname`.
/// For a namespace of one's own ([`Namespaces::UTS`]); in the initial one
/// this renames the machine.
///
/// The name is at most 64 bytes; `/proc/sys/kernel/hostname` reads it
/// back.
///
/// # Errors
///
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied) without
/// `CAP_SYS_ADMIN` in the user namespace that owns the UTS namespace;
/// [`InvalidInput`](crate::ErrorKind::InvalidInput) if the name is too
/// long.
pub fn set_host_name(name: &[u8]) -> Result<()> {
    sys::process::contain::set_host_name(name)
}

/// A range of a user namespace's identifier map: one line of `uid_map` or
/// `gid_map`. The `count` identifiers from `inside`, as the processes of
/// the namespace see them, are the `count` identifiers from `outside` in
/// the namespace of whoever writes the map.
///
/// `IdMap { inside: 0, outside: uid, count: 1 }` makes the process root in
/// its namespace, and itself to the rest of the system.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct IdMap {
    /// The first identifier of the range inside the namespace.
    pub inside: u32,
    /// The first identifier of the range outside of it.
    pub outside: u32,
    /// How many identifiers the range has; not 0.
    pub count: u32,
}

/// Writes the user map of the user namespace the process `pid` is in:
/// `/proc/<pid>/uid_map`. `pid` may be the caller
/// ([`id`](super::id)) after [`unshare`], or a child created in a new
/// namespace; [`CommandExt::uid_map`] does it for a child before its
/// program starts.
///
/// A map is written once, whole, and never changes. The writer must be in
/// the namespace or in its parent. Without `CAP_SETUID` in the parent
/// namespace, the map can only be one range of one identifier, `outside`
/// being the effective user of the writer (who must be the user that
/// created the namespace). A process that writes its own map is always in
/// that case, root included: its capabilities are those of the new
/// namespace. Larger maps are written from outside, by the parent.
///
/// # Errors
///
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied) when the rules
/// above are not met, or the map was already written;
/// [`InvalidInput`](crate::ErrorKind::InvalidInput) for an empty map, one
/// whose text is not shorter than 4096 bytes, and what the kernel finds
/// wrong with it (overlapping ranges, a count of 0, more than 340 ranges);
/// [`NotFound`](crate::ErrorKind::NotFound) when there is no such process,
/// or no `/proc`.
pub fn write_uid_map(pid: u32, map: &[IdMap]) -> Result<()> {
    sys::process::contain::write_id_map(super::checked_pid(pid)?, b"uid_map", map)
}

/// Writes the group map of the user namespace the process `pid` is in:
/// `/proc/<pid>/gid_map`. As [`write_uid_map`], with `CAP_SETGID` and the
/// effective group of the writer, and one more rule: without the
/// capability, `setgroups` must have been denied first
/// ([`allow_setgroups`]).
pub fn write_gid_map(pid: u32, map: &[IdMap]) -> Result<()> {
    sys::process::contain::write_id_map(super::checked_pid(pid)?, b"gid_map", map)
}

/// Says whether the processes of the user namespace the process `pid` is
/// in may call `setgroups`, which drops supplementary groups:
/// `/proc/<pid>/setgroups`, `allow` (the default) or `deny`.
///
/// Dropping a group can *give* access, to files that deny it to the group:
/// this is why a process without privilege must deny `setgroups` before it
/// may write a group map. It can only be set before the group map is
/// written, and once denied stays so, in that namespace and those created
/// inside it.
pub fn allow_setgroups(pid: u32, allow: bool) -> Result<()> {
    sys::process::contain::allow_setgroups(super::checked_pid(pid)?, allow)
}

/// Opens a descriptor of the process `pid`: `pidfd_open` (Linux 5.3).
///
/// The descriptor names that process even after it has exited and its
/// identifier was given to another. That holds from the moment it is
/// opened: `pid` itself is only safe to use while it cannot have been
/// reused, which is the case of a child not yet waited for, and of the
/// caller.
///
/// The handle is close-on-exec, becomes readable when the process exits
/// (see the [module documentation](self#process-descriptors)), and is
/// what [`pidfd_send_signal`], [`pidfd_wait`] and [`setns`] take.
///
/// # Errors
///
/// [`NoSuchProcess`](crate::ErrorKind::NoSuchProcess);
/// [`InvalidInput`](crate::ErrorKind::InvalidInput) if `pid` is 0 or does
/// not fit in the system's type, or names a thread that is not the first
/// of its process; [`Unsupported`](crate::ErrorKind::Unsupported) under an
/// emulator that lacks the call.
pub fn pidfd_open(pid: u32) -> Result<crate::OwnedHandle> {
    sys::process::contain::pidfd_open(super::checked_pid(pid)?)
}

/// Sends a signal to the process of a pidfd: `pidfd_send_signal`
/// (Linux 5.1). `signal` is the system's number, as for
/// `unix::send_signal`; 0 sends nothing and checks that the process is
/// there.
///
/// # Errors
///
/// [`NoSuchProcess`](crate::ErrorKind::NoSuchProcess) once the process has
/// been waited for (one that has exited and was not yet collected takes
/// the signal without an error);
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied);
/// [`BadHandle`](crate::ErrorKind::BadHandle) if the handle is not a
/// pidfd; [`InvalidInput`](crate::ErrorKind::InvalidInput) if the system
/// has no such signal.
pub fn pidfd_send_signal<H: AsHandle + ?Sized>(pidfd: &H, signal: i32) -> Result<()> {
    sys::process::contain::pidfd_send_signal(pidfd.as_handle().as_raw(), signal)
}

/// Waits for the process of a pidfd, which must be a child of the caller,
/// to exit, and collects it: `waitid(P_PIDFD)` (Linux 5.4).
///
/// The status of a child can be collected once. A
/// [`Child`](super::Child) for the same process then finds it gone (see
/// there): wait for a child through one or the other.
///
/// # Errors
///
/// `ECHILD` when the process is not a child of the caller, or was
/// collected already; [`Interrupted`](crate::ErrorKind::Interrupted) when
/// a signal handler runs during the wait.
#[cfg(feature = "alloc")]
pub fn pidfd_wait<H: AsHandle + ?Sized>(pidfd: &H) -> Result<ExitStatus> {
    match sys::process::wait_pidfd(pidfd.as_handle().as_raw(), true)? {
        Some((status, _)) => Ok(status),
        // The system returned from a blocking wait with no child.
        None => Err(crate::ErrorKind::Other.into()),
    }
}

/// As [`pidfd_wait`], without waiting: `None` if the process is still
/// running.
#[cfg(feature = "alloc")]
pub fn pidfd_try_wait<H: AsHandle + ?Sized>(pidfd: &H) -> Result<Option<ExitStatus>> {
    Ok(sys::process::wait_pidfd(pidfd.as_handle().as_raw(), false)?.map(|(status, _)| status))
}

/// Forbids the calling **thread** to gain privileges by running a program:
/// `prctl(PR_SET_NO_NEW_PRIVS)`. From then on set-user-ID and set-group-ID
/// bits and file capabilities are ignored by `execve`. It cannot be
/// undone, and the threads and processes created afterwards by this thread
/// inherit it; the other threads of the process are not affected.
///
/// A process without privilege needs this before it may install a
/// `seccomp` filter. [`CommandExt::no_new_privileges`] sets it for a child
/// alone.
pub fn set_no_new_privileges() -> Result<()> {
    sys::process::contain::set_no_new_privileges()
}

/// Whether the calling thread may no longer gain privileges
/// (`PR_GET_NO_NEW_PRIVS`): see [`set_no_new_privileges`].
pub fn no_new_privileges() -> Result<bool> {
    sys::process::contain::no_new_privileges()
}

/// Makes the calling process the one that adopts its orphaned descendants,
/// or stops it being so: `prctl(PR_SET_CHILD_SUBREAPER)`.
///
/// When a process exits, its children are normally adopted by process 1.
/// With this they go to the nearest ancestor that asked for them, which is
/// told when they exit (`SIGCHLD`) and must wait for them, as for its own
/// children: this is how a supervisor keeps track of every process a
/// program left behind. oscall has no call that waits for *any* child:
/// `waitid(P_ALL)` through [`raw::linux`](crate::raw::linux) does, and a
/// pidfd opened for a known identifier.
///
/// The setting is not inherited by children.
pub fn set_child_subreaper(on: bool) -> Result<()> {
    sys::process::contain::set_child_subreaper(on)
}

/// Whether the calling process adopts its orphaned descendants
/// (`PR_GET_CHILD_SUBREAPER`): see [`set_child_subreaper`].
pub fn is_child_subreaper() -> Result<bool> {
    sys::process::contain::is_child_subreaper()
}

/// Says whether the calling process may be dumped and inspected:
/// `prctl(PR_SET_DUMPABLE)`.
///
/// A process that is not dumpable writes no core dump, cannot be attached
/// to with `ptrace` by a process without privilege, and its files under
/// `/proc/<pid>` belong to root: other processes of the same user, and
/// the process itself where it is not root, can no longer open most of
/// them (which includes `/proc/self/environ`, `maps`, `mem` and the
/// namespace files of `ns`). The kernel clears the flag by itself when a
/// process changes identity, and sets it again when a program is run.
pub fn set_dumpable(on: bool) -> Result<()> {
    sys::process::contain::set_dumpable(on)
}

/// Whether the calling process may be dumped (`PR_GET_DUMPABLE`): see
/// [`set_dumpable`]. `false` also for a process the system only lets root
/// dump (`fs.suid_dumpable = 2`).
pub fn is_dumpable() -> Result<bool> {
    sys::process::contain::is_dumpable()
}

/// What [`CommandExt`] sets.
#[cfg(feature = "alloc")]
#[derive(Clone, Copy, Default)]
pub(crate) struct Options<'a> {
    pub(crate) namespaces: Namespaces,
    pub(crate) uid_map: Option<&'a [IdMap]>,
    pub(crate) gid_map: Option<&'a [IdMap]>,
    pub(crate) deny_setgroups: bool,
    pub(crate) cgroup: Option<BorrowedHandle<'a>>,
    pub(crate) no_new_privileges: bool,
    pub(crate) parent_death_signal: Option<i32>,
}

/// More to set on a [`Command`] on Linux.
#[cfg(feature = "alloc")]
pub trait CommandExt<'a> {
    /// Starts the child in new namespaces of these kinds: the `CLONE_NEW*`
    /// flags of the `clone` that creates it. Unlike with [`unshare`], the
    /// child itself is in every one of them, from its first instruction.
    ///
    /// What [`unshare`] says of privilege holds: without `CAP_SYS_ADMIN`,
    /// ask for [`USER`](Namespaces::USER) too. With a new user namespace
    /// and no map ([`uid_map`](Self::uid_map)), the program runs as
    /// `nobody` and without capabilities.
    ///
    /// With [`PID`](Namespaces::PID) the child is **process 1** of its
    /// namespace, which the kernel treats as an `init`:
    ///
    /// - [`id`](super::id) is 1 in the child and
    ///   [`parent_id`](super::parent_id) 0; [`Child::id`](super::Child::id)
    ///   is its identifier in the namespace of the caller.
    /// - A signal the program has no handler for is *not delivered*, even
    ///   one whose default action is to terminate: `SIGTERM` does nothing
    ///   to a program that does not handle it. Only `SIGKILL` and
    ///   `SIGSTOP` sent from outside the namespace always work, so
    ///   [`Child::kill`](super::Child::kill) does.
    /// - It adopts the orphans of its namespace and should wait for them.
    /// - When it exits, every other process of the namespace is killed.
    /// - `/proc` still shows the processes of the namespace it was mounted
    ///   in, until the child mounts its own (in a new
    ///   [`MOUNT`](Namespaces::MOUNT) namespace).
    ///
    /// With [`TIME`](Namespaces::TIME) the child enters the new namespace
    /// when the program starts, and the offsets of its clocks, which can
    /// only be written before that, are zero. It also needs `clone3`,
    /// which an emulator or a system call filter may lack:
    /// [`spawn`](Command::spawn) then fails with
    /// [`Unsupported`](crate::ErrorKind::Unsupported).
    ///
    /// # Errors
    ///
    /// [`spawn`](Command::spawn) fails as [`unshare`] does. qemu-user
    /// refuses to create a process in new namespaces, with
    /// [`InvalidInput`](crate::ErrorKind::InvalidInput).
    fn namespaces(self, namespaces: Namespaces) -> Self;

    /// Sets the user map of the child's new user namespace
    /// ([`Namespaces::USER`] must be asked for): what [`write_uid_map`]
    /// writes, written by the caller **before the program starts**. The
    /// child is created, waits while the caller writes
    /// `/proc/<pid>/setgroups`, `gid_map` and `uid_map` as the command
    /// says, and only then sets itself up and runs the program; if a write
    /// fails, [`spawn`](Command::spawn) fails with its error and the child
    /// is gone. The files are those of `/proc`, which must be mounted.
    ///
    /// The rules of [`write_uid_map`] apply to the caller: without
    /// privilege, one range of one identifier, its own effective user.
    ///
    /// # Errors
    ///
    /// [`spawn`](Command::spawn) fails with
    /// [`InvalidInput`](crate::ErrorKind::InvalidInput) for a map without
    /// a new user namespace, for an empty map and for one too long to
    /// write.
    fn uid_map(self, map: &'a [IdMap]) -> Self;

    /// Sets the group map of the child's new user namespace: as
    /// [`uid_map`](Self::uid_map), for what [`write_gid_map`] writes.
    /// Without privilege it needs
    /// [`deny_setgroups`](Self::deny_setgroups).
    fn gid_map(self, map: &'a [IdMap]) -> Self;

    /// Denies `setgroups` in the child's new user namespace
    /// ([`allow_setgroups`]), before the group map is written.
    fn deny_setgroups(self) -> Self;

    /// Starts the child in the cgroup (version 2) whose directory `cgroup`
    /// is an open handle of (borrowed as for
    /// [`Stdio::Handle`](super::Stdio::Handle): `directory.as_handle()`):
    /// `clone3` with `CLONE_INTO_CGROUP` (Linux 5.7). The child is in the cgroup from its creation, and so is
    /// everything it starts: this is how the memory of a child and its
    /// descendants is limited (see the
    /// [module documentation](self#memory-and-other-limits-cgroups)).
    ///
    /// The caller must be allowed to move a process there: write access to
    /// the `cgroup.procs` of the directory, and, for a caller that is not
    /// root, of the closest common ancestor with its own cgroup. A cgroup
    /// that has controllers enabled for its children
    /// (`cgroup.subtree_control`) takes no process itself.
    ///
    /// Where there is no `clone3` (qemu-user; the default system call
    /// filter of Docker, which answers `ENOSYS` to it), the child is
    /// created the usual way and moves itself, by writing to `cgroup.procs`
    /// before anything else: the same rights are needed and the program
    /// starts in the cgroup all the same.
    ///
    /// # Errors
    ///
    /// [`spawn`](Command::spawn) fails with
    /// [`PermissionDenied`](crate::ErrorKind::PermissionDenied) without
    /// the rights above; [`BadHandle`](crate::ErrorKind::BadHandle) if the
    /// handle is not a cgroup directory
    /// ([`NotFound`](crate::ErrorKind::NotFound) where the child moves
    /// itself).
    fn cgroup(self, cgroup: BorrowedHandle<'a>) -> Self;

    /// Forbids the child to gain privileges by running a program, starting
    /// with its own: [`set_no_new_privileges`], in the child, just before
    /// the program is run.
    fn no_new_privileges(self) -> Self;

    /// Has the system send `signal` to the child when its parent goes
    /// away: `prctl(PR_SET_PDEATHSIG)` in the child, once its identity is
    /// set. With `unix::SIGKILL`, a child that cannot outlive the caller;
    /// with a signal the program handles, one that is told.
    ///
    /// What the system means by "the parent" is **the thread that called
    /// [`spawn`](Command::spawn)**, not the process: the signal is sent
    /// when that thread ends, even if the process lives on. Spawn such a
    /// child from a thread that lasts as long as the child should.
    ///
    /// The setting belongs to the child process alone: the processes it
    /// creates do not have it. The system clears it when the program is
    /// set-user-ID, set-group-ID or has file capabilities, and whenever
    /// the child later changes its effective user or group; the child can
    /// change or clear it itself.
    ///
    /// If the caller is already gone by the time the child asks, which a
    /// process killed while it spawns can be, the child notices that its
    /// parent is no longer the caller and sends itself the signal. (A
    /// child that cannot see its parent, because it is in another process
    /// namespace, cannot check.)
    ///
    /// A number that is no signal makes `spawn` fail with
    /// [`InvalidInput`](crate::ErrorKind::InvalidInput).
    ///
    /// macOS has nothing of the kind, which is why this is not in `unix`:
    /// a child there finds out by itself, by watching for its parent's
    /// exit or for the end of a pipe the parent holds. On Windows, a job
    /// object that is closed with the parent does it
    /// (`process::windows::Job`).
    fn parent_death_signal(self, signal: i32) -> Self;
}

#[cfg(feature = "alloc")]
impl<'a> CommandExt<'a> for Command<'a> {
    fn namespaces(mut self, namespaces: Namespaces) -> Self {
        self.linux.namespaces = namespaces;
        self
    }

    fn uid_map(mut self, map: &'a [IdMap]) -> Self {
        self.linux.uid_map = Some(map);
        self
    }

    fn gid_map(mut self, map: &'a [IdMap]) -> Self {
        self.linux.gid_map = Some(map);
        self
    }

    fn deny_setgroups(mut self) -> Self {
        self.linux.deny_setgroups = true;
        self
    }

    fn cgroup(mut self, cgroup: BorrowedHandle<'a>) -> Self {
        self.linux.cgroup = Some(cgroup);
        self
    }

    fn no_new_privileges(mut self) -> Self {
        self.linux.no_new_privileges = true;
        self
    }

    fn parent_death_signal(mut self, signal: i32) -> Self {
        self.linux.parent_death_signal = Some(signal);
        self
    }
}