plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * 07/2001 Ported by Wayne Boyer |
| 4 | * |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 19 | /* |
| 20 | * DESCRIPTION |
| 21 | * 1. open a new file without O_CREAT, ENOENT should be returned. |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 22 | * 2. open a file with O_RDONLY | O_NOATIME and the caller was not |
| 23 | * privileged, EPERM should be returned. |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 24 | */ |
| 25 | |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 26 | #define _GNU_SOURCE |
| 27 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <errno.h> |
| 32 | #include <sys/fcntl.h> |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 33 | #include <pwd.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 34 | #include "test.h" |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 35 | #include "safe_macros.h" |
| 36 | #include "lapi/fcntl.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 38 | #define TEST_FILE "test_file" |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 39 | #define TEST_FILE2 "test_file2" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 41 | char *TCID = "open02"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
Wanlong Gao | 001ce03 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 43 | static void cleanup(void); |
| 44 | static void setup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 46 | static struct test_case_t { |
| 47 | char *filename; |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 48 | int flag; |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 49 | int exp_errno; |
| 50 | } test_cases[] = { |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 51 | {TEST_FILE, O_RDWR, ENOENT}, |
| 52 | {TEST_FILE2, O_RDONLY | O_NOATIME, EPERM}, |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
| 56 | static void open_verify(const struct test_case_t *); |
| 57 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 58 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 60 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 61 | const char *msg; |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 62 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
Wanlong Gao | 001ce03 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 64 | msg = parse_opts(ac, av, NULL, NULL); |
| 65 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 66 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
Wanlong Gao | 001ce03 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 68 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 71 | tst_count = 0; |
| 72 | for (i = 0; i < TST_TOTAL; i++) |
| 73 | open_verify(&test_cases[i]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
Wanlong Gao | 001ce03 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 76 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 77 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Wanlong Gao | 001ce03 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 80 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | { |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 82 | struct passwd *ltpuser; |
| 83 | |
| 84 | tst_require_root(NULL); |
| 85 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 87 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | TEST_PAUSE; |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 89 | |
| 90 | tst_tmpdir(); |
| 91 | |
| 92 | SAFE_TOUCH(cleanup, TEST_FILE2, 0644, NULL); |
| 93 | |
| 94 | ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); |
| 95 | |
| 96 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 97 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 99 | static void open_verify(const struct test_case_t *test) |
| 100 | { |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 101 | TEST(open(test->filename, test->flag, 0444)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 103 | if (TEST_RETURN != -1) { |
| 104 | tst_resm(TFAIL, "open(%s) succeeded unexpectedly", |
| 105 | test->filename); |
| 106 | return; |
| 107 | } |
| 108 | |
Zeng Linggang | a0a3bd9 | 2014-03-13 21:09:54 +0800 | [diff] [blame] | 109 | if (TEST_ERRNO != test->exp_errno) { |
| 110 | tst_resm(TFAIL | TTERRNO, |
| 111 | "open() failed unexpectedly; expected: %d - %s", |
| 112 | test->exp_errno, strerror(test->exp_errno)); |
| 113 | } else { |
| 114 | tst_resm(TPASS | TTERRNO, "open() failed as expected"); |
| 115 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Wanlong Gao | 001ce03 | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 118 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | { |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 120 | if (seteuid(0)) |
| 121 | tst_resm(TWARN | TERRNO, "seteuid(0) failed"); |
| 122 | |
Zeng Linggang | 8553a4b | 2014-03-13 21:10:48 +0800 | [diff] [blame] | 123 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 124 | } |