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
use std::collections::hash_map::DefaultHasher;
use std::fs::File;
use std::hash::Hasher;
use std::os::unix::io::RawFd;
use std::os::unix::net::UnixListener;
use std::path::{Path, PathBuf};
pub use containerd_shim_protos as protos;
use nix::ioctl_write_ptr_bad;
pub use protos::shim::shim::DeleteResponse;
pub use protos::ttrpc::{context::Context, Result as TtrpcResult};
#[cfg(feature = "async")]
pub use crate::asynchronous::*;
pub use crate::error::{Error, Result};
#[cfg(not(feature = "async"))]
pub use crate::synchronous::*;
#[macro_use]
pub mod error;
mod args;
#[cfg(feature = "async")]
pub mod asynchronous;
pub mod event;
pub mod io;
mod logger;
pub mod monitor;
pub mod mount;
mod reap;
#[cfg(not(feature = "async"))]
pub mod synchronous;
pub mod util;
pub mod api {
pub use super::protos::api::Status;
pub use super::protos::shim::oci::Options;
pub use super::protos::shim::shim::*;
pub use super::protos::types::empty::Empty;
}
macro_rules! cfg_not_async {
($($item:item)*) => {
$(
#[cfg(not(feature = "async"))]
#[cfg_attr(docsrs, doc(cfg(not(feature = "async"))))]
$item
)*
}
}
macro_rules! cfg_async {
($($item:item)*) => {
$(
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
$item
)*
}
}
cfg_not_async! {
pub use crate::synchronous::*;
pub use crate::synchronous::console;
pub use crate::synchronous::publisher;
pub use protos::shim::shim_ttrpc::Task;
pub use protos::ttrpc::TtrpcContext;
}
cfg_async! {
pub use crate::asynchronous::*;
pub use crate::asynchronous::console;
pub use crate::asynchronous::container;
pub use crate::asynchronous::processes;
pub use crate::asynchronous::task;
pub use crate::asynchronous::publisher;
pub use protos::shim_async::Task;
pub use protos::ttrpc::r#async::TtrpcContext;
}
ioctl_write_ptr_bad!(ioctl_set_winsz, libc::TIOCSWINSZ, libc::winsize);
const TTRPC_ADDRESS: &str = "TTRPC_ADDRESS";
#[derive(Debug, Default)]
pub struct Config {
pub no_setup_logger: bool,
pub no_reaper: bool,
pub no_sub_reaper: bool,
}
#[derive(Debug, Default)]
pub struct StartOpts {
pub id: String,
pub publish_binary: String,
pub address: String,
pub ttrpc_address: String,
pub namespace: String,
pub debug: bool,
}
const SOCKET_FD: RawFd = 3;
#[cfg(target_os = "linux")]
pub const SOCKET_ROOT: &str = "/run/containerd";
#[cfg(target_os = "macos")]
pub const SOCKET_ROOT: &str = "/var/run/containerd";
pub fn socket_address(socket_path: &str, namespace: &str, id: &str) -> String {
let path = PathBuf::from(socket_path)
.join(namespace)
.join(id)
.display()
.to_string();
let hash = {
let mut hasher = DefaultHasher::new();
hasher.write(path.as_bytes());
hasher.finish()
};
format!("unix://{}/{:x}.sock", SOCKET_ROOT, hash)
}
fn parse_sockaddr(addr: &str) -> &str {
if let Some(addr) = addr.strip_prefix("unix://") {
return addr;
}
if let Some(addr) = addr.strip_prefix("vsock://") {
return addr;
}
addr
}
fn start_listener(address: &str) -> std::io::Result<UnixListener> {
let path = parse_sockaddr(address);
if let Some(parent) = Path::new(path).parent() {
std::fs::create_dir_all(parent)?;
}
UnixListener::bind(path)
}
pub struct Console {
pub file: File,
}
#[cfg(test)]
mod tests {
use crate::start_listener;
#[test]
fn test_start_listener() {
let tmpdir = tempfile::tempdir().unwrap();
let path = tmpdir.path().to_str().unwrap().to_owned();
let socket = path + "/ns1/id1/socket";
let _listener = start_listener(&socket).unwrap();
let _listener2 = start_listener(&socket).expect_err("socket should already in use");
let socket2 = socket + "/socket";
assert!(start_listener(&socket2).is_err());
let path = tmpdir.path().to_str().unwrap().to_owned();
let txt_file = path + "demo.txt";
std::fs::write(&txt_file, "test").unwrap();
assert!(start_listener(&txt_file).is_err());
let context = std::fs::read_to_string(&txt_file).unwrap();
assert_eq!(context, "test");
}
}