1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CRASH_REPORTER_UDEV_COLLECTOR_H_
18 #define CRASH_REPORTER_UDEV_COLLECTOR_H_
19 
20 #include <string>
21 
22 #include <base/files/file_path.h>
23 #include <base/macros.h>
24 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
25 
26 #include "crash_collector.h"
27 
28 // Udev crash collector.
29 class UdevCollector : public CrashCollector {
30  public:
31   UdevCollector();
32 
33   ~UdevCollector() override;
34 
35   // The udev event string should be formatted as follows:
36   //   "ACTION=[action]:KERNEL=[name]:SUBSYSTEM=[subsystem]"
37   // The values don't have to be in any particular order. One or more of them
38   // could be omitted, in which case it would be treated as a wildcard (*).
39   bool HandleCrash(const std::string& udev_event);
40 
41  protected:
42   std::string dev_coredump_directory_;
43 
44  private:
45   friend class UdevCollectorTest;
46 
47   // Process udev crash logs, collecting log files according to the config
48   // file (crash_reporter_logs.conf).
49   bool ProcessUdevCrashLogs(const base::FilePath& crash_directory,
50                             const std::string& action,
51                             const std::string& kernel,
52                             const std::string& subsystem);
53   // Process device coredump, collecting device coredump file.
54   // |instance_number| is the kernel number of the virtual device for the device
55   // coredump instance.
56   bool ProcessDevCoredump(const base::FilePath& crash_directory,
57                           int instance_number);
58   // Copy device coredump file to crash directory, and perform necessary
59   // coredump file management.
60   bool AppendDevCoredump(const base::FilePath& crash_directory,
61                          const base::FilePath& coredump_path,
62                          int instance_number);
63   // Clear the device coredump file by performing a dummy write to it.
64   bool ClearDevCoredump(const base::FilePath& coredump_path);
65   // Return the driver name of the device that generates the coredump.
66   std::string GetFailingDeviceDriverName(int instance_number);
67 
68   // Mutator for unit testing.
set_log_config_path(const std::string & path)69   void set_log_config_path(const std::string& path) {
70     log_config_path_ = base::FilePath(path);
71   }
72 
73   DISALLOW_COPY_AND_ASSIGN(UdevCollector);
74 };
75 
76 #endif  // CRASH_REPORTER_UDEV_COLLECTOR_H_
77