pacman-key 0.4.1

Native Rust interface for managing the pacman keyring on Arch 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
use std::io;
use std::process::{ExitStatus, Output, Stdio};
use std::time::Duration;
use tokio::io::{AsyncRead, AsyncReadExt};
use tokio::process::{Child, Command};
use tokio::time::Instant;

use crate::{CancellationToken, Error, OperationOptions, Result};

pub(crate) const STDERR_LIMIT: u64 = 1024 * 1024;
const TRUNCATED: &[u8] = b"\n[output truncated]\n";

#[derive(Default)]
pub(crate) struct Operation {
    deadline: Option<(Instant, u64)>,
    cancel: Option<CancellationToken>,
}

impl Operation {
    pub(crate) fn new(options: OperationOptions) -> Self {
        let deadline = options
            .timeout_secs
            .filter(|seconds| *seconds > 0)
            .and_then(|seconds| {
                Some((
                    Instant::now().checked_add(Duration::from_secs(seconds))?,
                    seconds,
                ))
            });
        Self {
            deadline,
            cancel: options.cancel_token,
        }
    }

    fn check(&self) -> Result<()> {
        if self
            .cancel
            .as_ref()
            .is_some_and(CancellationToken::is_cancelled)
        {
            return Err(Error::Cancelled);
        }
        match self.deadline {
            Some((deadline, seconds)) if Instant::now() >= deadline => Err(Error::Timeout(seconds)),
            _ => Ok(()),
        }
    }

    async fn wait<F: Future>(&self, future: F) -> Result<F::Output> {
        let timeout = async {
            match self.deadline {
                Some((deadline, seconds)) => {
                    tokio::time::sleep_until(deadline).await;
                    seconds
                }
                None => std::future::pending().await,
            }
        };
        let cancel = async {
            match &self.cancel {
                Some(cancel) => cancel.cancelled().await,
                None => std::future::pending().await,
            }
        };
        tokio::select! {
            biased;
            _ = cancel => Err(Error::Cancelled),
            seconds = timeout => Err(Error::Timeout(seconds)),
            result = future => Ok(result),
        }
    }
}

struct ProcessGroup {
    leader: Child,
    watcher: Child,
}

impl ProcessGroup {
    // The watcher leads the group and kills it when its stdin, which only this
    // process holds, reaches EOF because this process has terminated.
    fn spawn(command: &mut Command) -> io::Result<Self> {
        let watcher = Command::new("/bin/sh")
            .args(["-c", "read line; kill -s KILL 0"])
            .stdin(Stdio::piped())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .process_group(0)
            .kill_on_drop(true)
            .spawn()?;
        let group = watcher.id().expect("an unwaited child has an id") as libc::pid_t;
        // Callers match English stderr text.
        let leader = command.env("LC_ALL", "C").process_group(group).spawn()?;
        Ok(Self { leader, watcher })
    }
}

impl Drop for ProcessGroup {
    fn drop(&mut self) {
        // Armed until the leader is reaped; the unreaped watcher keeps the group ID valid.
        if let (Some(_), Some(group)) = (self.leader.id(), self.watcher.id()) {
            unsafe { libc::killpg(group as libc::pid_t, libc::SIGKILL) };
            let _ = self.leader.start_kill();
        }
    }
}

// Kills the child's process group unless `body` succeeds and the leader is
// reaped, including when this future is dropped.
pub(crate) async fn run<T>(
    command: &mut Command,
    operation: &Operation,
    body: impl AsyncFnOnce(&mut Child) -> Result<T>,
) -> Result<(ExitStatus, T)> {
    operation.check()?;
    let mut process = ProcessGroup::spawn(command)?;
    let value = operation.wait(body(&mut process.leader)).await??;
    let status = operation.wait(process.leader.wait()).await??;
    // Before its stdin closes, so the watcher leaves the rest of the group alone.
    let _ = process.watcher.start_kill();
    Ok((status, value))
}

pub(crate) async fn collect(reader: impl AsyncRead + Unpin, limit: u64) -> io::Result<Vec<u8>> {
    let mut output = Vec::new();
    let mut retained = reader.take(limit);
    retained.read_to_end(&mut output).await?;
    if tokio::io::copy(&mut retained.into_inner(), &mut tokio::io::sink()).await? > 0 {
        output.extend_from_slice(TRUNCATED);
    }
    Ok(output)
}

pub(crate) async fn output(command: &mut Command, operation: &Operation) -> Result<Output> {
    let command = command.stdout(Stdio::piped()).stderr(Stdio::piped());
    let (status, (stdout, stderr)) = run(command, operation, async |child| {
        let stdout = child.stdout.take().expect("stdout is piped");
        let stderr = child.stderr.take().expect("stderr is piped");
        tokio::try_join!(collect(stdout, u64::MAX), collect(stderr, STDERR_LIMIT))
            .map_err(Error::Command)
    })
    .await?;
    Ok(Output {
        status,
        stdout,
        stderr,
    })
}

pub(crate) async fn stderr_only(
    command: &mut Command,
    operation: &Operation,
) -> Result<(ExitStatus, Vec<u8>)> {
    let command = command.stdout(Stdio::null()).stderr(Stdio::piped());
    run(command, operation, async |child| {
        let stderr = child.stderr.take().expect("stderr is piped");
        collect(stderr, STDERR_LIMIT).await.map_err(Error::Command)
    })
    .await
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use std::path::PathBuf;

    pub(crate) struct Fixture {
        directory: tempfile::TempDir,
    }

    impl Fixture {
        pub(crate) fn new() -> Self {
            Self {
                directory: tempfile::tempdir().unwrap(),
            }
        }

        pub(crate) fn path(&self, name: &str) -> PathBuf {
            self.directory.path().join(name)
        }

        pub(crate) fn command(&self, script: &str) -> Command {
            let mut command = Command::new("/bin/sh");
            command
                .args(["-c", script, "keyring-fixture"])
                .arg(self.directory.path());
            command
        }

        pub(crate) async fn ready(&self) {
            let path = self.path("ready");
            until(|| path.exists(), "fixture process did not become ready").await;
        }

        fn pid(&self, name: &str) -> u32 {
            std::fs::read_to_string(self.path(name))
                .unwrap()
                .trim()
                .parse()
                .unwrap()
        }

        pub(crate) async fn stopped(&self) {
            let mut names = vec!["parent"];
            if self.path("grandchild").exists() {
                names.push("grandchild");
            }
            for name in names {
                let pid = self.pid(name);
                until(|| !running(pid), "fixture process survived cleanup").await;
            }
        }
    }

    fn running(pid: u32) -> bool {
        std::fs::read_to_string(format!("/proc/{pid}/stat")).is_ok_and(|stat| {
            stat.rsplit_once(") ")
                .is_some_and(|(_, rest)| !rest.starts_with('Z'))
        })
    }

    async fn until(done: impl Fn() -> bool, failure: &str) {
        tokio::time::timeout(Duration::from_secs(5), async {
            while !done() {
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        })
        .await
        .expect(failure);
    }

    pub(crate) const HUNG_GROUP: &str = r#"
echo $$ > "$1/parent"
sh -c 'echo $$ > "$1/grandchild"; touch "$1/ready"; exec sleep 60' keyring-grandchild "$1" &
wait
"#;

    #[tokio::test]
    async fn timeout_kills_a_child_and_grandchild_holding_stderr() {
        let fixture = Fixture::new();
        let operation = Operation::new(OperationOptions {
            timeout_secs: Some(1),
            cancel_token: None,
        });
        let result = tokio::time::timeout(
            Duration::from_secs(5),
            output(&mut fixture.command(HUNG_GROUP), &operation),
        )
        .await
        .unwrap();
        assert!(matches!(result, Err(Error::Timeout(1))), "{result:?}");
        fixture.stopped().await;
    }

    #[tokio::test]
    async fn timeout_covers_stderr_held_open_after_the_parent_exits() {
        let fixture = Fixture::new();
        let script = HUNG_GROUP.replace("\nwait\n", "\nexit 0\n");
        assert_ne!(script, HUNG_GROUP);
        let operation = Operation::new(OperationOptions {
            timeout_secs: Some(1),
            cancel_token: None,
        });
        let result = tokio::time::timeout(
            Duration::from_secs(5),
            output(&mut fixture.command(&script), &operation),
        )
        .await
        .unwrap();
        assert!(matches!(result, Err(Error::Timeout(1))), "{result:?}");
        fixture.stopped().await;
    }

    #[tokio::test]
    async fn cancellation_kills_the_process_group() {
        let fixture = Fixture::new();
        let cancel = CancellationToken::new();
        let operation = Operation::new(OperationOptions {
            timeout_secs: None,
            cancel_token: Some(cancel.clone()),
        });
        let mut command = fixture.command(HUNG_GROUP);
        let task = tokio::spawn(async move { output(&mut command, &operation).await });
        fixture.ready().await;
        cancel.cancel();
        let result = tokio::time::timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap();
        assert!(matches!(result, Err(Error::Cancelled)), "{result:?}");
        fixture.stopped().await;
    }

    #[test]
    fn cancellation_does_not_need_the_time_driver() {
        let cancel = CancellationToken::new();
        let operation = Operation::new(OperationOptions {
            timeout_secs: None,
            cancel_token: Some(cancel.clone()),
        });
        let canceller = std::thread::spawn(move || {
            std::thread::sleep(Duration::from_millis(100));
            cancel.cancel();
        });
        let result = tokio::runtime::Builder::new_current_thread()
            .enable_io()
            .build()
            .unwrap()
            .block_on(output(Command::new("sleep").arg("5"), &operation));
        canceller.join().unwrap();
        assert!(matches!(result, Err(Error::Cancelled)), "{result:?}");
    }

    #[tokio::test]
    async fn cancellation_wins_over_a_ready_future() {
        let cancel = CancellationToken::new();
        cancel.cancel();
        let operation = Operation::new(OperationOptions {
            timeout_secs: None,
            cancel_token: Some(cancel),
        });
        for _ in 0..64 {
            let result = operation.wait(std::future::ready(())).await;
            assert!(matches!(result, Err(Error::Cancelled)), "{result:?}");
        }
    }

    #[tokio::test]
    async fn aborting_output_kills_its_process_group() {
        let fixture = Fixture::new();
        let mut command = fixture.command(HUNG_GROUP);
        let task = tokio::spawn(async move { output(&mut command, &Operation::default()).await });
        fixture.ready().await;
        task.abort();
        assert!(task.await.unwrap_err().is_cancelled());
        fixture.stopped().await;
    }

    #[tokio::test]
    async fn losing_the_caller_kills_the_group() {
        let fixture = Fixture::new();
        let mut process = ProcessGroup::spawn(&mut fixture.command(HUNG_GROUP)).unwrap();
        fixture.ready().await;
        drop(process.watcher.stdin.take());
        fixture.stopped().await;
    }

    #[tokio::test]
    async fn stderr_is_drained_with_bounded_retained_output() {
        let fixture = Fixture::new();
        let operation = Operation::new(OperationOptions {
            timeout_secs: Some(5),
            cancel_token: None,
        });
        let script = format!("head -c {} /dev/zero >&2", 2 * STDERR_LIMIT);
        let result = output(&mut fixture.command(&script), &operation)
            .await
            .unwrap();
        assert!(result.status.success());
        assert_eq!(
            result.stderr.len() as u64,
            STDERR_LIMIT + TRUNCATED.len() as u64
        );
    }

    #[tokio::test]
    async fn stdout_is_not_truncated() {
        let fixture = Fixture::new();
        let script = format!("head -c {} /dev/zero", 2 * STDERR_LIMIT);
        let result = output(&mut fixture.command(&script), &Operation::default())
            .await
            .unwrap();
        assert!(result.status.success());
        assert_eq!(result.stdout.len() as u64, 2 * STDERR_LIMIT);
    }

    #[tokio::test]
    async fn a_cancelled_operation_does_not_start_a_child() {
        let fixture = Fixture::new();
        let cancel = CancellationToken::new();
        cancel.cancel();
        let operation = Operation::new(OperationOptions {
            timeout_secs: None,
            cancel_token: Some(cancel),
        });
        let result = output(&mut Command::new(fixture.path("missing")), &operation).await;
        assert!(matches!(result, Err(Error::Cancelled)), "{result:?}");
    }

    #[tokio::test]
    async fn an_unrepresentable_timeout_means_no_timeout() {
        let operation = Operation::new(OperationOptions {
            timeout_secs: Some(u64::MAX),
            cancel_token: None,
        });
        let result = output(&mut Command::new("true"), &operation).await.unwrap();
        assert!(result.status.success());
    }

    #[tokio::test]
    async fn a_failed_command_returns_its_status_and_output() {
        let fixture = Fixture::new();
        let result = output(
            &mut fixture.command("echo out; echo boom >&2; exit 3"),
            &Operation::default(),
        )
        .await
        .unwrap();
        assert_eq!(result.status.code(), Some(3));
        assert_eq!(result.stdout, b"out\n");
        assert_eq!(result.stderr, b"boom\n");
    }

    #[tokio::test]
    async fn a_missing_program_is_a_command_error() {
        let fixture = Fixture::new();
        let result = output(
            &mut Command::new(fixture.path("missing")),
            &Operation::default(),
        )
        .await;
        assert!(
            matches!(&result, Err(Error::Command(e)) if e.kind() == io::ErrorKind::NotFound),
            "{result:?}"
        );
    }

    #[tokio::test]
    async fn success_leaves_the_reaped_group_alone() {
        let fixture = Fixture::new();
        let script = r#"sleep 5 >/dev/null 2>&1 & echo $! > "$1/background""#;
        let result = output(&mut fixture.command(script), &Operation::default())
            .await
            .unwrap();
        assert!(result.status.success());
        tokio::time::sleep(Duration::from_millis(200)).await;
        let background = fixture.pid("background");
        let alive = running(background);
        std::process::Command::new("kill")
            .arg(background.to_string())
            .status()
            .unwrap();
        assert!(alive);
    }

    #[tokio::test]
    async fn children_run_in_the_c_locale() {
        let fixture = Fixture::new();
        let mut command = fixture.command(r#"printf %s "$LC_ALL""#);
        command.env("LC_ALL", "de_DE.UTF-8");
        let result = output(&mut command, &Operation::default()).await.unwrap();
        assert_eq!(result.stdout, b"C");
    }

    #[tokio::test]
    async fn stderr_only_sends_stdout_to_dev_null() {
        let fixture = Fixture::new();
        let (status, stderr) = stderr_only(
            &mut fixture.command(r#"readlink "/proc/$$/fd/1" >&2"#),
            &Operation::default(),
        )
        .await
        .unwrap();
        assert!(status.success());
        assert_eq!(stderr, b"/dev/null\n");
    }
}