From: Theodore Ts'o Date: Fri, 15 Feb 2008 22:41:38 +0000 (-0500) Subject: libuuid: use fcntl locking instead of lockf X-Git-Tag: v1.40.7~41 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=e70f32b79d29e287f8347c5d41c6716f094cc654;p=tools%2Fe2fsprogs.git libuuid: use fcntl locking instead of lockf Cygwin doesn't support lockf(), so move to fcntl() locking as more portable. Also fix a bug which could cause get_lock() to loop forever if the attempt to lock the file fails for some reason. Signed-off-by: "Theodore Ts'o" --- diff --git a/lib/uuid/gen_uuid.c b/lib/uuid/gen_uuid.c index 9254eb6..d4befe4 100644 --- a/lib/uuid/gen_uuid.c +++ b/lib/uuid/gen_uuid.c @@ -266,6 +266,7 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low, THREAD_LOCAL FILE *state_f; THREAD_LOCAL uint16_t clock_seq; struct timeval tv; + struct flock fl; unsigned long long clock_reg; mode_t save_umask; @@ -280,14 +281,20 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low, state_fd = -1; } } + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; + fl.l_pid = 0; if (state_fd >= 0) { rewind(state_f); - while (lockf(state_fd, F_LOCK, 0) < 0) { + while (fcntl(state_fd, F_SETLKW, &fl) < 0) { if ((errno == EAGAIN) || (errno == EINTR)) continue; fclose(state_f); close(state_fd); state_fd = -1; + break; } } if (state_fd >= 0) { @@ -348,7 +355,8 @@ try_again: clock_seq, last.tv_sec, last.tv_usec, adjustment); fflush(state_f); rewind(state_f); - lockf(state_fd, F_ULOCK, 0); + fl.l_type = F_UNLCK; + fcntl(state_fd, F_SETLK, &fl); } *clock_high = clock_reg >> 32;