diff --git a/lib/uuid/gen_uuid.c b/lib/uuid/gen_uuid.c index 1b23d112..22b45134 100644 --- a/lib/uuid/gen_uuid.c +++ b/lib/uuid/gen_uuid.c @@ -326,10 +326,12 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low, state_fd = open("/var/lib/libuuid/clock.txt", O_RDWR|O_CREAT, 0660); (void) umask(save_umask); - state_f = fdopen(state_fd, "r+"); - if (!state_f) { - close(state_fd); - state_fd = -1; + if (state_fd >= 0) { + state_f = fdopen(state_fd, "r+"); + if (!state_f) { + close(state_fd); + state_fd = -1; + } } } fl.l_type = F_WRLCK; @@ -343,7 +345,6 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low, if ((errno == EAGAIN) || (errno == EINTR)) continue; fclose(state_f); - close(state_fd); state_fd = -1; break; } @@ -412,7 +413,10 @@ try_again: } rewind(state_f); fl.l_type = F_UNLCK; - fcntl(state_fd, F_SETLK, &fl); + if (fcntl(state_fd, F_SETLK, &fl) < 0) { + fclose(state_f); + state_fd = -1; + } } *clock_high = clock_reg >> 32;