bob 0.99.5

Fast, robust, powerful, user-friendly pkgsrc package builder
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
/*
 * Copyright (c) 2026 Jonathan Perkin <jonathan@perkin.org.uk>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

use crate::sandbox::Sandbox;
use anyhow::{Context, bail};
use std::fs;
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
use tracing::{debug, warn};

impl Sandbox {
    pub fn mount_bindfs(
        &self,
        src: &Path,
        dest: &Path,
        opts: &[&str],
    ) -> anyhow::Result<Option<ExitStatus>> {
        let cmd = self.config.bindfs();
        /*
         * pre_exec raises the NOFILE limit for the bindfs process so it
         * does not run out of file descriptors when mounting large
         * directory trees (e.g. Xcode SDKs).  The unsafe block is
         * required by the pre_exec API; setrlimit is async-signal-safe.
         */
        Ok(Some(unsafe {
            Command::new(cmd)
                .args(opts)
                .arg(src)
                .arg(dest)
                .process_group(0)
                .pre_exec(|| {
                    let mut rlim = libc::rlimit {
                        rlim_cur: 0,
                        rlim_max: 0,
                    };
                    if libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) == 0 {
                        rlim.rlim_cur = rlim.rlim_max;
                        libc::setrlimit(libc::RLIMIT_NOFILE, &rlim);
                    }
                    Ok(())
                })
                .status()
                .context(format!("Unable to execute {}", cmd))?
        }))
    }

    pub fn mount_devfs(
        &self,
        _src: &Path,
        dest: &Path,
        opts: &[&str],
    ) -> anyhow::Result<Option<ExitStatus>> {
        let cmd = "/sbin/mount_devfs";
        Ok(Some(
            Command::new(cmd)
                .arg("devfs")
                .args(opts)
                .arg(dest)
                .process_group(0)
                .status()
                .context(format!("Unable to execute {}", cmd))?,
        ))
    }

    pub fn mount_fdfs(
        &self,
        _src: &Path,
        _dest: &Path,
        _opts: &[&str],
    ) -> anyhow::Result<Option<ExitStatus>> {
        bail!("fd mounts are not supported on macOS");
    }

    pub fn mount_nfs(
        &self,
        src: &Path,
        dest: &Path,
        opts: &[&str],
    ) -> anyhow::Result<Option<ExitStatus>> {
        let cmd = "/sbin/mount_nfs";
        Ok(Some(
            Command::new(cmd)
                .args(opts)
                .arg(src)
                .arg(dest)
                .process_group(0)
                .status()
                .context(format!("Unable to execute {}", cmd))?,
        ))
    }

    pub fn mount_procfs(
        &self,
        _src: &Path,
        _dest: &Path,
        _opts: &[&str],
    ) -> anyhow::Result<Option<ExitStatus>> {
        bail!("procfs mounts are not supported on macOS");
    }

    pub fn mount_tmpfs(
        &self,
        _src: &Path,
        dest: &Path,
        opts: &[&str],
    ) -> anyhow::Result<Option<ExitStatus>> {
        let cmd = "/sbin/mount_tmpfs";
        let status = Command::new(cmd)
            .args(opts)
            .args(["-o", "nobrowse"])
            .arg(dest)
            .process_group(0)
            .status()
            .context(format!("Unable to execute {}", cmd))?;
        if status.success() {
            let fseventsd = dest.join(".fseventsd");
            let _ = fs::create_dir_all(&fseventsd);
            let _ = fs::File::create(fseventsd.join("no_log"));
        }
        Ok(Some(status))
    }

    /*
     * General unmount routine common to file system types that involve
     * mounted file systems.
     *
     * Uses diskutil unmount with retries (every second, up to 3 minutes).
     * Use process_group(0) to put diskutil in its own process group,
     * preventing it from receiving SIGINT when the user presses Ctrl+C.
     */
    fn unmount_common(&self, dest: &Path) -> anyhow::Result<Option<ExitStatus>> {
        let cmd = "/usr/sbin/diskutil";
        let max_retries = 180;
        let mut last_status = None;

        for attempt in 0..max_retries {
            let status = Command::new(cmd)
                .arg("unmount")
                .arg(dest)
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .process_group(0)
                .status()
                .context(format!("Unable to execute {}", cmd))?;

            if status.success() {
                if attempt > 0 {
                    debug!(
                        path = %dest.display(),
                        retries = attempt,
                        "Unmount succeeded after retries"
                    );
                }
                return Ok(Some(status));
            }

            last_status = Some(status);

            if attempt < max_retries - 1 {
                std::thread::sleep(std::time::Duration::from_secs(1));
            }
        }

        Ok(last_status)
    }

    pub fn unmount_bindfs(&self, dest: &Path) -> anyhow::Result<Option<ExitStatus>> {
        self.unmount_common(dest)
    }

    pub fn unmount_devfs(&self, dest: &Path) -> anyhow::Result<Option<ExitStatus>> {
        let cmd = "/sbin/umount";
        Ok(Some(
            Command::new(cmd)
                .arg(dest)
                .process_group(0)
                .status()
                .context(format!("Unable to execute {}", cmd))?,
        ))
    }

    /* Not actually supported but try to unmount it anyway. */
    pub fn unmount_fdfs(&self, dest: &Path) -> anyhow::Result<Option<ExitStatus>> {
        self.unmount_common(dest)
    }

    pub fn unmount_nfs(&self, dest: &Path) -> anyhow::Result<Option<ExitStatus>> {
        self.unmount_common(dest)
    }

    /* Not actually supported but try to unmount it anyway. */
    pub fn unmount_procfs(&self, dest: &Path) -> anyhow::Result<Option<ExitStatus>> {
        self.unmount_common(dest)
    }

    pub fn unmount_tmpfs(&self, dest: &Path) -> anyhow::Result<Option<ExitStatus>> {
        self.unmount_common(dest)
    }

    /**
     * Configure mDNSResponder to listen on a Unix socket inside the sandbox,
     * enabling DNS resolution within the chroot.
     *
     * macOS does not support name resolution inside a chroot by default
     * because the mDNSResponder listening socket is not available.  This
     * method modifies the mDNSResponder launchd plist to add an additional
     * Unix socket listener inside the sandbox.
     *
     * Uses a lock directory for concurrency safety when multiple sandboxes
     * are being created simultaneously.
     */
    pub fn create_mdns_listener(&self, id: usize) -> anyhow::Result<()> {
        let sandbox_path = self.path(id);
        let sock_path = sandbox_path.join("var/run/mDNSResponder");
        let plist = Path::new("/var/run/com.apple.mDNSResponder.plist");
        let plist_system = Path::new("/System/Library/LaunchDaemons/com.apple.mDNSResponder.plist");
        let pb = Path::new("/usr/libexec/PlistBuddy");
        let entry = "Sockets:Listeners";

        fs::create_dir_all(sandbox_path.join("var/run"))?;

        let add_plist = sandbox_path.join("var/run/add.plist");
        fs::write(
            &add_plist,
            format!(
                "<array>\n\
                 \t<dict>\n\
                 \t\t<key>SockFamily</key>\n\
                 \t\t<string>Unix</string>\n\
                 \t\t<key>SockPathName</key>\n\
                 \t\t<string>{}</string>\n\
                 \t\t<key>SockPathMode</key>\n\
                 \t\t<integer>438</integer>\n\
                 \t</dict>\n\
                 </array>\n",
                sock_path.display()
            ),
        )?;

        let lock = Path::new("/tmp/updatemdns.lock");
        let _guard = MdnsLock::acquire(lock)?;

        if !plist.exists() {
            fs::copy(plist_system, plist).with_context(|| {
                format!(
                    "Failed to copy {} to {}",
                    plist_system.display(),
                    plist.display()
                )
            })?;

            let import_plist = sandbox_path.join("var/run/import.plist");
            let output = Command::new(pb)
                .args([
                    "-x",
                    "-c",
                    &format!("Print {entry}"),
                    &plist.display().to_string(),
                ])
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .process_group(0)
                .output()
                .context("Failed to run PlistBuddy")?;
            if !output.status.success() {
                bail!(
                    "PlistBuddy Print failed: {}",
                    String::from_utf8_lossy(&output.stderr).trim()
                );
            }
            fs::write(&import_plist, &output.stdout)?;

            let plist_str = plist.display().to_string();
            let import_str = import_plist.display().to_string();
            let status = Command::new(pb)
                .args([
                    "-c",
                    &format!("Delete {entry}"),
                    "-c",
                    &format!("Add {entry} array"),
                    "-c",
                    &format!("Add {entry}:0 dict"),
                    "-c",
                    &format!("Merge {import_str} {entry}:0"),
                    "-c",
                    "Save",
                    &plist_str,
                ])
                .stdout(Stdio::null())
                .stderr(Stdio::piped())
                .process_group(0)
                .status()
                .context("Failed to run PlistBuddy")?;
            if !status.success() {
                bail!("PlistBuddy failed to convert Listeners to array");
            }
            let _ = fs::remove_file(&import_plist);

            let _ = Command::new("/bin/launchctl")
                .args(["unload", &plist_system.display().to_string()])
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .process_group(0)
                .status();
            let status = Command::new("/bin/launchctl")
                .args(["load", "-w", &plist_str])
                .stdout(Stdio::null())
                .stderr(Stdio::piped())
                .process_group(0)
                .status()
                .context("Failed to run launchctl load")?;
            if !status.success() {
                bail!("launchctl load failed for {}", plist.display());
            }
        }

        let plist_str = plist.display().to_string();
        let status = Command::new(pb)
            .args([
                "-c",
                &format!("Merge {} {entry}", add_plist.display()),
                &plist_str,
            ])
            .stdout(Stdio::null())
            .stderr(Stdio::piped())
            .process_group(0)
            .status()
            .context("Failed to run PlistBuddy")?;
        if !status.success() {
            bail!("PlistBuddy Merge failed");
        }

        Self::reload_mdns(plist)?;
        let _ = fs::remove_file(&add_plist);
        Ok(())
    }

    /**
     * Remove the mDNSResponder listener socket for this sandbox.
     */
    pub fn destroy_mdns_listener(&self, id: usize) -> anyhow::Result<()> {
        let sandbox_path = self.path(id);
        let sock_path = sandbox_path.join("var/run/mDNSResponder");
        let plist = Path::new("/var/run/com.apple.mDNSResponder.plist");
        let pb = Path::new("/usr/libexec/PlistBuddy");
        let entry = "Sockets:Listeners";

        if !plist.exists() {
            return Ok(());
        }

        let lock = Path::new("/tmp/updatemdns.lock");
        let _guard = MdnsLock::acquire(lock)?;

        let output = Command::new(pb)
            .args([
                "-c",
                &format!("Print {entry}"),
                &plist.display().to_string(),
            ])
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .process_group(0)
            .output()
            .context("Failed to run PlistBuddy")?;
        if !output.status.success() {
            return Ok(());
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let entries: Vec<_> = stdout.lines().filter(|l| l.contains("Dict {")).collect();
        let sock_str = sock_path.display().to_string();
        let plist_str = plist.display().to_string();

        for i in 0..entries.len() {
            let output = Command::new(pb)
                .args(["-c", &format!("Print {entry}:{i}:SockPathName"), &plist_str])
                .stdout(Stdio::piped())
                .stderr(Stdio::null())
                .process_group(0)
                .output()?;
            let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
            if path == sock_str {
                let status = Command::new(pb)
                    .args(["-c", &format!("Delete {entry}:{i}"), &plist_str])
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .process_group(0)
                    .status()?;
                if !status.success() {
                    warn!(sandbox = id, "Failed to remove mDNS listener entry");
                }
                break;
            }
        }

        Self::reload_mdns(plist)?;
        Ok(())
    }

    /**
     * Unload and reload the mDNSResponder plist.
     */
    fn reload_mdns(plist: &Path) -> anyhow::Result<()> {
        let plist_str = plist.display().to_string();
        let _ = Command::new("/bin/launchctl")
            .args(["unload", &plist_str])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .process_group(0)
            .status();
        let status = Command::new("/bin/launchctl")
            .args(["load", "-w", &plist_str])
            .stdout(Stdio::null())
            .stderr(Stdio::piped())
            .process_group(0)
            .status()
            .context("Failed to run launchctl")?;
        if !status.success() {
            bail!("launchctl load failed for {}", plist.display());
        }
        Ok(())
    }

    /**
     * Find PIDs of processes using files under the sandbox path.
     *
     * Uses `fuser` which matches by path, not filesystem.  Filters out
     * system daemons (Spotlight, notification services, etc.) that hold
     * file handles but must not be killed.
     */
    pub(super) fn find_pids(&self, sandbox: &Path) -> Vec<String> {
        const SKIP_LIST: &[&str] = &[
            "kextd",
            "mds",
            "mds_stores",
            "mdworker",
            "mdworker_shared",
            "notifyd",
        ];

        let output = Command::new("fuser")
            .arg(sandbox)
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .process_group(0)
            .output();
        let Ok(out) = output else { return vec![] };
        if !out.status.success() {
            return vec![];
        }
        let pids: Vec<String> = String::from_utf8_lossy(&out.stdout)
            .split_whitespace()
            .map(|s| s.to_string())
            .collect();
        if pids.is_empty() {
            return vec![];
        }

        let ps_output = Command::new("ps")
            .arg("-o")
            .arg("pid=,comm=")
            .arg("-p")
            .arg(pids.join(","))
            .process_group(0)
            .output();
        let Ok(ps_out) = ps_output else { return pids };

        String::from_utf8_lossy(&ps_out.stdout)
            .lines()
            .filter_map(|line| {
                let mut parts = line.split_whitespace();
                let pid = parts.next()?;
                let comm = parts.next()?;
                if SKIP_LIST.contains(&comm) {
                    debug!(pid, name = comm, "Skipping protected process");
                    None
                } else {
                    Some(pid.to_string())
                }
            })
            .collect()
    }
}

/**
 * RAII lock guard for mDNSResponder plist modification.
 *
 * Uses `mkdir` for atomic lock acquisition.  Automatically removes the
 * lock directory on drop.
 */
struct MdnsLock {
    path: PathBuf,
}

impl MdnsLock {
    fn acquire(path: &Path) -> anyhow::Result<Self> {
        use std::hash::BuildHasher;
        let max_retries = 60;
        let hasher = std::collections::hash_map::RandomState::new();
        for attempt in 1..=max_retries {
            if fs::create_dir(path).is_ok() {
                return Ok(Self {
                    path: path.to_path_buf(),
                });
            }
            if attempt < max_retries {
                let ms = 500 + (hasher.hash_one(attempt) % 500);
                std::thread::sleep(std::time::Duration::from_millis(ms));
            }
        }
        bail!(
            "Failed to acquire mDNS lock at {} after {} attempts",
            path.display(),
            max_retries
        );
    }
}

impl Drop for MdnsLock {
    fn drop(&mut self) {
        let _ = fs::remove_dir(&self.path);
    }
}