Salome HOME
Fix regression: killSalomeWithPort does not kill sessions of the old versions of...
[modules/kernel.git] / bin / killSalomeWithPort.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 #  Copyright (C) 2007-2010  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 launchConfigureParser 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, getHostName
57     if not hostname:
58         hostname = os.getenv("NSHOST") or getHostName()
59     if full:
60         # full path to the pidict file is requested
61         if hidden:
62             # new-style dot-prefixed pidict files
63             # are in the system-dependant temporary diretory
64             dir = getTmpDir()
65         else:
66             # old-style non-dot-prefixed pidict files
67             # are in the user's home directory
68             dir = os.getenv("HOME")
69             pass
70         pass
71     return generateFileName(dir,
72                             suffix="pidict",
73                             hidden=hidden,
74                             with_username=True,
75                             with_hostname=hostname,
76                             with_port=port,
77                             with_app=appname.upper())
78
79 def appliCleanOmniOrbConfig(port):
80     """
81     Remove omniorb config files related to the port in SALOME application:
82     - ${HOME}/${APPLI}/USERS/.omniORB_${USER}_${HOSTNAME}_${NSPORT}.cfg
83     - ${HOME}/${APPLI}/USERS/.omniORB_${USER}_last.cfg
84     the last is removed only if the link points to the first file.
85     """
86     from salome_utils import generateFileName
87     home  = os.getenv("HOME")
88     appli = os.getenv("APPLI")
89     if appli is None:
90         #Run outside application context
91         pass
92     else:
93         dir = os.path.join(home, appli,"USERS")
94         omniorb_config      = generateFileName(dir, 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(dir, 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             pointedPath = os.readlink(last_running_config)
107             if pointedPath[0] != '/':
108                 pointedPath=os.path.join(os.path.dirname(last_running_config), pointedPath)
109             if pointedPath == omniorb_config:
110                 os.unlink(last_running_config)
111                 pass
112             pass
113         if os.access(omniorb_config,os.F_OK):
114             os.remove(omniorb_config)
115             pass
116
117         if os.path.lexists(last_running_config):return 
118
119         #try to relink last.cfg to an existing config file if any
120         files = glob.glob(os.path.join(os.environ["HOME"],Utils_Identity.getapplipath(),
121                                        "USERS",".omniORB_"+salome_utils.getUserName()+"_*.cfg"))
122         current_config=None
123         current=0
124         for f in files:
125           stat=os.stat(f)
126           if stat.st_atime > current:
127             current=stat.st_atime
128             current_config=f
129         if current_config:
130           os.symlink(os.path.normpath(current_config), last_running_config)
131
132         pass
133     pass
134
135 ########## kills all salome processes with the given port ##########
136
137 def killMyPort(port):
138     """
139     Kill SALOME session running on the specified port.
140     Parameters:
141     - port - port number
142     """
143     from salome_utils import getShortHostName
144     # new-style dot-prefixed pidict file
145     filedict = getPiDict(port, hidden=True)
146     # provide compatibility with old-style pidict file (not dot-prefixed)
147     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
148     # provide compatibility with old-style pidict file (shost hostname)
149     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True,  hostname=getShortHostName())
150     # provide compatibility with old-style pidict file (not dot-prefixed, shost hostname)
151     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getShortHostName())
152     #
153     try:
154         fpid = open(filedict, 'r')
155         #
156         from salome_utils import generateFileName
157         if sys.platform == "win32":
158             username = os.getenv( "USERNAME" )
159         else:
160             username = os.getenv('USER')
161         path = os.path.join('/tmp/logs', username)
162         fpidomniNames = generateFileName(path,
163                                          prefix="",
164                                          suffix="Pid_omniNames",
165                                          extension="log",
166                                          with_port=port)
167         if not sys.platform == 'win32':        
168             cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start %s"` ; echo $pid > %s' % ( str(port), fpidomniNames )
169             a = os.system(cmd)
170             pass
171         try:
172             fpidomniNamesFile = open(fpidomniNames)
173             lines = fpidomniNamesFile.readlines()
174             fpidomniNamesFile.close()
175             os.remove(fpidomniNames)
176             for l in lines:
177                 try:
178                     pidfield = l.split()[0] # pid should be at the first position
179                     if sys.platform == "win32":
180                         import win32pm
181                         if verbose(): print 'stop process '+pidfield+' : omniNames'
182                         win32pm.killpid(int(pidfield),0)
183                     else:
184                         if verbose(): print 'stop process '+pidfield+' : omniNames'
185                         os.kill(int(pidfield),signal.SIGKILL)
186                         pass
187                     pass
188                 except:
189                     pass
190                 pass
191             pass
192         except:
193             pass
194         #
195         try:
196             process_ids=pickle.load(fpid)
197             fpid.close()
198             for process_id in process_ids:
199                 for pid, cmd in process_id.items():
200                     if verbose(): print "stop process %s : %s"% (pid, cmd[0])
201                     try:
202                         if sys.platform == "win32":
203                             import win32pm
204                             win32pm.killpid(int(pid),0)
205                         else:
206                             os.kill(int(pid),signal.SIGKILL)
207                             pass
208                         pass
209                     except:
210                         if verbose(): print "  ------------------ process %s : %s not found"% (pid, cmd[0])
211                         pass
212                     pass # for pid, cmd ...
213                 pass # for process_id ...
214             pass # try...
215         except:
216             pass
217         #
218         os.remove(filedict)
219         cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
220         pid = commands.getoutput(cmd)
221         a = ""
222         while pid and len(a.split()) < 2:
223             a = commands.getoutput("kill -9 " + pid)
224             pid = commands.getoutput(cmd)
225             #print pid
226             pass
227         pass
228     except:
229         print "Cannot find or open SALOME PIDs file for port", port
230         pass
231     #
232     appliCleanOmniOrbConfig(port)
233     pass
234             
235 def killNotifdAndClean(port):
236     """
237     Kill notifd daemon and clean application running on the specified port.
238     Parameters:
239     - port - port number
240     """
241     try:
242       filedict=getPiDict(port)
243       f=open(filedict, 'r')
244       pids=pickle.load(f)
245       for d in pids:
246         for pid,process in d.items():
247           if 'notifd' in process:
248             cmd='kill -9 %d'% pid
249             os.system(cmd)
250       os.remove(filedict)
251     except:
252       #import traceback
253       #traceback.print_exc()
254       pass
255
256     appliCleanOmniOrbConfig(port)
257     
258 if __name__ == "__main__":
259     for port in sys.argv[1:]:
260         killMyPort(port)
261         pass
262     pass