source: trunk/dll/killproc.c@ 775

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

Minor clean up add comments re recent changes

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