summaryrefslogtreecommitdiffstats
path: root/src/hooks/abrt-install-ccpp-hook.in
blob: 9c7df7508e2a2c961257e1906b58fefaec4dbc5d (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
#!/bin/bash
# Install coredump handler which saves segfault data

# For debugging

dry_run=false
verbose=false

DEFAULT_LOCATION=/var/spool/abrt

DUMP_LOCATION=`cat @sysconfdir@/abrt/abrt.conf | grep '^[ 	]*DumpLocation'`
DUMP_LOCATION=${DUMP_LOCATION#*DumpLocation*=}
DUMP_LOCATION=${DUMP_LOCATION// /}

if [ "$DUMP_LOCATION" = "" ]; then
    DUMP_LOCATION=$DEFAULT_LOCATION
fi

PATTERN_FILE="/proc/sys/kernel/core_pattern"
SAVED_PATTERN_DIR="/var/run/abrt"
SAVED_PATTERN_FILE="/var/run/abrt/saved_core_pattern"
HOOK_BIN="/usr/libexec/abrt-hook-ccpp"
# Must match percent_specifiers[] order in abrt-hook-ccpp.c:
PATTERN="|$HOOK_BIN $DUMP_LOCATION %s %c %p %u %g %t %e"
# Same, but with bogus "executable name" parameter
PATTERN1="|$HOOK_BIN $DUMP_LOCATION %s %c %p %u %g %t e"

# core_pipe_limit specifies how many dump_helpers can run at the same time
# 0 - means unlimited, but it's not guaranteed that /proc/<pid> of crashing
#     process will be available for dump_helper.
# 4 - means that 4 dump_helpers can run at the same time (the rest will also
#     run, but they will fail to read /proc/<pid>).
#
# This should be enough for ABRT, we can miss some crashes, but what are
# the odds that more processes crash at the same time? And moreover,
# do people want to save EVERY ONE of the crashes when they have
# a crash storm? I don't think so.
# The value of 4 has been recommended by nhorman.
#
CORE_PIPE_LIMIT_FILE="/proc/sys/kernel/core_pipe_limit"
CORE_PIPE_LIMIT="4"

start() {
	cur=`cat "$PATTERN_FILE"`
	cur_first=`printf "%s" "$cur" | sed 's/ .*//'`

	# Is there a %e (executable name) in old pattern anywhere?
	if test x"${cur#.*%e}" = x"${cur}"; then
		# No. Can use PATTERN with less risk of overflow
		# on expansion (executable names can be LONG).
		# Overflow would cause kernel to abort coredump. BAD.
		PATTERN="$PATTERN1"
	fi

	$verbose && printf "cur:'%s'\n" "$cur"
	# Is it already installed?
	if test x"$cur_first" != x"|$HOOK_BIN"; then   # no
		# It is not installed
		mkdir -p -- "$SAVED_PATTERN_DIR"
		printf "%s\n" "$cur" >"$SAVED_PATTERN_FILE"
		# Install new handler
		$verbose && printf "Installing to %s:'%s'\n" "$PATTERN_FILE" "$PATTERN"
		$dry_run || echo "$PATTERN" >"$PATTERN_FILE"

		# Check core_pipe_limit and change it if it's 0,
		# otherwise the abrt-hook-ccpp won't be able to read /proc/<pid>
		# of the crashing process
		if test x"`cat "$CORE_PIPE_LIMIT_FILE"`" = x"0"; then
			echo "$CORE_PIPE_LIMIT" >"$CORE_PIPE_LIMIT_FILE"
		fi
	fi
}

stop() {
	if test -f "$SAVED_PATTERN_FILE"; then
		$verbose && printf "Restoring to %s:'%s'\n" "$PATTERN_FILE" "`cat "$SAVED_PATTERN_FILE"`"
		$dry_run || cat "$SAVED_PATTERN_FILE" >"$PATTERN_FILE"
	fi
}

status() {
	cur=`cat "$PATTERN_FILE"`
	cur_first=`printf "%s" "$cur" | sed 's/ .*//'`
	# Is it already installed?
	if test x"$cur_first" = x"|$HOOK_BIN"; then   # yes
		$verbose && printf "Installed\n"
		return 0
	else
		$verbose && printf "Not installed\n"
		return 1
	fi
}

case "$1" in
install)
	start
	;;
uninstall)
	stop
	;;
is-installed)
	status
	;;
*)
	echo $"Usage: $0 {install|uninstall|is-installed}"
	return 2
esac