about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cache.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/cache.c b/cache.c index 801e63f..900b161 100644 --- a/cache.c +++ b/cache.c
@@ -161,10 +161,23 @@ static int close_lock(struct cache_slot *slot)
161 */ 161 */
162static int lock_slot(struct cache_slot *slot) 162static int lock_slot(struct cache_slot *slot)
163{ 163{
164 slot->lock_fd = open(slot->lock_name, O_RDWR | O_CREAT | O_EXCL, 164 struct flock lock = {
165 .l_type = F_WRLCK,
166 .l_whence = SEEK_SET,
167 .l_start = 0,
168 .l_len = 0,
169 };
170
171 slot->lock_fd = open(slot->lock_name, O_RDWR | O_CREAT,
165 S_IRUSR | S_IWUSR); 172 S_IRUSR | S_IWUSR);
166 if (slot->lock_fd == -1) 173 if (slot->lock_fd == -1)
167 return errno; 174 return errno;
175 if (fcntl(slot->lock_fd, F_SETLK, &lock) < 0) {
176 int saved_errno = errno;
177 close(slot->lock_fd);
178 slot->lock_fd = -1;
179 return saved_errno;
180 }
168 if (xwrite(slot->lock_fd, slot->key, slot->keylen + 1) < 0) 181 if (xwrite(slot->lock_fd, slot->key, slot->keylen + 1) < 0)
169 return errno; 182 return errno;
170 return 0; 183 return 0;