1 // Copyright (c) 2010 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #ifndef CLIENT_LINUX_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_
31 #define CLIENT_LINUX_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_
32 
33 #include <pthread.h>
34 
35 #include <string>
36 
37 #include "common/using_std_string.h"
38 
39 namespace google_breakpad {
40 
41 class ClientInfo;
42 
43 class CrashGenerationServer {
44 public:
45   // WARNING: callbacks may be invoked on a different thread
46   // than that which creates the CrashGenerationServer.  They must
47   // be thread safe.
48   typedef void (*OnClientDumpRequestCallback)(void* context,
49                                               const ClientInfo* client_info,
50                                               const string* file_path);
51 
52   typedef void (*OnClientExitingCallback)(void* context,
53                                           const ClientInfo* client_info);
54 
55   // Create an instance with the given parameters.
56   //
57   // Parameter listen_fd: The server fd created by CreateReportChannel().
58   // Parameter dump_callback: Callback for a client crash dump request.
59   // Parameter dump_context: Context for client crash dump request callback.
60   // Parameter exit_callback: Callback for client process exit.
61   // Parameter exit_context: Context for client exit callback.
62   // Parameter generate_dumps: Whether to automatically generate dumps.
63   //     Client code of this class might want to generate dumps explicitly
64   //     in the crash dump request callback. In that case, false can be
65   //     passed for this parameter.
66   // Parameter dump_path: Path for generating dumps; required only if true is
67   //     passed for generateDumps parameter; NULL can be passed otherwise.
68   CrashGenerationServer(const int listen_fd,
69                         OnClientDumpRequestCallback dump_callback,
70                         void* dump_context,
71                         OnClientExitingCallback exit_callback,
72                         void* exit_context,
73                         bool generate_dumps,
74                         const string* dump_path);
75 
76   ~CrashGenerationServer();
77 
78   // Perform initialization steps needed to start listening to clients.
79   //
80   // Return true if initialization is successful; false otherwise.
81   bool Start();
82 
83   // Stop the server.
84   void Stop();
85 
86   // Create a "channel" that can be used by clients to report crashes
87   // to a CrashGenerationServer.  |*server_fd| should be passed to
88   // this class's constructor, and |*client_fd| should be passed to
89   // the ExceptionHandler constructor in the client process.
90   static bool CreateReportChannel(int* server_fd, int* client_fd);
91 
92 private:
93   // Run the server's event loop
94   void Run();
95 
96   // Invoked when an child process (client) event occurs
97   // Returning true => "keep running", false => "exit loop"
98   bool ClientEvent(short revents);
99 
100   // Invoked when the controlling thread (main) event occurs
101   // Returning true => "keep running", false => "exit loop"
102   bool ControlEvent(short revents);
103 
104   // Return a unique filename at which a minidump can be written
105   bool MakeMinidumpFilename(string& outFilename);
106 
107   // Trampoline to |Run()|
108   static void* ThreadMain(void* arg);
109 
110   int server_fd_;
111 
112   OnClientDumpRequestCallback dump_callback_;
113   void* dump_context_;
114 
115   OnClientExitingCallback exit_callback_;
116   void* exit_context_;
117 
118   bool generate_dumps_;
119 
120   string dump_dir_;
121 
122   bool started_;
123 
124   pthread_t thread_;
125   int control_pipe_in_;
126   int control_pipe_out_;
127 
128   // disable these
129   CrashGenerationServer(const CrashGenerationServer&);
130   CrashGenerationServer& operator=(const CrashGenerationServer&);
131 };
132 
133 } // namespace google_breakpad
134 
135 #endif // CLIENT_LINUX_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_
136