Salome HOME
Updated copyright comment
[modules/kernel.git] / src / Launcher / SALOME_ExternalServerHandler.cxx
1 // Copyright (C) 2019-2024  CEA, EDF, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony GEAY (EDF R&D)
20
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"
27
28 #include <sstream>
29 #include <fstream>
30 #include <algorithm>
31
32 #ifndef WIN32
33 #include <sys/types.h>
34 #include <signal.h>
35 #endif
36
37 unsigned SALOME_ExternalServerHandler::CNT = 0;
38
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)
40 {
41 }
42
43 SALOME_ExternalServerHandler::~SALOME_ExternalServerHandler()
44 {
45 }
46
47 void SALOME_ExternalServerHandler::registerToKill(const SALOME_CPythonHelper *pyHelper) const
48 {
49   std::ostringstream oss;
50   oss << _name << "_" << CNT++;
51   pyHelper->registerToSalomePiDict(oss.str(),_pid);
52 }
53
54 CORBA::Long SALOME_ExternalServerHandler::getPID()
55 {
56   return _pid;
57 }
58
59 char *SALOME_ExternalServerHandler::getName()
60 {
61   return CORBA::string_dup(_name.c_str());
62 }
63
64 void SALOME_ExternalServerHandler::KillPID(long pid)
65 {
66 #ifndef WIN32
67   kill(pid,SIGTERM);//SIGTERM is emitted not SIGKILL to give _pid process a chance to trap it.
68 #endif
69 }
70
71 void SALOME_ExternalServerHandler::killMe()
72 {
73   KillPID(_pid);
74 }
75
76 void SALOME_ExternalServerHandler::ping()
77 {
78 #ifndef WIN32
79   if( kill(_pid,0) != 0 )
80     {
81       std::ostringstream oss2; oss2 << "SALOME_ExternalServerHandler::ping : Fail to ping server " << _name << "\" with pid = " << _pid << " !";
82       throw SALOME_LauncherException(oss2.str());
83     }
84   // check for a non zombie process
85   std::ostringstream statusFile;
86   statusFile << "/proc/" << _pid << "/status";
87   std::ifstream ifs(statusFile.str());
88   if(!ifs.good())
89     {
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());
92     }
93   constexpr char PAT[]="State:";
94   while(ifs.good())
95     {
96       std::string line;
97       std::getline(ifs,line);
98       if(line.substr(0,strlen(PAT))==PAT)
99         {
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)
103             return ;
104           char state(part2[pos]);
105           if(state!='Z')
106             return ;
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());
109         }
110     }
111 #endif
112 }
113
114 void SALOME_ExternalServerHandler::shutdown()
115 {
116   killMe();
117   _boss->cleanServersInNS();
118 }
119
120 SALOME::LongVec *SALOME_ExternalServerHandler::listOfChildrenPID()
121 {
122   SALOME::LongVec *ret(new SALOME::LongVec);
123   const SALOME_CPythonHelper *pyh(_boss->getPyHelper());
124 #ifndef WIN32
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());
129   ret->length(sz);
130   for(size_t i=0;i<sz;i++)
131     (*ret)[i] = pids[i];
132 #endif
133   return ret;
134 }