1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SALOME Container : implementation of container and engine for Kernel
24 // File : Container_i.cxx
25 // Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
28 //#define private public
33 #include <sys/types.h>
46 #include "utilities.h"
47 #include <SALOMEconfig.h>
48 #include CORBA_SERVER_HEADER(SALOME_Component)
49 #include CORBA_SERVER_HEADER(SALOME_Exception)
50 #include <pthread.h> // must be before Python.h !
51 #include "SALOME_Container_i.hxx"
52 #include "SALOME_Component_i.hxx"
53 #include "SALOME_FileRef_i.hxx"
54 #include "SALOME_FileTransfer_i.hxx"
55 #include "Salome_file_i.hxx"
56 #include "SALOME_NamingService.hxx"
57 #include "SALOME_Fake_NamingService.hxx"
58 #include "Basics_Utils.hxx"
65 #include "Container_init_python.hxx"
67 bool _Sleeping = false ;
69 // // Needed by multi-threaded Python --- Supervision
73 extern "C" {void ActSigIntHandler() ; }
75 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
77 extern "C" {void SigIntHandler( int ) ; }
83 #define ENGINESO "Engine.dylib"
85 #define ENGINESO "Engine.so"
89 #define ENGINESO "Engine.dll"
100 std::map<std::string, int> Engines_Container_i::_cntInstances_map;
101 std::map<std::string, void *> Engines_Container_i::_library_map;
102 std::map<std::string, void *> Engines_Container_i::_toRemove_map;
103 omni_mutex Engines_Container_i::_numInstanceMutex ;
105 static PyObject* _pyCont;
107 int checkifexecutable(const std::string&);
108 int findpathof(const std::string& path, std::string&, const std::string&);
110 /*! \class Engines_Container_i
111 * \brief C++ implementation of Engines::Container interface
116 //=============================================================================
118 * Default constructor, not for use
120 //=============================================================================
122 Engines_Container_i::Engines_Container_i () :
123 _NS(0),_id(0),_numInstance(0)
127 //=============================================================================
131 //=============================================================================
133 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb,
134 PortableServer::POA_ptr poa,
135 char *containerName ,
136 int argc , char* argv[],
137 SALOME_NamingService_Abstract *ns,
138 bool isServantAloneInProcess
140 _NS(0),_id(0),_numInstance(0),_isServantAloneInProcess(isServantAloneInProcess)
142 _pid = (long)getpid();
150 std::string hostname = Kernel_Utils::GetHostname();
152 MESSAGE(hostname << " " << getpid() <<
153 " Engines_Container_i starting argc " <<
154 _argc << " Thread " << pthread_self() ) ;
156 MESSAGE(hostname << " " << _getpid() <<
157 " Engines_Container_i starting argc " << _argc<< " Thread " << pthread_self().p ) ;
163 MESSAGE(" argv" << i << " " << _argv[ i ]) ;
169 INFOS("SALOME_Container usage : SALOME_Container ServerName");
173 _isSupervContainer = false;
175 _orb = CORBA::ORB::_duplicate(orb) ;
176 _poa = PortableServer::POA::_duplicate(poa) ;
178 // Pour les containers paralleles: il ne faut pas enregistrer et activer
179 // le container generique, mais le container specialise
182 _id = _poa->activate_object(this);
183 _NS = ns==nullptr ? new SALOME_NamingService : ns->clone();
184 _NS->init_orb( _orb ) ;
185 CORBA::Object_var obj=_poa->id_to_reference(*_id);
186 Engines::Container_var pCont
187 = Engines::Container::_narrow(obj);
190 _containerName = _NS->BuildContainerNameForNS(containerName,
192 SCRUTE(_containerName);
193 _NS->Register(pCont, _containerName.c_str());
194 MESSAGE("Engines_Container_i::Engines_Container_i : Container name "
198 // import SALOME_Container
199 // pycont = SALOME_Container.SALOME_Container_i(containerIORStr)
201 CORBA::String_var sior = _orb->object_to_string(pCont);
202 std::string myCommand="pyCont = SALOME_Container.SALOME_Container_i('";
203 myCommand += _containerName + "','";
208 //[RNV]: Comment the PyEval_AcquireLock() and PyEval_ReleaseLock() because this
209 //approach leads to the deadlock of the main thread of the application on Windows platform
210 //in case if cppContainer runs in the standalone mode. The problem with the PyThreadState
211 //described by ABN seems not reproduced, to be checked carefully later...
212 PyGILState_STATE gstate = PyGILState_Ensure();
214 //// [ABN]: using the PyGILState* API here is unstable. omniORB logic is invoked
215 //// by the Python code executed below, and in some (random) cases, the Python code
216 //// execution ends with a PyThreadState which was not the one we have here.
217 //// (TODO: understand why ...)
218 //// To be on the safe side we get and load the thread state ourselves:
219 //PyEval_AcquireLock(); // get GIL
220 //PyThreadState * mainThreadState = PyThreadState_Get();
221 //PyThreadState_Swap(mainThreadState);
224 // mpv: this is temporary solution: there is a unregular crash if not
227 // first element is the path to Registry.dll, but it's wrong
228 PyRun_SimpleString("import sys\n");
229 PyRun_SimpleString("sys.path = sys.path[1:]\n");
231 PyRun_SimpleString("import SALOME_Container\n");
232 PyRun_SimpleString((char*)myCommand.c_str());
233 PyObject *mainmod = PyImport_AddModule("__main__");
234 PyObject *globals = PyModule_GetDict(mainmod);
235 _pyCont = PyDict_GetItemString(globals, "pyCont");
237 //PyThreadState_Swap(NULL);
238 //PyEval_ReleaseLock();
239 PyGILState_Release(gstate);
241 fileTransfer_i* aFileTransfer = new fileTransfer_i();
242 CORBA::Object_var obref=aFileTransfer->_this();
243 _fileTransfer = Engines::fileTransfer::_narrow(obref);
244 aFileTransfer->_remove_ref();
248 //=============================================================================
252 //=============================================================================
254 Engines_Container_i::~Engines_Container_i()
256 MESSAGE("Container_i::~Container_i()");
264 //=============================================================================
265 //! Get container name
267 * CORBA attribute: Container name (see constructor)
269 //=============================================================================
271 char* Engines_Container_i::name()
273 return CORBA::string_dup(_containerName.c_str()) ;
276 //=============================================================================
277 //! Get container working directory
279 * CORBA attribute: Container working directory
281 //=============================================================================
283 char* Engines_Container_i::workingdir()
287 return CORBA::string_dup(wd) ;
290 //=============================================================================
291 //! Get container log file name
293 * CORBA attribute: Container log file name
295 //=============================================================================
297 char* Engines_Container_i::logfilename()
299 return CORBA::string_dup(_logfilename.c_str()) ;
302 //! Set container log file name
303 void Engines_Container_i::logfilename(const char* name)
308 //=============================================================================
309 //! Get container host name
311 * CORBA method: Get the hostName of the Container (without domain extensions)
313 //=============================================================================
315 char* Engines_Container_i::getHostName()
317 std::string s = Kernel_Utils::GetHostname();
318 // MESSAGE("Engines_Container_i::getHostName " << s);
319 return CORBA::string_dup(s.c_str()) ;
322 //=============================================================================
323 //! Get container PID
325 * CORBA method: Get the PID (process identification) of the Container
327 //=============================================================================
329 CORBA::Long Engines_Container_i::getPID()
331 return (CORBA::Long)getpid();
334 //=============================================================================
335 //! Ping the servant to check it is still alive
337 * CORBA method: check if servant is still alive
339 //=============================================================================
340 void Engines_Container_i::ping()
342 MESSAGE("Engines_Container_i::ping() pid "<< getpid());
345 //=============================================================================
346 //! Shutdown the container
348 * CORBA method, oneway: Server shutdown.
349 * - Container name removed from naming service,
350 * - servant deactivation,
351 * - orb shutdown if no other servants in the process
353 //=============================================================================
354 void Engines_Container_i::Shutdown()
356 MESSAGE("Engines_Container_i::Shutdown()");
358 // Clear registered temporary files
359 clearTemporaryFiles();
361 /* For each component contained in this container
362 * tell it to self-destroy
364 std::map<std::string, Engines::EngineComponent_var>::iterator itm;
365 for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
369 itm->second->destroy();
371 catch(const CORBA::Exception&)
373 // ignore this entry and continue
377 // ignore this entry and continue
380 _listInstances_map.clear();
382 _NS->Destroy_FullDirectory(_containerName.c_str());
383 _NS->Destroy_Name(_containerName.c_str());
384 if(_isServantAloneInProcess)
386 MESSAGE("Effective Shutdown of container Begins...");
387 if(!CORBA::is_nil(_orb))
392 //=============================================================================
393 //! load a component implementation
396 * \param componentName component name
397 * \param reason explains error when load fails
398 * \return true if dlopen successful or already done, false otherwise
400 //=============================================================================
402 Engines_Container_i::load_component_Library(const char* componentName, CORBA::String_out reason)
405 //=================================================================
406 // --- C++ implementation section
407 //=================================================================
409 if(load_component_CppImplementation(componentName,retso))
411 reason=CORBA::string_dup("");
414 else if(retso != "ImplementationNotFound")
416 reason=CORBA::string_dup(retso.c_str());
421 retso+=componentName;
422 retso+=": Can't find C++ implementation ";
423 retso+=std::string(LIB) + componentName + ENGINESO;
425 //=================================================================
426 // --- Python implementation section
427 //=================================================================
429 if(load_component_PythonImplementation(componentName,retpy))
431 reason=CORBA::string_dup("");
434 else if(retpy != "ImplementationNotFound")
436 reason=CORBA::string_dup(retpy.c_str());
441 retpy+=componentName;
442 retpy+=": Can't find python implementation ";
443 retpy+=componentName;
446 //=================================================================
447 // -- Executable implementation section
448 //=================================================================
450 if(load_component_ExecutableImplementation(componentName,retex))
452 reason=CORBA::string_dup("");
455 else if(retex != "ImplementationNotFound")
457 reason=CORBA::string_dup(retex.c_str());
462 retex+=componentName;
463 retex+=": Can't find executable implementation ";
464 retex+=componentName;
467 std::string ret="Component implementation not found: ";
468 ret += componentName ;
474 std::cerr << ret << std::endl;
475 reason=CORBA::string_dup(ret.c_str());
480 //=============================================================================
481 //! try to load a C++ component implementation
484 * \param componentName the name of the component (COMPONENT, for example)
485 * \param reason explains error when load fails
486 * \return true if loading is successful or already done, false otherwise
488 //=============================================================================
490 Engines_Container_i::load_component_CppImplementation(const char* componentName, std::string& reason)
492 std::string aCompName(componentName);
493 std::string impl_name = std::string(LIB) + aCompName + ENGINESO;
496 _numInstanceMutex.lock(); // lock to be alone
497 // (see decInstanceCnt, finalize_removal))
498 if (_toRemove_map.count(impl_name) != 0) _toRemove_map.erase(impl_name);
499 if (_library_map.count(impl_name) != 0)
501 MESSAGE("Library " << impl_name << " already loaded");
502 _numInstanceMutex.unlock();
506 _numInstanceMutex.unlock();
510 handle = dlopen( impl_name.c_str() , RTLD_NOW | RTLD_GLOBAL ) ;
513 //not loadable. Try to find the lib file in LD_LIBRARY_PATH
516 char* p=getenv("DYLD_LIBRARY_PATH");
518 char* p=getenv("LD_LIBRARY_PATH");
521 path=path+SEP+"/usr/lib"+SEP+"/lib";
524 if(findpathof(path, pth, impl_name))
526 //found but not loadable
529 reason+=": C++ implementation found ";
531 reason+=" but it is not loadable. Error:\n";
533 std::cerr << reason << std::endl;
539 //continue with other implementation
540 reason="ImplementationNotFound";
547 std::wstring libToLoad = Kernel_Utils::utf8_decode_s( impl_name );
549 std::string libToLoad = impl_name;
551 handle = LoadLibrary(libToLoad.c_str() );
554 reason="ImplementationNotFound";
560 _numInstanceMutex.lock();
561 _library_map[impl_name] = handle;
562 _numInstanceMutex.unlock();
570 //=============================================================================
571 //! try to load a Python component implementation
574 * \param componentName name of the component
575 * \param reason explains error when load fails
576 * \return true if loading is successful or already done, false otherwise
578 //=============================================================================
580 Engines_Container_i::load_component_PythonImplementation(const char* componentName, std::string& reason)
582 std::string aCompName(componentName);
584 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
585 if (_library_map.count(aCompName) != 0)
587 _numInstanceMutex.unlock() ;
589 return true; // Python Component, already imported
591 _numInstanceMutex.unlock() ;
593 PyGILState_STATE gstate = PyGILState_Ensure();
594 PyObject *result = PyObject_CallMethod(_pyCont,
595 (char*)"import_component",
596 (char*)"s",componentName);
598 reason=PyUnicode_AsUTF8(result);
601 PyGILState_Release(gstate);
605 //Python component has been loaded (import componentName)
606 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
607 _library_map[aCompName] = (void *)_pyCont; // any non O value OK
608 _numInstanceMutex.unlock() ;
609 MESSAGE("import Python: "<< aCompName <<" OK");
612 else if(reason=="ImplementationNotFound")
614 //Python implementation has not been found. Continue with other implementation
615 reason="ImplementationNotFound";
619 //Python implementation has been found but loading has failed
620 std::cerr << reason << std::endl;
625 //=============================================================================
626 //! try to load a Executable component implementation
629 * \param componentName name of the component
630 * \param reason explains error when load fails
631 * \return true if loading is successful or already done, false otherwise
633 //=============================================================================
635 Engines_Container_i::load_component_ExecutableImplementation(const char* componentName, std::string& reason)
637 std::string aCompName(componentName);
638 std::string executable=aCompName+".exe";
643 char* p=getenv("PATH");
646 if (findpathof(path, pth, executable))
648 if(checkifexecutable(pth))
650 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
651 _library_map[executable] = (void *)1; // any non O value OK
652 _numInstanceMutex.unlock() ;
653 MESSAGE("import executable: "<< pth <<" OK");
659 reason+=": implementation found ";
661 reason+=" but it is not executable";
662 std::cerr << reason << std::endl;
666 reason="ImplementationNotFound";
671 //=============================================================================
672 //! Create a new component instance
674 * CORBA method: Creates a new servant instance of a component.
675 * The servant registers itself to naming service and Registry.
676 * \param genericRegisterName Name of the component instance to register
677 * in Registry & Name Service (without _inst_n suffix)
678 * \return a loaded component
680 //=============================================================================
681 Engines::EngineComponent_ptr
682 Engines_Container_i::create_component_instance(const char*genericRegisterName)
684 Engines::FieldsDict_var env = new Engines::FieldsDict;
686 Engines::EngineComponent_ptr compo =
687 create_component_instance_env(genericRegisterName, env, reason);
688 CORBA::string_free(reason);
692 //=============================================================================
693 //! Create a new component instance with environment variables specified
695 * CORBA method: Creates a new servant instance of a component.
696 * The servant registers itself to naming service and Registry.
697 * \param genericRegisterName Name of the component instance to register
698 * in Registry & Name Service (without _inst_n suffix)
699 * \param env dict of env variables
700 * \param reason explains error when create_component_instance_env fails
701 * \return a loaded component
703 //=============================================================================
704 Engines::EngineComponent_ptr
705 Engines_Container_i::create_component_instance_env(const char*genericRegisterName,
706 const Engines::FieldsDict& env,
707 CORBA::String_out reason)
710 if (_library_map.count(genericRegisterName) != 0)
712 // It's a Python component
713 Engines::EngineComponent_ptr compo = createPythonInstance(genericRegisterName, error);
714 reason=CORBA::string_dup(error.c_str());
718 std::string impl_name = std::string(LIB) + genericRegisterName + ENGINESO;
719 if (_library_map.count(impl_name) != 0)
721 // It's a C++ component
722 void* handle = _library_map[impl_name];
723 Engines::EngineComponent_ptr compo = createInstance(genericRegisterName, handle, error);
724 reason=CORBA::string_dup(error.c_str());
728 impl_name = std::string(genericRegisterName) + ".exe";
729 if (_library_map.count(impl_name) != 0)
731 //It's an executable component
732 Engines::EngineComponent_ptr compo = createExecutableInstance(genericRegisterName, env, error);
733 reason=CORBA::string_dup(error.c_str());
737 error="load_component_Library has probably not been called for component: ";
738 error += genericRegisterName;
740 reason=CORBA::string_dup(error.c_str());
741 return Engines::EngineComponent::_nil() ;
744 //=============================================================================
745 //! Create a new component instance (Executable implementation)
747 * \param CompName Name of the component instance
748 * \param env dict of env variables
749 * \param reason explains error when creation fails
750 * \return a loaded component
752 * This component is implemented in an executable with name genericRegisterName.exe
753 * It must register itself in Naming Service. The container waits some time (10 s max)
756 //=============================================================================
757 Engines::EngineComponent_ptr
758 Engines_Container_i::createExecutableInstance(std::string CompName,
759 const Engines::FieldsDict& env,
762 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
764 _numInstanceMutex.lock() ; // lock on the instance number
766 int numInstance = _numInstance ;
767 _numInstanceMutex.unlock() ;
770 sprintf( aNumI , "%d" , numInstance ) ;
771 std::string instanceName = CompName + "_inst_" + aNumI ;
772 std::string component_registerName = _containerName + "/" + instanceName;
774 //check if an entry exist in naming service
775 CORBA::Object_var nsobj = _NS->Resolve(component_registerName.c_str());
776 if ( !CORBA::is_nil(nsobj) )
778 // unregister the registered component
779 _NS->Destroy_Name(component_registerName.c_str());
780 //kill or shutdown it ???
783 // first arg container ior string
784 // second arg container name
785 // third arg instance name
787 Engines::Container_var pCont= _this();
788 CORBA::String_var sior = _orb->object_to_string(pCont);
792 command+=instanceName;
794 command+=instanceName;
799 command+= sior; // container ior string
801 command+=_containerName; //container name
803 command+=instanceName; //instance name
805 MESSAGE("SALOME_Container::create_component_instance command=" << command);
808 // use fork/execl instead of system to get finer control on env variables
811 if(pid == 0) // child
813 for (CORBA::ULong i=0; i < env.length(); i++)
815 if (env[i].value.type()->kind() == CORBA::tk_string)
818 env[i].value >>= value;
819 std::string s(env[i].key);
822 putenv(strdup(s.c_str()));
826 execl("/bin/sh", "sh", "-c", command.c_str() , (char *)0);
829 else if(pid < 0) // failed to fork
838 tpid = wait(&status);
839 } while (tpid != pid);
842 // launch component with a system call
843 int status=system(command.c_str());
848 reason="SALOME_Container::create_component_instance system failed (system command status -1)";
850 return Engines::EngineComponent::_nil();
853 else if (WEXITSTATUS(status) == 217)
855 reason="SALOME_Container::create_component_instance system failed (system command status 217)";
857 return Engines::EngineComponent::_nil();
863 if (getenv("TIMEOUT_TO_WAIT_EXE_COMPONENT") != 0)
865 std::string new_count_str = getenv("TIMEOUT_TO_WAIT_EXE_COMPONENT");
867 std::istringstream ss(new_count_str);
868 if (!(ss >> new_count))
870 INFOS("[Container] TIMEOUT_TO_WAIT_EXE_COMPONENT should be an int");
875 INFOS("[Container] waiting " << count << " second steps exe component ");
876 CORBA::Object_var obj = CORBA::Object::_nil() ;
877 while ( CORBA::is_nil(obj) && count )
885 MESSAGE( count << ". Waiting for component " << CompName);
886 obj = _NS->Resolve(component_registerName.c_str());
889 if(CORBA::is_nil(obj))
891 reason="SALOME_Container::create_component_instance failed";
893 return Engines::EngineComponent::_nil();
897 MESSAGE("SALOME_Container::create_component_instance successful");
898 iobject = Engines::EngineComponent::_narrow(obj);
899 _listInstances_map[instanceName] = iobject;
900 return iobject._retn();
906 //=============================================================================
907 //! Create a new component instance (Python implementation)
909 * \param CompName Name of the component instance
910 * \param reason explains error when creation fails
911 * \return a loaded component
913 //=============================================================================
914 Engines::EngineComponent_ptr
915 Engines_Container_i::createPythonInstance(std::string CompName,
918 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
920 _numInstanceMutex.lock() ; // lock on the instance number
922 int numInstance = _numInstance ;
923 _numInstanceMutex.unlock() ;
926 sprintf( aNumI , "%d" , numInstance ) ;
927 std::string instanceName = CompName + "_inst_" + aNumI ;
928 std::string component_registerName = _containerName + "/" + instanceName;
930 PyGILState_STATE gstate = PyGILState_Ensure();
931 PyObject *result = PyObject_CallMethod(_pyCont,
932 (char*)"create_component_instance",
935 instanceName.c_str());
938 PyArg_ParseTuple(result,"ss", &ior, &error);
939 std::string iors = ior;
942 PyGILState_Release(gstate);
946 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
947 iobject = Engines::EngineComponent::_narrow( obj ) ;
948 _listInstances_map[instanceName] = iobject;
950 return iobject._retn();
954 Engines_Container_i::create_python_service_instance(const char * CompName,
955 CORBA::String_out reason)
957 CORBA::Object_var object = CORBA::Object::_nil();
959 _numInstanceMutex.lock() ; // lock on the instance number
961 int numInstance = _numInstance ;
962 _numInstanceMutex.unlock() ;
965 sprintf( aNumI , "%d" , numInstance ) ;
966 std::string instanceName = std::string(CompName) + "_inst_" + aNumI ;
967 std::string component_registerName = _containerName + "/" + instanceName;
969 PyGILState_STATE gstate = PyGILState_Ensure();
970 PyObject *result = PyObject_CallMethod(_pyCont,
971 (char*)"create_component_instance",
974 instanceName.c_str());
977 PyArg_ParseTuple(result,"ss", &ior, &error);
978 reason = CORBA::string_dup(error);
979 char * _ior = CORBA::string_dup(ior);
981 PyGILState_Release(gstate);
987 //=============================================================================
988 //! Create a new component instance (C++ implementation)
990 * C++ method: create a servant instance of a component.
991 * \param genericRegisterName Name of the component instance to register
992 * in Registry & Name Service,
993 * (without _inst_n suffix, like "COMPONENT")
994 * \param handle loaded library handle
995 * \param reason explains error when creation fails
996 * \return a loaded component
998 * example with names:
999 * - aGenRegisterName = COMPONENT (= first argument)
1000 * - _containerName = /Containers/cli76ce/FactoryServer
1001 * - factoryName = COMPONENTEngine_factory
1002 * - component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
1003 * - instanceName = COMPONENT_inst_1
1004 * - component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
1006 //=============================================================================
1007 Engines::EngineComponent_ptr
1008 Engines_Container_i::createInstance(std::string genericRegisterName,
1010 std::string& reason)
1012 // --- find the factory
1014 std::string aGenRegisterName = genericRegisterName;
1015 std::string factory_name = aGenRegisterName + std::string("Engine_factory");
1016 SCRUTE(factory_name) ;
1018 typedef PortableServer::ObjectId* (*FACTORY_FUNCTION) (CORBA::ORB_ptr,
1019 PortableServer::POA_ptr,
1020 PortableServer::ObjectId *,
1025 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION)dlsym( handle, factory_name.c_str() );
1027 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION)GetProcAddress( (HINSTANCE)handle, factory_name.c_str() );
1030 if ( !Component_factory )
1032 MESSAGE( "Can't resolve symbol: " + factory_name );
1037 return Engines::EngineComponent::_nil() ;
1040 // --- create instance
1042 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
1046 _numInstanceMutex.lock() ; // lock on the instance number
1048 int numInstance = _numInstance ;
1049 _numInstanceMutex.unlock() ;
1052 sprintf( aNumI , "%d" , numInstance ) ;
1053 std::string instanceName = aGenRegisterName + "_inst_" + aNumI ;
1054 std::string component_registerName =
1055 _containerName + "/" + instanceName;
1057 // --- Instantiate required CORBA object
1059 PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
1060 id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(),
1061 aGenRegisterName.c_str() ) ;
1064 reason="Can't get ObjectId from factory";
1066 return iobject._retn();
1069 // --- get reference from id
1071 CORBA::Object_var obj = _poa->id_to_reference(*id);
1072 iobject = Engines::EngineComponent::_narrow( obj ) ;
1074 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1075 _listInstances_map[instanceName] = iobject;
1076 _cntInstances_map[aGenRegisterName] += 1;
1077 _numInstanceMutex.unlock() ;
1078 SCRUTE(aGenRegisterName);
1079 SCRUTE(_cntInstances_map[aGenRegisterName]);
1081 // --- register the engine under the name
1082 // containerName(.dir)/instanceName(.object)
1084 _NS->Register( iobject , component_registerName.c_str() ) ;
1085 MESSAGE( component_registerName.c_str() << " bound" ) ;
1089 reason="Container_i::createInstance exception caught";
1092 return iobject._retn();
1095 //=============================================================================
1096 //! Find an existing (in the container) component instance
1098 * CORBA method: Finds a servant instance of a component
1099 * \param registeredName Name of the component in Registry or Name Service,
1100 * without instance suffix number
1101 * \return the first found instance
1103 //=============================================================================
1104 Engines::EngineComponent_ptr
1105 Engines_Container_i::find_component_instance( const char* registeredName)
1107 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_nil();
1108 std::map<std::string,Engines::EngineComponent_var>::iterator itm =_listInstances_map.begin();
1109 while (itm != _listInstances_map.end())
1111 std::string instance = (*itm).first;
1113 if (instance.find(registeredName) == 0)
1115 anEngine = (*itm).second;
1116 return anEngine._retn();
1120 return anEngine._retn();
1123 //=============================================================================
1124 //! Remove the component instance from container
1126 * CORBA method: Stops the component servant, and deletes all related objects
1127 * \param component_i Component to be removed
1129 //=============================================================================
1131 void Engines_Container_i::remove_impl(Engines::EngineComponent_ptr component_i)
1133 ASSERT(! CORBA::is_nil(component_i));
1134 std::string instanceName = component_i->instanceName() ;
1135 MESSAGE("unload component " << instanceName);
1136 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1137 _listInstances_map.erase(instanceName);
1138 _numInstanceMutex.unlock() ;
1139 component_i->destroy() ;
1140 _NS->Destroy_Name(instanceName.c_str());
1143 //=============================================================================
1144 //! Unload component libraries from the container
1146 * CORBA method: Discharges unused libraries from the container.
1148 //=============================================================================
1149 void Engines_Container_i::finalize_removal()
1151 MESSAGE("finalize unload : dlclose");
1152 _numInstanceMutex.lock(); // lock to be alone
1153 // (see decInstanceCnt, load_component_Library)
1154 std::map<std::string, void *>::iterator ith;
1155 for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
1157 void *handle = (*ith).second;
1158 std::string impl_name= (*ith).first;
1163 // dlclose(handle); // SALOME unstable after ...
1164 // _library_map.erase(impl_name);
1167 _toRemove_map.clear();
1168 _numInstanceMutex.unlock();
1171 //=============================================================================
1172 //! Decrement component instance reference count
1176 //=============================================================================
1177 void Engines_Container_i::decInstanceCnt(std::string genericRegisterName)
1179 if(_cntInstances_map.count(genericRegisterName)==0)
1181 std::string aGenRegisterName =genericRegisterName;
1182 MESSAGE("Engines_Container_i::decInstanceCnt " << aGenRegisterName);
1183 ASSERT(_cntInstances_map[aGenRegisterName] > 0);
1184 _numInstanceMutex.lock(); // lock to be alone
1185 // (see finalize_removal, load_component_Library)
1186 _cntInstances_map[aGenRegisterName] -= 1;
1187 SCRUTE(_cntInstances_map[aGenRegisterName]);
1188 if (_cntInstances_map[aGenRegisterName] == 0)
1190 std::string impl_name =
1191 Engines_Component_i::GetDynLibraryName(aGenRegisterName.c_str());
1193 void* handle = _library_map[impl_name];
1195 _toRemove_map[impl_name] = handle;
1197 _numInstanceMutex.unlock();
1200 //=============================================================================
1201 //! Find or create a new component instance
1203 * CORBA method: find or create an instance of the component (servant),
1204 * load a new component class (dynamic library) if required,
1206 * ---- FOR COMPATIBILITY WITH 2.2 ----
1208 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
1210 * The servant registers itself to naming service and Registry.
1211 * \param genericRegisterName Name of the component to register
1212 * in Registry & Name Service
1213 * \param componentName Name of the constructed library of the component
1214 * \return a loaded component
1216 //=============================================================================
1218 Engines::EngineComponent_ptr
1219 Engines_Container_i::load_impl( const char* genericRegisterName,
1220 const char* /*componentName*/ )
1223 std::string impl_name = std::string(LIB) + genericRegisterName + ENGINESO;
1224 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
1225 if (load_component_Library(genericRegisterName,reason))
1226 iobject = find_or_create_instance(genericRegisterName, impl_name);
1227 CORBA::string_free(reason);
1228 return iobject._retn();
1231 //=============================================================================
1232 //! Finds an already existing component instance or create a new instance
1234 * C++ method: Finds an already existing servant instance of a component, or
1235 * create an instance.
1236 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
1237 * \param genericRegisterName Name of the component instance to register
1238 * in Registry & Name Service,
1239 * (without _inst_n suffix, like "COMPONENT")
1240 * \param componentLibraryName like "libCOMPONENTEngine.so"
1241 * \return a loaded component
1243 * example with names:
1244 * - aGenRegisterName = COMPONENT (= first argument)
1245 * - impl_name = libCOMPONENTEngine.so (= second argument)
1246 * - _containerName = /Containers/cli76ce/FactoryServer
1247 * - factoryName = COMPONENTEngine_factory
1248 * - component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
1249 * - instanceName = COMPONENT_inst_1
1250 * - component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
1252 //=============================================================================
1254 Engines::EngineComponent_ptr
1255 Engines_Container_i::find_or_create_instance(std::string genericRegisterName,
1256 std::string componentLibraryName)
1258 std::string aGenRegisterName = genericRegisterName;
1259 std::string impl_name = componentLibraryName;
1260 if (_library_map.count(impl_name) == 0)
1262 INFOS("shared library " << impl_name <<" must be loaded before creating instance");
1263 return Engines::EngineComponent::_nil() ;
1267 // --- find a registered instance in naming service, or create
1269 void* handle = _library_map[impl_name];
1270 std::string component_registerBase =
1271 _containerName + "/" + aGenRegisterName;
1272 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
1276 CORBA::Object_var obj =
1277 _NS->ResolveFirst( component_registerBase.c_str());
1278 if ( CORBA::is_nil( obj ) )
1280 iobject = createInstance(genericRegisterName,
1286 iobject = Engines::EngineComponent::_narrow( obj ) ;
1291 INFOS( "Container_i::load_impl caught" ) ;
1293 return iobject._retn();
1297 //=============================================================================
1298 //! Indicate if container is a python one
1300 * Retrieves only with container naming convention if it is a python container
1302 //=============================================================================
1303 bool Engines_Container_i::isPythonContainer(const char* ContainerName)
1306 size_t len=strlen(ContainerName);
1308 if(strcmp(ContainerName+len-2,"Py")==0)
1313 //=============================================================================
1314 //! Kill the container
1316 * CORBA method: Kill the container process with exit(0).
1317 * To remove : never returns !
1319 //=============================================================================
1320 bool Engines_Container_i::Kill_impl()
1322 MESSAGE("Engines_Container_i::Kill() pid "<< getpid() << " containerName "
1323 << _containerName.c_str() << " machineName "
1324 << Kernel_Utils::GetHostname().c_str());
1325 INFOS("===============================================================");
1326 INFOS("= REMOVE calls to Kill_impl in C++ container =");
1327 INFOS("===============================================================");
1333 //=============================================================================
1337 //=============================================================================
1338 void ActSigIntHandler()
1341 struct sigaction SigIntAct ;
1342 SigIntAct.sa_sigaction = &SigIntHandler ;
1343 sigemptyset(&SigIntAct.sa_mask);
1344 SigIntAct.sa_flags = SA_SIGINFO ;
1347 // DEBUG 03.02.2005 : the first parameter of sigaction is not a mask of signals
1348 // (SIGINT | SIGUSR1) :
1349 // it must be only one signal ===> one call for SIGINT
1350 // and an other one for SIGUSR1
1353 if ( sigaction( SIGINT , &SigIntAct, NULL ) )
1355 perror("SALOME_Container main ") ;
1358 if ( sigaction( SIGUSR1 , &SigIntAct, NULL ) )
1360 perror("SALOME_Container main ") ;
1363 if ( sigaction( SIGUSR2 , &SigIntAct, NULL ) )
1365 perror("SALOME_Container main ") ;
1369 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1370 // use of streams (and so on) should never be used because :
1371 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1372 // A stream operation may be interrupted by a signal and if the Handler use stream we
1373 // may have a "Dead-Lock" ===HangUp
1374 //==INFOS is commented
1375 // INFOS(pthread_self() << "SigIntHandler activated") ;
1378 signal( SIGINT, SigIntHandler );
1379 // legacy code required to supervisor. Commented in order to avoid problems on Windows
1380 // signal( SIGUSR1, SigIntHandler );
1386 void CallCancelThread() ;
1389 void SigIntHandler(int /*what*/ ,
1390 siginfo_t * siginfo ,
1393 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1394 // use of streams (and so on) should never be used because :
1395 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1396 // A stream operation may be interrupted by a signal and if the Handler use stream we
1397 // may have a "Dead-Lock" ===HangUp
1398 //==MESSAGE is commented
1399 // MESSAGE(pthread_self() << "SigIntHandler what " << what << std::endl
1400 // << " si_signo " << siginfo->si_signo << std::endl
1401 // << " si_code " << siginfo->si_code << std::endl
1402 // << " si_pid " << siginfo->si_pid) ;
1407 // MESSAGE("SigIntHandler END sleeping.") ;
1412 ActSigIntHandler() ;
1413 if ( siginfo->si_signo == SIGUSR1 )
1417 else if ( siginfo->si_signo == SIGUSR2 )
1419 CallCancelThread() ;
1424 // MESSAGE("SigIntHandler BEGIN sleeping.") ;
1431 // MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
1437 void SigIntHandler( int what )
1440 MESSAGE( pthread_self() << "SigIntHandler what " << what << std::endl );
1442 MESSAGE( "SigIntHandler what " << what << std::endl );
1447 MESSAGE("SigIntHandler END sleeping.") ;
1452 ActSigIntHandler() ;
1453 if ( what == SIGUSR1 )
1460 MESSAGE("SigIntHandler BEGIN sleeping.") ;
1467 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
1474 //=============================================================================
1475 //! Get or create a file reference object associated to a local file (to transfer it)
1477 * CORBA method: get or create a fileRef object associated to a local file
1478 * (a file on the computer on which runs the container server), which stores
1479 * a list of (machine, localFileName) corresponding to copies already done.
1481 * \param origFileName absolute path for a local file to copy on other
1483 * \return a fileRef object associated to the file.
1485 //=============================================================================
1486 Engines::fileRef_ptr
1487 Engines_Container_i::createFileRef(const char* origFileName)
1489 std::string origName(origFileName);
1490 Engines::fileRef_var theFileRef = Engines::fileRef::_nil();
1492 if (origName[0] != '/')
1494 INFOS("path of file to copy must be an absolute path beginning with '/'");
1495 return Engines::fileRef::_nil();
1498 if (CORBA::is_nil(_fileRef_map[origName]))
1500 CORBA::Object_var obj=_poa->id_to_reference(*_id);
1501 Engines::Container_var pCont = Engines::Container::_narrow(obj);
1502 fileRef_i* aFileRef = new fileRef_i(pCont, origFileName);
1503 theFileRef = Engines::fileRef::_narrow(aFileRef->_this());
1504 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1505 _fileRef_map[origName] = theFileRef;
1506 _numInstanceMutex.unlock() ;
1509 theFileRef = Engines::fileRef::_duplicate(_fileRef_map[origName]);
1510 ASSERT(! CORBA::is_nil(theFileRef));
1511 return theFileRef._retn();
1514 //=============================================================================
1515 //! Get a fileTransfer reference
1518 * \return a reference to the fileTransfer object
1520 //=============================================================================
1521 Engines::fileTransfer_ptr
1522 Engines_Container_i::getFileTransfer()
1524 Engines::fileTransfer_var aFileTransfer
1525 = Engines::fileTransfer::_duplicate(_fileTransfer);
1526 return aFileTransfer._retn();
1529 //=============================================================================
1530 //! Create a Salome file
1531 //=============================================================================
1532 Engines::Salome_file_ptr
1533 Engines_Container_i::createSalome_file(const char* origFileName)
1535 std::string origName(origFileName);
1536 if (CORBA::is_nil(_Salome_file_map[origName]))
1538 Salome_file_i* aSalome_file = new Salome_file_i();
1539 aSalome_file->setContainer(Engines::Container::_duplicate(this->_this()));
1542 aSalome_file->setLocalFile(origFileName);
1543 aSalome_file->recvFiles();
1545 catch (const SALOME::SALOME_Exception& /*e*/) //!< TODO: unused variable
1547 return Engines::Salome_file::_nil();
1550 Engines::Salome_file_var theSalome_file = Engines::Salome_file::_nil();
1551 theSalome_file = Engines::Salome_file::_narrow(aSalome_file->_this());
1552 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1553 _Salome_file_map[origName] = theSalome_file;
1554 _numInstanceMutex.unlock() ;
1557 Engines::Salome_file_ptr theSalome_file =
1558 Engines::Salome_file::_duplicate(_Salome_file_map[origName]);
1559 ASSERT(!CORBA::is_nil(theSalome_file));
1560 return theSalome_file;
1563 //=============================================================================
1564 /*! \brief copy a file from a remote host (container) to the local host
1565 * \param container the remote container
1566 * \param remoteFile the file to copy locally from the remote host into localFile
1567 * \param localFile the local file
1569 //=============================================================================
1570 void Engines_Container_i::copyFile(Engines::Container_ptr container, const char* remoteFile, const char* localFile)
1572 Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
1575 if ((fp = fopen(localFile,"wb")) == NULL)
1577 INFOS("file " << localFile << " cannot be open for writing");
1581 CORBA::Long fileId = fileTransfer->open(remoteFile);
1584 Engines::fileBlock* aBlock;
1591 aBlock = fileTransfer->getBlock(fileId);
1592 toFollow = aBlock->length();
1594 CORBA::Octet *buf = aBlock->get_buffer();
1595 fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
1599 MESSAGE("end of transfer");
1600 fileTransfer->close(fileId);
1604 INFOS("open reference file for copy impossible");
1608 //=============================================================================
1609 /*! \brief create a PyNode object to execute remote python code
1610 * \param nodeName the name of the node
1611 * \param code the python code to load
1612 * \return the PyNode
1614 //=============================================================================
1615 Engines::PyNode_ptr Engines_Container_i::createPyNode(const char* nodeName, const char* code)
1617 Engines::PyNode_var node= Engines::PyNode::_nil();
1619 PyGILState_STATE gstate = PyGILState_Ensure();
1620 PyObject *res = PyObject_CallMethod(_pyCont,
1621 (char*)"create_pynode",
1629 PyGILState_Release(gstate);
1630 SALOME::ExceptionStruct es;
1631 es.type = SALOME::INTERNAL_ERROR;
1632 es.text = "can not create a python node";
1633 throw SALOME::SALOME_Exception(es);
1635 long ierr=PyLong_AsLong(PyTuple_GetItem(res,0));
1636 PyObject* result=PyTuple_GetItem(res,1);
1637 std::string astr=PyUnicode_AsUTF8(result);
1639 PyGILState_Release(gstate);
1642 Utils_Locker lck(&_mutexForDftPy);
1643 CORBA::Object_var obj=_orb->string_to_object(astr.c_str());
1644 node=Engines::PyNode::_narrow(obj);
1645 std::map<std::string,Engines::PyNode_var>::iterator it(_dftPyNode.find(nodeName));
1646 if(it==_dftPyNode.end())
1648 _dftPyNode[nodeName]=node;
1652 Engines::PyNode_var oldNode((*it).second);
1653 if(!CORBA::is_nil(oldNode))
1654 oldNode->UnRegister();
1657 if(!CORBA::is_nil(node))
1659 return node._retn();
1663 SALOME::ExceptionStruct es;
1664 es.type = SALOME::INTERNAL_ERROR;
1665 es.text = astr.c_str();
1666 throw SALOME::SALOME_Exception(es);
1670 //=============================================================================
1671 /*! \brief Retrieves the last created PyNode instance with createPyNode.
1674 //=============================================================================
1675 Engines::PyNode_ptr Engines_Container_i::getDefaultPyNode(const char *nodeName)
1677 Utils_Locker lck(&_mutexForDftPy);
1678 std::map<std::string,Engines::PyNode_var>::iterator it(_dftPyNode.find(nodeName));
1679 if(it==_dftPyNode.end())
1680 return Engines::PyNode::_nil();
1683 Engines::PyNode_var tmpVar((*it).second);
1684 if(!CORBA::is_nil(tmpVar))
1685 return Engines::PyNode::_duplicate(tmpVar);
1687 return Engines::PyNode::_nil();
1691 //=============================================================================
1692 /*! \brief create a PyScriptNode object to execute remote python code
1693 * \param nodeName the name of the node
1694 * \param code the python code to load
1695 * \return the PyScriptNode
1697 //=============================================================================
1698 Engines::PyScriptNode_ptr Engines_Container_i::createPyScriptNode(const char* nodeName, const char* code)
1700 Engines::PyScriptNode_var node= Engines::PyScriptNode::_nil();
1702 PyGILState_STATE gstate = PyGILState_Ensure();
1703 PyObject *res = PyObject_CallMethod(_pyCont,
1704 (char*)"create_pyscriptnode",
1712 PyGILState_Release(gstate);
1713 SALOME::ExceptionStruct es;
1714 es.type = SALOME::INTERNAL_ERROR;
1715 es.text = "can not create a python node";
1716 throw SALOME::SALOME_Exception(es);
1718 long ierr=PyLong_AsLong(PyTuple_GetItem(res,0));
1719 PyObject* result=PyTuple_GetItem(res,1);
1720 std::string astr=PyUnicode_AsUTF8(result);
1722 PyGILState_Release(gstate);
1726 Utils_Locker lck(&_mutexForDftPy);
1727 CORBA::Object_var obj=_orb->string_to_object(astr.c_str());
1728 node=Engines::PyScriptNode::_narrow(obj);
1729 std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
1730 if(it==_dftPyScriptNode.end())
1732 _dftPyScriptNode[nodeName]=node;
1736 Engines::PyScriptNode_var oldNode((*it).second);
1737 if(!CORBA::is_nil(oldNode))
1738 oldNode->UnRegister();
1741 return node._retn();
1745 SALOME::ExceptionStruct es;
1746 es.type = SALOME::INTERNAL_ERROR;
1747 es.text = astr.c_str();
1748 throw SALOME::SALOME_Exception(es);
1752 void Engines_Container_i::removePyScriptNode(const char *nodeName)
1754 std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
1755 if(it==_dftPyScriptNode.end())
1757 std::ostringstream oss; oss << "Engines_Container_i::removePyScriptNode : node \"" << nodeName << "\" is not map !";
1758 SALOME::ExceptionStruct es;
1759 es.type = SALOME::INTERNAL_ERROR;
1760 es.text = oss.str().c_str();
1761 throw SALOME::SALOME_Exception(es);
1763 (*it).second->UnRegister();
1764 _dftPyScriptNode.erase(it);
1767 void Engines_Container_i::cleanAllPyScripts()
1769 for(std::map<std::string,Engines::PyNode_var>::iterator it=_dftPyNode.begin();it!=_dftPyNode.end();it++)
1771 Engines::PyNode_var tmpVar((*it).second);
1772 if(!CORBA::is_nil(tmpVar))
1773 tmpVar->UnRegister();
1776 for(std::map<std::string,Engines::PyScriptNode_var>::iterator it=_dftPyScriptNode.begin();it!=_dftPyScriptNode.end();it++)
1778 Engines::PyScriptNode_var tmpVar((*it).second);
1779 if(!CORBA::is_nil(tmpVar))
1780 tmpVar->UnRegister();
1782 _dftPyScriptNode.clear();
1785 //=============================================================================
1786 /*! \brief Retrieves the last created PyScriptNode instance with createPyScriptNode.
1789 //=============================================================================
1790 Engines::PyScriptNode_ptr Engines_Container_i::getDefaultPyScriptNode(const char *nodeName)
1792 Utils_Locker lck(&_mutexForDftPy);
1793 std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
1794 if(it==_dftPyScriptNode.end())
1795 return Engines::PyScriptNode::_nil();
1798 Engines::PyScriptNode_var tmpVar((*it).second);
1799 if(!CORBA::is_nil(tmpVar))
1800 return Engines::PyScriptNode::_duplicate(tmpVar);
1802 return Engines::PyScriptNode::_nil();
1806 //=============================================================================
1807 /* int checkifexecutable(const char *filename)
1809 * Return non-zero if the name is an executable file, and
1810 * zero if it is not executable, or if it does not exist.
1812 //=============================================================================
1813 int checkifexecutable(const std::string& filename)
1816 struct stat statinfo;
1818 result = stat(filename.c_str(), &statinfo);
1819 if (result < 0) return 0;
1820 if (!S_ISREG(statinfo.st_mode)) return 0;
1825 if (statinfo.st_uid == geteuid()) return statinfo.st_mode & S_IXUSR;
1826 if (statinfo.st_gid == getegid()) return statinfo.st_mode & S_IXGRP;
1827 return statinfo.st_mode & S_IXOTH;
1832 //=============================================================================
1833 /*! \brief Find a file by searching in a path
1834 * \param filename file name to search
1835 * \param path path to search in
1836 * \param pth the complete file path if found
1837 * \return 1 if found 0 if not
1839 //=============================================================================
1840 int findpathof(const std::string& path, std::string& pth, const std::string& filename)
1842 if ( path.size() == 0 ) return 0;
1844 std::string::size_type offset = 0;
1845 std::string::size_type pos = 0;
1847 struct stat statinfo;
1851 pos = path.find( SEP, offset );
1852 pth = path.substr( offset, pos - offset );
1853 if ( pth.size() > 0 )
1855 if( pth[pth.size()-1] != SLASH ) pth += SLASH;
1857 int result=stat(pth.c_str(), &statinfo);
1858 if(result == 0) found=1;
1860 if (pos == std::string::npos) break;
1866 void Engines_Container_i::registerTemporaryFile( const std::string& fileName )
1868 _tmp_files.remove( fileName );
1869 _tmp_files.push_back( fileName );
1872 void Engines_Container_i::unregisterTemporaryFile( const std::string& fileName )
1874 _tmp_files.remove( fileName );
1877 void Engines_Container_i::clearTemporaryFiles()
1879 std::list<std::string>::const_iterator it;
1880 for ( it = _tmp_files.begin(); it != _tmp_files.end(); ++it ) {
1881 #if defined(WIN32) && defined(UNICODE)
1882 std::wstring aFile = Kernel_Utils::utf8_decode_s(*it);
1883 std::wstring command = (GetFileAttributes(aFile.c_str()) == FILE_ATTRIBUTE_DIRECTORY) ? L"rd /Q \"" : L"del /F /Q \"";
1885 command += L"\" 2>NUL";
1886 _wsystem(command.c_str());
1889 std::string aFile = *it;
1890 std::string command = (GetFileAttributes(aFile.c_str()) == FILE_ATTRIBUTE_DIRECTORY) ? "rd /Q \"" : "del /F /Q \"";
1892 command += "\" 2>NUL";
1894 std::string command = "rm -rf ";
1897 system(command.c_str());
1903 static Engines_Container_i *_container_singleton_ssl = nullptr;
1905 static Engines::Container_var _container_ref_singleton_ssl;
1907 Engines_Container_i *KERNEL::getContainerSA()
1909 if(!_container_singleton_ssl)
1912 CORBA::ORB_var orb = CORBA::ORB_init(argc,nullptr);
1913 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
1914 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
1915 PortableServer::POAManager_var pman = poa->the_POAManager();
1916 CORBA::PolicyList policies;
1919 char *argv[4] = {"Container","FactoryServer","toto",nullptr};
1920 SALOME_Fake_NamingService ns;
1921 _container_singleton_ssl = new Engines_Container_i(orb,poa,"FactoryServer",2,argv,&ns,false);
1922 PortableServer::ObjectId * cont_id = _container_singleton_ssl->getCORBAId();
1924 CORBA::Object_var zeRef = poa->id_to_reference(*cont_id);
1925 _container_ref_singleton_ssl = Engines::Container::_narrow(zeRef);
1927 return _container_singleton_ssl;
1930 Engines::Container_var KERNEL::getContainerRefSA()
1933 return _container_ref_singleton_ssl;