Use flocks for pid file to enable auto release

This can help in situations where the process is forcefully terminated.
next
Sayan Nandan 3 years ago
parent 96a2cc3f77
commit 8b7de7173e

@ -115,6 +115,7 @@ All changes in this project will be noted in this file.
- `sky-bench` no longer affects your personal data because it creates a random temporary table
under the `default` keyspace
- Fix log output in `sky-bench` even if the `--json` flag was passed
- Use flocks to enable auto release of pid file, even if process is forcefully terminated
## Version 0.6.3 [2021-06-27]
@ -218,7 +219,7 @@ Fix persistence (see [#150](https://github.com/skytable/skytable/issues/150))
- Built-in TLS/SSL support
- Custom host/port settings in `sky-bench`
- Mock keys can be created with `sky-bench`
- Security patch for VE/S/00001
- Security patch for VE/S/00001 [(CVE-2021-32814)](https://nvd.nist.gov/vuln/detail/CVE-2021-32814)
- Escaping for spaces in `skysh`
- `tdb` is now called `skyd` (short for 'Skytable Daemon')

@ -29,7 +29,7 @@
//! This module provides the `FileLock` struct that can be used for locking and/or unlocking files on
//! unix-based systems and Windows systems
#![allow(dead_code)] // TODO: Enable this lint or remove the offending methods
#![allow(dead_code)] // TODO(@ohsayan): Remove lint or remove offending methods
use std::fs::File;
use std::fs::OpenOptions;
@ -64,6 +64,7 @@ impl FileLock {
///
/// This function will create and lock a file if it doesn't exist or it
/// will create and lock a new file
/// **This will immediately fail if locking fails, i.e it is non-blocking**
pub fn lock(filename: impl Into<PathBuf>) -> Result<Self> {
let file = OpenOptions::new()
.create(true)

@ -34,14 +34,12 @@
//! the modules for their respective documentation.
use crate::corestore::memstore::Memstore;
use crate::diskstore::flock::FileLock;
use env_logger::Builder;
use libsky::util::terminal;
use libsky::URL;
use libsky::VERSION;
use std::env;
use std::fs;
use std::io::Write;
use std::path;
use std::process;
use std::thread;
use std::time;
@ -151,10 +149,9 @@ fn main() {
terminal::write_info("Goodbye :)\n").unwrap();
}
pub fn pre_shutdown_cleanup(pid_file: fs::File, mr: Option<&Memstore>) {
drop(pid_file);
if let Err(e) = fs::remove_file(PATH) {
log::error!("Shutdown failure: Failed to remove pid file: {}", e);
pub fn pre_shutdown_cleanup(mut pid_file: FileLock, mr: Option<&Memstore>) {
if let Err(e) = pid_file.unlock() {
log::error!("Shutdown failure: Failed to unlock pid file: {}", e);
process::exit(0x01);
}
if let Some(mr) = mr {
@ -203,29 +200,15 @@ fn check_args_and_get_cfg() -> (PortConfig, BGSave, SnapshotConfig, Option<Strin
/// processes will detect this and this helps us prevent two processes from writing
/// to the same directory which can cause potentially undefined behavior.
///
fn run_pre_startup_tasks() -> fs::File {
let path = path::Path::new(PATH);
if path.exists() {
let pid = fs::read_to_string(path).unwrap_or_else(|_| "unknown".to_owned());
log::error!(
"Startup failure: Another process with parent PID {} is using the data directory",
pid
);
process::exit(0x01);
}
let mut file = match fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(PATH)
{
fn run_pre_startup_tasks() -> FileLock {
let mut file = match FileLock::lock(PATH) {
Ok(fle) => fle,
Err(e) => {
log::error!("Startup failure: Failed to open pid file: {}", e);
log::error!("Startup failure: Failed to lock pid file: {}", e);
process::exit(0x01);
}
};
if let Err(e) = file.write_all(process::id().to_string().as_bytes()) {
if let Err(e) = file.write(process::id().to_string().as_bytes()) {
log::error!("Startup failure: Failed to write to pid file: {}", e);
process::exit(0x01);
}

Loading…
Cancel
Save