]> git.cworth.org Git - apitrace/blob - common/os_posix.cpp
os: Ensure len is properly computed on all getProcessName code paths.
[apitrace] / common / os_posix.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010-2011 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <assert.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include <unistd.h>
33 #include <sys/wait.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <signal.h>
37
38 #if defined(__linux__)
39 #include <linux/limits.h> // PATH_MAX
40 #endif
41
42 #ifdef __APPLE__
43 #include <sys/syslimits.h> // PATH_MAX
44 #include <mach-o/dyld.h>
45 #endif
46
47 #ifdef ANDROID
48 #include <android/log.h>
49 #endif
50
51 #ifndef PATH_MAX
52 #warning PATH_MAX undefined
53 #define PATH_MAX 4096
54 #endif
55
56 #include "os.hpp"
57 #include "os_string.hpp"
58
59
60 namespace os {
61
62
63 String
64 getProcessName(void)
65 {
66     String path;
67     size_t size = PATH_MAX;
68     char *buf = path.buf(size);
69
70     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
71 #ifdef __APPLE__
72     uint32_t len = size;
73     if (_NSGetExecutablePath(buf, &len) != 0) {
74         // grow buf and retry
75         buf = path.buf(len);
76         _NSGetExecutablePath(buf, &len);
77     }
78     len = strlen(buf);
79 #else
80     ssize_t len;
81     len = readlink("/proc/self/exe", buf, size - 1);
82     if (len <= 0) {
83         // /proc/self/exe is not available on setuid processes, so fallback to
84         // /proc/self/cmdline.
85         int fd = open("/proc/self/cmdline", O_RDONLY);
86         if (fd >= 0) {
87             // buf already includes trailing zero
88             len = read(fd, buf, size);
89             close(fd);
90             if (len >= 0) {
91                 len = strlen(buf);
92             }
93         }
94     }
95     if (len <= 0) {
96         // fallback to process ID
97         len = snprintf(buf, size, "%i", (int)getpid());
98         if (len >= size) {
99             len = size - 1;
100         }
101     }
102 #endif
103     path.truncate(len);
104
105     return path;
106 }
107
108 String
109 getCurrentDir(void)
110 {
111     String path;
112     size_t size = PATH_MAX;
113     char *buf = path.buf(size);
114
115     getcwd(buf, size);
116     buf[size - 1] = 0;
117     
118     path.truncate();
119     return path;
120 }
121
122 bool
123 createDirectory(const String &path)
124 {
125     return mkdir(path, 0777) == 0;
126 }
127
128 bool
129 String::exists(void) const
130 {
131     struct stat st;
132     int err;
133
134     err = stat(str(), &st);
135     if (err) {
136         return false;
137     }
138
139     return true;
140 }
141
142 int execute(char * const * args)
143 {
144     pid_t pid = fork();
145     if (pid == 0) {
146         // child
147         execvp(args[0], args);
148         fprintf(stderr, "error: failed to execute:");
149         for (unsigned i = 0; args[i]; ++i) {
150             fprintf(stderr, " %s", args[i]);
151         }
152         fprintf(stderr, "\n");
153         exit(-1);
154     } else {
155         // parent
156         if (pid == -1) {
157             fprintf(stderr, "error: failed to fork\n");
158             return -1;
159         }
160         int status = -1;
161         int ret;
162         waitpid(pid, &status, 0);
163         if (WIFEXITED(status)) {
164             ret = WEXITSTATUS(status);
165         } else if (WIFSIGNALED(status)) {
166             // match shell return code
167             ret = WTERMSIG(status) + 128;
168         } else {
169             ret = 128;
170         }
171         return ret;
172     }
173 }
174
175 static volatile bool logging = false;
176
177 void
178 log(const char *format, ...)
179 {
180     logging = true;
181     va_list ap;
182     va_start(ap, format);
183     fflush(stdout);
184 #ifdef ANDROID
185     __android_log_vprint(ANDROID_LOG_DEBUG, "apitrace", format, ap);
186 #else
187     static FILE *log = NULL;
188     if (!log) {
189         // Duplicate stderr file descriptor, to prevent applications from
190         // redirecting our debug messages to somewhere else.
191         //
192         // Another alternative would be to log to /dev/tty when available.
193         log = fdopen(dup(STDERR_FILENO), "at");
194     }
195     vfprintf(log, format, ap);
196     fflush(log);
197 #endif
198     va_end(ap);
199     logging = false;
200 }
201
202 #if defined(__APPLE__)
203 long long timeFrequency = 0LL;
204 #endif
205
206 void
207 abort(void)
208 {
209     _exit(1);
210 }
211
212
213 static void (*gCallback)(void) = NULL;
214
215 #define NUM_SIGNALS 16
216
217 struct sigaction old_actions[NUM_SIGNALS];
218
219
220 /*
221  * See also:
222  * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
223  * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
224  */
225 static void
226 signalHandler(int sig, siginfo_t *info, void *context)
227 {
228     /*
229      * There are several signals that can happen when logging to stdout/stderr.
230      * For example, SIGPIPE will be emitted if stderr is a pipe with no
231      * readers.  Therefore ignore any signal while logging by returning
232      * immediately, to prevent deadlocks.
233      */
234     if (logging) {
235         return;
236     }
237
238     static int recursion_count = 0;
239
240     log("apitrace: warning: caught signal %i\n", sig);
241
242     if (recursion_count) {
243         log("apitrace: warning: recursion handling signal %i\n", sig);
244     } else {
245         if (gCallback) {
246             ++recursion_count;
247             gCallback();
248             --recursion_count;
249         }
250     }
251
252     struct sigaction *old_action;
253     if (sig >= NUM_SIGNALS) {
254         /* This should never happen */
255         log("error: unexpected signal %i\n", sig);
256         raise(SIGKILL);
257     }
258     old_action = &old_actions[sig];
259
260     if (old_action->sa_flags & SA_SIGINFO) {
261         // Handler is in sa_sigaction
262         old_action->sa_sigaction(sig, info, context);
263     } else {
264         if (old_action->sa_handler == SIG_DFL) {
265             log("apitrace: info: taking default action for signal %i\n", sig);
266
267 #if 1
268             struct sigaction dfl_action;
269             dfl_action.sa_handler = SIG_DFL;
270             sigemptyset (&dfl_action.sa_mask);
271             dfl_action.sa_flags = 0;
272             sigaction(sig, &dfl_action, NULL);
273
274             raise(sig);
275 #else
276             raise(SIGKILL);
277 #endif
278         } else if (old_action->sa_handler == SIG_IGN) {
279             /* ignore */
280         } else {
281             /* dispatch to handler */
282             old_action->sa_handler(sig);
283         }
284     }
285 }
286
287 void
288 setExceptionCallback(void (*callback)(void))
289 {
290     assert(!gCallback);
291     if (!gCallback) {
292         gCallback = callback;
293
294         struct sigaction new_action;
295         new_action.sa_sigaction = signalHandler;
296         sigemptyset(&new_action.sa_mask);
297         new_action.sa_flags = SA_SIGINFO | SA_RESTART;
298
299
300         for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
301             // SIGKILL and SIGSTOP can't be handled.
302             if (sig == SIGKILL || sig == SIGSTOP) {
303                 continue;
304             }
305
306             /*
307              * SIGPIPE can be emitted when writing to stderr that is redirected
308              * to a pipe without readers.  It is also very unlikely to ocurr
309              * inside graphics APIs, and most applications where it can occur
310              * normally already ignore it.  In summary, it is unlikely that a
311              * SIGPIPE will cause abnormal termination, which it is likely that
312              * intercepting here will cause problems, so simple don't intercept
313              * it here.
314              */
315             if (sig == SIGPIPE) {
316                 continue;
317             }
318
319             if (sigaction(sig,  NULL, &old_actions[sig]) >= 0) {
320                 sigaction(sig,  &new_action, NULL);
321             }
322         }
323     }
324 }
325
326 void
327 resetExceptionCallback(void)
328 {
329     gCallback = NULL;
330 }
331
332 } /* namespace os */
333