source: trunk/dll/killproc.c@ 404

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

Use xfgets

  • 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 404 2006-07-29 20:01:14Z 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 if(rc == -1) {
183 saymsg(MB_CANCEL,
184 hwnd,
185 GetPString(IDS_ARGHTEXT),
186 GetPString(IDS_CANTRUNPSTATTEXT));
187 goto Abort;
188 }
189 }
190 fp = fopen(s,"r");
191 if (fp) {
192 while (!feof(fp)) {
193 strset(s,0);
194 if (!xfgets(s,1025,fp,pszSrcFile,__LINE__))
195 break;
196 if (!foundstart) {
197 if (*s == ' ' && strstr(s,startstring))
198 foundstart = TRUE;
199 }
200 else {
201 if(*s == ' ' && strstr(s,endstring))
202 break;
203 if(*s == ' ' && s[5] == ' ' && isxdigit(s[1]) &&
204 isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4])) {
205 p = &s[1];
206 pid = strtol(&s[1],&p,16);
207 if(pid && pid != mypid) {
208 strcpy(progname,&s[30]);
209 p = strchr(progname,' ');
210 if(p)
211 *p = 0;
212 if(!stristr(progname,"\\PSTAT.EXE")) {
213 sprintf(s,"%04x %s",pid,progname);
214 WinSendDlgItemMsg(hwnd,
215 KILL_LISTBOX,
216 LM_INSERTITEM,
217 MPFROM2SHORT(LIT_SORTASCENDING,0),
218 MPFROMP(s));
219 }
220 }
221 }
222 }
223 }
224 fclose(fp);
225 }
226Abort:
227 DosForceDelete("$PSTAT#$.#$#");
228 PostMsg(hwnd,
229 UM_CONTAINER_FILLED,
230 MPVOID,
231 MPVOID);
232 WinDestroyMsgQueue(thmq);
233 WinTerminate(thab);
234}
235
236
237MRESULT EXPENTRY KillDlgProc (HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)
238{
239 SHORT sSelect;
240 PID pid;
241 static BOOL listdone;
242 static HPOINTER hptrIcon = (HPOINTER)0;
243
244 switch(msg) {
245 case WM_INITDLG:
246 hptrIcon = WinLoadPointer(HWND_DESKTOP,
247 FM3ModHandle,
248 KILL_FRAME);
249 WinDefDlgProc(hwnd,
250 WM_SETICON,
251 MPFROMLONG(hptrIcon),
252 MPVOID);
253 WinCheckButton(hwnd,
254 KILL_CHECKBOX,
255 fUseQProcStat);
256 PostMsg(hwnd,
257 WM_COMMAND,
258 MPFROM2SHORT(KILL_RESCAN,0),
259 MPVOID);
260 break;
261
262 case UM_CONTAINER_FILLED:
263 listdone = TRUE;
264 if((SHORT)WinSendDlgItemMsg(hwnd,
265 KILL_LISTBOX,
266 LM_QUERYITEMCOUNT,
267 MPVOID,
268 MPVOID) == 0) {
269 if(!fUseQProcStat)
270 saymsg(MB_CANCEL,
271 hwnd,
272 GetPString(IDS_ICHOKEDTEXT),
273 GetPString(IDS_ISPSTATTHERETEXT));
274 else
275 saymsg(MB_CANCEL,
276 hwnd,
277 GetPString(IDS_ICHOKEDTEXT),
278 GetPString(IDS_DOSQPROCSTATFAILEDTEXT));
279 }
280 return 0;
281
282 case WM_CONTROL:
283 switch(SHORT1FROMMP(mp1)) {
284 case KILL_CHECKBOX:
285 fUseQProcStat = WinQueryButtonCheckstate(hwnd,
286 KILL_CHECKBOX);
287 PrfWriteProfileData(fmprof,
288 FM3Str,
289 "UseQProcStat",
290 &fUseQProcStat,
291 sizeof(BOOL));
292 PostMsg(hwnd,
293 WM_COMMAND,
294 MPFROM2SHORT(KILL_RESCAN,0),
295 MPVOID);
296 break;
297
298 case KILL_LISTBOX:
299 switch(SHORT2FROMMP(mp2)) {
300 case LN_ENTER:
301 WinSendDlgItemMsg(hwnd,
302 DID_OK,
303 BM_CLICK,
304 MPFROMSHORT(TRUE),
305 MPVOID);
306 break;
307 }
308 break;
309
310 default:
311 break;
312 }
313 return 0;
314
315 case WM_ADJUSTWINDOWPOS:
316 PostMsg(hwnd,
317 UM_STRETCH,
318 MPVOID,
319 MPVOID);
320 break;
321
322 case UM_STRETCH:
323 {
324 SWP swpC,swp,swpH;
325
326 WinQueryWindowPos(hwnd,&swp);
327 if(!(swp.fl & (SWP_HIDE | SWP_MINIMIZE))) {
328 WinQueryWindowPos(WinWindowFromID(hwnd,KILL_LISTBOX),&swpC);
329 WinQueryWindowPos(WinWindowFromID(hwnd,KILL_HDR),&swpH);
330 WinSetWindowPos(WinWindowFromID(hwnd,KILL_LISTBOX),HWND_TOP,
331 SysVal(SV_CXSIZEBORDER),
332 swpC.y,
333 swp.cx - (SysVal(SV_CXSIZEBORDER) * 2),
334 ((swp.cy - swpC.y) - (SysVal(SV_CYTITLEBAR) +
335 SysVal(SV_CYSIZEBORDER)) -
336 (swpH.cy + 8)),
337 SWP_MOVE | SWP_SIZE);
338 WinSetWindowPos(WinWindowFromID(hwnd,KILL_HDR),HWND_TOP,
339 SysVal(SV_CXSIZEBORDER) + 4,
340 swpC.y + ((swp.cy - swpC.y) -
341 (SysVal(SV_CYTITLEBAR) +
342 SysVal(SV_CYSIZEBORDER)) -
343 (swpH.cy + 4)),
344 swpH.cx,
345 swpH.cy,
346 SWP_MOVE);
347 }
348 }
349 return 0;
350
351 case WM_COMMAND:
352 switch(SHORT1FROMMP(mp1)) {
353 case KILL_RESCAN:
354 listdone = FALSE;
355 if(fUseQProcStat) {
356 if(_beginthread(FillKillList2,
357 NULL,
358 65536,
359 (PVOID)&hwnd) != -1)
360 DosSleep(250L);
361 else
362 WinDismissDlg(hwnd,0);
363 }
364 else {
365 if(_beginthread(FillKillList,
366 NULL,
367 65536,
368 (PVOID)&hwnd) != -1)
369 DosSleep(250L);
370 else
371 WinDismissDlg(hwnd,0);
372 }
373 break;
374
375 case KILL_SHOW:
376 case DID_OK:
377 sSelect = (USHORT)WinSendDlgItemMsg(hwnd,
378 KILL_LISTBOX,
379 LM_QUERYSELECTION,
380 MPFROMSHORT(LIT_FIRST),
381 MPVOID);
382 if(sSelect >= 0) {
383
384 CHAR s[31],*p;
385 APIRET error;
386
387 *s = 0;
388 WinSendDlgItemMsg(hwnd,
389 KILL_LISTBOX,
390 LM_QUERYITEMTEXT,
391 MPFROM2SHORT(sSelect,30),
392 MPFROMP(s));
393 if(*s) {
394 p = s;
395 pid = strtol(s,&p,16);
396 if(pid) {
397 if(SHORT1FROMMP(mp1) == DID_OK) {
398 error = DosKillProcess(DKP_PROCESS,pid);
399 if(error && error != ERROR_INVALID_PROCID) {
400 Dos_Error(MB_CANCEL,
401 error,
402 hwnd,
403 __FILE__,
404 __LINE__,
405 GetPString(IDS_DOSKILLFAILEDTEXT));
406 }
407 else
408 WinSendDlgItemMsg(hwnd,
409 KILL_LISTBOX,
410 LM_DELETEITEM,
411 MPFROM2SHORT(sSelect,0),
412 MPVOID);
413 }
414 else if(!ShowSession(hwnd,pid))
415 Notify(GetPString(IDS_SORRYCANTSHOWTEXT));
416 }
417 }
418 }
419 break;
420
421 case DID_CANCEL:
422 if(!listdone)
423 Runtime_Error(pszSrcFile, __LINE__, "busy");
424 else
425 WinDismissDlg(hwnd,0);
426 break;
427
428 case IDM_HELP:
429 saymsg(MB_ENTER | MB_ICONASTERISK,
430 hwnd,
431 GetPString(IDS_KILLPROCHELPTITLETEXT),
432 GetPString(IDS_KILLPROCHELPTEXT));
433 break;
434 }
435 return 0;
436
437 case WM_CLOSE:
438 if(!listdone) {
439 Runtime_Error(pszSrcFile, __LINE__, "busy");
440 return 0;
441 }
442 break;
443
444 case WM_DESTROY:
445 if(hptrIcon)
446 WinDestroyPointer(hptrIcon);
447 hptrIcon = (HPOINTER)0;
448 break;
449 }
450 return WinDefDlgProc(hwnd,msg,mp1,mp2);
451}
452
Note: See TracBrowser for help on using the repository browser.