Whamcloud - gitweb
libuuid: use fcntl locking instead of lockf
authorTheodore Ts'o <tytso@mit.edu>
Fri, 15 Feb 2008 22:41:38 +0000 (17:41 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Sat, 16 Feb 2008 15:05:48 +0000 (10:05 -0500)
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" <tytso@mit.edu>
lib/uuid/gen_uuid.c

index 9254eb6..d4befe4 100644 (file)
@@ -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;