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@cn.ibm.com>
28 *
29 * History
30 * 08/28/2006 Created first by Yi Yang <yyangcdl@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 <stdlib.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <signal.h>
44 #include "test.h"
45 #include "safe_macros.h"
46 #include "lapi/syscalls.h"
47
48 #define TEST_CASES 6
49 #ifndef AT_FDCWD
50 #define AT_FDCWD -100
51 #endif
52 void setup();
53 void cleanup();
54
55 char *TCID = "fchmodat01";
56 int TST_TOTAL = TEST_CASES;
57 char pathname[256];
58 char testfile[256];
59 char testfile2[256];
60 char testfile3[256];
61 int fds[TEST_CASES];
62 char *filenames[TEST_CASES];
63 int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0, 0 };
64
myfchmodat(int dirfd,const char * filename,mode_t mode)65 int myfchmodat(int dirfd, const char *filename, mode_t mode)
66 {
67 return ltp_syscall(__NR_fchmodat, dirfd, filename, mode);
68 }
69
main(int ac,char ** av)70 int main(int ac, char **av)
71 {
72 int lc;
73 int i;
74
75 /* Disable test if the version of the kernel is less than 2.6.16 */
76 if ((tst_kvercmp(2, 6, 16)) < 0) {
77 tst_resm(TWARN, "This test can only run on kernels that are ");
78 tst_resm(TWARN, "2.6.16 and higher");
79 exit(0);
80 }
81
82 tst_parse_opts(ac, av, NULL, NULL);
83
84 setup();
85
86 for (lc = 0; TEST_LOOPING(lc); lc++) {
87 tst_count = 0;
88
89 for (i = 0; i < TST_TOTAL; i++) {
90 TEST(myfchmodat(fds[i], filenames[i], 0600));
91
92 if (TEST_ERRNO == expected_errno[i]) {
93 tst_resm(TPASS,
94 "fchmodat() returned the expected errno %d: %s",
95 TEST_ERRNO, strerror(TEST_ERRNO));
96 } else {
97 tst_resm(TFAIL,
98 "fchmodat() Failed, errno=%d : %s",
99 TEST_ERRNO, strerror(TEST_ERRNO));
100 }
101 }
102 }
103
104 cleanup();
105 tst_exit();
106 }
107
setup(void)108 void setup(void)
109 {
110 tst_sig(NOFORK, DEF_HANDLER, cleanup);
111
112 tst_tmpdir();
113
114 /* Initialize test dir and file names */
115 char *abs_path = tst_get_tmpdir();
116 int p = getpid();
117
118 sprintf(pathname, "fchmodattestdir%d", p);
119 sprintf(testfile, "fchmodattest%d.txt", p);
120 sprintf(testfile2, "%s/fchmodattest%d.txt", abs_path, p);
121 sprintf(testfile3, "fchmodattestdir%d/fchmodattest%d.txt", p, p);
122
123 free(abs_path);
124
125 SAFE_MKDIR(cleanup, pathname, 0700);
126
127 fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
128 fds[1] = fds[4] = fds[0];
129
130 SAFE_FILE_PRINTF(cleanup, testfile, "%s", testfile);
131 SAFE_FILE_PRINTF(cleanup, testfile2, "%s", testfile2);
132
133 fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
134 fds[3] = 100;
135 fds[5] = AT_FDCWD;
136
137 filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
138 filenames[1] = testfile2;
139 filenames[5] = testfile3;
140
141 TEST_PAUSE;
142 }
143
cleanup(void)144 void cleanup(void)
145 {
146 if (fds[0] > 0)
147 close(fds[0]);
148 if (fds[2] > 0)
149 close(fds[2]);
150
151 tst_rmdir();
152 }
153