forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessLock.h
73 lines (59 loc) · 1.7 KB
/
ProcessLock.h
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
#pragma once
#include "FileDescriptor.h"
#include <chrono>
#include <variant>
namespace watchman {
class ProcessLock {
public:
using LockError = std::string;
/**
* Move-only unit type that indicates the process lock has been acquired.
*/
class Handle {
public:
Handle(Handle&&) = default;
Handle& operator=(Handle&&) = default;
private:
Handle() = default;
Handle(const Handle&) = delete;
Handle& operator=(const Handle&) = delete;
};
/**
* Acquires an fd to the pidfile and locks it.
*
* Call before fork(), so failure can be printed to the daemonizing process.
*
* Prints an error and exits the process if it fails.
*/
static ProcessLock acquire(const std::string& pid_file);
/**
* Acquires an fd to the pidfile and locks it.
*
* Call before fork(), so failure can be printed to the daemonizing process.
*
* If it fails, it returns a string containing the error message.
*/
static std::variant<ProcessLock, LockError> tryAcquire(
const std::string& pid_file);
ProcessLock(ProcessLock&&) = default;
ProcessLock& operator=(ProcessLock&&) = default;
ProcessLock(const ProcessLock&) = delete;
ProcessLock& operator=(const ProcessLock&) = delete;
/**
* Called by the daemonized process to write the daemon pid into the locked
* pidfile.
*
* This releases the FileDescriptor but does not close it, as the lock should
* be held for the process's lifetime.
*/
Handle writePid(const std::string& pid_file);
private:
#ifndef _WIN32
ProcessLock() = delete;
explicit ProcessLock(FileDescriptor fd) : fd_{std::move(fd)} {}
FileDescriptor fd_;
#else
ProcessLock() = default;
#endif
};
} // namespace watchman