Skip to content

Commit b991811

Browse files
committed
Stop stratisd-min from stratisd when stratisd is started
1 parent 6dc54f4 commit b991811

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/bin/stratisd.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ use std::{
1313

1414
use clap::{Arg, Command};
1515
use env_logger::Builder;
16+
use libc::pid_t;
1617
use log::LevelFilter;
1718
use nix::{
19+
errno::Errno,
1820
fcntl::{flock, FlockArg},
19-
unistd::getpid,
21+
sys::signal::{kill, Signal},
22+
unistd::{getpid, Pid},
2023
};
2124

2225
use stratisd::stratis::{run, StratisError, StratisResult, VERSION};
@@ -78,7 +81,7 @@ fn trylock_pid_file() -> StratisResult<File> {
7881
}
7982
};
8083

81-
let f = OpenOptions::new()
84+
let mut f = OpenOptions::new()
8285
.read(true)
8386
.write(true)
8487
.create(true)
@@ -91,6 +94,16 @@ fn trylock_pid_file() -> StratisResult<File> {
9194
Box::new(StratisError::from(err)),
9295
)
9396
})?;
97+
98+
if let Err(Errno::EWOULDBLOCK) = flock(f.as_raw_fd(), FlockArg::LockExclusiveNonblock) {
99+
let mut string = String::new();
100+
f.read_to_string(&mut string)?;
101+
let pid = string
102+
.parse::<pid_t>()
103+
.map_err(|_| StratisError::Msg(format!("Failed to parse {} as PID", string)))?;
104+
kill(Pid::from_raw(pid), Signal::SIGINT)?;
105+
}
106+
94107
match flock(f.as_raw_fd(), FlockArg::LockExclusive) {
95108
Ok(_) => drop(f),
96109
Err(e) => {

systemd/stratisd-min-postinitrd.service.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
Description=Stratis minimal daemon
33
Documentation=man:stratisd(8)
44
DefaultDependencies=no
5-
StopWhenUnneeded=true
65
After=systemd-udevd.service
76
Requires=systemd-udevd.service
7+
Before=local-fs.target
88

99
[Service]
1010
Type=notify
@@ -14,3 +14,6 @@ ExecStart=@LIBEXECDIR@/stratisd-min --log-level debug
1414
KillSignal=SIGINT
1515
KillMode=process
1616
Restart=on-abort
17+
18+
[Install]
19+
WantedBy=local-fs.target

systemd/stratisd.service.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Description=Stratis daemon
33
Documentation=man:stratisd(8)
44
DefaultDependencies=no
5+
After=sysinit.target
56

67
[Service]
78
BusName=org.storage.stratis3
@@ -13,4 +14,4 @@ KillMode=process
1314
Restart=on-abort
1415

1516
[Install]
16-
WantedBy=sysinit.target
17+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)