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 import os, sys, string
26 from salome_utils import getHostName
29 # -----------------------------------------------------------------------------
31 # Definition des classes d'objets pour le lancement des Server CORBA
35 """Generic class for CORBA server launch"""
37 server_launch_args = []
43 if self.args.get('xterm'):
44 if sys.platform != "win32":
45 self.ARGS=['xterm', '-iconic', '-sb', '-sl', '500', '-hold']
47 self.ARGS=['cmd', '/c', 'start cmd.exe', '/K']
49 def __init__(self,args):
54 def set_server_launch_cmd(cmd):
56 Server.server_launch_args = ["srun", "-n", "1", "-N", "1"]
57 Server.server_launch_args += ["--share", "--nodelist=%s" % getHostName()]
59 print >>sys.stderr, "Unknown server launch command:%s" % cmd
64 if self.args.get('xterm'):
65 # (Debian) send LD_LIBRARY_PATH to children shells (xterm)
66 if sys.platform != "win32":
67 env_ld_library_path=['env', 'LD_LIBRARY_PATH='
68 + os.getenv("LD_LIBRARY_PATH")]
69 myargs = myargs +['-T']+self.CMD[:1]+['-e'] + env_ld_library_path
70 command = myargs + self.CMD
71 #print "command = ", command
72 if sys.platform == "win32":
74 #cmd_str = "\"" + string.join(command, " ") + "\""
76 #pid = win32pm.spawnpid( cmd_str )
77 pid = win32pm.spawnpid( string.join(command, " "), '-nc' )
78 #pid = win32pm.spawnpid( string.join(command, " ") )
80 #pid = os.spawnvp(os.P_NOWAIT, command[0], command)
81 pid=self.daemonize(command)
83 #store process pid if it really exists
84 process_id[pid]=self.CMD
88 def daemonize(self,args):
89 # to daemonize a process need to do the UNIX double-fork magic
90 # see Stevens, "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177)
91 # and UNIX Programming FAQ 1.7 How do I get my program to act like a daemon?
92 # http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
94 c2pread, c2pwrite = os.pipe()
100 #receive real pid from child
101 data=os.read(c2pread,24) #read 24 bytes
102 os.waitpid(pid,0) #remove zombie
104 # return : first parent
115 # decouple from parent environment
119 # do second fork : second child not a session leader
123 #send real pid to parent
124 os.write(c2pwrite,"%d" % pid)
126 # exit from second parent
129 print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
130 os.write(c2pwrite,"-1")
135 os.close(0) #close stdin
136 os.open("/dev/null", os.O_RDWR) # redirect standard input (0) to /dev/null
137 all_args = Server.server_launch_args + args
139 os.execvp(all_args[0], all_args)
141 if args[0] != "notifd":
142 print >>sys.stderr, "(%s) launch failed: %d (%s)" % (args[0],e.errno, e.strerror)