plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 3 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 7 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 11 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | */ |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * Description: |
| 27 | * Basic test for access(2) using F_OK, R_OK, W_OK and X_OK on tmp file |
| 28 | * AUTHOR : William Roske |
| 29 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | |
| 31 | #include <string.h> |
plars | 19d169e | 2002-09-04 13:34:31 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | #include <sys/types.h> |
| 34 | #include <sys/fcntl.h> |
| 35 | #include <errno.h> |
| 36 | #include <signal.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include "test.h" |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 39 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 41 | char *TCID = "access01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 43 | char fname[255]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | |
| 45 | static struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 46 | char *file; |
| 47 | int mode; |
| 48 | char *string; |
| 49 | int experrno; |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 50 | } test_cases[] = { |
| 51 | {fname, F_OK, "F_OK", 0}, |
| 52 | {fname, X_OK, "X_OK", 0}, |
| 53 | {fname, W_OK, "W_OK", 0}, |
| 54 | {fname, R_OK, "R_OK", 0}, |
| 55 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 57 | int TST_TOTAL = sizeof(test_cases) / sizeof(struct test_case_t); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 59 | static void setup(void); |
| 60 | static void cleanup(void); |
| 61 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | int main(int ac, char **av) |
| 63 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 64 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 65 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | int tc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 68 | msg = parse_opts(ac, av, NULL, NULL); |
| 69 | if (msg != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 70 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 75 | tst_count = 0; |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 76 | for (tc = 0; tc < TST_TOTAL; tc++) { |
| 77 | TEST(access(test_cases[tc].file, test_cases[tc].mode)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 79 | if (TEST_RETURN == -1 && test_cases[tc].experrno == 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | tst_resm(TFAIL | TTERRNO, |
| 81 | "access(%s, %s) failed", |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 82 | test_cases[tc].file, |
| 83 | test_cases[tc].string); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | } else if (TEST_RETURN != -1 |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 86 | && test_cases[tc].experrno != 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 88 | "access(%s, %s) returned %ld, " |
| 89 | "exp -1, errno:%d", |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 90 | test_cases[tc].file, |
| 91 | test_cases[tc].string, TEST_RETURN, |
| 92 | test_cases[tc].experrno); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 94 | tst_resm(TPASS, |
| 95 | "access(%s, %s) returned %ld", |
| 96 | test_cases[tc].file, |
| 97 | test_cases[tc].string, |
| 98 | TEST_RETURN); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 102 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | cleanup(); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 105 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 106 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 108 | static void setup(void) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 109 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | int fd; |
| 111 | struct stat stbuf; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 115 | umask(0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | TEST_PAUSE; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | * Since files inherit group ids, make sure our dir has a valid grp |
| 121 | * to us. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | */ |
yaberauneya | db37adc | 2009-11-09 05:56:58 +0000 | [diff] [blame] | 123 | if (chown(".", -1, getegid()) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | tst_brkm(TBROK | TERRNO, cleanup, |
| 125 | "chown(\".\", -1, %d) failed", getegid()); |
yaberauneya | db37adc | 2009-11-09 05:56:58 +0000 | [diff] [blame] | 126 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 128 | snprintf(fname, sizeof(fname), "accessfile"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 130 | fd = open(fname, O_RDWR | O_CREAT, 06777); |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 131 | if (fd == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | tst_brkm(TBROK | TERRNO, cleanup, |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 133 | "open(%s, O_RDWR|O_CREAT, 06777) failed", fname); |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 134 | else if (close(fd) == -1) |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 135 | tst_resm(TINFO | TERRNO, "close(%s) failed", fname); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * force the mode to be set to 6777 |
| 139 | */ |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 140 | if (chmod(fname, 06777) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, 06777) failed", |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 142 | fname); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 144 | stat(fname, &stbuf); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | |
| 146 | if ((stbuf.st_mode & 06777) != 06777) { |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 147 | tst_brkm(TBROK, cleanup, "'%s' can't be properly setup", |
| 148 | fname); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | } |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 150 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | |
Wanlong Gao | f85fea7 | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 152 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 155 | } |