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
use super::{
utils, ServiceInstallCtx, ServiceLevel, ServiceManager, ServiceStartCtx, ServiceStopCtx,
ServiceUninstallCtx,
};
use std::{
ffi::{OsStr, OsString},
io,
path::PathBuf,
process::{Command, Stdio},
};
static SERVICE: &str = "service";
const SCRIPT_FILE_PERMISSIONS: u32 = 0o755;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RcdConfig {}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RcdServiceManager {
pub config: RcdConfig,
}
impl RcdServiceManager {
pub fn system() -> Self {
Self::default()
}
pub fn with_config(self, config: RcdConfig) -> Self {
Self { config }
}
}
impl ServiceManager for RcdServiceManager {
fn available(&self) -> io::Result<bool> {
match std::fs::metadata(service_dir_path()) {
Ok(_) => Ok(true),
Err(x) if x.kind() == io::ErrorKind::NotFound => Ok(false),
Err(x) => Err(x),
}
}
fn install(&self, ctx: ServiceInstallCtx) -> io::Result<()> {
let service = ctx.label.to_script_name();
let script = make_script(&service, &service, ctx.program.as_os_str(), ctx.args);
utils::write_file(
&rc_d_script_path(&service),
script.as_bytes(),
SCRIPT_FILE_PERMISSIONS,
)?;
rc_d_script("enable", &service)
}
fn uninstall(&self, ctx: ServiceUninstallCtx) -> io::Result<()> {
let service = ctx.label.to_script_name();
rc_d_script("delete", &service)?;
std::fs::remove_file(rc_d_script_path(&service))
}
fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> {
let service = ctx.label.to_script_name();
rc_d_script("start", &service)
}
fn stop(&self, ctx: ServiceStopCtx) -> io::Result<()> {
let service = ctx.label.to_script_name();
rc_d_script("stop", &service)
}
fn level(&self) -> ServiceLevel {
ServiceLevel::System
}
fn set_level(&mut self, level: ServiceLevel) -> io::Result<()> {
match level {
ServiceLevel::System => Ok(()),
ServiceLevel::User => Err(io::Error::new(
io::ErrorKind::Unsupported,
"rc.d does not support user-level services",
)),
}
}
}
#[inline]
fn rc_d_script_path(name: &str) -> PathBuf {
service_dir_path().join(name)
}
#[inline]
fn service_dir_path() -> PathBuf {
PathBuf::from("/usr/local/etc/rc.d")
}
fn rc_d_script(cmd: &str, service: &str) -> io::Result<()> {
let status = Command::new(SERVICE)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.arg(service)
.arg(cmd)
.status()?;
if status.success() {
Ok(())
} else {
let msg = format!("Failed to {cmd} {service}");
Err(io::Error::new(io::ErrorKind::Other, msg))
}
}
fn make_script(description: &str, provide: &str, program: &OsStr, args: Vec<OsString>) -> String {
let name = provide.replace('-', "_");
let program = program.to_string_lossy();
let args = args
.into_iter()
.map(|a| a.to_string_lossy().to_string())
.collect::<Vec<String>>()
.join(" ");
format!(
r#"
#!/bin/sh
#
# PROVIDE: {provide}
# REQUIRE: LOGIN FILESYSTEMS
# KEYWORD: shutdown
. /etc/rc.subr
name="{name}"
desc="{description}"
rcvar="{name}_enable"
load_rc_config ${{name}}
: ${{{name}_options="{args}"}}
pidfile="/var/run/{name}.pid"
procname="{program}"
command="/usr/sbin/daemon"
command_args="-c -S -T ${{name}} -p ${{pidfile}} ${{procname}} ${{{name}_options}}"
run_rc_command "$1"
"#
)
.trim()
.to_string()
}