Skip to content

Commit 84b7286

Browse files
committed
Workspace/WM: enhance process exit handling.
1 parent e8b70f8 commit 84b7286

File tree

5 files changed

+47
-41
lines changed

5 files changed

+47
-41
lines changed

Applications/Workspace/WM/application.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,6 @@ WApplication *wApplicationCreate(WWindow *wwin)
430430
int pid = wNETWMGetPidForWindow(wwin->client_win);
431431
if (pid > 0) {
432432
wAddExitHandler(wNETWMGetPidForWindow(wwin->client_win), _applicationProcessHandler, wapp);
433-
CFLog(kCFLogLevelInfo,
434-
CFSTR("%s: Created death handler for X11 application PID == %i"), __func__, pid);
435-
} else {
436-
CFLog(kCFLogLevelWarning,
437-
CFSTR("%s: Failed to add death handler for X11 application. PID == %i"), __func__, pid);
438433
}
439434
}
440435

Applications/Workspace/WM/dock.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,7 @@ static void _dockHandleProcessExit(pid_t pid, unsigned char status, WDock *dock)
27742774
}
27752775
wDockFinishLaunch(icon);
27762776
icon->pid = 0;
2777+
27772778
if (status != 0) {
27782779
char msg[PATH_MAX];
27792780
char *cmd;
@@ -2789,7 +2790,7 @@ static void _dockHandleProcessExit(pid_t pid, unsigned char status, WDock *dock)
27892790
else
27902791
cmd = icon->command;
27912792

2792-
snprintf(msg, sizeof(msg), _("Could not execute command \"%s\"\n%s"), cmd, strerror(errno));
2793+
snprintf(msg, sizeof(msg), _("Could not execute command \"%s\"\n%s"), cmd, strerror(status));
27932794
message = wstrdup(msg);
27942795
dispatch_sync(workspace_q, ^{
27952796
WSRunAlertPanel(_("Workspace Dock"), message, _("Got It"), NULL, NULL);
@@ -2829,6 +2830,8 @@ static pid_t _dockExecCommand(WAppIcon *btn, const char *command, WSavedState *s
28292830
return 0;
28302831
}
28312832

2833+
// Forked process return `errno` if exit code will be <= 0.
2834+
// It's intentional to make possible to process show error description later by process exit handler .
28322835
pid = fork();
28332836
if (pid == 0) {
28342837
char **args;
@@ -2855,8 +2858,9 @@ static pid_t _dockExecCommand(WAppIcon *btn, const char *command, WSavedState *s
28552858
int ret;
28562859
ret = execvp(argv[0], args);
28572860
if (ret < 0) {
2858-
CFLog(kCFLogLevelError, CFSTR("%s: return %i error #%i -- %s"), __func__, ret, errno,
2859-
strerror(errno));
2861+
// CFLog(kCFLogLevelError, CFSTR("%s: return %i error #%i -- %s"), __func__, ret, errno,
2862+
// strerror(errno));
2863+
exit(errno);
28602864
}
28612865
exit(ret);
28622866
}

Applications/Workspace/WM/event.c

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,6 @@ static void _handleXkbStateNotify(XkbEvent *event);
132132
static void _handleApplicationProcess(void);
133133
static void _deleteExitHandler(WMagicNumber id);
134134

135-
// typedef struct DeadProcesses {
136-
// pid_t pid;
137-
// unsigned char exit_status;
138-
// } DeadProcesses;
139-
140-
/* stack of dead processes */
141-
// static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
142-
// static int deadProcessPtr = 0;
143-
144135
typedef struct ExitHandler {
145136
WExitHandler *callback;
146137
pid_t pid;
@@ -151,22 +142,34 @@ static CFMutableArrayRef appExitHandlers = NULL;
151142

152143
WMagicNumber wAddExitHandler(pid_t pid, WExitHandler *callback, void *cdata)
153144
{
154-
AppExitHandler *handler;
155-
156-
CFLog(kCFLogLevelInfo, CFSTR("%s: PID == %i"), __func__, pid);
157-
158-
handler = malloc(sizeof(AppExitHandler));
159-
if (!handler) {
160-
return 0;
161-
}
162-
handler->pid = pid;
163-
handler->callback = callback;
164-
handler->client_data = cdata;
145+
AppExitHandler *handler = NULL;
165146

166147
if (!appExitHandlers) {
167148
appExitHandlers = CFArrayCreateMutable(kCFAllocatorDefault, 8, NULL);
149+
} else {
150+
for (int i = CFArrayGetCount(appExitHandlers) - 1; i >= 0; i--) {
151+
handler = (AppExitHandler *)CFArrayGetValueAtIndex(appExitHandlers, i);
152+
if (handler->pid == pid) {
153+
break;
154+
} else {
155+
handler = NULL;
156+
}
157+
}
158+
}
159+
160+
if (handler == NULL) {
161+
// CFLog(kCFLogLevelInfo, CFSTR("%s: PID == %i"), __func__, pid);
162+
163+
handler = malloc(sizeof(AppExitHandler));
164+
if (!handler) {
165+
return 0;
166+
}
167+
handler->pid = pid;
168+
handler->callback = callback;
169+
handler->client_data = cdata;
170+
171+
CFArrayAppendValue(appExitHandlers, handler);
168172
}
169-
CFArrayAppendValue(appExitHandlers, handler);
170173

171174
return handler;
172175
}
@@ -191,15 +194,16 @@ void wNotifyProcessExit(pid_t pid, int status)
191194
{
192195
AppExitHandler *tmp;
193196

194-
CFLog(kCFLogLevelInfo, CFSTR("%s: PID == %i, exit status == %i"), __func__, pid, status);
197+
// CFLog(kCFLogLevelInfo, CFSTR("%s: PID == %i, exit status == %i"), __func__, pid,
198+
// WEXITSTATUS(status));
195199
if (!appExitHandlers) {
196200
return;
197201
}
198-
202+
199203
for (int i = CFArrayGetCount(appExitHandlers) - 1; i >= 0; i--) {
200204
tmp = (AppExitHandler *)CFArrayGetValueAtIndex(appExitHandlers, i);
201205
if (tmp && tmp->pid == pid) {
202-
(*tmp->callback)(tmp->pid, status, tmp->client_data);
206+
(*tmp->callback)(tmp->pid, WEXITSTATUS(status), tmp->client_data);
203207
_deleteExitHandler(tmp);
204208
}
205209
}

Applications/Workspace/WM/startup.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ static RETSIGTYPE _childExitHandler(int foo)
266266
sigset_t sigs;
267267

268268
sigfillset(&sigs);
269-
/* Block signals so that NotifyDeadProcess() doesn't get fux0red */
269+
/* Block signals so that wNotifyProcessExit() doesn't get fux0red */
270270
sigprocmask(SIG_BLOCK, &sigs, NULL);
271271

272272
/* If 2 or more kids exit in a small time window, before this handler gets
@@ -275,8 +275,11 @@ static RETSIGTYPE _childExitHandler(int foo)
275275
* exited child status because we can't count on the number of SIGCHLD
276276
* signals to know exactly how many kids have exited. -Dan
277277
*/
278+
// CFLog(kCFLogLevelError, CFSTR("%s: error #%i -- saved_errno %i"), __func__, errno, save_errno);
279+
278280
while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || (pid < 0 && errno == EINTR)) {
279-
wNotifyProcessExit(pid, WEXITSTATUS(status));
281+
// CFLog(kCFLogLevelError, CFSTR("%s: PID == %i exit status == %i"), __func__, pid, WEXITSTATUS(status));
282+
wNotifyProcessExit(pid, status);
280283
}
281284

282285
sigprocmask(SIG_UNBLOCK, &sigs, NULL);

Applications/Workspace/WM/wmspec.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,11 +1855,11 @@ Bool wNETWMProcessClientMessage(XClientMessageEvent *event)
18551855
}
18561856
last_event_serial = event->serial;
18571857

1858-
fprintf(stderr,
1859-
"%s: _NET_WM_MOVERESIZE: %li, %li | direction: %li, button: %li, source direction: %li "
1860-
"serial: %lu event type: %i\n",
1861-
__func__, event->data.l[0], event->data.l[1], event->data.l[2], event->data.l[3],
1862-
event->data.l[4], event->serial, event->type);
1858+
// fprintf(stderr,
1859+
// "%s: _NET_WM_MOVERESIZE: %li, %li | direction: %li, button: %li, source direction: %li "
1860+
// "serial: %lu event type: %i\n",
1861+
// __func__, event->data.l[0], event->data.l[1], event->data.l[2], event->data.l[3],
1862+
// event->data.l[4], event->serial, event->type);
18631863

18641864
xevent.xmotion.x_root = event->data.l[0];
18651865
xevent.xmotion.y_root = event->data.l[1];
@@ -1875,7 +1875,7 @@ Bool wNETWMProcessClientMessage(XClientMessageEvent *event)
18751875
GrabModeAsync, None, None, CurrentTime) == GrabSuccess) {
18761876
wMouseMoveWindow(wwin, &xevent);
18771877
XUngrabPointer(dpy, CurrentTime);
1878-
fprintf(stderr, "%s: _NET_WM_MOVERESIZE_MOVE finished\n", __func__);
1878+
// fprintf(stderr, "%s: _NET_WM_MOVERESIZE_MOVE finished\n", __func__);
18791879
}
18801880
break;
18811881
case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
@@ -1891,7 +1891,7 @@ Bool wNETWMProcessClientMessage(XClientMessageEvent *event)
18911891
GrabModeAsync, None, None, CurrentTime) == GrabSuccess) {
18921892
wMouseResizeWindow(wwin, &xevent, 1);
18931893
XUngrabPointer(dpy, CurrentTime);
1894-
fprintf(stderr, "%s: _NET_WM_MOVERESIZE_BOTTOMRIGHT finished\n", __func__);
1894+
// fprintf(stderr, "%s: _NET_WM_MOVERESIZE_BOTTOMRIGHT finished\n", __func__);
18951895
}
18961896
break;
18971897
}

0 commit comments

Comments
 (0)