blob: a2a42b0d9e41808aa60b1eb5a4f9a9e2223a43d5 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis64985a82014-06-05 12:44:04 +02002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis64985a82014-06-05 12:44:04 +02005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubis64985a82014-06-05 12:44:04 +020010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubis64985a82014-06-05 12:44:04 +020015 * 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
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name: lstat03
plars865695b2001-08-27 22:15:12 +000022 *
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.
plars865695b2001-08-27 22:15:12 +000030 */
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>
robbiewda2f8ab2001-09-12 16:59:47 +000038#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000039
40#include "test.h"
Cyril Hrubis64985a82014-06-05 12:44:04 +020041#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000042
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 Hrubisfdce7d52013-04-04 18:35:48 +020050char *TCID = "lstat03";
51int TST_TOTAL = 1;
Cyril Hrubis64985a82014-06-05 12:44:04 +020052static uid_t user_id;
53static gid_t group_id;
plars865695b2001-08-27 22:15:12 +000054
Cyril Hrubis64985a82014-06-05 12:44:04 +020055static char nobody_uid[] = "nobody";
56static struct passwd *ltpuser;
robbiewda2f8ab2001-09-12 16:59:47 +000057
Cyril Hrubis64985a82014-06-05 12:44:04 +020058static void setup(void);
59static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000060
subrata_modak56207ce2009-03-23 13:35:39 +000061int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000062{
Cyril Hrubis64985a82014-06-05 12:44:04 +020063 struct stat stat_buf;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020064 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020065 const char *msg;
plars865695b2001-08-27 22:15:12 +000066
Garrett Cooper45e285d2010-11-22 12:19:25 -080067 msg = parse_opts(ac, av, NULL, NULL);
Cyril Hrubis64985a82014-06-05 12:44:04 +020068 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +000069 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080070
plars865695b2001-08-27 22:15:12 +000071 setup();
72
plars865695b2001-08-27 22:15:12 +000073 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080074 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +000075
plars865695b2001-08-27 22:15:12 +000076 TEST(lstat(SFILE, &stat_buf));
subrata_modakbdbaec52009-02-26 12:14:51 +000077
plars865695b2001-08-27 22:15:12 +000078 if (TEST_RETURN == -1) {
Cyril Hrubis64985a82014-06-05 12:44:04 +020079 tst_resm(TFAIL | TTERRNO, "lstat(%s) failed", SFILE);
plars865695b2001-08-27 22:15:12 +000080 continue;
81 }
Cyril Hrubis64985a82014-06-05 12:44:04 +020082
Cyril Hrubise38b9612014-06-02 17:20:57 +020083 if ((stat_buf.st_uid != user_id) ||
84 (stat_buf.st_gid != group_id) ||
Nicolas Joly88c5aa72014-06-05 08:33:48 +020085 ((stat_buf.st_mode & S_IFMT) != S_IFLNK) ||
Cyril Hrubise38b9612014-06-02 17:20:57 +020086 (stat_buf.st_size != strlen(TESTFILE))) {
87 tst_resm(TFAIL, "Functionality of lstat(2) on "
88 "'%s' Failed", SFILE);
plars865695b2001-08-27 22:15:12 +000089 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +020090 tst_resm(TPASS, "Functionality of lstat(2) on "
91 "'%s' Succcessful", SFILE);
plars865695b2001-08-27 22:15:12 +000092 }
Garrett Cooper2c282152010-12-16 00:55:50 -080093 }
plars865695b2001-08-27 22:15:12 +000094
plars865695b2001-08-27 22:15:12 +000095 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080096 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -080097}
plars865695b2001-08-27 22:15:12 +000098
Cyril Hrubis64985a82014-06-05 12:44:04 +020099static void setup(void)
plars865695b2001-08-27 22:15:12 +0000100{
plars865695b2001-08-27 22:15:12 +0000101 tst_sig(NOFORK, DEF_HANDLER, cleanup);
102
Cyril Hrubis64985a82014-06-05 12:44:04 +0200103 tst_require_root(NULL);
104
105 ltpuser = SAFE_GETPWNAM(NULL, nobody_uid);
106 SAFE_SETUID(NULL, ltpuser->pw_uid);
plars865695b2001-08-27 22:15:12 +0000107
plars865695b2001-08-27 22:15:12 +0000108 TEST_PAUSE;
109
plars865695b2001-08-27 22:15:12 +0000110 tst_tmpdir();
111
Cyril Hrubis64985a82014-06-05 12:44:04 +0200112 if (tst_fill_file(TESTFILE, 'a', 1024, 1))
113 tst_brkm(TBROK, cleanup, "Failed to create " TESTFILE);
plars865695b2001-08-27 22:15:12 +0000114
Cyril Hrubis64985a82014-06-05 12:44:04 +0200115 SAFE_SYMLINK(cleanup, TESTFILE, SFILE);
subrata_modakbdbaec52009-02-26 12:14:51 +0000116
Garrett Cooper53740502010-12-16 00:04:01 -0800117 user_id = getuid();
118 group_id = getgid();
plars865695b2001-08-27 22:15:12 +0000119}
120
Cyril Hrubis64985a82014-06-05 12:44:04 +0200121static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000122{
plars865695b2001-08-27 22:15:12 +0000123 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700124}