blob: 21ef14b532870c5459cb015591a397f08e81bada [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>
Ken Mixter2953c3a2010-10-18 14:42:20 -07009#include <vector>
Chris Sosae4a86032010-06-16 17:08:34 -070010
Ken Mixter03403162010-08-18 15:23:16 -070011#include "crash-reporter/crash_collector.h"
Ken Mixter777484c2010-07-23 16:22:44 -070012#include "gtest/gtest_prod.h" // for FRIEND_TEST
Chris Sosae4a86032010-06-16 17:08:34 -070013
14class FilePath;
Ken Mixter03403162010-08-18 15:23:16 -070015class SystemLogging;
Chris Sosae4a86032010-06-16 17:08:34 -070016
17// User crash collector.
Ken Mixter03403162010-08-18 15:23:16 -070018class UserCollector : public CrashCollector {
Chris Sosae4a86032010-06-16 17:08:34 -070019 public:
Chris Sosae4a86032010-06-16 17:08:34 -070020 UserCollector();
21
22 // Initialize the user crash collector for detection of crashes,
23 // given a crash counting function, the path to this executable,
24 // metrics collection enabled oracle, and system logger facility.
Ken Mixter777484c2010-07-23 16:22:44 -070025 // Crash detection/reporting is not enabled until Enable is called.
26 // |generate_diagnostics| is used to indicate whether or not to try
27 // to generate a minidump from crashes.
Chris Sosae4a86032010-06-16 17:08:34 -070028 void Initialize(CountCrashFunction count_crash,
29 const std::string &our_path,
30 IsFeedbackAllowedFunction is_metrics_allowed,
Ken Mixter777484c2010-07-23 16:22:44 -070031 SystemLogging *logger,
32 bool generate_diagnostics);
Chris Sosae4a86032010-06-16 17:08:34 -070033
34 virtual ~UserCollector();
35
36 // Enable collection.
37 bool Enable() { return SetUpInternal(true); }
38
39 // Disable collection.
40 bool Disable() { return SetUpInternal(false); }
41
Ken Mixter777484c2010-07-23 16:22:44 -070042 // Handle a specific user crash. Returns true on success.
Ken Mixter1b8fe012011-01-25 13:33:05 -080043 bool HandleCrash(const std::string &crash_attributes,
44 const char *force_exec);
Chris Sosae4a86032010-06-16 17:08:34 -070045
46 // Set (override the default) core file pattern.
47 void set_core_pattern_file(const std::string &pattern) {
48 core_pattern_file_ = pattern;
49 }
50
Ken Mixterc49dbd42010-12-14 17:44:11 -080051 // Set (override the default) core pipe limit file.
52 void set_core_pipe_limit_file(const std::string &path) {
53 core_pipe_limit_file_ = path;
54 }
55
Chris Sosae4a86032010-06-16 17:08:34 -070056 private:
57 friend class UserCollectorTest;
Ken Mixter777484c2010-07-23 16:22:44 -070058 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPath);
59 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPid);
60 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesOK);
Ken Mixterd49d3622011-02-09 18:23:00 -080061 FRIEND_TEST(UserCollectorTest, GetExecutableBaseNameFromPid);
Ken Mixter777484c2010-07-23 16:22:44 -070062 FRIEND_TEST(UserCollectorTest, GetIdFromStatus);
63 FRIEND_TEST(UserCollectorTest, GetProcessPath);
64 FRIEND_TEST(UserCollectorTest, GetSymlinkTarget);
65 FRIEND_TEST(UserCollectorTest, GetUserInfoFromName);
Ken Mixter1b8fe012011-01-25 13:33:05 -080066 FRIEND_TEST(UserCollectorTest, ParseCrashAttributes);
Ken Mixter777484c2010-07-23 16:22:44 -070067
68 // Enumeration to pass to GetIdFromStatus. Must match the order
69 // that the kernel lists IDs in the status file.
70 enum IdKind {
71 kIdReal = 0, // uid and gid
72 kIdEffective = 1, // euid and egid
73 kIdSet = 2, // suid and sgid
74 kIdFileSystem = 3, // fsuid and fsgid
75 kIdMax
76 };
Chris Sosae4a86032010-06-16 17:08:34 -070077
Ken Mixter2953c3a2010-10-18 14:42:20 -070078 static const int kForkProblem = 255;
79
Chris Sosae4a86032010-06-16 17:08:34 -070080 std::string GetPattern(bool enabled) const;
81 bool SetUpInternal(bool enabled);
82
Ken Mixter777484c2010-07-23 16:22:44 -070083 FilePath GetProcessPath(pid_t pid);
84 bool GetSymlinkTarget(const FilePath &symlink,
85 FilePath *target);
86 bool GetExecutableBaseNameFromPid(uid_t pid,
87 std::string *base_name);
88 bool GetIdFromStatus(const char *prefix,
89 IdKind kind,
90 const std::string &status_contents,
91 int *id);
Ken Mixter207694d2010-10-28 15:42:37 -070092
93 void LogCollectionError(const std::string &error_message);
94 void EnqueueCollectionErrorLog(pid_t pid, const std::string &exec_name);
95
Ken Mixter777484c2010-07-23 16:22:44 -070096 bool CopyOffProcFiles(pid_t pid, const FilePath &process_map);
Ken Mixter777484c2010-07-23 16:22:44 -070097 // Determines the crash directory for given pid based on pid's owner,
98 // and creates the directory if necessary with appropriate permissions.
99 // Returns true whether or not directory needed to be created, false on
100 // any failure.
101 bool GetCreatedCrashDirectory(pid_t pid,
Ken Mixter207694d2010-10-28 15:42:37 -0700102 FilePath *crash_file_path,
103 bool *out_of_capacity);
Ken Mixter777484c2010-07-23 16:22:44 -0700104 bool CopyStdinToCoreFile(const FilePath &core_path);
Ken Mixter207694d2010-10-28 15:42:37 -0700105 bool RunCoreToMinidump(const FilePath &core_path,
106 const FilePath &procfs_directory,
107 const FilePath &minidump_path,
108 const FilePath &temp_directory);
109 bool ConvertCoreToMinidump(pid_t pid,
110 const FilePath &container_dir,
111 const FilePath &core_path,
112 const FilePath &minidump_path);
113 bool ConvertAndEnqueueCrash(int pid, const std::string &exec_name,
114 bool *out_of_capacity);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800115 bool ParseCrashAttributes(const std::string &crash_attributes,
116 pid_t *pid, int *signal,
117 std::string *kernel_supplied_name);
Ken Mixter777484c2010-07-23 16:22:44 -0700118
119 bool generate_diagnostics_;
Chris Sosae4a86032010-06-16 17:08:34 -0700120 std::string core_pattern_file_;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800121 std::string core_pipe_limit_file_;
Chris Sosae4a86032010-06-16 17:08:34 -0700122 std::string our_path_;
123 bool initialized_;
Ken Mixter777484c2010-07-23 16:22:44 -0700124
125 static const char *kUserId;
126 static const char *kGroupId;
Chris Sosae4a86032010-06-16 17:08:34 -0700127};
128
Ken Mixter777484c2010-07-23 16:22:44 -0700129#endif // _CRASH_REPORTER_USER_COLLECTOR_H_