Lines Matching defs:lock

5 #include "file-lock.h"
31 static void file_lock_log_warning_if_slow(struct file_lock *lock);
88 return t_strdup_printf(" (%s lock held by pid %ld)",
140 return " (BUG: lock is held by our own process)";
141 return t_strdup_printf(" (%s lock held by pid %ld)", lock_type, (long)pid);
186 (lock_type == F_RDLCK ? "read-lock" : "write-lock");
192 "Can't lock file %s: fcntl() locks not supported", path);
239 "Can't lock file %s: flock() not supported", path);
315 struct file_lock *lock;
322 lock = i_new(struct file_lock, 1);
323 lock->fd = fd;
324 lock->path = i_strdup(path);
325 lock->lock_type = lock_type;
326 lock->lock_method = lock_method;
327 if (gettimeofday(&lock->locked_time, NULL) < 0)
329 *lock_r = lock;
333 int file_lock_try_update(struct file_lock *lock, int lock_type)
338 ret = file_lock_do(lock->fd, lock->path, lock_type,
339 lock->lock_method, 0, &error);
342 file_lock_log_warning_if_slow(lock);
343 lock->lock_type = lock_type;
347 void file_lock_set_unlink_on_free(struct file_lock *lock, bool set)
349 lock->unlink_on_free = set;
352 void file_lock_set_close_on_free(struct file_lock *lock, bool set)
354 lock->close_on_free = set;
359 struct file_lock *lock;
361 lock = i_new(struct file_lock, 1);
362 lock->fd = -1;
363 lock->path = i_strdup(file_dotlock_get_lock_path(*dotlock));
364 lock->lock_type = F_WRLCK;
365 lock->lock_method = FILE_LOCK_METHOD_DOTLOCK;
366 if (gettimeofday(&lock->locked_time, NULL) < 0)
368 lock->dotlock = *dotlock;
371 return lock;
374 static void file_unlock_real(struct file_lock *lock)
378 if (file_lock_do(lock->fd, lock->path, F_UNLCK,
379 lock->lock_method, 0, &error) == 0) {
381 i_error("file_unlock(%s) failed: %m", lock->path);
387 struct file_lock *lock = *_lock;
393 could be deleting the new lock. */
394 i_assert(!lock->unlink_on_free);
396 if (lock->dotlock == NULL)
397 file_unlock_real(lock);
398 file_lock_free(&lock);
401 static void file_try_unlink_locked(struct file_lock *lock)
408 file_unlock_real(lock);
409 ret = file_try_lock_error(lock->fd, lock->path, F_WRLCK,
410 lock->lock_method, &temp_lock, &error);
413 lock->path, error);
416 } else if (fstat(lock->fd, &st1) < 0) {
418 i_error("file_lock_free(): fstat(%s) failed: %m", lock->path);
419 } else if (stat(lock->path, &st2) < 0) {
421 i_error("file_lock_free(): stat(%s) failed: %m", lock->path);
424 /* lock file was recreated already - don't delete it */
426 /* nobody was waiting on the lock - unlink it */
427 i_unlink(lock->path);
434 struct file_lock *lock = *_lock;
436 if (lock == NULL)
441 if (lock->dotlock != NULL)
442 file_dotlock_delete(&lock->dotlock);
443 if (lock->unlink_on_free)
444 file_try_unlink_locked(lock);
445 if (lock->close_on_free)
446 i_close_fd(&lock->fd);
448 file_lock_log_warning_if_slow(lock);
449 i_free(lock->path);
450 i_free(lock);
453 const char *file_lock_get_path(struct file_lock *lock)
455 return lock->path;
458 void file_lock_set_path(struct file_lock *lock, const char *path)
460 if (path != lock->path) {
461 i_free(lock->path);
462 lock->path = i_strdup(path);
493 static void file_lock_log_warning_if_slow(struct file_lock *lock)
501 if (lock->lock_type != F_WRLCK) {
511 int diff = timeval_diff_msecs(&now, &lock->locked_time);
513 i_warning("Lock %s kept for %d.%03d secs", lock->path,