Home | History | Annotate | Download | only in fcntl
      1 /*
      2  * Copyright (c) 2014 Fujitsu Ltd.
      3  * Author: Xiaoguang Wang <wangxg.fnst (at) cn.fujitsu.com>
      4  *
      5  * This program is free software; you can redistribute it and/or modify it
      6  * under the terms of version 2 of the GNU General Public License as
      7  * published by the Free Software Foundation.
      8  *
      9  * This program is distributed in the hope that it would be useful, but
     10  * WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     12  *
     13  * You should have received a copy of the GNU General Public License along
     14  * with this program; if not, write the Free Software Foundation, Inc.,
     15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     16  */
     17 
     18 /*
     19  * Description:
     20  * Verify that,
     21  *   Basic test for fcntl(2) using F_DUPFD_CLOEXEC argument.
     22  */
     23 
     24 #include <stdio.h>
     25 #include <errno.h>
     26 #include <unistd.h>
     27 #include <fcntl.h>
     28 #include <string.h>
     29 #include <signal.h>
     30 #include <sys/types.h>
     31 #include <sys/stat.h>
     32 #include <pwd.h>
     33 
     34 #include "test.h"
     35 #include "safe_macros.h"
     36 #include "lapi/fcntl.h"
     37 
     38 char *TCID = "fcntl29";
     39 int TST_TOTAL = 1;
     40 
     41 static void setup(void);
     42 static void cleanup(void);
     43 
     44 static int test_fd;
     45 
     46 int main(int ac, char **av)
     47 {
     48 	int lc, dup_fd;
     49 
     50 	tst_parse_opts(ac, av, NULL, NULL);
     51 
     52 	setup();
     53 
     54 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     55 		tst_count = 0;
     56 
     57 		TEST(fcntl(test_fd, F_DUPFD_CLOEXEC, 0));
     58 		if (TEST_RETURN < 0) {
     59 			tst_brkm(TFAIL | TTERRNO, cleanup, "fcntl "
     60 				 "test F_DUPFD_CLOEXEC failed");
     61 		}
     62 		dup_fd = TEST_RETURN;
     63 
     64 		TEST(fcntl(dup_fd, F_GETFD));
     65 		if (TEST_RETURN < 0) {
     66 			SAFE_CLOSE(cleanup, dup_fd);
     67 			tst_brkm(TFAIL | TTERRNO, cleanup, "fcntl "
     68 				 "test F_GETFD failed");
     69 		}
     70 
     71 		if (TEST_RETURN & FD_CLOEXEC) {
     72 			tst_resm(TPASS, "fcntl test "
     73 				 "F_DUPFD_CLOEXEC success");
     74 		} else {
     75 			tst_resm(TFAIL, "fcntl test "
     76 				 "F_DUPFD_CLOEXEC fail");
     77 		}
     78 
     79 		SAFE_CLOSE(cleanup, dup_fd);
     80 	}
     81 
     82 	cleanup();
     83 	tst_exit();
     84 }
     85 
     86 static void setup(void)
     87 {
     88 	if ((tst_kvercmp(2, 6, 24)) < 0) {
     89 		tst_brkm(TCONF, NULL, "This test can only run on kernels"
     90 			 "that are 2.6.24 and higher");
     91 	}
     92 
     93 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
     94 
     95 	tst_tmpdir();
     96 
     97 	TEST_PAUSE;
     98 
     99 	test_fd = SAFE_CREAT(cleanup, "testfile", 0644);
    100 }
    101 
    102 static void cleanup(void)
    103 {
    104 	if (test_fd > 0)
    105 		SAFE_CLOSE(NULL, test_fd);
    106 
    107 	tst_rmdir();
    108 }
    109