\ Standard daemonization of a process. http://www.faqs.org/faqs/unix-faq/programmer/faq/ section 1.7
(enable_stdio_inheritance=False)
| 510 | |
| 511 | |
| 512 | def daemonize(enable_stdio_inheritance=False): |
| 513 | """\ |
| 514 | Standard daemonization of a process. |
| 515 | http://www.faqs.org/faqs/unix-faq/programmer/faq/ section 1.7 |
| 516 | """ |
| 517 | if 'GUNICORN_FD' not in os.environ: |
| 518 | if os.fork(): |
| 519 | os._exit(0) |
| 520 | os.setsid() |
| 521 | |
| 522 | if os.fork(): |
| 523 | os._exit(0) |
| 524 | |
| 525 | os.umask(0o22) |
| 526 | |
| 527 | # In both the following any file descriptors above stdin |
| 528 | # stdout and stderr are left untouched. The inheritance |
| 529 | # option simply allows one to have output go to a file |
| 530 | # specified by way of shell redirection when not wanting |
| 531 | # to use --error-log option. |
| 532 | |
| 533 | if not enable_stdio_inheritance: |
| 534 | # Remap all of stdin, stdout and stderr on to |
| 535 | # /dev/null. The expectation is that users have |
| 536 | # specified the --error-log option. |
| 537 | |
| 538 | closerange(0, 3) |
| 539 | |
| 540 | fd_null = os.open(REDIRECT_TO, os.O_RDWR) |
| 541 | # PEP 446, make fd for /dev/null inheritable |
| 542 | os.set_inheritable(fd_null, True) |
| 543 | |
| 544 | # expect fd_null to be always 0 here, but in-case not ... |
| 545 | if fd_null != 0: |
| 546 | os.dup2(fd_null, 0) |
| 547 | |
| 548 | os.dup2(fd_null, 1) |
| 549 | os.dup2(fd_null, 2) |
| 550 | |
| 551 | else: |
| 552 | fd_null = os.open(REDIRECT_TO, os.O_RDWR) |
| 553 | |
| 554 | # Always redirect stdin to /dev/null as we would |
| 555 | # never expect to need to read interactive input. |
| 556 | |
| 557 | if fd_null != 0: |
| 558 | os.close(0) |
| 559 | os.dup2(fd_null, 0) |
| 560 | |
| 561 | # If stdout and stderr are still connected to |
| 562 | # their original file descriptors we check to see |
| 563 | # if they are associated with terminal devices. |
| 564 | # When they are we map them to /dev/null so that |
| 565 | # are still detached from any controlling terminal |
| 566 | # properly. If not we preserve them as they are. |
| 567 | # |
| 568 | # If stdin and stdout were not hooked up to the |
| 569 | # original file descriptors, then all bets are |
nothing calls this directly
no test coverage detected