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