]> git.cworth.org Git - apitrace/blobdiff - common/os_posix.cpp
Merge branch 'trace-threads'
[apitrace] / common / os_posix.cpp
index 37dd36431d391eaa90c8ce4df9bc69acea675107..65c5404e3c0e64dd92c2184143474e05fe815b10 100644 (file)
 
 #include <unistd.h>
 #include <sys/time.h>
-#include <pthread.h>
+#include <sys/wait.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <signal.h>
 
+#if defined(__linux__)
+#include <linux/limits.h> // PATH_MAX
+#endif
+
 #ifdef __APPLE__
+#include <sys/syslimits.h> // PATH_MAX
 #include <mach-o/dyld.h>
 #endif
 
+#ifndef PATH_MAX
+#warning PATH_MAX undefined
+#define PATH_MAX 4096
+#endif
+
 #include "os.hpp"
+#include "os_string.hpp"
 
 
 namespace os {
 
 
-static pthread_mutex_t 
-mutex = PTHREAD_MUTEX_INITIALIZER;
-
-
-void
-acquireMutex(void)
-{
-    pthread_mutex_lock(&mutex);
-}
-
-
-void
-releaseMutex(void)
-{
-    pthread_mutex_unlock(&mutex);
-}
-
-
-Path
+String
 getProcessName(void)
 {
-    Path path;
-
-    char *szProcessPath = path.buf(PATH_MAX);
+    String path;
+    size_t size = PATH_MAX;
+    char *buf = path.buf(size);
 
     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
 #ifdef __APPLE__
-    uint32_t len = PATH_MAX;
-    if (_NSGetExecutablePath(szProcessPath, &len) != 0) {
-        *szProcessPath = 0;
+    uint32_t len = size;
+    if (_NSGetExecutablePath(buf, &len) != 0) {
+        *buf = 0;
         return path;
     }
+    len = strlen(buf);
 #else
     ssize_t len;
-    len = readlink("/proc/self/exe", szProcessPath, PATH_MAX - 1);
+    len = readlink("/proc/self/exe", buf, size - 1);
     if (len == -1) {
         // /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, PATH_MAX - 1);
+            len = read(fd, buf, size - 1);
             close(fd);
         }
     }
     if (len <= 0) {
-        snprintf(szProcessPath, PATH_MAX, "%i", (int)getpid());
+        snprintf(buf, size, "%i", (int)getpid());
         return path;
     }
 #endif
@@ -100,18 +94,57 @@ getProcessName(void)
     return path;
 }
 
-Path
+String
 getCurrentDir(void)
 {
-    Path path;
+    String path;
     size_t size = PATH_MAX;
-    char *str = path.buf(size);
-    getcwd(str, size);
-    str[size - 1] = 0;
+    char *buf = path.buf(size);
+
+    getcwd(buf, size);
+    buf[size - 1] = 0;
+    
     path.truncate();
     return path;
 }
 
+bool
+String::exists(void) const
+{
+    struct stat st;
+    int err;
+
+    err = stat(str(), &st);
+    if (err) {
+        return false;
+    }
+
+    if (!S_ISREG(st.st_mode))
+        return false;
+
+    return true;
+}
+
+int execute(char * const * args)
+{
+    pid_t pid = fork();
+    if (pid == 0) {
+        // child
+        execvp(args[0], args);
+        fprintf(stderr, "error: failed to execute %s\n", args[0]);
+        exit(-1);
+    } else {
+        // parent
+        if (pid == -1) {
+            fprintf(stderr, "error: failed to fork\n");
+            return -1;
+        }
+        int status = -1;
+        waitpid(pid, &status, 0);
+        return status;
+    }
+}
+
 void
 log(const char *format, ...)
 {