Blob Blame Raw
From 883d35109b55928d4c36d3cd6ee262d7cdc5bd4d Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Wed, 15 Jul 2015 10:20:59 +0200
Subject: [PATCH] abrtd: de-prioritize post-create event scripts

The crash processing should not make the computer unusable. It sometimes
happens that the captured data causes abrt scripts to take an inadequate
amount of resources and the computer becomes less responsive.

This patch increases the nice value of post-create processes by 10 (I took
10 because it is the default value of command 'nice'), so those
processes will be scheduled after the more valuable processes.

Related: rhbz#1236422

Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 doc/abrtd.txt                  |  7 +++++++
 src/daemon/abrt-handle-event.c | 19 ++++++++++++++++++-
 src/daemon/abrt-server.c       | 14 ++++++++------
 3 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/doc/abrtd.txt b/doc/abrtd.txt
index b129d3e..32d044b 100644
--- a/doc/abrtd.txt
+++ b/doc/abrtd.txt
@@ -36,6 +36,13 @@ OPTIONS
 -p::
    Add program names to log.
 
+ENVIRONMENT
+-----------
+ABRT_EVENT_NICE::
+  'abrtd' runs its post-mortem processing with the nice value incremented by 10
+  in order to not take too much resources and keep the computer responsive.  If
+  you want to adjust the increment value, use the ABRT_EVENT_NICE environment
+  variable.
 
 CAVEATS
 -------
diff --git a/src/daemon/abrt-handle-event.c b/src/daemon/abrt-handle-event.c
index 4a21aa4..fda21bd 100644
--- a/src/daemon/abrt-handle-event.c
+++ b/src/daemon/abrt-handle-event.c
@@ -403,16 +403,18 @@ int main(int argc, char **argv)
     abrt_init(argv);
 
     const char *program_usage_string = _(
-        "& [-v -i] -e|--event EVENT DIR..."
+        "& [-v -i -n INCREMENT] -e|--event EVENT DIR..."
         );
 
     char *event_name = NULL;
     int interactive = 0; /* must be _int_, OPT_BOOL expects that! */
+    int nice_incr = 0;
 
     struct options program_options[] = {
         OPT__VERBOSE(&g_verbose),
         OPT_STRING('e', "event" , &event_name, "EVENT",  _("Run EVENT on DIR")),
         OPT_BOOL('i', "interactive" , &interactive, _("Communicate directly to the user")),
+        OPT_INTEGER('n',     "nice" , &nice_incr,   _("Increment the nice value by INCREMENT")),
         OPT_END()
     };
 
@@ -423,6 +425,21 @@ int main(int argc, char **argv)
 
     load_abrt_conf();
 
+    const char *const opt_env_nice = getenv("ABRT_EVENT_NICE");
+    if (opt_env_nice != NULL && opt_env_nice[0] != '\0')
+    {
+        log_debug("Using ABRT_EVENT_NICE=%s to increment the nice value", opt_env_nice);
+        nice_incr = xatoi(opt_env_nice);
+    }
+
+    if (nice_incr != 0)
+    {
+        log_debug("Incrementing the nice value by %d", nice_incr);
+        const int ret = nice(nice_incr);
+        if (ret == -1)
+            perror_msg_and_die("Failed to increment the nice value");
+    }
+
     bool post_create = (strcmp(event_name, "post-create") == 0);
     char *dump_dir_name = NULL;
     while (*argv)
diff --git a/src/daemon/abrt-server.c b/src/daemon/abrt-server.c
index d7556e2..9f177e9 100644
--- a/src/daemon/abrt-server.c
+++ b/src/daemon/abrt-server.c
@@ -126,15 +126,17 @@ static int delete_path(const char *dump_dir_name)
 
 static pid_t spawn_event_handler_child(const char *dump_dir_name, const char *event_name, int *fdp)
 {
-    char *args[7];
+    char *args[9];
     args[0] = (char *) LIBEXEC_DIR"/abrt-handle-event";
     /* Do not forward ASK_* messages to parent*/
     args[1] = (char *) "-i";
-    args[2] = (char *) "-e";
-    args[3] = (char *) event_name;
-    args[4] = (char *) "--";
-    args[5] = (char *) dump_dir_name;
-    args[6] = NULL;
+    args[2] = (char *) "--nice";
+    args[3] = (char *) "10";
+    args[4] = (char *) "-e";
+    args[5] = (char *) event_name;
+    args[6] = (char *) "--";
+    args[7] = (char *) dump_dir_name;
+    args[8] = NULL;
 
     int pipeout[2];
     int flags = EXECFLG_INPUT_NUL | EXECFLG_OUTPUT | EXECFLG_QUIET | EXECFLG_ERR2OUT;
-- 
2.4.3