- long long int start;
- long long int elapsed = 0;
- int fd;
- uint8_t curr_cookie[CFG_COOKIE_LEN];
-
- assert(lock_fd < 0);
- COVERAGE_INC(cfg_lock);
-
- time_refresh();
- start = time_msec();
- for (;;) {
- int error;
-
- /* Open lock file. */
- fd = open_lockfile(lock_name);
- if (fd < 0) {
- return -fd;
- }
-
- /* Try to lock it. This will block (if 'timeout' > 0). */
- error = try_lock(fd, timeout > 0);
- time_refresh();
- elapsed = time_msec() - start;
- if (!error) {
- /* Success! */
- break;
- }
-
- /* Lock failed. Close the lock file and reopen it on the next
- * iteration, just in case someone deletes it underneath us (even
- * though that should not happen). */
- close(fd);
- if (error != EINTR) {
- /* Hard error, give up. */
- COVERAGE_INC(cfg_lock_error);
- VLOG_WARN("%s: failed to lock file "
- "(after %lld ms, with %d-ms timeout): %s",
- lock_name, elapsed, timeout, strerror(error));
- return error;
- }