source: trunk/dll/killproc.c@ 1063

Last change on this file since 1063 was 1063, checked in by Gregg Young, 17 years ago

Fortify ifdef reformat

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
Line 
1
2/***********************************************************************
3
4 $Id: killproc.c 1063 2008-07-11 03:33:36Z gyoung $
5
6 Kill a process
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2005, 2006 Steven H. Levine
10
11 24 May 05 SHL Rework Win_Error usage
12 14 Jul 06 SHL Use Runtime_Error
13 29 Jul 06 SHL Use xfgets
14 03 Nov 06 SHL Renames
15 03 Nov 06 SHL Count thread usage
16 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
17 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
18 02 Sep 07 GKY Replaced DosQProcStatus with DosQuerySysState to fix trap in thunk code
19 02 Sep 07 SHL Expand FillKillListThread2 stack to avoid exception in __TNK
20
21***********************************************************************/
22
23#include <stdlib.h>
24#include <string.h>
25#include <ctype.h>
26#include <process.h>
27#include <limits.h>
28
29#define INCL_DOS
30#define INCL_DOSERRORS
31#define INCL_WIN
32#define INCL_LONGLONG // dircnrs.h
33
34#include "fm3dlg.h"
35#include "fm3str.h"
36#include "procstat.h"
37#include "errutil.h" // Dos_Error...
38#include "strutil.h" // GetPString
39#include "fm3dll.h"
40#include "fortify.h"
41
42#pragma data_seg(DATA2)
43
44static PSZ pszSrcFile = __FILE__;
45
46CHAR *GetDosPgmName(PID pid, CHAR * string)
47{
48 HSWITCH hs;
49 SWCNTRL swctl;
50 PCH pch;
51
52 *string = 0;
53 hs = WinQuerySwitchHandle(0, pid);
54 if (hs) {
55 WinQuerySwitchEntry(hs, &swctl);
56 pch = swctl.szSwtitle;
57 while (*pch) {
58 if (*pch < 0x10)
59 if (pch != swctl.szSwtitle && *(pch - 1) == 0x20)
60 memmove(pch, pch + 1, strlen(pch));
61 else {
62 *pch = 0x20;
63 pch++;
64 }
65 else
66 pch++;
67 }
68 strcpy(string, swctl.szSwtitle);
69 }
70 if (!*string)
71 strcpy(string, GetPString(IDS_UNKNOWNDOSPROCTEXT));
72 return string;
73}
74
75static VOID FillKillListThread2(VOID * arg)
76{
77 HWND hwnd = *(HWND *) arg;
78 CHAR s[1036];
79 HAB thab;
80 HMQ thmq;
81 INT rc;
82 PROCESSINFO *ppi;
83 BUFFHEADER *pbh;
84 MODINFO *pmi;
85
86# ifdef FORTIFY
87 Fortify_EnterScope();
88# endif
89 thab = WinInitialize(0);
90 thmq = WinCreateMsgQueue(thab, 0);
91 WinCancelShutdown(thmq, TRUE);
92 IncrThreadUsage();
93
94 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
95 rc = DosAllocMem((PVOID) & pbh, USHRT_MAX + 4096,
96 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
97 if (rc)
98 Dos_Error(MB_CANCEL, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
99 GetPString(IDS_OUTOFMEMORY));
100 else {
101 rc = DosQProcStatus((ULONG *)pbh, USHRT_MAX);
102 if (!rc) {
103 ppi = pbh->ppi;
104 while (ppi->ulEndIndicator != PROCESS_END_INDICATOR) {
105 if (ppi->pid != mypid) {
106 pmi = pbh->pmi;
107 while (pmi && ppi->hModRef != pmi->hMod)
108 pmi = pmi->pNext;
109 if (pmi) {
110 sprintf(s, "%04x ", ppi->pid);
111 if (!stricmp(pmi->szModName, "SYSINIT"))
112 GetDosPgmName(ppi->pid, s + strlen(s));
113 else {
114 if (*pmi->szModName)
115 strcat(s, pmi->szModName);
116 else
117 strcat(s, GetPString(IDS_UNKNOWNPROCTEXT));
118 }
119 if (WinIsWindow(thab, hwnd)) {
120 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_INSERTITEM,
121 MPFROM2SHORT(LIT_SORTASCENDING, 0),
122 MPFROMP(s));
123 }
124 else
125 break;
126 }
127 }
128 ppi = (PPROCESSINFO) (ppi->ptiFirst + ppi->usThreadCount);
129 } // while
130 }
131 DosFreeMem(pbh);
132 }
133
134 if (WinIsWindow(thab, hwnd))
135 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
136 WinDestroyMsgQueue(thmq);
137 DecrThreadUsage();
138 WinTerminate(thab);
139# ifdef FORTIFY
140 Fortify_LeaveScope();
141# endif
142}
143
144static VOID FillKillListThread3(VOID * arg)
145{
146 HWND hwnd = *(HWND *) arg;
147 CHAR s[1036];
148 HAB thab;
149 HMQ thmq;
150 INT rc;
151 QSPREC *ppi;
152 QSPTRREC *pbh;
153 QSLREC *pmi;
154
155# ifdef FORTIFY
156 Fortify_EnterScope();
157# endif
158 thab = WinInitialize(0);
159 thmq = WinCreateMsgQueue(thab, 0);
160 WinCancelShutdown(thmq, TRUE);
161 IncrThreadUsage();
162
163 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
164 rc = DosAllocMem((PVOID) & pbh, USHRT_MAX + 4096,
165 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
166 if (rc)
167 Dos_Error(MB_CANCEL, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
168 GetPString(IDS_OUTOFMEMORY));
169 else {
170 rc = DosQuerySysState(QS_PROCESS | QS_MTE, 0, 0, 0, pbh, USHRT_MAX);
171 if (!rc) {
172 ppi = pbh->pProcRec;
173 while (ppi->RecType == 1) {
174 if (ppi->pid != mypid) {
175 pmi = pbh->pLibRec;
176 while (pmi && ppi->hMte != pmi->hmte)
177 pmi = pmi->pNextRec;
178 if (pmi) {
179 sprintf(s, "%04x ", ppi->pid);
180 if (!stricmp((CHAR *) pmi->pName, "SYSINIT"))
181 GetDosPgmName(ppi->pid, s + strlen(s));
182 else {
183 if (*pmi->pName)
184 strcat(s, (CHAR *) pmi->pName);
185 else
186 strcat(s, GetPString(IDS_UNKNOWNPROCTEXT));
187 }
188 if (WinIsWindow(thab, hwnd)) {
189 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_INSERTITEM,
190 MPFROM2SHORT(LIT_SORTASCENDING, 0),
191 MPFROMP(s));
192 }
193 else
194 break;
195 }
196 }
197 ppi = (QSPREC *) (ppi->pThrdRec + ppi->cTCB);
198 } // while
199 }
200 DosFreeMem(pbh);
201 }
202
203 if (WinIsWindow(thab, hwnd))
204 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
205 WinDestroyMsgQueue(thmq);
206 DecrThreadUsage();
207 WinTerminate(thab);
208# ifdef FORTIFY
209 Fortify_LeaveScope();
210# endif
211}
212
213static VOID FillKillListThread(VOID * arg)
214{
215 HWND hwnd = *(HWND *) arg;
216 CHAR s[1036], progname[1027], *p;
217 HAB thab;
218 HMQ thmq;
219 FILE *fp;
220 BOOL foundstart = FALSE;
221 INT rc;
222 CHAR *startstring = "Process and Thread Information";
223 CHAR *endstring = "System Semaphore Information";
224 PID pid;
225 HFILE oldstdout, newstdout;
226
227 DosError(FERR_DISABLEHARDERR);
228
229# ifdef FORTIFY
230 Fortify_EnterScope();
231# endif
232 thab = WinInitialize(0);
233 thmq = WinCreateMsgQueue(thab, 0);
234 WinCancelShutdown(thmq, TRUE);
235 IncrThreadUsage();
236
237 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
238 strcpy(s, "$PSTAT#$.#$#");
239 unlinkf("%s", s);
240 fp = fopen(s, "w");
241 if (!fp) {
242 Win_Error(NULLHANDLE, HWND_DESKTOP, __FILE__, __LINE__,
243 GetPString(IDS_REDIRECTERRORTEXT));
244 goto Abort;
245 }
246 else {
247 newstdout = -1;
248 rc = DosDupHandle(fileno(stdout), &newstdout);
249 if (rc)
250 Dos_Error(MB_CANCEL, rc, hwnd, __FILE__, __LINE__, "DosDupHandle");
251 oldstdout = fileno(stdout);
252 DosDupHandle(fileno(fp), &oldstdout);
253 rc = runemf2(SEPARATE | INVISIBLE | FULLSCREEN | BACKGROUND | WAIT,
254 hwnd, pszSrcFile, __LINE__, NULL, NULL,
255 "%s", "PSTAT.EXE /C");
256 oldstdout = fileno(stdout);
257 DosDupHandle(newstdout, &oldstdout);
258 DosClose(newstdout);
259 fclose(fp);
260 // fixme to be gone?
261 if (rc == -1) {
262 saymsg(MB_CANCEL,
263 hwnd,
264 GetPString(IDS_ARGHTEXT), GetPString(IDS_CANTRUNPSTATTEXT));
265 goto Abort;
266 }
267 }
268 fp = fopen(s, "r");
269 if (fp) {
270 while (!feof(fp)) {
271 strset(s, 0);
272 if (!xfgets(s, 1025, fp, pszSrcFile, __LINE__))
273 break;
274 if (!foundstart) {
275 if (*s == ' ' && strstr(s, startstring))
276 foundstart = TRUE;
277 }
278 else {
279 if (*s == ' ' && strstr(s, endstring))
280 break;
281 if (*s == ' ' && s[5] == ' ' && isxdigit(s[1]) &&
282 isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4])) {
283 p = &s[1];
284 pid = strtol(&s[1], &p, 16);
285 if (pid && pid != mypid) {
286 strcpy(progname, &s[30]);
287 p = strchr(progname, ' ');
288 if (p)
289 *p = 0;
290 if (!stristr(progname, "\\PSTAT.EXE")) {
291 sprintf(s, "%04x %s", pid, progname);
292 WinSendDlgItemMsg(hwnd,
293 KILL_LISTBOX,
294 LM_INSERTITEM,
295 MPFROM2SHORT(LIT_SORTASCENDING, 0),
296 MPFROMP(s));
297 }
298 }
299 }
300 }
301 }
302 fclose(fp);
303 }
304Abort:
305 DosForceDelete("$PSTAT#$.#$#");
306 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
307 WinDestroyMsgQueue(thmq);
308 DecrThreadUsage();
309 WinTerminate(thab);
310# ifdef FORTIFY
311 Fortify_LeaveScope();
312# endif
313}
314
315MRESULT EXPENTRY KillDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
316{
317 SHORT sSelect;
318 PID pid;
319 static BOOL listdone;
320 static HPOINTER hptrIcon = (HPOINTER) 0;
321
322 switch (msg) {
323 case WM_INITDLG:
324 hptrIcon = WinLoadPointer(HWND_DESKTOP, FM3ModHandle, KILL_FRAME);
325 WinDefDlgProc(hwnd, WM_SETICON, MPFROMLONG(hptrIcon), MPVOID);
326 WinCheckButton(hwnd, KILL_CHECKBOX, fUseQProcStat);
327 WinCheckButton(hwnd, KILL2_CHECKBOX, fUseQSysState);
328 if (WinQueryButtonCheckstate(hwnd, KILL2_CHECKBOX)) {
329 WinCheckButton(hwnd, KILL_CHECKBOX, FALSE);
330 WinEnableWindow(WinWindowFromID(hwnd, KILL_CHECKBOX), FALSE);
331 }
332 if (WinQueryButtonCheckstate(hwnd, KILL_CHECKBOX)) {
333 WinCheckButton(hwnd, KILL2_CHECKBOX, FALSE);
334 WinEnableWindow(WinWindowFromID(hwnd, KILL2_CHECKBOX), FALSE);
335 }
336 PostMsg(hwnd, WM_COMMAND, MPFROM2SHORT(KILL_RESCAN, 0), MPVOID);
337 break;
338
339 case UM_CONTAINER_FILLED:
340 listdone = TRUE;
341 if ((SHORT) WinSendDlgItemMsg(hwnd,
342 KILL_LISTBOX,
343 LM_QUERYITEMCOUNT, MPVOID, MPVOID) == 0) {
344 if (!fUseQProcStat)
345 saymsg(MB_CANCEL,
346 hwnd,
347 GetPString(IDS_ICHOKEDTEXT), GetPString(IDS_ISPSTATTHERETEXT));
348 else
349 saymsg(MB_CANCEL,
350 hwnd,
351 GetPString(IDS_ICHOKEDTEXT),
352 GetPString(IDS_DOSQPROCSTATFAILEDTEXT));
353 }
354 return 0;
355
356 case WM_CONTROL:
357 switch (SHORT1FROMMP(mp1)) {
358 case KILL_CHECKBOX:
359 fUseQProcStat = WinQueryButtonCheckstate(hwnd, KILL_CHECKBOX);
360 PrfWriteProfileData(fmprof,
361 FM3Str,
362 "UseQProcStat", &fUseQProcStat, sizeof(BOOL));
363 PostMsg(hwnd, WM_COMMAND, MPFROM2SHORT(KILL_RESCAN, 0), MPVOID);
364 if (WinQueryButtonCheckstate(hwnd, KILL_CHECKBOX)) {
365 WinCheckButton(hwnd, KILL2_CHECKBOX, FALSE);
366 WinEnableWindow(WinWindowFromID(hwnd, KILL2_CHECKBOX), FALSE);
367 }
368 else
369 WinEnableWindow(WinWindowFromID(hwnd, KILL2_CHECKBOX), TRUE);
370 break;
371 case KILL2_CHECKBOX:
372 fUseQSysState = WinQueryButtonCheckstate(hwnd, KILL2_CHECKBOX);
373 PrfWriteProfileData(fmprof,
374 FM3Str,
375 "UseQSysState", &fUseQSysState, sizeof(BOOL));
376 PostMsg(hwnd, WM_COMMAND, MPFROM2SHORT(KILL_RESCAN, 0), MPVOID);
377 if (WinQueryButtonCheckstate(hwnd, KILL2_CHECKBOX)) {
378 WinCheckButton(hwnd, KILL_CHECKBOX, FALSE);
379 WinEnableWindow(WinWindowFromID(hwnd, KILL_CHECKBOX), FALSE);
380 }
381 else
382 WinEnableWindow(WinWindowFromID(hwnd, KILL_CHECKBOX), TRUE);
383 break;
384
385 case KILL_LISTBOX:
386 switch (SHORT2FROMMP(mp2)) {
387 case LN_ENTER:
388 WinSendDlgItemMsg(hwnd, DID_OK, BM_CLICK, MPFROMSHORT(TRUE), MPVOID);
389 break;
390 }
391 break;
392
393 default:
394 break;
395 }
396 return 0;
397
398 case WM_ADJUSTWINDOWPOS:
399 PostMsg(hwnd, UM_STRETCH, MPVOID, MPVOID);
400 break;
401
402 case UM_STRETCH:
403 {
404 SWP swpC, swp, swpH;
405
406 WinQueryWindowPos(hwnd, &swp);
407 if (!(swp.fl & (SWP_HIDE | SWP_MINIMIZE))) {
408 WinQueryWindowPos(WinWindowFromID(hwnd, KILL_LISTBOX), &swpC);
409 WinQueryWindowPos(WinWindowFromID(hwnd, KILL_HDR), &swpH);
410 WinSetWindowPos(WinWindowFromID(hwnd, KILL_LISTBOX), HWND_TOP,
411 SysVal(SV_CXSIZEBORDER),
412 swpC.y,
413 swp.cx - (SysVal(SV_CXSIZEBORDER) * 2),
414 ((swp.cy - swpC.y) - (SysVal(SV_CYTITLEBAR) +
415 SysVal(SV_CYSIZEBORDER)) -
416 (swpH.cy + 8)), SWP_MOVE | SWP_SIZE);
417 WinSetWindowPos(WinWindowFromID(hwnd, KILL_HDR), HWND_TOP,
418 SysVal(SV_CXSIZEBORDER) + 4,
419 swpC.y + ((swp.cy - swpC.y) -
420 (SysVal(SV_CYTITLEBAR) +
421 SysVal(SV_CYSIZEBORDER)) -
422 (swpH.cy + 4)), swpH.cx, swpH.cy, SWP_MOVE);
423 }
424 }
425 return 0;
426
427 case WM_COMMAND:
428 switch (SHORT1FROMMP(mp1)) {
429 case KILL_RESCAN:
430 listdone = FALSE;
431 if (fUseQProcStat) {
432 if (_beginthread(FillKillListThread2,
433 NULL, 65536 + 8192, (PVOID)&hwnd) != -1)
434 DosSleep(100); // 05 Aug 07 GKY 250
435 else
436 WinDismissDlg(hwnd, 0);
437 }
438 else if (fUseQSysState)
439 if (_beginthread(FillKillListThread3,
440 NULL, 65536, (PVOID) & hwnd) != -1)
441 DosSleep(100);//05 Aug 07 GKY 250
442 else
443 WinDismissDlg(hwnd, 0);
444 else {
445 if (_beginthread(FillKillListThread,
446 NULL, 65536, (PVOID) & hwnd) != -1)
447 DosSleep(100); // 05 Aug 07 GKY 250
448 else
449 WinDismissDlg(hwnd, 0);
450 }
451 break;
452
453 case KILL_SHOW:
454 case DID_OK:
455 sSelect = (USHORT) WinSendDlgItemMsg(hwnd,
456 KILL_LISTBOX,
457 LM_QUERYSELECTION,
458 MPFROMSHORT(LIT_FIRST), MPVOID);
459 if (sSelect >= 0) {
460
461 CHAR s[31], *p;
462 APIRET error;
463
464 *s = 0;
465 WinSendDlgItemMsg(hwnd,
466 KILL_LISTBOX,
467 LM_QUERYITEMTEXT,
468 MPFROM2SHORT(sSelect, 30), MPFROMP(s));
469 if (*s) {
470 p = s;
471 pid = strtol(s, &p, 16);
472 if (pid) {
473 if (SHORT1FROMMP(mp1) == DID_OK) {
474 error = DosKillProcess(DKP_PROCESS, pid);
475 if (error && error != ERROR_INVALID_PROCID) {
476 Dos_Error(MB_CANCEL,
477 error,
478 hwnd,
479 __FILE__,
480 __LINE__, GetPString(IDS_DOSKILLFAILEDTEXT));
481 }
482 else
483 WinSendDlgItemMsg(hwnd,
484 KILL_LISTBOX,
485 LM_DELETEITEM,
486 MPFROM2SHORT(sSelect, 0), MPVOID);
487 }
488 else if (!ShowSession(hwnd, pid))
489 Notify(GetPString(IDS_SORRYCANTSHOWTEXT));
490 }
491 }
492 }
493 break;
494
495 case DID_CANCEL:
496 if (!listdone)
497 Runtime_Error(pszSrcFile, __LINE__, "busy");
498 else
499 WinDismissDlg(hwnd, 0);
500 break;
501
502 case IDM_HELP:
503 saymsg(MB_ENTER | MB_ICONASTERISK,
504 hwnd,
505 GetPString(IDS_KILLPROCHELPTITLETEXT),
506 GetPString(IDS_KILLPROCHELPTEXT));
507 break;
508 }
509 return 0;
510
511 case WM_CLOSE:
512 if (!listdone) {
513 Runtime_Error(pszSrcFile, __LINE__, "busy");
514 return 0;
515 }
516 break;
517
518 case WM_DESTROY:
519 if (hptrIcon)
520 WinDestroyPointer(hptrIcon);
521 hptrIcon = (HPOINTER) 0;
522 break;
523 }
524 return WinDefDlgProc(hwnd, msg, mp1, mp2);
525}
526
527#pragma alloc_text(KILLPROC,FillKillListThread,FillKillListThread2,GetDosPgmName,KillDlgProc)
528#pragma alloc_text(KILLPROC,FillKillListThread3)
Note: See TracBrowser for help on using the repository browser.