| 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 <syslog.h> | |
| 15 #include <unistd.h> | |
| 16 | |
| 17 #include "client/linux/handler/exception_handler.h" | |
| 18 #include "common/linux/linux_libc_support.h" | |
| 19 #include "common/linux/linux_syscall_support.h" | |
| 20 #include "crash-dumper/crash_dumper.h" | |
| 21 | |
| 22 // Define sys_mkdir (sys_open passes mode_t as int, so we do too). | |
| 23 LSS_INLINE _syscall2(int, mkdir, const char *, pathname, int, mode); | |
| 24 | |
| 25 static const char kDefaultUser[] = "chronos"; | |
| 26 static const char kSystemCrashParentPath[] = "/var/spool"; | |
| 27 static const char kSystemCrashPath[] = "/var/spool/crash"; | |
| 28 static const char kUserCrashParentPath[] = "/home/chronos/user"; | |
| 29 static const char kUserCrashPath[] = "/home/chronos/user/crash"; | |
| 30 | |
| 31 // Pointers to paths, set when crash dumping is enabled. | |
| 32 static const char *s_crash_path; | |
| 33 static const char *s_crash_parent_path; | |
| 34 static mode_t s_dump_directory_mode; | |
| 35 | |
| 36 static bool s_any_crashes_occurred = false; | |
| 37 static google_breakpad::ExceptionHandler *s_breakpad_handler; | |
| 38 | |
| 39 static bool s_enabled = false; | |
| 40 | |
| 41 // Use CHECK_CONDITION to abort if a condition is met - the condition | |
| 42 // should only be a condition that would fail if a program was written | |
| 43 // incorrectly. Use REPORT_ERROR for conditions that are legal run-time | |
| 44 // error conditions. | |
| 45 #define CHECK_CONDITION(_cond, _message) \ | |
| 46 do { if (!(_cond)) { fputs(_message"\n", stderr); abort(); } } while(false) | |
| 47 | |
| 48 // Use REPORT_ERROR to report a run-time condition (via syslog) that should | |
| 49 // not abort the entire program running, but still indicate a failure to | |
| 50 // be investigated. | |
| 51 #define REPORT_ERROR(_message) \ | |
| 52 do { syslog(LOG_ERR, _message); fputs(_message"\n", stderr); } while(false) | |
| 53 | |
| 54 // This static object will cause anything that links in this object to get | |
| 55 // crash handling for the duration of this file's scope. | |
| 56 static CrashDumper g_crash_dumper; | |
| 57 | |
| 58 | |
| 59 // Prepare the crash path. Must avoid allocating memory. | |
| 60 static bool PrepareCrashPath() { | |
| 61 struct kernel_stat buf; | |
| 62 if (sys_stat(s_crash_path, &buf) < 0) { | |
| 63 // Dump directory does not exist, so create it and its parent now, | |
| 64 // at the time of the crash. | |
| 65 sys_mkdir(s_crash_parent_path, 755); | |
| 66 sys_mkdir(s_crash_path, s_dump_directory_mode); | |
| 67 } | |
| 68 return sys_stat(s_crash_path, &buf) == 0; | |
| 69 } | |
| 70 | |
| 71 // Use FilterCallback to avoid recursive crashing. | |
| 72 // TODO(kmixter): Also use it to avoid enqueuing too many crash dumps | |
| 73 // system wide - if we get in a crash/restart loop we don't want the entire | |
| 74 // stateful partition to be filled up. | |
| 75 static bool FilterCallback(void *) { | |
| 76 // This function runs in a compromised context - a crash has already | |
| 77 // occurred so memory allocation and libc should be avoided. | |
| 78 bool old_any_crashes_occured = s_any_crashes_occurred; | |
| 79 s_any_crashes_occurred = true; | |
| 80 // The crash path may have been removed or mounted-over, so make sure | |
| 81 // there is a container directory. If it fails, not much we can do. | |
| 82 PrepareCrashPath(); | |
| 83 return !old_any_crashes_occured; | |
| 84 } | |
| 85 | |
| 86 static bool GetEffectiveUser(std::string *result) { | |
| 87 char storage[256]; | |
| 88 struct passwd passwd_storage; | |
| 89 struct passwd *passwd_result = NULL; | |
| 90 | |
| 91 if (getpwuid_r(geteuid(), &passwd_storage, storage, sizeof(storage), | |
| 92 &passwd_result) != 0 || passwd_result == NULL) { | |
| 93 return false; | |
| 94 } | |
| 95 | |
| 96 *result = passwd_result->pw_name; | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 void CrashDumper::Enable() { | |
| 101 CHECK_CONDITION(!s_enabled, "Crash handling already enabled"); | |
| 102 | |
| 103 std::string name; | |
| 104 if (!GetEffectiveUser(&name)) { | |
| 105 REPORT_ERROR("getpwuid_r failed, cannot enable crash reporting"); | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 if (name == kDefaultUser) { | |
| 110 // Crashes as "chronos" when the user is not yet logged in will | |
| 111 // still be recorded to /home/chronos/user/crash in the | |
| 112 // stateful partition (outside cryptohome). These will eventually | |
| 113 // be uploaded when no user is logged in. | |
| 114 s_crash_path = kUserCrashPath; | |
| 115 s_crash_parent_path = kUserCrashParentPath; | |
| 116 s_dump_directory_mode = 0755; | |
| 117 } else { | |
| 118 s_crash_path = kSystemCrashPath; | |
| 119 s_crash_parent_path = kSystemCrashParentPath; | |
| 120 // Make the dump directory sticky so any UID can write to | |
| 121 // it but not remove another UID's crashes. | |
| 122 s_dump_directory_mode = 01777; | |
| 123 } | |
| 124 | |
| 125 if (!PrepareCrashPath()) { | |
| 126 REPORT_ERROR("Unable to create crash path"); | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 // Begin collecting crashes | |
| 131 s_breakpad_handler = new google_breakpad::ExceptionHandler( | |
| 132 s_crash_path, | |
| 133 FilterCallback, | |
| 134 NULL, // No minidump callback - sending happens asynchronously to writing | |
| 135 NULL, // No callback context necessary | |
| 136 true); // Install handler now. | |
| 137 | |
| 138 s_enabled = true; | |
| 139 } | |
| 140 | |
| 141 bool CrashDumper::IsEnabled() { | |
| 142 return s_enabled; | |
| 143 } | |
| 144 | |
| 145 void CrashDumper::Disable() { | |
| 146 CHECK_CONDITION(s_enabled, "Crash handling was not enabled"); | |
| 147 delete s_breakpad_handler; | |
| 148 s_breakpad_handler = NULL; | |
| 149 s_crash_path = NULL; | |
| 150 s_crash_parent_path = NULL; | |
| 151 s_enabled = false; | |
| 152 } | |
| OLD | NEW |