Back to index

salome-kernel  6.5.0
server.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 import os, sys, string
00026 from salome_utils import getHostName
00027 process_id = {}
00028 
00029 # -----------------------------------------------------------------------------
00030 #
00031 # Definition des classes d'objets pour le lancement des Server CORBA
00032 #
00033 
00034 class Server:
00035     """Generic class for CORBA server launch"""
00036     
00037     server_launch_mode = "daemon"
00038 
00039     def initArgs(self):
00040         self.PID=None
00041         self.CMD=[]
00042         self.ARGS=[]
00043         if self.args.get('xterm'):
00044           if sys.platform != "win32":
00045             self.ARGS=['xterm', '-iconic', '-sb', '-sl', '500', '-hold']
00046           else:
00047             self.ARGS=['cmd', '/c', 'start  cmd.exe', '/K']
00048 
00049     def __init__(self,args):
00050         self.args=args
00051         self.initArgs()
00052 
00053     @staticmethod
00054     def set_server_launch_mode(mode):
00055       if mode == "daemon" or mode == "fork":
00056         Server.server_launch_mode = mode
00057       else:
00058         raise Exception("Unsupported server launch mode: %s" % mode)
00059 
00060     def run(self):
00061         global process_id
00062         myargs=self.ARGS
00063         if self.args.get('xterm'):
00064             # (Debian) send LD_LIBRARY_PATH to children shells (xterm)
00065             if sys.platform != "win32":
00066               env_ld_library_path=['env', 'LD_LIBRARY_PATH='
00067                                    + os.getenv("LD_LIBRARY_PATH")]
00068               myargs = myargs +['-T']+self.CMD[:1]+['-e'] + env_ld_library_path
00069         command = myargs + self.CMD
00070         #print "command = ", command
00071         if sys.platform == "win32":
00072           import win32pm
00073           #cmd_str = "\"" + string.join(command, " ") + "\""
00074           #print cmd_str
00075           #pid = win32pm.spawnpid( cmd_str )
00076           pid = win32pm.spawnpid( string.join(command, " "), '-nc' )
00077           #pid = win32pm.spawnpid( string.join(command, " ") )
00078         elif Server.server_launch_mode == "fork":
00079           pid = os.spawnvp(os.P_NOWAIT, command[0], command)
00080         else: # Server launch mode is daemon
00081           pid=self.daemonize(command)
00082         if pid is not None:
00083           #store process pid if it really exists
00084           process_id[pid]=self.CMD
00085         self.PID = pid
00086         return pid
00087 
00088     def daemonize(self,args):
00089         # to daemonize a process need to do the UNIX double-fork magic
00090         # see Stevens, "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177)
00091         # and UNIX Programming FAQ 1.7 How do I get my program to act like a daemon?
00092         #     http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
00093         #open a pipe
00094         c2pread, c2pwrite = os.pipe()
00095         #do first fork
00096         pid=os.fork()
00097         if pid > 0:
00098           #first parent
00099           os.close(c2pwrite)
00100           #receive real pid from child
00101           data=os.read(c2pread,24) #read 24 bytes
00102           os.waitpid(pid,0) #remove zombie
00103           os.close(c2pread)
00104           # return : first parent
00105           childpid=int(data)
00106           if childpid==-1:
00107             return None
00108           try:
00109             os.kill(childpid,0)
00110             return childpid
00111           except:
00112             return None
00113 
00114         #first child
00115         # decouple from parent environment
00116         os.setsid()
00117         os.close(c2pread)
00118 
00119         # do second fork : second child not a session leader
00120         try:
00121           pid = os.fork()
00122           if pid > 0:
00123             #send real pid to parent
00124             os.write(c2pwrite,"%d" % pid)
00125             os.close(c2pwrite)
00126             # exit from second parent
00127             os._exit(0)
00128         except OSError, e:
00129           print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
00130           os.write(c2pwrite,"-1")
00131           os.close(c2pwrite)
00132           sys.exit(1)
00133 
00134         #I am a daemon
00135         os.close(0) #close stdin
00136         os.open("/dev/null", os.O_RDWR)  # redirect standard input (0) to /dev/null
00137         try:
00138           os.execvp(args[0], args)
00139         except OSError, e:
00140           if args[0] != "notifd":
00141             print >>sys.stderr, "(%s) launch failed: %d (%s)" % (args[0],e.errno, e.strerror)
00142             pass
00143           os._exit(127)