blob: f89120945a172e4ec7f30159e8c85f9358c5f702 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
plars865695b2001-08-27 22:15:12 +000021 * 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 *
Wanlong Gao330f4e32013-04-26 14:55:25 +080025 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +000026 */
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>
robbiew436cad92001-09-05 16:47:48 +000036#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000037
38#include "test.h"
plars865695b2001-08-27 22:15:12 +000039
40#define TESTDIR "testdir"
41#define TESTFILE "testdir/testfile"
Wanlong Gao330f4e32013-04-26 14:55:25 +080042#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)
plars865695b2001-08-27 22:15:12 +000044
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020045char *TCID = "access04";
46int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000047
Wanlong Gao330f4e32013-04-26 14:55:25 +080048static const char nobody_uid[] = "nobody";
49static struct passwd *ltpuser;
50
51static void setup(void);
52static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000053
subrata_modak56207ce2009-03-23 13:35:39 +000054int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000055{
Wanlong Gao330f4e32013-04-26 14:55:25 +080056 struct stat stat_buf;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020057 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020058 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000059
Wanlong Gao330f4e32013-04-26 14:55:25 +080060 msg = parse_opts(ac, av, NULL, NULL);
61 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080062 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000063
plars865695b2001-08-27 22:15:12 +000064 setup();
65
plars865695b2001-08-27 22:15:12 +000066 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080067 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000068
plars865695b2001-08-27 22:15:12 +000069 TEST(access(TESTFILE, F_OK));
70
plars865695b2001-08-27 22:15:12 +000071 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080072 tst_resm(TFAIL | TTERRNO, "access(%s, F_OK) failed",
73 TESTFILE);
plars865695b2001-08-27 22:15:12 +000074 continue;
75 }
76
Cyril Hrubise38b9612014-06-02 17:20:57 +020077 if (stat(TESTFILE, &stat_buf) < 0) {
78 tst_resm(TFAIL | TERRNO, "stat(%s) failed",
79 TESTFILE);
Wanlong Gao330f4e32013-04-26 14:55:25 +080080 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +020081 tst_resm(TPASS, "functionality of "
82 "access(%s, F_OK) ok", TESTFILE);
Wanlong Gao330f4e32013-04-26 14:55:25 +080083 }
Garrett Cooper2c282152010-12-16 00:55:50 -080084 }
plars865695b2001-08-27 22:15:12 +000085
plars865695b2001-08-27 22:15:12 +000086 cleanup();
Garrett Cooper187e27c2010-12-17 13:30:26 -080087 tst_exit();
Garrett Cooper187e27c2010-12-17 13:30:26 -080088}
89
Wanlong Gao330f4e32013-04-26 14:55:25 +080090static void setup(void)
plars865695b2001-08-27 22:15:12 +000091{
Garrett Cooper187e27c2010-12-17 13:30:26 -080092 int fd;
plars865695b2001-08-27 22:15:12 +000093
plars865695b2001-08-27 22:15:12 +000094 tst_sig(NOFORK, DEF_HANDLER, cleanup);
Garrett Cooper187e27c2010-12-17 13:30:26 -080095 tst_require_root(NULL);
96
subrata_modak56207ce2009-03-23 13:35:39 +000097 ltpuser = getpwnam(nobody_uid);
Garrett Cooper187e27c2010-12-17 13:30:26 -080098 if (ltpuser == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
Garrett Cooper187e27c2010-12-17 13:30:26 -0800100
vapier6f550172009-08-28 10:46:41 +0000101 if (setuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 tst_brkm(TINFO | TERRNO, NULL, "setuid failed");
plars865695b2001-08-27 22:15:12 +0000103
plars865695b2001-08-27 22:15:12 +0000104 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000105 tst_tmpdir();
106
vapier6f550172009-08-28 10:46:41 +0000107 if (mkdir(TESTDIR, DIR_MODE) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 tst_brkm(TBROK | TERRNO, cleanup, "mkdir(%s, %#o) failed",
vapier6f550172009-08-28 10:46:41 +0000109 TESTDIR, DIR_MODE);
plars865695b2001-08-27 22:15:12 +0000110
vapier6f550172009-08-28 10:46:41 +0000111 if (chmod(TESTDIR, DIR_MODE) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, %#o) failed",
vapier6f550172009-08-28 10:46:41 +0000113 TESTDIR, DIR_MODE);
plars865695b2001-08-27 22:15:12 +0000114
vapier6f550172009-08-28 10:46:41 +0000115 fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
116 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800117 tst_brkm(TBROK | TERRNO, cleanup,
vapier6f550172009-08-28 10:46:41 +0000118 "open(%s, O_RDWR|O_CREAT, %#o) failed",
119 TESTFILE, FILE_MODE);
plars865695b2001-08-27 22:15:12 +0000120
vapier6f550172009-08-28 10:46:41 +0000121 if (close(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000123
vapier6f550172009-08-28 10:46:41 +0000124 if (chmod(TESTFILE, 0) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800125 tst_brkm(TBROK | TERRNO, cleanup,
126 "chmod(%s, 0) failed", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000127}
128
Wanlong Gao330f4e32013-04-26 14:55:25 +0800129static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000130{
plars865695b2001-08-27 22:15:12 +0000131 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700132}