source: trunk/dll/killproc.c@ 771

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

Increase subject to 1024 reduce DosSleep times

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