Little Palm fixes
[MonkeyD.git] / palm / lib / proc.py
blob65c7c97a55dbc47af37ec2a038aed5f5641dea42
1 # Copyright (C) 2008-2010, Eduardo Silva <edsiper@gmail.com>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 import os
18 import sys
19 import signal
20 import time
22 from child import Child
23 from listener import Listener
24 from config import PalmConfig
25 from debug import *
27 VERSION = 0.2
29 class BigPalm:
30 def __init__(self):
31 self.print_info()
33 # Read Palm configuration
34 self.conf = PalmConfig()
35 self.conf.readconf("conf/palm.conf")
37 signal.signal(signal.SIGINT, self._on_sigint_cb)
38 self._palms = {}
40 # Load Palms
41 self.create_palms()
43 def _on_sigint_cb(self, frame, a):
44 for p in self._palms:
45 os.kill(self._palms[p], signal.SIGKILL)
47 exit(0)
49 def print_info(self):
50 print "Monkey Palm Server", VERSION
51 print "Visit us: http://www.monkey-project.com"
53 def create_palms(self):
54 for h in self.conf.get_handlers():
55 # Handler configuration
56 try:
57 port = int(self.conf.get(h, 'Port'))
58 except:
59 self.conf_error(h, 'Port')
61 try:
62 childs = int(self.conf.get(h, 'Childs'))
63 except:
64 self.conf_error(h, 'Childs')
66 try:
67 bin = self.conf.get(h, 'Exec')
68 except:
69 bin = None
71 try:
72 opts = self.conf.get(h, 'Arguments')
73 except:
74 opts = None
76 # Debug Message
77 debug("[+] Handler [%s] running on port %i, %i childs" % (h, port, childs))
79 # Creating Palm under process context
80 pid = os.fork()
81 if pid:
82 self._palms[h] = pid
83 else:
84 p = Palm(h, port, bin, opts)
85 p.create_n_childs(childs)
86 while 1:
87 time.sleep(1)
89 while 1:
90 time.sleep(1)
92 def conf_error(self, h, var):
93 print "\nPalm config error, no option '%s' in handler '%s'" % (var, h)
94 exit(1)
96 class Palm:
97 _childs = []
99 def __init__(self, name, port, bin, opts):
100 self.name = name
101 self.port = port
102 self.bin = bin
103 self.opts = opts
105 self.listen = Listener(port)
106 self.s = self.listen.s
108 def create_n_childs(self, n):
109 for i in range(n):
110 self.create_child()
112 def create_child(self):
113 child = Child(self.name, self.s, self)
114 self._register_child(child)
116 def _register_child(self, child):
117 self._childs.append(child)
119 def get_child_list(self):
120 return self._childs
122 def get_childs_len(self):
123 return len(self._childs)
125 def kill_childs(self):
126 for c in self._childs:
127 c.kill()