Salome HOME
SALOME_Container\(_No_Serv\) executables set SSLMode singleton properly
[modules/kernel.git] / src / Container / SALOME_Container_Common.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME Container : implementation of container and engine for Kernel
24 //  File   : SALOME_Container.cxx
25 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
26 //  Module : SALOME
27 //  $Header$
28 //
29 #ifdef _MPI_SEQ_CONTAINER_
30   #ifdef HAVE_MPI2
31 #include <mpi.h>
32   #endif
33 #endif
34
35 #include <iostream>
36 #include <sstream>
37 #include <string>
38 #include <stdio.h>
39 #include <time.h>
40 #ifndef WIN32
41 # include <sys/time.h>
42 # include <dlfcn.h>
43 #endif
44
45
46 #ifndef WIN32
47 #include <unistd.h>
48 #else
49 #include <process.h>
50 #endif
51 #include "SALOME_Container_Common.hxx"
52 #include "SALOME_Container_i.hxx"
53 #include "utilities.h"
54 #include "Utils_ORB_INIT.hxx"
55 #include "Utils_SINGLETON.hxx"
56 #include "OpUtil.hxx"
57 #include "KernelBasis.hxx"
58
59 #ifdef CHECKTIME
60 #include <Utils_Timer.hxx>
61 #endif
62
63 #include "Container_init_python.hxx"
64
65 extern "C" void HandleServerSideSignals(CORBA::ORB_ptr theORB);
66
67 #include <stdexcept>
68 #include <signal.h>
69 #include <sys/types.h>
70 #ifndef WIN32
71 # include <sys/wait.h>
72 #endif
73
74 #include <memory>
75
76 void AttachDebugger();
77 void Handler(int);
78 void terminateHandler();
79 void unexpectedHandler();
80
81 #ifndef WIN32
82 void (* setsig(int, void (*)(int)))(int);
83
84 typedef void (*sighandler_t)(int);
85 sighandler_t setsig(int sig, sighandler_t handler)
86 {
87   struct sigaction context, ocontext;
88   context.sa_handler = handler;
89   sigemptyset(&context.sa_mask);
90   context.sa_flags = 0;
91   if (sigaction(sig, &context, &ocontext) == -1)
92     return SIG_ERR;
93   return ocontext.sa_handler;
94 }
95 #endif //WIN32
96
97 void AttachDebugger()
98 {
99 #ifndef WIN32
100   if(getenv ("DEBUGGER"))
101     {
102       std::stringstream exec;
103       exec << "$DEBUGGER SALOME_Container " << getpid() << "&";
104       std::cerr << exec.str() << std::endl;
105       system(exec.str().c_str());
106       while(1);
107     }
108 #endif
109 }
110
111 void Handler(int theSigId)
112 {
113   std::cerr << "Signal= "<< theSigId  << std::endl;
114   AttachDebugger();
115   //to exit or not to exit
116   _exit(1);
117 }
118
119 void terminateHandler(void)
120 {
121   std::cerr << "Terminate: not managed exception !"  << std::endl;
122   AttachDebugger();
123 }
124
125 void unexpectedHandler(void)
126 {
127   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
128   AttachDebugger();
129 }
130
131 int container_common_main(int argc, char* argv[], std::unique_ptr<SALOME_NamingService_Container_Abstract> ns)
132 {
133   setSSLMode(!ns->IsTrueNS());
134 #ifdef _MPI_SEQ_CONTAINER_
135   #ifdef HAVE_MPI2
136   MPI_Init(&argc,&argv);
137   #endif
138 #endif  
139
140 #ifndef WIN32
141   if(getenv ("DEBUGGER"))
142     {
143       setsig(SIGSEGV,&Handler);
144       setsig(SIGFPE,&Handler);
145       std::set_terminate(&terminateHandler);
146       std::set_unexpected(&unexpectedHandler);
147     }
148 #endif
149
150   // Initialise the ORB.
151   //SRN: BugID: IPAL9541, it's necessary to set a size of one message to be at least 100Mb
152   //CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ;
153   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
154   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
155   CORBA::ORB_ptr orb = init(argc , argv ) ;
156
157   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
158   INFOS_COMPILATION;
159   BEGIN_OF(argv[0]);
160
161   ASSERT(argc > 1);
162   SCRUTE(argv[1]);
163
164   KERNEL_PYTHON::init_python(argc,argv);
165     
166   char *containerName = (char *)"";
167   if(argc > 1)
168     {
169       containerName = argv[1] ;
170     }
171
172   try
173     {  
174       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
175       ASSERT(!CORBA::is_nil(obj));
176       PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
177
178       PortableServer::POAManager_var pman = root_poa->the_POAManager();
179
180       // add new container to the kill list
181 #ifndef WIN32
182       std::stringstream aCommand ;
183       aCommand << "addToKillList.py " << getpid() << " SALOME_Container" << std::ends ;
184       system(aCommand.str().c_str());
185 #endif
186       
187       new Engines_Container_i(orb, root_poa, containerName , argc , argv, ns.get() );
188       ns.release();
189
190       pman->activate();
191       
192 #ifdef CHECKTIME
193       Utils_Timer timer;
194       timer.Start();
195       timer.Stop();
196       timer.ShowAbsolute();
197 #endif
198
199       HandleServerSideSignals(orb);
200
201 //#define MEMORYLEAKS
202 #ifdef MEMORYLEAKS
203         PyGILState_Ensure();
204         //Destroy orb from python (for chasing memory leaks)
205         PyRun_SimpleString("from omniORB import CORBA");
206         PyRun_SimpleString("orb=CORBA.ORB_init([''], CORBA.ORB_ID)");
207         PyRun_SimpleString("orb.destroy()");
208         Py_Finalize();
209 #endif
210     }
211   catch(CORBA::SystemException&)
212     {
213       INFOS("Caught CORBA::SystemException.");
214     }
215   catch(PortableServer::POA::ServantAlreadyActive&)
216     {
217       INFOS("Caught CORBA::ServantAlreadyActiveException");
218     }
219   catch(CORBA::Exception&)
220     {
221       INFOS("Caught CORBA::Exception.");
222     }
223   catch(std::exception& exc)
224     {
225       INFOS("Caught std::exception - "<<exc.what()); 
226     }
227   catch(...)
228     {
229       INFOS("Caught unknown exception.");
230     }
231
232 #ifdef _MPI_SEQ_CONTAINER_
233   #ifdef HAVE_MPI2
234   MPI_Finalize();
235   #endif
236 #endif  
237
238   return 0 ;
239 }
240