runc/
lib.rs

1/*
2   Copyright The containerd Authors.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17// Forked from https://github.com/pwFoo/rust-runc/blob/313e6ae5a79b54455b0a242a795c69adf035141a/src/lib.rs
18
19/*
20 * Copyright 2020 fsyncd, Berlin, Germany.
21 * Additional material, copyright of the containerd authors.
22 *
23 * Licensed under the Apache License, Version 2.0 (the "License");
24 * you may not use this file except in compliance with the License.
25 * You may obtain a copy of the License at
26 *
27 *     http://www.apache.org/licenses/LICENSE-2.0
28 *
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
34 */
35
36#![cfg_attr(feature = "docs", doc = include_str!("../README.md"))]
37
38//! A crate for consuming the runc binary in your Rust applications, similar to
39//! [go-runc](https://github.com/containerd/go-runc) for Go.
40use std::{
41    fmt::{self, Debug, Display},
42    path::PathBuf,
43    process::{ExitStatus, Stdio},
44    sync::Arc,
45};
46
47#[cfg(feature = "async")]
48pub use crate::asynchronous::*;
49#[cfg(not(feature = "async"))]
50pub use crate::synchronous::*;
51
52#[cfg(feature = "async")]
53pub mod asynchronous;
54pub mod container;
55pub mod error;
56pub mod events;
57#[cfg(not(feature = "async"))]
58pub mod synchronous;
59
60#[cfg(feature = "async")]
61pub mod monitor;
62pub mod options;
63pub mod utils;
64
65const JSON: &str = "json";
66const TEXT: &str = "text";
67
68pub type Result<T> = std::result::Result<T, crate::error::Error>;
69
70/// Response is for (pid, exit status, outputs).
71#[derive(Debug, Clone)]
72pub struct Response {
73    pub pid: u32,
74    pub status: ExitStatus,
75    pub output: String,
76}
77
78#[derive(Debug, Clone)]
79pub struct Version {
80    pub runc_version: Option<String>,
81    pub spec_version: Option<String>,
82    pub commit: Option<String>,
83}
84
85#[derive(Debug, Clone, Default)]
86pub enum LogFormat {
87    Json,
88    #[default]
89    Text,
90}
91
92impl Display for LogFormat {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        match self {
95            LogFormat::Json => write!(f, "{}", JSON),
96            LogFormat::Text => write!(f, "{}", TEXT),
97        }
98    }
99}
100
101#[cfg(not(feature = "async"))]
102pub type Command = std::process::Command;
103
104#[cfg(feature = "async")]
105pub type Command = tokio::process::Command;
106
107#[derive(Debug, Clone)]
108pub struct Runc {
109    command: PathBuf,
110    args: Vec<String>,
111    spawner: Arc<dyn Spawner + Send + Sync>,
112}
113
114impl Runc {
115    fn command(&self, args: &[String]) -> Result<Command> {
116        let args = [&self.args, args].concat();
117        let mut cmd = Command::new(&self.command);
118
119        // Default to piped stdio, and they may be override by command options.
120        cmd.stdin(Stdio::null())
121            .stdout(Stdio::piped())
122            .stderr(Stdio::piped());
123
124        // NOTIFY_SOCKET introduces a special behavior in runc but should only be set if invoked from systemd
125        cmd.args(&args).env_remove("NOTIFY_SOCKET");
126
127        Ok(cmd)
128    }
129}