-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactoryLibrary.cpp
236 lines (207 loc) · 6.29 KB
/
FactoryLibrary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "ProcessFactory/FactoryLibrary.h"
#include "ProcessFactory/ProcessFactory.h"
#ifdef USE_GCC
#include<errno.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<time.h>
#else
#include<process.h>
#include<fcntl.h>
#include<time.h>
#include<windows.h>
#include<tlhelp32.h>
#include<vdmdbg.h>
#include<wchar.h>
#endif
using namespace std;
namespace ProcessFactoryLibrary
{
// -------------------------------------------------------
// check if process with pid is running
// -------------------------------------------------------
int isProcessRunning(int64 pid, string executableName)
{
//if(pid < 0) return 0;
#ifdef USE_GCC
stringstream directory;
directory << "/proc/" << pid ;
ifstream procFile(((directory).str()).c_str());
if(!procFile.is_open())
{
//cerr << "isProcessRunning(pid, name): Couldn't find process id : " << pid << endl;
return 0;
}
// -- further check if executableName matches /proc/<pid>/cmdline
if(!executableName.empty())
{
directory << "/cmdline";
ifstream cmdFile(directory.str().c_str());
if(cmdFile.is_open())
{
stringbuf cmdLine;
while(cmdFile.get(cmdLine))
{
string foundExecutable = HostInformationSpace::GetExecutableFileName(cmdLine.str(), executableName.size());
if(strncmp(executableName.c_str(), foundExecutable.c_str(), executableName.size()) == 0)
{
//cerr << "isProcessRunning(): Found " << executableName << " == " << foundExecutable << endl;
return 1;
}
}
//cerr << "isProcessRunning(): Could not find running instance of " << executableName << " with pid = " << pid << endl;
return 0;
}
else
{
cerr << "isProcessRunning(): ERROR! Couldn't open /proc/" << pid << "/cmdline " << endl;
//return 0;
}
}
return 1;
#else
int count = 0;
HANDLE hSnap = NULL;
PROCESSENTRY32 proc32;
if((hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE)
return -1;
proc32.dwSize=sizeof(PROCESSENTRY32);
while((Process32Next(hSnap, &proc32)) == TRUE)
{
if(proc32.th32ProcessID == pid)
{
if(!executableName.empty())
{
string cmd(proc32.szExeFile);
size_t found = cmd.find_last_of("\\");
cmd.replace(0, found + 1, "");
if(strcmp(executableName.c_str(), cmd.c_str()) != 0)
{
cerr << "No Match " << executableName << " != " << cmd << endl;
return 0;
}
else count++;
}
else count++;
break;
}
}
CloseHandle(hSnap);
return count;
#endif
}
// -------------------------------------------------------
// kill process
// -------------------------------------------------------
#if 0 //#ifndef USE_GCC
BOOL CALLBACK TerminateAppEnum(HWND hwnd, LPARAM lParam)
{
DWORD dwID;
GetWindowThreadProcessId(hwnd, &dwID);
if(dwID == (DWORD)lParam)
{
cout << "Posting message WM_CLOSE!" << endl;
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
return TRUE;
}
#endif
void killProcess(const HostInformation& info)
{
if(info.GetProcessId() == -1)
{
cerr << "WARNING! killProcess(processID == -1). Not Allowed!" << endl;
return;
}
#ifdef USE_GCC
int sig = 15;
int ret = kill(info.GetProcessId(), sig);
if(ret == -1)
{
cerr << "killProcess(): WARNING! kill(" << info.GetProcessId() << "," << sig << ") returned " << strerror(errno) << endl;
}
// Remove file containing process id.
stringstream stream;
stream << "/var/run/sonarproc/" << info.GetProcessId() << "-" << info.GetExecutableName() << "-" << info.GetComponentName();
remove(stream.str().c_str());
#else
#if 0
// This is a better cleaner way to terminate a process
HANDLE processHandle = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, true, info.GetProcessId());
if(processHandle == NULL)
cerr << "WARNING! Could not find processhandle for process: " << info << endl;
EnumWindows((WNDENUMPROC)TerminateAppEnum, info.GetProcessId());
DWORD dwRet;
if(WaitForSingleObject(processHandle, 10000) != WAIT_OBJECT_0)
dwRet = (TerminateProcess(processHandle, 0) ? 2:0);
else
dwRet = 1;
#else
HANDLE processHandle = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, true, info.GetProcessId());
if(processHandle == NULL)
cerr << "WARNING! Could not find processhandle for process: " << info << endl;
TerminateProcess(processHandle, 0); // TODO: Is there a better and cleaner way to terminate a process?
#endif
CloseHandle(processHandle);
#endif
}
// -------------------------------------------------------
// create process
// -------------------------------------------------------
void createProcess(const string &commandLine, const string& title)
{
#ifdef USE_GCC
stringstream command;
if(strcmp(ProcessFactoryConfig::display.c_str(), "noX") != 0)
{
command << "xterm -geometry 100x35 -title " << title;
if(ProcessFactoryConfig::display != "")
command << " -display " << ProcessFactoryConfig::display;
if (ProcessFactoryConfig::debugLevel > 0)
command << " -hold";
command << " -e ./" << commandLine << " ";
}
command << "./" << commandLine << " &";
int ret = system(command.str().c_str());
if(ret == -1)
cout << "FactoryLibrary::createProcess(): ERROR! system call failed for : " << command.str() << endl;
#else
STARTUPINFO startupInfo;
startupInfo.cb = sizeof(STARTUPINFO);
startupInfo.lpReserved = 0;
startupInfo.lpDesktop = 0;
startupInfo.lpTitle = (LPSTR)title.c_str();
startupInfo.dwX = 0;
startupInfo.dwY = 0;
startupInfo.dwXSize = 0;
startupInfo.dwYSize = 0;
startupInfo.dwXCountChars = 0;
startupInfo.dwYCountChars = 0;
startupInfo.dwFillAttribute = 0;
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_SHOWMINIMIZED;
startupInfo.cbReserved2 = 0;
startupInfo.lpReserved2 = 0;
startupInfo.hStdInput = 0;
startupInfo.hStdOutput = 0;
startupInfo.hStdError = 0;
PROCESS_INFORMATION processInfo;
BOOL success = CreateProcess(
0, // lpApplicationName
(LPSTR)commandLine.c_str(), // lpCommandLine
0, // lpProcessAttributes
0, // lpThreadAttributes
false, // bInheritHandles
CREATE_NEW_CONSOLE, // dwCreationFlags
0, // lpEnvironment
0, // lpCurrentDirectory
&startupInfo, // lpStartupInfo
&processInfo // lpProcessInformation
);
if(!success)
throw MiddlewareException(
"Could not create process. Be sure that the following executable "
"is located in the path: " + commandLine);
#endif
}
}; // ProcessFactoryLibrary