Home | History | Annotate | Download | only in fanotify
      1 /*
      2  * Copyright (c) 2017 RedHat.  All Rights Reserved.
      3  *
      4  * This program is free software; you can redistribute it and/or modify it
      5  * under the terms of version 2 of the GNU General Public License as
      6  * published by the Free Software Foundation.
      7  *
      8  * This program is distributed in the hope that it would be useful, but
      9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     11  *
     12  * Further, this software is distributed without any warranty that it is
     13  * free of the rightful claim of any third person regarding infringement
     14  * or the like.  Any license provided herein, whether implied or
     15  * otherwise, applies only to this software file.  Patent licenses, if
     16  * any, provided herein do not apply to combinations of this program with
     17  * other software, or any other product whatsoever.
     18  *
     19  * You should have received a copy of the GNU General Public License along
     20  * with this program; if not, write the Free Software Foundation, Inc.,
     21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     22  *
     23  * Started by Xiong Zhou <xzhou (at) redhat.com>
     24  *
     25  * DESCRIPTION
     26  *     Sanity check fanotify_init flag FAN_CLOEXEC by fcntl.
     27  */
     28 #define _GNU_SOURCE
     29 #include "config.h"
     30 
     31 #include <stdio.h>
     32 #include <sys/stat.h>
     33 #include <sys/types.h>
     34 #include <fcntl.h>
     35 #include <errno.h>
     36 #include <string.h>
     37 #include <sys/syscall.h>
     38 #include "tst_test.h"
     39 #include "fanotify.h"
     40 
     41 #if defined(HAVE_SYS_FANOTIFY_H)
     42 #include <sys/fanotify.h>
     43 
     44 static int fd_notify;
     45 
     46 static void test_init_bit(unsigned int fan_bit,
     47 			unsigned int fd_bit, char *msg)
     48 {
     49 	int ret;
     50 
     51 	fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF|fan_bit, O_RDONLY);
     52 
     53 	ret = SAFE_FCNTL(fd_notify, F_GETFD);
     54 
     55 	if ((ret & FD_CLOEXEC) == fd_bit) {
     56 		tst_res(TPASS, "%s", msg);
     57 	} else {
     58 		tst_res(TFAIL, "%s", msg);
     59 	}
     60 
     61 	SAFE_CLOSE(fd_notify);
     62 }
     63 
     64 static void run(unsigned int i)
     65 {
     66 	switch (i) {
     67 	case 0:
     68 		test_init_bit(0, 0, "not set close_on_exec");
     69 		break;
     70 	case 1:
     71 		test_init_bit(FAN_CLOEXEC, FD_CLOEXEC, "set close_on_exec");
     72 		break;
     73 	}
     74 }
     75 
     76 static void cleanup(void)
     77 {
     78 	if (fd_notify > 0)
     79 		SAFE_CLOSE(fd_notify);
     80 }
     81 
     82 static struct tst_test test = {
     83 	.test = run,
     84 	.tcnt = 2,
     85 	.cleanup = cleanup,
     86 	.needs_root = 1,
     87 };
     88 
     89 #else
     90 	TST_TEST_TCONF("system doesn't have required fanotify support");
     91 #endif
     92