]> git.cworth.org Git - apitrace/blobdiff - common/os_posix.cpp
Abstract execv().
[apitrace] / common / os_posix.cpp
index b16d1bdf1b1fff09ba6e31ae91f9e1382eabead5..da7878311a72f970a309fd11a3f3c5cf90fddc58 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <unistd.h>
 #include <sys/time.h>
+#include <sys/wait.h>
 #include <pthread.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -89,6 +90,7 @@ getProcessName(void)
         *buf = 0;
         return path;
     }
+    len = strlen(buf);
 #else
     ssize_t len;
     len = readlink("/proc/self/exe", buf, size - 1);
@@ -125,6 +127,43 @@ getCurrentDir(void)
     return path;
 }
 
+bool
+Path::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, ...)
 {