2323#include < cstdarg>
2424
2525#include " Executor.h"
26+ #include " Linker.h"
2627#include " Logger.h"
2728#include " Resolver.h"
2829#include " Session.h"
2930
31+ #if defined HAVE_NSGETENVIRON
32+ #include < crt_externs.h>
33+ #else
34+ #include < unistd.h>
35+
36+ #endif
37+
3038namespace {
3139
3240 size_t va_length (va_list& args)
@@ -42,6 +50,24 @@ namespace {
4250 for (size_t idx = 0 ; idx < argc; ++idx)
4351 argv[idx] = va_arg (args, char *);
4452 }
53+
54+ /* *
55+ * Abstraction to get the current environment.
56+ *
57+ * When the dynamic linker loads the library the `environ` variable
58+ * might not be available. (This is the case for OSX.) This method
59+ * makes it uniform to access the current environment on all platform.
60+ *
61+ * @return the current environment.
62+ */
63+ const char ** environment () noexcept
64+ {
65+ #ifdef HAVE_NSGETENVIRON
66+ return const_cast <const char **>(*_NSGetEnviron ());
67+ #else
68+ return const_cast <const char **>(environ);
69+ #endif
70+ }
4571}
4672
4773/* *
@@ -57,7 +83,7 @@ namespace {
5783 std::atomic<bool > LOADED (false );
5884 // These are related to the functionality of this library.
5985 el::Session SESSION = el::session::init();
60- el::Resolver RESOLVER ;
86+ el::Linker LINKER ;
6187
6288 constexpr el::log::Logger LOGGER (" lib.cc" );
6389}
@@ -74,7 +100,7 @@ extern "C" void on_load()
74100 if (LOADED.exchange (true ))
75101 return ;
76102
77- el::session::from (SESSION, RESOLVER. environment ());
103+ el::session::from (SESSION, environment ());
78104 el::session::persist (SESSION, BUFFER, BUFFER + BUFFER_SIZE);
79105
80106 el::log::Level level = SESSION.verbose ? el::log::VERBOSE : el::log::SILENT;
@@ -102,7 +128,8 @@ extern "C" int execve(const char* path, char* const argv[], char* const envp[])
102128{
103129 LOGGER.debug (" execve path: " , path);
104130
105- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).execve (path, argv, envp);
131+ el::Resolver resolver;
132+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).execve (path, argv, envp);
106133 errno = result.error_code ;
107134 return result.return_value ;
108135}
@@ -111,8 +138,9 @@ extern "C" int execv(const char* path, char* const argv[])
111138{
112139 LOGGER.debug (" execv path: " , path);
113140
114- auto envp = const_cast <char * const *>(RESOLVER.environment ());
115- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).execve (path, argv, envp);
141+ auto envp = const_cast <char * const *>(environment ());
142+ el::Resolver resolver;
143+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).execve (path, argv, envp);
116144 errno = result.error_code ;
117145 return result.return_value ;
118146}
@@ -121,7 +149,8 @@ extern "C" int execvpe(const char* file, char* const argv[], char* const envp[])
121149{
122150 LOGGER.debug (" execvpe file: " , file);
123151
124- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).execvpe (file, argv, envp);
152+ el::Resolver resolver;
153+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).execvpe (file, argv, envp);
125154 errno = result.error_code ;
126155 return result.return_value ;
127156}
@@ -130,8 +159,9 @@ extern "C" int execvp(const char* file, char* const argv[])
130159{
131160 LOGGER.debug (" execvp file: " , file);
132161
133- auto envp = const_cast <char * const *>(RESOLVER.environment ());
134- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).execvpe (file, argv, envp);
162+ auto envp = const_cast <char * const *>(environment ());
163+ el::Resolver resolver;
164+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).execvpe (file, argv, envp);
135165 errno = result.error_code ;
136166 return result.return_value ;
137167}
@@ -140,8 +170,9 @@ extern "C" int execvP(const char* file, const char* search_path, char* const arg
140170{
141171 LOGGER.debug (" execvP file: " , file);
142172
143- auto envp = const_cast <char * const *>(RESOLVER.environment ());
144- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).execvP (file, search_path, argv, envp);
173+ auto envp = const_cast <char * const *>(environment ());
174+ el::Resolver resolver;
175+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).execvP (file, search_path, argv, envp);
145176 errno = result.error_code ;
146177 return result.return_value ;
147178}
@@ -150,7 +181,8 @@ extern "C" int exect(const char* path, char* const argv[], char* const envp[])
150181{
151182 LOGGER.debug (" exect path: " , path);
152183
153- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).execve (path, argv, envp);
184+ el::Resolver resolver;
185+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).execve (path, argv, envp);
154186 errno = result.error_code ;
155187 return result.return_value ;
156188}
@@ -174,8 +206,9 @@ extern "C" int execl(const char* path, const char* arg, ...)
174206 va_copy_n (ap, &argv[1 ], argc + 1 );
175207 va_end (ap);
176208
177- auto envp = const_cast <char * const *>(RESOLVER.environment ());
178- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).execve (path, argv, envp);
209+ auto envp = const_cast <char * const *>(environment ());
210+ el::Resolver resolver;
211+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).execve (path, argv, envp);
179212 errno = result.error_code ;
180213 return result.return_value ;
181214}
@@ -196,8 +229,9 @@ extern "C" int execlp(const char* file, const char* arg, ...)
196229 va_copy_n (ap, &argv[1 ], argc + 1 );
197230 va_end (ap);
198231
199- auto envp = const_cast <char * const *>(RESOLVER.environment ());
200- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).execvpe (file, argv, envp);
232+ auto envp = const_cast <char * const *>(environment ());
233+ el::Resolver resolver;
234+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).execvpe (file, argv, envp);
201235 errno = result.error_code ;
202236 return result.return_value ;
203237}
@@ -220,7 +254,8 @@ extern "C" int execle(const char* path, const char* arg, ...)
220254 char ** envp = va_arg (ap, char **);
221255 va_end (ap);
222256
223- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).execve (path, argv, envp);
257+ el::Resolver resolver;
258+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).execve (path, argv, envp);
224259 errno = result.error_code ;
225260 return result.return_value ;
226261}
@@ -234,7 +269,8 @@ extern "C" int posix_spawn(pid_t* pid, const char* path,
234269{
235270 LOGGER.debug (" posix_spawn path:" , path);
236271
237- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).posix_spawn (pid, path, file_actions, attrp, argv, envp);
272+ el::Resolver resolver;
273+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).posix_spawn (pid, path, file_actions, attrp, argv, envp);
238274 errno = result.error_code ;
239275 return result.return_value ;
240276}
@@ -246,7 +282,8 @@ extern "C" int posix_spawnp(pid_t* pid, const char* file,
246282{
247283 LOGGER.debug (" posix_spawnp file:" , file);
248284
249- const el::Executor::Result result = el::Executor (RESOLVER, SESSION).posix_spawnp (pid, file, file_actions, attrp, argv, envp);
285+ el::Resolver resolver;
286+ const el::Executor::Result result = el::Executor (LINKER, SESSION, resolver).posix_spawnp (pid, file, file_actions, attrp, argv, envp);
250287 errno = result.error_code ;
251288 return result.return_value ;
252289}
0 commit comments