Salome HOME
To see which hdf5 we're using in configure log
[modules/kernel.git] / bin / server.py
1 #!/usr/bin/env python
2 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 #  This library is free software; you can redistribute it and/or
8 #  modify it under the terms of the GNU Lesser General Public
9 #  License as published by the Free Software Foundation; either
10 #  version 2.1 of the License.
11 #
12 #  This library is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  Lesser General Public License for more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public
18 #  License along with this library; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23 import os, sys, string
24 process_id = {}
25
26 # -----------------------------------------------------------------------------
27 #
28 # Definition des classes d'objets pour le lancement des Server CORBA
29 #
30
31 class Server:
32     """Generic class for CORBA server launch"""
33
34     def initArgs(self):
35         self.PID=None
36         self.CMD=[]
37         self.ARGS=[]
38         if self.args.get('xterm'):
39             self.ARGS=['xterm', '-iconic', '-sb', '-sl', '500', '-hold']
40
41     def __init__(self,args):
42         self.args=args
43         self.initArgs()
44
45
46     def run(self):
47         global process_id
48         myargs=self.ARGS
49         if self.args.get('xterm'):
50             # (Debian) send LD_LIBRARY_PATH to children shells (xterm)
51             env_ld_library_path=['env', 'LD_LIBRARY_PATH='
52                                  + os.getenv("LD_LIBRARY_PATH")]
53             myargs = myargs +['-T']+self.CMD[:1]+['-e'] + env_ld_library_path
54         command = myargs + self.CMD
55         #print "command = ", command
56         if sys.platform == "win32":
57           import win32pm
58           #cmd_str = "\"" + string.join(command, " ") + "\""
59           #print cmd_str
60           #pid = win32pm.spawnpid( cmd_str )
61           pid = win32pm.spawnpid( string.join(command, " "), '-nc' )
62           #pid = win32pm.spawnpid( string.join(command, " ") )
63         else:
64           #pid = os.spawnvp(os.P_NOWAIT, command[0], command)
65           pid=self.daemonize(command)
66         if pid is not None:
67           #store process pid if it really exists
68           process_id[pid]=self.CMD
69         self.PID = pid
70         return pid
71
72     def daemonize(self,args):
73         # to daemonize a process need to do the UNIX double-fork magic
74         # see Stevens, "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177)
75         # and UNIX Programming FAQ 1.7 How do I get my program to act like a daemon?
76         #     http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
77         #open a pipe
78         c2pread, c2pwrite = os.pipe()
79         #do first fork
80         pid=os.fork()
81         if pid > 0:
82           #first parent
83           os.close(c2pwrite)
84           #receive real pid from child
85           data=os.read(c2pread,24) #read 24 bytes
86           os.waitpid(pid,0) #remove zombie
87           os.close(c2pread)
88           # return : first parent
89           childpid=int(data)
90           if childpid==-1:
91             return None
92           try:
93             os.kill(childpid,0)
94             return childpid
95           except:
96             return None
97
98         #first child
99         # decouple from parent environment
100         os.setsid()
101         os.close(c2pread)
102
103         # do second fork : second child not a session leader
104         try:
105           pid = os.fork()
106           if pid > 0:
107             #send real pid to parent
108             os.write(c2pwrite,"%d" % pid)
109             os.close(c2pwrite)
110             # exit from second parent
111             os._exit(0)
112         except OSError, e:
113           print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
114           os.write(c2pwrite,"-1")
115           os.close(c2pwrite)
116           sys.exit(1)
117
118         #I am a daemon
119         os.close(0) #close stdin
120         os.open("/dev/null", os.O_RDWR)  # redirect standard input (0) to /dev/null
121         try:
122           os.execvp(args[0], args)
123         except OSError, e:
124           print >>sys.stderr, "(%s) launch failed: %d (%s)" % (args[0],e.errno, e.strerror)
125           os._exit(127)