#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
ssize_t len;
len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1);
if (len == -1) {
- *str = 0;
- return false;
+ // /proc/self/exe is not available on setuid processes, so fallback to
+ // /proc/self/cmdline.
+ int fd = open("/proc/self/cmdline", O_RDONLY);
+ if (fd >= 0) {
+ len = read(fd, szProcessPath, sizeof(szProcessPath) - 1);
+ close(fd);
+ }
+ }
+ if (len <= 0) {
+ snprintf(str, size, "%i", (int)getpid());
+ return true;
}
#endif
szProcessPath[len] = 0;