OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 // | |
5 // Simple wrapper and basic configuration of Google breakpad. We try | |
6 // to avoid using any unnecessary code (like libbase) to make it as | |
7 // non-intrusive as possible to link in this library. | |
8 | |
9 #include <errno.h> | |
10 #include <pwd.h> | |
11 #include <stddef.h> | |
12 #include <sys/stat.h> | |
13 #include <sys/types.h> | |
14 #include <unistd.h> | |
15 | |
16 #include "client/linux/handler/exception_handler.h" | |
17 #include "common/linux/linux_libc_support.h" | |
18 #include "common/linux/linux_syscall_support.h" | |
19 #include "crash/crash_dumper.h" | |
20 | |
21 // Define sys_mkdir (sys_open passes mode_t as int, so we do too). | |
22 LSS_INLINE _syscall2(int, mkdir, const char *, pathname, int, mode); | |
23 | |
24 static const char kDefaultUser[] = "chronos"; | |
25 static const char kSystemCrashParentPath[] = "/var/spool"; | |
26 static const char kSystemCrashPath[] = "/var/spool/crash"; | |
27 static const char kUserCrashParentPath[] = "/home/chronos/user"; | |
28 static const char kUserCrashPath[] = "/home/chronos/user/crash"; | |
29 | |
30 // Pointers to paths, set when crash dumping is enabled. | |
31 static const char *s_crash_path; | |
32 static const char *s_crash_parent_path; | |
33 static mode_t s_dump_directory_mode; | |
34 | |
35 static bool s_any_crashes_occurred = false; | |
36 static google_breakpad::ExceptionHandler *s_breakpad_handler; | |
37 | |
38 static bool s_enabled = false; | |
39 | |
40 #define CHECK_CONDITION(_cond, _message) \ | |
41 do { if (!(_cond)) { fputs(_message"\n", stderr); abort(); } } while(false) | |
42 | |
43 // This static object will cause anything that links in this object to get | |
44 // crash handling for the duration of this file's scope. | |
45 static CrashDumper g_crash_dumper; | |
46 | |
47 | |
48 // Prepare the crash path. Must avoid allocating memory. | |
49 static bool PrepareCrashPath() { | |
50 struct kernel_stat buf; | |
51 if (sys_stat(s_crash_path, &buf) < 0) { | |
52 // Dump directory does not exist, so create it and its parent now, | |
53 // at the time of the crash. | |
54 sys_mkdir(s_crash_parent_path, 755); | |
55 sys_mkdir(s_crash_path, s_dump_directory_mode); | |
56 } | |
57 return sys_stat(s_crash_path, &buf) == 0; | |
58 } | |
59 | |
60 // Use FilterCallback to avoid recursive crashing. | |
61 // TODO(kmixter): Also use it to avoid enqueuing too many crash dumps | |
62 // system wide - if we get in a crash/restart loop we don't want the entire | |
63 // stateful partition to be filled up. | |
64 static bool FilterCallback(void *) { | |
65 // This function runs in a compromised context - a crash has already | |
66 // occurred so memory allocation and libc should be avoided. | |
67 bool old_any_crashes_occured = s_any_crashes_occurred; | |
68 s_any_crashes_occurred = true; | |
69 // The crash path may have been removed or mounted-over, so make sure | |
70 // there is a container directory. If it fails, not much we can do. | |
71 PrepareCrashPath(); | |
72 return !old_any_crashes_occured; | |
73 } | |
74 | |
75 static bool GetEffectiveUser(std::string *result) { | |
76 char storage[256]; | |
77 struct passwd passwd_storage; | |
78 struct passwd *passwd_result = NULL; | |
79 | |
80 if (getpwuid_r(geteuid(), &passwd_storage, storage, sizeof(storage), | |
81 &passwd_result) != 0 || passwd_result == NULL) { | |
82 return false; | |
83 } | |
84 | |
85 *result = passwd_result->pw_name; | |
86 return true; | |
87 } | |
88 | |
89 void CrashDumper::Enable() { | |
90 CHECK_CONDITION(!s_enabled, "Crash handling already enabled"); | |
91 | |
92 std::string name; | |
93 CHECK_CONDITION(GetEffectiveUser(&name), | |
94 "getpwuid_r failed, cannot enable crash reporting"); | |
95 | |
96 if (name == kDefaultUser) { | |
97 // Crashes as "chronos" when the user is not yet logged in will | |
98 // still be recorded to /home/chronos/user/crash in the | |
99 // stateful partition (outside cryptohome). These will eventually | |
100 // be uploaded when no user is logged in. | |
101 s_crash_path = kUserCrashPath; | |
102 s_crash_parent_path = kUserCrashParentPath; | |
103 s_dump_directory_mode = 0755; | |
104 } else { | |
105 s_crash_path = kSystemCrashPath; | |
106 s_crash_parent_path = kSystemCrashParentPath; | |
107 // Make the dump directory sticky so any UID can write to | |
108 // it but not remove another UID's crashes. | |
109 s_dump_directory_mode = 01777; | |
110 } | |
111 | |
112 CHECK_CONDITION(PrepareCrashPath(), "Unable to create crash path"); | |
113 | |
114 // Begin collecting crashes | |
115 s_breakpad_handler = new google_breakpad::ExceptionHandler( | |
116 s_crash_path, | |
117 FilterCallback, | |
118 NULL, // No minidump callback - sending happens asynchronously to writing | |
119 NULL, // No callback context necessary | |
120 true); // Install handler now. | |
121 | |
122 s_enabled = true; | |
123 } | |
124 | |
125 bool CrashDumper::IsEnabled() { | |
126 return s_enabled; | |
127 } | |
128 | |
129 void CrashDumper::Disable() { | |
130 CHECK_CONDITION(s_enabled, "Crash handling was not enabled"); | |
131 delete s_breakpad_handler; | |
132 s_breakpad_handler = NULL; | |
133 s_crash_path = NULL; | |
134 s_crash_parent_path = NULL; | |
135 s_enabled = false; | |
136 } | |
OLD | NEW |