2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 ## \file killSalomeWithPort.py
26 # Stop all %SALOME servers from given sessions by killing them
28 # The sessions are indicated by their ports on the command line as in :
30 # killSalomeWithPort.py 2811 2815
34 import os, sys, pickle, signal, commands,glob
35 from launchConfigureParser import verbose
39 def getPiDict(port,appname='salome',full=True,hidden=True,hostname=None):
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
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
56 from salome_utils import generateFileName, getTmpDir
59 hostname = os.getenv("NSHOST")
60 if hostname: hostname = hostname.split(".")[0]
63 # full path to the pidict file is requested
65 # new-style dot-prefixed pidict files
66 # are in the system-dependant temporary diretory
69 # old-style non-dot-prefixed pidict files
70 # are in the user's home directory
71 dir = os.getenv("HOME")
74 return generateFileName(dir,
78 with_hostname=hostname or True,
80 with_app=appname.upper())
82 def appliCleanOmniOrbConfig(port):
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.
89 from salome_utils import generateFileName
90 home = os.getenv("HOME")
91 appli = os.getenv("APPLI")
93 #Run outside application context
96 dir = os.path.join(home, appli,"USERS")
97 omniorb_config = generateFileName(dir, prefix="omniORB",
103 last_running_config = generateFileName(dir, prefix="omniORB",
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)
116 if os.access(omniorb_config,os.F_OK):
117 os.remove(omniorb_config)
120 if os.path.lexists(last_running_config):return
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_"+salome_utils.getUserName()+"_*.cfg"))
129 if stat.st_atime > current:
130 current=stat.st_atime
133 os.symlink(os.path.normpath(current_config), last_running_config)
138 ########## kills all salome processes with the given port ##########
140 def killMyPort(port):
142 Kill SALOME session running on the specified port.
146 from salome_utils import getShortHostName, getHostName
147 # new-style dot-prefixed pidict file
148 filedict = getPiDict(port, hidden=True)
149 # provide compatibility with old-style pidict file (not dot-prefixed)
150 if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
151 # provide compatibility with old-style pidict file (short hostname)
152 if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True, hostname=getShortHostName())
153 # provide compatibility with old-style pidict file (not dot-prefixed, short hostname)
154 if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getShortHostName())
155 # provide compatibility with old-style pidict file (long hostname)
156 if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True, hostname=getHostName())
157 # provide compatibility with old-style pidict file (not dot-prefixed, long hostname)
158 if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getHostName())
161 fpid = open(filedict, 'r')
163 from salome_utils import generateFileName
164 if sys.platform == "win32":
165 username = os.getenv( "USERNAME" )
167 username = os.getenv('USER')
168 path = os.path.join('/tmp/logs', username)
169 fpidomniNames = generateFileName(path,
171 suffix="Pid_omniNames",
174 if not sys.platform == 'win32':
175 cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start %s"` ; echo $pid > %s' % ( str(port), fpidomniNames )
179 fpidomniNamesFile = open(fpidomniNames)
180 lines = fpidomniNamesFile.readlines()
181 fpidomniNamesFile.close()
182 os.remove(fpidomniNames)
185 pidfield = l.split()[0] # pid should be at the first position
186 if sys.platform == "win32":
188 if verbose(): print 'stop process '+pidfield+' : omniNames'
189 win32pm.killpid(int(pidfield),0)
191 if verbose(): print 'stop process '+pidfield+' : omniNames'
192 os.kill(int(pidfield),signal.SIGKILL)
203 process_ids=pickle.load(fpid)
205 for process_id in process_ids:
206 for pid, cmd in process_id.items():
207 if verbose(): print "stop process %s : %s"% (pid, cmd[0])
209 if sys.platform == "win32":
211 win32pm.killpid(int(pid),0)
213 os.kill(int(pid),signal.SIGKILL)
217 if verbose(): print " ------------------ process %s : %s not found"% (pid, cmd[0])
219 pass # for pid, cmd ...
220 pass # for process_id ...
226 cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
227 pid = commands.getoutput(cmd)
229 while pid and len(a.split()) < 2:
230 a = commands.getoutput("kill -9 " + pid)
231 pid = commands.getoutput(cmd)
236 print "Cannot find or open SALOME PIDs file for port", port
239 appliCleanOmniOrbConfig(port)
242 def killNotifdAndClean(port):
244 Kill notifd daemon and clean application running on the specified port.
249 filedict=getPiDict(port)
250 f=open(filedict, 'r')
253 for pid,process in d.items():
254 if 'notifd' in process:
255 cmd='kill -9 %d'% pid
260 #traceback.print_exc()
263 appliCleanOmniOrbConfig(port)
265 def killMyPortSpy(pid, port):
268 if sys.platform == "win32":
269 from win32pm import killpid
270 if killpid(int(pid), 0) != 0:
281 from time import sleep
284 filedict = getPiDict(port, hidden=True)
285 if not os.path.exists(filedict):
289 orb = omniORB.CORBA.ORB_init(sys.argv, omniORB.CORBA.ORB_ID)
290 import SALOME_NamingServicePy
291 ns = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb)
293 session = ns.Resolve("/Kernel/Session")
298 status = session.GetStatSession()
300 # -- session is in naming service but has crash
304 if not status.activeGUI:
310 if __name__ == "__main__":
311 if sys.argv[1] == "--spy":
314 killMyPortSpy(pid, port)
317 for port in sys.argv[1:]: