• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Copyright (c) International Business Machines  Corp., 2006
3  *   AUTHOR: Yi Yang <yyangcdl@cn.ibm.com>
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 Foundation,
17  *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 /*
20  * DESCRIPTION
21  *	This test case will verify basic function of fchownat
22  *	added by kernel 2.6.16 or up.
23  */
24 
25 #define _GNU_SOURCE
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <error.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <signal.h>
36 
37 #include "test.h"
38 #include "safe_macros.h"
39 #include "fchownat.h"
40 #include "lapi/fcntl.h"
41 
42 #define TESTFILE	"testfile"
43 
44 static void setup(void);
45 static void cleanup(void);
46 
47 static int dir_fd;
48 static int fd;
49 static int no_fd = -1;
50 static int cu_fd = AT_FDCWD;
51 
52 static struct test_case_t {
53 	int exp_ret;
54 	int exp_errno;
55 	int flag;
56 	int *fds;
57 	char *filenames;
58 } test_cases[] = {
59 	{0, 0, 0, &dir_fd, TESTFILE},
60 	{-1, ENOTDIR, 0, &fd, TESTFILE},
61 	{-1, EBADF, 0, &no_fd, TESTFILE},
62 	{-1, EINVAL, 9999, &dir_fd, TESTFILE},
63 	{0, 0, 0, &cu_fd, TESTFILE},
64 };
65 
66 char *TCID = "fchownat01";
67 int TST_TOTAL = ARRAY_SIZE(test_cases);
68 static void fchownat_verify(const struct test_case_t *);
69 
main(int ac,char ** av)70 int main(int ac, char **av)
71 {
72 	int lc;
73 	int i;
74 
75 	tst_parse_opts(ac, av, NULL, NULL);
76 
77 	setup();
78 
79 	for (lc = 0; TEST_LOOPING(lc); lc++) {
80 		tst_count = 0;
81 		for (i = 0; i < TST_TOTAL; i++)
82 			fchownat_verify(&test_cases[i]);
83 	}
84 
85 	cleanup();
86 	tst_exit();
87 }
88 
setup(void)89 static void setup(void)
90 {
91 	if ((tst_kvercmp(2, 6, 16)) < 0)
92 		tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer");
93 
94 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
95 
96 	TEST_PAUSE;
97 
98 	tst_tmpdir();
99 
100 	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
101 
102 	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
103 
104 	fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
105 }
106 
fchownat_verify(const struct test_case_t * test)107 static void fchownat_verify(const struct test_case_t *test)
108 {
109 	TEST(fchownat(*(test->fds), test->filenames, geteuid(),
110 		      getegid(), test->flag));
111 
112 	if (TEST_RETURN != test->exp_ret) {
113 		tst_resm(TFAIL | TTERRNO,
114 			 "fchownat() returned %ld, expected %d, errno=%d",
115 			 TEST_RETURN, test->exp_ret, test->exp_errno);
116 		return;
117 	}
118 
119 	if (TEST_ERRNO == test->exp_errno) {
120 		tst_resm(TPASS | TTERRNO,
121 			 "fchownat() returned the expected errno %d: %s",
122 			 test->exp_ret, strerror(test->exp_errno));
123 	} else {
124 		tst_resm(TFAIL | TTERRNO,
125 			 "fchownat() failed unexpectedly; expected: %d - %s",
126 			 test->exp_errno, strerror(test->exp_errno));
127 	}
128 }
129 
cleanup(void)130 static void cleanup(void)
131 {
132 	if (fd > 0)
133 		close(fd);
134 
135 	if (dir_fd > 0)
136 		close(dir_fd);
137 
138 	tst_rmdir();
139 }
140