| 345 | sleep(1) |
| 346 | |
| 347 | def daemonize(self): |
| 348 | grandfatherPid = os.getpid() |
| 349 | parentPid = None |
| 350 | try: |
| 351 | if os.fork(): |
| 352 | # unlock |
| 353 | shared.thisapp.cleanup() |
| 354 | # wait until grandchild ready |
| 355 | while True: |
| 356 | sleep(1) |
| 357 | os._exit(0) |
| 358 | except AttributeError: |
| 359 | # fork not implemented |
| 360 | pass |
| 361 | else: |
| 362 | parentPid = os.getpid() |
| 363 | shared.thisapp.lock() # relock |
| 364 | os.umask(0) |
| 365 | try: |
| 366 | os.setsid() |
| 367 | except AttributeError: |
| 368 | # setsid not implemented |
| 369 | pass |
| 370 | try: |
| 371 | if os.fork(): |
| 372 | # unlock |
| 373 | shared.thisapp.cleanup() |
| 374 | # wait until child ready |
| 375 | while True: |
| 376 | sleep(1) |
| 377 | os._exit(0) |
| 378 | except AttributeError: |
| 379 | # fork not implemented |
| 380 | pass |
| 381 | else: |
| 382 | shared.thisapp.lock() # relock |
| 383 | shared.thisapp.lockPid = None # indicate we're the final child |
| 384 | sys.stdout.flush() |
| 385 | sys.stderr.flush() |
| 386 | if not sys.platform.startswith('win'): |
| 387 | si = file(os.devnull, 'r') |
| 388 | so = file(os.devnull, 'a+') |
| 389 | se = file(os.devnull, 'a+', 0) |
| 390 | os.dup2(si.fileno(), sys.stdin.fileno()) |
| 391 | os.dup2(so.fileno(), sys.stdout.fileno()) |
| 392 | os.dup2(se.fileno(), sys.stderr.fileno()) |
| 393 | if parentPid: |
| 394 | # signal ready |
| 395 | os.kill(parentPid, signal.SIGTERM) |
| 396 | os.kill(grandfatherPid, signal.SIGTERM) |
| 397 | |
| 398 | def setSignalHandler(self): |
| 399 | signal.signal(signal.SIGINT, helper_generic.signal_handler) |