blob: 484445fcb8011797584ba98447e84c88eb4a0b91 [file] [log] [blame]
Chris Sosae4a86032010-06-16 17:08:34 -07001// 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
Ken Mixter777484c2010-07-23 16:22:44 -07005#ifndef _CRASH_REPORTER_USER_COLLECTOR_H_
6#define _CRASH_REPORTER_USER_COLLECTOR_H_
Chris Sosae4a86032010-06-16 17:08:34 -07007
8#include <string>
9
Ken Mixter03403162010-08-18 15:23:16 -070010#include "crash-reporter/crash_collector.h"
Ken Mixter777484c2010-07-23 16:22:44 -070011#include "gtest/gtest_prod.h" // for FRIEND_TEST
Chris Sosae4a86032010-06-16 17:08:34 -070012
13class FilePath;
Ken Mixter03403162010-08-18 15:23:16 -070014class SystemLogging;
Chris Sosae4a86032010-06-16 17:08:34 -070015
16// User crash collector.
Ken Mixter03403162010-08-18 15:23:16 -070017class UserCollector : public CrashCollector {
Chris Sosae4a86032010-06-16 17:08:34 -070018 public:
Chris Sosae4a86032010-06-16 17:08:34 -070019 UserCollector();
20
21 // Initialize the user crash collector for detection of crashes,
22 // given a crash counting function, the path to this executable,
23 // metrics collection enabled oracle, and system logger facility.
Ken Mixter777484c2010-07-23 16:22:44 -070024 // Crash detection/reporting is not enabled until Enable is called.
25 // |generate_diagnostics| is used to indicate whether or not to try
26 // to generate a minidump from crashes.
Chris Sosae4a86032010-06-16 17:08:34 -070027 void Initialize(CountCrashFunction count_crash,
28 const std::string &our_path,
29 IsFeedbackAllowedFunction is_metrics_allowed,
Ken Mixter777484c2010-07-23 16:22:44 -070030 SystemLogging *logger,
31 bool generate_diagnostics);
Chris Sosae4a86032010-06-16 17:08:34 -070032
33 virtual ~UserCollector();
34
35 // Enable collection.
36 bool Enable() { return SetUpInternal(true); }
37
38 // Disable collection.
39 bool Disable() { return SetUpInternal(false); }
40
Ken Mixter777484c2010-07-23 16:22:44 -070041 // Handle a specific user crash. Returns true on success.
42 bool HandleCrash(int signal, int pid, const char *force_exec);
Chris Sosae4a86032010-06-16 17:08:34 -070043
44 // Set (override the default) core file pattern.
45 void set_core_pattern_file(const std::string &pattern) {
46 core_pattern_file_ = pattern;
47 }
48
49 private:
50 friend class UserCollectorTest;
Ken Mixter777484c2010-07-23 16:22:44 -070051 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPath);
52 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPid);
53 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesOK);
Ken Mixter777484c2010-07-23 16:22:44 -070054 FRIEND_TEST(UserCollectorTest, GetIdFromStatus);
55 FRIEND_TEST(UserCollectorTest, GetProcessPath);
56 FRIEND_TEST(UserCollectorTest, GetSymlinkTarget);
57 FRIEND_TEST(UserCollectorTest, GetUserInfoFromName);
58
59 // Enumeration to pass to GetIdFromStatus. Must match the order
60 // that the kernel lists IDs in the status file.
61 enum IdKind {
62 kIdReal = 0, // uid and gid
63 kIdEffective = 1, // euid and egid
64 kIdSet = 2, // suid and sgid
65 kIdFileSystem = 3, // fsuid and fsgid
66 kIdMax
67 };
Chris Sosae4a86032010-06-16 17:08:34 -070068
69 std::string GetPattern(bool enabled) const;
70 bool SetUpInternal(bool enabled);
71
Ken Mixter777484c2010-07-23 16:22:44 -070072 FilePath GetProcessPath(pid_t pid);
73 bool GetSymlinkTarget(const FilePath &symlink,
74 FilePath *target);
75 bool GetExecutableBaseNameFromPid(uid_t pid,
76 std::string *base_name);
77 bool GetIdFromStatus(const char *prefix,
78 IdKind kind,
79 const std::string &status_contents,
80 int *id);
Ken Mixter777484c2010-07-23 16:22:44 -070081 bool CopyOffProcFiles(pid_t pid, const FilePath &process_map);
Ken Mixter777484c2010-07-23 16:22:44 -070082 // Determines the crash directory for given pid based on pid's owner,
83 // and creates the directory if necessary with appropriate permissions.
84 // Returns true whether or not directory needed to be created, false on
85 // any failure.
86 bool GetCreatedCrashDirectory(pid_t pid,
87 FilePath *crash_file_path);
Ken Mixter777484c2010-07-23 16:22:44 -070088 bool CopyStdinToCoreFile(const FilePath &core_path);
89 bool ConvertCoreToMinidump(const FilePath &core_path,
90 const FilePath &procfs_directory,
91 const FilePath &minidump_path,
92 const FilePath &temp_directory);
93 bool GenerateDiagnostics(pid_t pid, const std::string &exec_name);
94
95 bool generate_diagnostics_;
Chris Sosae4a86032010-06-16 17:08:34 -070096 std::string core_pattern_file_;
Chris Sosae4a86032010-06-16 17:08:34 -070097 std::string our_path_;
98 bool initialized_;
Ken Mixter777484c2010-07-23 16:22:44 -070099
100 static const char *kUserId;
101 static const char *kGroupId;
Chris Sosae4a86032010-06-16 17:08:34 -0700102};
103
Ken Mixter777484c2010-07-23 16:22:44 -0700104#endif // _CRASH_REPORTER_USER_COLLECTOR_H_