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
use super::*;
use std::fs::{self, File};
use std::io::prelude::*;
use std::net::UdpSocket;
pub fn check_fs() -> Result<(), Error> {
let test_file = "/home/obc-test-file";
let test_string = "I'm a test string";
if check_write(test_file, test_string)
.and(check_read(test_file, test_string))
.is_err()
&& COMMS_SERVICE != ""
{
let _ = send_error();
}
let _ = fs::remove_file(test_file);
Ok(())
}
fn check_write(file_name: &str, test_string: &str) -> Result<(), Error> {
let mut file = File::create(file_name)?;
file.write_all(test_string.as_bytes())?;
file.sync_all()?;
Ok(())
}
fn check_read(file_name: &str, test_string: &str) -> Result<(), Error> {
let mut file = File::open(file_name)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
if contents != test_string {
bail!("Failed to verify test file");
}
Ok(())
}
fn send_error() -> Result<(), Error> {
let config = ServiceConfig::new(COMMS_SERVICE)?;
let host = config
.hosturl()
.ok_or_else(|| failure::format_err!("Unable to fetch addr for comms service"))?;
let mut host_parts = host.split(':').map(|val| val.to_owned());
let comms_ip = host_parts.next().unwrap_or_else(|| {
error!("Failed to lookup comms service IP. Using default 0.0.0.0");
"0.0.0.0".to_owned()
});
let downlink = format!("{}:{}", comms_ip, DOWNLINK_PORT);
let local_socket = UdpSocket::bind("0.0.0.0:0")?;
match local_socket.send_to(b"USER PARTITION CORRUPTED", downlink) {
Ok(_) => error!("Sent distress beacon"),
Err(err) => error!("Failed to send distress beacon: {:?}", err),
}
Ok(())
}