summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZdenek Prikryl <zprikryl@redhat.com>2009-06-10 10:01:39 +0200
committerZdenek Prikryl <zprikryl@redhat.com>2009-06-10 10:01:39 +0200
commitda6687d1b76eb8f83ff10010c765e6959a20f3fe (patch)
tree9a387cb3199a7f788678b83594db8b1c55385253
parent79a688a1e928817f3d95289e9afdd07f4b830b64 (diff)
downloadabrt-da6687d1b76eb8f83ff10010c765e6959a20f3fe.tar.gz
abrt-da6687d1b76eb8f83ff10010c765e6959a20f3fe.tar.xz
abrt-da6687d1b76eb8f83ff10010c765e6959a20f3fe.zip
new simple command line client
-rw-r--r--inc/CrashTypesSocket.h (renamed from lib/CommLayer/SocketCrashTypes.h)13
-rw-r--r--src/TUI/ABRTSocket.cpp130
-rw-r--r--src/TUI/ABRTSocket.h29
-rw-r--r--src/TUI/Cmd.cpp178
-rw-r--r--src/TUI/Makefile.am3
5 files changed, 347 insertions, 6 deletions
diff --git a/lib/CommLayer/SocketCrashTypes.h b/inc/CrashTypesSocket.h
index cf5f9dd9..0c761545 100644
--- a/lib/CommLayer/SocketCrashTypes.h
+++ b/inc/CrashTypesSocket.h
@@ -5,6 +5,13 @@
#include <sstream>
#include <stdlib.h>
+
+#define MESSAGE_DELETE_DEBUG_DUMP "(DELETE_DEBUG_DUMP)"
+#define MESSAGE_GET_CRASH_INFOS "(GET_CRASH_INFOS)"
+#define MESSAGE_REPORT "(REPORT)"
+#define MESSAGE_CREATE_REPORT "(CREATE_REPORT)"
+#define MESSAGE_END_MARKER 23
+
inline std::string crash_data_to_string(const map_crash_data_t& pCrashData)
{
std::stringstream sCD;
@@ -111,12 +118,6 @@ inline vector_crash_infos_t string_to_crash_infos(const std::string& pMessage)
std::string message = pMessage;
int len;
- if (!message.compare(0, sizeof("(CRASH_INFOS)"), "(CRASH_INFOS)"))
- {
- return vci;
- }
- message.erase(0, sizeof("(CRASH_INFOS)") - 1);
-
while (message != "")
{
map_crash_info_t crash_info = string_to_crash_data(message, len);
diff --git a/src/TUI/ABRTSocket.cpp b/src/TUI/ABRTSocket.cpp
new file mode 100644
index 00000000..dac690ad
--- /dev/null
+++ b/src/TUI/ABRTSocket.cpp
@@ -0,0 +1,130 @@
+#include "ABRTSocket.h"
+#include "ABRTException.h"
+#include "SocketCrashTypes.h"
+
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <string.h>
+
+CABRTSocket::CABRTSocket() :
+ m_nSocket(-1)
+{}
+
+CABRTSocket::~CABRTSocket()
+{
+ DisConnect();
+}
+#include <iostream>
+void CABRTSocket::Send(const std::string& pMessage)
+{
+ int ret = 0;
+ int len = pMessage.length();
+ int offset = 0;
+ char* message = new char[len + 3];
+ memcpy(message, pMessage.c_str(), len);
+ message[len] = MESSAGE_END_MARKER;
+ message[len + 1] = '\n';
+ message[len + 2] = '\0';
+
+ while (ret != strlen(message + offset))
+ {
+ offset += ret;
+ ret = send(m_nSocket, message + offset, strlen(message + offset), 0);
+ if (ret == -1)
+ {
+ throw CABRTException(EXCEP_FATAL, "CABRTSocket::Send(): Can not send data");
+ }
+ }
+ delete[] message;
+}
+
+void CABRTSocket::Recv(std::string& pMessage)
+{
+ std::string message;
+ bool receivingMessage = true;
+ char buff[1];
+ int ret;
+
+ pMessage = "";
+ while (receivingMessage)
+ {
+ ret = recv(m_nSocket, buff, 1, 0);
+ if (ret == -1)
+ {
+ throw CABRTException(EXCEP_FATAL, "CABRTSocket::Recv(): Can not recv data");
+ }
+ else if (ret == 0)
+ {
+ throw CABRTException(EXCEP_FATAL, "CABRTSocket::Recv(): Connection closed by abrt server");
+ }
+
+ message += buff[0];
+
+ if (message.length() > 2 &&
+ message[message.length() - 2] == MESSAGE_END_MARKER &&
+ message[message.length() - 1] == '\n')
+ {
+ receivingMessage = false;
+ message = message.substr(0, message.length() - 2);
+ }
+ }
+ pMessage = message;
+}
+
+
+void CABRTSocket::Connect(const std::string& pPath)
+{
+ int len;
+ struct sockaddr_un remote;
+ if ((m_nSocket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+ {
+ throw CABRTException(EXCEP_FATAL, "CABRTSocket::Connect(): Can not create socket");
+ }
+ remote.sun_family = AF_UNIX;
+ strcpy(remote.sun_path, pPath.c_str());
+ len = strlen(remote.sun_path) + sizeof(remote.sun_family);
+ if (connect(m_nSocket, (struct sockaddr *)&remote, len) == -1)
+ {
+ throw CABRTException(EXCEP_FATAL, "CABRTSocket::Connect(): Can not connect to remote");
+ }
+}
+
+void CABRTSocket::DisConnect()
+{
+ if (m_nSocket != -1)
+ {
+ close(m_nSocket);
+ }
+}
+
+vector_crash_infos_t CABRTSocket::GetCrashInfos()
+{
+ std::string message = MESSAGE_GET_CRASH_INFOS;
+ Send(message);
+ Recv(message);
+ message.erase(0, sizeof(MESSAGE_GET_CRASH_INFOS) - 1);
+ return string_to_crash_infos(message);
+}
+
+map_crash_report_t CABRTSocket::CreateReport(const std::string &pUUID)
+{
+ std::string message = MESSAGE_CREATE_REPORT + pUUID;
+ Send(message);
+ Recv(message);
+ message.erase(0, sizeof(MESSAGE_CREATE_REPORT) - 1);
+ return string_to_crash_report(message);
+}
+
+void CABRTSocket::Report(map_crash_report_t pReport)
+{
+ std::string message = MESSAGE_REPORT + crash_report_to_string(pReport);
+ Send(message);
+}
+
+void CABRTSocket::DeleteDebugDump(const std::string& pUUID)
+{
+ std::string message = MESSAGE_DELETE_DEBUG_DUMP + pUUID;
+ Send(message);
+}
diff --git a/src/TUI/ABRTSocket.h b/src/TUI/ABRTSocket.h
new file mode 100644
index 00000000..ef255ac7
--- /dev/null
+++ b/src/TUI/ABRTSocket.h
@@ -0,0 +1,29 @@
+#ifndef ABRTSOCKET_H_
+#define ABRTSOCKET_H_
+
+#include <string>
+
+#include "CrashTypes.h"
+
+class CABRTSocket
+{
+ private:
+ int m_nSocket;
+
+ void Send(const std::string& pMessage);
+ void Recv(std::string& pMessage);
+
+ public:
+ CABRTSocket();
+ ~CABRTSocket();
+
+ void Connect(const std::string& pPath);
+ void DisConnect();
+
+ vector_crash_infos_t GetCrashInfos();
+ map_crash_report_t CreateReport(const std::string &pUUID);
+ void Report(map_crash_report_t pReport);
+ void DeleteDebugDump(const std::string& pUUID);
+};
+
+#endif /* ABRTSOCKET_H_ */
diff --git a/src/TUI/Cmd.cpp b/src/TUI/Cmd.cpp
new file mode 100644
index 00000000..e9c27879
--- /dev/null
+++ b/src/TUI/Cmd.cpp
@@ -0,0 +1,178 @@
+#include "ABRTSocket.h"
+#include "ABRTException.h"
+#include <iostream>
+
+#include <string.h>
+
+
+#define SOCKET_PATH "/tmp/abrt.socket"
+
+typedef enum {HELP,
+ GET_LIST,
+ GET_LIST_FULL,
+ REPORT,
+ REPORT_ALWAYS,
+ DELETE} mode_t;
+
+typedef struct param_s
+{
+ mode_t m_Mode;
+ char* m_sUUID;
+} param_t;
+
+void print_usage(char* pProgramName)
+{
+ std::cout << pProgramName << " [OPTION]" << std::endl << std::endl;
+ std::cout << "[OPTION]" << std::endl;
+ std::cout << "\t--help - prints this text" << std::endl;
+ std::cout << "\t--get-list - prints list of crashes which are not reported" << std::endl;
+ std::cout << "\t--get-list-full - prints list of all crashes" << std::endl;
+ std::cout << "\t--report <uuid> - create and send a report" << std::endl;
+ std::cout << "\t--report-always <uuid> - create and send a report without asking" << std::endl;
+ std::cout << "\t--delete <uuid> - delete crash" << std::endl;
+}
+
+void parse_args(int argc, char** argv, param_t& param)
+{
+ if (argc == 2)
+ {
+ if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "--version"))
+ {
+ param.m_Mode = HELP;
+ }
+ else if (!strcmp(argv[1], "--get-list"))
+ {
+ param.m_Mode = GET_LIST;
+ }
+ else if (!strcmp(argv[1], "--get-list-full"))
+ {
+ param.m_Mode = GET_LIST_FULL;
+ }
+ else
+ {
+ param.m_Mode = HELP;
+ }
+ }
+ else if (argc == 3)
+ {
+ if (!strcmp(argv[1], "--report"))
+ {
+ param.m_Mode = REPORT;
+ param.m_sUUID = argv[2];
+ }
+ else if (!strcmp(argv[1], "--report-always"))
+ {
+ param.m_Mode = REPORT_ALWAYS;
+ param.m_sUUID = argv[2];
+ }
+ else if (!strcmp(argv[1], "--delete"))
+ {
+ param.m_Mode = DELETE;
+ param.m_sUUID = argv[2];
+ }
+ else
+ {
+ param.m_Mode = HELP;
+ }
+ }
+ else
+ {
+ param.m_Mode = HELP;
+ }
+}
+
+void print_crash_infos(const vector_crash_infos_t& pCrashInfos,
+ const mode_t& pMode)
+{
+ unsigned int ii;
+ for (ii = 0; ii < pCrashInfos.size(); ii++)
+ {
+ if (pCrashInfos[ii].find(CD_REPORTED)->second[CD_CONTENT] != "1" || pMode == GET_LIST_FULL)
+ {
+ std::cout << ii << ". " << std::endl;
+ std::cout << "\tUID : " << pCrashInfos[ii].find(CD_UID)->second[CD_CONTENT] << std::endl;
+ std::cout << "\tUUID : " << pCrashInfos[ii].find(CD_UUID)->second[CD_CONTENT] << std::endl;
+ std::cout << "\tPackage : " << pCrashInfos[ii].find(CD_PACKAGE)->second[CD_CONTENT] << std::endl;
+ std::cout << "\tExecutable: " << pCrashInfos[ii].find(CD_EXECUTABLE)->second[CD_CONTENT] << std::endl;
+ std::cout << "\tCrash time: " << pCrashInfos[ii].find(CD_TIME)->second[CD_CONTENT] << std::endl;
+ std::cout << "\tCrash Rate: " << pCrashInfos[ii].find(CD_COUNT)->second[CD_CONTENT] << std::endl;
+ }
+ }
+}
+
+void print_crash_report(const map_crash_report_t& pCrashReport)
+{
+ map_crash_report_t::const_iterator it;
+ for (it = pCrashReport.begin(); it != pCrashReport.end(); it++)
+ {
+ if (it->second[CD_TYPE] != CD_SYS)
+ {
+ std::cout << std::endl << it->first << std::endl;
+ std::cout << "-----" << std::endl;
+ std::cout << it->second[CD_CONTENT] << std::endl;
+ }
+ }
+}
+
+int main(int argc, char** argv)
+{
+ CABRTSocket ABRTSocket;
+ vector_crash_infos_t ci;
+ map_crash_report_t cr;
+ param_t param;
+ std::string answer = "n";
+
+ parse_args(argc, argv, param);
+
+ if (param.m_Mode == HELP)
+ {
+ print_usage(argv[0]);
+ return 1;
+ }
+
+ try
+ {
+ ABRTSocket.Connect(SOCKET_PATH);
+
+ switch (param.m_Mode)
+ {
+ case GET_LIST:
+ ci = ABRTSocket.GetCrashInfos();
+ print_crash_infos(ci, GET_LIST);
+ break;
+ case GET_LIST_FULL:
+ ci = ABRTSocket.GetCrashInfos();
+ print_crash_infos(ci, GET_LIST_FULL);
+ break;
+ case REPORT:
+ cr = ABRTSocket.CreateReport(param.m_sUUID);
+ print_crash_report(cr);
+ std::cout << std::endl << "Do you want to send the report? [y/n]: ";
+ std::flush(std::cout);
+ std::cin >> answer;
+ if (answer == "Y" || answer == "y")
+ {
+ ABRTSocket.Report(cr);
+ }
+ break;
+ case REPORT_ALWAYS:
+ cr = ABRTSocket.CreateReport(param.m_sUUID);
+ ABRTSocket.Report(cr);
+ break;
+ case DELETE:
+ ABRTSocket.DeleteDebugDump(param.m_sUUID);
+ break;
+ default:
+ print_usage(argv[0]);
+ break;
+ }
+
+ ABRTSocket.DisConnect();
+ }
+ catch (CABRTException& e)
+ {
+ std::cout << e.what() << std::endl;
+ }
+
+ return 0;
+}
diff --git a/src/TUI/Makefile.am b/src/TUI/Makefile.am
new file mode 100644
index 00000000..bd89309c
--- /dev/null
+++ b/src/TUI/Makefile.am
@@ -0,0 +1,3 @@
+bin_PROGRAMS = abrt-cmd
+abrt_cmd_SOURCES = ABRTSocket.cpp ABRTSocket.h Cmd.cpp CrashTypes.h SocketCrashTypes.h
+abrt_cmd_CPPFLAGS = -I$(srcdir)/../../inc \ No newline at end of file