MCPcopy Create free account
hub / github.com/NetSys/bess / TryAcquirePidfileLock

Function TryAcquirePidfileLock

core/bessd.cc:202–225  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

200}
201
202std::tuple<bool, pid_t> TryAcquirePidfileLock(int fd) {
203 bool lockacquired = false;
204 pid_t pid = 0;
205
206 if (flock(fd, LOCK_EX | LOCK_NB)) {
207 // Lock is already held by another process.
208 if (errno != EWOULDBLOCK) {
209 PLOG(FATAL) << "flock(pidfile=" << FLAGS_i << ")";
210 } else {
211 VLOG(1) << "flock";
212 }
213
214 // Try to read the pid of the other process.
215 bool success = false;
216 std::tie(success, pid) = ReadPidfile(fd);
217 if (!success) {
218 PLOG(FATAL) << "Couldn't read pidfile";
219 }
220 } else {
221 lockacquired = true;
222 }
223
224 return std::make_tuple(lockacquired, pid);
225}
226
227int CheckUniqueInstance(const std::string &pidfile_path) {
228 static const int kMaxPidfileLockTrials = 5;

Callers 2

TESTFunction · 0.85
CheckUniqueInstanceFunction · 0.85

Calls 1

ReadPidfileFunction · 0.85

Tested by 1

TESTFunction · 0.68