-
Notifications
You must be signed in to change notification settings - Fork 271
/
test-file-create-locked.c
91 lines (81 loc) · 1.96 KB
/
test-file-create-locked.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#include "file-create-locked.h"
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
static void create_file(const char *path)
{
int fd;
fd = creat(path, 0600);
if (fd == -1)
i_fatal("creat(%s) failed: %m", path);
i_close_fd(&fd);
}
static bool wait_for_file(pid_t pid, const char *path)
{
struct stat st;
for (unsigned int i = 0; i < 1000; i++) {
if (stat(path, &st) == 0)
return TRUE;
if (errno != ENOENT)
i_fatal("stat(%s) failed: %m", path);
if (kill(pid, 0) < 0) {
if (errno == ESRCH)
return FALSE;
i_fatal("kill(SIGSRCH) failed: %m");
}
usleep(10000);
}
i_error("%s isn't being created", path);
return FALSE;
}
static void test_file_create_locked_basic(void)
{
struct file_create_settings set = {
.lock_timeout_secs = 0,
.lock_method = FILE_LOCK_METHOD_FCNTL,
};
const char *path = ".test-file-create-locked";
struct file_lock *lock;
const char *error;
bool created;
pid_t pid;
int fd;
test_begin("file_create_locked()");
i_unlink_if_exists(path);
i_unlink_if_exists(".test-temp-file-create-locked-child");
pid = fork();
switch (pid) {
case (pid_t)-1:
i_error("fork() failed: %m");
break;
case 0:
/* child */
fd = file_create_locked(path, &set, &lock, &created, &error);
test_assert(fd > 0);
test_assert(created);
if (test_has_failed())
exit(1);
create_file(".test-temp-file-create-locked-child");
sleep(60);
i_close_fd(&fd);
exit(0);
default:
/* parent */
test_assert(wait_for_file(pid, ".test-temp-file-create-locked-child"));
if (test_has_failed())
break;
test_assert(file_create_locked(path, &set, &lock, &created, &error) == -1);
test_assert(errno == EAGAIN);
if (kill(pid, SIGKILL) < 0)
i_error("kill(SIGKILL) failed: %m");
break;
}
i_unlink_if_exists(".test-temp-file-create-locked-child");
test_end();
}
void test_file_create_locked(void)
{
test_file_create_locked_basic();
}