summaryrefslogtreecommitdiffstats
path: root/util.cxx
diff options
context:
space:
mode:
authorDavid Smith <dsmith@redhat.com>2009-05-21 16:57:04 -0500
committerDavid Smith <dsmith@redhat.com>2009-05-21 16:57:04 -0500
commitc8e9eb18d8d13d099a4a177fe53de507c1d9ce8b (patch)
treeab2388afb795ed1a7ead2fbbf8b9d1b368a8231f /util.cxx
parentdd9a3bcbef65bde65491d959e9458bc641924811 (diff)
parent3863e7999255deeaa7f8f4bba7df893773812537 (diff)
downloadsystemtap-steved-c8e9eb18d8d13d099a4a177fe53de507c1d9ce8b.tar.gz
systemtap-steved-c8e9eb18d8d13d099a4a177fe53de507c1d9ce8b.tar.xz
systemtap-steved-c8e9eb18d8d13d099a4a177fe53de507c1d9ce8b.zip
Merge commit 'origin/master' into pr7043
Conflicts: runtime/print.c runtime/transport/transport.c runtime/transport/transport_msgs.h
Diffstat (limited to 'util.cxx')
-rw-r--r--util.cxx89
1 files changed, 84 insertions, 5 deletions
diff --git a/util.cxx b/util.cxx
index 7d191cd2..5c05a1dd 100644
--- a/util.cxx
+++ b/util.cxx
@@ -1,5 +1,5 @@
// Copyright (C) Andrew Tridgell 2002 (original file)
-// Copyright (C) 2006 Red Hat Inc. (systemtap changes)
+// Copyright (C) 2006, 2009 Red Hat Inc. (systemtap changes)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
@@ -20,13 +20,15 @@
#include <cerrno>
extern "C" {
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <fcntl.h>
#include <pwd.h>
-#include <unistd.h>
+#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
-#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
}
using namespace std;
@@ -133,6 +135,25 @@ create_dir(const char *dir)
return 0;
}
+// Remove a file or directory
+int
+remove_file_or_dir (const char *name)
+{
+ int rc;
+ struct stat st;
+
+ if ((rc = stat(name, &st)) != 0)
+ {
+ if (errno == ENOENT)
+ return 0;
+ return 1;
+ }
+
+ if (remove (name) != 0)
+ return 1;
+ cerr << "remove returned 0" << endl;
+ return 0;
+}
void
tokenize(const string& str, vector<string>& tokens,
@@ -248,4 +269,62 @@ const string cmdstr_quoted(const string& cmd)
return quoted_cmd;
}
+
+string
+git_revision(const string& path)
+{
+ string revision = "(not-a-git-repository)";
+ string git_dir = path + "/.git/";
+
+ struct stat st;
+ if (stat(git_dir.c_str(), &st) == 0)
+ {
+ string command = "git --git-dir=\"" + git_dir
+ + "\" rev-parse HEAD 2>/dev/null";
+
+ char buf[50];
+ FILE *fp = popen(command.c_str(), "r");
+ if (fp != NULL)
+ {
+ char *bufp = fgets(buf, sizeof(buf), fp);
+ int rc = pclose(fp);
+ if (bufp != NULL && rc == 0)
+ revision = buf;
+ }
+ }
+
+ return revision;
+}
+
+
+static pid_t spawned_pid = 0;
+
+// Runs a command with a saved PID, so we can kill it from the signal handler
+int
+stap_system(const char *command)
+{
+ const char * argv[] = { "sh", "-c", command, NULL };
+ int ret, status;
+
+ spawned_pid = 0;
+ ret = posix_spawn(&spawned_pid, "/bin/sh", NULL, NULL,
+ const_cast<char **>(argv), environ);
+ if (ret == 0)
+ {
+ if (waitpid(spawned_pid, &status, 0) == spawned_pid)
+ ret = WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status);
+ else
+ ret = errno;
+ }
+ spawned_pid = 0;
+ return ret;
+}
+
+// Send a signal to our spawned command
+int
+kill_stap_spawn(int sig)
+{
+ return spawned_pid ? kill(spawned_pid, sig) : 0;
+}
+
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */