summaryrefslogtreecommitdiffstats
path: root/src/CLI
diff options
context:
space:
mode:
authorKarel Klic <kklic@redhat.com>2009-11-09 13:55:20 +0100
committerKarel Klic <kklic@redhat.com>2009-11-09 13:55:20 +0100
commit19f406b4930c931a932dc6930762e8e12e29ce8b (patch)
treeb08468bf9a010cbce0ecd000a0279222f0d4b4e7 /src/CLI
parent801ab58f32f2cb7dca3352b721a07c83705a0287 (diff)
parent8fa9a6ecd247454ab758efecf818d8067455c778 (diff)
downloadabrt-19f406b4930c931a932dc6930762e8e12e29ce8b.tar.gz
abrt-19f406b4930c931a932dc6930762e8e12e29ce8b.tar.xz
abrt-19f406b4930c931a932dc6930762e8e12e29ce8b.zip
merge
Diffstat (limited to 'src/CLI')
-rw-r--r--src/CLI/CLI.cpp118
-rw-r--r--src/CLI/dbus.cpp79
-rw-r--r--src/CLI/report.cpp50
-rw-r--r--src/CLI/run-command.cpp62
-rw-r--r--src/CLI/run-command.h2
5 files changed, 174 insertions, 137 deletions
diff --git a/src/CLI/CLI.cpp b/src/CLI/CLI.cpp
index 18e99dcf..edc8df00 100644
--- a/src/CLI/CLI.cpp
+++ b/src/CLI/CLI.cpp
@@ -24,16 +24,16 @@
#include "report.h"
#include "dbus.h"
#if HAVE_CONFIG_H
-#include <config.h>
+# include <config.h>
#endif
#if HAVE_LOCALE_H
-#include <locale.h>
+# include <locale.h>
#endif
#if ENABLE_NLS
-#include <libintl.h>
-#define _(S) gettext(S)
+# include <libintl.h>
+# define _(S) gettext(S)
#else
-#define _(S) (S)
+# define _(S) (S)
#endif
/* Program options */
@@ -56,31 +56,31 @@ static void print_crash_infos(vector_crash_infos_t& pCrashInfos, int pMode)
map_crash_info_t& info = pCrashInfos[ii];
if (pMode == OPT_GET_LIST_FULL || info.find(CD_REPORTED)->second[CD_CONTENT] != "1")
{
- const char *timestr = info[CD_TIME][CD_CONTENT].c_str();
- long time = strtol(timestr, 0, 10);
- if (time == 0)
- error_msg_and_die("Error while converting time string.");
-
- char timeloc[256];
- int success = strftime(timeloc, 128, "%c", localtime(&time));
- if (!success)
- error_msg_and_die("Error while converting time to string.");
-
- printf(_("%u.\n"
- "\tUID : %s\n"
- "\tUUID : %s\n"
- "\tPackage : %s\n"
- "\tExecutable : %s\n"
- "\tCrash Time : %s\n"
- "\tCrash Count: %s\n"),
- ii,
- info[CD_UID][CD_CONTENT].c_str(),
- info[CD_UUID][CD_CONTENT].c_str(),
- info[CD_PACKAGE][CD_CONTENT].c_str(),
- info[CD_EXECUTABLE][CD_CONTENT].c_str(),
- timeloc,
- info[CD_COUNT][CD_CONTENT].c_str()
- );
+ const char *timestr = info[CD_TIME][CD_CONTENT].c_str();
+ long time = strtol(timestr, NULL, 10);
+ if (time == 0)
+ error_msg_and_die("Error while converting time string.");
+
+ char timeloc[256];
+ int success = strftime(timeloc, 128, "%c", localtime(&time));
+ if (!success)
+ error_msg_and_die("Error while converting time to string.");
+
+ printf(_("%u.\n"
+ "\tUID : %s\n"
+ "\tUUID : %s\n"
+ "\tPackage : %s\n"
+ "\tExecutable : %s\n"
+ "\tCrash Time : %s\n"
+ "\tCrash Count: %s\n"),
+ ii,
+ info[CD_UID][CD_CONTENT].c_str(),
+ info[CD_UUID][CD_CONTENT].c_str(),
+ info[CD_PACKAGE][CD_CONTENT].c_str(),
+ info[CD_EXECUTABLE][CD_CONTENT].c_str(),
+ timeloc,
+ info[CD_COUNT][CD_CONTENT].c_str()
+ );
}
}
}
@@ -111,21 +111,21 @@ static char *progname(char *argv0)
/* Prints abrt-cli version and some help text. */
static void usage(char *argv0)
{
- char *name = progname(argv0);
- printf("%s " VERSION "\n\n", name);
-
- /* Message has embedded tabs. */
- printf(_("Usage: %s [OPTION]\n\n"
- "Startup:\n"
- " -V, --version display the version of %s and exit\n"
- " -?, --help print this help\n\n"
- "Actions:\n"
- " --get-list print list of crashes which are not reported yet\n"
- " --get-list-full print list of all crashes\n"
- " --report UUID create and send a report\n"
- " --report-always UUID create and send a report without asking\n"
- " --delete UUID remove crash\n"),
- name, name);
+ char *name = progname(argv0);
+ printf("%s " VERSION "\n\n", name);
+
+ /* Message has embedded tabs. */
+ printf(_("Usage: %s [OPTION]\n\n"
+ "Startup:\n"
+ " -V, --version display the version of %s and exit\n"
+ " -?, --help print this help\n\n"
+ "Actions:\n"
+ " --get-list print list of crashes which are not reported yet\n"
+ " --get-list-full print list of all crashes\n"
+ " --report UUID create and send a report\n"
+ " --report-always UUID create and send a report without asking\n"
+ " --delete UUID remove crash\n"),
+ name, name);
}
int main(int argc, char** argv)
@@ -154,21 +154,21 @@ int main(int argc, char** argv)
case OPT_GET_LIST_FULL:
if (op == -1)
break;
- error_msg(_("You must specify exactly one operation."));
+ error_msg(_("You must specify exactly one operation."));
return 1;
- case -1: /* end of options */
- if (op != -1) /* if some operation was specified... */
- break;
+ case -1: /* end of options */
+ if (op != -1) /* if some operation was specified... */
+ break;
/* fall through */
default:
- case '?':
+ case '?':
case OPT_HELP:
- usage(argv[0]);
- return 1;
+ usage(argv[0]);
+ return 1;
case 'V':
- case OPT_VERSION:
- printf("%s " VERSION "\n", progname(argv[0]));
- return 0;
+ case OPT_VERSION:
+ printf("%s " VERSION "\n", progname(argv[0]));
+ return 0;
}
if (c == -1)
break;
@@ -195,11 +195,11 @@ int main(int argc, char** argv)
break;
}
case OPT_REPORT:
- report(uuid, false);
- break;
+ report(uuid, false);
+ break;
case OPT_REPORT_ALWAYS:
- report(uuid, true);
- break;
+ report(uuid, true);
+ break;
case OPT_DELETE:
{
call_DeleteDebugDump(uuid);
diff --git a/src/CLI/dbus.cpp b/src/CLI/dbus.cpp
index 42222d15..50ca651b 100644
--- a/src/CLI/dbus.cpp
+++ b/src/CLI/dbus.cpp
@@ -27,7 +27,7 @@ DBusConnection* s_dbus_conn;
/* helpers */
static DBusMessage* new_call_msg(const char* method)
{
- DBusMessage* msg = dbus_message_new_method_call(CC_DBUS_NAME, CC_DBUS_PATH, CC_DBUS_IFACE, method);
+ DBusMessage* msg = dbus_message_new_method_call(ABRTD_DBUS_NAME, ABRTD_DBUS_PATH, ABRTD_DBUS_IFACE, method);
if (!msg)
die_out_of_memory();
return msg;
@@ -35,18 +35,73 @@ static DBusMessage* new_call_msg(const char* method)
static DBusMessage* send_get_reply_and_unref(DBusMessage* msg)
{
- DBusError err;
- dbus_error_init(&err);
- DBusMessage *reply;
- reply = dbus_connection_send_with_reply_and_block(s_dbus_conn,
- msg, /*timeout*/ -1, &err);
- if (reply == NULL)
- {
- //TODO: analyse err
+ dbus_uint32_t serial;
+ if (TRUE != dbus_connection_send(s_dbus_conn, msg, &serial))
error_msg_and_die("Error sending DBus message");
- }
dbus_message_unref(msg);
- return reply;
+
+ while (true)
+ {
+ DBusMessage *received = dbus_connection_pop_message(s_dbus_conn);
+ if (!received)
+ {
+ if (FALSE == dbus_connection_read_write(s_dbus_conn, -1))
+ error_msg_and_die("DBus connection closed");
+ continue;
+ }
+
+ /* Debugging */
+ /*
+ const char *sender = dbus_message_get_sender(received);
+ if (sender)
+ printf("sender: %s\n", sender);
+ const char *path = dbus_message_get_path(received);
+ if (path)
+ printf("path: %s\n", path);
+ const char *member = dbus_message_get_member(received);
+ if (member)
+ printf("member: %s\n", member);
+ const char *interface = dbus_message_get_interface(received);
+ if (interface)
+ printf("interface: %s\n", interface);
+ const char *destination = dbus_message_get_destination(received);
+ if (destination)
+ printf("destination: %s\n", destination);
+ */
+
+ DBusError err;
+ dbus_error_init(&err);
+
+ if (dbus_message_is_signal(received, ABRTD_DBUS_IFACE, "Update"))
+ {
+ const char *update_msg;
+ if (!dbus_message_get_args(received, &err,
+ DBUS_TYPE_STRING, &update_msg,
+ DBUS_TYPE_INVALID))
+ {
+ error_msg_and_die("dbus Update message: arguments mismatch");
+ }
+ printf(">> %s\n", update_msg);
+ }
+ else if (dbus_message_is_signal(received, ABRTD_DBUS_IFACE, "Warning"))
+ {
+ const char *warning_msg;
+ if (!dbus_message_get_args(received, &err,
+ DBUS_TYPE_STRING, &warning_msg,
+ DBUS_TYPE_INVALID))
+ {
+ error_msg_and_die("dbus Update message: arguments mismatch");
+ }
+ printf(">! %s\n", warning_msg);
+ }
+ else if (dbus_message_get_type(received) == DBUS_MESSAGE_TYPE_METHOD_RETURN &&
+ dbus_message_get_reply_serial(received) == serial)
+ {
+ return received;
+ }
+
+ dbus_message_unref(received);
+ }
}
vector_crash_infos_t call_GetCrashInfos()
@@ -141,5 +196,5 @@ void handle_dbus_err(bool error_flag, DBusError *err)
error_msg_and_die(
"error requesting DBus name %s, possible reasons: "
"abrt run by non-root; dbus config is incorrect",
- CC_DBUS_NAME);
+ ABRTD_DBUS_NAME);
}
diff --git a/src/CLI/report.cpp b/src/CLI/report.cpp
index b9a13914..ac80e481 100644
--- a/src/CLI/report.cpp
+++ b/src/CLI/report.cpp
@@ -21,13 +21,13 @@
#include "abrtlib.h"
#include "DebugDump.h" // FILENAME_* defines
#if HAVE_CONFIG_H
-#include <config.h>
+# include <config.h>
#endif
#if ENABLE_NLS
-#include <libintl.h>
-#define _(S) gettext(S)
+# include <libintl.h>
+# define _(S) gettext(S)
#else
-#define _(S) (S)
+# define _(S) (S)
#endif
/* Field separator for the crash report file that is edited by user. */
@@ -45,7 +45,7 @@ char *trim(char *str)
// Remove leading spaces.
char *ibuf;
for (ibuf = str; *ibuf && isspace(*ibuf); ++ibuf)
- ;
+ continue;
if (str != ibuf)
memmove(str, ibuf, ibuf - str);
@@ -79,7 +79,7 @@ static char *escape(const char *str)
{
if (*ptr == '#')
++count;
- if (*ptr == '\\' && *(ptr + 1) == '#')
+ if (*ptr == '\\' && ptr[1] == '#')
++count;
}
@@ -89,9 +89,7 @@ static char *escape(const char *str)
// Copy the input string to the resultant string, and escape all
// occurences of \# and #.
- char *result = (char*)malloc(strlen(str) + 1 + count);
- if (!result)
- error_msg_and_die("Memory error while escaping a field.");
+ char *result = (char*)xmalloc(strlen(str) + 1 + count);
const char *src = str;
char *dest = result;
@@ -136,10 +134,9 @@ static void remove_comments_and_unescape(char *str)
++src;
continue;
}
- else if (*src == '\\' &&
- (*(src + 1) == '#' ||
- (*(src + 1) == '\\' && *(src + 2) == '#')))
- {
+ if (*src == '\\'
+ && (src[1] == '#' || (src[1] == '\\' && src[2] == '#'))
+ ) {
++src; // Unescape escaped char.
}
}
@@ -189,7 +186,7 @@ static void write_crash_report_field(FILE *fp, const map_crash_report_t &report,
* If the report is successfully stored to the file, a zero value is returned.
* On failure, nonzero value is returned.
*/
-static int write_crash_report(const map_crash_report_t &report, FILE *fp)
+static void write_crash_report(const map_crash_report_t &report, FILE *fp)
{
fprintf(fp, "# Please check this report. Lines starting with '#' will be ignored.\n"
"# Lines starting with '%%----' separate fields, please do not delete them.\n\n");
@@ -210,8 +207,6 @@ static int write_crash_report(const map_crash_report_t &report, FILE *fp)
write_crash_report_field(fp, report, FILENAME_PACKAGE, _("# Package"));
write_crash_report_field(fp, report, FILENAME_REASON, _("# Reason of crash"));
write_crash_report_field(fp, report, FILENAME_RELEASE, _("# Release string of the operating system"));
-
- return 0;
}
/*
@@ -303,7 +298,7 @@ static int read_crash_report(map_crash_report_t &report, const char *text)
}
/* Runs external editor. */
-int launch_editor(const char *path)
+static int launch_editor(const char *path)
{
const char *editor, *terminal;
@@ -323,9 +318,10 @@ int launch_editor(const char *path)
if (!editor)
editor = "vi";
- const char *args[6];
- args[0] = editor;
- args[1] = path;
+ char *args[3];
+ args[0] = (char*)editor;
+ args[1] = (char*)path;
+ args[2] = NULL;
run_command(args);
return 0;
@@ -349,14 +345,14 @@ int report(const char *uuid, bool always)
int fd = mkstemp(filename);
if (fd == -1)
{
- error_msg("could not generate temporary file name");
+ error_msg("can't generate temporary file name");
return 1;
}
FILE *fp = fdopen(fd, "w");
if (!fp)
{
- error_msg("could not open '%s' to save the crash report", filename);
+ error_msg("can't open '%s' to save the crash report", filename);
return 1;
}
@@ -364,7 +360,7 @@ int report(const char *uuid, bool always)
if (fclose(fp))
{
- error_msg("could not close '%s'", filename);
+ error_msg("can't close '%s'", filename);
return 2;
}
@@ -375,7 +371,7 @@ int report(const char *uuid, bool always)
fp = fopen(filename, "r");
if (!fp)
{
- error_msg("could not open '%s' to read the crash report", filename);
+ error_msg("can't open '%s' to read the crash report", filename);
return 1;
}
@@ -383,10 +379,10 @@ int report(const char *uuid, bool always)
long size = ftell(fp);
fseek(fp, 0, SEEK_SET);
- char *text = (char*)malloc(size + 1);
+ char *text = (char*)xmalloc(size + 1);
if (fread(text, 1, size, fp) != size)
{
- error_msg("could not read '%s'", filename);
+ error_msg("can't read '%s'", filename);
return 1;
}
text[size] = '\0';
@@ -403,7 +399,7 @@ int report(const char *uuid, bool always)
free(text);
if (unlink(filename) != 0) // Delete the tempfile.
- error_msg("could not unlink %s: %s", filename, strerror(errno));
+ perror_msg("can't unlink %s", filename);
// Report only if the user is sure.
printf(_("Do you want to send the report? [y/N]: "));
diff --git a/src/CLI/run-command.cpp b/src/CLI/run-command.cpp
index 80184cfa..8f9d63ec 100644
--- a/src/CLI/run-command.cpp
+++ b/src/CLI/run-command.cpp
@@ -23,65 +23,51 @@
http://git.kernel.org/?p=git/git.git;a=blob;f=run-command.c;hb=HEAD
*/
-struct child_process
+static pid_t start_command(char **argv)
{
- const char **argv;
- pid_t pid;
-};
-
-static int start_command(struct child_process *cmd)
-{
- cmd->pid = fork();
- if (cmd->pid == 0)
+ pid_t pid = vfork();
+ if (pid < 0)
+ {
+ perror_msg_and_die("Can't fork");
+ }
+ if (pid == 0)
{ // new process
- execvp(cmd->argv[0], (char *const*)cmd->argv);
+ execvp(argv[0], argv);
exit(127);
}
- if (cmd->pid < 0)
- {
- error_msg_and_die("Unable to fork for %s: %s", cmd->argv[0], strerror(errno));
- return -1;
- }
- return 0;
+ return pid;
}
-static int finish_command(struct child_process *cmd)
+static int finish_command(pid_t pid, char **argv)
{
pid_t waiting;
- int status, code = -1;
- while ((waiting = waitpid(cmd->pid, &status, 0)) < 0 && errno == EINTR)
- ; /* nothing */
-
+ int status;
+ while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
+ continue;
if (waiting < 0)
- error_msg_and_die("waitpid for %s failed: %s", cmd->argv[0], strerror(errno));
- else if (waiting != cmd->pid)
- error_msg_and_die("waitpid is confused (%s)", cmd->argv[0]);
- else if (WIFSIGNALED(status))
+ perror_msg_and_die("waitpid");
+
+ int code = -1;
+ if (WIFSIGNALED(status))
{
code = WTERMSIG(status);
- error_msg("%s died of signal %d", cmd->argv[0], code);
+ error_msg("'%s' killed by signal %d", argv[0], code);
+ code += 128; /* shells use this convention for deaths by signal */
}
- else if (WIFEXITED(status))
+ else /* if (WIFEXITED(status)) */
{
code = WEXITSTATUS(status);
if (code == 127)
{
- code = -1;
- error_msg_and_die("cannot run %s: %s", cmd->argv[0], strerror(ENOENT));
+ error_msg_and_die("Can't run '%s'", argv[0]);
}
}
- else
- error_msg_and_die("waitpid is confused (%s)", cmd->argv[0]);
return code;
}
-int run_command(const char **argv)
+int run_command(char **argv)
{
- struct child_process cmd;
- cmd.argv = argv;
- int code = start_command(&cmd);
- if (code)
- return code;
- return finish_command(&cmd);
+ pid_t pid = start_command(argv);
+ return finish_command(pid, argv);
}
diff --git a/src/CLI/run-command.h b/src/CLI/run-command.h
index 45a85af5..71391579 100644
--- a/src/CLI/run-command.h
+++ b/src/CLI/run-command.h
@@ -18,6 +18,6 @@
#ifndef ABRT_CLI_RUN_COMMAND_H
#define ABRT_CLI_RUN_COMMAND_H
-int run_command(const char **argv);
+int run_command(char **argv);
#endif