source: trunk/dll/killproc.c@ 533

Last change on this file since 533 was 533, checked in by root, 19 years ago

Renames
Count thread usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1
2/***********************************************************************
3
4 $Id: killproc.c 533 2006-11-04 21:07:44Z root $
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
72
73static VOID FillKillListThread2 (VOID *arg)
74{
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;
83
84 thab = WinInitialize(0);
85 thmq = WinCreateMsgQueue(thab,0);
86 WinCancelShutdown(thmq,TRUE);
87 IncrThreadUsage();
88
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);
92 if (rc)
93 Dos_Error(MB_CANCEL,rc,HWND_DESKTOP,pszSrcFile,__LINE__,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
135
136static VOID FillKillListThread (VOID *arg)
137{
138 HWND hwnd = *(HWND *)arg;
139 CHAR s[1036],progname[1027],*p;
140 HAB thab;
141 HMQ thmq;
142 FILE *fp;
143 BOOL foundstart = FALSE;
144 INT rc;
145 CHAR *startstring = "Process and Thread Information";
146 CHAR *endstring = "System Semaphore Information";
147 PID pid;
148 HFILE oldstdout,newstdout;
149
150 DosError(FERR_DISABLEHARDERR);
151
152 thab = WinInitialize(0);
153 thmq = WinCreateMsgQueue(thab,0);
154 WinCancelShutdown(thmq,TRUE);
155 IncrThreadUsage();
156
157 WinSendDlgItemMsg(hwnd,
158 KILL_LISTBOX,
159 LM_DELETEALL,
160 MPVOID,
161 MPVOID);
162 strcpy(s,"$PSTAT#$.#$#");
163 unlinkf("%s",s);
164 fp = fopen(s,"w");
165 if(!fp) {
166 Win_Error(NULLHANDLE,HWND_DESKTOP,__FILE__,__LINE__,
167 GetPString(IDS_REDIRECTERRORTEXT));
168 goto Abort;
169 }
170 else {
171 newstdout = -1;
172 rc = DosDupHandle(fileno(stdout),&newstdout);
173 if (rc)
174 Dos_Error(MB_CANCEL,rc,hwnd,__FILE__,__LINE__,"DosDupHandle");
175 oldstdout = fileno(stdout);
176 DosDupHandle(fileno(fp),&oldstdout);
177 rc = runemf2(SEPARATE | INVISIBLE | FULLSCREEN | BACKGROUND | WAIT,
178 hwnd,
179 NULL,
180 NULL,
181 "%s",
182 "PSTAT.EXE /C");
183 oldstdout = fileno(stdout);
184 DosDupHandle(newstdout,&oldstdout);
185 DosClose(newstdout);
186 fclose(fp);
187 // fixme to be gone?
188 if(rc == -1) {
189 saymsg(MB_CANCEL,
190 hwnd,
191 GetPString(IDS_ARGHTEXT),
192 GetPString(IDS_CANTRUNPSTATTEXT));
193 goto Abort;
194 }
195 }
196 fp = fopen(s,"r");
197 if (fp) {
198 while (!feof(fp)) {
199 strset(s,0);
200 if (!xfgets(s,1025,fp,pszSrcFile,__LINE__))
201 break;
202 if (!foundstart) {
203 if (*s == ' ' && strstr(s,startstring))
204 foundstart = TRUE;
205 }
206 else {
207 if(*s == ' ' && strstr(s,endstring))
208 break;
209 if(*s == ' ' && s[5] == ' ' && isxdigit(s[1]) &&
210 isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4])) {
211 p = &s[1];
212 pid = strtol(&s[1],&p,16);
213 if(pid && pid != mypid) {
214 strcpy(progname,&s[30]);
215 p = strchr(progname,' ');
216 if(p)
217 *p = 0;
218 if(!stristr(progname,"\\PSTAT.EXE")) {
219 sprintf(s,"%04x %s",pid,progname);
220 WinSendDlgItemMsg(hwnd,
221 KILL_LISTBOX,
222 LM_INSERTITEM,
223 MPFROM2SHORT(LIT_SORTASCENDING,0),
224 MPFROMP(s));
225 }
226 }
227 }
228 }
229 }
230 fclose(fp);
231 }
232Abort:
233 DosForceDelete("$PSTAT#$.#$#");
234 PostMsg(hwnd,UM_CONTAINER_FILLED,MPVOID,MPVOID);
235 WinDestroyMsgQueue(thmq);
236 DecrThreadUsage();
237 WinTerminate(thab);
238}
239
240
241MRESULT EXPENTRY KillDlgProc (HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)
242{
243 SHORT sSelect;
244 PID pid;
245 static BOOL listdone;
246 static HPOINTER hptrIcon = (HPOINTER)0;
247
248 switch(msg) {
249 case WM_INITDLG:
250 hptrIcon = WinLoadPointer(HWND_DESKTOP,
251 FM3ModHandle,
252 KILL_FRAME);
253 WinDefDlgProc(hwnd,
254 WM_SETICON,
255 MPFROMLONG(hptrIcon),
256 MPVOID);
257 WinCheckButton(hwnd,
258 KILL_CHECKBOX,
259 fUseQProcStat);
260 PostMsg(hwnd,
261 WM_COMMAND,
262 MPFROM2SHORT(KILL_RESCAN,0),
263 MPVOID);
264 break;
265
266 case UM_CONTAINER_FILLED:
267 listdone = TRUE;
268 if((SHORT)WinSendDlgItemMsg(hwnd,
269 KILL_LISTBOX,
270 LM_QUERYITEMCOUNT,
271 MPVOID,
272 MPVOID) == 0) {
273 if(!fUseQProcStat)
274 saymsg(MB_CANCEL,
275 hwnd,
276 GetPString(IDS_ICHOKEDTEXT),
277 GetPString(IDS_ISPSTATTHERETEXT));
278 else
279 saymsg(MB_CANCEL,
280 hwnd,
281 GetPString(IDS_ICHOKEDTEXT),
282 GetPString(IDS_DOSQPROCSTATFAILEDTEXT));
283 }
284 return 0;
285
286 case WM_CONTROL:
287 switch(SHORT1FROMMP(mp1)) {
288 case KILL_CHECKBOX:
289 fUseQProcStat = WinQueryButtonCheckstate(hwnd,
290 KILL_CHECKBOX);
291 PrfWriteProfileData(fmprof,
292 FM3Str,
293 "UseQProcStat",
294 &fUseQProcStat,
295 sizeof(BOOL));
296 PostMsg(hwnd,
297 WM_COMMAND,
298 MPFROM2SHORT(KILL_RESCAN,0),
299 MPVOID);
300 break;
301
302 case KILL_LISTBOX:
303 switch(SHORT2FROMMP(mp2)) {
304 case LN_ENTER:
305 WinSendDlgItemMsg(hwnd,
306 DID_OK,
307 BM_CLICK,
308 MPFROMSHORT(TRUE),
309 MPVOID);
310 break;
311 }
312 break;
313
314 default:
315 break;
316 }
317 return 0;
318
319 case WM_ADJUSTWINDOWPOS:
320 PostMsg(hwnd,
321 UM_STRETCH,
322 MPVOID,
323 MPVOID);
324 break;
325
326 case UM_STRETCH:
327 {
328 SWP swpC,swp,swpH;
329
330 WinQueryWindowPos(hwnd,&swp);
331 if(!(swp.fl & (SWP_HIDE | SWP_MINIMIZE))) {
332 WinQueryWindowPos(WinWindowFromID(hwnd,KILL_LISTBOX),&swpC);
333 WinQueryWindowPos(WinWindowFromID(hwnd,KILL_HDR),&swpH);
334 WinSetWindowPos(WinWindowFromID(hwnd,KILL_LISTBOX),HWND_TOP,
335 SysVal(SV_CXSIZEBORDER),
336 swpC.y,
337 swp.cx - (SysVal(SV_CXSIZEBORDER) * 2),
338 ((swp.cy - swpC.y) - (SysVal(SV_CYTITLEBAR) +
339 SysVal(SV_CYSIZEBORDER)) -
340 (swpH.cy + 8)),
341 SWP_MOVE | SWP_SIZE);
342 WinSetWindowPos(WinWindowFromID(hwnd,KILL_HDR),HWND_TOP,
343 SysVal(SV_CXSIZEBORDER) + 4,
344 swpC.y + ((swp.cy - swpC.y) -
345 (SysVal(SV_CYTITLEBAR) +
346 SysVal(SV_CYSIZEBORDER)) -
347 (swpH.cy + 4)),
348 swpH.cx,
349 swpH.cy,
350 SWP_MOVE);
351 }
352 }
353 return 0;
354
355 case WM_COMMAND:
356 switch(SHORT1FROMMP(mp1)) {
357 case KILL_RESCAN:
358 listdone = FALSE;
359 if(fUseQProcStat) {
360 if(_beginthread(FillKillListThread2,
361 NULL,
362 65536,
363 (PVOID)&hwnd) != -1)
364 DosSleep(250L);
365 else
366 WinDismissDlg(hwnd,0);
367 }
368 else {
369 if(_beginthread(FillKillListThread,
370 NULL,
371 65536,
372 (PVOID)&hwnd) != -1)
373 DosSleep(250L);
374 else
375 WinDismissDlg(hwnd,0);
376 }
377 break;
378
379 case KILL_SHOW:
380 case DID_OK:
381 sSelect = (USHORT)WinSendDlgItemMsg(hwnd,
382 KILL_LISTBOX,
383 LM_QUERYSELECTION,
384 MPFROMSHORT(LIT_FIRST),
385 MPVOID);
386 if(sSelect >= 0) {
387
388 CHAR s[31],*p;
389 APIRET error;
390
391 *s = 0;
392 WinSendDlgItemMsg(hwnd,
393 KILL_LISTBOX,
394 LM_QUERYITEMTEXT,
395 MPFROM2SHORT(sSelect,30),
396 MPFROMP(s));
397 if(*s) {
398 p = s;
399 pid = strtol(s,&p,16);
400 if(pid) {
401 if(SHORT1FROMMP(mp1) == DID_OK) {
402 error = DosKillProcess(DKP_PROCESS,pid);
403 if(error && error != ERROR_INVALID_PROCID) {
404 Dos_Error(MB_CANCEL,
405 error,
406 hwnd,
407 __FILE__,
408 __LINE__,
409 GetPString(IDS_DOSKILLFAILEDTEXT));
410 }
411 else
412 WinSendDlgItemMsg(hwnd,
413 KILL_LISTBOX,
414 LM_DELETEITEM,
415 MPFROM2SHORT(sSelect,0),
416 MPVOID);
417 }
418 else if(!ShowSession(hwnd,pid))
419 Notify(GetPString(IDS_SORRYCANTSHOWTEXT));
420 }
421 }
422 }
423 break;
424
425 case DID_CANCEL:
426 if(!listdone)
427 Runtime_Error(pszSrcFile, __LINE__, "busy");
428 else
429 WinDismissDlg(hwnd,0);
430 break;
431
432 case IDM_HELP:
433 saymsg(MB_ENTER | MB_ICONASTERISK,
434 hwnd,
435 GetPString(IDS_KILLPROCHELPTITLETEXT),
436 GetPString(IDS_KILLPROCHELPTEXT));
437 break;
438 }
439 return 0;
440
441 case WM_CLOSE:
442 if(!listdone) {
443 Runtime_Error(pszSrcFile, __LINE__, "busy");
444 return 0;
445 }
446 break;
447
448 case WM_DESTROY:
449 if(hptrIcon)
450 WinDestroyPointer(hptrIcon);
451 hptrIcon = (HPOINTER)0;
452 break;
453 }
454 return WinDefDlgProc(hwnd,msg,mp1,mp2);
455}
456
Note: See TracBrowser for help on using the repository browser.