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
/*
   Copyright The containerd Authors.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

use std::{
    fs::{rename, File, OpenOptions},
    io::{Read, Write},
    path::Path,
};

use containerd_shim_protos::shim::oci::Options;
#[cfg(unix)]
use libc::mode_t;
use log::warn;
#[cfg(unix)]
use nix::sys::stat::Mode;
use oci_spec::runtime::Spec;

use crate::{
    util::{JsonOptions, OPTIONS_FILE_NAME, RUNTIME_FILE_NAME},
    Error,
};

pub fn read_file_to_str<P: AsRef<Path>>(filename: P) -> crate::Result<String> {
    let mut file = File::open(&filename).map_err(io_error!(
        e,
        "open {}",
        filename.as_ref().to_string_lossy()
    ))?;
    let mut content: String = String::new();
    file.read_to_string(&mut content).map_err(io_error!(
        e,
        "read {}",
        filename.as_ref().to_string_lossy()
    ))?;
    Ok(content)
}

pub fn read_options(bundle: impl AsRef<Path>) -> crate::Result<Options> {
    let path = bundle.as_ref().join(OPTIONS_FILE_NAME);
    let opts_str = read_file_to_str(path)?;
    let json_opt: JsonOptions = serde_json::from_str(&opts_str)?;
    Ok(json_opt.into())
}

pub fn read_runtime(bundle: impl AsRef<Path>) -> crate::Result<String> {
    let path = bundle.as_ref().join(RUNTIME_FILE_NAME);
    read_file_to_str(path)
}

pub fn read_address() -> crate::Result<String> {
    let path = Path::new("address");
    read_file_to_str(path)
}

pub fn read_pid_from_file(pid_path: &Path) -> crate::Result<i32> {
    let pid_str = read_file_to_str(pid_path)?;
    let pid = pid_str.parse::<i32>()?;
    Ok(pid)
}

pub fn write_str_to_path(filename: &Path, s: &str) -> crate::Result<()> {
    let file = filename
        .file_name()
        .ok_or_else(|| Error::InvalidArgument(String::from("pid path illegal")))?;
    let tmp_path = filename
        .parent()
        .map(|x| x.join(format!(".{}", file.to_str().unwrap_or(""))))
        .ok_or_else(|| Error::InvalidArgument(String::from("failed to create tmp path")))?;
    let tmp_path = tmp_path
        .to_str()
        .ok_or_else(|| Error::InvalidArgument(String::from("failed to get path")))?;
    let mut f = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(tmp_path)
        .map_err(io_error!(e, "open {}", filename.to_str().unwrap()))?;
    f.write_all(s.as_bytes())
        .map_err(io_error!(e, "write tmp file"))?;
    rename(tmp_path, filename).map_err(io_error!(
        e,
        "rename tmp file to {}",
        filename.to_str().unwrap()
    ))?;
    Ok(())
}

pub fn write_options(bundle: &str, opt: &Options) -> crate::Result<()> {
    let json_opt = JsonOptions::from(opt.to_owned());
    let opts_str = serde_json::to_string(&json_opt)?;
    let path = Path::new(bundle).join(OPTIONS_FILE_NAME);
    write_str_to_path(path.as_path(), opts_str.as_str())
}

pub fn write_runtime(bundle: &str, binary_name: &str) -> crate::Result<()> {
    let path = Path::new(bundle).join(RUNTIME_FILE_NAME);
    write_str_to_path(path.as_path(), binary_name)
}

pub fn write_address(address: &str) -> crate::Result<()> {
    let path = Path::new("address");
    write_str_to_path(path, address)
}

pub fn read_spec_from_file(bundle: &str) -> crate::Result<Spec> {
    let path = Path::new(bundle).join("config.json");
    Spec::load(path).map_err(other_error!(e, "read spec file"))
}

#[cfg(unix)]
pub fn mkdir(path: impl AsRef<Path>, mode: mode_t) -> crate::Result<()> {
    let path_buf = path.as_ref().to_path_buf();
    if !path_buf.as_path().exists() {
        let mode = Mode::from_bits(mode).ok_or_else(|| other!("invalid dir mode {}", mode))?;
        nix::unistd::mkdir(path_buf.as_path(), mode)?;
    }
    Ok(())
}

/// A helper to help remove temperate file or dir when it became useless
pub struct HelperRemoveFile {
    path: String,
}

impl HelperRemoveFile {
    pub fn new(path: String) -> Self {
        Self { path }
    }
}

impl Drop for HelperRemoveFile {
    fn drop(&mut self) {
        std::fs::remove_file(&self.path)
            .unwrap_or_else(|e| warn!("remove dir {} error: {}", &self.path, e));
    }
}

#[cfg(target_os = "windows")]
// helper to configure pause thread until signaled. Useful in attaching a debugger
// https://github.com/microsoft/hcsshim/blob/v0.10.0-rc.7/cmd/containerd-shim-runhcs-v1/serve.go#L313-L315
// use with https://github.com/moby/docker-signal
pub(crate) fn setup_debugger_event() {
    use std::{env, io, process};

    use log::{debug, error};
    use windows_sys::Win32::System::Threading::{WaitForSingleObject, INFINITE};

    let debugger = env::var("SHIM_DEBUGGER").unwrap_or_else(|_| "".to_string());
    if debugger.is_empty() {
        return;
    }
    let event_name = format!("Global\\debugger-{}", process::id());
    debug!("Halting until signalled: {}", event_name);
    let e = match create_event(event_name) {
        Ok(e) => e,
        Err(e) => {
            error!("failed to create event for debugger: {}", e);
            return;
        }
    };
    match unsafe { WaitForSingleObject(e, INFINITE) } {
        0 => {}
        _ => {
            error!(
                "failed to wait for debugger event: {}",
                io::Error::last_os_error()
            );
            return;
        }
    }
    debug!("signal received, continuing");
}

#[cfg(target_os = "windows")]
fn create_event(name: String) -> crate::Result<isize> {
    use std::{ffi::OsStr, io, os::windows::prelude::OsStrExt};

    use windows_sys::Win32::System::Threading::CreateEventW;

    let name = OsStr::new(name.as_str())
        .encode_wide()
        .chain(Some(0)) // add NULL termination
        .collect::<Vec<_>>();

    let result = unsafe { CreateEventW(std::ptr::null_mut(), 0, 0, name.as_ptr()) };
    match result {
        0 => Err(Error::Other(io::Error::last_os_error().to_string())),
        _ => Ok(result),
    }
}