source: trunk/dll/killproc.c@ 1398

Last change on this file since 1398 was 1398, checked in by Gregg Young, 16 years ago

Move embeded strings to PCSZ variables or string table; Eliminate Error2 functions Runtime_Error with NULL format string returns "No data" error. Change declares from PSZ to PCSZ in functions where the variable isn't changed. Added btm as an executable file type in several additional places. Use fProtectOnly to prevent attempt to execute Dos and Win programs on "Protect only" installs in several additional places.

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