1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
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
20 /*
21 * Test Description:
22 * Verify that access() succeeds to check the existance of a file if
23 * search access is permitted on the pathname of the specified file.
24 *
25 * 07/2001 Ported by Wayne Boyer
26 */
27
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <signal.h>
35 #include <sys/stat.h>
36 #include <pwd.h>
37
38 #include "test.h"
39
40 #define TESTDIR "testdir"
41 #define TESTFILE "testdir/testfile"
42 #define DIR_MODE (S_IRWXU | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP)
43 #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
44
45 char *TCID = "access04";
46 int TST_TOTAL = 1;
47
48 static const char nobody_uid[] = "nobody";
49 static struct passwd *ltpuser;
50
51 static void setup(void);
52 static void cleanup(void);
53
main(int ac,char ** av)54 int main(int ac, char **av)
55 {
56 struct stat stat_buf;
57 int lc;
58
59 tst_parse_opts(ac, av, NULL, NULL);
60
61 setup();
62
63 for (lc = 0; TEST_LOOPING(lc); lc++) {
64 tst_count = 0;
65
66 TEST(access(TESTFILE, F_OK));
67
68 if (TEST_RETURN == -1) {
69 tst_resm(TFAIL | TTERRNO, "access(%s, F_OK) failed",
70 TESTFILE);
71 continue;
72 }
73
74 if (stat(TESTFILE, &stat_buf) < 0) {
75 tst_resm(TFAIL | TERRNO, "stat(%s) failed",
76 TESTFILE);
77 } else {
78 tst_resm(TPASS, "functionality of "
79 "access(%s, F_OK) ok", TESTFILE);
80 }
81 }
82
83 cleanup();
84 tst_exit();
85 }
86
setup(void)87 static void setup(void)
88 {
89 int fd;
90
91 tst_sig(NOFORK, DEF_HANDLER, cleanup);
92 tst_require_root();
93
94 ltpuser = getpwnam(nobody_uid);
95 if (ltpuser == NULL)
96 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
97
98 if (setuid(ltpuser->pw_uid) == -1)
99 tst_brkm(TINFO | TERRNO, NULL, "setuid failed");
100
101 TEST_PAUSE;
102 tst_tmpdir();
103
104 if (mkdir(TESTDIR, DIR_MODE) < 0)
105 tst_brkm(TBROK | TERRNO, cleanup, "mkdir(%s, %#o) failed",
106 TESTDIR, DIR_MODE);
107
108 if (chmod(TESTDIR, DIR_MODE) < 0)
109 tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, %#o) failed",
110 TESTDIR, DIR_MODE);
111
112 fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
113 if (fd == -1)
114 tst_brkm(TBROK | TERRNO, cleanup,
115 "open(%s, O_RDWR|O_CREAT, %#o) failed",
116 TESTFILE, FILE_MODE);
117
118 if (close(fd) == -1)
119 tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", TESTFILE);
120
121 if (chmod(TESTFILE, 0) < 0)
122 tst_brkm(TBROK | TERRNO, cleanup,
123 "chmod(%s, 0) failed", TESTFILE);
124 }
125
cleanup(void)126 static void cleanup(void)
127 {
128 tst_rmdir();
129 }
130