• 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 <unistd.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <signal.h>
34 
35 #include "test.h"
36 #include "safe_macros.h"
37 #include "fchownat.h"
38 #include "lapi/fcntl.h"
39 
40 #define TESTFILE	"testfile"
41 
42 static void setup(void);
43 static void cleanup(void);
44 
45 static int dir_fd;
46 static int fd;
47 static int no_fd = -1;
48 static int cu_fd = AT_FDCWD;
49 
50 static struct test_case_t {
51 	int exp_ret;
52 	int exp_errno;
53 	int flag;
54 	int *fds;
55 	char *filenames;
56 } test_cases[] = {
57 	{0, 0, 0, &dir_fd, TESTFILE},
58 	{-1, ENOTDIR, 0, &fd, TESTFILE},
59 	{-1, EBADF, 0, &no_fd, TESTFILE},
60 	{-1, EINVAL, 9999, &dir_fd, TESTFILE},
61 	{0, 0, 0, &cu_fd, TESTFILE},
62 };
63 
64 char *TCID = "fchownat01";
65 int TST_TOTAL = ARRAY_SIZE(test_cases);
66 static void fchownat_verify(const struct test_case_t *);
67 
main(int ac,char ** av)68 int main(int ac, char **av)
69 {
70 	int lc;
71 	int i;
72 
73 	tst_parse_opts(ac, av, NULL, NULL);
74 
75 	setup();
76 
77 	for (lc = 0; TEST_LOOPING(lc); lc++) {
78 		tst_count = 0;
79 		for (i = 0; i < TST_TOTAL; i++)
80 			fchownat_verify(&test_cases[i]);
81 	}
82 
83 	cleanup();
84 	tst_exit();
85 }
86 
setup(void)87 static void setup(void)
88 {
89 	if ((tst_kvercmp(2, 6, 16)) < 0)
90 		tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer");
91 
92 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
93 
94 	TEST_PAUSE;
95 
96 	tst_tmpdir();
97 
98 	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
99 
100 	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
101 
102 	fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
103 }
104 
fchownat_verify(const struct test_case_t * test)105 static void fchownat_verify(const struct test_case_t *test)
106 {
107 	TEST(fchownat(*(test->fds), test->filenames, geteuid(),
108 		      getegid(), test->flag));
109 
110 	if (TEST_RETURN != test->exp_ret) {
111 		tst_resm(TFAIL | TTERRNO,
112 			 "fchownat() returned %ld, expected %d, errno=%d",
113 			 TEST_RETURN, test->exp_ret, test->exp_errno);
114 		return;
115 	}
116 
117 	if (TEST_ERRNO == test->exp_errno) {
118 		tst_resm(TPASS | TTERRNO,
119 			 "fchownat() returned the expected errno %d: %s",
120 			 test->exp_ret, strerror(test->exp_errno));
121 	} else {
122 		tst_resm(TFAIL | TTERRNO,
123 			 "fchownat() failed unexpectedly; expected: %d - %s",
124 			 test->exp_errno, strerror(test->exp_errno));
125 	}
126 }
127 
cleanup(void)128 static void cleanup(void)
129 {
130 	if (fd > 0)
131 		close(fd);
132 
133 	if (dir_fd > 0)
134 		close(dir_fd);
135 
136 	tst_rmdir();
137 }
138