source: trunk/dll/killproc.c@ 451

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

Comments

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