-static int
-open_lockfile(const char *name)
-{
- for (;;) {
- /* Try to open an existing lock file. */
- int fd = open(name, O_RDWR);
- if (fd >= 0) {
- return fd;
- } else if (errno != ENOENT) {
- VLOG_WARN("%s: failed to open lock file: %s",
- name, strerror(errno));
- return -errno;
- }
-
- /* Try to create a new lock file. */
- VLOG_INFO("%s: lock file does not exist, creating", name);
- fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
- if (fd >= 0) {
- return fd;
- } else if (errno != EEXIST) {
- VLOG_WARN("%s: failed to create lock file: %s",
- name, strerror(errno));
- return -errno;
- }
-
- /* Someone else created the lock file. Try again. */
- }
-}
-
-static int
-try_lock(int fd, bool block)
-{
- struct flock l;
- memset(&l, 0, sizeof l);
- l.l_type = F_WRLCK;
- l.l_whence = SEEK_SET;
- l.l_start = 0;
- l.l_len = 0;
- return fcntl(fd, block ? F_SETLKW : F_SETLK, &l) == -1 ? errno : 0;
-}
-