blackship 0.3.0

A FreeBSD jail manager with state machine control
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Native jexec via `jail_attach(2)`, modeled on FreeBSD's jexec(8).

// FFI module: fork/exec, jail_attach(2), chroot(2), procctl(2) reaper.
#![allow(unsafe_code)]

use crate::error::{Error, Result};
use deadline_machines::{Clock, Deadline, NS_PER_SECOND, SystemClock};
use nix::sys::wait::{WaitStatus, waitpid};
use nix::unistd::{ForkResult, Pid, fork, pipe};
use std::ffi::CString;
use std::io::Read;
use std::os::fd::{AsRawFd, OwnedFd};
use std::time::Duration;

/// Run `command` in jail `jid`; returns (`exit_code`, stdout, stderr).
pub fn jexec_with_output(jid: i32, command: &[&str]) -> Result<(i32, Vec<u8>, Vec<u8>)> {
    check_nonempty_command(command)?;
    let argv = ExecArgv::new(command)?;

    let (stdout_read, stdout_write) = jexec_pipe("stdout")?;
    let (stderr_read, stderr_write) = jexec_pipe("stderr")?;

    // SAFETY: child runs only async-signal-safe syscalls until exec/_exit.
    match unsafe { fork() } {
        Ok(ForkResult::Parent { child }) => {
            drop(stdout_write);
            drop(stderr_write);

            let readers = OutputReaders::spawn(stdout_read, stderr_read);

            // Kill the group before reaping, while the zombie pins its pid.
            let _ = crate::proc::wait_exited(child, true);
            kill_process_group(child);
            let result = wait_child(child, jid, command);
            let (stdout, stderr) = readers.join();

            result.map(|code| (code, stdout, stderr))
        }
        Ok(ForkResult::Child) => piped_child(
            ChildPipes {
                stdout_read,
                stderr_read,
                stdout_write,
                stderr_write,
            },
            jid,
            &argv,
        ),
        Err(e) => Err(jexec_fork_error(e)),
    }
}

/// Run `command` in jail `jid` with stdio on /dev/null and wait for it.
///
/// For /etc/rc and friends: daemons would inherit pipes and block the reader forever.
pub fn jexec_run(jid: i32, command: &[&str]) -> Result<i32> {
    check_nonempty_command(command)?;
    let argv = ExecArgv::new(command)?;

    // SAFETY: child runs only async-signal-safe syscalls until exec/_exit.
    match unsafe { fork() } {
        Ok(ForkResult::Parent { child }) => wait_child(child, jid, command),
        Ok(ForkResult::Child) => {
            // SAFETY: static C string and plain fds.
            unsafe {
                let null = libc::open(c"/dev/null".as_ptr(), libc::O_RDWR);
                if null >= 0 {
                    libc::dup2(null, 0);
                    libc::dup2(null, 1);
                    libc::dup2(null, 2);
                    if null > 2 {
                        libc::close(null);
                    }
                }
            }
            attach_and_exec(jid, &argv);
        }
        Err(e) => Err(jexec_fork_error(e)),
    }
}

/// Argv built before `fork()`; the child must not allocate.
struct ExecArgv {
    args: Vec<CString>,
    /// NULL-terminated pointers into `args`.
    ptrs: Vec<*const libc::c_char>,
}

impl ExecArgv {
    fn new(command: &[&str]) -> Result<Self> {
        let args = command
            .iter()
            .map(|arg| CString::new(*arg))
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(|e| Error::CommandFailed {
                command: "jexec".to_string(),
                message: format!("Invalid argument string: {e}"),
            })?;
        let mut ptrs: Vec<*const libc::c_char> = args.iter().map(|s| s.as_ptr()).collect();
        ptrs.push(std::ptr::null());
        Ok(Self { args, ptrs })
    }
}

/// Close inherited fds, enter the jail, exec; exits 127 if exec fails.
fn attach_and_exec(jid: i32, argv: &ExecArgv) -> ! {
    // SAFETY: integer arg; the child never returns, so no OwnedFd drop follows.
    unsafe {
        libc::closefrom(3);
    }

    // SAFETY: integer arg.
    if unsafe { libc::jail_attach(jid) } != 0 {
        child_fail(b"jail_attach failed", 1);
    }

    // SAFETY: NULL-terminated argv, non-empty; FreeBSD execvp does not allocate.
    unsafe {
        libc::execvp(argv.args[0].as_ptr(), argv.ptrs.as_ptr());
    }
    child_fail(b"execvp failed", 127);
}

/// Write `what: errno N` to fd 2 and `_exit`, without allocating or locking.
fn child_fail(what: &[u8], code: libc::c_int) -> ! {
    let errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
    let mut digits = [0u8; 10];
    let mut n = errno.unsigned_abs();
    let mut start = digits.len();
    loop {
        start -= 1;
        digits[start] = b'0' + (n % 10) as u8;
        n /= 10;
        if n == 0 {
            break;
        }
    }
    let sep = b": errno ";
    // SAFETY: writes stay within each slice; both calls are async-signal-safe.
    unsafe {
        libc::write(2, what.as_ptr().cast(), what.len());
        libc::write(2, sep.as_ptr().cast(), sep.len());
        libc::write(2, digits[start..].as_ptr().cast(), digits.len() - start);
        libc::write(2, b"\n".as_ptr().cast(), 1);
        libc::_exit(code)
    }
}

fn wait_child(child: Pid, jid: i32, command: &[&str]) -> Result<i32> {
    exit_code_of(waitpid(child, None), || format!("jexec {jid} {command:?}"))
}

fn exit_code_of(status: nix::Result<WaitStatus>, label: impl FnOnce() -> String) -> Result<i32> {
    let message = match status {
        Ok(WaitStatus::Exited(_, exit_code)) => return Ok(exit_code),
        Ok(WaitStatus::Signaled(_, signal, _)) => format!("Process killed by signal {signal}"),
        Ok(status) => format!("Unexpected wait status: {status:?}"),
        Err(e) => format!("waitpid failed: {e}"),
    };
    Err(Error::CommandFailed {
        command: label(),
        message,
    })
}

/// SIGKILL the child's process group; false when no such group exists.
fn kill_process_group(child: Pid) -> bool {
    // SAFETY: plain integer args.
    unsafe { libc::kill(-child.as_raw(), libc::SIGKILL) == 0 }
}

/// Drains stdout and stderr concurrently so a full pipe never blocks the child.
struct OutputReaders {
    stdout: std::thread::JoinHandle<Vec<u8>>,
    stderr: std::thread::JoinHandle<Vec<u8>>,
}

impl OutputReaders {
    fn spawn(stdout: OwnedFd, stderr: OwnedFd) -> Self {
        Self {
            stdout: std::thread::spawn(move || read_fd_to_end(stdout)),
            stderr: std::thread::spawn(move || read_fd_to_end(stderr)),
        }
    }

    /// Blocks until EOF; kill the writers first.
    fn join(self) -> (Vec<u8>, Vec<u8>) {
        (
            self.stdout.join().unwrap_or_default(),
            self.stderr.join().unwrap_or_default(),
        )
    }
}

fn check_nonempty_command(command: &[&str]) -> Result<()> {
    if command.is_empty() {
        return Err(Error::CommandFailed {
            command: "jexec".to_string(),
            message: "Empty command".to_string(),
        });
    }
    Ok(())
}

fn jexec_fork_error(e: nix::errno::Errno) -> Error {
    Error::CommandFailed {
        command: "jexec".to_string(),
        message: format!("Fork failed: {e}"),
    }
}

fn jexec_pipe(label: &str) -> Result<(OwnedFd, OwnedFd)> {
    pipe().map_err(|e| Error::CommandFailed {
        command: "jexec".to_string(),
        message: format!("Failed to create {label} pipe: {e}"),
    })
}

/// Child side of a piped jexec; own process group so the parent can kill grandchildren.
fn piped_child(pipes: ChildPipes, jid: i32, argv: &ExecArgv) -> ! {
    drop(pipes.stdout_read);
    drop(pipes.stderr_read);
    // SAFETY: plain integer args.
    unsafe {
        libc::setpgid(0, 0);
    }
    redirect_child_io(pipes.stdout_write, pipes.stderr_write);
    attach_and_exec(jid, argv);
}

/// Reaper status is process-wide, so overlapping `chroot_exec` calls share it.
static REAPER_USERS: std::sync::Mutex<usize> = std::sync::Mutex::new(0);

/// Subreaper guard; the last holder releases.
struct Reaper;

impl Reaper {
    // Guard held across procctl so a concurrent Drop cannot release mid-acquire.
    #[allow(clippy::significant_drop_tightening)]
    fn acquire() -> Result<Self> {
        let mut users = REAPER_USERS
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if *users == 0 {
            // SAFETY: PROC_REAP_ACQUIRE takes no data; NULL is valid.
            let ret = unsafe {
                libc::procctl(
                    libc::P_PID as libc::idtype_t,
                    0, // self
                    libc::PROC_REAP_ACQUIRE,
                    std::ptr::null_mut(),
                )
            };
            if ret != 0 {
                return Err(Error::CommandFailed {
                    command: "chroot".to_string(),
                    message: format!(
                        "procctl(PROC_REAP_ACQUIRE) failed: {}",
                        std::io::Error::last_os_error()
                    ),
                });
            }
        }
        *users += 1;
        Ok(Self)
    }
}

impl Drop for Reaper {
    fn drop(&mut self) {
        let mut users = REAPER_USERS
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        *users -= 1;
        if *users == 0 {
            // Failure is harmless: we just keep reaping.
            // SAFETY: PROC_REAP_RELEASE takes no data; NULL is valid.
            unsafe {
                libc::procctl(
                    libc::P_PID as libc::idtype_t,
                    0,
                    libc::PROC_REAP_RELEASE,
                    std::ptr::null_mut(),
                );
            }
        }
    }
}

/// struct `procctl_reaper_pidinfo`
#[repr(C)]
#[allow(clippy::struct_field_names)] // mirrors struct procctl_reaper_pidinfo in <sys/procctl.h>
#[derive(Clone, Copy)]
struct ReaperPidInfo {
    pi_pid: libc::pid_t,
    pi_subtree: libc::pid_t,
    pi_flags: libc::c_uint,
    pi_pad0: [libc::c_uint; 15],
}

/// struct `procctl_reaper_pids`
#[repr(C)]
#[allow(clippy::struct_field_names)] // mirrors struct procctl_reaper_pids in <sys/procctl.h>
struct ReaperPids {
    rp_count: libc::c_uint,
    rp_pad0: [libc::c_uint; 15],
    rp_pids: *mut ReaperPidInfo,
}

const _: [(); crate::sys::consts::SIZEOF_PROCCTL_REAPER_PIDINFO] =
    [(); std::mem::size_of::<ReaperPidInfo>()];
const _: [(); crate::sys::consts::SIZEOF_PROCCTL_REAPER_PIDS] =
    [(); std::mem::size_of::<ReaperPids>()];
const _: [(); crate::sys::consts::OFFSETOF_PROCCTL_REAPER_PIDS_RP_PIDS] =
    [(); std::mem::offset_of!(ReaperPids, rp_pids)];

/// SIGKILL everything left in `subtree`, setsid'd included.
fn kill_subtree(subtree: nix::unistd::Pid) {
    #[repr(C)]
    #[allow(clippy::struct_field_names)] // mirrors struct procctl_reaper_kill in <sys/procctl.h>
    struct ProcctlReapKill {
        rk_sig: libc::c_int,
        rk_flags: libc::c_uint,
        rk_subtree: libc::pid_t,
        rk_killed: libc::c_uint,
        rk_fpid: libc::pid_t,
        rk_pad0: [libc::c_uint; 15],
    }
    const _: [(); crate::sys::consts::SIZEOF_PROCCTL_REAPER_KILL] =
        [(); std::mem::size_of::<ProcctlReapKill>()];
    let mut rk = ProcctlReapKill {
        rk_sig: libc::SIGKILL,
        rk_flags: crate::sys::consts::REAPER_KILL_SUBTREE,
        rk_subtree: subtree.as_raw(),
        rk_killed: 0,
        rk_fpid: 0,
        rk_pad0: [0; 15],
    };
    // ESRCH (nothing left) is expected; the reap loop confirms.
    // SAFETY: rk matches procctl_reaper_kill (size asserted) and outlives the call.
    unsafe {
        libc::procctl(
            libc::P_PID as libc::idtype_t,
            0,
            libc::PROC_REAP_KILL,
            (&raw mut rk).cast::<libc::c_void>(),
        );
    }
}

/// Kill and reap only `subtree`'s orphans; waitpid(-1) would steal unrelated children.
fn reap_subtree(subtree: nix::unistd::Pid) {
    const SLOTS: libc::c_uint = 256;
    const EMPTY: ReaperPidInfo = ReaperPidInfo {
        pi_pid: 0,
        pi_subtree: 0,
        pi_flags: 0,
        pi_pad0: [0; 15],
    };
    let mut infos = vec![EMPTY; SLOTS as usize];
    loop {
        // Every pass: a process may have forked before it died.
        kill_subtree(subtree);
        infos.fill(EMPTY);
        let mut req = ReaperPids {
            rp_count: SLOTS,
            rp_pad0: [0; 15],
            rp_pids: infos.as_mut_ptr(),
        };
        // SAFETY: req matches procctl_reaper_pids (layout asserted); rp_count slots writable.
        let ret = unsafe {
            libc::procctl(
                libc::P_PID as libc::idtype_t,
                0,
                libc::PROC_REAP_GETPIDS,
                (&raw mut req).cast(),
            )
        };
        if ret != 0 {
            return;
        }

        let ours = infos.iter().filter(|info| {
            let flags = info.pi_flags;
            flags & crate::sys::consts::REAPER_PIDINFO_VALID != 0
                && flags & crate::sys::consts::REAPER_PIDINFO_CHILD != 0
                && info.pi_subtree == subtree.as_raw()
        });
        let mut reaped = 0;
        for info in ours {
            // Blocking is fine: everything got SIGKILL.
            let _ = waitpid(nix::unistd::Pid::from_raw(info.pi_pid), None);
            reaped += 1;
        }
        if reaped == 0 {
            return;
        }
    }
}

struct ChildPipes {
    stdout_read: OwnedFd,
    stderr_read: OwnedFd,
    stdout_write: OwnedFd,
    stderr_write: OwnedFd,
}

/// Child-only: dup the write ends onto fd 1/2.
fn redirect_child_io(stdout_write: OwnedFd, stderr_write: OwnedFd) {
    // SAFETY: plain fds from open OwnedFds.
    unsafe {
        libc::dup2(stdout_write.as_raw_fd(), 1);
        libc::dup2(stderr_write.as_raw_fd(), 2);
    }
    drop(stdout_write);
    drop(stderr_write);
}

fn read_fd_to_end(fd: OwnedFd) -> Vec<u8> {
    let mut buffer = Vec::new();
    let mut file = std::fs::File::from(fd);
    // Partial output beats none.
    file.read_to_end(&mut buffer).ok();
    buffer
}

/// Like `jexec_with_output`, but fails with `Error::JailTimeout` after
/// `timeout_secs` (0 = no timeout).
pub fn jexec_with_timeout(
    jid: i32,
    command: &[&str],
    timeout_secs: u64,
) -> Result<(i32, String, String)> {
    check_nonempty_command(command)?;
    let argv = ExecArgv::new(command)?;

    let (stdout_read, stdout_write) = jexec_pipe("stdout")?;
    let (stderr_read, stderr_write) = jexec_pipe("stderr")?;

    // SAFETY: child runs only async-signal-safe syscalls until exec/_exit.
    match unsafe { fork() } {
        Ok(ForkResult::Parent { child }) => {
            drop(stdout_write);
            drop(stderr_write);

            let readers = OutputReaders::spawn(stdout_read, stderr_read);

            let clock = SystemClock::new();
            let deadline = if timeout_secs == 0 {
                Deadline::never()
            } else {
                Deadline::after(clock.now(), timeout_secs.saturating_mul(NS_PER_SECOND))
            };

            while crate::proc::wait_exited(child, false) == Ok(false) {
                if deadline.is_expired(clock.now()) {
                    if !kill_process_group(child) {
                        // No group yet (child not past setpgid).
                        let _ = nix::sys::signal::kill(child, nix::sys::signal::SIGKILL);
                    }
                    // Status is moot; we report the timeout.
                    let _ = waitpid(child, None);
                    readers.join();
                    return Err(Error::JailTimeout(timeout_secs));
                }
                std::thread::sleep(Duration::from_millis(10));
            }

            kill_process_group(child);
            let status = waitpid(child, None);
            let (stdout, stderr) = readers.join();
            let exit_code = exit_code_of(status, || format!("jexec {jid} {command:?}"))?;
            Ok((
                exit_code,
                crate::sys::string_from_utf8_lossy(stdout),
                crate::sys::string_from_utf8_lossy(stderr),
            ))
        }
        Ok(ForkResult::Child) => piped_child(
            ChildPipes {
                stdout_read,
                stderr_read,
                stdout_write,
                stderr_write,
            },
            jid,
            &argv,
        ),
        Err(e) => Err(jexec_fork_error(e)),
    }
}

/// Run `/bin/sh -c command` chrooted into `root_path`; returns (`exit_code`, stdout, stderr).
pub fn chroot_exec(
    root_path: &str,
    command: &str,
    env_vars: &[(String, String)],
) -> Result<(i32, Vec<u8>, Vec<u8>)> {
    let (stdout_read, stdout_write) = pipe().map_err(|e| Error::CommandFailed {
        command: "chroot".to_string(),
        message: format!("Failed to create stdout pipe: {e}"),
    })?;

    let (stderr_read, stderr_write) = pipe().map_err(|e| Error::CommandFailed {
        command: "chroot".to_string(),
        message: format!("Failed to create stderr pipe: {e}"),
    })?;

    let root_cstring = CString::new(root_path).map_err(|e| Error::CommandFailed {
        command: "chroot".to_string(),
        message: format!("Invalid path: {e}"),
    })?;

    // Built before fork: the child must not allocate.
    let arg_cmd = CString::new(command).map_err(|e| Error::CommandFailed {
        command: "chroot".to_string(),
        message: format!("Invalid command string: {e}"),
    })?;
    let sh_argv: [*const libc::c_char; 4] = [
        c"/bin/sh".as_ptr(),
        c"-c".as_ptr(),
        arg_cmd.as_ptr(),
        std::ptr::null(),
    ];
    let envp = ChildEnv::new(env_vars)?;

    // Subreaper, so RUN can't leave detached processes racing later steps.
    let _reaper = Reaper::acquire()?;

    // SAFETY: child runs only async-signal-safe syscalls until exec/_exit.
    match unsafe { fork() } {
        Ok(ForkResult::Parent { child }) => {
            drop(stdout_write);
            drop(stderr_write);

            let readers = OutputReaders::spawn(stdout_read, stderr_read);

            // Kill the group before reaping, while the zombie pins its pid.
            let _ = crate::proc::wait_exited(child, true);
            kill_process_group(child);
            let wait_result = waitpid(child, None);

            reap_subtree(child);

            let (stdout, stderr) = readers.join();

            exit_code_of(wait_result, || format!("chroot {root_path}"))
                .map(|code| (code, stdout, stderr))
        }
        Ok(ForkResult::Child) => {
            drop(stdout_read);
            drop(stderr_read);

            redirect_child_io(stdout_write, stderr_write);

            // SAFETY: integer arg; the child never returns, so no OwnedFd drop follows.
            unsafe {
                libc::closefrom(3);
            }

            // SAFETY: live NUL-terminated path.
            if unsafe { libc::chroot(root_cstring.as_ptr()) } != 0 {
                child_fail(b"chroot failed", 1);
            }

            // Fatal: a stale cwd is a chroot escape.
            // SAFETY: static NUL-terminated path.
            if unsafe { libc::chdir(c"/".as_ptr()) } != 0 {
                child_fail(b"chdir(\"/\") after chroot failed", 1);
            }

            // SAFETY: plain integer args.
            unsafe {
                libc::setpgid(0, 0);
            }

            // No privilege drop: RUN needs root.
            // SAFETY: NULL-terminated pointer arrays into live CStrings.
            unsafe {
                libc::execve(c"/bin/sh".as_ptr(), sh_argv.as_ptr(), envp.ptrs.as_ptr());
            }

            child_fail(b"execve /bin/sh failed", 127);
        }
        Err(e) => Err(Error::CommandFailed {
            command: "chroot".to_string(),
            message: format!("Fork failed: {e}"),
        }),
    }
}

/// Inherited env with `env_vars` on top (later wins), built before `fork()`.
struct ChildEnv {
    _vars: Vec<CString>,
    /// NULL-terminated pointers into `_vars`.
    ptrs: Vec<*const libc::c_char>,
}

impl ChildEnv {
    fn new(env_vars: &[(String, String)]) -> Result<Self> {
        use std::os::unix::ffi::OsStrExt;

        let invalid = |message: String| Error::CommandFailed {
            command: "chroot".to_string(),
            message,
        };
        let overridden = |key: &[u8]| env_vars.iter().any(|(k, _)| k.as_bytes() == key);

        let mut vars = Vec::new();
        for (key, value) in std::env::vars_os() {
            if overridden(key.as_bytes()) {
                continue;
            }
            let mut entry = key.as_bytes().to_vec();
            entry.push(b'=');
            entry.extend_from_slice(value.as_bytes());
            vars.push(CString::new(entry)?);
        }
        for (i, (key, value)) in env_vars.iter().enumerate() {
            if key.is_empty() || key.contains('=') {
                return Err(invalid(format!(
                    "Invalid environment variable name: {key:?}"
                )));
            }
            if env_vars[i + 1..].iter().any(|(k, _)| k == key) {
                continue;
            }
            let entry = CString::new(format!("{key}={value}"))
                .map_err(|e| invalid(format!("Invalid environment variable {key}: {e}")))?;
            vars.push(entry);
        }

        let mut ptrs: Vec<*const libc::c_char> = vars.iter().map(|v| v.as_ptr()).collect();
        ptrs.push(std::ptr::null());
        Ok(Self { _vars: vars, ptrs })
    }
}

#[cfg(test)]
mod tests;