def __daemonize(self):
        """Fork off a background thread for execution.  If this method returns True, it is the new process, otherwise
        it is the original process.

        This will also populate the PID file with the new process id.

        This uses the UNIX double-fork magic, see Stevens' "Advanced
        Programming in the UNIX Environment" for details (ISBN 0201563177)
        http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
        """
        original_pid = os.getpid()

        def debug_logger(message):
            self._log_init_debug('%s (orig_pid=%s) %s' % (message, original_pid, scalyr_util.get_pid_tid()))

        def logger(message):
            self._log_init('%s (orig_pid=%s) %s' % (message, original_pid, scalyr_util.get_pid_tid()))

        # Create a temporary file that we will use to communicate between the calling process and the daemonize
        # process.
        reporter = StatusReporter()

        # The double fork thing is required to really dettach the eventual process for the current one, including
        # such weird details as making sure it can never be the session leader for the old process.

        # Do the first fork.

        debug_logger('Forking service')
        try:
            pid = os.fork()
            if pid > 0:
                # We are the calling process.  We will attempt to wait until we see the daemon process report its
                # status.
                status_line = reporter.read_status(timeout=30.0, timeout_status='timeout')
                if status_line == 'success':
                    return False
                elif status_line == 'alreadyRunning':
                    self.is_agent_running(fail_if_running=True)
                    raise AgentAlreadyRunning('The agent could not be started because it believes another agent '
                                              'is already running.')
                elif status_line == 'timeout':
                    sys.stderr.write('Warning, could not verify the agent started in the background.  '
                                     'May not be running.\n')
                    return False
                else:
                    sys.stderr.write("The agent appears to have failed to have started.  Error message was "
                                     "%s\n" % status_line)
                    sys.exit(1)
        except OSError, e:
            sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1)
        except SystemExit, e:
            raise e
        except Exception, e:
            reporter.report_status('forked #1 failed due to generic error: %s' % str(e))
            sys.exit(1)

        debug_logger('Second fork')

        # noinspection PyBroadException
        try:
            # decouple from parent environment
            os.chdir("/")
            # noinspection PyArgumentList
            os.setsid()
            os.umask(0)

            # do second fork
            pid = os.fork()
            if pid > 0:
                # exit from second parent
                sys.exit(0)
        except OSError, e:
            reporter.report_status((e.errno, e.strerror) % 'fork #2 failed: %d (%s)')
            sys.stderr.write('fork #2 failed: %d (%s)\n' % (e.errno, e.strerror))
            sys.exit(1)
        except SystemExit, e:
            raise e
        except Exception, e:
            reporter.report_status('forked #2 failed due to generic error: %s' % str(e))
            sys.exit(1)

        debug_logger('Finished forking')

        try:
            # redirect standard file descriptors
            sys.stdout.flush()
            sys.stderr.flush()
            si = file(self.__stdin, 'r')
            so = file(self.__stdout, 'a+')
            se = file(self.__stderr, 'a+', 0)
            os.dup2(si.fileno(), sys.stdin.fileno())
            os.dup2(so.fileno(), sys.stdout.fileno())
            os.dup2(se.fileno(), sys.stderr.fileno())

            # Write out our process id to the pidfile.
            if not self.__write_pidfile(debug_logger=debug_logger, logger=logger):
                reporter.report_status('alreadyRunning')
                return False

            reporter.report_status('success')
            logger('Process has been daemonized')
            return True
        except Exception, e:
            reporter.report_status('Finalizing fork failed due to generic error: %s' % str(e))
            sys.exit(1)
dataset/ETHPy150Open scalyr/scalyr-agent-2/scalyr_agent/platform_posix.py/PosixPlatformController.__daemonize/swapped operands of `%`