Improve dockapp recognition
[wmaker-crm.git] / src / monitor.c
blob16d6c757d4fbc906595c81c92cad4381f62fb8d3
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <signal.h>
28 #include <sys/wait.h>
29 #ifdef __FreeBSD__
30 #include <sys/signal.h>
31 #endif
33 #include <X11/Xlib.h>
34 #include <X11/Xutil.h>
35 #include <X11/Xproto.h>
37 #include "WindowMaker.h"
38 #include "screen.h"
39 #include "window.h"
40 #include "dialog.h"
41 #include "funcs.h"
43 /****** Global Variables ******/
44 extern WPreferences wPreferences;
46 int showCrashDialog(int sig)
48 int crashAction;
50 dpy = XOpenDisplay(NULL);
51 if (dpy) {
52 /* XXX TODO make sure that window states are saved and restored via netwm */
54 XGrabServer(dpy);
55 crashAction = wShowCrashingDialogPanel(sig);
56 XCloseDisplay(dpy);
57 dpy = NULL;
58 } else {
59 wsyserror(_("cannot open connection for crashing dialog panel. Aborting."));
60 crashAction = WMAbort;
63 if (crashAction == WMStartAlternate) {
64 int i;
66 wmessage(_("trying to start alternate window manager..."));
68 for (i = 0; i < WMGetArrayItemCount(wPreferences.fallbackWMs); i++) {
69 Restart(WMGetFromArray(wPreferences.fallbackWMs, i), False);
72 wfatal(_("failed to start alternate window manager. Aborting."));
74 return 0;
75 } else if (crashAction == WMAbort)
76 return 0;
77 else
78 return 1;
81 int MonitorLoop(int argc, char **argv)
83 pid_t pid, exited;
84 char **child_argv = wmalloc(sizeof(char *) * (argc + 2));
85 int i, status;
86 time_t last_start;
87 Bool error = False;
89 for (i = 0; i < argc; i++)
90 child_argv[i] = argv[i];
91 child_argv[i++] = "--for-real";
92 child_argv[i] = NULL;
94 for (;;) {
95 last_start = time(NULL);
97 /* Start Window Maker */
98 pid = fork();
99 if (pid == 0) {
100 execvp(child_argv[0], child_argv);
101 wsyserror(_("Error respawning Window Maker"));
102 exit(1);
103 } else if (pid < 0) {
104 wsyserror(_("Error respawning Window Maker"));
105 exit(1);
108 do {
109 if ((exited = waitpid(-1, &status, 0)) < 0) {
110 wsyserror(_("Error during monitoring of Window Maker process."));
111 error = True;
112 break;
114 } while (exited != pid);
116 if (error)
117 break;
119 child_argv[argc] = "--for-real-";
121 /* Check if the wmaker process exited due to a crash */
122 if (WIFSIGNALED(status) &&
123 (WTERMSIG(status) == SIGSEGV ||
124 WTERMSIG(status) == SIGBUS ||
125 WTERMSIG(status) == SIGILL || WTERMSIG(status) == SIGABRT || WTERMSIG(status) == SIGFPE)) {
126 /* If so, we check when was the last restart.
127 * If it was less than 3s ago, it's a bad sign, so we show
128 * the crash panel and ask the user what to do */
129 if (time(NULL) - last_start < 3) {
130 if (showCrashDialog(WTERMSIG(status)) == 0) {
131 return 1;
134 wwarning(_("Window Maker exited due to a crash (signal %i) and will be restarted."),
135 WTERMSIG(status));
136 } else
137 break;
139 return 0;