summaryrefslogtreecommitdiffstats
path: root/src/Hooks/abrt-pyhook-helper.cpp
blob: cd17e4024e9316e4814538460fd222288e356524 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
    abrt-hook-python.cpp - writes data to the /var/cache/abrt directory
    with SUID bit

    Copyright (C) 2009  RedHat inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <getopt.h>
#include <unistd.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>
#endif

#define MAX_BT_SIZE (1024*1024)

static char *pid;
static char *executable;
static char *uuid;
static char *cmdline;

int main(int argc, char** argv)
{
  // 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' },
    { 0 },
  };
  int opt;
  while ((opt = getopt_long(argc, argv, "p:e:u:c:l:", longopts, NULL)) != -1)
  {
    switch (opt)
    {
    case 'p':
      pid = optarg;
      break;
    case 'e':
      executable = optarg;
      break;
    case 'u':
      uuid = optarg;
      break;
    case 'c':
      cmdline = optarg;
      break;
    default:
 usage:
      error_msg_and_die(
                "Usage: abrt-hook-python [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"
      );
    }
  }
  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), DEBUG_DUMPS_DIR"/pyhook-%ld-%s",
	   (long)time(NULL), pid);

  CDebugDump dd;
  dd.Create(path, geteuid());
  dd.SaveText(FILENAME_ANALYZER, "Python");
  if (executable)
    dd.SaveText(FILENAME_EXECUTABLE, executable);
  if (cmdline)
    dd.SaveText("cmdline", cmdline);
  if (uuid)
    dd.SaveText("uuid", uuid);

  char uid[sizeof(int) * 3 + 2];
  sprintf(uid, "%d", (int)getuid());
  dd.SaveText("uid", uid);

  dd.SaveText("backtrace", bt);
  free(bt);
  dd.Close();

  return 0;
}