]> git.cworth.org Git - apitrace/blob - common/os_posix.cpp
Don't intercept SIGPIPE.
[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/time.h>
34 #include <sys/wait.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <signal.h>
38
39 #if defined(__linux__)
40 #include <linux/limits.h> // PATH_MAX
41 #endif
42
43 #ifdef __APPLE__
44 #include <sys/syslimits.h> // PATH_MAX
45 #include <mach-o/dyld.h>
46 #endif
47
48 #ifndef PATH_MAX
49 #warning PATH_MAX undefined
50 #define PATH_MAX 4096
51 #endif
52
53 #include "os.hpp"
54 #include "os_string.hpp"
55
56
57 namespace os {
58
59
60 String
61 getProcessName(void)
62 {
63     String path;
64     size_t size = PATH_MAX;
65     char *buf = path.buf(size);
66
67     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
68 #ifdef __APPLE__
69     uint32_t len = size;
70     if (_NSGetExecutablePath(buf, &len) != 0) {
71         *buf = 0;
72         return path;
73     }
74     len = strlen(buf);
75 #else
76     ssize_t len;
77     len = readlink("/proc/self/exe", buf, size - 1);
78     if (len == -1) {
79         // /proc/self/exe is not available on setuid processes, so fallback to
80         // /proc/self/cmdline.
81         int fd = open("/proc/self/cmdline", O_RDONLY);
82         if (fd >= 0) {
83             len = read(fd, buf, size - 1);
84             close(fd);
85         }
86     }
87     if (len <= 0) {
88         snprintf(buf, size, "%i", (int)getpid());
89         return path;
90     }
91 #endif
92     path.truncate(len);
93
94     return path;
95 }
96
97 String
98 getCurrentDir(void)
99 {
100     String path;
101     size_t size = PATH_MAX;
102     char *buf = path.buf(size);
103
104     getcwd(buf, size);
105     buf[size - 1] = 0;
106     
107     path.truncate();
108     return path;
109 }
110
111 bool
112 String::exists(void) const
113 {
114     struct stat st;
115     int err;
116
117     err = stat(str(), &st);
118     if (err) {
119         return false;
120     }
121
122     if (!S_ISREG(st.st_mode))
123         return false;
124
125     return true;
126 }
127
128 int execute(char * const * args)
129 {
130     pid_t pid = fork();
131     if (pid == 0) {
132         // child
133         execvp(args[0], args);
134         fprintf(stderr, "error: failed to execute %s\n", args[0]);
135         exit(-1);
136     } else {
137         // parent
138         if (pid == -1) {
139             fprintf(stderr, "error: failed to fork\n");
140             return -1;
141         }
142         int status = -1;
143         waitpid(pid, &status, 0);
144         return status;
145     }
146 }
147
148 static volatile bool logging = false;
149
150 void
151 log(const char *format, ...)
152 {
153     logging = true;
154     va_list ap;
155     va_start(ap, format);
156     fflush(stdout);
157     vfprintf(stderr, format, ap);
158     va_end(ap);
159     logging = false;
160 }
161
162 long long
163 getTime(void)
164 {
165     struct timeval tv;
166     gettimeofday(&tv, NULL);
167     return tv.tv_usec + tv.tv_sec*1000000LL;
168 }
169
170 void
171 abort(void)
172 {
173     exit(0);
174 }
175
176
177 static void (*gCallback)(void) = NULL;
178
179 #define NUM_SIGNALS 16
180
181 struct sigaction old_actions[NUM_SIGNALS];
182
183
184 /*
185  * See also:
186  * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
187  * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
188  */
189 static void
190 signalHandler(int sig, siginfo_t *info, void *context)
191 {
192     /*
193      * There are several signals that can happen when logging to stdout/stderr.
194      * For example, SIGPIPE will be emitted if stderr is a pipe with no
195      * readers.  Therefore ignore any signal while logging by returning
196      * immediately, to prevent deadlocks.
197      */
198     if (logging) {
199         return;
200     }
201
202     static int recursion_count = 0;
203
204     log("apitrace: warning: caught signal %i\n", sig);
205
206     if (recursion_count) {
207         log("apitrace: warning: recursion handling signal %i\n", sig);
208     } else {
209         if (gCallback) {
210             ++recursion_count;
211             gCallback();
212             --recursion_count;
213         }
214     }
215
216     struct sigaction *old_action;
217     if (sig >= NUM_SIGNALS) {
218         /* This should never happen */
219         log("error: unexpected signal %i\n", sig);
220         raise(SIGKILL);
221     }
222     old_action = &old_actions[sig];
223
224     if (old_action->sa_flags & SA_SIGINFO) {
225         // Handler is in sa_sigaction
226         old_action->sa_sigaction(sig, info, context);
227     } else {
228         if (old_action->sa_handler == SIG_DFL) {
229             log("apitrace: info: taking default action for signal %i\n", sig);
230
231 #if 1
232             struct sigaction dfl_action;
233             dfl_action.sa_handler = SIG_DFL;
234             sigemptyset (&dfl_action.sa_mask);
235             dfl_action.sa_flags = 0;
236             sigaction(sig, &dfl_action, NULL);
237
238             raise(sig);
239 #else
240             raise(SIGKILL);
241 #endif
242         } else if (old_action->sa_handler == SIG_IGN) {
243             /* ignore */
244         } else {
245             /* dispatch to handler */
246             old_action->sa_handler(sig);
247         }
248     }
249 }
250
251 void
252 setExceptionCallback(void (*callback)(void))
253 {
254     assert(!gCallback);
255     if (!gCallback) {
256         gCallback = callback;
257
258         struct sigaction new_action;
259         new_action.sa_sigaction = signalHandler;
260         sigemptyset(&new_action.sa_mask);
261         new_action.sa_flags = SA_SIGINFO | SA_RESTART;
262
263
264         for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
265             // SIGKILL and SIGSTOP can't be handled.
266             if (sig == SIGKILL || sig == SIGSTOP) {
267                 continue;
268             }
269
270             /*
271              * SIGPIPE can be emitted when writing to stderr that is redirected
272              * to a pipe without readers.  It is also very unlikely to ocurr
273              * inside graphics APIs, and most applications where it can occur
274              * normally already ignore it.  In summary, it is unlikely that a
275              * SIGPIPE will cause abnormal termination, which it is likely that
276              * intercepting here will cause problems, so simple don't intercept
277              * it here.
278              */
279             if (sig == SIGPIPE) {
280                 continue;
281             }
282
283             if (sigaction(sig,  NULL, &old_actions[sig]) >= 0) {
284                 sigaction(sig,  &new_action, NULL);
285             }
286         }
287     }
288 }
289
290 void
291 resetExceptionCallback(void)
292 {
293     gCallback = NULL;
294 }
295
296 } /* namespace os */
297