source: trunk/dll/killproc.c

Last change on this file was 1880, checked in by Gregg Young, 10 years ago

Remove dead code and comments from remaining c files. #if 0 and #if NEVER were not addressed

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