This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
/
StartProcessThread.cpp
executable file
·364 lines (323 loc) · 12.8 KB
/
StartProcessThread.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
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
//+=============================================================================
//
// file : StartProcessThread.cpp
//
// description : C++ source for the Starter start process thread.
//
// project : TANGO Device Server
//
// $Author$
//
// Copyright (C) : 2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This file is part of Tango.
//
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tango is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Tango. If not, see <http://www.gnu.org/licenses/>.
//
// $Revision$
// $Date$
//
//+=============================================================================
#include <tango.h>
#ifdef _TG_WINDOWS_
# include <process.h>
# include <direct.h>
# include <io.h>
#else
# include <sys/wait.h>
#endif
#include <fcntl.h>
#include <Starter.h>
namespace Starter_ns {
//+------------------------------------------------------------------
/**
* Thread constructor
*/
//+------------------------------------------------------------------
StartProcessThread::StartProcessThread(vector<NewProcess *> v_np, int level, Starter *st) {
for (unsigned long i = 0; i < v_np.size(); i++)
processes.push_back(v_np[i]);
thread_level = level;
starter = st;
}
//+------------------------------------------------------------------
/**
* Execute the fork of the sub process in a thread.
*/
//+------------------------------------------------------------------
void StartProcessThread::run(TANGO_UNUSED(void *ptr)) {
CheckProcessUtil *process_util = starter->util->proc_util;
// Check if a previous level thread is terminated
if (thread_level > 1) {
while (starter->start_proc_data->get_current_level() != thread_level &&
starter->start_proc_data->level_is_still_active(thread_level)) {
ms_sleep(500)
}
}
// Start the level process start loop
for (unsigned long i = 0; i < processes.size() &&
starter->start_proc_data->level_is_still_active(thread_level); i++) {
// Build server name from admin name and check if stopped before start it
char *p = processes[i]->adminName + strlen("dserver/");
string servname(p);
if (!process_util->is_server_running(servname)) {
start_process(processes[i]);
// Sleep a bit between start ups
// to do not overload database
bool started = false;
time_t t0 = time(nullptr);
time_t t1 = t0;
bool failed = false;
while (!failed && !started &&
(t1 - t0) < starter->serverStartupTimeout) {
# ifdef _TG_WINDOWS_
_sleep(1000);
# else
sleep(1);
# endif
// do not test before 4 seconds to be sure
// process list has been updated.
t1 = time(nullptr);
if ((t1 - t0) >= 4) {
// Check if server running or failed
if (!process_util->is_server_running(servname))
failed = true;
else {
// if not failed check if start up is terminated
Tango::DeviceProxy *proxy = nullptr;
try {
proxy = new Tango::DeviceProxy(processes[i]->adminName);
proxy->ping();
started = true;
}
catch (Tango::DevFailed &) {
//cout << e.errors[0].desc << endl;
}
delete proxy;
t1 = time(nullptr);
}
}
}
if (started) {
TangoSys_OMemStream out_stream;
out_stream << servname << " started";
starter->util->log_starter_info(out_stream.str());
//cout << "----- " << serverName << " started in " << (t1-t0) << " sec." << endl;
} else if (failed)
cout << "----- " << servname << " failed in " << (t1 - t0) << " sec." << endl;
else
cout << "----- " << servname << " Timeout = " << (t1 - t0) << endl;
// Wait a bit between 2 startup (not for last one)
if (thread_level > 0 && i < processes.size() - 1) {
ms_sleep(TIME_BETWEEN_STARTUPS)
}
}
}
// Check if a time to wait between two start up levels in seconds as been set
// Except for level 0 which is a single start up
if (thread_level > 0 && starter->interStartupLevelWait > 0 &&
starter->start_proc_data->level_is_still_active(thread_level)) {
//cout << "Thread level " << thread_level <<
// " wait for " << starter->interStartupLevelWait << endl;
ms_sleep(starter->interStartupLevelWait * 1000)
}
// Free the process structure field
for (unsigned long i = 0; i < processes.size(); i++) {
delete[] processes[i]->serverName;
delete[] processes[i]->instanceName;
delete[] processes[i]->adminName;
delete[] processes[i]->logFileName;
delete processes[i];
}
// remove in level vector to start another level
starter->start_proc_data->remove_level(thread_level);
}
#ifndef _TG_WINDOWS_
//+------------------------------------------------------------------
/**
* Start one process
*/
//+------------------------------------------------------------------
void StartProcessThread::start_process(NewProcess *process) {
char *argv[4];
int i = 0;
argv[i++] = process->serverName;
argv[i++] = process->instanceName;
//argv[i++] = "-v5";
argv[i] = nullptr;
// Fork a child process to start the device server
int fork_id = fork();
switch (fork_id) {
case -1:
cerr << "Fork Failed !" << endl;
break;
case 0:
// Double fork to prevent defunct child processes
switch (fork()) {
case -1:
cerr << "Fork Failed !" << endl;
break;
case 0:
// Change process group and close control tty
#if defined(__darwin__) || defined( __MACOS__)
setpgrp();
#elif defined (__freebsd__)
setpgrp(0,setsid());
#else
setpgrp();
// Call setsid() to do NOT stop children if Starter is killed.
setsid();
#endif
// Close standard out
close(1);
open("/dev/null", O_RDWR | O_CREAT, 0664);
// Close the stderr and re-open it on a log file.
close(2);
starter->util->manage_log_file_history(
process->logFileName, starter->keepLogFiles);
open(process->logFileName, O_RDWR | O_CREAT, 0664);
// Start the execution of the device server
if (execvp(argv[0], argv) < 0) {
ofstream of(process->logFileName);
of << "Exec(" << argv[0] << ") failed " << endl;
of << strerror(errno) << endl;
of.close();
}
_exit(0);
default:
//cout << fork_id << " exit()" << endl;
_exit(0);
}
break;
default:
int res_wait;
wait(&res_wait);
break;
}
}
#else // _TG_WINDOWS_
//+----------------------------------------------------------
// WIN 32 methods to fork a sub process
//+----------------------------------------------------------
void StartProcessThread::start_process(NewProcess *process)
{
/*****
On Win32 problem with permission denied !
It seems that this not necessary because there is nothing in file.
starter->util->manage_log_file_history(
process->logfile, starter->keepLogFiles);
*/
StartWinThread *win_thread = new StartWinThread(process, starter);
win_thread->start();
}
//+------------------------------------------------------------------
/**
* Set the path between cotes for windows.
*
* @param serverName server name
*/
//+------------------------------------------------------------------
string StartWinThread::get_server_name_with_cotes(string serverName)
{
string::size_type pos = serverName.find_last_of("/\\");
if (pos!=string::npos)
{
string str("\"");
str += serverName.substr(0, pos);
str += "\"";
str += serverName.substr(pos);
return str;
}
else
return serverName;
}
//+----------------------------------------------------------
// WIN 32 Thread to fork a sub process
// If batch file, the spawnv is blocking !!!
//+----------------------------------------------------------
void StartWinThread::run(void *ptr)
{
// Check if batch file
string str_server(process->serverName);
string str_with_cotes = get_server_name_with_cotes(process->serverName);
string cmd(str_with_cotes);
cmd += " ";
cmd += process->instanceName;
cout << "system(" << cmd << ");" << endl;
system(cmd.c_str());
return;
}
#endif // _TG_WINDOWS_
//+------------------------------------------------------------------
//+------------------------------------------------------------------
int StartProcessShared::get_starting_processes() {
omni_mutex_lock sync(*this);
return starting_processes;
}
//+------------------------------------------------------------------
/**
* StartProcessShared::push_back_level(int level)
*/
//+------------------------------------------------------------------
void StartProcessShared::push_back_level(int level) {
omni_mutex_lock sync(*this);
start_process_thread_levels.push_back(level);
starting_processes++;
}
//+------------------------------------------------------------------
/**
* StartProcessShared::get_current_level()
*/
//+------------------------------------------------------------------
int StartProcessShared::get_current_level() {
omni_mutex_lock sync(*this);
return start_process_thread_levels[0];
}
//+------------------------------------------------------------------
/**
* StartProcessShared::level_is_still_active(int level)
*/
//+------------------------------------------------------------------
bool StartProcessShared::level_is_still_active(int level) {
omni_mutex_lock sync(*this);
auto it = start_process_thread_levels.begin();
for (; it < start_process_thread_levels.end(); it++)
if (*it == level) {
//cout << "Level " << level << " is still active" << endl;
return true;
}
//cout << "Level " << level << " is NOT still active" << endl;
return false;
}
//+------------------------------------------------------------------
/**
* StartProcessShared::remove_level(int level)
*/
//+------------------------------------------------------------------
void StartProcessShared::remove_level(int level) {
omni_mutex_lock sync(*this);
auto it = start_process_thread_levels.begin();
for (; it < start_process_thread_levels.end(); it++) {
if (*it == level) {
//cout << "StartProcessShared::remove_level " << level << endl;
start_process_thread_levels.erase(it);
starting_processes--;
return;
}
}
}
} // namespace