blob: 7d6c7110faff899e60a57a60cb2e797e81fc3a63 [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.
43 bool HandleCrash(int signal, int pid, const char *force_exec);
Chris Sosae4a86032010-06-16 17:08:34 -070044
45 // Set (override the default) core file pattern.
46 void set_core_pattern_file(const std::string &pattern) {
47 core_pattern_file_ = pattern;
48 }
49
50 private:
51 friend class UserCollectorTest;
Ken Mixter777484c2010-07-23 16:22:44 -070052 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPath);
53 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPid);
54 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesOK);
Ken Mixter2953c3a2010-10-18 14:42:20 -070055 FRIEND_TEST(UserCollectorTest, ForkExecAndPipe);
Ken Mixter777484c2010-07-23 16:22:44 -070056 FRIEND_TEST(UserCollectorTest, GetIdFromStatus);
57 FRIEND_TEST(UserCollectorTest, GetProcessPath);
58 FRIEND_TEST(UserCollectorTest, GetSymlinkTarget);
59 FRIEND_TEST(UserCollectorTest, GetUserInfoFromName);
60
61 // Enumeration to pass to GetIdFromStatus. Must match the order
62 // that the kernel lists IDs in the status file.
63 enum IdKind {
64 kIdReal = 0, // uid and gid
65 kIdEffective = 1, // euid and egid
66 kIdSet = 2, // suid and sgid
67 kIdFileSystem = 3, // fsuid and fsgid
68 kIdMax
69 };
Chris Sosae4a86032010-06-16 17:08:34 -070070
Ken Mixter2953c3a2010-10-18 14:42:20 -070071 static const int kForkProblem = 255;
72
Chris Sosae4a86032010-06-16 17:08:34 -070073 std::string GetPattern(bool enabled) const;
74 bool SetUpInternal(bool enabled);
75
Ken Mixter777484c2010-07-23 16:22:44 -070076 FilePath GetProcessPath(pid_t pid);
77 bool GetSymlinkTarget(const FilePath &symlink,
78 FilePath *target);
79 bool GetExecutableBaseNameFromPid(uid_t pid,
80 std::string *base_name);
81 bool GetIdFromStatus(const char *prefix,
82 IdKind kind,
83 const std::string &status_contents,
84 int *id);
Ken Mixter777484c2010-07-23 16:22:44 -070085 bool CopyOffProcFiles(pid_t pid, const FilePath &process_map);
Ken Mixter777484c2010-07-23 16:22:44 -070086 // Determines the crash directory for given pid based on pid's owner,
87 // and creates the directory if necessary with appropriate permissions.
88 // Returns true whether or not directory needed to be created, false on
89 // any failure.
90 bool GetCreatedCrashDirectory(pid_t pid,
91 FilePath *crash_file_path);
Ken Mixter777484c2010-07-23 16:22:44 -070092 bool CopyStdinToCoreFile(const FilePath &core_path);
Ken Mixter2953c3a2010-10-18 14:42:20 -070093 int ForkExecAndPipe(std::vector<const char *> &arguments,
94 const char *output_file);
Ken Mixter777484c2010-07-23 16:22:44 -070095 bool ConvertCoreToMinidump(const FilePath &core_path,
96 const FilePath &procfs_directory,
97 const FilePath &minidump_path,
98 const FilePath &temp_directory);
99 bool GenerateDiagnostics(pid_t pid, const std::string &exec_name);
100
101 bool generate_diagnostics_;
Chris Sosae4a86032010-06-16 17:08:34 -0700102 std::string core_pattern_file_;
Chris Sosae4a86032010-06-16 17:08:34 -0700103 std::string our_path_;
104 bool initialized_;
Ken Mixter777484c2010-07-23 16:22:44 -0700105
106 static const char *kUserId;
107 static const char *kGroupId;
Chris Sosae4a86032010-06-16 17:08:34 -0700108};
109
Ken Mixter777484c2010-07-23 16:22:44 -0700110#endif // _CRASH_REPORTER_USER_COLLECTOR_H_