tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (c) International Business Machines  Corp., 2001
 *  07/2001 Ported by Wayne Boyer
 *
 * This program is free software;  you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY;  without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program;  if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
 
/*
 * Test Name: lstat03
 *
 * Test Description:
 *  Verify that, lstat(2) succeeds to get the status of a file pointed to by
 *  symlink and fills the stat structure elements.
 *
 * Expected Result:
 *  lstat() should return value 0 on success and the stat structure elements
 *  should be filled with the symlink file information.
 */
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <pwd.h>
 
#include "test.h"
#include "safe_macros.h"
 
#define FILE_MODE    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
#define TESTFILE    "testfile"
#define SFILE        "sfile"
#define FILE_SIZE       1024
#define BUF_SIZE    256
#define PERMS        0644
 
char *TCID = "lstat03";
int TST_TOTAL = 1;
static uid_t user_id;
static gid_t group_id;
 
static char nobody_uid[] = "nobody";
static struct passwd *ltpuser;
 
static void setup(void);
static void cleanup(void);
 
int main(int ac, char **av)
{
   struct stat stat_buf;
   int lc;
 
   tst_parse_opts(ac, av, NULL, NULL);
 
   setup();
 
   for (lc = 0; TEST_LOOPING(lc); lc++) {
       tst_count = 0;
 
       TEST(lstat(SFILE, &stat_buf));
 
       if (TEST_RETURN == -1) {
           tst_resm(TFAIL | TTERRNO, "lstat(%s) failed", SFILE);
           continue;
       }
 
       if ((stat_buf.st_uid != user_id) ||
           (stat_buf.st_gid != group_id) ||
           ((stat_buf.st_mode & S_IFMT) != S_IFLNK) ||
           (stat_buf.st_size != strlen(TESTFILE))) {
           tst_resm(TFAIL, "Functionality of lstat(2) on "
                "'%s' Failed", SFILE);
       } else {
           tst_resm(TPASS, "Functionality of lstat(2) on "
                "'%s' Succcessful", SFILE);
       }
   }
 
   cleanup();
   tst_exit();
}
 
static void setup(void)
{
   tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
   tst_require_root();
 
   ltpuser = SAFE_GETPWNAM(NULL, nobody_uid);
   SAFE_SETUID(NULL, ltpuser->pw_uid);
 
   TEST_PAUSE;
 
   tst_tmpdir();
 
   if (tst_fill_file(TESTFILE, 'a', 1024, 1))
       tst_brkm(TBROK, cleanup, "Failed to create " TESTFILE);
 
   SAFE_SYMLINK(cleanup, TESTFILE, SFILE);
 
   user_id = getuid();
   group_id = getgid();
}
 
static void cleanup(void)
{
   tst_rmdir();
}