|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "crashreporter.h" |
|
7 |
|
8 #include <unistd.h> |
|
9 #include <dlfcn.h> |
|
10 #include <errno.h> |
|
11 #include <glib.h> |
|
12 #include <gtk/gtk.h> |
|
13 #include <signal.h> |
|
14 #include <stdio.h> |
|
15 #include <stdlib.h> |
|
16 #include <string.h> |
|
17 #include <sys/stat.h> |
|
18 #include <sys/types.h> |
|
19 #include <sys/wait.h> |
|
20 #include <gdk/gdkkeysyms.h> |
|
21 |
|
22 #include <algorithm> |
|
23 #include <string> |
|
24 #include <vector> |
|
25 |
|
26 #include "mozilla/NullPtr.h" |
|
27 #include "common/linux/http_upload.h" |
|
28 #include "crashreporter.h" |
|
29 #include "crashreporter_gtk_common.h" |
|
30 |
|
31 #ifndef GDK_KEY_Escape |
|
32 #define GDK_KEY_Escape GDK_Escape |
|
33 #endif |
|
34 |
|
35 using std::string; |
|
36 using std::vector; |
|
37 |
|
38 using namespace CrashReporter; |
|
39 |
|
40 GtkWidget* gWindow = 0; |
|
41 GtkWidget* gSubmitReportCheck = 0; |
|
42 GtkWidget* gIncludeURLCheck = 0; |
|
43 GtkWidget* gThrobber = 0; |
|
44 GtkWidget* gProgressLabel = 0; |
|
45 GtkWidget* gCloseButton = 0; |
|
46 GtkWidget* gRestartButton = 0; |
|
47 |
|
48 bool gInitialized = false; |
|
49 bool gDidTrySend = false; |
|
50 string gDumpFile; |
|
51 StringTable gQueryParameters; |
|
52 string gHttpProxy; |
|
53 string gAuth; |
|
54 string gCACertificateFile; |
|
55 string gSendURL; |
|
56 string gURLParameter; |
|
57 vector<string> gRestartArgs; |
|
58 GThread* gSendThreadID; |
|
59 |
|
60 // From crashreporter_linux.cpp |
|
61 void SaveSettings(); |
|
62 void SendReport(); |
|
63 void TryInitGnome(); |
|
64 void UpdateSubmit(); |
|
65 |
|
66 static bool RestartApplication() |
|
67 { |
|
68 char** argv = reinterpret_cast<char**>( |
|
69 malloc(sizeof(char*) * (gRestartArgs.size() + 1))); |
|
70 |
|
71 if (!argv) return false; |
|
72 |
|
73 unsigned int i; |
|
74 for (i = 0; i < gRestartArgs.size(); i++) { |
|
75 argv[i] = (char*)gRestartArgs[i].c_str(); |
|
76 } |
|
77 argv[i] = 0; |
|
78 |
|
79 pid_t pid = fork(); |
|
80 if (pid == -1) |
|
81 return false; |
|
82 else if (pid == 0) { |
|
83 (void)execv(argv[0], argv); |
|
84 _exit(1); |
|
85 } |
|
86 |
|
87 free(argv); |
|
88 |
|
89 return true; |
|
90 } |
|
91 |
|
92 // Quit the app, used as a timeout callback |
|
93 static gboolean CloseApp(gpointer data) |
|
94 { |
|
95 gtk_main_quit(); |
|
96 g_thread_join(gSendThreadID); |
|
97 return FALSE; |
|
98 } |
|
99 |
|
100 static gboolean ReportCompleted(gpointer success) |
|
101 { |
|
102 gtk_widget_hide(gThrobber); |
|
103 string str = success ? gStrings[ST_REPORTSUBMITSUCCESS] |
|
104 : gStrings[ST_SUBMITFAILED]; |
|
105 gtk_label_set_text(GTK_LABEL(gProgressLabel), str.c_str()); |
|
106 g_timeout_add(5000, CloseApp, 0); |
|
107 return FALSE; |
|
108 } |
|
109 |
|
110 #ifdef MOZ_ENABLE_GCONF |
|
111 #define HTTP_PROXY_DIR "/system/http_proxy" |
|
112 |
|
113 void LoadProxyinfo() |
|
114 { |
|
115 class GConfClient; |
|
116 typedef GConfClient * (*_gconf_default_fn)(); |
|
117 typedef gboolean (*_gconf_bool_fn)(GConfClient *, const gchar *, GError **); |
|
118 typedef gint (*_gconf_int_fn)(GConfClient *, const gchar *, GError **); |
|
119 typedef gchar * (*_gconf_string_fn)(GConfClient *, const gchar *, GError **); |
|
120 |
|
121 if (getenv ("http_proxy")) |
|
122 return; // libcurl can use the value from the environment |
|
123 |
|
124 static void* gconfLib = dlopen("libgconf-2.so.4", RTLD_LAZY); |
|
125 if (!gconfLib) |
|
126 return; |
|
127 |
|
128 _gconf_default_fn gconf_client_get_default = |
|
129 (_gconf_default_fn)dlsym(gconfLib, "gconf_client_get_default"); |
|
130 _gconf_bool_fn gconf_client_get_bool = |
|
131 (_gconf_bool_fn)dlsym(gconfLib, "gconf_client_get_bool"); |
|
132 _gconf_int_fn gconf_client_get_int = |
|
133 (_gconf_int_fn)dlsym(gconfLib, "gconf_client_get_int"); |
|
134 _gconf_string_fn gconf_client_get_string = |
|
135 (_gconf_string_fn)dlsym(gconfLib, "gconf_client_get_string"); |
|
136 |
|
137 if(!(gconf_client_get_default && |
|
138 gconf_client_get_bool && |
|
139 gconf_client_get_int && |
|
140 gconf_client_get_string)) { |
|
141 dlclose(gconfLib); |
|
142 return; |
|
143 } |
|
144 |
|
145 GConfClient *conf = gconf_client_get_default(); |
|
146 |
|
147 if (gconf_client_get_bool(conf, HTTP_PROXY_DIR "/use_http_proxy", nullptr)) { |
|
148 gint port; |
|
149 gchar *host = nullptr, *httpproxy = nullptr; |
|
150 |
|
151 host = gconf_client_get_string(conf, HTTP_PROXY_DIR "/host", nullptr); |
|
152 port = gconf_client_get_int(conf, HTTP_PROXY_DIR "/port", nullptr); |
|
153 |
|
154 if (port && host && *host != '\0') { |
|
155 httpproxy = g_strdup_printf("http://%s:%d/", host, port); |
|
156 gHttpProxy = httpproxy; |
|
157 } |
|
158 |
|
159 g_free(host); |
|
160 g_free(httpproxy); |
|
161 |
|
162 if (gconf_client_get_bool(conf, HTTP_PROXY_DIR "/use_authentication", |
|
163 nullptr)) { |
|
164 gchar *user, *password, *auth = nullptr; |
|
165 |
|
166 user = gconf_client_get_string(conf, |
|
167 HTTP_PROXY_DIR "/authentication_user", |
|
168 nullptr); |
|
169 password = gconf_client_get_string(conf, |
|
170 HTTP_PROXY_DIR |
|
171 "/authentication_password", |
|
172 nullptr); |
|
173 |
|
174 if (user && password) { |
|
175 auth = g_strdup_printf("%s:%s", user, password); |
|
176 gAuth = auth; |
|
177 } |
|
178 |
|
179 g_free(user); |
|
180 g_free(password); |
|
181 g_free(auth); |
|
182 } |
|
183 } |
|
184 |
|
185 g_object_unref(conf); |
|
186 |
|
187 // Don't dlclose gconfLib as libORBit-2 uses atexit(). |
|
188 } |
|
189 #endif |
|
190 |
|
191 gpointer SendThread(gpointer args) |
|
192 { |
|
193 string response, error; |
|
194 long response_code; |
|
195 |
|
196 bool success = google_breakpad::HTTPUpload::SendRequest |
|
197 (gSendURL, |
|
198 gQueryParameters, |
|
199 gDumpFile, |
|
200 "upload_file_minidump", |
|
201 gHttpProxy, gAuth, |
|
202 gCACertificateFile, |
|
203 &response, |
|
204 &response_code, |
|
205 &error); |
|
206 if (success) { |
|
207 LogMessage("Crash report submitted successfully"); |
|
208 } |
|
209 else { |
|
210 LogMessage("Crash report submission failed: " + error); |
|
211 } |
|
212 |
|
213 SendCompleted(success, response); |
|
214 // Apparently glib is threadsafe, and will schedule this |
|
215 // on the main thread, see: |
|
216 // http://library.gnome.org/devel/gtk-faq/stable/x499.html |
|
217 g_idle_add(ReportCompleted, (gpointer)success); |
|
218 |
|
219 return nullptr; |
|
220 } |
|
221 |
|
222 gboolean WindowDeleted(GtkWidget* window, |
|
223 GdkEvent* event, |
|
224 gpointer userData) |
|
225 { |
|
226 SaveSettings(); |
|
227 gtk_main_quit(); |
|
228 return TRUE; |
|
229 } |
|
230 |
|
231 gboolean check_escape(GtkWidget* window, |
|
232 GdkEventKey* event, |
|
233 gpointer userData) |
|
234 { |
|
235 if (event->keyval == GDK_KEY_Escape) { |
|
236 gtk_main_quit(); |
|
237 return TRUE; |
|
238 } |
|
239 return FALSE; |
|
240 } |
|
241 |
|
242 static void MaybeSubmitReport() |
|
243 { |
|
244 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gSubmitReportCheck))) { |
|
245 gDidTrySend = true; |
|
246 SendReport(); |
|
247 } else { |
|
248 gtk_main_quit(); |
|
249 } |
|
250 } |
|
251 |
|
252 void CloseClicked(GtkButton* button, |
|
253 gpointer userData) |
|
254 { |
|
255 SaveSettings(); |
|
256 MaybeSubmitReport(); |
|
257 } |
|
258 |
|
259 void RestartClicked(GtkButton* button, |
|
260 gpointer userData) |
|
261 { |
|
262 SaveSettings(); |
|
263 RestartApplication(); |
|
264 MaybeSubmitReport(); |
|
265 } |
|
266 |
|
267 static void UpdateURL() |
|
268 { |
|
269 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gIncludeURLCheck))) { |
|
270 gQueryParameters["URL"] = gURLParameter; |
|
271 } else { |
|
272 gQueryParameters.erase("URL"); |
|
273 } |
|
274 } |
|
275 |
|
276 void SubmitReportChecked(GtkButton* sender, gpointer userData) |
|
277 { |
|
278 UpdateSubmit(); |
|
279 } |
|
280 |
|
281 void IncludeURLClicked(GtkButton* sender, gpointer userData) |
|
282 { |
|
283 UpdateURL(); |
|
284 } |
|
285 |
|
286 /* === Crashreporter UI Functions === */ |
|
287 |
|
288 bool UIInit() |
|
289 { |
|
290 // breakpad probably left us with blocked signals, unblock them here |
|
291 sigset_t signals, old; |
|
292 sigfillset(&signals); |
|
293 sigprocmask(SIG_UNBLOCK, &signals, &old); |
|
294 |
|
295 // tell glib we're going to use threads |
|
296 g_thread_init(nullptr); |
|
297 |
|
298 if (gtk_init_check(&gArgc, &gArgv)) { |
|
299 gInitialized = true; |
|
300 |
|
301 if (gStrings.find("isRTL") != gStrings.end() && |
|
302 gStrings["isRTL"] == "yes") |
|
303 gtk_widget_set_default_direction(GTK_TEXT_DIR_RTL); |
|
304 |
|
305 return true; |
|
306 } |
|
307 |
|
308 return false; |
|
309 } |
|
310 |
|
311 void UIShowDefaultUI() |
|
312 { |
|
313 GtkWidget* errorDialog = |
|
314 gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, |
|
315 GTK_MESSAGE_ERROR, |
|
316 GTK_BUTTONS_CLOSE, |
|
317 "%s", gStrings[ST_CRASHREPORTERDEFAULT].c_str()); |
|
318 |
|
319 gtk_window_set_title(GTK_WINDOW(errorDialog), |
|
320 gStrings[ST_CRASHREPORTERTITLE].c_str()); |
|
321 gtk_dialog_run(GTK_DIALOG(errorDialog)); |
|
322 } |
|
323 |
|
324 void UIError_impl(const string& message) |
|
325 { |
|
326 if (!gInitialized) { |
|
327 // Didn't initialize, this is the best we can do |
|
328 printf("Error: %s\n", message.c_str()); |
|
329 return; |
|
330 } |
|
331 |
|
332 GtkWidget* errorDialog = |
|
333 gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, |
|
334 GTK_MESSAGE_ERROR, |
|
335 GTK_BUTTONS_CLOSE, |
|
336 "%s", message.c_str()); |
|
337 |
|
338 gtk_window_set_title(GTK_WINDOW(errorDialog), |
|
339 gStrings[ST_CRASHREPORTERTITLE].c_str()); |
|
340 gtk_dialog_run(GTK_DIALOG(errorDialog)); |
|
341 } |
|
342 |
|
343 bool UIGetIniPath(string& path) |
|
344 { |
|
345 path = gArgv[0]; |
|
346 path.append(".ini"); |
|
347 |
|
348 return true; |
|
349 } |
|
350 |
|
351 /* |
|
352 * Settings are stored in ~/.vendor/product, or |
|
353 * ~/.product if vendor is empty. |
|
354 */ |
|
355 bool UIGetSettingsPath(const string& vendor, |
|
356 const string& product, |
|
357 string& settingsPath) |
|
358 { |
|
359 char* home = getenv("HOME"); |
|
360 |
|
361 if (!home) |
|
362 return false; |
|
363 |
|
364 settingsPath = home; |
|
365 settingsPath += "/."; |
|
366 if (!vendor.empty()) { |
|
367 string lc_vendor; |
|
368 std::transform(vendor.begin(), vendor.end(), back_inserter(lc_vendor), |
|
369 (int(*)(int)) std::tolower); |
|
370 settingsPath += lc_vendor + "/"; |
|
371 } |
|
372 string lc_product; |
|
373 std::transform(product.begin(), product.end(), back_inserter(lc_product), |
|
374 (int(*)(int)) std::tolower); |
|
375 settingsPath += lc_product + "/Crash Reports"; |
|
376 return true; |
|
377 } |
|
378 |
|
379 bool UIEnsurePathExists(const string& path) |
|
380 { |
|
381 int ret = mkdir(path.c_str(), S_IRWXU); |
|
382 int e = errno; |
|
383 if (ret == -1 && e != EEXIST) |
|
384 return false; |
|
385 |
|
386 return true; |
|
387 } |
|
388 |
|
389 bool UIFileExists(const string& path) |
|
390 { |
|
391 struct stat sb; |
|
392 int ret = stat(path.c_str(), &sb); |
|
393 if (ret == -1 || !(sb.st_mode & S_IFREG)) |
|
394 return false; |
|
395 |
|
396 return true; |
|
397 } |
|
398 |
|
399 bool UIMoveFile(const string& file, const string& newfile) |
|
400 { |
|
401 if (!rename(file.c_str(), newfile.c_str())) |
|
402 return true; |
|
403 if (errno != EXDEV) |
|
404 return false; |
|
405 |
|
406 // use system /bin/mv instead, time to fork |
|
407 pid_t pID = vfork(); |
|
408 if (pID < 0) { |
|
409 // Failed to fork |
|
410 return false; |
|
411 } |
|
412 if (pID == 0) { |
|
413 char* const args[4] = { |
|
414 "mv", |
|
415 strdup(file.c_str()), |
|
416 strdup(newfile.c_str()), |
|
417 0 |
|
418 }; |
|
419 if (args[1] && args[2]) |
|
420 execve("/bin/mv", args, 0); |
|
421 if (args[1]) |
|
422 free(args[1]); |
|
423 if (args[2]) |
|
424 free(args[2]); |
|
425 exit(-1); |
|
426 } |
|
427 int status; |
|
428 waitpid(pID, &status, 0); |
|
429 return UIFileExists(newfile); |
|
430 } |
|
431 |
|
432 bool UIDeleteFile(const string& file) |
|
433 { |
|
434 return (unlink(file.c_str()) != -1); |
|
435 } |
|
436 |
|
437 std::ifstream* UIOpenRead(const string& filename) |
|
438 { |
|
439 return new std::ifstream(filename.c_str(), std::ios::in); |
|
440 } |
|
441 |
|
442 std::ofstream* UIOpenWrite(const string& filename, bool append) // append=false |
|
443 { |
|
444 return new std::ofstream(filename.c_str(), |
|
445 append ? std::ios::out | std::ios::app |
|
446 : std::ios::out); |
|
447 } |