Back to index

salome-kernel  6.5.0
killSalomeWithPort.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00006 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00007 #
00008 # This library is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU Lesser General Public
00010 # License as published by the Free Software Foundation; either
00011 # version 2.1 of the License.
00012 #
00013 # This library is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 # Lesser General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU Lesser General Public
00019 # License along with this library; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00021 #
00022 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00023 #
00024 
00025 ## \file killSalomeWithPort.py
00026 #  Stop all %SALOME servers from given sessions by killing them
00027 #
00028 #  The sessions are indicated by their ports on the command line as in :
00029 #  \code
00030 #  killSalomeWithPort.py 2811 2815
00031 #  \endcode
00032 #
00033 
00034 import os, sys, pickle, signal, commands,glob
00035 from salome_utils import verbose
00036 import Utils_Identity
00037 import salome_utils
00038 
00039 def getPiDict(port,appname='salome',full=True,hidden=True,hostname=None):
00040     """
00041     Get file with list of SALOME processes.
00042     This file is located in the user's home directory
00043     and named .<user>_<host>_<port>_SALOME_pidict
00044     where
00045     <user> is user name
00046     <host> is host name
00047     <port> is port number
00048 
00049     Parameters:
00050     - port    : port number
00051     - appname : application name (default is 'SALOME')
00052     - full    : if True, full path to the file is returned, otherwise only file name is returned
00053     - hidden  : if True, file name is prefixed with . (dot) symbol; this internal parameter is used
00054     to support compatibility with older versions of SALOME
00055     """
00056     from salome_utils import generateFileName, getTmpDir
00057     dir = ""
00058     if not hostname:
00059         hostname = os.getenv("NSHOST")
00060         if hostname: hostname = hostname.split(".")[0]
00061         pass
00062     if full:
00063         # full path to the pidict file is requested
00064         if hidden:
00065             # new-style dot-prefixed pidict files
00066             # are in the system-dependant temporary diretory
00067             dir = getTmpDir()
00068         else:
00069             # old-style non-dot-prefixed pidict files
00070             # are in the user's home directory
00071             dir = os.getenv("HOME")
00072             pass
00073         pass
00074     return generateFileName(dir,
00075                             suffix="pidict",
00076                             hidden=hidden,
00077                             with_username=True,
00078                             with_hostname=hostname or True,
00079                             with_port=port,
00080                             with_app=appname.upper())
00081 
00082 def appliCleanOmniOrbConfig(port):
00083     """
00084     Remove omniorb config files related to the port in SALOME application:
00085     - ${HOME}/${APPLI}/USERS/.omniORB_${USER}_${HOSTNAME}_${NSPORT}.cfg
00086     - ${HOME}/${APPLI}/USERS/.omniORB_${USER}_last.cfg
00087     the last is removed only if the link points to the first file.
00088     """
00089     from salome_utils import generateFileName, getUserName
00090     home  = os.getenv("HOME")
00091     appli = os.getenv("APPLI")
00092     if appli is None:
00093         #Run outside application context
00094         pass
00095     else:
00096         dir = os.path.join(home, appli,"USERS")
00097         omniorb_config      = generateFileName(dir, prefix="omniORB",
00098                                                extension="cfg",
00099                                                hidden=True,
00100                                                with_username=True,
00101                                                with_hostname=True,
00102                                                with_port=port)
00103         last_running_config = generateFileName(dir, prefix="omniORB",
00104                                                with_username=True,
00105                                                suffix="last",
00106                                                extension="cfg",
00107                                                hidden=True)
00108         if os.access(last_running_config,os.F_OK):
00109             pointedPath = os.readlink(last_running_config)
00110             if pointedPath[0] != '/':
00111                 pointedPath=os.path.join(os.path.dirname(last_running_config), pointedPath)
00112             if pointedPath == omniorb_config:
00113                 os.unlink(last_running_config)
00114                 pass
00115             pass
00116         if os.access(omniorb_config,os.F_OK):
00117             os.remove(omniorb_config)
00118             pass
00119 
00120         if os.path.lexists(last_running_config):return 
00121 
00122         #try to relink last.cfg to an existing config file if any
00123         files = glob.glob(os.path.join(os.environ["HOME"],Utils_Identity.getapplipath(),
00124                                        "USERS",".omniORB_"+getUserName()+"_*.cfg"))
00125         current_config=None
00126         current=0
00127         for f in files:
00128           stat=os.stat(f)
00129           if stat.st_atime > current:
00130             current=stat.st_atime
00131             current_config=f
00132         if current_config:
00133           os.symlink(os.path.normpath(current_config), last_running_config)
00134 
00135         pass
00136     pass
00137 
00138 ########## kills all salome processes with the given port ##########
00139 
00140 def shutdownMyPort(port):
00141     """
00142     Shutdown SALOME session running on the specified port.
00143     Parameters:
00144     - port - port number
00145     """
00146     if not port: return
00147     
00148     from salome_utils import generateFileName
00149 
00150     # set OMNIORB_CONFIG variable to the proper file
00151     home  = os.getenv("HOME")
00152     appli = os.getenv("APPLI")
00153     kwargs = {}
00154     if appli is not None: 
00155         home = os.path.join(home, appli,"USERS")
00156         kwargs["with_username"]=True
00157         pass
00158     omniorb_config = generateFileName(home, prefix="omniORB",
00159                                       extension="cfg",
00160                                       hidden=True,
00161                                       with_hostname=True,
00162                                       with_port=port,
00163                                       **kwargs)
00164     os.environ['OMNIORB_CONFIG'] = omniorb_config
00165 
00166     # give the chance to the servers to shutdown properly
00167     try:
00168         import time
00169         import salome_kernel
00170         orb, lcc, naming_service, cm = salome_kernel.salome_kernel_init()
00171         # shutdown all
00172         lcc.shutdownServers()
00173         # give some time to shutdown to complete
00174         time.sleep(1)
00175         # shutdown omniNames and notifd
00176         salome_kernel.LifeCycleCORBA.killOmniNames()
00177     except:
00178         pass
00179     pass
00180     
00181 def killMyPort(port):
00182     """
00183     Kill SALOME session running on the specified port.
00184     Parameters:
00185     - port - port number
00186     """
00187     from salome_utils import getShortHostName, getHostName
00188     
00189     # try to shutdown session nomally
00190     import threading, time
00191     threading.Thread(target=shutdownMyPort, args=(port,)).start()
00192     time.sleep(3) # wait a little, then kill processes (should be done if shutdown procedure hangs up)
00193     
00194     # new-style dot-prefixed pidict file
00195     filedict = getPiDict(port, hidden=True)
00196     # provide compatibility with old-style pidict file (not dot-prefixed)
00197     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
00198     # provide compatibility with old-style pidict file (short hostname)
00199     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True,  hostname=getShortHostName())
00200     # provide compatibility with old-style pidict file (not dot-prefixed, short hostname)
00201     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getShortHostName())
00202     # provide compatibility with old-style pidict file (long hostname)
00203     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=True,  hostname=getHostName())
00204     # provide compatibility with old-style pidict file (not dot-prefixed, long hostname)
00205     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False, hostname=getHostName())
00206     #
00207     try:
00208         fpid = open(filedict, 'r')
00209         #
00210         from salome_utils import generateFileName
00211         if sys.platform == "win32":
00212             username = os.getenv( "USERNAME" )
00213         else:
00214             username = os.getenv('USER')
00215         path = os.path.join('/tmp/logs', username)
00216         fpidomniNames = generateFileName(path,
00217                                          prefix="",
00218                                          suffix="Pid_omniNames",
00219                                          extension="log",
00220                                          with_port=port)
00221         if not sys.platform == 'win32':        
00222             cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start %s"` ; echo $pid > %s' % ( str(port), fpidomniNames )
00223             a = os.system(cmd)
00224             pass
00225         try:
00226             fpidomniNamesFile = open(fpidomniNames)
00227             lines = fpidomniNamesFile.readlines()
00228             fpidomniNamesFile.close()
00229             os.remove(fpidomniNames)
00230             for l in lines:
00231                 try:
00232                     pidfield = l.split()[0] # pid should be at the first position
00233                     if sys.platform == "win32":
00234                         import win32pm
00235                         if verbose(): print 'stop process '+pidfield+' : omniNames'
00236                         win32pm.killpid(int(pidfield),0)
00237                     else:
00238                         if verbose(): print 'stop process '+pidfield+' : omniNames'
00239                         os.kill(int(pidfield),signal.SIGKILL)
00240                         pass
00241                     pass
00242                 except:
00243                     pass
00244                 pass
00245             pass
00246         except:
00247             pass
00248         #
00249         try:
00250             process_ids=pickle.load(fpid)
00251             fpid.close()
00252             for process_id in process_ids:
00253                 for pid, cmd in process_id.items():
00254                     if verbose(): print "stop process %s : %s"% (pid, cmd[0])
00255                     try:
00256                         if sys.platform == "win32":
00257                             import win32pm
00258                             win32pm.killpid(int(pid),0)                            
00259                         else:
00260                             os.kill(int(pid),signal.SIGKILL)
00261                             pass
00262                         pass
00263                     except:
00264                         if verbose(): print "  ------------------ process %s : %s not found"% (pid, cmd[0])
00265                         pass
00266                     pass # for pid, cmd ...
00267                 pass # for process_id ...
00268             pass # try...
00269         except:
00270             pass
00271         #
00272         os.remove(filedict)
00273         cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
00274         pid = commands.getoutput(cmd)
00275         a = ""
00276         while pid and len(a.split()) < 2:
00277             a = commands.getoutput("kill -9 " + pid)
00278             pid = commands.getoutput(cmd)
00279             #print pid
00280             pass
00281         pass
00282     except:
00283         print "Cannot find or open SALOME PIDs file for port", port
00284         pass
00285     #
00286     appliCleanOmniOrbConfig(port)
00287     pass
00288             
00289 def killNotifdAndClean(port):
00290     """
00291     Kill notifd daemon and clean application running on the specified port.
00292     Parameters:
00293     - port - port number
00294     """
00295     try:
00296       filedict=getPiDict(port)
00297       f=open(filedict, 'r')
00298       pids=pickle.load(f)
00299       for d in pids:
00300         for pid,process in d.items():
00301           if 'notifd' in process:
00302             cmd='kill -9 %d'% pid
00303             os.system(cmd)
00304       os.remove(filedict)
00305     except:
00306       #import traceback
00307       #traceback.print_exc()
00308       pass
00309 
00310     appliCleanOmniOrbConfig(port)
00311 
00312 def killMyPortSpy(pid, port):
00313     dt = 1.0
00314     while 1:
00315         if sys.platform == "win32":
00316             from win32pm import killpid
00317             if killpid(int(pid), 0) != 0:
00318                 return
00319         else:
00320             from os import kill
00321             try:
00322                 kill(int(pid), 0)
00323             except OSError, e:
00324                 if e.errno != 3:
00325                     return
00326                 break
00327             pass
00328         from time import sleep
00329         sleep(dt)
00330         pass
00331     filedict = getPiDict(port, hidden=True)
00332     if not os.path.exists(filedict):
00333         return
00334     try:
00335         import omniORB
00336         orb = omniORB.CORBA.ORB_init(sys.argv, omniORB.CORBA.ORB_ID)
00337         import SALOME_NamingServicePy
00338         ns = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb)
00339         import SALOME
00340         session = ns.Resolve("/Kernel/Session")
00341         assert session
00342     except:
00343         return
00344     try:
00345         status = session.GetStatSession()
00346     except:
00347         # -- session is in naming service but has crash
00348         status = None
00349         pass
00350     if status:
00351         if not status.activeGUI:
00352             return
00353         pass
00354     killMyPort(port)
00355     return
00356 
00357 if __name__ == "__main__":
00358     if sys.argv[1] == "--spy":
00359         pid = sys.argv[2]
00360         port = sys.argv[3]
00361         killMyPortSpy(pid, port)
00362         sys.exit(0)
00363         pass
00364     for port in sys.argv[1:]:
00365         killMyPort(port)
00366         pass
00367     pass