1 /* Copyright 2021, Google Inc. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following disclaimer
11 * in the documentation and/or other materials provided with the
12 * distribution.
13 * * Neither the name of Google Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "test_skel.h"
31
main(int argc,char * argv[])32 int main(int argc, char *argv[]) {
33 int exit_status = 0;
34
35 // Get two unique paths to play with.
36 char foo[] = "tempfile.XXXXXX";
37 char bar[] = "tempfile.XXXXXX";
38 int fd_foo = mkstemp(foo);
39 int fd_bar = mkstemp(bar);
40 assert(fd_foo != -1);
41 assert(fd_bar != -1);
42
43 // Then delete foo.
44 assert(sys_unlink(foo) == 0);
45
46 // Now make foo a symlink to bar.
47 assert(symlink(bar, foo) == 0);
48
49 // Make sure sys_stat() and sys_lstat() implementation return different
50 // information.
51
52 // We need to check our stat syscalls for EOVERFLOW, as sometimes the integer
53 // types used in the stat structures are too small to fit the actual value.
54 // E.g. on some systems st_ino is 32-bit, but some filesystems have 64-bit
55 // inodes.
56
57 struct kernel_stat lstat_info;
58 int rc = sys_lstat(foo, &lstat_info);
59 if (rc < 0 && errno == EOVERFLOW) {
60 // Bail out since we had an overflow in the stat structure.
61 exit_status = SKIP_TEST_EXIT_STATUS;
62 goto cleanup;
63 }
64 assert(rc == 0);
65
66 struct kernel_stat stat_info;
67 rc = sys_stat(foo, &stat_info);
68 if (rc < 0 && errno == EOVERFLOW) {
69 // Bail out since we had an overflow in the stat structure.
70 exit_status = SKIP_TEST_EXIT_STATUS;
71 goto cleanup;
72 }
73 assert(rc == 0);
74
75 struct kernel_stat bar_stat_info;
76 rc = sys_stat(bar, &bar_stat_info);
77 if (rc < 0 && errno == EOVERFLOW) {
78 // Bail out since we had an overflow in the stat structure.
79 exit_status = SKIP_TEST_EXIT_STATUS;
80 goto cleanup;
81 }
82 assert(rc == 0);
83
84 // lstat should produce information about a symlink.
85 assert((lstat_info.st_mode & S_IFMT) == S_IFLNK);
86
87 // stat-ing foo and bar should produce the same inode.
88 assert(stat_info.st_ino == bar_stat_info.st_ino);
89
90 // lstat-ing foo should give a different inode than stat-ing foo.
91 assert(stat_info.st_ino != lstat_info.st_ino);
92
93 cleanup:
94 sys_unlink(foo);
95 sys_unlink(bar);
96 return exit_status;
97 }
98