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
RevLine 
[145]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
[350]9 Copyright (c) 2005, 2006 Steven H. Levine
[145]10
11 24 May 05 SHL Rework Win_Error usage
[350]12 14 Jul 06 SHL Use Runtime_Error
[404]13 29 Jul 06 SHL Use xfgets
[533]14 03 Nov 06 SHL Renames
15 03 Nov 06 SHL Count thread usage
[775]16 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
[145]17
[775]18
[145]19***********************************************************************/
20
[2]21#define INCL_DOSERRORS
22#define INCL_DOS
23#define INCL_WIN
[350]24#include <os2.h>
[2]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>
[350]33
[2]34#include "procstat.h"
35#include "fm3dll.h"
36#include "fm3dlg.h"
37#include "fm3str.h"
38
39#pragma data_seg(DATA2)
40
[350]41static PSZ pszSrcFile = __FILE__;
[2]42
[533]43#pragma alloc_text(KILLPROC,FillKillListThread,FillKillListThread2,GetDosPgmName,KillDlgProc)
[2]44
[551]45CHAR *GetDosPgmName(PID pid, CHAR * string)
[350]46{
[2]47 HSWITCH hs;
48 SWCNTRL swctl;
[551]49 PCH pch;
[2]50
51 *string = 0;
[551]52 hs = WinQuerySwitchHandle(0, pid);
53 if (hs) {
54 WinQuerySwitchEntry(hs, &swctl);
[2]55 pch = swctl.szSwtitle;
[551]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 }
[2]64 else
[551]65 pch++;
[2]66 }
[551]67 strcpy(string, swctl.szSwtitle);
[2]68 }
[551]69 if (!*string)
70 strcpy(string, GetPString(IDS_UNKNOWNDOSPROCTEXT));
[2]71 return string;
72}
73
[551]74static VOID FillKillListThread2(VOID * arg)
[350]75{
[551]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;
[2]84
85 thab = WinInitialize(0);
[551]86 thmq = WinCreateMsgQueue(thab, 0);
87 WinCancelShutdown(thmq, TRUE);
[533]88 IncrThreadUsage();
[2]89
[551]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);
[350]93 if (rc)
[551]94 Dos_Error(MB_CANCEL, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
95 GetPString(IDS_OUTOFMEMORY));
[350]96 else {
[551]97 rc = DosQProcStatus(pbh, USHRT_MAX);
[350]98 if (!rc) {
[2]99 ppi = pbh->ppi;
[551]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));
[350]114 }
[551]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
[2]126 }
127 DosFreeMem(pbh);
128 }
[350]129
[551]130 if (WinIsWindow(thab, hwnd))
131 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
[2]132 WinDestroyMsgQueue(thmq);
[533]133 DecrThreadUsage();
[2]134 WinTerminate(thab);
135}
136
[551]137static VOID FillKillListThread(VOID * arg)
[350]138{
[551]139 HWND hwnd = *(HWND *) arg;
140 CHAR s[1036], progname[1027], *p;
141 HAB thab;
142 HMQ thmq;
[2]143 FILE *fp;
[551]144 BOOL foundstart = FALSE;
145 INT rc;
[2]146 CHAR *startstring = "Process and Thread Information";
147 CHAR *endstring = "System Semaphore Information";
[551]148 PID pid;
149 HFILE oldstdout, newstdout;
[2]150
151 DosError(FERR_DISABLEHARDERR);
152
153 thab = WinInitialize(0);
[551]154 thmq = WinCreateMsgQueue(thab, 0);
155 WinCancelShutdown(thmq, TRUE);
[533]156 IncrThreadUsage();
[2]157
[551]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));
[350]165 goto Abort;
166 }
167 else {
[2]168 newstdout = -1;
[551]169 rc = DosDupHandle(fileno(stdout), &newstdout);
[350]170 if (rc)
[551]171 Dos_Error(MB_CANCEL, rc, hwnd, __FILE__, __LINE__, "DosDupHandle");
[2]172 oldstdout = fileno(stdout);
[551]173 DosDupHandle(fileno(fp), &oldstdout);
[2]174 rc = runemf2(SEPARATE | INVISIBLE | FULLSCREEN | BACKGROUND | WAIT,
[551]175 hwnd, NULL, NULL, "%s", "PSTAT.EXE /C");
[2]176 oldstdout = fileno(stdout);
[551]177 DosDupHandle(newstdout, &oldstdout);
[2]178 DosClose(newstdout);
179 fclose(fp);
[451]180 // fixme to be gone?
[551]181 if (rc == -1) {
[2]182 saymsg(MB_CANCEL,
[551]183 hwnd,
184 GetPString(IDS_ARGHTEXT), GetPString(IDS_CANTRUNPSTATTEXT));
[2]185 goto Abort;
186 }
187 }
[551]188 fp = fopen(s, "r");
[404]189 if (fp) {
190 while (!feof(fp)) {
[551]191 strset(s, 0);
192 if (!xfgets(s, 1025, fp, pszSrcFile, __LINE__))
193 break;
[404]194 if (!foundstart) {
[551]195 if (*s == ' ' && strstr(s, startstring))
196 foundstart = TRUE;
[2]197 }
198 else {
[551]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 }
[2]220 }
221 }
222 fclose(fp);
223 }
224Abort:
225 DosForceDelete("$PSTAT#$.#$#");
[551]226 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
[2]227 WinDestroyMsgQueue(thmq);
[533]228 DecrThreadUsage();
[2]229 WinTerminate(thab);
230}
231
[551]232MRESULT EXPENTRY KillDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
[350]233{
[551]234 SHORT sSelect;
235 PID pid;
236 static BOOL listdone;
237 static HPOINTER hptrIcon = (HPOINTER) 0;
[2]238
[551]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;
[2]246
[551]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;
[2]263
[551]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;
[2]273
[551]274 case KILL_LISTBOX:
275 switch (SHORT2FROMMP(mp2)) {
276 case LN_ENTER:
277 WinSendDlgItemMsg(hwnd, DID_OK, BM_CLICK, MPFROMSHORT(TRUE), MPVOID);
278 break;
[2]279 }
[551]280 break;
[2]281
[551]282 default:
[2]283 break;
[551]284 }
285 return 0;
[2]286
[551]287 case WM_ADJUSTWINDOWPOS:
288 PostMsg(hwnd, UM_STRETCH, MPVOID, MPVOID);
289 break;
[2]290
[551]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);
[2]312 }
[551]313 }
314 return 0;
[2]315
[551]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)
[771]323 DosSleep(100);//05 Aug 07 GKY 250
[551]324 else
325 WinDismissDlg(hwnd, 0);
326 }
327 else {
328 if (_beginthread(FillKillListThread,
329 NULL, 65536, (PVOID) & hwnd) != -1)
[771]330 DosSleep(100); //05 Aug 07 GKY 250
[551]331 else
332 WinDismissDlg(hwnd, 0);
333 }
334 break;
[2]335
[551]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) {
[2]343
[551]344 CHAR s[31], *p;
345 APIRET error;
[2]346
[551]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 }
[2]375 }
[551]376 break;
[2]377
[551]378 case DID_CANCEL:
379 if (!listdone)
380 Runtime_Error(pszSrcFile, __LINE__, "busy");
381 else
382 WinDismissDlg(hwnd, 0);
[2]383 break;
384
[551]385 case IDM_HELP:
386 saymsg(MB_ENTER | MB_ICONASTERISK,
387 hwnd,
388 GetPString(IDS_KILLPROCHELPTITLETEXT),
389 GetPString(IDS_KILLPROCHELPTEXT));
[2]390 break;
[551]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;
[2]406 }
[551]407 return WinDefDlgProc(hwnd, msg, mp1, mp2);
[2]408}
Note: See TracBrowser for help on using the repository browser.