| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
/* Include here so consumers have LOCK_{EX,SH,NB} available. */ |
| 5 |
#include <sys/file.h> /* IWYU pragma: export */ |
| 6 |
|
| 7 |
#include "basic-forward.h" |
| 8 |
|
| 9 |
typedef struct LockFile { |
| 10 |
int dir_fd; |
| 11 |
char *path; |
| 12 |
int fd; |
| 13 |
int operation; |
| 14 |
} LockFile; |
| 15 |
|
| 16 |
int make_lock_file_at(int dir_fd, const char *p, int operation, LockFile *ret); |
| 17 |
static inline int make_lock_file(const char *p, int operation, LockFile *ret) { |
| 18 |
return make_lock_file_at(AT_FDCWD, p, operation, ret); |
| 19 |
} |
| 20 |
int make_lock_file_for(const char *p, int operation, LockFile *ret); |
| 21 |
void release_lock_file(LockFile *f); |
| 22 |
|
| 23 |
#define LOCK_FILE_INIT (LockFile) { .dir_fd = -EBADF, .fd = -EBADF } |
| 24 |
|
| 25 |
/* POSIX locks with the same interface as flock(). */ |
| 26 |
int posix_lock(int fd, int operation); |
| 27 |
void posix_unlockpp(int **fd); |
| 28 |
|
| 29 |
#define CLEANUP_POSIX_UNLOCK(fd) \ |
| 30 |
_cleanup_(posix_unlockpp) _unused_ int *CONCATENATE(_cleanup_posix_unlock_, UNIQ) = &(fd) |
| 31 |
|
| 32 |
/* Open File Description locks with the same interface as flock(). */ |
| 33 |
int unposix_lock(int fd, int operation); |
| 34 |
void unposix_unlockpp(int **fd); |
| 35 |
|
| 36 |
#define CLEANUP_UNPOSIX_UNLOCK(fd) \ |
| 37 |
_cleanup_(unposix_unlockpp) _unused_ int *CONCATENATE(_cleanup_unposix_unlock_, UNIQ) = &(fd) |
| 38 |
|
| 39 |
typedef enum LockType { |
| 40 |
LOCK_NONE, /* Don't lock the file descriptor. Useful if you need to conditionally lock a file. */ |
| 41 |
LOCK_BSD, |
| 42 |
LOCK_POSIX, |
| 43 |
LOCK_UNPOSIX, |
| 44 |
} LockType; |
| 45 |
|
| 46 |
int lock_generic(int fd, LockType type, int operation); |
| 47 |
|
| 48 |
int lock_generic_with_timeout(int fd, LockType type, int operation, usec_t timeout); |