summaryrefslogtreecommitdiffstats
path: root/lib/Utils
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Utils')
-rw-r--r--lib/Utils/Action.h6
-rw-r--r--lib/Utils/Analyzer.h6
-rw-r--r--lib/Utils/CommLayerInner.cpp57
-rw-r--r--lib/Utils/CommLayerInner.h16
-rw-r--r--lib/Utils/DBusCommon.h6
-rw-r--r--lib/Utils/DebugDump.cpp280
-rw-r--r--lib/Utils/DebugDump.h16
-rw-r--r--lib/Utils/Makefile.am9
-rw-r--r--lib/Utils/Observer.h4
-rw-r--r--lib/Utils/Plugin.cpp13
-rw-r--r--lib/Utils/Plugin.h27
-rw-r--r--lib/Utils/Reporter.h1
-rw-r--r--lib/Utils/copyfd.cpp22
-rw-r--r--lib/Utils/encbase64.cpp78
-rw-r--r--lib/Utils/logging.cpp46
-rw-r--r--lib/Utils/make_descr.cpp133
-rw-r--r--lib/Utils/skip_whitespace.cpp22
-rw-r--r--lib/Utils/time.cpp65
-rw-r--r--lib/Utils/xconnect.cpp416
-rw-r--r--lib/Utils/xfuncs.cpp63
20 files changed, 1055 insertions, 231 deletions
diff --git a/lib/Utils/Action.h b/lib/Utils/Action.h
index 1286feba..5992cbff 100644
--- a/lib/Utils/Action.h
+++ b/lib/Utils/Action.h
@@ -22,7 +22,6 @@
#ifndef ACTION_H_
#define ACTION_H_
-#include <string>
#include "Plugin.h"
/**
@@ -40,8 +39,7 @@ class CAction : public CPlugin
* @param pActionDir An actual directory.
* @param pArgs Plugin's arguments.
*/
- virtual void Run(const std::string& pActionDir,
- const std::string& pArgs) = 0;
+ virtual void Run(const char *pActionDir, const char *pArgs) = 0;
};
-#endif /*ACTION_H_*/
+#endif
diff --git a/lib/Utils/Analyzer.h b/lib/Utils/Analyzer.h
index e5bda574..9108a914 100644
--- a/lib/Utils/Analyzer.h
+++ b/lib/Utils/Analyzer.h
@@ -37,20 +37,20 @@ class CAnalyzer : public CPlugin
* @param pDebugDumpPath A debugdump dir containing all necessary data.
* @return A local UUID.
*/
- virtual std::string GetLocalUUID(const std::string& pDebugDumpPath) = 0;
+ virtual std::string GetLocalUUID(const char *pDebugDumpDir) = 0;
/**
* A method, which gets a global UUID of particular crash.
* @param pDebugDumpPath A debugdump dir containing all necessary data.
* @return A global UUID.
*/
- virtual std::string GetGlobalUUID(const std::string& pDebugDumpPath) = 0;
+ virtual std::string GetGlobalUUID(const char *pDebugDumpDir) = 0;
/**
* A method, which takes care of getting all additional data needed
* for computing UUIDs and creating a report. This report could be send
* somewhere afterwards.
* @param pDebugDumpPath A debugdump dir containing all necessary data.
*/
- virtual void CreateReport(const std::string& pDebugDumpPath, int force) = 0;
+ virtual void CreateReport(const char *pDebugDumpDir, int force) = 0;
};
#endif /*ANALYZER_H_*/
diff --git a/lib/Utils/CommLayerInner.cpp b/lib/Utils/CommLayerInner.cpp
index b5b8db78..307fe66d 100644
--- a/lib/Utils/CommLayerInner.cpp
+++ b/lib/Utils/CommLayerInner.cpp
@@ -10,29 +10,48 @@ static map_uint_str_t s_mapClientID;
static pthread_mutex_t s_map_mutex;
static bool s_map_mutex_inited;
+/* called via [p]error_msg() */
+static void warn_client(const char *msg)
+{
+ if (!s_pObs)
+ return;
+
+ uint64_t key = uint64_t(pthread_self());
+
+ pthread_mutex_lock(&s_map_mutex);
+ map_uint_str_t::const_iterator ki = s_mapClientID.find(key);
+ const char* peer = (ki != s_mapClientID.end() ? ki->second.c_str() : NULL);
+ pthread_mutex_unlock(&s_map_mutex);
+
+ if (peer)
+ s_pObs->Warning(msg, peer, key);
+}
+
void init_daemon_logging(CObserver *pObs)
{
s_pObs = pObs;
if (!s_map_mutex_inited)
{
- pthread_mutex_init(&s_map_mutex, NULL);
s_map_mutex_inited = true;
+ pthread_mutex_init(&s_map_mutex, NULL);
+ g_custom_logger = &warn_client;
}
}
-void set_client_name(const char* name)
+void set_client_name(const char *name)
{
uint64_t key = uint64_t(pthread_self());
pthread_mutex_lock(&s_map_mutex);
- if (!name)
+ if (!name) {
s_mapClientID.erase(key);
- else
+ } else {
s_mapClientID[key] = name;
+ }
pthread_mutex_unlock(&s_map_mutex);
}
-void warn_client(const std::string& pMessage)
+void update_client(const char *fmt, ...)
{
if (!s_pObs)
return;
@@ -44,26 +63,16 @@ void warn_client(const std::string& pMessage)
const char* peer = (ki != s_mapClientID.end() ? ki->second.c_str() : NULL);
pthread_mutex_unlock(&s_map_mutex);
- if (peer)
- s_pObs->Warning(pMessage, peer, key);
- else /* Bug: someone tries to warn_client() without set_client_name()!? */
- log("Hmm, stray %s: '%s'", __func__, pMessage.c_str());
-}
-
-void update_client(const std::string& pMessage)
-{
- if (!s_pObs)
+ if (!peer)
return;
- uint64_t key = uint64_t(pthread_self());
+ va_list p;
+ va_start(p, fmt);
+ char *msg;
+ int used = vasprintf(&msg, fmt, p);
+ va_end(p);
+ if (used < 0)
+ return;
- pthread_mutex_lock(&s_map_mutex);
- map_uint_str_t::const_iterator ki = s_mapClientID.find(key);
- const char* peer = (ki != s_mapClientID.end() ? ki->second.c_str() : NULL);
- pthread_mutex_unlock(&s_map_mutex);
-
- if (peer)
- s_pObs->Status(pMessage, peer, key);
- else
- log("Hmm, stray %s: '%s'", __func__, pMessage.c_str());
+ s_pObs->Status(msg, peer, key);
}
diff --git a/lib/Utils/CommLayerInner.h b/lib/Utils/CommLayerInner.h
index d161cfc7..9c22968a 100644
--- a/lib/Utils/CommLayerInner.h
+++ b/lib/Utils/CommLayerInner.h
@@ -9,15 +9,19 @@ void init_daemon_logging(CObserver *pObs);
* Set client's name (dbus ID). NULL unsets it.
*/
void set_client_name(const char* name);
-/* Ask a client to warn the user about a non-fatal, but unexpected condition.
+
+/*
+ * Ask a client to warn the user about a non-fatal, but unexpected condition.
* In GUI, it will usually be presented as a popup message.
+ * Usually there is no need to call it directly, just use [p]error_msg().
*/
-void warn_client(const std::string& pMessage);
-/* Logs a message to a client.
+//now static: void warn_client(const char *msg);
+
+/*
+ * Logs a message to a client.
* In UI, it will usually appear as a new status line message in GUI,
* or as a new message line in CLI.
*/
-void update_client(const std::string& pMessage);
-
-#endif /* COMMLAYERINNER_H_ */
+void update_client(const char *fmt, ...);
+#endif
diff --git a/lib/Utils/DBusCommon.h b/lib/Utils/DBusCommon.h
index b3e3af2c..2e3ed8a5 100644
--- a/lib/Utils/DBusCommon.h
+++ b/lib/Utils/DBusCommon.h
@@ -22,8 +22,8 @@
#include "CrashTypes.h"
-#define CC_DBUS_NAME "com.redhat.abrt"
-#define CC_DBUS_PATH "/com/redhat/abrt"
-#define CC_DBUS_IFACE "com.redhat.abrt"
+#define ABRTD_DBUS_NAME "com.redhat.abrt"
+#define ABRTD_DBUS_PATH "/com/redhat/abrt"
+#define ABRTD_DBUS_IFACE "com.redhat.abrt"
#endif
diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp
index 43eb3244..bf793bbf 100644
--- a/lib/Utils/DebugDump.cpp
+++ b/lib/Utils/DebugDump.cpp
@@ -23,45 +23,41 @@
#include <iostream>
#include <sstream>
#include <sys/utsname.h>
-#include <magic.h>
+//#include <magic.h>
#include "abrtlib.h"
#include "DebugDump.h"
#include "ABRTException.h"
#include "CommLayerInner.h"
-/* Is it "." or ".."? */
-/* abrtlib candidate */
-static bool dot_or_dotdot(const char *filename)
-{
- if (filename[0] != '.') return false;
- if (filename[1] == '\0') return true;
- if (filename[1] != '.') return false;
- if (filename[2] == '\0') return true;
- return false;
-}
-
static bool isdigit_str(const char *str)
{
- while (*str)
+ do
{
if (*str < '0' || *str > '9') return false;
str++;
- }
+ } while (*str);
return true;
}
-static std::string RemoveBackSlashes(const std::string& pDir);
+static std::string RemoveBackSlashes(const char *pDir)
+{
+ unsigned len = strlen(pDir);
+ while (len != 0 && pDir[len-1] == '/')
+ len--;
+ return std::string(pDir, len);
+}
+
static bool ExistFileDir(const char* pPath);
-static void LoadTextFile(const std::string& pPath, std::string& pData);
+static void LoadTextFile(const char *pPath, std::string& pData);
CDebugDump::CDebugDump() :
m_sDebugDumpDir(""),
- m_bOpened(false),
m_pGetNextFileDir(NULL),
- m_nLockfileFD(-1)
+ m_bOpened(false),
+ m_bLocked(false)
{}
-void CDebugDump::Open(const std::string& pDir)
+void CDebugDump::Open(const char *pDir)
{
if (m_bOpened)
{
@@ -70,7 +66,7 @@ void CDebugDump::Open(const std::string& pDir)
m_sDebugDumpDir = RemoveBackSlashes(pDir);
if (!ExistFileDir(m_sDebugDumpDir.c_str()))
{
- throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::CDebugDump(): "+m_sDebugDumpDir+" does not exist.");
+ throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::CDebugDump(): " + m_sDebugDumpDir + " does not exist.");
}
Lock();
m_bOpened = true;
@@ -96,10 +92,49 @@ static bool ExistFileDir(const char* pPath)
return false;
}
-static int GetAndSetLock(const char* pLockFile, const char* pPID)
+static bool GetAndSetLock(const char* pLockFile, const char* pPID)
{
- int fd;
+ while (symlink(pPID, pLockFile) != 0)
+ {
+ if (errno != EEXIST)
+ perror_msg_and_die("Can't create lock file '%s'", pLockFile);
+ char pid_buf[sizeof(pid_t)*3 + 4];
+ ssize_t r = readlink(pLockFile, pid_buf, sizeof(pid_buf) - 1);
+ if (r < 0)
+ perror_msg_and_die("Can't read lock file '%s'", pLockFile);
+ pid_buf[r] = '\0';
+
+ if (strcmp(pid_buf, pPID) == 0)
+ {
+ log("Lock file '%s' is already locked by us", pLockFile);
+ return false;
+ }
+ if (isdigit_str(pid_buf))
+ {
+ if (access(ssprintf("/proc/%s", pid_buf).c_str(), F_OK) == 0)
+ {
+ log("Lock file '%s' is locked by process %s", pLockFile, pid_buf);
+ return false;
+ }
+ log("Lock file '%s' was locked by process %s, but it crashed?", pLockFile, pid_buf);
+ }
+ /* The file may be deleted by now by other process. Ignore ENOENT */
+ if (unlink(pLockFile) != 0 && errno != ENOENT)
+ {
+ perror_msg_and_die("Can't remove stale lock file '%s'", pLockFile);
+ }
+ }
+
+ VERB1 log("Locked '%s'", pLockFile);
+ return true;
+
+#if 0
+/* Old code was using ordinary files instead of symlinks,
+ * but it had a race window between open and write, during which file was
+ * empty. It was seen to happen in practice.
+ */
+ int fd;
while ((fd = open(pLockFile, O_WRONLY | O_CREAT | O_EXCL, 0640)) < 0)
{
if (errno != EEXIST)
@@ -148,20 +183,22 @@ static int GetAndSetLock(const char* pLockFile, const char* pPID)
/* close(fd); - not needed, exiting does it too */
perror_msg_and_die("Can't write lock file '%s'", pLockFile);
}
+ close(fd);
VERB1 log("Locked '%s'", pLockFile);
- return fd;
+ return true;
+#endif
}
void CDebugDump::Lock()
{
- if (m_nLockfileFD >= 0)
+ if (m_bLocked)
error_msg_and_die("Locking bug on '%s'", m_sDebugDumpDir.c_str());
std::string lockFile = m_sDebugDumpDir + ".lock";
char pid_buf[sizeof(int)*3 + 2];
sprintf(pid_buf, "%u", (unsigned)getpid());
- while ((m_nLockfileFD = GetAndSetLock(lockFile.c_str(), pid_buf)) < 0)
+ while ((m_bLocked = GetAndSetLock(lockFile.c_str(), pid_buf)) != true)
{
usleep(500000);
}
@@ -169,27 +206,26 @@ void CDebugDump::Lock()
void CDebugDump::UnLock()
{
- if (m_nLockfileFD >= 0)
+ if (m_bLocked)
{
+ m_bLocked = false;
std::string lockFile = m_sDebugDumpDir + ".lock";
- close(m_nLockfileFD);
- m_nLockfileFD = -1;
xunlink(lockFile.c_str());
VERB1 log("UnLocked '%s'", lockFile.c_str());
}
}
-void CDebugDump::Create(const std::string& pDir, uid_t uid)
+void CDebugDump::Create(const char *pDir, int64_t uid)
{
if (m_bOpened)
{
- throw CABRTException(EXCEP_ERROR, "CDebugDump::CDebugDump(): DebugDump is already opened.");
+ throw CABRTException(EXCEP_ERROR, "DebugDump is already opened");
}
m_sDebugDumpDir = RemoveBackSlashes(pDir);
if (ExistFileDir(m_sDebugDumpDir.c_str()))
{
- throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::CDebugDump(): "+m_sDebugDumpDir+" already exists.");
+ throw CABRTException(EXCEP_DD_OPEN, ssprintf("'%s' already exists", m_sDebugDumpDir.c_str()));
}
Lock();
@@ -199,13 +235,13 @@ void CDebugDump::Create(const std::string& pDir, uid_t uid)
{
UnLock();
m_bOpened = false;
- throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::Create(): Cannot create dir: " + pDir);
+ throw CABRTException(EXCEP_DD_OPEN, ssprintf("Can't create dir '%s'", pDir));
}
if (chmod(m_sDebugDumpDir.c_str(), 0700) == -1)
{
UnLock();
m_bOpened = false;
- throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::Create(): Cannot change permissions, dir: " + pDir);
+ throw CABRTException(EXCEP_DD_OPEN, ssprintf("Can't change mode of '%s'", pDir));
}
struct passwd* pw = getpwuid(uid);
gid_t gid = pw ? pw->pw_gid : uid;
@@ -216,14 +252,15 @@ void CDebugDump::Create(const std::string& pDir, uid_t uid)
perror_msg("can't change '%s' ownership to %u:%u", m_sDebugDumpDir.c_str(), (int)uid, (int)gid);
}
- SaveText(FILENAME_UID, ssprintf("%u", (int)uid));
+ SaveText(FILENAME_UID, to_string(uid).c_str());
SaveKernelArchitectureRelease();
- SaveTime();
+ time_t t = time(NULL);
+ SaveText(FILENAME_TIME, to_string(t).c_str());
}
-static void DeleteFileDir(const std::string& pDir)
+static void DeleteFileDir(const char *pDir)
{
- DIR *dir = opendir(pDir.c_str());
+ DIR *dir = opendir(pDir);
if (!dir)
return;
@@ -232,26 +269,28 @@ static void DeleteFileDir(const std::string& pDir)
{
if (dot_or_dotdot(dent->d_name))
continue;
- std::string fullPath = pDir + "/" + dent->d_name;
+ std::string fullPath = concat_path_file(pDir, dent->d_name);
if (unlink(fullPath.c_str()) == -1)
{
if (errno != EISDIR)
{
closedir(dir);
- throw CABRTException(EXCEP_DD_DELETE, std::string(__func__) + ": Cannot remove file: " + fullPath);
+ throw CABRTException(EXCEP_DD_DELETE, ssprintf("Can't remove dir %s", fullPath.c_str()));
}
- DeleteFileDir(fullPath);
+ DeleteFileDir(fullPath.c_str());
}
}
closedir(dir);
- if (remove(pDir.c_str()) == -1)
+ if (remove(pDir) == -1)
{
- throw CABRTException(EXCEP_DD_DELETE, std::string(__func__) + ": Cannot remove dir: " + pDir);
+ throw CABRTException(EXCEP_DD_DELETE, ssprintf("Can't remove dir %s", pDir));
}
}
-static bool IsTextFile(const std::string& pName)
+static bool IsTextFile(const char *name)
{
+/* This idiotic library thinks that file containing just "0" is not text (!!)
+
magic_t m = magic_open(MAGIC_MIME_TYPE);
if (m == NULL)
@@ -280,16 +319,24 @@ static bool IsTextFile(const std::string& pName)
magic_close(m);
return isText;
-}
+ */
+ int fd = open(name, O_RDONLY);
+ if (fd < 0)
+ return false;
-static std::string RemoveBackSlashes(const std::string& pDir)
-{
- std::string ret = pDir;
- while (ret[ret.length() - 1] == '/')
+ unsigned char buf[4*1024];
+ int r = full_read(fd, buf, sizeof(buf));
+ close(fd);
+
+ while (--r >= 0)
{
- ret = ret.substr(0, ret.length() - 2);
+ if (buf[r] >= 0x7f)
+ return false;
+ /* Among control chars, only '\t','\n' etc are allowed */
+ if (buf[r] < ' ' && !isspace(buf[r]))
+ return false;
}
- return ret;
+ return true;
}
void CDebugDump::Delete()
@@ -298,7 +345,7 @@ void CDebugDump::Delete()
{
return;
}
- DeleteFileDir(m_sDebugDumpDir);
+ DeleteFileDir(m_sDebugDumpDir.c_str());
}
void CDebugDump::Close()
@@ -322,100 +369,47 @@ void CDebugDump::SaveKernelArchitectureRelease()
}
std::string release;
LoadTextFile("/etc/redhat-release", release);
- SaveText(FILENAME_RELEASE, release);
+ const char *release_ptr = release.c_str();
+ unsigned len_1st_str = strchrnul(release_ptr, '\n') - release_ptr;
+ release.erase(len_1st_str); /* usually simply removes trailing '\n' */
+ SaveText(FILENAME_RELEASE, release.c_str());
}
-void CDebugDump::SaveTime()
+static void LoadTextFile(const char *pPath, std::string& pData)
{
- time_t t = time(NULL);
- SaveText(FILENAME_TIME, to_string(t));
-}
-
-static void LoadTextFile(const std::string& pPath, std::string& pData)
-{
- std::ifstream fIn;
+ FILE *fp = fopen(pPath, "r");
+ if (!fp)
+ {
+ throw CABRTException(EXCEP_DD_LOAD, ssprintf("Can't open file '%s'", pPath));
+ }
pData = "";
- fIn.open(pPath.c_str());
- if (fIn.is_open())
+ int ch;
+ while ((ch = fgetc(fp)) != EOF)
{
- // TODO: rewrite this
- int ch;
- while ((ch = fIn.get())!= EOF)
+ if (ch == '\0')
{
- if (ch == 0)
- {
- pData += " ";
- }
- else if (isspace(ch) || (isascii(ch) && !iscntrl(ch)))
- {
- pData += ch;
- }
+ pData += ' ';
}
- fIn.close();
- }
- else
- {
- throw CABRTException(EXCEP_DD_LOAD, std::string(__func__) + ": Cannot open file " + pPath);
- }
-}
-
-static void LoadBinaryFile(const std::string& pPath, char** pData, unsigned int* pSize)
-{
- std::ifstream fIn;
- fIn.open(pPath.c_str(), std::ios::binary | std::ios::ate);
- unsigned int size;
- if (fIn.is_open())
- {
- size = fIn.tellg();
- char *data = new char [size];
- fIn.read(data, size);
-
- *pData = data;
- *pSize = size;
-
- fIn.close();
- }
- else
- {
- throw CABRTException(EXCEP_DD_LOAD, std::string(__func__) + ": Cannot open file " + pPath);
- }
-}
-
-static void SaveTextFile(const std::string& pPath, const std::string& pData)
-{
- std::ofstream fOut;
- fOut.open(pPath.c_str());
- if (fOut.is_open())
- {
- fOut << pData;
- if (!fOut.good())
+ else if (isspace(ch) || (isascii(ch) && !iscntrl(ch)))
{
- throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot save file " + pPath);
+ pData += ch;
}
- fOut.close();
- }
- else
- {
- throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot open file " + pPath);
}
+ fclose(fp);
}
-static void SaveBinaryFile(const std::string& pPath, const char* pData, const unsigned pSize)
+static void SaveBinaryFile(const char *pPath, const char* pData, unsigned pSize)
{
- std::ofstream fOut;
- fOut.open(pPath.c_str(), std::ios::binary);
- if (fOut.is_open())
+ int fd = open(pPath, O_WRONLY | O_TRUNC | O_CREAT, 0666);
+ if (fd < 0)
{
- fOut.write(pData, pSize);
- if (!fOut.good())
- {
- throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot save file " + pPath);
- }
- fOut.close();
+ throw CABRTException(EXCEP_DD_SAVE, ssprintf("Can't open file '%s'", pPath));
}
- else
+ unsigned r = full_write(fd, pData, pSize);
+ close(fd);
+ if (r != pSize)
{
- throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot open file " + pPath);
+ throw CABRTException(EXCEP_DD_SAVE, ssprintf("Can't save file '%s'", pPath));
}
}
@@ -425,36 +419,27 @@ void CDebugDump::LoadText(const char* pName, std::string& pData)
{
throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::LoadText(): DebugDump is not opened.");
}
- std::string fullPath = m_sDebugDumpDir + "/" + pName;
- LoadTextFile(fullPath, pData);
-}
-void CDebugDump::LoadBinary(const char* pName, char** pData, unsigned int* pSize)
-{
- if (!m_bOpened)
- {
- throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::LoadBinary(): DebugDump is not opened.");
- }
- std::string fullPath = m_sDebugDumpDir + "/" + pName;
- LoadBinaryFile(fullPath, pData, pSize);
+ std::string fullPath = m_sDebugDumpDir + '/' + pName;
+ LoadTextFile(fullPath.c_str(), pData);
}
-void CDebugDump::SaveText(const char* pName, const std::string& pData)
+void CDebugDump::SaveText(const char* pName, const char* pData)
{
if (!m_bOpened)
{
throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::SaveText(): DebugDump is not opened.");
}
std::string fullPath = m_sDebugDumpDir + "/" + pName;
- SaveTextFile(fullPath, pData);
+ SaveBinaryFile(fullPath.c_str(), pData, strlen(pData));
}
-void CDebugDump::SaveBinary(const char* pName, const char* pData, const unsigned int pSize)
+void CDebugDump::SaveBinary(const char* pName, const char* pData, unsigned pSize)
{
if (!m_bOpened)
{
throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::SaveBinary(): DebugDump is not opened.");
}
std::string fullPath = m_sDebugDumpDir + "/" + pName;
- SaveBinaryFile(fullPath, pData, pSize);
+ SaveBinaryFile(fullPath.c_str(), pData, pSize);
}
void CDebugDump::InitGetNextFile()
@@ -486,17 +471,17 @@ bool CDebugDump::GetNextFile(std::string& pFileName, std::string& pContent, bool
{
if (is_regular_file(dent, m_sDebugDumpDir.c_str()))
{
- std::string fullname = m_sDebugDumpDir + "/" + dent->d_name;
+ std::string fullname = m_sDebugDumpDir + '/' + dent->d_name;
pFileName = dent->d_name;
- if (IsTextFile(fullname))
+ if (IsTextFile(fullname.c_str()))
{
LoadText(dent->d_name, pContent);
pIsTextFile = true;
}
else
{
- pContent = "";
+ pContent.clear();
pIsTextFile = false;
}
return true;
@@ -506,4 +491,3 @@ bool CDebugDump::GetNextFile(std::string& pFileName, std::string& pContent, bool
m_pGetNextFileDir = NULL;
return false;
}
-
diff --git a/lib/Utils/DebugDump.h b/lib/Utils/DebugDump.h
index d6ff4f9f..b48a386d 100644
--- a/lib/Utils/DebugDump.h
+++ b/lib/Utils/DebugDump.h
@@ -25,6 +25,7 @@
#include <string>
#include <dirent.h>
+#include <stdint.h>
#define FILENAME_ARCHITECTURE "architecture"
#define FILENAME_KERNEL "kernel"
@@ -39,17 +40,17 @@
#define FILENAME_REASON "reason"
#define FILENAME_COMMENT "comment"
#define FILENAME_REPRODUCE "reproduce"
+#define FILENAME_RATING "rating"
class CDebugDump
{
private:
std::string m_sDebugDumpDir;
- bool m_bOpened;
DIR* m_pGetNextFileDir;
- int m_nLockfileFD;
+ bool m_bOpened;
+ bool m_bLocked;
void SaveKernelArchitectureRelease();
- void SaveTime();
void Lock();
void UnLock();
@@ -58,18 +59,17 @@ class CDebugDump
CDebugDump();
~CDebugDump() { Close(); }
- void Open(const std::string& pDir);
- void Create(const std::string& pDir, uid_t nUID);
+ void Open(const char *pDir);
+ void Create(const char *pDir, int64_t uid);
void Delete();
void Close();
bool Exist(const char* pFileName);
void LoadText(const char* pName, std::string& pData);
- void LoadBinary(const char* pName, char** pData, unsigned int* pSize);
- void SaveText(const char* pName, const std::string& pData);
- void SaveBinary(const char* pName, const char* pData, const unsigned int pSize);
+ void SaveText(const char* pName, const char *pData);
+ void SaveBinary(const char* pName, const char* pData, unsigned pSize);
void InitGetNextFile();
bool GetNextFile(std::string& pFileName, std::string& pContent, bool& pIsTextFile);
diff --git a/lib/Utils/Makefile.am b/lib/Utils/Makefile.am
index 713fe868..68c925fd 100644
--- a/lib/Utils/Makefile.am
+++ b/lib/Utils/Makefile.am
@@ -1,15 +1,21 @@
lib_LTLIBRARIES = libABRTUtils.la
+# Not used just yet:
+# time.cpp
+# xconnect.cpp
+
libABRTUtils_la_SOURCES = \
xfuncs.cpp \
+ encbase64.cpp \
read_write.cpp \
logging.cpp \
copyfd.cpp \
+ skip_whitespace.cpp \
CrashTypesSocket.cpp \
DebugDump.h DebugDump.cpp \
CommLayerInner.h CommLayerInner.cpp \
abrt_dbus.h abrt_dbus.cpp \
- Plugin.h Plugin.cpp \
+ Plugin.h Plugin.cpp make_descr.cpp \
Polkit.h Polkit.cpp \
Action.h Database.h Reporter.h Analyzer.h \
Observer.h \
@@ -31,7 +37,6 @@ libABRTUtils_la_LDFLAGS = \
$(DL_LIBS) \
$(DBUS_LIBS)
libABRTUtils_la_LIBADD = \
- -lmagic \
$(POLKIT_LIBS)
install-data-local:
diff --git a/lib/Utils/Observer.h b/lib/Utils/Observer.h
index d6ec6f35..db748659 100644
--- a/lib/Utils/Observer.h
+++ b/lib/Utils/Observer.h
@@ -8,8 +8,8 @@
class CObserver {
public:
virtual ~CObserver() {}
- virtual void Status(const std::string& pMessage, const char* peer, uint64_t pDest) = 0;
- virtual void Warning(const std::string& pMessage, const char* peer, uint64_t pDest) = 0;
+ virtual void Status(const char *pMessage, const char* peer, uint64_t pDest) = 0;
+ virtual void Warning(const char *pMessage, const char* peer, uint64_t pDest) = 0;
};
#endif
diff --git a/lib/Utils/Plugin.cpp b/lib/Utils/Plugin.cpp
index 161ead8a..4d561b46 100644
--- a/lib/Utils/Plugin.cpp
+++ b/lib/Utils/Plugin.cpp
@@ -19,9 +19,18 @@
#include "Plugin.h"
+CPlugin::CPlugin() {}
+
/* class CPlugin's virtuals */
CPlugin::~CPlugin() {}
void CPlugin::Init() {}
void CPlugin::DeInit() {}
-void CPlugin::SetSettings(const map_plugin_settings_t& pSettings) {}
-map_plugin_settings_t CPlugin::GetSettings() {return map_plugin_settings_t();}
+void CPlugin::SetSettings(const map_plugin_settings_t& pSettings)
+{
+ m_pSettings = pSettings;
+}
+
+const map_plugin_settings_t& CPlugin::GetSettings()
+{
+ return m_pSettings;
+}
diff --git a/lib/Utils/Plugin.h b/lib/Utils/Plugin.h
index 39290231..f93f7e7b 100644
--- a/lib/Utils/Plugin.h
+++ b/lib/Utils/Plugin.h
@@ -24,17 +24,10 @@
#define PLUGIN_H_
#include "abrt_types.h"
-
-#define PLUGINS_MAGIC_NUMBER 6
-
-#define PLUGINS_CONF_EXTENSION "conf"
-#define PLUGINS_LIB_EXTENSION "so"
-#define PLUGINS_LIB_PREFIX "lib"
-
+#include "CrashTypes.h"
#if HAVE_CONFIG_H
#include <config.h>
#endif
-
#if ENABLE_NLS
#include <libintl.h>
#define _(S) gettext(S)
@@ -42,13 +35,23 @@
#define _(S) (S)
#endif
+#define PLUGINS_MAGIC_NUMBER 6
+
+#define PLUGINS_CONF_EXTENSION "conf"
+#define PLUGINS_LIB_EXTENSION "so"
+#define PLUGINS_LIB_PREFIX "lib"
+
/**
* An abstract class. The class defines a common plugin interface. If a plugin
* has some settings, then a *Settings(*) method has to be written.
*/
class CPlugin
{
+ protected:
+ map_plugin_settings_t m_pSettings;
+
public:
+ CPlugin();
/**
* A destructor.
*/
@@ -70,7 +73,7 @@ class CPlugin
* A method, which return current settings. It is not mandatory method.
* @return Plugin's settings
*/
- virtual map_plugin_settings_t GetSettings();
+ virtual const map_plugin_settings_t& GetSettings();
};
/**
@@ -115,4 +118,8 @@ typedef struct SPluginInfo
PLUGINS_MAGIC_NUMBER,\
};
-#endif /* PLUGIN_H_ */
+/* helper finctions */
+std::string make_description_bz(const map_crash_report_t& pCrashReport);
+std::string make_description_logger(const map_crash_report_t& pCrashReport);
+
+#endif
diff --git a/lib/Utils/Reporter.h b/lib/Utils/Reporter.h
index c74a10c3..f2788993 100644
--- a/lib/Utils/Reporter.h
+++ b/lib/Utils/Reporter.h
@@ -41,6 +41,7 @@ class CReporter : public CPlugin
* @retun A message which can be displayed after a report is created.
*/
virtual std::string Report(const map_crash_report_t& pCrashReport,
+ const map_plugin_settings_t& pSettings,
const std::string& pArgs) = 0;
};
diff --git a/lib/Utils/copyfd.cpp b/lib/Utils/copyfd.cpp
index cda52b03..9abe7522 100644
--- a/lib/Utils/copyfd.cpp
+++ b/lib/Utils/copyfd.cpp
@@ -105,3 +105,25 @@ off_t copyfd_eof(int fd1, int fd2)
{
return full_fd_action(fd1, fd2, 0);
}
+
+off_t copy_file(const char *src_name, const char *dst_name)
+{
+ off_t r;
+ int src = open(src_name, O_RDONLY);
+ if (src < 0)
+ {
+ perror_msg("Can't open '%s'", src_name);
+ return -1;
+ }
+ int dst = open(dst_name, O_WRONLY | O_TRUNC | O_CREAT, 0666);
+ if (dst < 0)
+ {
+ close(src);
+ perror_msg("Can't open '%s'", dst_name);
+ return -1;
+ }
+ r = copyfd_eof(src, dst);
+ close(src);
+ close(dst);
+ return r;
+}
diff --git a/lib/Utils/encbase64.cpp b/lib/Utils/encbase64.cpp
new file mode 100644
index 00000000..6a6f1f75
--- /dev/null
+++ b/lib/Utils/encbase64.cpp
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2006 Rob Landley <rob@landley.net>
+ *
+ * Licensed under GPLv2 or later.
+ */
+#include "abrtlib.h" /* xmalloc */
+
+/* Conversion table for base 64 */
+static const char tbl_base64[65 /*+ 2*/] = {
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+ 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3',
+ '4', '5', '6', '7', '8', '9', '+', '/',
+ '=' /* termination character */,
+ // '\n', '\0' /* needed for uudecode.c */
+};
+
+/* Conversion table for uuencode
+const char tbl_uuencode[65] ALIGN1 = {
+ '`', '!', '"', '#', '$', '%', '&', '\'',
+ '(', ')', '*', '+', ',', '-', '.', '/',
+ '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', ':', ';', '<', '=', '>', '?',
+ '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
+ 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
+ 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
+ '`'
+};
+*/
+
+/*
+ * Encode bytes at S of length LENGTH.
+ * Result will be 0-terminated, and must point to a writable
+ * buffer of at least 1+BASE64_LENGTH(length) bytes,
+ * where BASE64_LENGTH(len) = 4 * ((LENGTH + 2) / 3)
+ */
+static void encode_64bit(char *p, const void *src, int length, const char *tbl)
+{
+ const unsigned char *s = (const unsigned char *)src;
+
+ /* Transform the 3x8 bits to 4x6 bits */
+ while (length > 0) {
+ unsigned s1, s2;
+
+ /* Are s[1], s[2] valid or should be assumed 0? */
+ s1 = s2 = 0;
+ length -= 3; /* can be >=0, -1, -2 */
+ if (length >= -1) {
+ s1 = s[1];
+ if (length >= 0)
+ s2 = s[2];
+ }
+ *p++ = tbl[s[0] >> 2];
+ *p++ = tbl[((s[0] & 3) << 4) + (s1 >> 4)];
+ *p++ = tbl[((s1 & 0xf) << 2) + (s2 >> 6)];
+ *p++ = tbl[s2 & 0x3f];
+ s += 3;
+ }
+ /* Zero-terminate */
+ *p = '\0';
+ /* If length is -2 or -1, pad last char or two */
+ while (length) {
+ *--p = tbl[64];
+ length++;
+ }
+}
+
+char *encode_base64(const void *src, int length)
+{
+ char *dst = (char *)xmalloc(4 * ((length + 2) / 3) + 1);
+ encode_64bit(dst, src, length, tbl_base64);
+ return dst;
+}
diff --git a/lib/Utils/logging.cpp b/lib/Utils/logging.cpp
index f70d23f3..cae609bc 100644
--- a/lib/Utils/logging.cpp
+++ b/lib/Utils/logging.cpp
@@ -7,19 +7,18 @@
#include <syslog.h>
int xfunc_error_retval = EXIT_FAILURE;
-
int g_verbose;
+int logmode = LOGMODE_STDIO;
+const char *msg_prefix = "";
+const char *msg_eol = "\n";
+void (*g_custom_logger)(const char*);
void xfunc_die(void)
{
exit(xfunc_error_retval);
}
-const char *msg_prefix = "";
-const char *msg_eol = "\n";
-int logmode = LOGMODE_STDIO;
-
-void verror_msg(const char *s, va_list p, const char* strerr)
+static void verror_msg_helper(const char *s, va_list p, const char* strerr, int flags)
{
char *msg;
int prefix_len, strerr_len, msgeol_len, used;
@@ -27,9 +26,6 @@ void verror_msg(const char *s, va_list p, const char* strerr)
if (!logmode)
return;
- if (!s) /* nomsg[_and_die] uses NULL fmt */
- s = ""; /* some libc don't like printf(NULL) */
-
used = vasprintf(&msg, s, p);
if (used < 0)
return;
@@ -51,7 +47,7 @@ void verror_msg(const char *s, va_list p, const char* strerr)
memcpy(msg, msg_prefix, prefix_len);
}
if (strerr) {
- if (s[0]) { /* not perror_nomsg? */
+ if (s[0]) {
msg[used++] = ':';
msg[used++] = ' ';
}
@@ -60,24 +56,26 @@ void verror_msg(const char *s, va_list p, const char* strerr)
}
strcpy(&msg[used], msg_eol);
- if (logmode & LOGMODE_STDIO) {
+ if (flags & LOGMODE_STDIO) {
fflush(stdout);
full_write(STDERR_FILENO, msg, used + msgeol_len);
}
- if (logmode & LOGMODE_SYSLOG) {
+ if (flags & LOGMODE_SYSLOG) {
syslog(LOG_ERR, "%s", msg + prefix_len);
}
+ if ((flags & LOGMODE_CUSTOM) && g_custom_logger) {
+ g_custom_logger(msg + prefix_len);
+ }
free(msg);
}
-void error_msg_and_die(const char *s, ...)
+void log_msg(const char *s, ...)
{
va_list p;
va_start(p, s);
- verror_msg(s, p, NULL);
+ verror_msg_helper(s, p, NULL, logmode);
va_end(p);
- xfunc_die();
}
void error_msg(const char *s, ...)
@@ -85,8 +83,18 @@ void error_msg(const char *s, ...)
va_list p;
va_start(p, s);
- verror_msg(s, p, NULL);
+ verror_msg_helper(s, p, NULL, (logmode | LOGMODE_CUSTOM));
+ va_end(p);
+}
+
+void error_msg_and_die(const char *s, ...)
+{
+ va_list p;
+
+ va_start(p, s);
+ verror_msg_helper(s, p, NULL, (logmode | LOGMODE_CUSTOM));
va_end(p);
+ xfunc_die();
}
void perror_msg_and_die(const char *s, ...)
@@ -95,7 +103,7 @@ void perror_msg_and_die(const char *s, ...)
va_start(p, s);
/* Guard against "<error message>: Success" */
- verror_msg(s, p, errno ? strerror(errno) : NULL);
+ verror_msg_helper(s, p, errno ? strerror(errno) : NULL, (logmode | LOGMODE_CUSTOM));
va_end(p);
xfunc_die();
}
@@ -106,7 +114,7 @@ void perror_msg(const char *s, ...)
va_start(p, s);
/* Guard against "<error message>: Success" */
- verror_msg(s, p, errno ? strerror(errno) : NULL);
+ verror_msg_helper(s, p, errno ? strerror(errno) : NULL, (logmode | LOGMODE_CUSTOM));
va_end(p);
}
@@ -122,5 +130,5 @@ void simple_perror_msg(const char *s)
void die_out_of_memory(void)
{
- error_msg_and_die("Out of memory, exiting");
+ error_msg_and_die("Out of memory, exiting");
}
diff --git a/lib/Utils/make_descr.cpp b/lib/Utils/make_descr.cpp
new file mode 100644
index 00000000..1352149e
--- /dev/null
+++ b/lib/Utils/make_descr.cpp
@@ -0,0 +1,133 @@
+#include "abrtlib.h"
+//#include "abrt_types.h"
+#include "CrashTypes.h"
+#include "DebugDump.h" /* FILENAME_ARCHITECTURE etc */
+
+using namespace std;
+
+static void add_content(bool &was_multiline, string& description, const char *header, const char *content)
+{
+ /* We separate multiline contents with emply line */
+ if (was_multiline)
+ description += '\n';
+
+ while (content[0] == '\n')
+ content++;
+
+ if (strchr(content, '\n') == NULL)
+ {
+ if (skip_whitespace(content)[0] == '\0')
+ {
+ /* empty, dont report at all */
+ return;
+ }
+ /* one string value, like OS release */
+ description += header;
+ description += ": ";
+ description += content;
+ description += '\n';
+ was_multiline = 0;
+ }
+ else
+ {
+ /* multi-string value, like backtrace */
+ if (!was_multiline && description.size() != 0) /* if wasn't yet separated */
+ description += '\n'; /* do it now */
+ description += header;
+ description += "\n-----\n";
+ description += content;
+ if (content[strlen(content) - 1] != '\n')
+ description += '\n';
+ was_multiline = 1;
+ }
+}
+
+string make_description_bz(const map_crash_report_t& pCrashReport)
+{
+ string description;
+
+ map_crash_report_t::const_iterator it;
+ map_crash_report_t::const_iterator end = pCrashReport.end();
+
+ bool was_multiline = 0;
+ it = pCrashReport.find(CD_REPRODUCE);
+ if (it != end && it->second[CD_CONTENT] != "1.\n2.\n3.\n")
+ {
+ add_content(was_multiline, description, "How to reproduce", it->second[CD_CONTENT].c_str());
+ }
+
+ it = pCrashReport.find(CD_COMMENT);
+ if (it != end)
+ {
+ add_content(was_multiline, description, "Comment", it->second[CD_CONTENT].c_str());
+ }
+
+ it = pCrashReport.begin();
+ for (; it != end; it++)
+ {
+ const string &filename = it->first;
+ const string &type = it->second[CD_TYPE];
+ const string &content = it->second[CD_CONTENT];
+ if (type == CD_TXT)
+ {
+ if (filename != CD_UUID
+ && filename != FILENAME_ARCHITECTURE
+ && filename != FILENAME_RELEASE
+ && filename != CD_REPRODUCE
+ && filename != CD_COMMENT
+ ) {
+ add_content(was_multiline, description, filename.c_str(), content.c_str());
+ }
+ }
+ else if (type == CD_ATT)
+ {
+ add_content(was_multiline, description, "Attached file", filename.c_str());
+ }
+ }
+
+ return description;
+}
+
+string make_description_logger(const map_crash_report_t& pCrashReport)
+{
+ string description;
+ string long_description;
+
+ map_crash_report_t::const_iterator it = pCrashReport.begin();
+ for (; it != pCrashReport.end(); it++)
+ {
+ const string &filename = it->first;
+ const string &type = it->second[CD_TYPE];
+ const string &content = it->second[CD_CONTENT];
+ if (type == CD_TXT
+ || type == CD_ATT
+ || type == CD_BIN
+ ) {
+ if (content == "1.\n2.\n3.\n")
+ continue; /* user did not change default "How to reproduce" */
+
+ bool was_multiline = 0;
+ string tmp;
+ add_content(was_multiline, tmp, filename.c_str(), content.c_str());
+
+ if (was_multiline)
+ {
+ if (long_description.size() != 0)
+ long_description += '\n';
+ long_description += tmp;
+ }
+ else
+ {
+ description += tmp;
+ }
+ }
+ }
+
+ if (description.size() != 0 && long_description.size() != 0)
+ {
+ description += '\n';
+ description += long_description;
+ }
+
+ return description;
+}
diff --git a/lib/Utils/skip_whitespace.cpp b/lib/Utils/skip_whitespace.cpp
new file mode 100644
index 00000000..816928bf
--- /dev/null
+++ b/lib/Utils/skip_whitespace.cpp
@@ -0,0 +1,22 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+#include "abrtlib.h"
+
+char* skip_whitespace(const char *s)
+{
+ /* NB: isspace('\0') returns 0 */
+ while (isspace(*s)) ++s;
+
+ return (char *) s;
+}
+
+char* skip_non_whitespace(const char *s)
+{
+ while (*s && !isspace(*s)) ++s;
+
+ return (char *) s;
+}
diff --git a/lib/Utils/time.cpp b/lib/Utils/time.cpp
new file mode 100644
index 00000000..37ade2cc
--- /dev/null
+++ b/lib/Utils/time.cpp
@@ -0,0 +1,65 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Utility routines.
+ *
+ * Licensed under GPL version 2, see file LICENSE in this tarball for details.
+ */
+#include "abrtlib.h"
+
+#define ENABLE_MONOTONIC_SYSCALL 1
+
+#if ENABLE_MONOTONIC_SYSCALL
+
+#include <sys/syscall.h>
+/* Old glibc (< 2.3.4) does not provide this constant. We use syscall
+ * directly so this definition is safe. */
+#ifndef CLOCK_MONOTONIC
+#define CLOCK_MONOTONIC 1
+#endif
+
+/* libc has incredibly messy way of doing this,
+ * typically requiring -lrt. We just skip all this mess */
+static void get_mono(struct timespec *ts)
+{
+ if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
+ error_msg_and_die("clock_gettime(MONOTONIC) failed");
+}
+unsigned long long monotonic_ns(void)
+{
+ struct timespec ts;
+ get_mono(&ts);
+ return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+}
+unsigned long long monotonic_us(void)
+{
+ struct timespec ts;
+ get_mono(&ts);
+ return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
+}
+unsigned monotonic_sec(void)
+{
+ struct timespec ts;
+ get_mono(&ts);
+ return ts.tv_sec;
+}
+
+#else
+
+unsigned long long monotonic_ns(void)
+{
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
+}
+unsigned long long monotonic_us(void)
+{
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return tv.tv_sec * 1000000ULL + tv.tv_usec;
+}
+unsigned monotonic_sec(void)
+{
+ return time(NULL);
+}
+
+#endif
diff --git a/lib/Utils/xconnect.cpp b/lib/Utils/xconnect.cpp
new file mode 100644
index 00000000..746edd63
--- /dev/null
+++ b/lib/Utils/xconnect.cpp
@@ -0,0 +1,416 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Utility routines.
+ *
+ * Connect to host at port using address resolution from getaddrinfo
+ *
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ */
+
+#include "abrtlib.h"
+#include <sys/socket.h> /* netinet/in.h needs it */
+#include <netinet/in.h>
+#include <net/if.h>
+#include <sys/un.h>
+#include <netdb.h>
+
+#define ENABLE_FEATURE_IPV6 1
+#define ENABLE_FEATURE_PREFER_IPV4_ADDRESS 1
+
+static const int const_int_1 = 1;
+
+void setsockopt_reuseaddr(int fd)
+{
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
+}
+int setsockopt_broadcast(int fd)
+{
+ return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
+}
+int setsockopt_bindtodevice(int fd, const char *iface)
+{
+ int r;
+ struct ifreq ifr;
+ strncpy(ifr.ifr_name, iface, IFNAMSIZ);
+ /* NB: passing (iface, strlen(iface) + 1) does not work!
+ * (maybe it works on _some_ kernels, but not on 2.6.26)
+ * Actually, ifr_name is at offset 0, and in practice
+ * just giving char[IFNAMSIZ] instead of struct ifreq works too.
+ * But just in case it's not true on some obscure arch... */
+ r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
+ if (r)
+ perror_msg("can't bind to interface %s", iface);
+ return r;
+}
+
+len_and_sockaddr* get_sock_lsa(int fd)
+{
+ len_and_sockaddr lsa;
+ len_and_sockaddr *lsa_ptr;
+
+ lsa.len = LSA_SIZEOF_SA;
+ if (getsockname(fd, &lsa.u.sa, &lsa.len) != 0)
+ return NULL;
+
+ lsa_ptr = (len_and_sockaddr *)xzalloc(LSA_LEN_SIZE + lsa.len);
+ if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
+ lsa_ptr->len = lsa.len;
+ getsockname(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
+ } else {
+ memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
+ }
+ return lsa_ptr;
+}
+
+void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
+{
+ if (connect(s, s_addr, addrlen) < 0) {
+ close(s);
+ if (s_addr->sa_family == AF_INET)
+ perror_msg_and_die("%s (%s)",
+ "cannot connect to remote host",
+ inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
+ perror_msg_and_die("cannot connect to remote host");
+ }
+}
+
+/* Return port number for a service.
+ * If "port" is a number use it as the port.
+ * If "port" is a name it is looked up in /etc/services,
+ * if it isnt found return default_port
+ */
+unsigned lookup_port(const char *port, const char *protocol, unsigned default_port)
+{
+ unsigned port_nr = default_port;
+ if (port) {
+ int old_errno;
+ char *end;
+
+ /* Since this is a lib function, we're not allowed to reset errno to 0.
+ * Doing so could break an app that is deferring checking of errno. */
+ old_errno = errno;
+ errno = 0;
+ port_nr = strtoul(port, &end, 10);
+ if (errno || *end || port_nr > 65535) {
+ struct servent *tserv = getservbyname(port, protocol);
+ port_nr = default_port;
+ if (tserv)
+ port_nr = ntohs(tserv->s_port);
+ }
+ errno = old_errno;
+ }
+ return (uint16_t)port_nr;
+}
+
+int get_nport(const struct sockaddr *sa)
+{
+#if ENABLE_FEATURE_IPV6
+ if (sa->sa_family == AF_INET6) {
+ return ((struct sockaddr_in6*)sa)->sin6_port;
+ }
+#endif
+ if (sa->sa_family == AF_INET) {
+ return ((struct sockaddr_in*)sa)->sin_port;
+ }
+ /* What? UNIX socket? IPX?? :) */
+ return -1;
+}
+
+void set_nport(len_and_sockaddr *lsa, unsigned port)
+{
+#if ENABLE_FEATURE_IPV6
+ if (lsa->u.sa.sa_family == AF_INET6) {
+ lsa->u.sin6.sin6_port = port;
+ return;
+ }
+#endif
+ if (lsa->u.sa.sa_family == AF_INET) {
+ lsa->u.sin.sin_port = port;
+ return;
+ }
+ /* What? UNIX socket? IPX?? :) */
+}
+
+/* We hijack this constant to mean something else */
+/* It doesn't hurt because we will remove this bit anyway */
+#define DIE_ON_ERROR AI_CANONNAME
+
+/* host: "1.2.3.4[:port]", "www.google.com[:port]"
+ * port: if neither of above specifies port # */
+static len_and_sockaddr* str2sockaddr(
+ const char *host, int port,
+ sa_family_t af,
+ int ai_flags)
+{
+ int rc;
+ len_and_sockaddr *r;
+ struct addrinfo *result = NULL;
+ struct addrinfo *used_res;
+ const char *org_host = host; /* only for error msg */
+ const char *cp;
+ struct addrinfo hint;
+
+ r = NULL;
+
+ /* Ugly parsing of host:addr */
+ if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
+ /* Even uglier parsing of [xx]:nn */
+ host++;
+ cp = strchr(host, ']');
+ if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
+ /* Malformed: must be [xx]:nn or [xx] */
+ error_msg("bad address '%s'", org_host);
+ if (ai_flags & DIE_ON_ERROR)
+ xfunc_die();
+ return NULL;
+ }
+ } else {
+ cp = strrchr(host, ':');
+ if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
+ /* There is more than one ':' (e.g. "::1") */
+ cp = NULL; /* it's not a port spec */
+ }
+ }
+ if (cp) { /* points to ":" or "]:" */
+ int sz = cp - host + 1;
+ char *hbuf = (char*)alloca(sz);
+ hbuf[--sz] = '\0';
+ host = strncpy(hbuf, host, sz);
+ if (ENABLE_FEATURE_IPV6 && *cp != ':') {
+ cp++; /* skip ']' */
+ if (*cp == '\0') /* [xx] without port */
+ goto skip;
+ }
+ cp++; /* skip ':' */
+ char *end;
+ errno = 0;
+ port = strtoul(cp, &end, 10);
+ if (errno || *end || (unsigned)port > 0xffff) {
+ error_msg("bad port spec '%s'", org_host);
+ if (ai_flags & DIE_ON_ERROR)
+ xfunc_die();
+ return NULL;
+ }
+ skip: ;
+ }
+
+ memset(&hint, 0 , sizeof(hint));
+#if !ENABLE_FEATURE_IPV6
+ hint.ai_family = AF_INET; /* do not try to find IPv6 */
+#else
+ hint.ai_family = af;
+#endif
+ /* Needed. Or else we will get each address thrice (or more)
+ * for each possible socket type (tcp,udp,raw...): */
+ hint.ai_socktype = SOCK_STREAM;
+ hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
+ rc = getaddrinfo(host, NULL, &hint, &result);
+ if (rc || !result) {
+ error_msg("bad address '%s'", org_host);
+ if (ai_flags & DIE_ON_ERROR)
+ xfunc_die();
+ goto ret;
+ }
+ used_res = result;
+#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
+ while (1) {
+ if (used_res->ai_family == AF_INET)
+ break;
+ used_res = used_res->ai_next;
+ if (!used_res) {
+ used_res = result;
+ break;
+ }
+ }
+#endif
+ r = (len_and_sockaddr *)xmalloc(offsetof(len_and_sockaddr, u.sa) + used_res->ai_addrlen);
+ r->len = used_res->ai_addrlen;
+ memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
+ set_nport(r, htons(port));
+ ret:
+ freeaddrinfo(result);
+ return r;
+}
+#if !ENABLE_FEATURE_IPV6
+#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
+#endif
+
+#if ENABLE_FEATURE_IPV6
+len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af)
+{
+ return str2sockaddr(host, port, af, 0);
+}
+
+len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
+{
+ return str2sockaddr(host, port, af, DIE_ON_ERROR);
+}
+#endif
+
+len_and_sockaddr* host2sockaddr(const char *host, int port)
+{
+ return str2sockaddr(host, port, AF_UNSPEC, 0);
+}
+
+len_and_sockaddr* xhost2sockaddr(const char *host, int port)
+{
+ return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
+}
+
+len_and_sockaddr* xdotted2sockaddr(const char *host, int port)
+{
+ return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
+}
+
+#undef xsocket_type
+int xsocket_type(len_and_sockaddr **lsap, int family, int sock_type)
+{
+ len_and_sockaddr *lsa;
+ int fd;
+ int len;
+
+#if ENABLE_FEATURE_IPV6
+ if (family == AF_UNSPEC) {
+ fd = socket(AF_INET6, sock_type, 0);
+ if (fd >= 0) {
+ family = AF_INET6;
+ goto done;
+ }
+ family = AF_INET;
+ }
+#endif
+ fd = xsocket(family, sock_type, 0);
+ len = sizeof(struct sockaddr_in);
+#if ENABLE_FEATURE_IPV6
+ if (family == AF_INET6) {
+ done:
+ len = sizeof(struct sockaddr_in6);
+ }
+#endif
+ lsa = (len_and_sockaddr *)xzalloc(offsetof(len_and_sockaddr, u.sa) + len);
+ lsa->len = len;
+ lsa->u.sa.sa_family = family;
+ *lsap = lsa;
+ return fd;
+}
+
+int xsocket_stream(len_and_sockaddr **lsap)
+{
+ return xsocket_type(lsap, AF_UNSPEC, SOCK_STREAM);
+}
+
+static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
+{
+ int fd;
+ len_and_sockaddr *lsa;
+
+ if (bindaddr && bindaddr[0]) {
+ lsa = xdotted2sockaddr(bindaddr, port);
+ /* user specified bind addr dictates family */
+ fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
+ } else {
+ fd = xsocket_type(&lsa, AF_UNSPEC, sock_type);
+ set_nport(lsa, htons(port));
+ }
+ setsockopt_reuseaddr(fd);
+ xbind(fd, &lsa->u.sa, lsa->len);
+ free(lsa);
+ return fd;
+}
+
+int create_and_bind_stream_or_die(const char *bindaddr, int port)
+{
+ return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
+}
+
+int create_and_bind_dgram_or_die(const char *bindaddr, int port)
+{
+ return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
+}
+
+
+int create_and_connect_stream_or_die(const char *peer, int port)
+{
+ int fd;
+ len_and_sockaddr *lsa;
+
+ lsa = xhost2sockaddr(peer, port);
+ fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
+ setsockopt_reuseaddr(fd);
+ xconnect(fd, &lsa->u.sa, lsa->len);
+ free(lsa);
+ return fd;
+}
+
+int xconnect_stream(const len_and_sockaddr *lsa)
+{
+ int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
+ xconnect(fd, &lsa->u.sa, lsa->len);
+ return fd;
+}
+
+/* We hijack this constant to mean something else */
+/* It doesn't hurt because we will add this bit anyway */
+#define IGNORE_PORT NI_NUMERICSERV
+static char* sockaddr2str(const struct sockaddr *sa, int flags)
+{
+ char host[128];
+ char serv[16];
+ int rc;
+ socklen_t salen;
+
+ salen = LSA_SIZEOF_SA;
+#if ENABLE_FEATURE_IPV6
+ if (sa->sa_family == AF_INET)
+ salen = sizeof(struct sockaddr_in);
+ if (sa->sa_family == AF_INET6)
+ salen = sizeof(struct sockaddr_in6);
+#endif
+ rc = getnameinfo(sa, salen,
+ host, sizeof(host),
+ /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
+ serv, sizeof(serv),
+ /* do not resolve port# into service _name_ */
+ flags | NI_NUMERICSERV
+ );
+ if (rc)
+ return NULL;
+ if (flags & IGNORE_PORT)
+ return xstrdup(host);
+#if ENABLE_FEATURE_IPV6
+ if (sa->sa_family == AF_INET6) {
+ if (strchr(host, ':')) /* heh, it's not a resolved hostname */
+ return xasprintf("[%s]:%s", host, serv);
+ /*return xasprintf("%s:%s", host, serv);*/
+ /* - fall through instead */
+ }
+#endif
+ /* For now we don't support anything else, so it has to be INET */
+ /*if (sa->sa_family == AF_INET)*/
+ return xasprintf("%s:%s", host, serv);
+ /*return xstrdup(host);*/
+}
+
+char* xmalloc_sockaddr2host(const struct sockaddr *sa)
+{
+ return sockaddr2str(sa, 0);
+}
+
+char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
+{
+ return sockaddr2str(sa, IGNORE_PORT);
+}
+
+char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
+{
+ return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
+}
+char* xmalloc_sockaddr2dotted(const struct sockaddr *sa)
+{
+ return sockaddr2str(sa, NI_NUMERICHOST);
+}
+
+char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
+{
+ return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
+}
diff --git a/lib/Utils/xfuncs.cpp b/lib/Utils/xfuncs.cpp
index d256c195..97c2f763 100644
--- a/lib/Utils/xfuncs.cpp
+++ b/lib/Utils/xfuncs.cpp
@@ -5,6 +5,22 @@
*/
#include "abrtlib.h"
+/* Turn on nonblocking I/O on a fd */
+int ndelay_on(int fd)
+{
+ return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL) | O_NONBLOCK);
+}
+
+int ndelay_off(int fd)
+{
+ return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL) & ~O_NONBLOCK);
+}
+
+int close_on_exec_on(int fd)
+{
+ return fcntl(fd, F_SETFD, FD_CLOEXEC);
+}
+
// Die if we can't allocate size bytes of memory.
void* xmalloc(size_t size)
{
@@ -305,3 +321,50 @@ int is_regular_file(struct dirent *dent, const char *dirname)
return r == 0 && S_ISREG(statbuf.st_mode);
}
+
+/* Is it "." or ".."? */
+/* abrtlib candidate */
+bool dot_or_dotdot(const char *filename)
+{
+ if (filename[0] != '.') return false;
+ if (filename[1] == '\0') return true;
+ if (filename[1] != '.') return false;
+ if (filename[2] == '\0') return true;
+ return false;
+}
+
+/* Find out if the last character of a string matches the one given.
+ * Don't underrun the buffer if the string length is 0.
+ */
+char *last_char_is(const char *s, int c)
+{
+ if (s && *s) {
+ s += strlen(s) - 1;
+ if ((unsigned char)*s == c)
+ return (char*)s;
+ }
+ return NULL;
+}
+
+std::string concat_path_file(const char *path, const char *filename)
+{
+ char *lc;
+
+ while (*filename == '/')
+ filename++;
+ lc = last_char_is(path, '/');
+ return ssprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename);
+}
+
+bool string_to_bool(const char *s)
+{
+ if (s[0] == '1' && s[1] == '\0')
+ return true;
+ if (strcasecmp(s, "on") == 0)
+ return true;
+ if (strcasecmp(s, "yes") == 0)
+ return true;
+ if (strcasecmp(s, "true") == 0)
+ return true;
+ return false;
+}