1 // Copyright (C) 2019-2024 CEA, EDF, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony GEAY (EDF R&D)
21 #include "SALOME_ExternalServerHandler.hxx"
22 #include "SALOME_ExternalServerLauncher.hxx"
23 #include "SALOME_NamingService.hxx"
24 #include "SALOME_LauncherException.hxx"
25 #include "SALOME_ContainerManager.hxx"
26 #include "SALOME_CPythonHelper.hxx"
33 #include <sys/types.h>
37 unsigned SALOME_ExternalServerHandler::CNT = 0;
39 SALOME_ExternalServerHandler::SALOME_ExternalServerHandler(SALOME_ExternalServerLauncher *boss, const std::string& name, SALOME_NamingService_Abstract *ns, long pid):_name(name),_pid(pid),_NS(ns),_boss(boss)
43 SALOME_ExternalServerHandler::~SALOME_ExternalServerHandler()
47 void SALOME_ExternalServerHandler::registerToKill(const SALOME_CPythonHelper *pyHelper) const
49 std::ostringstream oss;
50 oss << _name << "_" << CNT++;
51 pyHelper->registerToSalomePiDict(oss.str(),_pid);
54 CORBA::Long SALOME_ExternalServerHandler::getPID()
59 char *SALOME_ExternalServerHandler::getName()
61 return CORBA::string_dup(_name.c_str());
64 void SALOME_ExternalServerHandler::KillPID(long pid)
67 kill(pid,SIGTERM);//SIGTERM is emitted not SIGKILL to give _pid process a chance to trap it.
71 void SALOME_ExternalServerHandler::killMe()
76 void SALOME_ExternalServerHandler::ping()
79 if( kill(_pid,0) != 0 )
81 std::ostringstream oss2; oss2 << "SALOME_ExternalServerHandler::ping : Fail to ping server " << _name << "\" with pid = " << _pid << " !";
82 throw SALOME_LauncherException(oss2.str());
84 // check for a non zombie process
85 std::ostringstream statusFile;
86 statusFile << "/proc/" << _pid << "/status";
87 std::ifstream ifs(statusFile.str());
90 std::ostringstream oss2; oss2 << "SALOME_ExternalServerHandler::ping : Fail to ping server " << _name << "\" with pid = " << _pid << " during access of status file !";
91 throw SALOME_LauncherException(oss2.str());
93 constexpr char PAT[]="State:";
97 std::getline(ifs,line);
98 if(line.substr(0,strlen(PAT))==PAT)
100 std::string part2(line.substr(strlen(PAT)));
101 std::size_t pos(part2.find_first_not_of(" \t"));
102 if(pos==std::string::npos)
104 char state(part2[pos]);
107 std::ostringstream oss2; oss2 << "SALOME_ExternalServerHandler::ping : server " << _name << "\" with pid = " << _pid << " has been detected as a Zombie !";
108 throw SALOME_LauncherException(oss2.str());
114 void SALOME_ExternalServerHandler::shutdown()
117 _boss->cleanServersInNS();
120 SALOME::LongVec *SALOME_ExternalServerHandler::listOfChildrenPID()
122 SALOME::LongVec *ret(new SALOME::LongVec);
123 const SALOME_CPythonHelper *pyh(_boss->getPyHelper());
125 std::ostringstream oss;
126 oss << "[int(elt) for elt in sp.check_output([\"ps\",\"-o\",\"pid=\",\"--ppid\",\"" << _pid << "\"]).split()]";
127 std::vector<long> pids(pyh->evalVL(oss.str()));
128 std::size_t sz(pids.size());
130 for(size_t i=0;i<sz;i++)