Home | History | Annotate | Download | only in faccessat
      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  *      faccessat01.c
     21  *
     22  * DESCRIPTION
     23  *	This test case will verify basic function of faccessat
     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 = "faccessat01";
     57 int TST_TOTAL = TEST_CASES;
     58 static char pathname[256];
     59 static char testfile[256];
     60 static char testfile2[256];
     61 static char testfile3[256];
     62 static int fds[TEST_CASES];
     63 static char *filenames[TEST_CASES];
     64 static int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0, 0 };
     65 
     66 int myfaccessat(int dirfd, const char *filename, int mode)
     67 {
     68 	return ltp_syscall(__NR_faccessat, 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_brkm(TCONF, NULL, "Test must be run with kernel 2.6.16+");
     79 
     80 	tst_parse_opts(ac, av, NULL, NULL);
     81 
     82 	setup();
     83 
     84 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     85 		tst_count = 0;
     86 
     87 		/*
     88 		 * Call faccessat
     89 		 */
     90 		for (i = 0; i < TST_TOTAL; i++) {
     91 			TEST(myfaccessat(fds[i], filenames[i], R_OK));
     92 
     93 			/* check return code */
     94 			if (TEST_ERRNO == expected_errno[i]) {
     95 				tst_resm(TPASS,
     96 					 "faccessat() returned the expected  errno %d: %s",
     97 					 TEST_ERRNO,
     98 					 strerror(TEST_ERRNO));
     99 			} else {
    100 				tst_resm(TFAIL,
    101 					 "faccessdat() Failed, errno=%d : %s",
    102 					 TEST_ERRNO, strerror(TEST_ERRNO));
    103 			}
    104 		}
    105 	}
    106 
    107 	cleanup();
    108 	tst_exit();
    109 }
    110 
    111 void setup(void)
    112 {
    113 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    114 
    115 	tst_tmpdir();
    116 
    117 	char *abs_path = tst_get_tmpdir();
    118 	int p = getpid();
    119 
    120 	/* Initialize test dir and file names */
    121 	sprintf(pathname, "faccessattestdir%d", p);
    122 	sprintf(testfile, "faccessattestfile%d.txt", p);
    123 	sprintf(testfile2, "%s/faccessattestfile%d.txt", abs_path, p);
    124 	sprintf(testfile3, "faccessattestdir%d/faccessattestfile%d.txt", p, p);
    125 
    126 	free(abs_path);
    127 
    128 	SAFE_MKDIR(cleanup, pathname, 0700);
    129 
    130 	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
    131 	fds[1] = fds[4] = fds[0];
    132 
    133 	SAFE_FILE_PRINTF(cleanup, testfile, testfile);
    134 	SAFE_FILE_PRINTF(cleanup, testfile2, testfile2);
    135 
    136 	fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
    137 
    138 	fds[3] = 100;
    139 	fds[5] = AT_FDCWD;
    140 
    141 	filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
    142 	filenames[1] = testfile2;
    143 	filenames[5] = testfile3;
    144 
    145 	TEST_PAUSE;
    146 }
    147 
    148 void cleanup(void)
    149 {
    150 	if (fds[0] > 0)
    151 		close(fds[0]);
    152 	if (fds[2] > 0)
    153 		close(fds[2]);
    154 
    155 	tst_rmdir();
    156 }
    157