Beispiel #1
0
    def daemonize(self):
        """
        Fork off as a daemon
        """
        # pylint: disable=E1101,W0212
        # An object is accessed for a non-existent member.
        # Access to a protected member of a client class
        # Make a non-session-leader child process
        try:
            pid = os.fork()  # @UndefinedVariable - only available in UNIX
            if pid != 0:
                os._exit(0)
        except OSError as e:
            sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1)

        os.setsid()  # @UndefinedVariable - only available in UNIX

        # https://github.com/SiCKRAGETV/sickrage-issues/issues/2969
        # http://www.microhowto.info/howto/cause_a_process_to_become_a_daemon_in_c.html#idp23920
        # https://www.safaribooksonline.com/library/view/python-cookbook/0596001673/ch06s08.html
        # Previous code simply set the umask to whatever it was because it was ANDing instead of ORring
        # Daemons traditionally run with umask 0 anyways and this should not have repercussions
        os.umask(0)

        # Make the child a session-leader by detaching from the terminal
        try:
            pid = os.fork()  # @UndefinedVariable - only available in UNIX
            if pid != 0:
                os._exit(0)
        except OSError as e:
            sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1)

        # Write pid
        if self.CREATEPID:
            pid = str(os.getpid())
            logging.info("Writing PID: " + pid + " to " + str(self.PIDFILE))

            try:
                file(self.PIDFILE, 'w').write("%s\n" % pid)
            except IOError as e:
                logging.log_error_and_exit(
                        "Unable to write PID file: " + self.PIDFILE + " Error: " + str(e.strerror) + " [" + str(
                                e.errno) + "]")

        # Redirect all output
        sys.stdout.flush()
        sys.stderr.flush()

        devnull = getattr(os, 'devnull', '/dev/null')
        stdin = file(devnull, 'r')
        stdout = file(devnull, 'a+')
        stderr = file(devnull, 'a+')

        os.dup2(stdin.fileno(), getattr(sys.stdin, 'device', sys.stdin).fileno())
        os.dup2(stdout.fileno(), getattr(sys.stdout, 'device', sys.stdout).fileno())
        os.dup2(stderr.fileno(), getattr(sys.stderr, 'device', sys.stderr).fileno())
Beispiel #2
0
    def migrate_config(self):
        """
        Calls each successive migration until the config is the same version as SB expects
        """

        if self.config_version > self.expected_config_version:
            logging.log_error_and_exit(
                """Your config version (%i) has been incremented past what this version of SiCKRAGE supports (%i).
                    If you have used other forks or a newer version of SiCKRAGE, your config file may be unusable due to their modifications."""
                % (self.config_version, self.expected_config_version))

        sickbeard.CONFIG_VERSION = self.config_version

        while self.config_version < self.expected_config_version:
            next_version = self.config_version + 1

            if next_version in self.migration_names:
                migration_name = ': ' + self.migration_names[next_version]
            else:
                migration_name = ''

            logging.info("Backing up config before upgrade")
            if not helpers.backupVersionedFile(sickbeard.CONFIG_FILE,
                                               self.config_version):
                logging.log_error_and_exit(
                    "Config backup failed, abort upgrading config")
            else:
                logging.info("Proceeding with upgrade")

            # do the migration, expect a method named _migrate_v<num>
            logging.info("Migrating config up to version " +
                         str(next_version) + migration_name)
            getattr(self, '_migrate_v' + str(next_version))()
            self.config_version = next_version

            # save new config after migration
            sickbeard.CONFIG_VERSION = self.config_version
            logging.info("Saving config file to disk")
            sickbeard.save_config()
Beispiel #3
0
    def migrate_config(self):
        """
        Calls each successive migration until the config is the same version as SB expects
        """

        if self.config_version > self.expected_config_version:
            logging.log_error_and_exit(
                u"""Your config version (%i) has been incremented past what this version of SickRage supports (%i).
                If you have used other forks or a newer version of SickRage, your config file may be unusable due to their modifications.""" %
                (self.config_version, self.expected_config_version)
            )

        sickbeard.CONFIG_VERSION = self.config_version

        while self.config_version < self.expected_config_version:
            next_version = self.config_version + 1

            if next_version in self.migration_names:
                migration_name = ': ' + self.migration_names[next_version]
            else:
                migration_name = ''

            logging.log(u"Backing up config before upgrade")
            if not helpers.backupVersionedFile(sickbeard.CONFIG_FILE, self.config_version):
                logging.log_error_and_exit(u"Config backup failed, abort upgrading config")
            else:
                logging.log(u"Proceeding with upgrade")

            # do the migration, expect a method named _migrate_v<num>
            logging.log(u"Migrating config up to version " + str(next_version) + migration_name)
            getattr(self, '_migrate_v' + str(next_version))()
            self.config_version = next_version

            # save new config after migration
            sickbeard.CONFIG_VERSION = self.config_version
            logging.log(u"Saving config file to disk")
            sickbeard.save_config()
Beispiel #4
0
    def daemonize(self):
        """
        Fork off as a daemon
        """
        # pylint: disable=E1101,W0212
        # An object is accessed for a non-existent member.
        # Access to a protected member of a client class
        # Make a non-session-leader child process
        try:
            pid = os.fork()  # @UndefinedVariable - only available in UNIX
            if pid != 0:
                os._exit(0)
        except OSError as e:
            sys.stderr.write("fork #1 failed: %d (%s)\n" %
                             (e.errno, e.strerror))
            sys.exit(1)

        os.setsid()  # @UndefinedVariable - only available in UNIX

        # https://github.com/SiCKRAGETV/sickrage-issues/issues/2969
        # http://www.microhowto.info/howto/cause_a_process_to_become_a_daemon_in_c.html#idp23920
        # https://www.safaribooksonline.com/library/view/python-cookbook/0596001673/ch06s08.html
        # Previous code simply set the umask to whatever it was because it was ANDing instead of ORring
        # Daemons traditionally run with umask 0 anyways and this should not have repercussions
        os.umask(0)

        # Make the child a session-leader by detaching from the terminal
        try:
            pid = os.fork()  # @UndefinedVariable - only available in UNIX
            if pid != 0:
                os._exit(0)
        except OSError as e:
            sys.stderr.write("fork #2 failed: %d (%s)\n" %
                             (e.errno, e.strerror))
            sys.exit(1)

        # Write pid
        if self.CREATEPID:
            pid = str(os.getpid())
            logging.info("Writing PID: " + pid + " to " + str(self.PIDFILE))

            try:
                file(self.PIDFILE, 'w').write("%s\n" % pid)
            except IOError as e:
                logging.log_error_and_exit("Unable to write PID file: " +
                                           self.PIDFILE + " Error: " +
                                           str(e.strerror) + " [" +
                                           str(e.errno) + "]")

        # Redirect all output
        sys.stdout.flush()
        sys.stderr.flush()

        devnull = getattr(os, 'devnull', '/dev/null')
        stdin = file(devnull, 'r')
        stdout = file(devnull, 'a+')
        stderr = file(devnull, 'a+')

        os.dup2(stdin.fileno(),
                getattr(sys.stdin, 'device', sys.stdin).fileno())
        os.dup2(stdout.fileno(),
                getattr(sys.stdout, 'device', sys.stdout).fileno())
        os.dup2(stderr.fileno(),
                getattr(sys.stderr, 'device', sys.stderr).fileno())