1 #ifndef FILE_CREATE_LOCKED_H
2 #define FILE_CREATE_LOCKED_H
3 
4 #include "file-lock.h"
5 
6 struct file_create_settings {
7 	/* 0 = try locking without waiting */
8 	unsigned int lock_timeout_secs;
9 
10 	struct file_lock_settings lock_settings;
11 
12 	/* 0 = 0600 */
13 	int mode;
14 	/* 0 = default */
15 	uid_t uid;
16 	/* 0 = default */
17 	gid_t gid;
18 	const char *gid_origin;
19 
20 	/* If parent directory doesn't exist, mkdir() it with this mode.
21 	   0 = don't mkdir(). The parent directories are assumed to be
22 	   potentially rmdir() simultaneously, so the mkdir()+locking may be
23 	   attempted multiple times. */
24 	int mkdir_mode;
25 	/* 0 = default */
26 	uid_t mkdir_uid;
27 	/* 0 = default */
28 	gid_t mkdir_gid;
29 	const char *mkdir_gid_origin;
30 };
31 
32 /* Either open an existing file and lock it, or create the file locked.
33    The creation is done by creating a temp file and link()ing it to path.
34    If link() fails, opening is retried again. Returns fd on success,
35    -1 on error. errno is preserved for the last failed syscall, so most
36    importantly ENOENT could mean that the directory doesn't exist and EAGAIN
37    means locking timed out.
38 
39    If this function is used to create lock files, file_lock_set_unlink_on_free()
40    should be used for the resulting lock. It attempts to avoid unlinking the
41    file if there are already other processes using the lock. That can help to
42    avoid "Creating a locked file ... keeps failing" errors */
43 int file_create_locked(const char *path, const struct file_create_settings *set,
44 		       struct file_lock **lock_r, bool *created_r,
45 		       const char **error_r);
46 
47 #endif
48