Statistics
| Branch: | Tag: | Revision:

root / daemons / ganeti-noded @ a0c3fea1

History | View | Annotate | Download (11.9 kB)

1 a8083063 Iustin Pop
#!/usr/bin/python
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 a8083063 Iustin Pop
# Copyright (C) 2006, 2007 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 a8083063 Iustin Pop
"""Ganeti node daemon"""
23 a8083063 Iustin Pop
24 a8083063 Iustin Pop
import os
25 a8083063 Iustin Pop
import sys
26 a8083063 Iustin Pop
import resource
27 a8083063 Iustin Pop
import traceback
28 a8083063 Iustin Pop
29 a8083063 Iustin Pop
from optparse import OptionParser
30 a8083063 Iustin Pop
31 a8083063 Iustin Pop
32 a8083063 Iustin Pop
from ganeti import backend
33 a8083063 Iustin Pop
from ganeti import logger
34 a8083063 Iustin Pop
from ganeti import constants
35 a8083063 Iustin Pop
from ganeti import objects
36 a8083063 Iustin Pop
from ganeti import errors
37 a8083063 Iustin Pop
from ganeti import ssconf
38 a8083063 Iustin Pop
39 a8083063 Iustin Pop
from twisted.spread import pb
40 a8083063 Iustin Pop
from twisted.internet import reactor
41 a8083063 Iustin Pop
from twisted.cred import checkers, portal
42 a8083063 Iustin Pop
from OpenSSL import SSL
43 a8083063 Iustin Pop
44 a8083063 Iustin Pop
45 a8083063 Iustin Pop
class ServerContextFactory:
46 a8083063 Iustin Pop
  def getContext(self):
47 a8083063 Iustin Pop
    ctx = SSL.Context(SSL.TLSv1_METHOD)
48 a8083063 Iustin Pop
    ctx.use_certificate_file(constants.SSL_CERT_FILE)
49 a8083063 Iustin Pop
    ctx.use_privatekey_file(constants.SSL_CERT_FILE)
50 a8083063 Iustin Pop
    return ctx
51 a8083063 Iustin Pop
52 a8083063 Iustin Pop
class ServerObject(pb.Avatar):
53 a8083063 Iustin Pop
  def __init__(self, name):
54 a8083063 Iustin Pop
    self.name = name
55 a8083063 Iustin Pop
56 a8083063 Iustin Pop
  def perspectiveMessageReceived(self, broker, message, args, kw):
57 a8083063 Iustin Pop
    """This method is called when a network message is received.
58 a8083063 Iustin Pop
59 a8083063 Iustin Pop
    I will call::
60 a8083063 Iustin Pop
61 a8083063 Iustin Pop
      |  self.perspective_%(message)s(*broker.unserialize(args),
62 a8083063 Iustin Pop
      |                               **broker.unserialize(kw))
63 a8083063 Iustin Pop
64 a8083063 Iustin Pop
    to handle the method; subclasses of Avatar are expected to
65 a8083063 Iustin Pop
    implement methods of this naming convention.
66 a8083063 Iustin Pop
67 098c0958 Michael Hanselmann
    """
68 a8083063 Iustin Pop
    args = broker.unserialize(args, self)
69 a8083063 Iustin Pop
    kw = broker.unserialize(kw, self)
70 a8083063 Iustin Pop
    method = getattr(self, "perspective_%s" % message)
71 a8083063 Iustin Pop
    tb = None
72 a8083063 Iustin Pop
    state = None
73 a8083063 Iustin Pop
    try:
74 a8083063 Iustin Pop
      state = method(*args, **kw)
75 a8083063 Iustin Pop
    except:
76 a8083063 Iustin Pop
      tb = traceback.format_exc()
77 a8083063 Iustin Pop
78 a8083063 Iustin Pop
    return broker.serialize((tb, state), self, method, args, kw)
79 a8083063 Iustin Pop
80 a8083063 Iustin Pop
  # the new block devices  --------------------------
81 a8083063 Iustin Pop
82 588d1da3 Michael Hanselmann
  def perspective_blockdev_create(self, params):
83 a0c3fea1 Michael Hanselmann
    bdev_s, size, on_primary, info = params
84 a8083063 Iustin Pop
    bdev = objects.ConfigObject.Loads(bdev_s)
85 a8083063 Iustin Pop
    if bdev is None:
86 a8083063 Iustin Pop
      raise ValueError("can't unserialize data!")
87 a0c3fea1 Michael Hanselmann
    return backend.CreateBlockDevice(bdev, size, on_primary, info)
88 a8083063 Iustin Pop
89 588d1da3 Michael Hanselmann
  def perspective_blockdev_remove(self, params):
90 a8083063 Iustin Pop
    bdev_s = params[0]
91 a8083063 Iustin Pop
    bdev = objects.ConfigObject.Loads(bdev_s)
92 a8083063 Iustin Pop
    return backend.RemoveBlockDevice(bdev)
93 a8083063 Iustin Pop
94 588d1da3 Michael Hanselmann
  def perspective_blockdev_assemble(self, params):
95 a8083063 Iustin Pop
    bdev_s, on_primary = params
96 a8083063 Iustin Pop
    bdev = objects.ConfigObject.Loads(bdev_s)
97 a8083063 Iustin Pop
    if bdev is None:
98 a8083063 Iustin Pop
      raise ValueError("can't unserialize data!")
99 a8083063 Iustin Pop
    return backend.AssembleBlockDevice(bdev, on_primary)
100 a8083063 Iustin Pop
101 588d1da3 Michael Hanselmann
  def perspective_blockdev_shutdown(self, params):
102 a8083063 Iustin Pop
    bdev_s = params[0]
103 a8083063 Iustin Pop
    bdev = objects.ConfigObject.Loads(bdev_s)
104 a8083063 Iustin Pop
    if bdev is None:
105 a8083063 Iustin Pop
      raise ValueError("can't unserialize data!")
106 a8083063 Iustin Pop
    return backend.ShutdownBlockDevice(bdev)
107 a8083063 Iustin Pop
108 588d1da3 Michael Hanselmann
  def perspective_blockdev_addchild(self, params):
109 a8083063 Iustin Pop
    bdev_s, ndev_s = params
110 a8083063 Iustin Pop
    bdev = objects.ConfigObject.Loads(bdev_s)
111 a8083063 Iustin Pop
    ndev = objects.ConfigObject.Loads(ndev_s)
112 a8083063 Iustin Pop
    if bdev is None or ndev is None:
113 a8083063 Iustin Pop
      raise ValueError("can't unserialize data!")
114 a8083063 Iustin Pop
    return backend.MirrorAddChild(bdev, ndev)
115 a8083063 Iustin Pop
116 588d1da3 Michael Hanselmann
  def perspective_blockdev_removechild(self, params):
117 a8083063 Iustin Pop
    bdev_s, ndev_s = params
118 a8083063 Iustin Pop
    bdev = objects.ConfigObject.Loads(bdev_s)
119 a8083063 Iustin Pop
    ndev = objects.ConfigObject.Loads(ndev_s)
120 a8083063 Iustin Pop
    if bdev is None or ndev is None:
121 a8083063 Iustin Pop
      raise ValueError("can't unserialize data!")
122 a8083063 Iustin Pop
    return backend.MirrorRemoveChild(bdev, ndev)
123 a8083063 Iustin Pop
124 a8083063 Iustin Pop
  def perspective_blockdev_getmirrorstatus(self, params):
125 a8083063 Iustin Pop
    disks = [objects.ConfigObject.Loads(dsk_s)
126 a8083063 Iustin Pop
            for dsk_s in params]
127 a8083063 Iustin Pop
    return backend.GetMirrorStatus(disks)
128 a8083063 Iustin Pop
129 a8083063 Iustin Pop
  def perspective_blockdev_find(self, params):
130 a8083063 Iustin Pop
    disk = objects.ConfigObject.Loads(params[0])
131 a8083063 Iustin Pop
    return backend.FindBlockDevice(disk)
132 a8083063 Iustin Pop
133 588d1da3 Michael Hanselmann
  def perspective_blockdev_snapshot(self, params):
134 a8083063 Iustin Pop
    cfbd = objects.ConfigObject.Loads(params[0])
135 a8083063 Iustin Pop
    return backend.SnapshotBlockDevice(cfbd)
136 a8083063 Iustin Pop
137 a8083063 Iustin Pop
  # export/import  --------------------------
138 a8083063 Iustin Pop
139 588d1da3 Michael Hanselmann
  def perspective_snapshot_export(self, params):
140 a8083063 Iustin Pop
    disk = objects.ConfigObject.Loads(params[0])
141 a8083063 Iustin Pop
    dest_node = params[1]
142 a8083063 Iustin Pop
    instance = objects.ConfigObject.Loads(params[2])
143 a8083063 Iustin Pop
    return backend.ExportSnapshot(disk,dest_node,instance)
144 a8083063 Iustin Pop
145 588d1da3 Michael Hanselmann
  def perspective_finalize_export(self, params):
146 a8083063 Iustin Pop
    instance = objects.ConfigObject.Loads(params[0])
147 a8083063 Iustin Pop
    snap_disks = [objects.ConfigObject.Loads(str_data)
148 a8083063 Iustin Pop
                  for str_data in params[1]]
149 a8083063 Iustin Pop
    return backend.FinalizeExport(instance, snap_disks)
150 a8083063 Iustin Pop
151 588d1da3 Michael Hanselmann
  def perspective_export_info(self, params):
152 a8083063 Iustin Pop
    dir = params[0]
153 a8083063 Iustin Pop
    einfo = backend.ExportInfo(dir)
154 a8083063 Iustin Pop
    if einfo is None:
155 a8083063 Iustin Pop
      return einfo
156 a8083063 Iustin Pop
    return einfo.Dumps()
157 a8083063 Iustin Pop
158 a8083063 Iustin Pop
  def perspective_export_list(self, params):
159 a8083063 Iustin Pop
    return backend.ListExports()
160 a8083063 Iustin Pop
161 a8083063 Iustin Pop
  def perspective_export_remove(self, params):
162 a8083063 Iustin Pop
    export = params[0]
163 a8083063 Iustin Pop
    return backend.RemoveExport(export)
164 a8083063 Iustin Pop
165 a8083063 Iustin Pop
  # volume  --------------------------
166 a8083063 Iustin Pop
167 588d1da3 Michael Hanselmann
  def perspective_volume_list(self, params):
168 a8083063 Iustin Pop
    vgname = params[0]
169 a8083063 Iustin Pop
    return backend.GetVolumeList(vgname)
170 a8083063 Iustin Pop
171 588d1da3 Michael Hanselmann
  def perspective_vg_list(self, params):
172 a8083063 Iustin Pop
    return backend.ListVolumeGroups()
173 a8083063 Iustin Pop
174 a8083063 Iustin Pop
  # bridge  --------------------------
175 a8083063 Iustin Pop
176 588d1da3 Michael Hanselmann
  def perspective_bridges_exist(self, params):
177 a8083063 Iustin Pop
    bridges_list = params[0]
178 a8083063 Iustin Pop
    return backend.BridgesExist(bridges_list)
179 a8083063 Iustin Pop
180 a8083063 Iustin Pop
  # instance  --------------------------
181 a8083063 Iustin Pop
182 588d1da3 Michael Hanselmann
  def perspective_instance_os_add(self, params):
183 a8083063 Iustin Pop
    inst_s, os_disk, swap_disk = params
184 a8083063 Iustin Pop
    inst = objects.ConfigObject.Loads(inst_s)
185 a8083063 Iustin Pop
    return backend.AddOSToInstance(inst, os_disk, swap_disk)
186 a8083063 Iustin Pop
187 a8083063 Iustin Pop
  def perspective_instance_os_import(self, params):
188 a8083063 Iustin Pop
    inst_s, os_disk, swap_disk, src_node, src_image = params
189 a8083063 Iustin Pop
    inst = objects.ConfigObject.Loads(inst_s)
190 a8083063 Iustin Pop
    return backend.ImportOSIntoInstance(inst, os_disk, swap_disk,
191 a8083063 Iustin Pop
                                        src_node, src_image)
192 a8083063 Iustin Pop
193 588d1da3 Michael Hanselmann
  def perspective_instance_shutdown(self, params):
194 a8083063 Iustin Pop
    instance = objects.ConfigObject.Loads(params[0])
195 a8083063 Iustin Pop
    return backend.ShutdownInstance(instance)
196 a8083063 Iustin Pop
197 588d1da3 Michael Hanselmann
  def perspective_instance_start(self, params):
198 a8083063 Iustin Pop
    instance = objects.ConfigObject.Loads(params[0])
199 a8083063 Iustin Pop
    extra_args = params[1]
200 a8083063 Iustin Pop
    return backend.StartInstance(instance, extra_args)
201 a8083063 Iustin Pop
202 588d1da3 Michael Hanselmann
  def perspective_instance_info(self, params):
203 a8083063 Iustin Pop
    return backend.GetInstanceInfo(params[0])
204 a8083063 Iustin Pop
205 588d1da3 Michael Hanselmann
  def perspective_all_instances_info(self, params):
206 a8083063 Iustin Pop
    return backend.GetAllInstancesInfo()
207 a8083063 Iustin Pop
208 588d1da3 Michael Hanselmann
  def perspective_instance_list(self, params):
209 a8083063 Iustin Pop
    return backend.GetInstanceList()
210 a8083063 Iustin Pop
211 a8083063 Iustin Pop
  # node --------------------------
212 a8083063 Iustin Pop
213 588d1da3 Michael Hanselmann
  def perspective_node_info(self, params):
214 a8083063 Iustin Pop
    vgname = params[0]
215 a8083063 Iustin Pop
    return backend.GetNodeInfo(vgname)
216 a8083063 Iustin Pop
217 588d1da3 Michael Hanselmann
  def perspective_node_add(self, params):
218 a8083063 Iustin Pop
    return backend.AddNode(params[0], params[1], params[2],
219 a8083063 Iustin Pop
                           params[3], params[4], params[5])
220 a8083063 Iustin Pop
221 588d1da3 Michael Hanselmann
  def perspective_node_verify(self, params):
222 a8083063 Iustin Pop
    return backend.VerifyNode(params[0])
223 a8083063 Iustin Pop
224 a8083063 Iustin Pop
  def perspective_node_start_master(self, params):
225 a8083063 Iustin Pop
    return backend.StartMaster()
226 a8083063 Iustin Pop
227 a8083063 Iustin Pop
  def perspective_node_stop_master(self, params):
228 a8083063 Iustin Pop
    return backend.StopMaster()
229 a8083063 Iustin Pop
230 a8083063 Iustin Pop
  def perspective_node_leave_cluster(self, params):
231 a8083063 Iustin Pop
    return backend.LeaveCluster()
232 a8083063 Iustin Pop
233 dcb93971 Michael Hanselmann
  def perspective_node_volumes(self, params):
234 dcb93971 Michael Hanselmann
    return backend.NodeVolumes()
235 dcb93971 Michael Hanselmann
236 a8083063 Iustin Pop
  # cluster --------------------------
237 a8083063 Iustin Pop
238 588d1da3 Michael Hanselmann
  def perspective_version(self, params):
239 a8083063 Iustin Pop
    return constants.PROTOCOL_VERSION
240 a8083063 Iustin Pop
241 588d1da3 Michael Hanselmann
  def perspective_upload_file(self, params):
242 a8083063 Iustin Pop
    return backend.UploadFile(*params)
243 a8083063 Iustin Pop
244 a8083063 Iustin Pop
245 a8083063 Iustin Pop
  # os -----------------------
246 a8083063 Iustin Pop
247 a8083063 Iustin Pop
  def perspective_os_diagnose(self, params):
248 a8083063 Iustin Pop
    os_list = backend.DiagnoseOS()
249 a8083063 Iustin Pop
    if not os_list:
250 a8083063 Iustin Pop
      # this catches also return values of 'False',
251 a8083063 Iustin Pop
      # for which we can't iterate over
252 a8083063 Iustin Pop
      return os_list
253 a8083063 Iustin Pop
    result = []
254 a8083063 Iustin Pop
    for data in os_list:
255 a8083063 Iustin Pop
      if isinstance(data, objects.OS):
256 a8083063 Iustin Pop
        result.append(data.Dumps())
257 a8083063 Iustin Pop
      elif isinstance(data, errors.InvalidOS):
258 a8083063 Iustin Pop
        result.append(data.args)
259 a8083063 Iustin Pop
      else:
260 a8083063 Iustin Pop
        raise errors.ProgrammerError, ("Invalid result from backend.DiagnoseOS"
261 a8083063 Iustin Pop
                                       " (class %s, %s)" %
262 a8083063 Iustin Pop
                                       (str(data.__class__), data))
263 a8083063 Iustin Pop
264 a8083063 Iustin Pop
    return result
265 a8083063 Iustin Pop
266 a8083063 Iustin Pop
  def perspective_os_get(self, params):
267 a8083063 Iustin Pop
    name = params[0]
268 a8083063 Iustin Pop
    try:
269 a8083063 Iustin Pop
      os = backend.OSFromDisk(name).Dumps()
270 a8083063 Iustin Pop
    except errors.InvalidOS, err:
271 a8083063 Iustin Pop
      os = err.args
272 a8083063 Iustin Pop
    return os
273 a8083063 Iustin Pop
274 a8083063 Iustin Pop
  # hooks -----------------------
275 a8083063 Iustin Pop
276 a8083063 Iustin Pop
  def perspective_hooks_runner(self, params):
277 a8083063 Iustin Pop
    hpath, phase, env = params
278 a8083063 Iustin Pop
    hr = backend.HooksRunner()
279 a8083063 Iustin Pop
    return hr.RunHooks(hpath, phase, env)
280 a8083063 Iustin Pop
281 a8083063 Iustin Pop
282 a8083063 Iustin Pop
class MyRealm:
283 a8083063 Iustin Pop
  __implements__ = portal.IRealm
284 a8083063 Iustin Pop
  def requestAvatar(self, avatarId, mind, *interfaces):
285 a8083063 Iustin Pop
    if pb.IPerspective not in interfaces:
286 a8083063 Iustin Pop
      raise NotImplementedError
287 a8083063 Iustin Pop
    return pb.IPerspective, ServerObject(avatarId), lambda:None
288 a8083063 Iustin Pop
289 a8083063 Iustin Pop
290 a8083063 Iustin Pop
def ParseOptions():
291 a8083063 Iustin Pop
  """Parse the command line options.
292 a8083063 Iustin Pop
293 a8083063 Iustin Pop
  Returns:
294 a8083063 Iustin Pop
    (options, args) as from OptionParser.parse_args()
295 a8083063 Iustin Pop
296 a8083063 Iustin Pop
  """
297 a8083063 Iustin Pop
  parser = OptionParser(description="Ganeti node daemon",
298 a8083063 Iustin Pop
                        usage="%prog [-f] [-d]",
299 a8083063 Iustin Pop
                        version="%%prog (ganeti) %s" %
300 a8083063 Iustin Pop
                        constants.RELEASE_VERSION)
301 a8083063 Iustin Pop
302 a8083063 Iustin Pop
  parser.add_option("-f", "--foreground", dest="fork",
303 a8083063 Iustin Pop
                    help="Don't detach from the current terminal",
304 a8083063 Iustin Pop
                    default=True, action="store_false")
305 a8083063 Iustin Pop
  parser.add_option("-d", "--debug", dest="debug",
306 a8083063 Iustin Pop
                    help="Enable some debug messages",
307 a8083063 Iustin Pop
                    default=False, action="store_true")
308 a8083063 Iustin Pop
  options, args = parser.parse_args()
309 a8083063 Iustin Pop
  return options, args
310 a8083063 Iustin Pop
311 a8083063 Iustin Pop
312 a8083063 Iustin Pop
def main():
313 a8083063 Iustin Pop
  options, args = ParseOptions()
314 a8083063 Iustin Pop
  for fname in (constants.SSL_CERT_FILE,):
315 a8083063 Iustin Pop
    if not os.path.isfile(fname):
316 a8083063 Iustin Pop
      print "config %s not there, will not run." % fname
317 a8083063 Iustin Pop
      sys.exit(5)
318 a8083063 Iustin Pop
319 a8083063 Iustin Pop
  try:
320 a8083063 Iustin Pop
    ss = ssconf.SimpleStore()
321 a8083063 Iustin Pop
    port = ss.GetNodeDaemonPort()
322 a8083063 Iustin Pop
    pwdata = ss.GetNodeDaemonPassword()
323 a8083063 Iustin Pop
  except errors.ConfigurationError, err:
324 a8083063 Iustin Pop
    print "Cluster configuration incomplete: '%s'" % str(err)
325 a8083063 Iustin Pop
    sys.exit(5)
326 a8083063 Iustin Pop
327 a8083063 Iustin Pop
  # become a daemon
328 a8083063 Iustin Pop
  if options.fork:
329 a8083063 Iustin Pop
    createDaemon()
330 a8083063 Iustin Pop
331 a8083063 Iustin Pop
  logger.SetupLogging(twisted_workaround=True, debug=options.debug,
332 a8083063 Iustin Pop
                      program="ganeti-noded")
333 a8083063 Iustin Pop
334 a8083063 Iustin Pop
  p = portal.Portal(MyRealm())
335 a8083063 Iustin Pop
  p.registerChecker(
336 a8083063 Iustin Pop
    checkers.InMemoryUsernamePasswordDatabaseDontUse(master_node=pwdata))
337 a8083063 Iustin Pop
  reactor.listenSSL(port, pb.PBServerFactory(p), ServerContextFactory())
338 a8083063 Iustin Pop
  reactor.run()
339 a8083063 Iustin Pop
340 a8083063 Iustin Pop
341 a8083063 Iustin Pop
def createDaemon():
342 a8083063 Iustin Pop
  """Detach a process from the controlling terminal and run it in the
343 a8083063 Iustin Pop
  background as a daemon.
344 098c0958 Michael Hanselmann
345 a8083063 Iustin Pop
  """
346 a8083063 Iustin Pop
  UMASK = 077
347 a8083063 Iustin Pop
  WORKDIR = "/"
348 a8083063 Iustin Pop
  # Default maximum for the number of available file descriptors.
349 a8083063 Iustin Pop
  if 'SC_OPEN_MAX' in os.sysconf_names:
350 a8083063 Iustin Pop
    try:
351 a8083063 Iustin Pop
      MAXFD = os.sysconf('SC_OPEN_MAX')
352 a8083063 Iustin Pop
      if MAXFD < 0:
353 a8083063 Iustin Pop
        MAXFD = 1024
354 a8083063 Iustin Pop
    except OSError:
355 a8083063 Iustin Pop
      MAXFD = 1024
356 a8083063 Iustin Pop
  else:
357 a8083063 Iustin Pop
    MAXFD = 1024
358 a8083063 Iustin Pop
  # The standard I/O file descriptors are redirected to /dev/null by default.
359 a8083063 Iustin Pop
  #REDIRECT_TO = getattr(os, "devnull", "/dev/null")
360 a8083063 Iustin Pop
  REDIRECT_TO = constants.LOG_NODESERVER
361 a8083063 Iustin Pop
  try:
362 a8083063 Iustin Pop
    pid = os.fork()
363 a8083063 Iustin Pop
  except OSError, e:
364 a8083063 Iustin Pop
    raise Exception, "%s [%d]" % (e.strerror, e.errno)
365 3c9a0742 Michael Hanselmann
  if (pid == 0):  # The first child.
366 a8083063 Iustin Pop
    os.setsid()
367 a8083063 Iustin Pop
    try:
368 3c9a0742 Michael Hanselmann
      pid = os.fork() # Fork a second child.
369 a8083063 Iustin Pop
    except OSError, e:
370 a8083063 Iustin Pop
      raise Exception, "%s [%d]" % (e.strerror, e.errno)
371 3c9a0742 Michael Hanselmann
    if (pid == 0):  # The second child.
372 a8083063 Iustin Pop
      os.chdir(WORKDIR)
373 a8083063 Iustin Pop
      os.umask(UMASK)
374 a8083063 Iustin Pop
    else:
375 a8083063 Iustin Pop
      # exit() or _exit()?  See below.
376 3c9a0742 Michael Hanselmann
      os._exit(0) # Exit parent (the first child) of the second child.
377 a8083063 Iustin Pop
  else:
378 3c9a0742 Michael Hanselmann
    os._exit(0) # Exit parent of the first child.
379 a8083063 Iustin Pop
  maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
380 a8083063 Iustin Pop
  if (maxfd == resource.RLIM_INFINITY):
381 a8083063 Iustin Pop
    maxfd = MAXFD
382 a8083063 Iustin Pop
383 a8083063 Iustin Pop
  # Iterate through and close all file descriptors.
384 a8083063 Iustin Pop
  for fd in range(0, maxfd):
385 a8083063 Iustin Pop
    try:
386 a8083063 Iustin Pop
      os.close(fd)
387 3c9a0742 Michael Hanselmann
    except OSError: # ERROR, fd wasn't open to begin with (ignored)
388 a8083063 Iustin Pop
      pass
389 a8083063 Iustin Pop
  os.open(REDIRECT_TO, os.O_RDWR|os.O_CREAT|os.O_APPEND) # standard input (0)
390 a8083063 Iustin Pop
  # Duplicate standard input to standard output and standard error.
391 3c9a0742 Michael Hanselmann
  os.dup2(0, 1)     # standard output (1)
392 3c9a0742 Michael Hanselmann
  os.dup2(0, 2)     # standard error (2)
393 a8083063 Iustin Pop
  return(0)
394 a8083063 Iustin Pop
395 a8083063 Iustin Pop
396 a8083063 Iustin Pop
if __name__=='__main__':
397 a8083063 Iustin Pop
  main()