summaryrefslogtreecommitdiff
path: root/test/misc/tst-inotify.c
blob: f9f6830e399276f3acc6f77cea9d2730e92e8f58 (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
/* vi: set sw=4 ts=4 sts=4: */
/*
 * inotify test for uClibc
 * Copyright (C) 2012 by Kevin Cernekee <cernekee@gmail.com>
 *
 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/inotify.h>
#include <sys/fcntl.h>

static int
do_test(void)
{
	int ifd, fd, ret, result = 0;
	struct inotify_event e;
	char tfile[] = "/tmp/inotify.XXXXXX";

	fd = mkstemp(tfile);
	close(fd);

	ifd = inotify_init1(IN_NONBLOCK);
	if (ifd < 0) {
		perror("inotify_init1()");
		result = 1;
	}
	if (inotify_add_watch(ifd, tfile, IN_DELETE_SELF) < 0) {
		perror("inotify_add_watch()");
		result = 1;
	}

	/* nonblocking inotify should return immediately with no events */
	ret = read(ifd, &e, sizeof(e));
	if (ret != -1 || errno != EAGAIN) {
		fprintf(stderr, "first read() returned %d\n", ret);
		result = 1;
	}

	/* generate an event */
	unlink(tfile);

	/* now check whether our event was seen */
	ret = read(ifd, &e, sizeof(e));
	if (ret != sizeof(e)) {
		fprintf(stderr, "second read() returned %d\n", ret);
		result = 1;
	}

	if (!(e.mask & IN_DELETE_SELF)) {
		fprintf(stderr, "incorrect event mask: %" PRIx32 "\n", e.mask);
		result = 1;
	}

	return result;
}

#define TIMEOUT 5
#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"