| 200 | } |
| 201 | |
| 202 | std::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 | |
| 227 | int CheckUniqueInstance(const std::string &pidfile_path) { |
| 228 | static const int kMaxPidfileLockTrials = 5; |