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 | /* |
| 21 | * Test Name : readlink01 |
| 22 | * |
| 23 | * Test Description : |
| 24 | * Verify that, readlink will succeed to read the contents of the symbolic |
| 25 | * link created the process. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * Expected Result: |
| 28 | * readlink() should return the contents of symbolic link path in the buffer |
| 29 | * on success. |
| 30 | * |
| 31 | * Algorithm: |
| 32 | * Setup: |
| 33 | * Setup signal handling. |
| 34 | * Create temporary directory. |
| 35 | * Pause for SIGUSR1 if option specified. |
| 36 | * |
| 37 | * Test: |
| 38 | * Loop if the proper options are given. |
| 39 | * Execute system call |
| 40 | * Check return code, if system call failed (return=-1) |
| 41 | * Issue a FAIL message. |
| 42 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 43 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * if successful, |
| 45 | * Issue Functionality-Pass message. |
| 46 | * Otherwise, |
| 47 | * Issue Functionality-Fail message. |
| 48 | * Cleanup: |
| 49 | * Print errno log and/or timing stats if options given |
| 50 | * Delete the temporary directory created. |
| 51 | * |
| 52 | * Usage: <for command-line> |
| 53 | * readlink01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 54 | * where, -c n : Run n copies concurrently. |
| 55 | * -f : Turn off functionality Testing. |
| 56 | * -i n : Execute test n times. |
| 57 | * -I x : Execute test for x seconds. |
| 58 | * -P x : Pause for x seconds between iterations. |
| 59 | * -t : Turn on syscall timing. |
| 60 | * |
| 61 | * HISTORY |
| 62 | * 07/2001 Ported by Wayne Boyer |
| 63 | * |
| 64 | * RESTRICTIONS: |
| 65 | * This test should be run by 'non-super-user' only. |
| 66 | * |
| 67 | */ |
| 68 | #include <unistd.h> |
| 69 | #include <fcntl.h> |
| 70 | #include <errno.h> |
| 71 | #include <sys/stat.h> |
robbiew | f44201d | 2001-09-25 16:07:44 +0000 | [diff] [blame] | 72 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
| 76 | #define TESTFILE "testfile" |
| 77 | #define SYMFILE "slink_file" |
| 78 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 79 | #define MAX_SIZE 256 |
| 80 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 81 | char *TCID = "readlink01"; |
| 82 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 84 | int exp_val; /* strlen of testfile */ |
| 85 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 86 | void setup(); |
| 87 | void cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
robbiew | f44201d | 2001-09-25 16:07:44 +0000 | [diff] [blame] | 89 | char nobody_uid[] = "nobody"; |
| 90 | struct passwd *ltpuser; |
| 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | char buffer[MAX_SIZE]; /* temporary buffer to hold symlink contents */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 95 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 96 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 98 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | setup(); |
| 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 104 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 105 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 106 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 107 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | * Call readlink(2) to read the contents of |
| 109 | * symlink into a buffer. |
| 110 | */ |
| 111 | TEST(readlink(SYMFILE, buffer, sizeof(buffer))); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | tst_resm(TFAIL, |
| 115 | "readlink() on %s failed, errno=%d : %s", |
| 116 | SYMFILE, TEST_ERRNO, strerror(TEST_ERRNO)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | continue; |
| 118 | } |
| 119 | |
| 120 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 121 | * Compare the return value of readlink() |
| 122 | * with the expected value which is the |
| 123 | * strlen() of testfile. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 125 | if (TEST_RETURN == exp_val) { |
| 126 | /* Check for the contents of buffer */ |
| 127 | if (memcmp(buffer, TESTFILE, exp_val) != 0) { |
| 128 | tst_resm(TFAIL, "Pathname %s and buffer" |
| 129 | " contents %s differ", |
| 130 | TESTFILE, buffer); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 132 | tst_resm(TPASS, "readlink() " |
| 133 | "functionality on '%s' is " |
| 134 | "correct", SYMFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } |
| 136 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 137 | tst_resm(TFAIL, "readlink() return value %ld " |
| 138 | "does't match, Expected %d", |
| 139 | TEST_RETURN, exp_val); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | } |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 141 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 144 | tst_exit(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 145 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * setup() - performs all ONE TIME setup for this test. |
| 149 | * |
| 150 | * Create a temporary directory and change directory to it. |
| 151 | * Create a test file under temporary directory and close it |
| 152 | * Create a symbolic link of testfile. |
| 153 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 154 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | { |
| 156 | int fd; /* file handle for testfile */ |
| 157 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 158 | tst_require_root(NULL); |
| 159 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | if ((ltpuser = getpwnam(nobody_uid)) == NULL) { |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 161 | tst_brkm(TBROK, cleanup, "getpwname(nobody_uid) failed "); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | } |
| 163 | if (seteuid(ltpuser->pw_uid) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 168 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | TEST_PAUSE; |
| 170 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | tst_tmpdir(); |
| 172 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | tst_brkm(TBROK | TERRNO, cleanup, |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 175 | "open(%s, O_RDWR|O_CREAT, %#o) failed", |
| 176 | TESTFILE, FILE_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if (close(fd) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | tst_resm(TWARN | TERRNO, "close(%s) failed", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* Create a symlink of testfile under temporary directory */ |
| 184 | if (symlink(TESTFILE, SYMFILE) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | tst_brkm(TBROK | TERRNO, cleanup, "symlink(%s, %s) failed", |
| 186 | TESTFILE, SYMFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* Get the strlen of testfile */ |
| 190 | exp_val = strlen(TESTFILE); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 195 | * completion or premature exit. |
| 196 | * |
| 197 | * Remove the test directory and testfile created in the setup. |
| 198 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 199 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | tst_rmdir(); |
| 203 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 204 | } |