plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | * |
Wanlong Gao | 330f4e3 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 25 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
robbiew | 436cad9 | 2001-09-05 16:47:48 +0000 | [diff] [blame] | 36 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | |
| 38 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | |
| 40 | #define TESTDIR "testdir" |
| 41 | #define TESTFILE "testdir/testfile" |
Wanlong Gao | 330f4e3 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 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) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 45 | char *TCID = "access04"; |
| 46 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | |
Wanlong Gao | 330f4e3 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 48 | static const char nobody_uid[] = "nobody"; |
| 49 | static struct passwd *ltpuser; |
| 50 | |
| 51 | static void setup(void); |
| 52 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 54 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | { |
Wanlong Gao | 330f4e3 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 56 | struct stat stat_buf; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 57 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 58 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | |
Wanlong Gao | 330f4e3 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 60 | msg = parse_opts(ac, av, NULL, NULL); |
| 61 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 62 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | setup(); |
| 65 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 67 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | TEST(access(TESTFILE, F_OK)); |
| 70 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 72 | tst_resm(TFAIL | TTERRNO, "access(%s, F_OK) failed", |
| 73 | TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | continue; |
| 75 | } |
| 76 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 77 | if (stat(TESTFILE, &stat_buf) < 0) { |
| 78 | tst_resm(TFAIL | TERRNO, "stat(%s) failed", |
| 79 | TESTFILE); |
Wanlong Gao | 330f4e3 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 80 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 81 | tst_resm(TPASS, "functionality of " |
| 82 | "access(%s, F_OK) ok", TESTFILE); |
Wanlong Gao | 330f4e3 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 83 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 84 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | cleanup(); |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 87 | tst_exit(); |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Wanlong Gao | 330f4e3 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 90 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | { |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 92 | int fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 95 | tst_require_root(NULL); |
| 96 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | ltpuser = getpwnam(nobody_uid); |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 98 | if (ltpuser == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed"); |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 100 | |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 101 | if (setuid(ltpuser->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | tst_brkm(TINFO | TERRNO, NULL, "setuid failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | tst_tmpdir(); |
| 106 | |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 107 | if (mkdir(TESTDIR, DIR_MODE) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 108 | tst_brkm(TBROK | TERRNO, cleanup, "mkdir(%s, %#o) failed", |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 109 | TESTDIR, DIR_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 111 | if (chmod(TESTDIR, DIR_MODE) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 112 | tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, %#o) failed", |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 113 | TESTDIR, DIR_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 115 | fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE); |
| 116 | if (fd == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 117 | tst_brkm(TBROK | TERRNO, cleanup, |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 118 | "open(%s, O_RDWR|O_CREAT, %#o) failed", |
| 119 | TESTFILE, FILE_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 121 | if (close(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 124 | if (chmod(TESTFILE, 0) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | tst_brkm(TBROK | TERRNO, cleanup, |
| 126 | "chmod(%s, 0) failed", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Wanlong Gao | 330f4e3 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 129 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 132 | } |