Welcome to mirror list, hosted at ThFree Co, Russian Federation.

inotify04.c « static « zdtm « test - github.com/checkpoint-restore/criu.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fb92930246fea099029c83e67b841d09d0237db2 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/inotify.h>

#include "zdtmtst.h"

const char *test_doc	= "Check inotify does not have trash in queue after c/r";
const char *test_author	= "Pavel Tikhomirov <ptikhomirov@virtuozzo.com>";

char *dirname;
TEST_OPTION(dirname, string, "directory name", 1);

#define TEST_FILE	"inotify-testfile"

#define BUFF_SIZE ((sizeof(struct inotify_event) + PATH_MAX))

static int inotify_read_events(int inotify_fd, unsigned int *n)
{
	struct inotify_event *event;
	char buf[BUFF_SIZE * 8];
	int ret, off;

	*n = 0;

	while (1) {
		ret = read(inotify_fd, buf, sizeof(buf));
		if (ret < 0) {
			if (errno != EAGAIN) {
				pr_perror("Can't read inotify queue");
				return -1;
			} else {
				ret = 0;
				break;
			}
		} else if (ret == 0)
			break;

		for (off = 0; off < ret; (*n)++, off += sizeof(*event) + event->len) {
			event = (void *)(buf + off);
			test_msg("Event %#10x\n", event->mask);
		}
	}

	test_msg("Read %u events\n", *n);
	return ret;
}

int main (int argc, char *argv[])
{
	unsigned int mask = IN_ALL_EVENTS;
	char test_file_path[PATH_MAX];
	int fd, ifd, ifd2, ret;
	unsigned int n;

	test_init(argc, argv);

	if (mkdir(dirname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
		pr_perror("Can't create directory %s", dirname);
		return 1;
	}

	snprintf(test_file_path, sizeof(test_file_path), "%s/%s", dirname, TEST_FILE);

	fd = open(test_file_path, O_CREAT, 0644);
	if (fd < 0) {
		pr_perror("Failed to create %s", test_file_path);
		return 1;
	}
	close(fd);

	ifd = inotify_init1(IN_NONBLOCK);
	if (ifd < 0) {
		pr_perror("Failed inotify_init");
		return 1;
	}

	ifd2 = inotify_init1(IN_NONBLOCK);
	if (ifd2 < 0) {
		pr_perror("Failed inotify_init");
		return 1;
	}

	if (inotify_add_watch(ifd, test_file_path, mask) < 0) {
		pr_perror("Failed inotify_add_watch");
		return 1;
	}

	if (inotify_add_watch(ifd2, test_file_path, mask) < 0) {
		pr_perror("Failed inotify_add_watch");
		return 1;
	}

	test_daemon();
	test_waitsig();

	ret = inotify_read_events(ifd, &n);
	if (ret < 0) {
		fail("Failed to read inotify events");
		return 1;
	} else if (n != 0) {
		fail("Found %d unexpected inotify events", n);
		return 1;
	}

	ret = inotify_read_events(ifd, &n);
	if (ret < 0) {
		fail("Failed to read inotify events");
		return 1;
	} else if (n != 0) {
		fail("Found %d unexpected inotify events", n);
		return 1;
	}

	close(ifd);
	close(ifd2);
	unlink(test_file_path);
	pass();

	return 0;
}