source: trunk/base/src/darwintracelib1.0/stat.c @ 136621

Last change on this file since 136621 was 117962, checked in by cal@…, 6 years ago

darwintrace: don't export symbols

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1/*
2 * Copyright (c) 2005 Apple Inc. All rights reserved.
3 * Copyright (c) 2005-2006 Paul Guyot <pguyot@kallisys.net>,
4 * All rights reserved.
5 * Copyright (c) 2006-2013 The MacPorts Project
6 *
7 * $Id: darwintrace.c 112642 2013-10-28 18:59:19Z cal@macports.org $
8 *
9 * @APPLE_BSD_LICENSE_HEADER_START@
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1.  Redistributions of source code must retain the above copyright
16 *     notice, this list of conditions and the following disclaimer.
17 * 2.  Redistributions in binary form must reproduce the above copyright
18 *     notice, this list of conditions and the following disclaimer in the
19 *     documentation and/or other materials provided with the distribution.
20 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
21 *     its contributors may be used to endorse or promote products derived
22 *     from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * @APPLE_BSD_LICENSE_HEADER_END@
36 */
37
38#include "darwintrace.h"
39
40#include <errno.h>
41#include <sys/syscall.h>
42#include <unistd.h>
43
44// Do *not* include sys/stat.h, it will rewrite the stat to a stat$INODE64 symbol
45
46/**
47 * Wrapper around \c stat(2) to hide information about files outside the
48 * sandbox.
49 */
50static int _dt_stat(const char *path, void *sb) {
51#define stat(path, sb) syscall(SYS_stat, path, sb)
52        __darwintrace_setup();
53
54        int result = 0;
55
56        if (!__darwintrace_is_in_sandbox(path, DT_REPORT | DT_ALLOWDIR | DT_FOLLOWSYMS)) {
57                errno = ENOENT;
58                result = -1;
59        } else {
60                result = stat(path, sb);
61        }
62
63        debug_printf("stat(%s) = %d\n", path, result);
64
65        return result;
66#undef stat
67}
68
69// We don't include sys/stat.h because it would rewrite all stat function
70// calls, but we need the declaration of stat here.
71int stat(const char *path, void *sb);
72DARWINTRACE_INTERPOSE(_dt_stat, stat);
73
74// Don't provide stat64 on systems that have no stat64 syscall
75#ifdef SYS_stat64
76static int _dt_stat64(const char *path, void *sb) {
77#define stat64(path, sb) syscall(SYS_stat64, path, sb)
78        __darwintrace_setup();
79
80        int result = 0;
81
82        if (!__darwintrace_is_in_sandbox(path, DT_REPORT | DT_ALLOWDIR | DT_FOLLOWSYMS)) {
83                errno = ENOENT;
84                result = -1;
85        } else {
86                result = stat64(path, sb);
87        }
88
89        debug_printf("stat64(%s) = %d\n", path, result);
90
91        return result;
92#undef stat64
93}
94int stat64(const char *path, void *sb);
95DARWINTRACE_INTERPOSE(_dt_stat64, stat64);
96
97int stat$INODE64(const char *path, void *sb);
98DARWINTRACE_INTERPOSE(_dt_stat64, stat$INODE64);
99
100#endif /* defined(SYS_stat64) */
101
102static int _dt_lstat(const char *path, void *sb) {
103#define lstat(path, sb) syscall(SYS_lstat, path, sb)
104        __darwintrace_setup();
105
106        int result = 0;
107
108        // don't follow symlinks for lstat
109        if (!__darwintrace_is_in_sandbox(path, DT_REPORT | DT_ALLOWDIR)) {
110                errno = ENOENT;
111                result = -1;
112        } else {
113                result = lstat(path, sb);
114        }
115
116        debug_printf("lstat(%s) = %d\n", path, result);
117
118        return result;
119#undef lstat
120}
121
122int lstat(const char *path, void *sb);
123DARWINTRACE_INTERPOSE(_dt_lstat, lstat);
124
125// Don't provide lstat64 on systems that have no lstat64 syscall
126#ifdef SYS_lstat64
127static int _dt_lstat64(const char *path, void *sb) {
128#define lstat64(path, sb) syscall(SYS_lstat64, path, sb)
129        __darwintrace_setup();
130
131        int result = 0;
132
133        // don't follow symlinks for lstat
134        if (!__darwintrace_is_in_sandbox(path, DT_REPORT | DT_ALLOWDIR)) {
135                errno = ENOENT;
136                result = -1;
137        } else {
138                result = lstat64(path, sb);
139        }
140
141        debug_printf("lstat64(%s) = %d\n", path, result);
142
143        return result;
144#undef lstat64
145}
146
147int lstat64(const char *path, void *sb);
148DARWINTRACE_INTERPOSE(_dt_lstat64, lstat64);
149
150int lstat$INODE64(const char *path, void *sb);
151DARWINTRACE_INTERPOSE(_dt_lstat64, lstat$INODE64);
152
153#endif /* defined(SYS_lstat64) */
Note: See TracBrowser for help on using the repository browser.