Salome HOME
updated copyright message
[modules/kernel.git] / bin / server.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  CEA, EDF, 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 import logging
28
29 # -----------------------------------------------------------------------------
30 #
31 # Definition des classes d'objets pour le lancement des Server CORBA
32 #
33
34 class Server:
35     """Generic class for CORBA server launch"""
36
37     server_launch_mode = "daemon"
38
39     def initArgs(self):
40         self.PID=None
41         self.CMD=[]
42         self.ARGS=[]
43         if self.args.get('xterm'):
44           if sys.platform != "win32":
45             self.ARGS=['xterm', '-iconic', '-sb', '-sl', '500', '-hold']
46           else:
47             self.ARGS=['cmd', '/c', 'start  cmd.exe', '/K']
48
49     def __init__(self,args):
50         self.args=args
51         self.initArgs()
52
53     @staticmethod
54     def set_server_launch_mode(mode):
55       if mode == "daemon" or mode == "fork":
56         Server.server_launch_mode = mode
57       else:
58         raise Exception("Unsupported server launch mode: %s" % mode)
59
60     def run(self, daemon=False):
61         global process_id
62         myargs=self.ARGS
63         if self.args.get('xterm'):
64             # (Debian) send LD_LIBRARY_PATH to children shells (xterm)
65           if sys.platform == "darwin":
66               env_ld_library_path=['env', 'DYLD_LIBRARY_PATH='
67                                    + os.getenv("DYLD_FALLBACK_LIBRARY_PATH")]
68               myargs = myargs +['-T']+self.CMD[:1]+['-e'] + env_ld_library_path
69           elif sys.platform != "win32":
70               env_ld_library_path=['env', 'LD_LIBRARY_PATH='
71                                    + os.getenv("LD_LIBRARY_PATH")]
72               myargs = myargs +['-T']+self.CMD[:1]+['-e'] + env_ld_library_path
73         command = myargs + self.CMD
74         for sapcfg in ["SalomeAppSLConfig","SalomeAppConfig"]:
75           if sapcfg in os.environ:
76             logging.getLogger().debug("{}={}".format(sapcfg,os.environ[sapcfg]))
77         command1 = (" ".join(command)).replace("(","\\\(") ; command1 = command1.replace(")","\\\)")
78         logging.getLogger().debug("Command to be launched : {}".format(command1))
79         # print("command = ", command)
80         if sys.platform == "win32":
81           import subprocess
82           if daemon:
83             DETACHED_PROCESS = 0x00000008
84             pid = subprocess.Popen(command, creationflags=DETACHED_PROCESS).pid
85           else:
86             pid = subprocess.Popen(command).pid
87         elif Server.server_launch_mode == "fork":
88           pid = os.spawnvp(os.P_NOWAIT, command[0], command)
89         else: # Server launch mode is daemon
90           pid=self.daemonize(command)
91         if pid is not None:
92           #store process pid if it really exists
93           process_id[pid]=self.CMD
94         self.PID = pid
95         logging.getLogger().debug("PID of launched command : {}".format(pid))
96         return pid
97
98     def daemonize(self,args):
99         # to daemonize a process need to do the UNIX double-fork magic
100         # see Stevens, "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177)
101         # and UNIX Programming FAQ 1.7 How do I get my program to act like a daemon?
102         #     http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
103         #open a pipe
104         c2pread, c2pwrite = os.pipe()
105         #do first fork
106         pid=os.fork()
107         if pid > 0:
108           #first parent
109           os.close(c2pwrite)
110           #receive real pid from child
111           data=os.read(c2pread,24) #read 24 bytes
112           os.waitpid(pid,0) #remove zombie
113           os.close(c2pread)
114           # return : first parent
115           childpid=int(data)
116           if childpid==-1:
117             return None
118           try:
119             os.kill(childpid,0)
120             return childpid
121           except Exception:
122             return None
123
124         #first child
125         # decouple from parent environment
126         os.setsid()
127         os.close(c2pread)
128
129         # do second fork : second child not a session leader
130         try:
131           pid = os.fork()
132           if pid > 0:
133             #send real pid to parent
134             pid_str = "%d" % pid
135             os.write(c2pwrite,pid_str.encode())
136             os.close(c2pwrite)
137             # exit from second parent
138             os._exit(0)
139         except OSError as e:
140           print("fork #2 failed: %d (%s)" % (e.errno, e.strerror), file=sys.stderr)
141           os.write(c2pwrite,"-1")
142           os.close(c2pwrite)
143           sys.exit(1)
144
145         #I am a daemon
146         os.close(0) #close stdin
147         os.open("/dev/null", os.O_RDWR)  # redirect standard input (0) to /dev/null
148         try:
149           os.execvp(args[0], args)
150         except OSError as e:
151           print("(%s) launch failed: %d (%s)" % (args[0],e.errno, e.strerror), file=sys.stderr)
152           os._exit(127)