Salome HOME
bos #26457 Factorization of ORB initialization
[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 "ArgvKeeper.hxx"
52 #include "SALOME_Container_Common.hxx"
53 #include "SALOME_Container_i.hxx"
54 #include "utilities.h"
55 #include "OpUtil.hxx"
56 #include "KernelBasis.hxx"
57
58 #ifdef CHECKTIME
59 #include <Utils_Timer.hxx>
60 #endif
61
62 #include "Container_init_python.hxx"
63
64 extern "C" void HandleServerSideSignals(CORBA::ORB_ptr theORB);
65
66 #include <stdexcept>
67 #include <signal.h>
68 #include <sys/types.h>
69 #ifndef WIN32
70 # include <sys/wait.h>
71 #endif
72
73 #include <memory>
74
75 void AttachDebugger();
76 void Handler(int);
77 void terminateHandler();
78 void unexpectedHandler();
79
80 #ifndef WIN32
81 void (* setsig(int, void (*)(int)))(int);
82
83 typedef void (*sighandler_t)(int);
84 sighandler_t setsig(int sig, sighandler_t handler)
85 {
86   struct sigaction context, ocontext;
87   context.sa_handler = handler;
88   sigemptyset(&context.sa_mask);
89   context.sa_flags = 0;
90   if (sigaction(sig, &context, &ocontext) == -1)
91     return SIG_ERR;
92   return ocontext.sa_handler;
93 }
94 #endif //WIN32
95
96 void AttachDebugger()
97 {
98 #ifndef WIN32
99   if(getenv ("DEBUGGER"))
100     {
101       std::stringstream exec;
102       exec << "$DEBUGGER SALOME_Container " << getpid() << "&";
103       std::cerr << exec.str() << std::endl;
104       system(exec.str().c_str());
105       while(1);
106     }
107 #endif
108 }
109
110 void Handler(int theSigId)
111 {
112   std::cerr << "Signal= "<< theSigId  << std::endl;
113   AttachDebugger();
114   //to exit or not to exit
115   _exit(1);
116 }
117
118 void terminateHandler(void)
119 {
120   std::cerr << "Terminate: not managed exception !"  << std::endl;
121   AttachDebugger();
122 }
123
124 void unexpectedHandler(void)
125 {
126   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
127   AttachDebugger();
128 }
129
130 int container_common_main(int argc, char* argv[], std::unique_ptr<SALOME_NamingService_Container_Abstract> ns)
131 {
132   setSSLMode(ns?!ns->IsTrueNS():false);
133 #ifdef _MPI_SEQ_CONTAINER_
134   #ifdef HAVE_MPI2
135   MPI_Init(&argc,&argv);
136   #endif
137 #endif  
138
139 #ifndef WIN32
140   if(getenv ("DEBUGGER"))
141     {
142       setsig(SIGSEGV,&Handler);
143       setsig(SIGFPE,&Handler);
144       std::set_terminate(&terminateHandler);
145       std::set_unexpected(&unexpectedHandler);
146     }
147 #endif
148
149   // Initialise the ORB.
150   //SRN: BugID: IPAL9541, it's necessary to set a size of one message to be at least 100Mb
151   //CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ;
152   SetArgcArgv(argc, argv);
153   CORBA::ORB_ptr orb = KERNEL::GetRefToORB();
154
155   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
156   INFOS_COMPILATION;
157   BEGIN_OF(argv[0]);
158
159   ASSERT(argc > 1);
160   SCRUTE(argv[1]);
161
162   KERNEL_PYTHON::init_python(argc,argv);
163     
164   char *containerName = (char *)"";
165   if(argc > 1)
166     {
167       containerName = argv[1] ;
168     }
169
170   try
171     {  
172       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
173       ASSERT(!CORBA::is_nil(obj));
174       PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
175
176       PortableServer::POAManager_var pman = root_poa->the_POAManager();
177
178       // add new container to the kill list
179 #ifndef WIN32
180       std::stringstream aCommand ;
181       aCommand << "addToKillList.py " << getpid() << " SALOME_Container" << std::ends ;
182       system(aCommand.str().c_str());
183 #endif
184       
185       new Engines_Container_i(orb, root_poa, containerName , argc , argv, ns.get() );
186       ns.release();
187
188       pman->activate();
189       
190 #ifdef CHECKTIME
191       Utils_Timer timer;
192       timer.Start();
193       timer.Stop();
194       timer.ShowAbsolute();
195 #endif
196
197       HandleServerSideSignals(orb);
198
199 //#define MEMORYLEAKS
200 #ifdef MEMORYLEAKS
201         PyGILState_Ensure();
202         //Destroy orb from python (for chasing memory leaks)
203         PyRun_SimpleString("from omniORB import CORBA");
204         PyRun_SimpleString("orb=CORBA.ORB_init([''], CORBA.ORB_ID)");
205         PyRun_SimpleString("orb.destroy()");
206         Py_Finalize();
207 #endif
208     }
209   catch(CORBA::SystemException&)
210     {
211       INFOS("Caught CORBA::SystemException.");
212     }
213   catch(PortableServer::POA::ServantAlreadyActive&)
214     {
215       INFOS("Caught CORBA::ServantAlreadyActiveException");
216     }
217   catch(CORBA::Exception&)
218     {
219       INFOS("Caught CORBA::Exception.");
220     }
221   catch(std::exception& exc)
222     {
223       INFOS("Caught std::exception - "<<exc.what()); 
224     }
225   catch(...)
226     {
227       INFOS("Caught unknown exception.");
228     }
229
230 #ifdef _MPI_SEQ_CONTAINER_
231   #ifdef HAVE_MPI2
232   MPI_Finalize();
233   #endif
234 #endif  
235
236   return 0 ;
237 }
238