-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.cc
838 lines (729 loc) · 24.9 KB
/
service.cc
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
#include "TRandom3.h"
#include "TSystem.h"
#include "TServerSocket.h"
#include "TEnv.h"
#include "TROOT.h"
#include "TFile.h"
#include "TApplication.h"
#include "TTree.h"
#include "ROOT/REveManager.hxx"
#include "ROOT/REveScene.hxx"
#include "ROOT/RWebWindow.hxx"
#include <ROOT/RFileDialog.hxx>
#include "nlohmann/json.hpp"
#include "evd.h"
#include <cstdio>
#include <ctime>
#include <string>
#include <regex>
#include <mutex>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <iostream>
#include <fstream>
static int SERVICE_PORT = 5555;
static int MAX_SERVERS = 100;
static int USER_TIMEOUT = 144000;
static int DISCONNECT_TIMEOUT = 600;
namespace REX = ROOT::Experimental;
//=============================================================================
// Message Queue stuff
//=============================================================================
int N_tot_children = 0;
int global_msgq_id;
pid_t global_child_pid = -1; // set for children after fork
struct fw_msgbuf
{
long mtype; // message type, must be > 0 -- pid of receiving process, 1 for master
REX::REveServerStatus mbody;
};
void msgq_test_send(long id, REX::REveServerStatus &rss)
{
struct fw_msgbuf msg;
msg.mtype = id;
msg.mbody = rss;
if (msgsnd(global_msgq_id, (void *)&msg, sizeof(msg.mbody), IPC_NOWAIT) == -1)
{
perror("msgsnd error");
return;
}
// printf("sent status: N conn = %d\n", msg.mbody.f_n_connects);
}
struct StatReportTimer : public TTimer
{
bool Notify() override
{
REX::REveServerStatus ss;
REX::gEve->GetServerStatus(ss);
msgq_test_send(1, ss);
Reset();
return true;
}
};
//=============================================================================
// Signal and child process handling
//=============================================================================
struct ChildInfo
{
REX::REveServerStatus fLastStatus;
pid_t fPid;
int fSeqId;
std::time_t fStartTime;
std::time_t fEndTime; // do we need this? if we want to keep a list of recent sessions, we do
std::string fUser;
std::string fLogFile;
ChildInfo() = default;
ChildInfo(pid_t pid, int sid, const std::string &usr, const std::string &log) : fPid(pid), fSeqId(sid),
fStartTime(std::time(nullptr)), fEndTime(0),
fUser(usr), fLogFile(log)
{
}
};
std::mutex g_mutex;
std::map<pid_t, ChildInfo> g_children_map;
static void child_handler(int sig)
{
pid_t pid;
int status;
printf("Got SigCHLD ... entering waitpid loop.\n");
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
{
const std::lock_guard<std::mutex> lock(g_mutex);
auto i = g_children_map.find(pid);
if (i != g_children_map.end())
{
printf("Child pid=%d id=%d died ... cleaning up.\n", i->first, i->second.fSeqId);
g_children_map.erase(i);
}
else
{
printf("Got SigCHLD for pid=%d, not in map, hmmh.\n", pid);
}
}
}
void msgq_receiver_thread_foo()
{
while (true)
{
struct fw_msgbuf msg;
if (msgrcv(global_msgq_id, (void *)&msg, sizeof(msg.mbody), 1, 0) == -1)
{
if (errno == EIDRM)
{
printf("message queue listener thread terminating on queue removal\n");
break;
}
perror("msgrcv");
}
else
{
REX::REveServerStatus &ss = msg.mbody;
if (false) {
std::time_t now = std::time(nullptr);
auto ttt = [=](std::time_t t) -> double { if (t==0) return -999; return std::difftime(now, t)/60; };
printf("message from pid %d: dt_start=%.1f, N_conn=%d, N_disconn=%d,"
" dt_last_mir=%.1f, dt_last_conn=%.1f, dt_last_dissconn=%.1f minutes ago\n",
ss.fPid, ttt(ss.fTStart), ss.fNConnects, ss.fNDisconnects,
ttt(ss.fTLastMir), ttt(ss.fTLastConnect), ttt(ss.fTLastDisconnect));
}
const std::lock_guard<std::mutex> lock(g_mutex);
auto it = g_children_map.find(ss.fPid);
if (it != g_children_map.end()) {
g_children_map[ss.fPid].fLastStatus = ss;
} else {
printf("Error: child %d can't be located in map\n", ss.fPid);
}
}
}
}
//=============================================================================
// Fill out html table report
//=============================================================================
void html_report(std::ostringstream &oss)
{
std::vector<ChildInfo> v;
{
const std::lock_guard<std::mutex> lock(g_mutex);
v.reserve(g_children_map.size());
oss << "{ 'total_sessions'=>" << N_tot_children << ", 'current_sessions'=>" << g_children_map.size() << ",\n";
oss << " 'table'=>'<style> table, th, td { border: 1px solid black; padding: 5px; } </style> <table>\n";
oss << "<tr><th>pid</th><th>dt_start[min]</th><th>N_conn</th><th>N_disconn</th><th>dt_last_mir[min]</th>"
<< "<th>dt_last_conn[min]</th><th>dt_last_dissconn [min]</th>"
<< "<th>user</th><th>log file</th></tr>\n";
for (const auto &[pid, cinfo] : g_children_map)
{
if (cinfo.fLastStatus.fPid)
v.push_back(cinfo);
}
}
std::sort(v.begin(), v.end(), [](auto &a, auto &b){ return a.fStartTime < b.fStartTime; });
std::time_t now = std::time(nullptr);
auto ttt = [=](std::time_t t) -> double { if (t==0) return -999; return std::difftime(now, t)/60; };
char tbl_line[1024];
for (auto &ci : v)
{
auto &ss = ci.fLastStatus;
snprintf(tbl_line, 1024,
"<tr><td>%d</td><td>%.1f</td><td>%d</td><td>%d</td>"
"<td>%.1f</td><td>%.1f</td><td>%.1f</td>"
"<td>%s</td><td><a href=\"%s\">%s</a></td></tr>\n",
ss.fPid, ttt(ss.fTStart), ss.fNConnects, ss.fNDisconnects,
ttt(ss.fTLastMir), ttt(ss.fTLastConnect), ttt(ss.fTLastDisconnect),
ci.fUser.c_str(), ci.fLogFile.c_str(), ci.fLogFile.c_str());
oss << tbl_line;
}
oss << "</table>' }\n";
}
//=============================================================================
// Clear idle processes
//=============================================================================
// Returns number of supposedly active processes (assuming sig-kill will work).
int KillIdleProcesses()
{
// store results in buffer
std::vector<REX::REveServerStatus> v;
int num_active;
{
const std::lock_guard<std::mutex> lock(g_mutex);
num_active = (int) g_children_map.size();
for (const auto &[pid, cinfo] : g_children_map)
{
if (cinfo.fLastStatus.fPid)
v.push_back(cinfo.fLastStatus);
else
printf("Info: child %d has not initialized status yet.\n", pid);
}
}
std::sort(v.begin(), v.end(), [](auto &a, auto &b){ return a.fTStart < b.fTStart; });
// check hard limits without sort
printf("Num servers [%d]. Checking idle processes ...\n", num_active);
std::time_t now = std::time(nullptr);
for (auto &s : v)
{
// QQQQ - to be revisited now with additional data.
int t_fac = num_active > MAX_SERVERS * 9 / 10 ? 1 : 2;
bool doKill = false;
if (s.n_active_connections() == 0) {
float dt = std::difftime(now, s.fNConnects > 0 ? s.fTLastDisconnect : s.fTStart);
if ((doKill = dt > t_fac * DISCONNECT_TIMEOUT))
printf("Going to kill pid %d disconnected %f seconds ago\n", s.fPid, dt);
}
else {
double dt = std::difftime(now, s.fTLastMir);
if ((doKill = dt > t_fac * USER_TIMEOUT))
printf("Going to kill pid %d, N connections %d, last client active %.0f seconds ago\n", s.fPid, s.fNConnects, dt);
}
if (doKill) {
--num_active;
kill(s.fPid, SIGKILL);
}
}
return num_active;
}
//=============================================================================
// Assert directory exists, presumably logs/ or config/ for current user
//=============================================================================
int assertDir(const std::string &dir)
{
bool fail = false;
struct stat sb;
if (stat(dir.c_str(), &sb))
{
if (errno == ENOENT)
{
printf("dir %s does not exist, trying to create.\n", dir.c_str());
if (mkdir(dir.c_str(), 0777))
{
printf(" mkdir failed: %s\n", strerror(errno));
fail = true;
}
}
else
{
printf("dir stat failed: %s\n", strerror(errno));
fail = true;
}
}
else
{
if ((sb.st_mode & S_IFMT) != S_IFDIR)
{
printf("dir is not a directory\n");
fail = true;
}
if (access(dir.c_str(), W_OK))
{
printf("logdir can not write: %s\n", strerror(errno));
fail = true;
}
}
return fail;
}
//=============================================================================
// Generate random string to be used as a sesssion key
//=============================================================================
std::string RandomString(TRandom &rnd, int len = 16)
{
std::string s;
s.resize(len);
for (int i = 0; i < len; ++i)
{
int r = rnd.Integer(10 + 26 + 26);
if (r < 10)
{
s[i] = '0' + r;
}
else
{
r -= 10;
if (r < 26)
s[i] = 'A' + r;
else
s[i] = 'a' + r - 26;
}
}
return s;
}
//=============================================================================
//=============================================================================
void SendRawString(TSocket *s, const char *msg)
{
s->SendRaw(msg, strlen(msg));
}
//=============================================================================
//=============================================================================
bool ACCEPT_NEW = true;
int CHILDREN_MAX_WAIT = 0;
static void int_handler(int sig)
{
printf("Got SigINT/TERM, exiting main loop, will reap children there.\n");
ACCEPT_NEW = false;
}
void revetor()
{
namespace REX = ROOT::Experimental;
// Establish signal handlers.
struct sigaction sa, sa_chld, sa_int, sa_term;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = child_handler;
sigaction(SIGCHLD, &sa, &sa_chld);
sa.sa_handler = int_handler;
sigaction(SIGINT, &sa, &sa_int);
sigaction(SIGTERM, &sa, &sa_term);
// Common init. If REveManager supported delayed server startup we could
// also do REveManager::Create() here.
TApplication app("REveService", 0, 0);
gSystem->Load("libVsdDict.so");
gSystem->Load("libFWDict.so");
//auto *ff = TFile::Open("/home/viz/universal-format/samples//UserVsd-0.root");
//ff->Close();
//delete ff;
TServerSocket *ss = new TServerSocket(SERVICE_PORT, kTRUE);
if (!ss->IsValid())
{
fprintf(stderr, "Failed creating TServerSocket with code %d\n", ss->GetErrorCode());
exit(1);
}
printf("Server socket created on port %d, listening ...\n", SERVICE_PORT);
// Message queue for child status report
if ((global_msgq_id = msgget(IPC_PRIVATE, IPC_CREAT | 0660)) == -1)
{
perror("msgget for child message queue failed");
exit(1);
}
std::thread msgq_listener_thread(msgq_receiver_thread_foo);
// ---------------------------------------------------------
while (ACCEPT_NEW)
{
fd_set read, write, except;
FD_ZERO(&read);
FD_ZERO(&write);
FD_ZERO(&except);
FD_SET(ss->GetDescriptor(), &read);
int max_fd = ss->GetDescriptor();
struct timeval timeout;
timeout.tv_sec = 60;
timeout.tv_usec = 0;
int selret = select(max_fd + 1, &read, &write, &except, &timeout);
if (selret == -1)
{
const char *fErrorStr;
switch (errno)
{
case 0: // Cancelled ... or sth ...
fErrorStr = "Unknown error (errno=0).";
break;
case EBADF:
fErrorStr = "Bad file-descriptor.";
break;
case EINTR:
fErrorStr = "Interrupted select.";
break;
case EINVAL:
fErrorStr = "Bad parameters (num fds or timeout).";
break;
case ENOMEM:
fErrorStr = "No memory for select.";
break;
default:
fErrorStr = "Undocumented error in select.";
break;
} // end switch
printf("Select error %d, '%s'\n", errno, fErrorStr);
continue;
}
if (selret == 0) {
KillIdleProcesses();
continue;
}
if (FD_ISSET(ss->GetDescriptor(), &read))
{
TSocket *s = ss->Accept();
TInetAddress ia = s->GetInetAddress();
printf("Connection from %s\n", ia.GetHostName());
SendRawString(s, "Hello, this is Revetor! What do you want?\n");
char resp[4096];
int rl = s->RecvRaw(resp, 4096, kDontBlock);
if (rl > 0 && resp[rl - 1] == '\n')
{
resp[rl - 1] = 0;
printf("Got request: %s\n", resp);
--rl;
}
else
{
printf("Error, bad response or no \\n (resp_len=%d), terminating connection.\n", rl);
s->Close();
delete s;
continue;
}
nlohmann::json req;
try
{
req = nlohmann::json::parse(resp);
}
catch (std::exception &exc)
{
std::cout << "JSON parse caugth exception: " << exc.what() << "\n";
SendRawString(s, "{'error'=>'json parse'}");
}
if (req["action"] == "status")
{
char pmsg[1024];
const std::lock_guard<std::mutex> lock(g_mutex);
snprintf(pmsg, 1024, "{ 'total_sessions'=>%d, 'current_sessions'=>%d }\n",
N_tot_children, (int)g_children_map.size());
SendRawString(s, pmsg);
s->Close();
delete s;
}
else if (req["action"] == "report_usage")
{
std::ostringstream oss;
html_report(oss);
SendRawString(s, oss.str().data());
s->Close();
delete s;
}
else if(req["action"] == "stop_server")
{
char pmsg[1024];
const std::lock_guard<std::mutex> lock(g_mutex);
snprintf(pmsg, 1024, "{ 'total_sessions'=>%d, 'current_sessions'=>%d, 'action'=>'stop_and_wait_for_children', 'timeout'=>%d }\n",
N_tot_children, (int)g_children_map.size(), 120);
SendRawString(s, pmsg);
s->Close();
delete s;
ACCEPT_NEW = false;
CHILDREN_MAX_WAIT = 3600 * 24 * 3;
}
else if(req["action"] == "kill_server")
{
char pmsg[1024];
const std::lock_guard<std::mutex> lock(g_mutex);
snprintf(pmsg, 1024, "{ 'total_sessions'=>%d, 'current_sessions'=>%d, 'action'=>'stop' }\n",
N_tot_children, (int)g_children_map.size());
SendRawString(s, pmsg);
s->Close();
delete s;
ACCEPT_NEW = false;
}
else if(req["action"] == "dataformat")
{
pid_t pid = fork();
if (pid)
{
s->Close();
delete s;
}
else
{
sigaction(SIGCHLD, &sa_chld, NULL);
sigaction(SIGINT, &sa_int, NULL);
sigaction(SIGTERM, &sa_term, NULL);
// Close the server socket.
ss->Close();
// test
std::string dataPath = req["file"].get<std::string>();
TFile::SetReadStreamerInfo(false);
TFile* file = TFile::Open(dataPath.c_str());
std::string format = "edm";
if (file == nullptr)
{
format = "invalid";
}
else {
TObject* x = file->Get("VSD");
if (x)
format = "vsd";
}
char pmsg[1024];
snprintf(pmsg, 1024, "{ 'format'=>'%s' }\n", format.c_str());
SendRawString(s, pmsg);
s->Close();
delete s;
printf("exit format definition\n");
exit(0);
}
}
else if (req["action"] == "load")
{
int n_active = KillIdleProcesses();
if (n_active >= MAX_SERVERS)
{
char pmsg[1024];
snprintf(pmsg, 1024, "{ 'error'=>'Maximum number of servers reached (%d).' }\n",
MAX_SERVERS);
SendRawString(s, pmsg);
s->Close();
delete s;
continue;
}
++N_tot_children;
std::string logdir = req["logdir"].get<std::string>();
std::string logdirurl = req["logdirurl"].get<std::string>();
{
bool log_fail = assertDir(logdir);
if (log_fail)
{
char pmsg[1024];
snprintf(pmsg, 1024, "{ 'error'=>'Failure creating log file/directory. This is a service misconfiguration.' }\n");
SendRawString(s, pmsg);
s->Close();
delete s;
continue;
}
}
/*
std::string fwconfig = req["fwconfig"].get<std::string>();
std::cout << "fwconfig is set !!!! \n";
std::string fwconfigdir = req["fwconfigdir"].get<std::string>();
{
bool cfg_fail = assertDir(fwconfigdir);
if (cfg_fail)
{
char pmsg[1024];
snprintf(pmsg, 1024, "{ 'error'=>'Failure creating config file/directory. This is a service misconfiguration.' }\n");
SendRawString(s, pmsg);
s->Close();
delete s;
continue;
}
}*/
time_t epoch = time(0);
struct tm t;
localtime_r(&epoch, &t);
pid_t pid = fork();
if (pid)
{
s->Close();
delete s;
char log_fname[128];
snprintf(log_fname, 1024, "%d%02d%02d-%02d%02d%02d-%d.log",
1900 + t.tm_year, 1 + t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec,
pid);
logdir += "/";
logdir += log_fname;
std::string user = req["user"].get<std::string>();
g_mutex.lock();
g_children_map[pid] = ChildInfo(pid, N_tot_children, user, logdir);
g_mutex.unlock();
printf("Forked an instance for user %s, log is %s\n", user.c_str(),
logdir.c_str());
continue;
}
else
{
// We are the child and will reuse the socket to tell back where EVE dude has started.
sigaction(SIGCHLD, &sa_chld, NULL);
sigaction(SIGINT, &sa_int, NULL);
sigaction(SIGTERM, &sa_term, NULL);
// Close the server socket.
ss->Close();
// Close stdin, redirect stdout/err
fclose(stdin);
stdin = fopen("/dev/null", "r");
dup2(fileno(stdin), 0);
fclose(stdout);
fclose(stderr);
global_child_pid = getpid();
char log_fname[128];
snprintf(log_fname, 128, "%d%02d%02d-%02d%02d%02d-%d.log",
1900 + t.tm_year, 1 + t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec,
global_child_pid);
logdir += "/";
logdir += log_fname;
std::cout << "AMT debug ....logdir = " << logdir << "\n";
if ((stdout = fopen(logdir.c_str(), "w")) == nullptr)
{
snprintf(log_fname, 128, "<unable to open: %s>", strerror(errno));
}
stderr = stdout;
dup2(fileno(stdout), 1);
dup2(fileno(stderr), 2);
setlinebuf(stdout);
// gROOT->LoadMacro("evd.h");
std::string dataPath = req["file"].get<std::string>();
printf( "dataPath = %s \n", dataPath.c_str());
evd(dataPath.c_str());
auto gui = REX::gEve->GetWorld()->LastChild();
std::string lp = logdirurl + log_fname;
auto le = new REX::REveElement(lp, lp);
gui->AddElement(le);
// Connection key
TRandom3 rnd(0);
std::string con_key = RandomString(rnd, 16);
REX::gEve->GetWebWindow()->SetConnToken(con_key);
REX::gEve->Show();
auto url = REX::gEve->GetWebWindow()->GetUrl();
printf("URL %s \n", url.c_str());
std::regex re("(\\w+)://([^:]+):(\\d+)/*(.*)");
std::smatch m;
std::regex_search(url, m, re);
int nm = m.size();
printf("URL match %d\n", nm);
for (int i = 0; i < nm; ++i)
{
printf(" %d: %s\n", i, m[i].str().c_str());
}
{
char pmsg[1024];
snprintf(pmsg, 1024, "{ 'port'=>%s, 'dir'=>'%s', 'key'=>'%s', 'log_fname'=>'%s' }\n",
m[3].str().c_str(), m[4].str().c_str(), con_key.c_str(), log_fname);
SendRawString(s, pmsg);
}
std::cout << "close TSocket\n";
s->Close();
delete s;
app.Run();
// Start status report timer
StatReportTimer stat_report_timer;
stat_report_timer.SetTime(30 * 1000);
stat_report_timer.Start();
// Run the standard event loop.
// Exit.
printf("Exiting cmsShowWeb.\n");
exit(0);
}
}
}
}
// End condition met ... shutdown server socket.
ss->Close();
delete ss;
printf("Exited main loop adn shut down server socket.\n");
int n_children = KillIdleProcesses();
if (n_children > 0 && CHILDREN_MAX_WAIT > 0)
{
printf("Still have %d child procs, will wait for %d seconds.\n", n_children, CHILDREN_MAX_WAIT);
time_t shutdown_start = time(0);
while (true)
{
sleep(60);
n_children = KillIdleProcesses();
if (n_children == 0) {
printf("All child procs gone, service shutdown proceeding.\n");
break;
}
if (time(0) - shutdown_start >= CHILDREN_MAX_WAIT) {
printf("Child proc wait time over, service shutdown proceeding.\n");
break;
}
printf("In shutdown, elapsed time %d, final shutdown at %d.\n",
(int)(time(0) - shutdown_start), CHILDREN_MAX_WAIT);
}
}
if (n_children > 0)
{
printf("Still have %d children procs, will kill them now.\n", n_children);
}
sigaction(SIGCHLD, &sa_chld, NULL);
sigaction(SIGINT, &sa_int, NULL);
sigaction(SIGTERM, &sa_term, NULL);
g_mutex.lock();
for (const auto &[pid, cinfo] : g_children_map)
{
printf(" Killing child %d, pid=%d\n", cinfo.fSeqId, pid);
kill(pid, SIGKILL);
}
g_mutex.unlock();
printf("Removing message queue.\n");
msgctl(global_msgq_id, IPC_RMID, 0);
msgq_listener_thread.join();
printf("Revetor exiting\n");
}
// parse options utilities
typedef std::list<std::string> lStr_t;
typedef lStr_t::iterator lStr_i;
void next_arg_or_die(lStr_t &args, lStr_i &i, bool allow_single_minus = false)
{
lStr_i j = i;
if (++j == args.end() || ((*j)[0] == '-' && !(*j == "-" && allow_single_minus)))
{
std::cerr << "Error: option " << *i << " requires an argument.\n";
exit(1);
}
i = j;
}
int main(int argc, char *argv[])
{
lStr_t mArgs;
for (int i = 1; i < argc; ++i)
{
mArgs.push_back(argv[i]);
}
bool run_shell = false;
lStr_i i = mArgs.begin();
while (i != mArgs.end())
{
lStr_i start = i;
if (*i == "-h" || *i == "-help" || *i == "--help")
{ printf(
" --port <int> service port number\n"
" --help "
"\n\n");
exit(0);
}
else if (*i == "--port")
{
next_arg_or_die(mArgs, i);
std::cout << "convert t ostring \n " << *i << "\n";
SERVICE_PORT = atoi(i->c_str());
i++;
}
else
{
fprintf(stderr, "Error: Unknown option/argument '%s'.\n", i->c_str());
exit(1);
}
} // end command line
revetor();
return 0;
}