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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
// **********************************************************************
//
// Copyright (c) 2001
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#ifdef _WIN32
# error Sorry, the IcePack Activator is not yet supported on WIN32.
#endif
#include <Ice/Ice.h>
#include <IcePack/ActivatorI.h>
#include <IcePack/Admin.h>
#include <IcePack/ServerManager.h>
#include <fcntl.h>
using namespace std;
using namespace Ice;
using namespace IcePack;
IcePack::ActivatorI::ActivatorI(const CommunicatorPtr& communicator, const vector<string>& defaultArgs) :
_communicator(communicator),
_destroy(false),
_defaultArgs(defaultArgs)
{
int fds[2];
if(pipe(fds) != 0)
{
SystemException ex(__FILE__, __LINE__);
ex.error = getSystemErrno();
throw ex;
}
_fdIntrRead = fds[0];
_fdIntrWrite = fds[1];
int flags = fcntl(_fdIntrRead, F_GETFL);
flags |= O_NONBLOCK;
fcntl(_fdIntrRead, F_SETFL, flags);
}
IcePack::ActivatorI::~ActivatorI()
{
assert(_destroy);
close(_fdIntrRead);
close(_fdIntrWrite);
for(vector<Process>::iterator p = _processes.begin(); p != _processes.end(); ++p)
{
close(p->fd);
}
}
void
IcePack::ActivatorI::run()
{
try
{
terminationListener();
}
catch(const Exception& ex)
{
Error out(_communicator->getLogger());
out << "exception in process termination listener:\n" << ex;
}
catch(...)
{
Error out(_communicator->getLogger());
out << "unknown exception in process termination listener";
}
}
void
IcePack::ActivatorI::destroy()
{
IceUtil::Mutex::Lock sync(*this);
if(_destroy) // Don't destroy twice.
{
return;
}
_destroy = true;
setInterrupt();
}
bool
IcePack::ActivatorI::activate(const ServerPrx& server, const ::Ice::Current&)
{
IceUtil::Mutex::Lock sync(*this);
if(_destroy)
{
return false;
}
ServerDescription desc = server->getServerDescription();
string path = desc.path;
if(path.empty())
{
return false;
}
//
// Normalize the pathname a bit.
//
string::size_type pos;
while((pos = path.find("//")) != string::npos)
{
path.erase(pos, 1);
}
while((pos = path.find("/./")) != string::npos)
{
path.erase(pos, 2);
}
//
// Process does not exist, activate and create.
//
int fds[2];
if(pipe(fds) != 0)
{
SystemException ex(__FILE__, __LINE__);
ex.error = getSystemErrno();
throw ex;
}
pid_t pid = fork();
if(pid == -1)
{
SystemException ex(__FILE__, __LINE__);
ex.error = getSystemErrno();
throw ex;
}
if(pid == 0) // Child process.
{
//
// Close all filedescriptors, except for standard input,
// standard output, standard error output, and the write side
// of the newly created pipe.
//
int maxFd = static_cast<int>(sysconf(_SC_OPEN_MAX));
for(int fd = 3; fd < maxFd; ++fd)
{
if(fd != fds[1])
{
close(fd);
}
}
//
// Change working directory.
//
string pwd = desc.pwd;
if(!pwd.empty())
{
string::size_type pos;
while((pos = pwd.find("//")) != string::npos)
{
pwd.erase(pos, 1);
}
while((pos = pwd.find("/./")) != string::npos)
{
pwd.erase(pos, 2);
}
if(chdir(pwd.c_str()) == -1)
{
//
// Send any errors to the parent process, using the write
// end of the pipe.
//
SystemException ex(__FILE__, __LINE__);
ex.error = getSystemErrno();
ostringstream s;
s << "can't change working directory to `" << pwd << "':\n" << ex;
write(fds[1], s.str().c_str(), s.str().length());
close(fds[1]);
exit(EXIT_FAILURE);
}
}
//
// Compute arguments.
//
int argc = desc.args.size() + _defaultArgs.size() + 2;
char** argv = static_cast<char**>(malloc(argc * sizeof(char*)));
argv[0] = strdup(path.c_str());
unsigned int i = 0;
vector<string>::const_iterator q;
for(q = desc.args.begin(); q != desc.args.end(); ++q, ++i)
{
argv[i + 1] = strdup(q->c_str());
}
for(q = _defaultArgs.begin(); q != _defaultArgs.end(); ++q, ++i)
{
argv[i + 1] = strdup(q->c_str());
}
argv[argc - 1] = 0;
if(execvp(argv[0], argv) == -1)
{
//
// Send any errors to the parent process, using the write
// end of the pipe.
//
SystemException ex(__FILE__, __LINE__);
ex.error = getSystemErrno();
ostringstream s;
s << "can't execute `" << path << "':\n" << ex;
write(fds[1], s.str().c_str(), s.str().length());
close(fds[1]);
exit(EXIT_FAILURE);
}
}
else // Parent process.
{
close(fds[1]);
Process process;
process.pid = pid;
process.fd = fds[0];
process.server = server;
_processes.push_back(process);
int flags = fcntl(process.fd, F_GETFL);
flags |= O_NONBLOCK;
fcntl(process.fd, F_SETFL, flags);
setInterrupt();
}
return true;
}
void
IcePack::ActivatorI::terminationListener()
{
while(true)
{
fd_set fdSet;
int maxFd = _fdIntrRead;
FD_ZERO(&fdSet);
FD_SET(_fdIntrRead, &fdSet);
{
IceUtil::Mutex::Lock sync(*this);
if(_destroy)
{
return;
}
for(vector<Process>::iterator p = _processes.begin(); p != _processes.end(); ++p)
{
int fd = p->fd;
FD_SET(fd, &fdSet);
if(maxFd < fd)
{
maxFd = fd;
}
}
}
repeatSelect:
int ret = ::select(maxFd + 1, &fdSet, 0, 0, 0);
assert(ret != 0);
if(ret == -1)
{
if(errno == EINTR || errno == EPROTO)
{
goto repeatSelect;
}
SystemException ex(__FILE__, __LINE__);
ex.error = getSystemErrno();
throw ex;
}
{
IceUtil::Mutex::Lock sync(*this);
if(FD_ISSET(_fdIntrRead, &fdSet))
{
clearInterrupt();
}
if(_destroy)
{
return;
}
vector<Process>::iterator p = _processes.begin();
while(p != _processes.end())
{
int fd = p->fd;
if(FD_ISSET(fd, &fdSet))
{
char s[16];
int ret;
string message;
//
// Read the message over the pipe.
//
while((ret = read(fd, &s, 16)) > 0)
{
message.append(s, ret);
}
if(ret == -1)
{
if(errno != EAGAIN || message.empty())
{
SystemException ex(__FILE__, __LINE__);
ex.error = getSystemErrno();
throw ex;
}
++p;
}
else if(ret == 0)
{
ServerPrx server = p->server;
//
// If the pipe was closed, the process has
// terminated.
//
p = _processes.erase(p);
close(fd);
//
// Notify the server it has terminated.
//
try
{
server->terminationCallback();
}
catch(const Ice::ObjectAdapterDeactivatedException&)
{
//
// Expected when IcePack is shutdown.
//
}
}
//
// Log the received message.
//
if(!message.empty())
{
Error out(_communicator->getLogger());
out << message;
}
}
else
{
++p;
}
}
}
}
}
void
IcePack::ActivatorI::clearInterrupt()
{
char s[32]; // Clear up to 32 interrupts at once.
while(read(_fdIntrRead, s, 32) == 32)
{
}
}
void
IcePack::ActivatorI::setInterrupt()
{
char c = 0;
write(_fdIntrWrite, &c, 1);
}
|