Fixed a few improper macro usages
[wmaker-crm.git] / src / monitor.c
blobc023b3069b40768675b1afdeacb713ae9c17db9e
1 /* monitor.c - monitors the wmaker process
3 * Window Maker window manager
5 * Copyright (c) 1997-2004 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <signal.h>
27 #include <sys/wait.h>
28 #ifdef __FreeBSD__
29 #include <sys/signal.h>
30 #endif
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
34 #include <X11/Xproto.h>
36 #include "WindowMaker.h"
37 #include "screen.h"
38 #include "window.h"
39 #include "dialog.h"
40 #include "main.h"
43 static int showCrashDialog(int sig)
45 int crashAction;
47 dpy = XOpenDisplay(NULL);
48 if (dpy) {
49 /* XXX TODO make sure that window states are saved and restored via netwm */
51 XGrabServer(dpy);
52 crashAction = wShowCrashingDialogPanel(sig);
53 XCloseDisplay(dpy);
54 dpy = NULL;
55 } else {
56 werror(_("cannot open connection for crashing dialog panel. Aborting."));
57 crashAction = WMAbort;
60 if (crashAction == WMStartAlternate) {
61 int i;
63 wmessage(_("trying to start alternate window manager..."));
65 for (i = 0; i < WMGetArrayItemCount(wPreferences.fallbackWMs); i++) {
66 Restart(WMGetFromArray(wPreferences.fallbackWMs, i), False);
69 wfatal(_("failed to start alternate window manager. Aborting."));
71 return 0;
72 } else if (crashAction == WMAbort)
73 return 0;
74 else
75 return 1;
78 int MonitorLoop(int argc, char **argv)
80 pid_t pid, exited;
81 char **child_argv = wmalloc(sizeof(char *) * (argc + 2));
82 int i, status;
83 time_t last_start;
84 Bool error = False;
86 for (i = 0; i < argc; i++)
87 child_argv[i] = argv[i];
88 child_argv[i++] = "--for-real";
89 child_argv[i] = NULL;
91 for (;;) {
92 last_start = time(NULL);
94 /* Start Window Maker */
95 pid = fork();
96 if (pid == 0) {
97 execvp(child_argv[0], child_argv);
98 werror(_("Error respawning Window Maker"));
99 exit(1);
100 } else if (pid < 0) {
101 werror(_("Error respawning Window Maker"));
102 exit(1);
105 do {
106 if ((exited = waitpid(-1, &status, 0)) < 0) {
107 werror(_("Error during monitoring of Window Maker process."));
108 error = True;
109 break;
111 } while (exited != pid);
113 if (error)
114 break;
116 child_argv[argc] = "--for-real-";
118 /* Check if the wmaker process exited due to a crash */
119 if (WIFSIGNALED(status) &&
120 (WTERMSIG(status) == SIGSEGV ||
121 WTERMSIG(status) == SIGBUS ||
122 WTERMSIG(status) == SIGILL || WTERMSIG(status) == SIGABRT || WTERMSIG(status) == SIGFPE)) {
123 /* If so, we check when was the last restart.
124 * If it was less than 3s ago, it's a bad sign, so we show
125 * the crash panel and ask the user what to do */
126 if (time(NULL) - last_start < 3) {
127 if (showCrashDialog(WTERMSIG(status)) == 0) {
128 return 1;
131 wwarning(_("Window Maker exited due to a crash (signal %i) and will be restarted."),
132 WTERMSIG(status));
133 } else
134 break;
136 return 0;