1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * inotify testcase common definitions.
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * Copyright (c) 2012-2019 Linux Test Project.  All Rights Reserved.
6f08c3bdfSopenharmony_ci * Ngie Cooper, April 2012
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci#ifndef	_INOTIFY_H
10f08c3bdfSopenharmony_ci#define	_INOTIFY_H
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci/* inotify(7) wrappers */
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#if __NR_inotify_init != __LTP__NR_INVALID_SYSCALL
17f08c3bdfSopenharmony_ci#define myinotify_init() \
18f08c3bdfSopenharmony_ci	tst_syscall(__NR_inotify_init)
19f08c3bdfSopenharmony_ci#else
20f08c3bdfSopenharmony_ci#define myinotify_init() \
21f08c3bdfSopenharmony_ci	tst_syscall(__NR_inotify_init1, 0)
22f08c3bdfSopenharmony_ci#endif
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#define myinotify_init1(flags) \
25f08c3bdfSopenharmony_ci	tst_syscall(__NR_inotify_init1, flags)
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#define myinotify_add_watch(fd, pathname, mask)	\
28f08c3bdfSopenharmony_ci	tst_syscall(__NR_inotify_add_watch, fd, pathname, mask)
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci#define myinotify_rm_watch(fd, wd) \
31f08c3bdfSopenharmony_ci	tst_syscall(__NR_inotify_rm_watch, fd, wd)
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic inline int safe_myinotify_init(const char *file, const int lineno, int fd)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	if (fd < 0) {
36f08c3bdfSopenharmony_ci		if (errno == ENOSYS) {
37f08c3bdfSopenharmony_ci			tst_brk(TCONF,
38f08c3bdfSopenharmony_ci				"%s:%d: inotify is not configured in this kernel",
39f08c3bdfSopenharmony_ci				file, lineno);
40f08c3bdfSopenharmony_ci		} else {
41f08c3bdfSopenharmony_ci			tst_brk(TBROK | TERRNO, "%s:%d: inotify_init failed",
42f08c3bdfSopenharmony_ci				file, lineno);
43f08c3bdfSopenharmony_ci		}
44f08c3bdfSopenharmony_ci	}
45f08c3bdfSopenharmony_ci	return fd;
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci#define SAFE_MYINOTIFY_INIT() \
49f08c3bdfSopenharmony_ci	safe_myinotify_init(__FILE__, __LINE__, myinotify_init())
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci#define SAFE_MYINOTIFY_INIT1(flags) \
52f08c3bdfSopenharmony_ci	safe_myinotify_init(__FILE__, __LINE__, myinotify_init1(flags))
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cistatic inline int safe_myinotify_watch(const char *file, const int lineno, int wd, int fd, const char* fname, const char* mask)
55f08c3bdfSopenharmony_ci{
56f08c3bdfSopenharmony_ci	if (wd < 0) {
57f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO,
58f08c3bdfSopenharmony_ci			"%s:%d: inotify_add_watch (%d, %s, %s) failed",
59f08c3bdfSopenharmony_ci			file, lineno, fd, fname, mask);
60f08c3bdfSopenharmony_ci	}
61f08c3bdfSopenharmony_ci	return wd;
62f08c3bdfSopenharmony_ci}
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci#define SAFE_MYINOTIFY_ADD_WATCH(fd, pathname, mask)	\
65f08c3bdfSopenharmony_ci	safe_myinotify_watch(__FILE__, __LINE__, myinotify_add_watch(fd, pathname, mask), fd, pathname, #mask)
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci#endif /* _INOTIFY_H */
68