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
use std::{
    fs::{self, File},
    io::Write,
    path::PathBuf,
    process,
    str::FromStr,
};

use crate::Error;
use runtime::AccountId;
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};

pub trait SystemProcess {
    fn refresh_process(&mut self, pid: Pid) -> bool;
    fn process_name(&self, pid: Pid) -> Result<String, Error>;
}

impl SystemProcess for System {
    fn refresh_process(&mut self, pid: Pid) -> bool {
        <Self as SystemExt>::refresh_process(self, pid)
    }

    fn process_name(&self, pid: Pid) -> Result<String, Error> {
        let process = <Self as SystemExt>::process(self, pid).ok_or_else(|| Error::ProcessNotFound(pid.to_string()))?;
        Ok(process.name().to_string())
    }
}

#[derive(Debug)]
pub struct PidFile {
    path: PathBuf,
    created_pidfile: bool,
}

impl PidFile {
    pub fn new(spec_name: &String, account_id: &AccountId) -> Self {
        Self {
            path: Self::compute_path(spec_name, account_id),
            created_pidfile: false,
        }
    }

    pub fn create(spec_name: &String, account_id: &AccountId, sys: &mut impl SystemProcess) -> Result<Self, Error> {
        let mut pid_file = Self::new(spec_name, account_id);
        if pid_file.path.exists() {
            let pid = pid_file.pid()?;
            if sys.refresh_process(pid) {
                match pid_name_matches_existing_client(sys, pid) {
                    Ok(true) => return Err(Error::ServiceAlreadyRunning(pid.as_u32())),
                    Ok(false) => (),
                    // There is a very small chance that a `pid` with a running process is killed exactly before
                    // `pid_name_matches_client` is run and `ProcessNotFound` is thrown. This is as if
                    // the `refresh_process` check had returned `false` (the pidfile process not running)
                    // so the error should not be propagated.
                    Err(Error::ProcessNotFound(_)) => (),
                    Err(e) => return Err(e),
                }
            }
            tracing::warn!(
                "Found existing PID file at: {}, but the process is no longer running.",
                pid_file
                    .path
                    .clone()
                    .into_os_string()
                    .into_string()
                    .map_err(|_| Error::OsStringError)?,
            );
        }

        tracing::info!(
            "Creating PID file at: {}",
            pid_file
                .path
                .clone()
                .into_os_string()
                .into_string()
                .map_err(|_| Error::OsStringError)?,
        );
        pid_file.set_pid(process::id())?;
        Ok(pid_file)
    }

    pub fn compute_path(spec_name: &String, account_id: &AccountId) -> PathBuf {
        let file_name = format!("{spec_name}_{account_id}.pid");
        // Use the default temporary directory of the OS
        std::env::temp_dir().join(file_name)
    }

    pub fn pid(&self) -> Result<Pid, Error> {
        let mut pid = fs::read_to_string(self.path.clone())?;
        pid = pid.strip_suffix('\n').unwrap_or(&pid).to_string();
        Ok(Pid::from_str(&pid)?)
    }

    pub fn set_pid(&mut self, pid: u32) -> Result<File, Error> {
        self.created_pidfile = true;
        let mut file = File::create(&self.path)?;
        file.write_all(format!("{pid}\n").as_bytes())?;
        file.sync_all()?;
        Ok(file)
    }

    pub fn remove(&mut self) -> Result<(), Error> {
        if self.created_pidfile {
            tracing::info!(
                "Removing PID file at: {}",
                self.path
                    .clone()
                    .into_os_string()
                    .into_string()
                    .map_err(|_| Error::OsStringError)?,
            );
            fs::remove_file(&self.path)?;
            self.path = PathBuf::default();
        } else {
            tracing::info!("No PID file created - no clean up required");
        }
        Ok(())
    }
}

impl Drop for PidFile {
    fn drop(&mut self) {
        if let Err(e) = self.remove() {
            tracing::error!("Failed to remove PID file: {}", e);
        }
    }
}

pub fn pid_name_matches_existing_client(sys: &mut impl SystemProcess, pidfile_value: Pid) -> Result<bool, Error> {
    let client_pid = Pid::from_u32(process::id());
    Ok(sys.process_name(client_pid)? == sys.process_name(pidfile_value)?)
}

#[cfg(all(test, feature = "parachain-metadata-kintsugi"))]
mod tests {
    use super::*;
    use serial_test::serial;
    use sysinfo::Pid;

    macro_rules! assert_err {
        ($result:expr, $($err:tt)+) => {{
            match $result {
                $($err)+ => (),
                ref e => panic!("expected `{}` but got `{:?}`", stringify!($($err)+), e),
            }
        }};
    }

    mockall::mock! {
        System {}

        pub trait SystemProcess {
            fn refresh_process(&mut self, pid: Pid) -> bool;
            fn process_name(&self, pid: Pid) -> Result<String, Error>;
        }
    }

    #[test]
    #[serial]
    fn test_create_pid_file() {
        let dummy_account_id = AccountId::new(Default::default());
        let dummy_spec_name = "kintsugi-testnet".to_string();
        let mut sys = MockSystem::default();
        assert_eq!(
            PidFile::compute_path(&dummy_spec_name, &dummy_account_id).exists(),
            false
        );
        let pidfile = PidFile::create(&dummy_spec_name, &dummy_account_id, &mut sys).unwrap();
        assert_eq!(pidfile.path.exists(), true);
    }

    #[test]
    #[serial]
    fn test_overwrite_stale_pid_file() {
        let dummy_account_id = AccountId::new(Default::default());
        let dummy_spec_name = "kintsugi-testnet".to_string();
        let mut sys = MockSystem::default();
        sys.expect_refresh_process().once().return_const(false);
        let mut pidfile = PidFile::new(&dummy_spec_name, &dummy_account_id);

        // Some high pid value. PIDs are stored as i32.
        pidfile.set_pid(i32::MAX as u32).unwrap();

        let initial_pid = pidfile.pid().unwrap();
        let pidfile = PidFile::create(&dummy_spec_name, &dummy_account_id, &mut sys).unwrap();
        let final_pid = pidfile.pid().unwrap();
        assert_ne!(initial_pid, final_pid);
    }

    #[test]
    #[serial]
    fn test_pid_file_with_same_process() {
        let dummy_account_id = AccountId::new(Default::default());
        let dummy_spec_name = "kintsugi-testnet".to_string();
        let mut sys = MockSystem::default();
        sys.expect_refresh_process().once().return_const(true);
        let mut pidfile = PidFile::new(&dummy_spec_name, &dummy_account_id);
        pidfile.set_pid(process::id()).unwrap();

        sys.expect_process_name()
            .times(2)
            .returning(|_| Ok("vault".to_string()));

        #[allow(unused_variables)]
        let own_pid = process::id();
        assert_err!(
            PidFile::create(&dummy_spec_name, &dummy_account_id, &mut sys),
            #[allow(unused_variables)]
            Err(Error::ServiceAlreadyRunning(own_pid))
        );
    }

    #[test]
    #[serial]
    fn test_pid_file_overwrite_does_not_fail_when_process_is_not_running() {
        let dummy_account_id = AccountId::new(Default::default());
        let dummy_spec_name = "kintsugi-testnet".to_string();
        let mut sys = MockSystem::default();
        sys.expect_refresh_process().once().return_const(true);
        let mut pidfile = PidFile::new(&dummy_spec_name, &dummy_account_id);

        // Some high pid value. PIDs are stored as i32.
        pidfile.set_pid(i32::MAX as u32).unwrap();

        let own_pid = process::id();
        sys.expect_process_name().returning(move |pid| {
            if pid == Pid::from_u32(own_pid) {
                Ok("vault".to_string())
            } else {
                Err(Error::ProcessNotFound(pid.to_string()))
            }
        });
        PidFile::create(&dummy_spec_name, &dummy_account_id, &mut sys).unwrap();
    }

    #[test]
    #[serial]
    fn test_pidfile_removal() {
        let dummy_account_id = AccountId::new(Default::default());
        let dummy_spec_name = "kintsugi-testnet".to_string();
        let mut sys = MockSystem::default();
        sys.expect_refresh_process().return_const(true); // indicate the process still exists
        sys.expect_process_name().returning(|_| Ok("vault".to_string()));

        let path = {
            // simulate start vault: create pidfile
            let pidfile = PidFile::create(&dummy_spec_name, &dummy_account_id, &mut sys).unwrap();
            assert!(pidfile.path.exists());

            // simulate the start of a second vault: attempt to create pidfile
            {
                let _ = PidFile::create(&dummy_spec_name, &dummy_account_id, &mut sys);
            }

            // The second pidfile going out of scope should not cause the removal of the pidfile
            assert!(pidfile.path.exists());
            pidfile.path.clone()
        };

        // after the first pidfile goes out of scope, the file should be removed
        assert!(!path.exists());
    }
}