plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 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 Foundation, |
| 17 | * 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 | /* |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 21 | * Test Name: lstat03 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, lstat(2) succeeds to get the status of a file pointed to by |
| 25 | * symlink and fills the stat structure elements. |
| 26 | * |
| 27 | * Expected Result: |
| 28 | * lstat() should return value 0 on success and the stat structure elements |
| 29 | * should be filled with the symlink file information. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | */ |
| 31 | #include <stdio.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/fcntl.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <errno.h> |
| 36 | #include <string.h> |
| 37 | #include <signal.h> |
robbiew | da2f8ab | 2001-09-12 16:59:47 +0000 | [diff] [blame] | 38 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | |
| 40 | #include "test.h" |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 41 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
| 43 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 44 | #define TESTFILE "testfile" |
| 45 | #define SFILE "sfile" |
| 46 | #define FILE_SIZE 1024 |
| 47 | #define BUF_SIZE 256 |
| 48 | #define PERMS 0644 |
| 49 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 50 | char *TCID = "lstat03"; |
| 51 | int TST_TOTAL = 1; |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 52 | static uid_t user_id; |
| 53 | static gid_t group_id; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 55 | static char nobody_uid[] = "nobody"; |
| 56 | static struct passwd *ltpuser; |
robbiew | da2f8ab | 2001-09-12 16:59:47 +0000 | [diff] [blame] | 57 | |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 58 | static void setup(void); |
| 59 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | { |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 63 | struct stat stat_buf; |
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; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 67 | msg = parse_opts(ac, av, NULL, NULL); |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 68 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 70 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | setup(); |
| 72 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 74 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 75 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | TEST(lstat(SFILE, &stat_buf)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 77 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | if (TEST_RETURN == -1) { |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 79 | tst_resm(TFAIL | TTERRNO, "lstat(%s) failed", SFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | continue; |
| 81 | } |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 82 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 83 | if ((stat_buf.st_uid != user_id) || |
| 84 | (stat_buf.st_gid != group_id) || |
Nicolas Joly | 88c5aa7 | 2014-06-05 08:33:48 +0200 | [diff] [blame] | 85 | ((stat_buf.st_mode & S_IFMT) != S_IFLNK) || |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 86 | (stat_buf.st_size != strlen(TESTFILE))) { |
| 87 | tst_resm(TFAIL, "Functionality of lstat(2) on " |
| 88 | "'%s' Failed", SFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 90 | tst_resm(TPASS, "Functionality of lstat(2) on " |
| 91 | "'%s' Succcessful", SFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 93 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 96 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 97 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 99 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 102 | |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 103 | tst_require_root(NULL); |
| 104 | |
| 105 | ltpuser = SAFE_GETPWNAM(NULL, nobody_uid); |
| 106 | SAFE_SETUID(NULL, ltpuser->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | TEST_PAUSE; |
| 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | tst_tmpdir(); |
| 111 | |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 112 | if (tst_fill_file(TESTFILE, 'a', 1024, 1)) |
| 113 | tst_brkm(TBROK, cleanup, "Failed to create " TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 115 | SAFE_SYMLINK(cleanup, TESTFILE, SFILE); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 116 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 117 | user_id = getuid(); |
| 118 | group_id = getgid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Cyril Hrubis | 64985a8 | 2014-06-05 12:44:04 +0200 | [diff] [blame] | 121 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 124 | } |