1 /**************************************************************************
3 * Copyright 2010-2011 VMware, Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
39 #include <mach-o/dyld.h>
48 static pthread_mutex_t
49 mutex = PTHREAD_MUTEX_INITIALIZER;
55 pthread_mutex_lock(&mutex);
62 pthread_mutex_unlock(&mutex);
67 GetProcessName(char *str, size_t size)
69 char szProcessPath[PATH_MAX + 1];
72 // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
74 uint32_t len = sizeof szProcessPath;
75 if (_NSGetExecutablePath(szProcessPath, &len) != 0) {
81 len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1);
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);
87 len = read(fd, szProcessPath, sizeof(szProcessPath) - 1);
92 snprintf(str, size, "%i", (int)getpid());
96 szProcessPath[len] = 0;
98 lpProcessName = strrchr(szProcessPath, '/');
99 lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
101 strncpy(str, lpProcessName, size);
109 GetCurrentDir(char *str, size_t size)
112 ret = getcwd(str, size);
114 return ret ? true : false;
118 DebugMessage(const char *format, ...)
121 va_start(ap, format);
123 vfprintf(stderr, format, ap);
127 long long GetTime(void)
130 gettimeofday(&tv, NULL);
131 return tv.tv_usec + tv.tv_sec*1000000LL;
153 void (*sig_int)(int);
154 void (*sig_hup)(int);
155 void (*sig_term)(int);
156 void (*sig_segv)(int);
158 void (*handler)(int);
160 static Interrupts interrupts;
162 static void InterruptHandler(int sig)
164 if (interrupts.set && interrupts.handler) {
165 interrupts.handler(sig);
168 if (!interrupts.sig_int) {
171 interrupts.sig_int(sig);
172 } else if (sig == SIGHUP) {
173 if (!interrupts.sig_hup) {
176 interrupts.sig_hup(sig);
177 } else if (sig == SIGTERM) {
178 if (!interrupts.sig_term) {
181 interrupts.sig_term(sig);
182 } else if (sig == SIGSEGV) {
183 if (interrupts.sig_segv) {
184 interrupts.sig_segv(sig);
190 CatchInterrupts(void (*func)(int))
192 interrupts.handler = func;
194 if (!interrupts.set) {
195 struct sigaction new_action, old_action;
196 new_action.sa_handler = InterruptHandler;
197 sigemptyset(&new_action.sa_mask);
198 new_action.sa_flags = 0;
199 #define SET_IF_NOT_IGNORED(sig, old_handler) \
201 sigaction(sig, NULL, &old_action); \
202 if (old_action.sa_handler != SIG_IGN) { \
203 old_handler = old_action.sa_handler; \
204 sigaction(sig, &new_action, NULL); \
208 SET_IF_NOT_IGNORED(SIGINT, interrupts.sig_int);
209 SET_IF_NOT_IGNORED(SIGHUP, interrupts.sig_hup);
210 SET_IF_NOT_IGNORED(SIGTERM, interrupts.sig_term);
211 SET_IF_NOT_IGNORED(SIGSEGV, interrupts.sig_segv);
213 interrupts.set = true;
214 #undef SET_IF_NOT_IGNORED