• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2017 RedHat.  All Rights Reserved.
4  *
5  * Started by Xiong Zhou <xzhou@redhat.com>
6  */
7 
8 /*\
9  * [Description]
10  * Sanity check fanotify_init flag FAN_CLOEXEC by fcntl.
11  */
12 
13 #define _GNU_SOURCE
14 #include "config.h"
15 
16 #include <stdio.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/syscall.h>
22 #include "tst_test.h"
23 
24 #ifdef HAVE_SYS_FANOTIFY_H
25 #include "fanotify.h"
26 
27 static int fd_notify;
28 
test_init_bit(unsigned int fan_bit,unsigned int fd_bit,char * msg)29 static void test_init_bit(unsigned int fan_bit,
30 			unsigned int fd_bit, char *msg)
31 {
32 	int ret;
33 
34 	fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF|fan_bit, O_RDONLY);
35 
36 	ret = SAFE_FCNTL(fd_notify, F_GETFD);
37 
38 	if ((ret & FD_CLOEXEC) == fd_bit) {
39 		tst_res(TPASS, "%s", msg);
40 	} else {
41 		tst_res(TFAIL, "%s", msg);
42 	}
43 
44 	SAFE_CLOSE(fd_notify);
45 }
46 
run(unsigned int i)47 static void run(unsigned int i)
48 {
49 	switch (i) {
50 	case 0:
51 		test_init_bit(0, 0, "not set close_on_exec");
52 		break;
53 	case 1:
54 		test_init_bit(FAN_CLOEXEC, FD_CLOEXEC, "set close_on_exec");
55 		break;
56 	}
57 }
58 
cleanup(void)59 static void cleanup(void)
60 {
61 	if (fd_notify > 0)
62 		SAFE_CLOSE(fd_notify);
63 }
64 
65 static struct tst_test test = {
66 	.test = run,
67 	.tcnt = 2,
68 	.cleanup = cleanup,
69 	.needs_root = 1,
70 };
71 
72 #else
73 	TST_TEST_TCONF("system doesn't have required fanotify support");
74 #endif
75