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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2020 Andrej Shadura.
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository, or online at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file is derived from the bsd-pidfile-rs project, available at
// https://github.com/andrewshadura/bsd-pidfile-rs. It was incorporated
// directly into Materialize on August 12, 2020.
//
// The original source code is subject to the terms of the MIT license, a copy
// of which can be found in the LICENSE file at the root of this repository.

//! PID file management for daemons.
//!
//! The `pid-file` crate wraps the `pidfile` family of functions provided by BSD
//! systems that provide mutual exclusion for daemons via PID files.
//!
//! Much of the code is inherited from [pidfile_rs], but the build system
//! bundles the necessary subset of libbsd rather than relying on libbsd to be
//! installed.
//!
//! [pidfile_rs]: https://docs.rs/pidfile_rs

#![warn(missing_docs)]

use std::ffi::{CString, NulError};
use std::fmt;
use std::fs::Permissions;
use std::io;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::ptr;

use ore::option::OptionExt;

#[allow(non_camel_case_types)]
#[repr(C)]
struct pidfh {
    private: [u8; 0],
}

extern "C" {
    fn pidfile_open(
        path: *const libc::c_char,
        mode: libc::mode_t,
        pid: *const libc::pid_t,
    ) -> *mut pidfh;
    fn pidfile_write(pfh: *mut pidfh) -> libc::c_int;
    fn pidfile_remove(pfh: *mut pidfh) -> libc::c_int;
}

/// An open PID file.
///
/// A process that manages to construct this type holds an exclusive lock on the
/// PID file.
///
/// Dropping the type will attempt to call [`remove`](PidFile::remove), but any
/// errors will be suppressed. Call `remove` manually if you need to handle PID
/// file removal errors.
pub struct PidFile(*mut pidfh);

impl PidFile {
    /// Attempts to open and lock the specified PID file.
    ///
    /// If the file is already locked by another process, it returns
    /// `Error::AlreadyRunning`.
    pub fn open<P>(path: P) -> Result<PidFile, Error>
    where
        P: AsRef<Path>,
    {
        PidFile::open_with(path, Permissions::from_mode(0o600))
    }

    /// Like [`open`](PidFile::open), but opens the file with the specified
    /// permissions rather than 0600.
    #[allow(clippy::unnecessary_mut_passed)] // this mut is being passed as a mut pointer
    pub fn open_with<P>(path: P, permissions: Permissions) -> Result<PidFile, Error>
    where
        P: AsRef<Path>,
    {
        let path_cstring = CString::new(path.as_ref().as_os_str().as_bytes())?;
        let mut old_pid: libc::pid_t = -1;

        #[allow(clippy::useless_conversion)] // the types differ on macos/linux
        let mode: libc::mode_t = permissions
            .mode()
            .try_into()
            .expect("file permissions not valid libc::mode_t");
        let f = unsafe { pidfile_open(path_cstring.as_ptr(), mode, &mut old_pid) };
        if !f.is_null() {
            let r = unsafe { pidfile_write(f) };
            if r == 0 {
                Ok(PidFile(f))
            } else {
                Err(Error::Io(io::Error::last_os_error()))
            }
        } else {
            let err = io::Error::last_os_error();
            if err.kind() == io::ErrorKind::AlreadyExists {
                Err(Error::AlreadyRunning {
                    pid: (old_pid != -1).then(|| old_pid),
                })
            } else {
                Err(Error::Io(err))
            }
        }
    }

    /// Closes the PID file and removes it from the filesystem.
    pub fn remove(mut self) -> Result<(), Error> {
        let r = unsafe { pidfile_remove(self.0) };
        // Set the pointer to null to prevent drop from calling remove again.
        self.0 = ptr::null_mut();
        if r == 0 {
            Ok(())
        } else {
            Err(Error::Io(io::Error::last_os_error()))
        }
    }
}

impl Drop for PidFile {
    fn drop(&mut self) {
        // If the pointer is null, the PID file has already been removed.
        if self.0 != ptr::null_mut() {
            unsafe { pidfile_remove(self.0) };
        }
    }
}

/// A PID file-related error.
#[derive(Debug)]
pub enum Error {
    /// An I/O error occurred.
    Io(io::Error),
    /// The provided path had embedded null bytes.
    Nul(NulError),
    /// Another process already has the lock on the requested PID file.
    AlreadyRunning {
        /// The PID of the existing process, if it is known.
        pid: Option<i32>,
    },
}

impl From<NulError> for Error {
    fn from(e: NulError) -> Error {
        Error::Nul(e)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::Io(e) => write!(f, "unable to open PID file: {}", e),
            Error::Nul(e) => write!(f, "PID file path contained null bytes: {}", e),
            Error::AlreadyRunning { pid } => write!(
                f,
                "process already running (PID: {})",
                pid.display_or("<unknown>")
            ),
        }
    }
}

impl std::error::Error for Error {}