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 | // Copyright (C) 2015-2025 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// 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/.
#ifndef PID_FILE_H
#define PID_FILE_H
#include <exceptions/exceptions.h>
#include <boost/shared_ptr.hpp>
#include <fstream>
#include <ostream>
#include <string>
namespace isc {
namespace util {
/// @brief Exception thrown when an error occurs during PID file processing.
class PIDFileError : public Exception {
public:
PIDFileError(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) { }
};
/// @brief Exception thrown when an error occurs trying to read a PID
/// from an opened file.
class PIDCantReadPID : public Exception {
public:
PIDCantReadPID(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) { }
};
/// @brief Class to help with processing PID files
///
/// This is a utility class to manipulate PID file. It provides
/// functions for writing and deleting a file containing a PID as
/// well as for extracting a PID from a file and checking if the
/// process is still running.
class PIDFile {
public:
/// @brief Constructor
///
/// @param filename PID filename.
PIDFile(const std::string& filename)<--- Class 'PIDFile' has a constructor with 1 argument that is not explicit. [+]Class 'PIDFile' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.
: filename_(filename) {
}
/// @brief Destructor
virtual ~PIDFile() = default;
/// @brief Read the PID in from the file and check it.
///
/// Read the PID in from the file then use it to see if
/// a process with that PID exists. If the file doesn't
/// exist treat it as the process not being there.
/// If the file exists but can't be read or it doesn't have
/// the proper format treat it as the process existing.
///
/// The PID file should be locked to avoid a race condition.
///
/// @return returns the PID if it is in, 0 otherwise.
///
/// @throw throws PIDCantReadPID if it was able to open the file
/// but was unable to read the PID from it.
int check() const;
/// @brief Write the PID to the file.
///
/// The PID file must be locked to avoid a race condition.
///
/// @param pid the pid to write to the file.
///
/// @throw throws PIDFileError if it can't open or write to the PID file.
void write(int) const;
/// @brief Get PID of the current process and write it to the file.
///
/// The PID file must be locked to avoid a race condition.
///
/// @throw throws PIDFileError if it can't open or write to the PID file.
void write() const;
/// @brief Delete the PID file.
///
/// This is an atomic operation not subject to a race condition.
///
/// @throw throws PIDFileError if it can't delete the PID file
void deleteFile() const;
/// @brief Returns the path to the PID file.
std::string getFilename() const {<--- Function 'getFilename()' should return member 'filename_' by const reference.<--- The function 'getFilename' is never used.
return (filename_);
}
/// @brief Returns the path to the lock file.
std::string getLockname() const {
return (filename_ + ".lock");
}
private:
/// @brief PID filename
std::string filename_;
};
/// @brief Defines a shared pointer to a PIDFile
typedef boost::shared_ptr<PIDFile> PIDFilePtr;
/// @brief RAII device to handle a lock file to avoid race conditions.
/// @note If there is a missing component in the path and the lock is
/// not blocking the lock is considered as being acquired: this does not
/// change the behavior of PIDFile methods (check() will succeed and
/// write() throw) and does not require unit test updates as if the
/// constructor throws.
class PIDLock {
public:
/// @brief Constructor
///
/// Try to get a lock.
///
/// @param lockname Lock filename.
/// @param blocking If true (default is false) block when already locked.
PIDLock(const std::string& lockname, bool blocking = false);<--- Class 'PIDLock' has a constructor with 1 argument that is not explicit. [+]Class 'PIDLock' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.
/// @brief Destructor
///
/// Release the lock when taken and delete the lock file.
~PIDLock();
/// @brief Return the lock status.
bool isLocked() {
return (locked_);
}
private:
/// @brief Name of the lock file.
std::string lockname_;
/// @brief File descriptor to the lock file.
int fd_;
/// @brief Lock status.
bool locked_;
};
} // namespace isc::util
} // namespace isc
#endif // PID_FILE_H
|