]> SALOME platform Git repositories - modules/kernel.git/blob - bin/server.py
Salome HOME
bos #19241 Prevent shaper tests hanging up
[modules/kernel.git] / bin / server.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2020  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, or (at your option) any later version.
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
24 import os, sys, string
25 from salome_utils import getHostName
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     server_launch_mode = "daemon"
37
38     def initArgs(self):
39         self.PID=None
40         self.CMD=[]
41         self.ARGS=[]
42         if self.args.get('xterm'):
43           if sys.platform != "win32":
44             self.ARGS=['xterm', '-iconic', '-sb', '-sl', '500', '-hold']
45           else:
46             self.ARGS=['cmd', '/c', 'start  cmd.exe', '/K']
47
48     def __init__(self,args):
49         self.args=args
50         self.initArgs()
51
52     @staticmethod
53     def set_server_launch_mode(mode):
54       if mode == "daemon" or mode == "fork":
55         Server.server_launch_mode = mode
56       else:
57         raise Exception("Unsupported server launch mode: %s" % mode)
58
59     def run(self):
60         global process_id
61         myargs=self.ARGS
62         if self.args.get('xterm'):
63             # (Debian) send LD_LIBRARY_PATH to children shells (xterm)
64           if sys.platform == "darwin":
65               env_ld_library_path=['env', 'DYLD_LIBRARY_PATH='
66                                    + os.getenv("DYLD_FALLBACK_LIBRARY_PATH")]
67               myargs = myargs +['-T']+self.CMD[:1]+['-e'] + env_ld_library_path
68           elif sys.platform != "win32":
69               env_ld_library_path=['env', 'LD_LIBRARY_PATH='
70                                    + os.getenv("LD_LIBRARY_PATH")]
71               myargs = myargs +['-T']+self.CMD[:1]+['-e'] + env_ld_library_path
72         command = myargs + self.CMD
73         # print("command = ", command)
74         if sys.platform == "win32":
75           import subprocess
76           pid = subprocess.Popen(command).pid
77         elif Server.server_launch_mode == "fork":
78           pid = os.spawnvp(os.P_NOWAIT, command[0], command)
79         else: # Server launch mode is daemon
80           pid=self.daemonize(command)
81         if pid is not None:
82           #store process pid if it really exists
83           process_id[pid]=self.CMD
84         self.PID = pid
85         return pid
86
87     def daemonize(self,args):
88         # to daemonize a process need to do the UNIX double-fork magic
89         # see Stevens, "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177)
90         # and UNIX Programming FAQ 1.7 How do I get my program to act like a daemon?
91         #     http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
92         #open a pipe
93         c2pread, c2pwrite = os.pipe()
94         #do first fork
95         pid=os.fork()
96         if pid > 0:
97           #first parent
98           os.close(c2pwrite)
99           #receive real pid from child
100           data=os.read(c2pread,24) #read 24 bytes
101           os.waitpid(pid,0) #remove zombie
102           os.close(c2pread)
103           # return : first parent
104           childpid=int(data)
105           if childpid==-1:
106             return None
107           try:
108             os.kill(childpid,0)
109             return childpid
110           except:
111             return None
112
113         #first child
114         # decouple from parent environment
115         os.setsid()
116         os.close(c2pread)
117
118         # do second fork : second child not a session leader
119         try:
120           pid = os.fork()
121           if pid > 0:
122             #send real pid to parent
123             pid_str = "%d" % pid
124             os.write(c2pwrite,pid_str.encode())
125             os.close(c2pwrite)
126             # exit from second parent
127             os._exit(0)
128         except OSError as e:
129           print("fork #2 failed: %d (%s)" % (e.errno, e.strerror), file=sys.stderr)
130           os.write(c2pwrite,"-1")
131           os.close(c2pwrite)
132           sys.exit(1)
133
134         #I am a daemon
135         os.close(0) #close stdin
136         os.open("/dev/null", os.O_RDWR)  # redirect standard input (0) to /dev/null
137         try:
138           os.execvp(args[0], args)
139         except OSError as e:
140           print("(%s) launch failed: %d (%s)" % (args[0],e.errno, e.strerror), file=sys.stderr)
141           os._exit(127)