]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
_dbus_disable_crash_handling: Factor out from test-segfault
authorSimon McVittie <smcv@collabora.com>
Tue, 20 Nov 2018 12:48:40 +0000 (12:48 +0000)
committerSimon McVittie <smcv@collabora.com>
Tue, 20 Nov 2018 12:58:17 +0000 (12:58 +0000)
Signed-off-by: Simon McVittie <smcv@collabora.com>
dbus/dbus-sysdeps-util-unix.c
dbus/dbus-sysdeps-util-win.c
dbus/dbus-sysdeps.h
test/Makefile.am
test/test-segfault.c

index 262dcfdedca56046eb763ba20827d9db6ad266c5..3e74cf17af5a7e4826feb57842008fc77b7e6e9e 100644 (file)
 #include <dirent.h>
 #include <sys/un.h>
 
+#ifdef HAVE_SYS_PRCTL_H
+#include <sys/prctl.h>
+#endif
+
 #ifdef HAVE_SYS_SYSLIMITS_H
 #include <sys/syslimits.h>
 #endif
@@ -1583,3 +1587,28 @@ _dbus_daemon_report_stopping (void)
   sd_notify (0, "STOPPING=1");
 #endif
 }
+
+/**
+ * Try to disable core dumps and similar special crash handling.
+ */
+void
+_dbus_disable_crash_handling (void)
+{
+#ifdef HAVE_SETRLIMIT
+  /* No core dumps please, we know we crashed. */
+  struct rlimit r = { 0, };
+
+  getrlimit (RLIMIT_CORE, &r);
+  r.rlim_cur = 0;
+  setrlimit (RLIMIT_CORE, &r);
+#endif
+
+#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
+  /* Really, no core dumps please. On Linux, if core_pattern is
+   * set to a pipe (for abrt/apport/corekeeper/etc.), RLIMIT_CORE of 0
+   * is ignored (deliberately, so people can debug init(8) and other
+   * early stuff); but Linux has PR_SET_DUMPABLE, so we can avoid core
+   * dumps anyway. */
+  prctl (PR_SET_DUMPABLE, 0, 0, 0, 0);
+#endif
+}
index fc35690804d93b47a8f256e4dfc1c3211d39127e..0182b6b1670de299051594486236b762ec39e69e 100644 (file)
@@ -1688,3 +1688,27 @@ _dbus_win_stderr_win_error (const char    *app,
   fprintf (stderr, "%s: %s: %s\n", app, message, error.message);
   dbus_error_free (&error);
 }
+
+static int exception_handler (LPEXCEPTION_POINTERS p) _DBUS_GNUC_NORETURN;
+
+static int
+exception_handler (LPEXCEPTION_POINTERS p)
+{
+  ExitProcess (0xc0000005);
+}
+
+/**
+ * Try to disable core dumps and similar special crash handling.
+ */
+void
+_dbus_disable_crash_handling (void)
+{
+  /* Disable Windows popup dialog when an app crashes so that app quits
+   * immediately with error code instead of waiting for user to dismiss
+   * the dialog.  */
+  DWORD dwMode = SetErrorMode (SEM_NOGPFAULTERRORBOX);
+
+  SetErrorMode (dwMode | SEM_NOGPFAULTERRORBOX);
+  /* Disable "just in time" debugger */
+  SetUnhandledExceptionFilter ((LPTOP_LEVEL_EXCEPTION_FILTER) &exception_handler);
+}
index fa20219ed2e3b1241b86938cab05b5dec9869420..9965c6e4dbe7bb39d3086803ef719d8a7d093d16 100644 (file)
@@ -732,6 +732,8 @@ void _dbus_combine_tcp_errors (DBusList **sources,
                                const char *port,
                                DBusError *dest);
 
+void _dbus_disable_crash_handling (void);
+
 /** @} */
 
 DBUS_END_DECLS
index e439bfe43063f9931549babba6c0643d1a17d584..59d436093f753468f5eb45d0f2def6a003ae7ca4 100644 (file)
@@ -105,6 +105,9 @@ test_printf_LDADD = $(top_builddir)/dbus/libdbus-internal.la
 test_refs_SOURCES = internals/refs.c
 test_refs_LDADD = libdbus-testutils.la $(GLIB_LIBS)
 
+test_segfault_SOURCES = test-segfault.c
+test_segfault_LDADD = $(top_builddir)/dbus/libdbus-internal.la
+
 test_server_oom_SOURCES = internals/server-oom.c
 test_server_oom_LDADD = libdbus-testutils.la $(GLIB_LIBS)
 
index 8517dd6c22b666406bba4a369b75f97629d7e250..a11d45b26d0d3f928afd227a10417b8f927a42f0 100644 (file)
@@ -5,63 +5,14 @@
 #include <signal.h>
 #endif
 
-#ifdef HAVE_SETRLIMIT
-#include <sys/resource.h>
-#endif
-
-#ifdef HAVE_SYS_PRCTL_H
-#include <sys/prctl.h>
-#endif
-
-#ifdef DBUS_WIN
-#include <stdio.h>
-#include <windows.h>
-
-#include <dbus/dbus-macros.h>
-
-int exception_handler (LPEXCEPTION_POINTERS p) _DBUS_GNUC_NORETURN;
-
-/* Explicit Windows exception handlers needed to supress OS popups */
-int
-exception_handler(LPEXCEPTION_POINTERS p)
-{
-  fprintf(stderr, "test-segfault: raised fatal exception as intended\n");
-  ExitProcess(0xc0000005);
-}
-#endif
+#include "dbus/dbus-sysdeps.h"
 
 int
 main (int argc, char **argv)
 {
   char *p;  
 
-#ifdef DBUS_WIN
-  /* Disable Windows popup dialog when an app crashes so that app quits
-   * immediately with error code instead of waiting for user to dismiss
-   * the dialog.  */
-  DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
-  SetErrorMode(dwMode | SEM_NOGPFAULTERRORBOX);
-  /* Disable "just in time" debugger */
-  SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)&exception_handler);
-#endif
-
-#ifdef HAVE_SETRLIMIT
-  /* No core dumps please, we know we crashed. */
-  struct rlimit r = { 0, };
-  
-  getrlimit (RLIMIT_CORE, &r);
-  r.rlim_cur = 0;
-  setrlimit (RLIMIT_CORE, &r);
-#endif
-
-#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
-  /* Really, no core dumps please. On Linux, if core_pattern is
-   * set to a pipe (for abrt/apport/corekeeper/etc.), RLIMIT_CORE of 0
-   * is ignored (deliberately, so people can debug init(8) and other
-   * early stuff); but Linux has PR_SET_DUMPABLE, so we can avoid core
-   * dumps anyway. */
-  prctl (PR_SET_DUMPABLE, 0, 0, 0, 0);
-#endif
+  _dbus_disable_crash_handling ();
 
 #ifdef HAVE_RAISE
   raise (SIGSEGV);