summaryrefslogtreecommitdiffstats
path: root/lib/Utils
diff options
context:
space:
mode:
authorKarel Klic <kklic@redhat.com>2009-12-18 19:12:50 +0100
committerKarel Klic <kklic@redhat.com>2009-12-18 19:12:50 +0100
commite7661d7e411172ddad8838040ded025ad6bfbb14 (patch)
treef2451b553b4fcf959bd2bfc29172f9fb855e5fdd /lib/Utils
parentce1904e24b576a7356488852a240d777717b2598 (diff)
parent46b2fb8df8d4e025f5bbdd9f53be1f658a9e82c6 (diff)
Merge branch 'master' of git://git.fedorahosted.org/git/abrt
Diffstat (limited to 'lib/Utils')
-rw-r--r--lib/Utils/CrashTypesSocket.cpp2
-rw-r--r--lib/Utils/DebugDump.cpp16
-rw-r--r--lib/Utils/DebugDump.h4
-rw-r--r--lib/Utils/Makefile.am2
-rw-r--r--lib/Utils/abrt_xmlrpc.cpp10
-rw-r--r--lib/Utils/abrt_xmlrpc.h5
-rw-r--r--lib/Utils/daemon.cpp139
-rw-r--r--lib/Utils/xatonum.cpp50
8 files changed, 225 insertions, 3 deletions
diff --git a/lib/Utils/CrashTypesSocket.cpp b/lib/Utils/CrashTypesSocket.cpp
index f16e2a9..3525c6a 100644
--- a/lib/Utils/CrashTypesSocket.cpp
+++ b/lib/Utils/CrashTypesSocket.cpp
@@ -114,7 +114,7 @@ static int get_number_from_string(const std::string& pMessage, int& len)
}
}
len = ii + 1;
- return atoi(sNumber.c_str());
+ return xatoi(sNumber.c_str());
}
//TODO: remove constant 4 and place it in a message
diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp
index 765b514..b4c3ee4 100644
--- a/lib/Utils/DebugDump.cpp
+++ b/lib/Utils/DebugDump.cpp
@@ -68,6 +68,22 @@ CDebugDump::CDebugDump() :
m_bLocked(false)
{}
+CDebugDump::~CDebugDump()
+{
+ /* Paranoia. In C++, destructor will abort() if it was called while unwinding
+ * the stack and it throws an exception.
+ */
+ try
+ {
+ Close();
+ m_sDebugDumpDir.clear();
+ }
+ catch (...)
+ {
+ error_msg_and_die("Internal error");
+ }
+}
+
void CDebugDump::Open(const char *pDir)
{
if (m_bOpened)
diff --git a/lib/Utils/DebugDump.h b/lib/Utils/DebugDump.h
index 7dfe61e..c59552e 100644
--- a/lib/Utils/DebugDump.h
+++ b/lib/Utils/DebugDump.h
@@ -41,6 +41,8 @@
#define FILENAME_COMMENT "comment"
#define FILENAME_REPRODUCE "reproduce"
#define FILENAME_RATING "rating"
+#define FILENAME_CMDLINE "cmdline"
+
class CDebugDump
{
@@ -57,7 +59,7 @@ class CDebugDump
public:
CDebugDump();
- ~CDebugDump() { Close(); }
+ ~CDebugDump();
void Open(const char *pDir);
void Create(const char *pDir, int64_t uid);
diff --git a/lib/Utils/Makefile.am b/lib/Utils/Makefile.am
index 9618751..d5e9d98 100644
--- a/lib/Utils/Makefile.am
+++ b/lib/Utils/Makefile.am
@@ -13,7 +13,9 @@ libABRTUtils_la_SOURCES = \
read_write.cpp \
logging.cpp \
copyfd.cpp \
+ daemon.cpp \
skip_whitespace.cpp \
+ xatonum.cpp \
popen_and_save_output.cpp \
stringops.cpp \
dirsize.cpp \
diff --git a/lib/Utils/abrt_xmlrpc.cpp b/lib/Utils/abrt_xmlrpc.cpp
index 7205316..ae1d098 100644
--- a/lib/Utils/abrt_xmlrpc.cpp
+++ b/lib/Utils/abrt_xmlrpc.cpp
@@ -5,6 +5,16 @@
#include "abrt_xmlrpc.h"
#include "ABRTException.h"
+CURL* xcurl_easy_init()
+{
+ CURL* curl = curl_easy_init();
+ if (!curl)
+ {
+ error_msg_and_die("Can't create curl handle");
+ }
+ return curl;
+}
+
void throw_if_xml_fault_occurred(xmlrpc_env *env)
{
if (env->fault_occurred)
diff --git a/lib/Utils/abrt_xmlrpc.h b/lib/Utils/abrt_xmlrpc.h
index e67ab19..352e80c 100644
--- a/lib/Utils/abrt_xmlrpc.h
+++ b/lib/Utils/abrt_xmlrpc.h
@@ -1,6 +1,7 @@
#ifndef ABRT_XMLRPC_H_
#define ABRT_XMLRPC_H_ 1
+#include <curl/curl.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/client.h>
@@ -15,13 +16,15 @@ struct abrt_xmlrpc_conn {
xmlrpc_server_info* m_pServer_info;
abrt_xmlrpc_conn(const char* url, bool no_ssl_verify) { new_xmlrpc_client(url, no_ssl_verify); }
+ /* this never throws exceptions - calls C functions only */
~abrt_xmlrpc_conn() { destroy_xmlrpc_client(); }
void new_xmlrpc_client(const char* url, bool no_ssl_verify);
void destroy_xmlrpc_client();
};
-/* Utility function */
+/* Utility functions */
void throw_if_xml_fault_occurred(xmlrpc_env *env);
+CURL* xcurl_easy_init();
#endif
diff --git a/lib/Utils/daemon.cpp b/lib/Utils/daemon.cpp
new file mode 100644
index 0000000..7aa891c
--- /dev/null
+++ b/lib/Utils/daemon.cpp
@@ -0,0 +1,139 @@
+/*
+ Copyright (C) 2009 RedHat inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+#include "abrtlib.h"
+#define FILENAME_CMDLINE "cmdline"
+#define VAR_RUN_PID_FILE VAR_RUN"/abrt.pid"
+
+static char *append_escaped(char *start, const char *s)
+{
+ char hex_char_buf[] = "\\x00";
+
+ *start++ = ' ';
+ char *dst = start;
+ const unsigned char *p = (unsigned char *)s;
+
+ while (1)
+ {
+ const unsigned char *old_p = p;
+ while (*p > ' ' && *p <= 0x7e && *p != '\"' && *p != '\'' && *p != '\\')
+ p++;
+ if (dst == start)
+ {
+ if (p != (unsigned char *)s && *p == '\0')
+ {
+ /* entire word does not need escaping and quoting */
+ strcpy(dst, s);
+ dst += strlen(s);
+ return dst;
+ }
+ *dst++ = '\'';
+ }
+
+ strncpy(dst, s, (p - old_p));
+ dst += (p - old_p);
+
+ if (*p == '\0')
+ {
+ *dst++ = '\'';
+ *dst = '\0';
+ return dst;
+ }
+ const char *a;
+ switch (*p)
+ {
+ case '\r': a = "\\r"; break;
+ case '\n': a = "\\n"; break;
+ case '\t': a = "\\t"; break;
+ case '\'': a = "\\\'"; break;
+ case '\"': a = "\\\""; break;
+ case '\\': a = "\\\\"; break;
+ case ' ': a = " "; break;
+ default:
+ hex_char_buf[2] = "0123456789abcdef"[*p >> 4];
+ hex_char_buf[3] = "0123456789abcdef"[*p & 0xf];
+ a = hex_char_buf;
+ }
+ strcpy(dst, a);
+ dst += strlen(a);
+ p++;
+ }
+}
+
+// taken from kernel
+#define COMMAND_LINE_SIZE 2048
+char* get_cmdline(pid_t pid)
+{
+ char path[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
+ char cmdline[COMMAND_LINE_SIZE];
+ char escaped_cmdline[COMMAND_LINE_SIZE*4 + 4];
+
+ escaped_cmdline[1] = '\0';
+ sprintf(path, "/proc/%u/cmdline", (int)pid);
+ int fd = open(path, O_RDONLY);
+ if (fd >= 0)
+ {
+ int len = read(fd, cmdline, sizeof(cmdline) - 1);
+ close(fd);
+
+ if (len > 0)
+ {
+ cmdline[len] = '\0';
+ char *src = cmdline;
+ char *dst = escaped_cmdline;
+ while ((src - cmdline) < len)
+ {
+ dst = append_escaped(dst, src);
+ src += strlen(src) + 1;
+ }
+ }
+ }
+
+ return xstrdup(escaped_cmdline + 1); /* +1 skips extraneous leading space */
+}
+
+int daemon_is_ok()
+{
+ int fd = open(VAR_RUN_PID_FILE, O_RDONLY);
+ if (fd < 0)
+ {
+ return 0;
+ }
+
+ char pid[sizeof(pid_t)*3 + 2];
+ int len = read(fd, pid, sizeof(pid)-1);
+ close(fd);
+ if (len <= 0)
+ return 0;
+
+ pid[len] = '\0';
+ *strchrnul(pid, '\n') = '\0';
+ /* paranoia: we don't want to check /proc//stat or /proc///stat */
+ if (pid[0] == '\0' || pid[0] == '/')
+ return 0;
+
+ /* TODO: maybe readlink and check that it is "xxx/abrt"? */
+ char path[sizeof("/proc/%s/stat") + sizeof(pid)];
+ sprintf(path, "/proc/%s/stat", pid);
+ struct stat sb;
+ if (stat(path, &sb) == -1)
+ {
+ return 0;
+ }
+
+ return 1;
+}
diff --git a/lib/Utils/xatonum.cpp b/lib/Utils/xatonum.cpp
new file mode 100644
index 0000000..b096ca8
--- /dev/null
+++ b/lib/Utils/xatonum.cpp
@@ -0,0 +1,50 @@
+/*
+ * Utility routines.
+ *
+ * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
+ *
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ */
+#include "abrtlib.h"
+
+unsigned xatou(const char *numstr)
+{
+ unsigned r;
+ int old_errno;
+ char *e;
+
+ if (*numstr < '0' || *numstr > '9')
+ goto inval;
+
+ old_errno = errno;
+ errno = 0;
+ r = strtoul(numstr, &e, 10);
+ if (errno || numstr == e || *e != '\0')
+ goto inval; /* error / no digits / illegal trailing chars */
+ errno = old_errno; /* Ok. So restore errno. */
+ return r;
+
+ inval:
+ error_msg_and_die("invalid number '%s'", numstr);
+}
+
+int xatoi_u(const char *numstr)
+{
+ unsigned r = xatou(numstr);
+ if (r > (unsigned)INT_MAX)
+ error_msg_and_die("invalid number '%s'", numstr);
+ return r;
+}
+
+int xatoi(const char *numstr)
+{
+ unsigned r;
+
+ if (*numstr != '-')
+ return xatoi_u(numstr);
+
+ r = xatou(numstr + 1);
+ if (r > (unsigned)INT_MAX + 1)
+ error_msg_and_die("invalid number '%s'", numstr);
+ return - (int)r;
+}