-
Notifications
You must be signed in to change notification settings - Fork 0
/
DaemonCtl.cpp
291 lines (242 loc) · 6.5 KB
/
DaemonCtl.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "DaemonCtl.h"
#include "Utilities.h"
#include "Hypervisor.h"
#include <sstream>
#include <cstdlib>
#if defined(__APPLE__) && defined(__MACH__)
#include <libproc.h>
#endif
#ifdef __linux__
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#endif
using namespace std;
/**
* Current daemon pid
*/
int currDaemonPid = 0;
/**
* Return the location of the daemon lockfile
*/
std::string getDaemonLockfile() {
CRASH_REPORT_BEGIN;
/* Return the system-wide daemon pidfile location */
std::string appDir = getAppDataPath();
return appDir + "/run/daemon.pid";
CRASH_REPORT_END;
}
/**
* Check if the given process ID is still running
*/
bool isAlive( int pid ) {
CRASH_REPORT_BEGIN;
#if defined(__APPLE__) && defined(__MACH__)
struct proc_bsdinfo bsdInfo;
int ret = proc_pidinfo( pid, PROC_PIDTBSDINFO, 0, &bsdInfo, sizeof(bsdInfo));
return (ret > 0);
#endif
#ifdef __linux__
int ret = kill( pid, 0 );
return (ret == 0);
#endif
#ifdef _WIN32
DWORD lpExitCode;
// Open handle
HANDLE hProc = OpenProcess( PROCESS_QUERY_INFORMATION, false, pid);
if (hProc == NULL) return false;
// Query exit code (returns STILL_ALIVE if it's still alive)
GetExitCodeProcess( hProc, &lpExitCode );
CloseHandle( hProc );
// Check status
return (lpExitCode == STILL_ACTIVE);
#endif
return false;
CRASH_REPORT_END;
}
/**
* Return the daemon pid from the lockfile
*/
int daemonLockFilePID () {
CRASH_REPORT_BEGIN;
std::string lockfile = getDaemonLockfile();
/* If thre is no lockfile return 0 */
if (!file_exists(lockfile))
return 0;
/* Read file */
ifstream lfStream;
lfStream.open( lockfile.c_str(), std::ios_base::in );
if (!lfStream.fail()) {
/* Get PID */
int pid; lfStream >> pid;
lfStream.close();
return pid;
}
/* Unable to read file */
return 0;
CRASH_REPORT_END;
}
/**
* Check if the file exists and is locked
*/
bool isDaemonRunning () {
CRASH_REPORT_BEGIN;
/* Fetch daemon PID */
int pid = daemonLockFilePID();
if (pid == 0)
return false;
/* Check if it's running */
return isAlive( pid );
CRASH_REPORT_END;
}
/**
* Try to lock or unlock the file
*/
DLOCKINFO * daemonLock( std::string lockfile ) {
CRASH_REPORT_BEGIN;
static DLOCKINFO dlInfo;
/* Open file for output */
dlInfo.fname = lockfile;
ofstream lfStream;
lfStream.open( lockfile.c_str(), std::ios_base::out );
if (!lfStream.fail()) {
/* Write pid */
int pid;
#ifndef _WIN32
pid = getpid();
#else
pid = GetCurrentProcessId();
#endif
lfStream << pid << endl;
lfStream.close();
return &dlInfo;
} else {
return NULL;
}
CRASH_REPORT_END;
}
/**
* Unlock the daemon lockfile
*/
void daemonUnlock( DLOCKINFO * dlInfo ) {
CRASH_REPORT_BEGIN;
/* Remove lockfile */
remove( dlInfo->fname.c_str() );
CRASH_REPORT_END;
}
/**
* Contact daemon
* WARNING! ThinIPCInitialize() *MUST* be called in advance!
*/
short int daemonIPC( ThinIPCMessage * send, ThinIPCMessage * recv ) {
CRASH_REPORT_BEGIN;
/* Pick a random port */
static int rPort = (rand() % ( 0xFFFE - DAEMON_PORT )) + DAEMON_PORT + 1;
static ThinIPCEndpoint ipc( rPort );
/* Send message */
if (ipc.send( DAEMON_PORT, send ) < 0)
return -1;
/* Wait 250 ms for answer */
if (!ipc.isPending( 250000 ))
return -2;
/* Read response */
int port;
ipc.recv( &port, recv );
if (port != DAEMON_PORT)
return -3;
/* Check if we had error */
int errNo = recv->readShort();
if (errNo != DIPC_ANS_OK)
return -abs(errNo);
/* Success */
return 0;
CRASH_REPORT_END;
}
/**
* Contact daemon requesting integer response
* (Shorthand for calling daemonIPC with ThinIPCMessage structures)
*/
short int daemonGet( short int action ) {
CRASH_REPORT_BEGIN;
static ThinIPCMessage send, recv;
send.reset(); recv.reset();
send.writeShort( action );
short int res = daemonIPC( &send, &recv );
if (res != 0) return res;
return recv.readShort();
CRASH_REPORT_END;
}
/**
* Contact daemon setting integer data
* (Shorthand for calling daemonIPC with ThinIPCMessage structures)
*/
short int daemonSet( short int action, short int value ) {
CRASH_REPORT_BEGIN;
static ThinIPCMessage send, recv;
send.reset();
recv.reset();
send.writeShort( action );
send.writeShort( value );
int res = daemonIPC( &send, &recv );
if (res != 0) return res;
return recv.readShort();
CRASH_REPORT_END;
}
/**
* Cross-platform way to start the daemon process in the background
*/
int daemonStart( std::string path_to_bin ) {
CRASH_REPORT_BEGIN;
#ifdef _WIN32
HINSTANCE hApp = ShellExecuteA(
NULL,
NULL,
path_to_bin.c_str(),
NULL,
NULL,
SW_HIDE);
if ( (int)hApp > 32 ) {
return HVE_SCHEDULED;
} else {
return HVE_IO_ERROR;
}
#else
// Use a local pointer for the fork
static char file[4096];
path_to_bin.copy( file, path_to_bin.length(), 0 );
file[path_to_bin.length()] = '\0';
// Fork and start
int pid = fork();
if (pid==0) {
setsid();
CVMWA_LOG("Daemon", "Running " << file );
execl( file, file, (char*) 0 );
return 0;
} else {
currDaemonPid = pid;
return HVE_SCHEDULED;
}
#endif
CRASH_REPORT_END;
};
/**
* Shut down and reap daemon
*/
int daemonStop( ) {
CRASH_REPORT_BEGIN;
/* (Assuming the user did his homework and checked if the daemon was running) */
int ipcRes = daemonGet( DIPC_SHUTDOWN );
if (ipcRes < 0) return ipcRes;
#ifndef _WIN32
/* (*NIX platforms require child reap in order to avoid leaving zombies) */
/* Try to find the daemon PID to reap */
int reapPid = currDaemonPid;
if (reapPid == 0) reapPid = daemonLockFilePID();
/* Wait PID */
pid_t pid; int status;
if (reapPid != 0) while((pid = waitpid(reapPid, &status, 0)) > 0) ;
#endif
/* Everything is OK */
return HVE_OK;
CRASH_REPORT_END;
}