Salome HOME
Merge from V5_1_main branch 24/11/2010
[modules/kernel.git] / bin / server.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 #  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
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.
12 #
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.
17 #
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
21 #
22 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24
25 import os, sys, string
26 process_id = {}
27
28 # -----------------------------------------------------------------------------
29 #
30 # Definition des classes d'objets pour le lancement des Server CORBA
31 #
32
33 class Server:
34     """Generic class for CORBA server launch"""
35
36     def initArgs(self):
37         self.PID=None
38         self.CMD=[]
39         self.ARGS=[]
40         if self.args.get('xterm'):
41           if sys.platform != "win32":
42             self.ARGS=['xterm', '-iconic', '-sb', '-sl', '500', '-hold']
43           else:
44             self.ARGS=['cmd', '/c', 'start  cmd.exe', '/K']
45
46     def __init__(self,args):
47         self.args=args
48         self.initArgs()
49
50
51     def run(self):
52         global process_id
53         myargs=self.ARGS
54         if self.args.get('xterm'):
55             # (Debian) send LD_LIBRARY_PATH to children shells (xterm)
56             if sys.platform != "win32":
57               env_ld_library_path=['env', 'LD_LIBRARY_PATH='
58                                    + os.getenv("LD_LIBRARY_PATH")]
59               myargs = myargs +['-T']+self.CMD[:1]+['-e'] + env_ld_library_path
60         command = myargs + self.CMD
61         #print "command = ", command
62         if sys.platform == "win32":
63           import win32pm
64           #cmd_str = "\"" + string.join(command, " ") + "\""
65           #print cmd_str
66           #pid = win32pm.spawnpid( cmd_str )
67           pid = win32pm.spawnpid( string.join(command, " "), '-nc' )
68           #pid = win32pm.spawnpid( string.join(command, " ") )
69         else:
70           #pid = os.spawnvp(os.P_NOWAIT, command[0], command)
71           pid=self.daemonize(command)
72         if pid is not None:
73           #store process pid if it really exists
74           process_id[pid]=self.CMD
75         self.PID = pid
76         return pid
77
78     def daemonize(self,args):
79         # to daemonize a process need to do the UNIX double-fork magic
80         # see Stevens, "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177)
81         # and UNIX Programming FAQ 1.7 How do I get my program to act like a daemon?
82         #     http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
83         #open a pipe
84         c2pread, c2pwrite = os.pipe()
85         #do first fork
86         pid=os.fork()
87         if pid > 0:
88           #first parent
89           os.close(c2pwrite)
90           #receive real pid from child
91           data=os.read(c2pread,24) #read 24 bytes
92           os.waitpid(pid,0) #remove zombie
93           os.close(c2pread)
94           # return : first parent
95           childpid=int(data)
96           if childpid==-1:
97             return None
98           try:
99             os.kill(childpid,0)
100             return childpid
101           except:
102             return None
103
104         #first child
105         # decouple from parent environment
106         os.setsid()
107         os.close(c2pread)
108
109         # do second fork : second child not a session leader
110         try:
111           pid = os.fork()
112           if pid > 0:
113             #send real pid to parent
114             os.write(c2pwrite,"%d" % pid)
115             os.close(c2pwrite)
116             # exit from second parent
117             os._exit(0)
118         except OSError, e:
119           print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
120           os.write(c2pwrite,"-1")
121           os.close(c2pwrite)
122           sys.exit(1)
123
124         #I am a daemon
125         os.close(0) #close stdin
126         os.open("/dev/null", os.O_RDWR)  # redirect standard input (0) to /dev/null
127         try:
128           os.execvp(args[0], args)
129         except OSError, e:
130           print >>sys.stderr, "(%s) launch failed: %d (%s)" % (args[0],e.errno, e.strerror)
131           os._exit(127)