diff options
Diffstat (limited to 'src/Hooks/abrt-pyhook-helper.cpp')
-rw-r--r-- | src/Hooks/abrt-pyhook-helper.cpp | 174 |
1 files changed, 79 insertions, 95 deletions
diff --git a/src/Hooks/abrt-pyhook-helper.cpp b/src/Hooks/abrt-pyhook-helper.cpp index e15ff684..8a5a1914 100644 --- a/src/Hooks/abrt-pyhook-helper.cpp +++ b/src/Hooks/abrt-pyhook-helper.cpp @@ -18,118 +18,102 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include <argp.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> + +#include <getopt.h> +/* We can easily get rid of abrtlib (libABRTUtils.so) usage in this file, + * but DebugDump will pull it in anyway */ +#include "abrtlib.h" #include "DebugDump.h" #if HAVE_CONFIG_H -#include <config.h> +# include <config.h> #endif - -const char *argp_program_version = "abrt-pyhook-helper " VERSION; -const char *argp_program_bug_address = "<crash-catcher@lists.fedorahosted.org>"; - -static char doc[] = "abrt-pyhook-helper -- stores crash data to abrt shared directory"; -static struct argp_option options[] = { - {"pid" , 'p', "PID" , 0, "PID of process that caused the crash" }, - {"executable", 'e', "PATH" , 0, "absolute path to the program that crashed" }, - {"uuid" , 'u', "UUID" , 0, "hash generated from the backtrace"}, - {"cmdline" , 'c', "TEXT" , 0, "command line of the crashed program"}, - {"loginuid" , 'l', "UID" , 0, "login UID"}, - { 0 } -}; +#define MAX_BT_SIZE (1024*1024) -struct arguments -{ - char *pid; - char *executable; - char *uuid; - char *cmdline; - char *loginuid; -}; +static char *pid; +static char *executable; +static char *uuid; +static char *cmdline; +static char *loginuid; -static error_t -parse_opt (int key, char *arg, struct argp_state *state) +int main(int argc, char** argv) { - /* Get the input argument from argp_parse, which we - know is a pointer to our arguments structure. */ - struct arguments *arguments = (struct arguments*)state->input; - - switch (key) + // Parse options + static const struct option longopts[] = { + // name , has_arg , flag, val + { "pid" , required_argument, NULL, 'p' }, + { "executable", required_argument, NULL, 'e' }, + { "uuid" , required_argument, NULL, 'u' }, + { "cmdline" , required_argument, NULL, 'c' }, + { "loginuid" , required_argument, NULL, 'l' }, + { 0 }, + }; + int opt; + while ((opt = getopt_long(argc, argv, "p:e:u:c:l:", longopts, NULL)) != -1) { - case 'p': arguments->pid = arg; break; - case 'e': arguments->executable = arg; break; - case 'u': arguments->uuid = arg; break; - case 'c': arguments->cmdline = arg; break; - case 'l': arguments->loginuid = arg; break; - - case ARGP_KEY_ARG: - argp_usage(state); - exit(1); - break; - - case ARGP_KEY_END: - if (!arguments->pid) + switch (opt) { - argp_usage(state); - exit(1); + case 'p': + pid = optarg; + break; + case 'e': + executable = optarg; + break; + case 'u': + uuid = optarg; + break; + case 'c': + cmdline = optarg; + break; + case 'l': + loginuid = optarg; + break; + default: + usage: + error_msg_and_die( + "Usage: abrt-pyhook-helper [OPTIONS] <BACKTRACE\n" + "\nOptions:\n" + " -p,--pid PID PID of process that caused the crash\n" + " -p,--executable PATH absolute path to the program that crashed\n" + " -u,--uuid UUID hash generated from the backtrace\n" + " -c,--cmdline TEXT command line of the crashed program\n" + " -l,--loginuid UID login UID\n" + ); } - break; - - default: - return ARGP_ERR_UNKNOWN; } - return 0; -} - -/* Our argp parser. */ -static struct argp argp = { options, parse_opt, 0, doc }; - -int main(int argc, char** argv) -{ - struct arguments arguments; - argp_parse (&argp, argc, argv, 0, 0, &arguments); + if (!pid) + goto usage; +// is it really ok if other params aren't specified? abrtd might get confused... + + // Read the backtrace from stdin + char *bt = (char*)xmalloc(MAX_BT_SIZE); + ssize_t len = full_read(STDIN_FILENO, bt, MAX_BT_SIZE-1); + if (len < 0) + { + perror_msg_and_die("Read error"); + } + bt[len] = '\0'; + if (len == MAX_BT_SIZE-1) + { + error_msg("Backtrace size limit exceeded, trimming to 1 MB"); + } + // Create directory with the debug dump char path[PATH_MAX]; - snprintf(path, sizeof(path), "%s/pyhook-%ld-%s", DEBUG_DUMPS_DIR, - (long)time(NULL), arguments.pid); + snprintf(path, sizeof(path), DEBUG_DUMPS_DIR"/pyhook-%ld-%s", + (long)time(NULL), pid); CDebugDump dd; dd.Create(path, geteuid()); dd.SaveText(FILENAME_ANALYZER, "Python"); - if (arguments.executable) - dd.SaveText(FILENAME_EXECUTABLE, arguments.executable); - if (arguments.cmdline) - dd.SaveText("cmdline", arguments.cmdline); - if (arguments.uuid) - dd.SaveText("uuid", arguments.uuid); - if (arguments.loginuid) - dd.SaveText("uid", arguments.loginuid); - - // Read the backtrace from stdin. - int c; - int capacity = 1024; - char *bt = (char*)malloc(capacity); - char *btptr = bt; - while ((c = getchar()) != EOF) - { - if (c >= 0 && c <= 255) - *btptr++ = (char)c; - if (btptr - bt >= capacity - 1) - { - capacity *= 2; - bt = (char*)realloc(bt, capacity); - if (!bt) - { - printf("Error while allocating memory for backtrace."); - return 1; - } - } - } - *btptr = '\0'; - + if (executable) + dd.SaveText(FILENAME_EXECUTABLE, executable); + if (cmdline) + dd.SaveText("cmdline", cmdline); + if (uuid) + dd.SaveText("uuid", uuid); + if (loginuid) + dd.SaveText("uid", loginuid); dd.SaveText("backtrace", bt); free(bt); dd.Close(); |