Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated usage of flock from nix #3649

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions src/bin/stratis-min/stratisd-min.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#![allow(deprecated)]

use std::{
env,
error::Error,
fs::{File, OpenOptions},
io::{Read, Write},
os::unix::io::AsRawFd,
str::FromStr,
};

use clap::{Arg, Command};
use env_logger::Builder;
use log::LevelFilter;
use nix::{
fcntl::{flock, FlockArg},
fcntl::{Flock, FlockArg},
unistd::getpid,
};

Expand Down Expand Up @@ -45,10 +42,10 @@ fn parse_args() -> Command {

/// To ensure only one instance of stratisd runs at a time, acquire an
/// exclusive lock. Return an error if lock attempt fails.
fn trylock_pid_file() -> StratisResult<File> {
fn trylock_pid_file() -> StratisResult<Flock<File>> {
#[allow(unknown_lints)]
#[allow(clippy::suspicious_open_options)]
let mut f = OpenOptions::new()
let f = OpenOptions::new()
.read(true)
.write(true)
.create(true)
Expand All @@ -61,13 +58,13 @@ fn trylock_pid_file() -> StratisResult<File> {
Box::new(StratisError::from(err)),
)
})?;
let stratisd_min_file = match flock(f.as_raw_fd(), FlockArg::LockExclusiveNonblock) {
Ok(_) => {
let stratisd_min_file = match Flock::lock(f, FlockArg::LockExclusiveNonblock) {
Ok(mut f) => {
f.set_len(0)?;
f.write_all(getpid().to_string().as_bytes())?;
f
}
Err(_) => {
Err((mut f, _)) => {
let mut buf = String::new();

if f.read_to_string(&mut buf).is_err() {
Expand All @@ -82,7 +79,7 @@ fn trylock_pid_file() -> StratisResult<File> {

#[allow(unknown_lints)]
#[allow(clippy::suspicious_open_options)]
let mut f = OpenOptions::new()
let f = OpenOptions::new()
.read(true)
.write(true)
.create(true)
Expand All @@ -93,9 +90,9 @@ fn trylock_pid_file() -> StratisResult<File> {
Box::new(StratisError::from(err)),
)
})?;
match flock(f.as_raw_fd(), FlockArg::LockExclusiveNonblock) {
Ok(_) => drop(f),
Err(_) => {
match Flock::lock(f, FlockArg::LockExclusiveNonblock) {
Ok(_) => (),
Err((mut f, _)) => {
let mut buf = String::new();

if f.read_to_string(&mut buf).is_err() {
Expand Down
39 changes: 18 additions & 21 deletions src/bin/stratisd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#![allow(deprecated)]

use std::{
env,
fs::{File, OpenOptions},
io::{Read, Write},
os::unix::io::AsRawFd,
process::exit,
str::FromStr,
};
Expand All @@ -19,7 +16,7 @@ use libc::pid_t;
use log::LevelFilter;
use nix::{
errno::Errno,
fcntl::{flock, FlockArg},
fcntl::{Flock, FlockArg},
sys::signal::{kill, Signal},
unistd::{getpid, Pid},
};
Expand Down Expand Up @@ -53,10 +50,10 @@ fn initialize_log(log_level: Option<&str>) {

/// To ensure only one instance of stratisd runs at a time, acquire an
/// exclusive lock. Return an error if lock attempt fails.
fn trylock_pid_file() -> StratisResult<File> {
fn trylock_pid_file() -> StratisResult<Flock<File>> {
#[allow(unknown_lints)]
#[allow(clippy::suspicious_open_options)]
let mut f = OpenOptions::new()
let f = OpenOptions::new()
.read(true)
.write(true)
.create(true)
Expand All @@ -67,13 +64,13 @@ fn trylock_pid_file() -> StratisResult<File> {
Box::new(StratisError::from(err)),
)
})?;
let stratisd_file = match flock(f.as_raw_fd(), FlockArg::LockExclusiveNonblock) {
Ok(_) => {
let stratisd_file = match Flock::lock(f, FlockArg::LockExclusiveNonblock) {
Ok(mut f) => {
f.set_len(0)?;
f.write_all(getpid().to_string().as_bytes())?;
Ok(f)
}
Err(_) => {
Err((mut f, _)) => {
let mut buf = String::new();

if f.read_to_string(&mut buf).is_err() {
Expand All @@ -88,7 +85,7 @@ fn trylock_pid_file() -> StratisResult<File> {

#[allow(unknown_lints)]
#[allow(clippy::suspicious_open_options)]
let mut f = OpenOptions::new()
let f = OpenOptions::new()
.read(true)
.write(true)
.create(true)
Expand All @@ -102,24 +99,24 @@ fn trylock_pid_file() -> StratisResult<File> {
)
})?;

if let Err(Errno::EWOULDBLOCK) = flock(f.as_raw_fd(), FlockArg::LockExclusiveNonblock) {
if let Err((mut f, Errno::EWOULDBLOCK)) = Flock::lock(f, FlockArg::LockExclusiveNonblock) {
let mut string = String::new();
f.read_to_string(&mut string)?;
let pid = string
.parse::<pid_t>()
.map_err(|_| StratisError::Msg(format!("Failed to parse {} as PID", string)))?;
kill(Pid::from_raw(pid), Signal::SIGINT)?;
}

match flock(f.as_raw_fd(), FlockArg::LockExclusive) {
Ok(_) => drop(f),
Err(e) => {
return Err(StratisError::Chained(
"Failed to wait on stratisd-min to exit".to_string(),
Box::new(StratisError::from(e)),
))
}
};
match Flock::lock(f, FlockArg::LockExclusive) {
Ok(_) => (),
Err((_, e)) => {
return Err(StratisError::Chained(
"Failed to wait on stratisd-min to exit".to_string(),
Box::new(StratisError::from(e)),
))
}
};
}

stratisd_file
}
Expand Down
Loading