1 #ifndef FILE_DOTLOCK_H
2 #define FILE_DOTLOCK_H
3 
4 #include <unistd.h>
5 #include <fcntl.h>
6 
7 struct dotlock;
8 
9 struct dotlock_settings {
10 	/* Dotlock files are created by first creating a temp file and then
11 	   link()ing it to the dotlock. temp_prefix specifies the prefix to
12 	   use for temp files. It may contain a full path. Default is
13 	   ".temp.hostname.pid.". */
14 	const char *temp_prefix;
15 	/* Use this suffix for dotlock filenames. Default is ".lock". */
16 	const char *lock_suffix;
17 
18 	/* Abort after this many seconds. */
19 	unsigned int timeout;
20 	/* Override the lock file when it and the file we're protecting is
21 	   older than stale_timeout. */
22 	unsigned int stale_timeout;
23 
24 	/* Callback is called once in a while. stale is set to TRUE if stale
25 	   lock is detected and will be overridden in secs_left. If callback
26 	   returns FALSE then, the lock will not be overridden. */
27 	bool (*callback)(unsigned int secs_left, bool stale, void *context);
28 	void *context;
29 
30 	/* Rely on O_EXCL locking to work instead of using hardlinks.
31 	   It's faster, but doesn't work with all NFS implementations. */
32 	bool use_excl_lock:1;
33 	/* Flush NFS attribute cache before stating files. */
34 	bool nfs_flush:1;
35 	/* Use io_add_notify() to speed up finding out when an existing
36 	   dotlock is deleted */
37 	bool use_io_notify:1;
38 };
39 
40 enum dotlock_create_flags {
41 	/* If lock already exists, fail immediately */
42 	DOTLOCK_CREATE_FLAG_NONBLOCK		= 0x01,
43 	/* Don't actually create the lock file, only make sure it doesn't
44 	   exist. This is racy, so you shouldn't rely on it much. */
45 	DOTLOCK_CREATE_FLAG_CHECKONLY		= 0x02
46 };
47 
48 enum dotlock_replace_flags {
49 	/* Check that lock file hasn't been overridden before renaming. */
50 	DOTLOCK_REPLACE_FLAG_VERIFY_OWNER	= 0x01,
51 	/* Don't close the file descriptor. */
52 	DOTLOCK_REPLACE_FLAG_DONT_CLOSE_FD	= 0x02
53 };
54 
55 /* Create dotlock. Returns 1 if successful, 0 if timeout or -1 if error.
56    When returning 0, errno is also set to EAGAIN. */
57 int file_dotlock_create(const struct dotlock_settings *set, const char *path,
58 			enum dotlock_create_flags flags,
59 			struct dotlock **dotlock_r);
60 
61 /* Delete the dotlock file. Returns 1 if successful, 0 if the file had already
62    been deleted or reused by someone else, -1 if I/O error. */
63 int ATTR_NOWARN_UNUSED_RESULT
64 file_dotlock_delete(struct dotlock **dotlock);
65 
66 /* Use dotlock as the new content for file. This provides read safety without
67    locks, but it's not very good for large files. Returns fd for lock file.
68    If locking timed out, returns -1 and errno = EAGAIN. */
69 int file_dotlock_open(const struct dotlock_settings *set, const char *path,
70 		      enum dotlock_create_flags flags,
71 		      struct dotlock **dotlock_r);
72 /* Like file_dotlock_open(), but use the given file permissions. */
73 int file_dotlock_open_mode(const struct dotlock_settings *set, const char *path,
74 			   enum dotlock_create_flags flags,
75 			   mode_t mode, uid_t uid, gid_t gid,
76 			   struct dotlock **dotlock_r);
77 int file_dotlock_open_group(const struct dotlock_settings *set, const char *path,
78 			    enum dotlock_create_flags flags,
79 			    mode_t mode, gid_t gid, const char *gid_origin,
80 			    struct dotlock **dotlock_r);
81 /* Replaces the file dotlock protects with the dotlock file itself. */
82 int file_dotlock_replace(struct dotlock **dotlock,
83 			 enum dotlock_replace_flags flags);
84 /* Update dotlock's mtime. If you're keeping the dotlock for a long time,
85    it's a good idea to update it once in a while so others won't override it.
86    If the timestamp is less than a second old, it's not updated. */
87 int file_dotlock_touch(struct dotlock *dotlock);
88 /* Returns TRUE if the lock is still ok, FALSE if it's been overridden. */
89 bool file_dotlock_is_locked(struct dotlock *dotlock);
90 
91 /* Returns the lock file path. */
92 const char *file_dotlock_get_lock_path(struct dotlock *dotlock);
93 
94 #endif
95