source: trunk/dll/killproc.c@ 793

Last change on this file since 793 was 793, checked in by Gregg Young, 18 years ago

Move #pragma alloc_text to end for OpenWatcom compat

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