Salome HOME
Improve killSalomeWithPort script: before killing the servers with kill command,...
[modules/kernel.git] / bin / killSalomeWithPort.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2011  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
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     - ${HOME}/${APPLI}/USERS/.omniORB_${USER}_${HOSTNAME}_${NSPORT}.cfg
85     - ${HOME}/${APPLI}/USERS/.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     home  = os.getenv("HOME")
90     appli = os.getenv("APPLI")
91     if appli is None:
92         #Run outside application context
93         pass
94     else:
95         dir = os.path.join(home, appli,"USERS")
96         omniorb_config      = generateFileName(dir, prefix="omniORB",
97                                                extension="cfg",
98                                                hidden=True,
99                                                with_username=True,
100                                                with_hostname=True,
101                                                with_port=port)
102         last_running_config = generateFileName(dir, prefix="omniORB",
103                                                with_username=True,
104                                                suffix="last",
105                                                extension="cfg",
106                                                hidden=True)
107         if os.access(last_running_config,os.F_OK):
108             pointedPath = os.readlink(last_running_config)
109             if pointedPath[0] != '/':
110                 pointedPath=os.path.join(os.path.dirname(last_running_config), pointedPath)
111             if pointedPath == omniorb_config:
112                 os.unlink(last_running_config)
113                 pass
114             pass
115         if os.access(omniorb_config,os.F_OK):
116             os.remove(omniorb_config)
117             pass
118
119         if os.path.lexists(last_running_config):return 
120
121         #try to relink last.cfg to an existing config file if any
122         files = glob.glob(os.path.join(os.environ["HOME"],Utils_Identity.getapplipath(),
123                                        "USERS",".omniORB_"+getUserName()+"_*.cfg"))
124         current_config=None
125         current=0
126         for f in files:
127           stat=os.stat(f)
128           if stat.st_atime > current:
129             current=stat.st_atime
130             current_config=f
131         if current_config:
132           os.symlink(os.path.normpath(current_config), last_running_config)
133
134         pass
135     pass
136
137 ########## kills all salome processes with the given port ##########
138
139 def shutdownMyPort(port):
140     """
141     Shutdown SALOME session running on the specified port.
142     Parameters:
143     - port - port number
144     """
145     from salome_utils import generateFileName
146
147     # set OMNIORB_CONFIG variable to the proper file
148     home  = os.getenv("HOME")
149     appli = os.getenv("APPLI")
150     kwargs = {}
151     if appli is not None: 
152         home = os.path.join(home, appli,"USERS")
153         kwargs["with_username"]=True
154         pass
155     omniorb_config = generateFileName(home, prefix="omniORB",
156                                       extension="cfg",
157                                       hidden=True,
158                                       with_hostname=True,
159                                       with_port=port,
160                                       **kwargs)
161     os.environ['OMNIORB_CONFIG'] = omniorb_config
162
163     # give the chance to the servers to shutdown properly
164     try:
165         import time
166         import salome_kernel
167         orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
168         # shutdown all
169         lcc.shutdownServers()
170         # give some time to shutdown to complete
171         time.sleep(1)
172         # shutdown omniNames and notifd
173         salome_kernel.LifeCycleCORBA.killOmniNames()
174     except:
175         pass
176     pass
177     
178 def killMyPort(port):
179     """
180     Kill SALOME session running on the specified port.
181     Parameters:
182     - port - port number
183     """
184     from salome_utils import getShortHostName, getHostName
185     
186     # try to shutdown session nomally
187     import threading, time
188     threading.Thread(target=shutdownMyPort, args=(port,)).start()
189     time.sleep(3) # wait a little, then kill processes (should be done if shutdown procedure hangs up)
190     
191     # new-style dot-prefixed pidict file
192     filedict = getPiDict(port, hidden=True)
193     # provide compatibility with old-style pidict file (not dot-prefixed)
194     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
195     # provide compatibility with old-style pidict file (short hostname)
196     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True,  hostname=getShortHostName())
197     # provide compatibility with old-style pidict file (not dot-prefixed, short hostname)
198     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getShortHostName())
199     # provide compatibility with old-style pidict file (long hostname)
200     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True,  hostname=getHostName())
201     # provide compatibility with old-style pidict file (not dot-prefixed, long hostname)
202     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getHostName())
203     #
204     try:
205         fpid = open(filedict, 'r')
206         #
207         from salome_utils import generateFileName
208         if sys.platform == "win32":
209             username = os.getenv( "USERNAME" )
210         else:
211             username = os.getenv('USER')
212         path = os.path.join('/tmp/logs', username)
213         fpidomniNames = generateFileName(path,
214                                          prefix="",
215                                          suffix="Pid_omniNames",
216                                          extension="log",
217                                          with_port=port)
218         if not sys.platform == 'win32':        
219             cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start %s"` ; echo $pid > %s' % ( str(port), fpidomniNames )
220             a = os.system(cmd)
221             pass
222         try:
223             fpidomniNamesFile = open(fpidomniNames)
224             lines = fpidomniNamesFile.readlines()
225             fpidomniNamesFile.close()
226             os.remove(fpidomniNames)
227             for l in lines:
228                 try:
229                     pidfield = l.split()[0] # pid should be at the first position
230                     if sys.platform == "win32":
231                         import win32pm
232                         if verbose(): print 'stop process '+pidfield+' : omniNames'
233                         win32pm.killpid(int(pidfield),0)
234                     else:
235                         if verbose(): print 'stop process '+pidfield+' : omniNames'
236                         os.kill(int(pidfield),signal.SIGKILL)
237                         pass
238                     pass
239                 except:
240                     pass
241                 pass
242             pass
243         except:
244             pass
245         #
246         try:
247             process_ids=pickle.load(fpid)
248             fpid.close()
249             for process_id in process_ids:
250                 for pid, cmd in process_id.items():
251                     if verbose(): print "stop process %s : %s"% (pid, cmd[0])
252                     try:
253                         if sys.platform == "win32":
254                             import win32pm
255                             win32pm.killpid(int(pid),0)                            
256                         else:
257                             os.kill(int(pid),signal.SIGKILL)
258                             pass
259                         pass
260                     except:
261                         if verbose(): print "  ------------------ process %s : %s not found"% (pid, cmd[0])
262                         pass
263                     pass # for pid, cmd ...
264                 pass # for process_id ...
265             pass # try...
266         except:
267             pass
268         #
269         os.remove(filedict)
270         cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
271         pid = commands.getoutput(cmd)
272         a = ""
273         while pid and len(a.split()) < 2:
274             a = commands.getoutput("kill -9 " + pid)
275             pid = commands.getoutput(cmd)
276             #print pid
277             pass
278         pass
279     except:
280         print "Cannot find or open SALOME PIDs file for port", port
281         pass
282     #
283     appliCleanOmniOrbConfig(port)
284     pass
285             
286 def killNotifdAndClean(port):
287     """
288     Kill notifd daemon and clean application running on the specified port.
289     Parameters:
290     - port - port number
291     """
292     try:
293       filedict=getPiDict(port)
294       f=open(filedict, 'r')
295       pids=pickle.load(f)
296       for d in pids:
297         for pid,process in d.items():
298           if 'notifd' in process:
299             cmd='kill -9 %d'% pid
300             os.system(cmd)
301       os.remove(filedict)
302     except:
303       #import traceback
304       #traceback.print_exc()
305       pass
306
307     appliCleanOmniOrbConfig(port)
308
309 def killMyPortSpy(pid, port):
310     dt = 1.0
311     while 1:
312         if sys.platform == "win32":
313             from win32pm import killpid
314             if killpid(int(pid), 0) != 0:
315                 return
316         else:
317             from os import kill
318             try:
319                 kill(int(pid), 0)
320             except OSError, e:
321                 if e.errno != 3:
322                     return
323                 break
324             pass
325         from time import sleep
326         sleep(dt)
327         pass
328     filedict = getPiDict(port, hidden=True)
329     if not os.path.exists(filedict):
330         return
331     try:
332         import omniORB
333         orb = omniORB.CORBA.ORB_init(sys.argv, omniORB.CORBA.ORB_ID)
334         import SALOME_NamingServicePy
335         ns = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb)
336         import SALOME
337         session = ns.Resolve("/Kernel/Session")
338         assert session
339     except:
340         return
341     try:
342         status = session.GetStatSession()
343     except:
344         # -- session is in naming service but has crash
345         status = None
346         pass
347     if status:
348         if not status.activeGUI:
349             return
350         pass
351     killMyPort(port)
352     return
353
354 if __name__ == "__main__":
355     if sys.argv[1] == "--spy":
356         pid = sys.argv[2]
357         port = sys.argv[3]
358         killMyPortSpy(pid, port)
359         sys.exit(0)
360         pass
361     for port in sys.argv[1:]:
362         killMyPort(port)
363         pass
364     pass