Salome HOME
174fb00f3aa97d9f63f88a8d0a6cdcb66e526e28
[modules/kernel.git] / bin / killSalomeWithPort.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 #
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24
25 ## \file killSalomeWithPort.py
26 #  Stop all %SALOME servers from given sessions by killing them
27 #
28 #  The sessions are indicated by their ports on the command line as in :
29 #  \code
30 #  killSalomeWithPort.py 2811 2815
31 #  \endcode
32 #
33
34 import os, sys, pickle, signal, commands,glob
35 from salome_utils import verbose
36 import Utils_Identity
37 import salome_utils
38
39 def getPiDict(port,appname='salome',full=True,hidden=True,hostname=None):
40     """
41     Get file with list of SALOME processes.
42     This file is located in the user's home directory
43     and named .<user>_<host>_<port>_SALOME_pidict
44     where
45     <user> is user name
46     <host> is host name
47     <port> is port number
48
49     Parameters:
50     - port    : port number
51     - appname : application name (default is 'SALOME')
52     - full    : if True, full path to the file is returned, otherwise only file name is returned
53     - hidden  : if True, file name is prefixed with . (dot) symbol; this internal parameter is used
54     to support compatibility with older versions of SALOME
55     """
56     from salome_utils import generateFileName, getTmpDir
57     dir = ""
58     if not hostname:
59         hostname = os.getenv("NSHOST")
60         if hostname: hostname = hostname.split(".")[0]
61         pass
62     if full:
63         # full path to the pidict file is requested
64         if hidden:
65             # new-style dot-prefixed pidict files
66             # are in the system-dependant temporary diretory
67             dir = getTmpDir()
68         else:
69             # old-style non-dot-prefixed pidict files
70             # are in the user's home directory
71             dir = os.getenv("HOME")
72             pass
73         pass
74     return generateFileName(dir,
75                             suffix="pidict",
76                             hidden=hidden,
77                             with_username=True,
78                             with_hostname=hostname or True,
79                             with_port=port,
80                             with_app=appname.upper())
81
82 def appliCleanOmniOrbConfig(port):
83     """
84     Remove omniorb config files related to the port in SALOME application:
85     - ${HOME}/${APPLI}/USERS/.omniORB_${USER}_${HOSTNAME}_${NSPORT}.cfg
86     - ${HOME}/${APPLI}/USERS/.omniORB_${USER}_last.cfg
87     the last is removed only if the link points to the first file.
88     """
89     from salome_utils import generateFileName, getUserName
90     home  = os.getenv("HOME")
91     appli = os.getenv("APPLI")
92     if appli is None:
93         #Run outside application context
94         pass
95     else:
96         dir = os.path.join(os.path.realpath(home), appli,"USERS")
97         omniorb_config      = generateFileName(dir, prefix="omniORB",
98                                                extension="cfg",
99                                                hidden=True,
100                                                with_username=True,
101                                                with_hostname=True,
102                                                with_port=port)
103         last_running_config = generateFileName(dir, prefix="omniORB",
104                                                with_username=True,
105                                                suffix="last",
106                                                extension="cfg",
107                                                hidden=True)
108         if os.access(last_running_config,os.F_OK):
109             pointedPath = os.readlink(last_running_config)
110             if pointedPath[0] != '/':
111                 pointedPath=os.path.join(os.path.dirname(last_running_config), pointedPath)
112             if pointedPath == omniorb_config:
113                 os.unlink(last_running_config)
114                 pass
115             pass
116         if os.access(omniorb_config,os.F_OK):
117             os.remove(omniorb_config)
118             pass
119
120         if os.path.lexists(last_running_config):return
121
122         #try to relink last.cfg to an existing config file if any
123         files = glob.glob(os.path.join(os.environ["HOME"],Utils_Identity.getapplipath(),
124                                        "USERS",".omniORB_"+getUserName()+"_*.cfg"))
125         current_config=None
126         current=0
127         for f in files:
128           stat=os.stat(f)
129           if stat.st_atime > current:
130             current=stat.st_atime
131             current_config=f
132         if current_config:
133           os.symlink(os.path.normpath(current_config), last_running_config)
134
135         pass
136     pass
137
138 ########## kills all salome processes with the given port ##########
139
140 def shutdownMyPort(port, cleanup=True):
141     """
142     Shutdown SALOME session running on the specified port.
143     Parameters:
144     - port - port number
145     """
146     if not port: return
147
148     from salome_utils import generateFileName
149
150     # set OMNIORB_CONFIG variable to the proper file
151     home  = os.getenv("HOME")
152     appli = os.getenv("APPLI")
153     kwargs = {}
154     if appli is not None:
155         home = os.path.join(os.path.realpath(home), appli,"USERS")
156         kwargs["with_username"]=True
157         pass
158     omniorb_config = generateFileName(home, prefix="omniORB",
159                                       extension="cfg",
160                                       hidden=True,
161                                       with_hostname=True,
162                                       with_port=port,
163                                       **kwargs)
164     os.environ['OMNIORB_CONFIG'] = omniorb_config
165     os.environ['NSPORT'] = str(port)
166
167     # give the chance to the servers to shutdown properly
168     try:
169         import time
170         from omniORB import CORBA
171         from LifeCycleCORBA import LifeCycleCORBA
172         # shutdown all
173         orb = CORBA.ORB_init([''], CORBA.ORB_ID)
174         lcc = LifeCycleCORBA(orb)
175         lcc.shutdownServers()
176         # give some time to shutdown to complete
177         time.sleep(1)
178         # shutdown omniNames and notifd
179         if cleanup:
180             lcc.killOmniNames()
181             time.sleep(1)
182             pass
183         pass
184     except:
185         pass
186     pass
187
188 def killMyPort(port):
189     """
190     Kill SALOME session running on the specified port.
191     Parameters:
192     - port - port number
193     """
194     from salome_utils import getShortHostName, getHostName
195
196     # try to shutdown session nomally
197     import threading, time
198     threading.Thread(target=shutdownMyPort, args=(port,False)).start()
199     time.sleep(3) # wait a little, then kill processes (should be done if shutdown procedure hangs up)
200
201     # new-style dot-prefixed pidict file
202     filedict = getPiDict(port, hidden=True)
203     # provide compatibility with old-style pidict file (not dot-prefixed)
204     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
205     # provide compatibility with old-style pidict file (short hostname)
206     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True,  hostname=getShortHostName())
207     # provide compatibility with old-style pidict file (not dot-prefixed, short hostname)
208     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getShortHostName())
209     # provide compatibility with old-style pidict file (long hostname)
210     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True,  hostname=getHostName())
211     # provide compatibility with old-style pidict file (not dot-prefixed, long hostname)
212     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getHostName())
213     #
214     try:
215         fpid = open(filedict, 'r')
216         #
217         from salome_utils import generateFileName
218         if sys.platform == "win32":
219             username = os.getenv( "USERNAME" )
220         else:
221             username = os.getenv('USER')
222         path = os.path.join('/tmp/logs', username)
223         fpidomniNames = generateFileName(path,
224                                          prefix="",
225                                          suffix="Pid_omniNames",
226                                          extension="log",
227                                          with_port=port)
228         if not sys.platform == 'win32':
229             cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start %s"` ; echo $pid > %s' % ( str(port), fpidomniNames )
230             a = os.system(cmd)
231             pass
232         try:
233             fpidomniNamesFile = open(fpidomniNames)
234             lines = fpidomniNamesFile.readlines()
235             fpidomniNamesFile.close()
236             os.remove(fpidomniNames)
237             for l in lines:
238                 try:
239                     pidfield = l.split()[0] # pid should be at the first position
240                     if sys.platform == "win32":
241                         import win32pm
242                         if verbose(): print 'stop process '+pidfield+' : omniNames'
243                         win32pm.killpid(int(pidfield),0)
244                     else:
245                         if verbose(): print 'stop process '+pidfield+' : omniNames'
246                         os.kill(int(pidfield),signal.SIGKILL)
247                         pass
248                     pass
249                 except:
250                     pass
251                 pass
252             pass
253         except:
254             pass
255         #
256         try:
257             process_ids=pickle.load(fpid)
258             fpid.close()
259             for process_id in process_ids:
260                 for pid, cmd in process_id.items():
261                     if verbose(): print "stop process %s : %s"% (pid, cmd[0])
262                     try:
263                         if sys.platform == "win32":
264                             import win32pm
265                             win32pm.killpid(int(pid),0)
266                         else:
267                             os.kill(int(pid),signal.SIGKILL)
268                             pass
269                         pass
270                     except:
271                         if verbose(): print "  ------------------ process %s : %s not found"% (pid, cmd[0])
272                         pass
273                     pass # for pid, cmd ...
274                 pass # for process_id ...
275             pass # try...
276         except:
277             pass
278         #
279         os.remove(filedict)
280         cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
281         pid = commands.getoutput(cmd)
282         a = ""
283         while pid and len(a.split()) < 2:
284             a = commands.getoutput("kill -9 " + pid)
285             pid = commands.getoutput(cmd)
286             #print pid
287             pass
288         pass
289     except:
290         print "Cannot find or open SALOME PIDs file for port", port
291         pass
292     #
293     appliCleanOmniOrbConfig(port)
294     pass
295
296 def killNotifdAndClean(port):
297     """
298     Kill notifd daemon and clean application running on the specified port.
299     Parameters:
300     - port - port number
301     """
302     try:
303       filedict=getPiDict(port)
304       f=open(filedict, 'r')
305       pids=pickle.load(f)
306       for d in pids:
307         for pid,process in d.items():
308           if 'notifd' in process:
309             cmd='kill -9 %d'% pid
310             os.system(cmd)
311       os.remove(filedict)
312     except:
313       #import traceback
314       #traceback.print_exc()
315       pass
316
317     appliCleanOmniOrbConfig(port)
318
319 def killMyPortSpy(pid, port):
320     dt = 1.0
321     while 1:
322         if sys.platform == "win32":
323             from win32pm import killpid
324             if killpid(int(pid), 0) != 0:
325                 return
326         else:
327             from os import kill
328             try:
329                 kill(int(pid), 0)
330             except OSError, e:
331                 if e.errno != 3:
332                     return
333                 break
334             pass
335         from time import sleep
336         sleep(dt)
337         pass
338     filedict = getPiDict(port, hidden=True)
339     if not os.path.exists(filedict):
340         return
341     try:
342         import omniORB
343         orb = omniORB.CORBA.ORB_init(sys.argv, omniORB.CORBA.ORB_ID)
344         import SALOME_NamingServicePy
345         ns = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb)
346         import SALOME
347         session = ns.Resolve("/Kernel/Session")
348         assert session
349     except:
350         return
351     try:
352         status = session.GetStatSession()
353     except:
354         # -- session is in naming service but has crash
355         status = None
356         pass
357     if status:
358         if not status.activeGUI:
359             return
360         pass
361     killMyPort(port)
362     return
363
364 if __name__ == "__main__":
365     if len(sys.argv) < 2:
366         print "Usage: "
367         print "  %s <port>" % os.path.basename(sys.argv[0])
368         print
369         print "Kills SALOME session running on specified <port>."
370         sys.exit(1)
371         pass
372     if sys.argv[1] == "--spy":
373         if len(sys.argv) > 3:
374             pid = sys.argv[2]
375             port = sys.argv[3]
376             killMyPortSpy(pid, port)
377             pass
378         sys.exit(0)
379         pass
380     for port in sys.argv[1:]:
381         killMyPort(port)
382         pass
383     pass