--- gdm-2.20.7.ori/daemon/gdm.c	2008-06-30 19:53:13.000000000 +0200
+++ gdm-2.20.7/daemon/gdm.c	2008-08-11 15:38:14.000000000 +0200
@@ -123,6 +123,8 @@ static gboolean no_console       = FALSE
                                              means, don't run static servers
                                              and second, don't display info on
                                              the console */
+static gboolean in_mainloop_sig  = FALSE;
+static gboolean in_final_cleanup = FALSE;
 
 GdmConnection *fifoconn = NULL; /* Fifo connection */
 GdmConnection *pipeconn = NULL; /* slavepipe (handled just like Fifo for
@@ -702,7 +704,10 @@ halt_machine (void)
 		/* maybe these don't run but oh well - there isn't
 		   really a good way to know a priori if the command
 		   will succeed. */
-		gdm_final_cleanup ();
+		if (! in_mainloop_sig) {
+			in_final_cleanup = TRUE;
+			gdm_final_cleanup ();
+		}
 		VE_IGNORE_EINTR (g_chdir ("/"));
 #ifdef __linux__
 		change_to_first_and_clear (FALSE);
@@ -721,7 +726,10 @@ restart_machine (void)
 	s = gdm_daemon_config_get_value_string_array (GDM_KEY_REBOOT);
 
 	if (try_commands (s)) {
-		gdm_final_cleanup ();
+		if (! in_mainloop_sig) {
+			in_final_cleanup = TRUE;
+			gdm_final_cleanup ();
+		}
 		VE_IGNORE_EINTR (g_chdir ("/"));
 
 #ifdef __linux__
@@ -761,7 +769,10 @@ custom_cmd_restart (long cmd_id)
 
         gdm_debug (_("Executing custom command %ld with restart option..."), cmd_id);
 
-	gdm_final_cleanup ();
+	if (! in_mainloop_sig) {
+		in_final_cleanup = TRUE;
+		gdm_final_cleanup ();
+	}
 	VE_IGNORE_EINTR (g_chdir ("/"));
 
 #ifdef __linux__
@@ -1046,15 +1057,19 @@ gdm_cleanup_children (void)
 	case DISPLAY_REBOOT:	/* Restart machine */
 		restart_machine ();
 
-		status = DISPLAY_REMANAGE;
-		goto start_autopsy;
+		if (! in_final_cleanup) {
+			status = DISPLAY_REMANAGE;
+			goto start_autopsy;
+		}
 		break;
 
 	case DISPLAY_HALT:	/* Halt machine */
 		halt_machine ();
 
-		status = DISPLAY_REMANAGE;
-		goto start_autopsy;
+		if (! in_final_cleanup) {
+			status = DISPLAY_REMANAGE;
+			goto start_autopsy;
+		}
 		break;
 
 	case DISPLAY_SUSPEND:	/* Suspend machine */
@@ -1062,8 +1077,10 @@ gdm_cleanup_children (void)
 		 * see GDM_SOP_SUSPEND_MACHINE */
 		suspend_machine ();
 
-		status = DISPLAY_REMANAGE;
-		goto start_autopsy;
+		if (! in_final_cleanup) {
+			status = DISPLAY_REMANAGE;
+			goto start_autopsy;
+		}
 		break;
 
 	case DISPLAY_RESTARTGDM:
@@ -1303,7 +1320,10 @@ mainloop_sig_callback (int sig, gpointer
 		case SIGINT:
 		case SIGTERM:
 			gdm_debug ("mainloop_sig_callback: Got TERM/INT. Going down!");
-			gdm_final_cleanup ();
+			if (! in_final_cleanup) {
+				in_mainloop_sig = TRUE;
+				gdm_final_cleanup ();
+			}
 			exit (EXIT_SUCCESS);
 			break;
 
