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;
142 CatchInterrupts(void (*func)(int))
144 struct sigaction new_action, old_action;
146 new_action.sa_handler = func;
147 sigemptyset(&new_action.sa_mask);
148 new_action.sa_flags = 0;
150 #define SET_IF_NOT_IGNORED(sig) \
152 sigaction(sig, NULL, &old_action); \
153 if (old_action.sa_handler != SIG_IGN) \
154 sigaction(sig, &new_action, NULL); \
157 SET_IF_NOT_IGNORED(SIGINT);
158 SET_IF_NOT_IGNORED(SIGHUP);
159 SET_IF_NOT_IGNORED(SIGTERM);
161 #undef SET_IF_NOT_IGNORED