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;
151 void (*sig_int)(int);
152 void (*sig_hup)(int);
153 void (*sig_term)(int);
155 void (*handler)(int);
157 static Interrupts interrupts;
159 static void InterruptHandler(int sig)
161 if (interrupts.set && interrupts.handler) {
162 interrupts.handler(sig);
165 if (!interrupts.sig_int)
167 interrupts.sig_int(sig);
168 } else if (sig == SIGHUP) {
169 if (!interrupts.sig_hup)
171 interrupts.sig_hup(sig);
172 } else if (sig == SIGTERM) {
173 if (!interrupts.sig_term)
175 interrupts.sig_term(sig);
180 CatchInterrupts(void (*func)(int))
182 interrupts.handler = func;
184 if (!interrupts.set) {
185 struct sigaction new_action, old_action;
186 new_action.sa_handler = InterruptHandler;
187 sigemptyset(&new_action.sa_mask);
188 new_action.sa_flags = 0;
189 #define SET_IF_NOT_IGNORED(sig, old_handler) \
191 sigaction(sig, NULL, &old_action); \
192 if (old_action.sa_handler != SIG_IGN) { \
193 old_handler = old_action.sa_handler; \
194 sigaction(sig, &new_action, NULL); \
198 SET_IF_NOT_IGNORED(SIGINT, interrupts.sig_int);
199 SET_IF_NOT_IGNORED(SIGHUP, interrupts.sig_hup);
200 SET_IF_NOT_IGNORED(SIGTERM, interrupts.sig_term);
202 interrupts.set = true;
203 #undef SET_IF_NOT_IGNORED