1// Copyright (c) 2011 The Chromium 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#ifndef CHROME_BROWSER_CRASH_HANDLER_HOST_LINUX_H_
6#define CHROME_BROWSER_CRASH_HANDLER_HOST_LINUX_H_
7#pragma once
8
9#include "base/message_loop.h"
10
11#if defined(USE_LINUX_BREAKPAD)
12#include <sys/types.h>
13
14#include <string>
15
16#include "base/memory/scoped_ptr.h"
17
18class BreakpadInfo;
19
20namespace base {
21class Thread;
22}
23#endif  // defined(USE_LINUX_BREAKPAD)
24
25template <typename T> struct DefaultSingletonTraits;
26
27// This is the base class for singleton objects which crash dump renderers and
28// plugins on Linux. We perform the crash dump from the browser because it
29// allows us to be outside the sandbox.
30//
31// PluginCrashHandlerHostLinux and RendererCrashHandlerHostLinux are singletons
32// that handle plugin and renderer crashes, respectively.
33//
34// Processes signal that they need to be dumped by sending a datagram over a
35// UNIX domain socket. All processes of the same type share the client end of
36// this socket which is installed in their descriptor table before exec.
37class CrashHandlerHostLinux : public MessageLoopForIO::Watcher,
38                              public MessageLoop::DestructionObserver {
39 public:
40  // Get the file descriptor which processes should be given in order to signal
41  // crashes to the browser.
42  int GetDeathSignalSocket() const {
43    return process_socket_;
44  }
45
46  // MessagePumbLibevent::Watcher impl:
47  virtual void OnFileCanWriteWithoutBlocking(int fd);
48  virtual void OnFileCanReadWithoutBlocking(int fd);
49
50  // MessageLoop::DestructionObserver impl:
51  virtual void WillDestroyCurrentMessageLoop();
52
53#if defined(USE_LINUX_BREAKPAD)
54  // Whether we are shutting down or not.
55  bool IsShuttingDown() const;
56#endif
57
58 protected:
59  CrashHandlerHostLinux();
60  virtual ~CrashHandlerHostLinux();
61
62#if defined(USE_LINUX_BREAKPAD)
63  // Only called in concrete subclasses.
64  void InitCrashUploaderThread();
65
66  std::string process_type_;
67#endif
68
69 private:
70  void Init();
71
72#if defined(USE_LINUX_BREAKPAD)
73  // This is here on purpose to make CrashHandlerHostLinux abstract.
74  virtual void SetProcessType() = 0;
75
76  // Do work on the FILE thread for OnFileCanReadWithoutBlocking().
77  void WriteDumpFile(BreakpadInfo* info,
78                     pid_t crashing_pid,
79                     char* crash_context,
80                     int signal_fd);
81
82  // Continue OnFileCanReadWithoutBlocking()'s work on the IO thread.
83  void QueueCrashDumpTask(BreakpadInfo* info, int signal_fd);
84#endif
85
86  int process_socket_;
87  int browser_socket_;
88
89#if defined(USE_LINUX_BREAKPAD)
90  MessageLoopForIO::FileDescriptorWatcher file_descriptor_watcher_;
91  scoped_ptr<base::Thread> uploader_thread_;
92  bool shutting_down_;
93#endif
94
95  DISALLOW_COPY_AND_ASSIGN(CrashHandlerHostLinux);
96};
97
98class GpuCrashHandlerHostLinux : public CrashHandlerHostLinux {
99 public:
100  // Returns the singleton instance.
101  static GpuCrashHandlerHostLinux* GetInstance();
102
103 private:
104  friend struct DefaultSingletonTraits<GpuCrashHandlerHostLinux>;
105  GpuCrashHandlerHostLinux();
106  virtual ~GpuCrashHandlerHostLinux();
107
108#if defined(USE_LINUX_BREAKPAD)
109  virtual void SetProcessType();
110#endif
111
112  DISALLOW_COPY_AND_ASSIGN(GpuCrashHandlerHostLinux);
113};
114
115class PluginCrashHandlerHostLinux : public CrashHandlerHostLinux {
116 public:
117  // Returns the singleton instance.
118  static PluginCrashHandlerHostLinux* GetInstance();
119
120 private:
121  friend struct DefaultSingletonTraits<PluginCrashHandlerHostLinux>;
122  PluginCrashHandlerHostLinux();
123  virtual ~PluginCrashHandlerHostLinux();
124
125#if defined(USE_LINUX_BREAKPAD)
126  virtual void SetProcessType();
127#endif
128
129  DISALLOW_COPY_AND_ASSIGN(PluginCrashHandlerHostLinux);
130};
131
132class RendererCrashHandlerHostLinux : public CrashHandlerHostLinux {
133 public:
134  // Returns the singleton instance.
135  static RendererCrashHandlerHostLinux* GetInstance();
136
137 private:
138  friend struct DefaultSingletonTraits<RendererCrashHandlerHostLinux>;
139  RendererCrashHandlerHostLinux();
140  virtual ~RendererCrashHandlerHostLinux();
141
142#if defined(USE_LINUX_BREAKPAD)
143  virtual void SetProcessType();
144#endif
145
146  DISALLOW_COPY_AND_ASSIGN(RendererCrashHandlerHostLinux);
147};
148
149class PpapiCrashHandlerHostLinux : public CrashHandlerHostLinux {
150 public:
151  // Returns the singleton instance.
152  static PpapiCrashHandlerHostLinux* GetInstance();
153
154 private:
155  friend struct DefaultSingletonTraits<PpapiCrashHandlerHostLinux>;
156  PpapiCrashHandlerHostLinux();
157  virtual ~PpapiCrashHandlerHostLinux();
158
159#if defined(USE_LINUX_BREAKPAD)
160  virtual void SetProcessType();
161#endif
162
163  DISALLOW_COPY_AND_ASSIGN(PpapiCrashHandlerHostLinux);
164};
165
166#endif  // CHROME_BROWSER_CRASH_HANDLER_HOST_LINUX_H_
167