Salome HOME
Python container is obsolete
[modules/yacs.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 salome_utils
37
38 def getPiDict(port,appname='salome',full=True,hidden=True,hostname=None):
39     """
40     Get file with list of SALOME processes.
41     This file is located in the user's home directory
42     and named .<user>_<host>_<port>_SALOME_pidict
43     where
44     <user> is user name
45     <host> is host name
46     <port> is port number
47
48     Parameters:
49     - port    : port number
50     - appname : application name (default is 'SALOME')
51     - full    : if True, full path to the file is returned, otherwise only file name is returned
52     - hidden  : if True, file name is prefixed with . (dot) symbol; this internal parameter is used
53     to support compatibility with older versions of SALOME
54     """
55     from salome_utils import generateFileName, getTmpDir
56     dir = ""
57     if not hostname:
58         hostname = os.getenv("NSHOST")
59         if hostname: hostname = hostname.split(".")[0]
60         pass
61     if full:
62         # full path to the pidict file is requested
63         if hidden:
64             # new-style dot-prefixed pidict files
65             # are in the system-dependant temporary diretory
66             dir = getTmpDir()
67         else:
68             # old-style non-dot-prefixed pidict files
69             # are in the user's home directory
70             dir = os.getenv("HOME")
71             pass
72         pass
73     return generateFileName(dir,
74                             suffix="pidict",
75                             hidden=hidden,
76                             with_username=True,
77                             with_hostname=hostname or True,
78                             with_port=port,
79                             with_app=appname.upper())
80
81 def appliCleanOmniOrbConfig(port):
82     """
83     Remove omniorb config files related to the port in SALOME application:
84     - ${OMNIORB_USER_PATH}/.omniORB_${USER}_${HOSTNAME}_${NSPORT}.cfg
85     - ${OMNIORB_USER_PATH}/.omniORB_${USER}_last.cfg
86     the last is removed only if the link points to the first file.
87     """
88     from salome_utils import generateFileName, getUserName
89     omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
90     if omniorbUserPath is None:
91         #Run outside application context
92         pass
93     else:
94         omniorb_config      = generateFileName(omniorbUserPath, prefix="omniORB",
95                                                extension="cfg",
96                                                hidden=True,
97                                                with_username=True,
98                                                with_hostname=True,
99                                                with_port=port)
100         last_running_config = generateFileName(omniorbUserPath, prefix="omniORB",
101                                                with_username=True,
102                                                suffix="last",
103                                                extension="cfg",
104                                                hidden=True)
105         if os.access(last_running_config,os.F_OK):
106             if not sys.platform == 'win32':
107                 pointedPath = os.readlink(last_running_config)
108                 if pointedPath[0] != '/':
109                     pointedPath=os.path.join(os.path.dirname(last_running_config), pointedPath)
110                     pass
111                 if pointedPath == omniorb_config:
112                     os.unlink(last_running_config)
113                     pass
114                 pass
115             else:
116                 os.remove(last_running_config)  
117                 pass
118             pass  
119
120         if os.access(omniorb_config,os.F_OK):
121             os.remove(omniorb_config)
122             pass
123
124         if os.path.lexists(last_running_config):return
125
126         #try to relink last.cfg to an existing config file if any
127         files = glob.glob(os.path.join(omniorbUserPath,".omniORB_"+getUserName()+"_*.cfg"))
128         current_config=None
129         current=0
130         for f in files:
131           stat=os.stat(f)
132           if stat.st_atime > current:
133             current=stat.st_atime
134             current_config=f
135         if current_config:
136           if sys.platform == "win32":
137             import shutil
138             shutil.copyfile(os.path.normpath(current_config), last_running_config)
139             pass
140           else:
141             os.symlink(os.path.normpath(current_config), last_running_config)
142             pass
143           pass
144         pass
145     pass
146
147 ########## kills all salome processes with the given port ##########
148
149 def shutdownMyPort(port, cleanup=True):
150     """
151     Shutdown SALOME session running on the specified port.
152     Parameters:
153     - port - port number
154     """
155     if not port: return
156
157     try:
158         from PortManager import releasePort
159         releasePort(port)
160     except ImportError:
161         pass
162
163     from salome_utils import generateFileName
164
165     # set OMNIORB_CONFIG variable to the proper file
166     omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
167     kwargs = {}
168     if omniorbUserPath is not None:
169         kwargs["with_username"]=True
170     else:
171         omniorbUserPath = os.path.realpath(os.path.expanduser('~'))
172     omniorb_config = generateFileName(omniorbUserPath, prefix="omniORB",
173                                       extension="cfg",
174                                       hidden=True,
175                                       with_hostname=True,
176                                       with_port=port,
177                                       **kwargs)
178     os.environ['OMNIORB_CONFIG'] = omniorb_config
179     os.environ['NSPORT'] = str(port)
180
181     # give the chance to the servers to shutdown properly
182     try:
183         import time
184         from omniORB import CORBA
185         from LifeCycleCORBA import LifeCycleCORBA
186         # shutdown all
187         orb = CORBA.ORB_init([''], CORBA.ORB_ID)
188         lcc = LifeCycleCORBA(orb)
189         lcc.shutdownServers()
190         # give some time to shutdown to complete
191         time.sleep(1)
192         # shutdown omniNames and notifd
193         if cleanup:
194             lcc.killOmniNames()
195             time.sleep(1)
196             pass
197         pass
198     except:
199         pass
200     pass
201
202 def killMyPort(port):
203     """
204     Kill SALOME session running on the specified port.
205     Parameters:
206     - port - port number
207     """
208     try:
209         import PortManager
210         PortManager.releasePort(port)
211     except ImportError:
212         pass
213
214     from salome_utils import getShortHostName, getHostName
215
216     # try to shutdown session nomally
217     import threading, time
218     threading.Thread(target=shutdownMyPort, args=(port,False)).start()
219     time.sleep(3) # wait a little, then kill processes (should be done if shutdown procedure hangs up)
220
221     # new-style dot-prefixed pidict file
222     filedict = getPiDict(port, hidden=True)
223     # provide compatibility with old-style pidict file (not dot-prefixed)
224     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
225     # provide compatibility with old-style pidict file (short hostname)
226     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True,  hostname=getShortHostName())
227     # provide compatibility with old-style pidict file (not dot-prefixed, short hostname)
228     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getShortHostName())
229     # provide compatibility with old-style pidict file (long hostname)
230     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True,  hostname=getHostName())
231     # provide compatibility with old-style pidict file (not dot-prefixed, long hostname)
232     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getHostName())
233     #
234     try:
235         fpid = open(filedict, 'r')
236         #
237         from salome_utils import generateFileName
238         if sys.platform == "win32":
239             username = os.getenv( "USERNAME" )
240         else:
241             username = os.getenv('USER')
242         path = os.path.join('/tmp/logs', username)
243         fpidomniNames = generateFileName(path,
244                                          prefix="",
245                                          suffix="Pid_omniNames",
246                                          extension="log",
247                                          with_port=port)
248         if not sys.platform == 'win32':
249             cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start %s"` ; echo $pid > %s' % ( str(port), fpidomniNames )
250             a = os.system(cmd)
251             pass
252         try:
253             fpidomniNamesFile = open(fpidomniNames)
254             lines = fpidomniNamesFile.readlines()
255             fpidomniNamesFile.close()
256             os.remove(fpidomniNames)
257             for l in lines:
258                 try:
259                     pidfield = l.split()[0] # pid should be at the first position
260                     if sys.platform == "win32":
261                         import win32pm
262                         if verbose(): print 'stop process '+pidfield+' : omniNames'
263                         win32pm.killpid(int(pidfield),0)
264                     else:
265                         if verbose(): print 'stop process '+pidfield+' : omniNames'
266                         os.kill(int(pidfield),signal.SIGKILL)
267                         pass
268                     pass
269                 except:
270                     pass
271                 pass
272             pass
273         except:
274             pass
275         #
276         try:
277             process_ids=pickle.load(fpid)
278             fpid.close()
279             for process_id in process_ids:
280                 for pid, cmd in process_id.items():
281                     if verbose(): print "stop process %s : %s"% (pid, cmd[0])
282                     try:
283                         if sys.platform == "win32":
284                             import win32pm
285                             win32pm.killpid(int(pid),0)
286                         else:
287                             os.kill(int(pid),signal.SIGKILL)
288                             pass
289                         pass
290                     except:
291                         if verbose(): print "  ------------------ process %s : %s not found"% (pid, cmd[0])
292                         pass
293                     pass # for pid, cmd ...
294                 pass # for process_id ...
295             pass # try...
296         except:
297             pass
298         #
299         os.remove(filedict)
300         cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
301         pid = commands.getoutput(cmd)
302         a = ""
303         while pid and len(a.split()) < 2:
304             a = commands.getoutput("kill -9 " + pid)
305             pid = commands.getoutput(cmd)
306             #print pid
307             pass
308         pass
309     except:
310         print "Cannot find or open SALOME PIDs file for port", port
311         pass
312     #
313     appliCleanOmniOrbConfig(port)
314     pass
315
316 def killNotifdAndClean(port):
317     """
318     Kill notifd daemon and clean application running on the specified port.
319     Parameters:
320     - port - port number
321     """
322     try:
323       filedict=getPiDict(port)
324       f=open(filedict, 'r')
325       pids=pickle.load(f)
326       for d in pids:
327         for pid,process in d.items():
328           if 'notifd' in process:
329             cmd='kill -9 %d'% pid
330             os.system(cmd)
331       os.remove(filedict)
332     except:
333       #import traceback
334       #traceback.print_exc()
335       pass
336
337     appliCleanOmniOrbConfig(port)
338
339 def killMyPortSpy(pid, port):
340     dt = 1.0
341     while 1:
342         if sys.platform == "win32":
343             from win32pm import killpid
344             if killpid(int(pid), 0) != 0:
345                 return
346         else:
347             from os import kill
348             try:
349                 kill(int(pid), 0)
350             except OSError, e:
351                 if e.errno != 3:
352                     return
353                 break
354             pass
355         from time import sleep
356         sleep(dt)
357         pass
358     filedict = getPiDict(port, hidden=True)
359     if not os.path.exists(filedict):
360         return
361     try:
362         import omniORB
363         orb = omniORB.CORBA.ORB_init(sys.argv, omniORB.CORBA.ORB_ID)
364         import SALOME_NamingServicePy
365         ns = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb)
366         import SALOME
367         session = ns.Resolve("/Kernel/Session")
368         assert session
369     except:
370         return
371     try:
372         status = session.GetStatSession()
373     except:
374         # -- session is in naming service but has crash
375         status = None
376         pass
377     if status:
378         if not status.activeGUI:
379             return
380         pass
381     killMyPort(port)
382     return
383
384 if __name__ == "__main__":
385     if len(sys.argv) < 2:
386         print "Usage: "
387         print "  %s <port>" % os.path.basename(sys.argv[0])
388         print
389         print "Kills SALOME session running on specified <port>."
390         sys.exit(1)
391         pass
392     if sys.argv[1] == "--spy":
393         if len(sys.argv) > 3:
394             pid = sys.argv[2]
395             port = sys.argv[3]
396             killMyPortSpy(pid, port)
397             pass
398         sys.exit(0)
399         pass
400     for port in sys.argv[1:]:
401         killMyPort(port)
402         pass
403     pass