mxc-sdk 1.2.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use super::{
    exit_after_signal_cleanup, install, run_startup_tool, with_container_startup, LxcContainer,
};
use nix::sys::signal::{kill, killpg, pthread_sigmask, SigSet, SigmaskHow, Signal};
use nix::sys::wait::{waitid, Id, WaitPidFlag, WaitStatus};
use nix::unistd::Pid;
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::process::Command;
use std::time::{Duration, Instant};

#[test]
fn startup_timeout_finishes_cleanup_before_executor_exit() {
    const FIXTURE_ENV: &str = "MXC_STARTUP_CLEANUP_TEST_DIRECTORY";
    const CONTAINER_NAME: &str = "startup-timeout";

    if let Some(directory) = std::env::var_os(FIXTURE_ENV) {
        let directory = PathBuf::from(directory);
        fs::write(
            directory.join("executor.pid"),
            format!("{}\n", std::process::id()),
        )
        .expect("record the isolated executor PID");
        install().expect("install cleanup in the isolated executor");
        let container = LxcContainer::new(
            CONTAINER_NAME,
            Some(directory.to_str().expect("UTF-8 container directory")),
        );
        let result = with_container_startup(&container, || {
            std::thread::sleep(Duration::from_secs(62));
            Ok(())
        });
        if result.is_err() {
            exit_after_signal_cleanup(1);
        }
        return;
    }

    let fixture = tempfile::tempdir_in(".").expect("create isolated cleanup fixture");
    let directory = fixture
        .path()
        .canonicalize()
        .expect("absolute fixture path");
    let tools = directory.join("bin");
    fs::create_dir(&tools).expect("create fake cleanup tool directory");
    fs::create_dir(directory.join(CONTAINER_NAME)).expect("create fake container directory");
    fs::write(directory.join(CONTAINER_NAME).join("config"), "")
        .expect("write fake container configuration");

    // Reject completion after the executor has already exited.
    let cleanup_script = r#"#!/bin/sh
read -r executor_pid < "$MXC_STARTUP_CLEANUP_TEST_DIRECTORY/executor.pid" || exit 1
/bin/sleep 2
read -r _ _ state _ < "/proc/$executor_pid/stat" || exit 1
case "$state" in Z|X|x) exit 1 ;; esac
: > "$MXC_STARTUP_CLEANUP_TEST_DIRECTORY/cleanup-complete"
"#;
    for name in ["lxc-stop", "lxc-destroy"] {
        let path = tools.join(name);
        fs::write(&path, cleanup_script).expect("write fake cleanup tool");
        fs::set_permissions(path, fs::Permissions::from_mode(0o755))
            .expect("make fake cleanup tool executable");
    }

    let log_path = directory.join("executor.log");
    let log = fs::File::create(&log_path).expect("create executor log");
    let thread = std::thread::current();
    let test_name = thread.name().expect("fully qualified libtest name");
    let mut command = Command::new(std::env::current_exe().expect("test executable"));
    command
        .args(["--exact", test_name, "--nocapture", "--test-threads=1"])
        .env(FIXTURE_ENV, &directory)
        .env("PATH", &tools)
        .stdout(log.try_clone().expect("clone executor log"))
        .stderr(log)
        .process_group(0);

    // The child's main thread must inherit the mask before libtest starts its worker.
    let mut original_mask = SigSet::empty();
    pthread_sigmask(
        SigmaskHow::SIG_BLOCK,
        Some(&SigSet::all()),
        Some(&mut original_mask),
    )
    .expect("block signals while spawning the isolated executor");
    let started = Instant::now();
    let child = command.spawn();
    pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&original_mask), None)
        .expect("restore the parent test thread's signal mask");
    let mut child = child.expect("spawn isolated executor");
    let process_group = Pid::from_raw(child.id() as i32);
    let completed_path = directory.join("cleanup-complete");
    let exit_poll_flags = WaitPidFlag::WEXITED | WaitPidFlag::WNOHANG | WaitPidFlag::WNOWAIT;

    let completed = loop {
        if !matches!(
            waitid(Id::Pid(process_group), exit_poll_flags)
                .expect("observe isolated executor exit without reaping"),
            WaitStatus::StillAlive
        ) {
            break completed_path.exists();
        }
        if started.elapsed() >= Duration::from_secs(90) {
            let _ = killpg(process_group, Signal::SIGKILL);
            child.wait().expect("reap hung executor");
            panic!(
                "startup timeout did not finish cleanup and exit within 90 seconds:\n{}",
                fs::read_to_string(&log_path).expect("read executor log")
            );
        }
        std::thread::sleep(Duration::from_millis(10));
    };
    let elapsed = started.elapsed();
    let _ = killpg(process_group, Signal::SIGKILL);
    let status = child
        .wait()
        .expect("reap isolated executor after its final signal");

    assert!(
        elapsed >= Duration::from_secs(60) && !status.success() && completed,
        "a successful 62-second startup must time out, finish cleanup, and exit unsuccessfully; \
         elapsed={elapsed:?}, status={status}, cleanup_completed_before_exit={completed}\n{}",
        fs::read_to_string(log_path).expect("read executor log")
    );
}

#[test]
fn sigint_finishes_startup_command_cleanup_before_executor_exit() {
    const FIXTURE_ENV: &str = "MXC_STARTUP_COMMAND_CLEANUP_TEST_DIRECTORY";
    const CONTAINER_NAME: &str = "startup-command-cancellation";

    if let Some(directory) = std::env::var_os(FIXTURE_ENV) {
        let directory = PathBuf::from(directory);
        fs::write(
            directory.join("executor.pid"),
            format!("{}\n", std::process::id()),
        )
        .expect("record the isolated executor PID");
        install().expect("install cleanup in the isolated executor");
        let container = LxcContainer::new(
            CONTAINER_NAME,
            Some(directory.to_str().expect("UTF-8 container directory")),
        );
        let result = with_container_startup(&container, || {
            let mut command = Command::new("/bin/sh");
            command
                .arg("-c")
                .arg(
                    r#"trap 'exit 130' INT
trap 'exit 143' TERM
trap '
    trap "" HUP INT QUIT TERM
    : > "$MXC_STARTUP_COMMAND_CLEANUP_TEST_DIRECTORY/command-cleanup-begun"
    /bin/sleep 6 || exit 1

    # Reject completion after the executor has exited.
    read -r executor_pid < "$MXC_STARTUP_COMMAND_CLEANUP_TEST_DIRECTORY/executor.pid" || exit 1
    read -r _ _ state _ < "/proc/$executor_pid/stat" || exit 1
    case "$state" in Z|X|x) exit 1 ;; esac
    : > "$MXC_STARTUP_COMMAND_CLEANUP_TEST_DIRECTORY/command-cleanup-complete"
' EXIT
printf '%s\n' "$$" > "$MXC_STARTUP_COMMAND_CLEANUP_TEST_DIRECTORY/command.pid"
: > "$MXC_STARTUP_COMMAND_CLEANUP_TEST_DIRECTORY/command-working"
if [ "$MXC_STARTUP_COMMAND_SIGNAL_AFTER" = command-cleanup-begun ]; then
    exit 1
fi
work_seconds_remaining=45
while [ "$work_seconds_remaining" -gt 0 ]; do
    /bin/sleep 1
    work_seconds_remaining=$((work_seconds_remaining - 1))
done
"#,
                )
                .process_group(0);
            run_startup_tool(command, Duration::from_secs(120)).map(|_| ())
        });
        if let Err(error) = result {
            eprintln!("{error}");
            exit_after_signal_cleanup(1);
        }
        return;
    }

    for signal_after in ["command-working", "command-cleanup-begun"] {
        let fixture = tempfile::tempdir_in(".").expect("create isolated cancellation fixture");
        let directory = fixture
            .path()
            .canonicalize()
            .expect("absolute fixture path");
        let tools = directory.join("bin");
        fs::create_dir(&tools).expect("create fake cleanup tool directory");
        fs::create_dir(directory.join(CONTAINER_NAME)).expect("create fake container directory");
        fs::write(directory.join(CONTAINER_NAME).join("config"), "")
            .expect("write fake container configuration");
        for name in ["lxc-stop", "lxc-destroy"] {
            let path = tools.join(name);
            fs::write(&path, "#!/bin/sh\nexit 0\n").expect("write fake cleanup tool");
            fs::set_permissions(path, fs::Permissions::from_mode(0o755))
                .expect("make fake cleanup tool executable");
        }

        let log_path = directory.join("executor.log");
        let log = fs::File::create(&log_path).expect("create executor log");
        let thread = std::thread::current();
        let test_name = thread.name().expect("fully qualified libtest name");
        let mut command = Command::new(std::env::current_exe().expect("test executable"));
        command
            .args(["--exact", test_name, "--nocapture", "--test-threads=1"])
            .env(FIXTURE_ENV, &directory)
            .env("MXC_STARTUP_COMMAND_SIGNAL_AFTER", signal_after)
            .env("PATH", &tools)
            .stdout(log.try_clone().expect("clone executor log"))
            .stderr(log)
            .process_group(0);

        // The child's main thread must inherit the mask before libtest starts its worker.
        let mut original_mask = SigSet::empty();
        pthread_sigmask(
            SigmaskHow::SIG_BLOCK,
            Some(&SigSet::all()),
            Some(&mut original_mask),
        )
        .expect("block signals while spawning the isolated executor");
        let started = Instant::now();
        let child = command.spawn();
        pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&original_mask), None)
            .expect("restore the parent test thread's signal mask");
        let mut child = child.expect("spawn isolated executor");
        let process_group = Pid::from_raw(child.id() as i32);
        let exit_poll_flags = WaitPidFlag::WEXITED | WaitPidFlag::WNOHANG | WaitPidFlag::WNOWAIT;

        while !directory.join(signal_after).exists() {
            if !matches!(
                waitid(Id::Pid(process_group), exit_poll_flags)
                    .expect("observe isolated executor exit without reaping"),
                WaitStatus::StillAlive
            ) || started.elapsed() >= Duration::from_secs(45)
            {
                let _ = killpg(process_group, Signal::SIGKILL);
                child.wait().expect("reap isolated executor");
                panic!(
                    "startup command did not reach {signal_after} before SIGINT:\n{}",
                    fs::read_to_string(&log_path).expect("read executor log")
                );
            }
            std::thread::sleep(Duration::from_millis(10));
        }
        let command_group = Pid::from_raw(
            fs::read_to_string(directory.join("command.pid"))
                .expect("read owned startup command PID")
                .trim()
                .parse::<i32>()
                .expect("owned startup command PID"),
        );
        let signal_result = kill(process_group, Signal::SIGINT);

        loop {
            if !matches!(
                waitid(Id::Pid(process_group), exit_poll_flags)
                    .expect("observe isolated executor exit without reaping"),
                WaitStatus::StillAlive
            ) {
                break;
            }
            if started.elapsed() >= Duration::from_secs(45) {
                let _ = killpg(process_group, Signal::SIGKILL);
                child.wait().expect("reap hung executor");
                panic!(
                    "SIGINT after {signal_after} did not finish startup command cleanup and exit:\n{}",
                    fs::read_to_string(&log_path).expect("read executor log")
                );
            }
            std::thread::sleep(Duration::from_millis(10));
        }
        let completed = directory.join("command-cleanup-complete").exists();
        let live_command_processes = || {
            fs::read_dir("/proc")
                .expect("inspect owned startup command processes")
                .filter_map(Result::ok)
                .filter_map(|process| fs::read_to_string(process.path().join("stat")).ok())
                .filter(|stat| {
                    let Some((_, fields)) = stat.rsplit_once(')') else {
                        return false;
                    };
                    let mut fields = fields.split_whitespace();
                    let state = fields.next();
                    let group = fields.nth(1).and_then(|group| group.parse::<i32>().ok());
                    group == Some(command_group.as_raw()) && !matches!(state, Some("Z" | "X" | "x"))
                })
                .collect::<Vec<_>>()
        };
        let live_helpers = live_command_processes();
        let _ = killpg(process_group, Signal::SIGKILL);
        let status = child
            .wait()
            .expect("reap isolated executor after its final signal");
        while !live_command_processes().is_empty() && started.elapsed() < Duration::from_secs(90) {
            std::thread::sleep(Duration::from_millis(10));
        }

        assert!(
            signal_result.is_ok() && !status.success() && completed && live_helpers.is_empty(),
            "SIGINT during startup ({signal_after}) must finish the command's six-second EXIT cleanup before \
             executor exit without leaving live owned helpers; signal={signal_result:?}, \
             status={status}, cleanup_completed_before_exit={completed}, \
             live_helpers={live_helpers:?}\n{}",
            fs::read_to_string(log_path).expect("read executor log")
        );
    }
}

#[test]
fn startup_command_preserves_complete_stdout() {
    const FIXTURE_ENV: &str = "MXC_STARTUP_COMMAND_OUTPUT_TEST_DIRECTORY";
    const CONTAINER_NAME: &str = "startup-command-output";

    if let Some(directory) = std::env::var_os(FIXTURE_ENV) {
        let directory = PathBuf::from(directory);
        install().expect("install cleanup in the isolated executor");
        let container = LxcContainer::new(
            CONTAINER_NAME,
            Some(directory.to_str().expect("UTF-8 container directory")),
        );
        let expected = "0123456789abcdef".repeat(2048);
        let mut stdout = Vec::new();
        with_container_startup(&container, || {
            let mut command = Command::new("/bin/sh");
            command
                .args(["-c", "printf '%s' \"$1\"", "startup-stdout", &expected])
                .process_group(0);
            stdout = run_startup_tool(command, Duration::from_secs(10))?.stdout;
            Ok(())
        })
        .expect("complete startup command");

        assert_eq!(
            stdout,
            expected.as_bytes(),
            "a normally completed startup command must return its complete 32 KiB stdout"
        );
        return;
    }

    let fixture = tempfile::tempdir_in(".").expect("create isolated stdout fixture");
    let directory = fixture
        .path()
        .canonicalize()
        .expect("absolute fixture path");
    let tools = directory.join("bin");
    fs::create_dir(&tools).expect("create fake cleanup tool directory");
    fs::create_dir(directory.join(CONTAINER_NAME)).expect("create fake container directory");
    fs::write(directory.join(CONTAINER_NAME).join("config"), "")
        .expect("write fake container configuration");
    for name in ["lxc-stop", "lxc-destroy"] {
        let path = tools.join(name);
        fs::write(&path, "#!/bin/sh\nexit 0\n").expect("write fake cleanup tool");
        fs::set_permissions(path, fs::Permissions::from_mode(0o755))
            .expect("make fake cleanup tool executable");
    }

    let log_path = directory.join("executor.log");
    let log = fs::File::create(&log_path).expect("create executor log");
    let thread = std::thread::current();
    let test_name = thread.name().expect("fully qualified libtest name");
    let mut command = Command::new(std::env::current_exe().expect("test executable"));
    command
        .args(["--exact", test_name, "--nocapture", "--test-threads=1"])
        .env(FIXTURE_ENV, &directory)
        .env("PATH", &tools)
        .stdout(log.try_clone().expect("clone executor log"))
        .stderr(log)
        .process_group(0);

    let mut original_mask = SigSet::empty();
    pthread_sigmask(
        SigmaskHow::SIG_BLOCK,
        Some(&SigSet::all()),
        Some(&mut original_mask),
    )
    .expect("block signals while spawning the isolated executor");
    let started = Instant::now();
    let child = command.spawn();
    pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&original_mask), None)
        .expect("restore the parent test thread's signal mask");
    let mut child = child.expect("spawn isolated executor");
    let process_group = Pid::from_raw(child.id() as i32);
    let exit_poll_flags = WaitPidFlag::WEXITED | WaitPidFlag::WNOHANG | WaitPidFlag::WNOWAIT;

    while matches!(
        waitid(Id::Pid(process_group), exit_poll_flags)
            .expect("observe isolated executor exit without reaping"),
        WaitStatus::StillAlive
    ) {
        if started.elapsed() >= Duration::from_secs(30) {
            let _ = killpg(process_group, Signal::SIGKILL);
            child.wait().expect("reap hung executor");
            panic!(
                "startup command did not return its stdout within 30 seconds:\n{}",
                fs::read_to_string(&log_path).expect("read executor log")
            );
        }
        std::thread::sleep(Duration::from_millis(10));
    }
    let _ = killpg(process_group, Signal::SIGKILL);
    let status = child.wait().expect("reap isolated executor");

    assert!(
        status.success(),
        "startup command did not preserve its complete stdout:\n{}",
        fs::read_to_string(log_path).expect("read executor log")
    );
}