Home | History | Annotate | Download | only in fchmodat
      1 /******************************************************************************
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2006
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  *
     19  * NAME
     20  *      fchmodat01.c
     21  *
     22  * DESCRIPTION
     23  *	This test case will verify basic function of fchmodat
     24  *	added by kernel 2.6.16 or up.
     25  *
     26  * Author
     27  *	Yi Yang <yyangcdl (at) cn.ibm.com>
     28  *
     29  * History
     30  *      08/28/2006      Created first by Yi Yang <yyangcdl (at) cn.ibm.com>
     31  *
     32  *****************************************************************************/
     33 
     34 #define _GNU_SOURCE
     35 
     36 #include <sys/types.h>
     37 #include <sys/stat.h>
     38 #include <fcntl.h>
     39 #include <unistd.h>
     40 #include <error.h>
     41 #include <stdlib.h>
     42 #include <errno.h>
     43 #include <string.h>
     44 #include <signal.h>
     45 #include "test.h"
     46 #include "safe_macros.h"
     47 #include "linux_syscall_numbers.h"
     48 
     49 #define TEST_CASES 6
     50 #ifndef AT_FDCWD
     51 #define AT_FDCWD -100
     52 #endif
     53 void setup();
     54 void cleanup();
     55 
     56 char *TCID = "fchmodat01";
     57 int TST_TOTAL = TEST_CASES;
     58 char pathname[256];
     59 char testfile[256];
     60 char testfile2[256];
     61 char testfile3[256];
     62 int fds[TEST_CASES];
     63 char *filenames[TEST_CASES];
     64 int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0, 0 };
     65 
     66 int myfchmodat(int dirfd, const char *filename, mode_t mode)
     67 {
     68 	return ltp_syscall(__NR_fchmodat, dirfd, filename, mode);
     69 }
     70 
     71 int main(int ac, char **av)
     72 {
     73 	int lc;
     74 	int i;
     75 
     76 	/* Disable test if the version of the kernel is less than 2.6.16 */
     77 	if ((tst_kvercmp(2, 6, 16)) < 0) {
     78 		tst_resm(TWARN, "This test can only run on kernels that are ");
     79 		tst_resm(TWARN, "2.6.16 and higher");
     80 		exit(0);
     81 	}
     82 
     83 	tst_parse_opts(ac, av, NULL, NULL);
     84 
     85 	setup();
     86 
     87 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     88 		tst_count = 0;
     89 
     90 		for (i = 0; i < TST_TOTAL; i++) {
     91 			TEST(myfchmodat(fds[i], filenames[i], 0600));
     92 
     93 			if (TEST_ERRNO == expected_errno[i]) {
     94 				tst_resm(TPASS,
     95 					 "fchmodat() returned the expected  errno %d: %s",
     96 					 TEST_ERRNO, strerror(TEST_ERRNO));
     97 			} else {
     98 				tst_resm(TFAIL,
     99 					 "fchmodat() Failed, errno=%d : %s",
    100 					 TEST_ERRNO, strerror(TEST_ERRNO));
    101 			}
    102 		}
    103 	}
    104 
    105 	cleanup();
    106 	tst_exit();
    107 }
    108 
    109 void setup(void)
    110 {
    111 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    112 
    113 	tst_tmpdir();
    114 
    115 	/* Initialize test dir and file names */
    116 	char *abs_path = tst_get_tmpdir();
    117 	int p = getpid();
    118 
    119 	sprintf(pathname, "fchmodattestdir%d", p);
    120 	sprintf(testfile, "fchmodattest%d.txt", p);
    121 	sprintf(testfile2, "%s/fchmodattest%d.txt", abs_path, p);
    122 	sprintf(testfile3, "fchmodattestdir%d/fchmodattest%d.txt", p, p);
    123 
    124 	free(abs_path);
    125 
    126 	SAFE_MKDIR(cleanup, pathname, 0700);
    127 
    128 	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
    129 	fds[1] = fds[4] = fds[0];
    130 
    131 	SAFE_FILE_PRINTF(cleanup, testfile, testfile);
    132 	SAFE_FILE_PRINTF(cleanup, testfile2, testfile2);
    133 
    134 	fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
    135 	fds[3] = 100;
    136 	fds[5] = AT_FDCWD;
    137 
    138 	filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
    139 	filenames[1] = testfile2;
    140 	filenames[5] = testfile3;
    141 
    142 	TEST_PAUSE;
    143 }
    144 
    145 void cleanup(void)
    146 {
    147 	if (fds[0] > 0)
    148 		close(fds[0]);
    149 	if (fds[2] > 0)
    150 		close(fds[2]);
    151 
    152 	tst_rmdir();
    153 }
    154