source: trunk/dll/killproc.c@ 793

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

Move #pragma alloc_text to end for OpenWatcom compat

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