-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.h
181 lines (161 loc) · 5.72 KB
/
common.h
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/syslimits.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/clonefile.h>
#include <sys/attr.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/xattr.h>
#include <stdint.h>
#include <spawn.h>
#include <dlfcn.h>
#include <pthread.h>
#include <mach-o/dyld.h>
/**
* @file common.h
* @author Karim Vergnes <[email protected]>
* @copyright GPLv2
* @brief Common headers to the fakedir library.
*
* This header file imports original definitions for all replaced system calls
* and defines the primary functions.
*/
/**
* @brief Flag to enable debug printing. Set by FAKEDIR_DEBUG env var.
* @see DEBUG Debug printing macro which uses this variable
*/
extern bool isdebug;
/**
* @brief Strictly locks syscall rewrite operations.
*/
extern pthread_mutex_t _lock;
/**
* @brief Dynamically loaded path to ourselves, for preservation across exec
* @see execve_patch_envp uses this variable
*/
extern const char *ownpath;
/**
* @brief Value of FAKEDIR_PATTERN, the fake directory.
*/
extern const char *pattern;
/**
* @brief Value of FAKEDIR_TARGET, the real directory to rewrite pattern to.
*/
extern const char *target;
/**
* @brief Special constant to tell pspawn_patch_envp() to use execve()
* instead.
*/
#define PSP_EXEC -15
#ifndef STRIP_DEBUG
/**
* @brief File descriptor to output debug messages to. Useful when stderr
* isn't guaranteed.
*/
extern int debugfd;
/**
* @brief Prints to stderr if FAKEDIR_DEBUG is set.
* @param p Format string for @ref printf
* @param args Additional arguments for @ref printf
*/
# define DEBUG(p, args...) \
if (isdebug) dprintf(debugfd, "[fakedir] " p "\n", ## args)
#else
# define DEBUG(p, args...) ;
#endif
/**
* This function parses a Mach-O binary, and calls a function for each library
* found that needs rewriting.
*
* @param path The path to the binary to parse
* @param step The handler to call on match
*/
void macho_add_dependencies(char const *path, void (*step)(char const *d));
/**
* This function compares the start of a path string with FAKEDIR_PATTERN.
* If there is a match, the rest of the path is appended to the path buffer,
* functionally replacing FAKEDIR_PATTERN with FAKEDIR_TARGET in the path.
*
* @brief Rewrites start of path according to env vars.
* @param path The path to be rewritten
* @return A string representing the rewritten path.
*/
char const *rewrite_path(char const *path);
/**
* This function performs the reverse operation to @ref rewrite_path, that is
* it replaces FAKEDIR_TARGET with FAKEDIR_PATTERN at the start of the given
* path.
*
* @brief Performs inverse of rewrite_path
* @param path The path to be rewritten
* @return A string represneting the rewritten path.
*/
char const *rewrite_path_rev(char const *path);
/**
* This function resolves all symbolic links through the given path down
* to the last parent directory. This is useful for open() with O_SYMLINK,
* so that the last symbolic link is preserved.
*
* @brief Resolve symbolic links in path down to parent directory.
* @param fd The file descriptor to pass back to @ref resolve_symlink_at,
* or -1 to use @ref resolve_symlink.
* @param path The path to perform resolution on
* @return A string representing the resolved path.
* @see Mutually recursive with @ref resolve_symlink and @ref resolve_symlink_at
*/
char const *resolve_symlink_parent(int fd, char const *path);
/**
* This function performs a recursive resolution of all symbolic links
* through the given path, including the final file if it is a symbolic link.
* This is used by open() without O_SYMLINK, O_NOFOLLOW or O_NOFOLLOW_ANY,
* as well as l-variants of certain calls (like lchown, lstat)
*
* @brief Resolve all symbolic links in path, taking fakedir into account.
* @param path The path to perform resolution on
* @return A string representing the resolved path
* @see Mutually recursive with @ref resolve_symlink_parent
*/
#define resolve_symlink(path) resolve_symlink_at(-1, path)
/**
* This function is an alternative entry point into the recursive symbolic
* link resolution system, for use with -at-variants of certain syscalls.
*
* @brief Like resolve_symlink, but with file descriptor as current dir.
* @param fd The file descriptor representing the current directory.
* @param path The path to perform resolution on.
* @return A string representing the resolved path
* @see Functionally equivalent to @ref resolve_symlink
* @see Mutually recursive with @ref resolve_symlink_parent
*/
char const *resolve_symlink_at(int fd, char const *path);
/**
* This function performs a limited length match against msg. If the contents
* of msg match those of pat until the end of pat, the function returns true.
*
* @brief Check if a string starts with another string.
* @param pat The pattern to use for comparison count.
* @param msg The string to check against.
* @return True if the start of msg is the contents of pat, False otherwise.
*/
bool startswith(char const *pat, char const *msg);
/**
* This function performs a limited length match against the end of msg. If the
* contents of the end of msg match those of pat until the end of both, the
* function returns true.
*
* @brief Check if a string ends with another string.
* @param pat The pattern to use for comparison count and offset.
* @param msg The string to check against.
* @return True if the end of msg is the contents of pat, False otherwise.
*/
bool endswith(char const *pattern, char const *msg);
// vim: ft=c.doxygen