<commit_before>#!/usr/bin/env python

import sys, os, time, atexit
import traceback
import signal
from zstacklib.utils import log
from zstacklib.utils import shell

logger = log.get_logger(__name__)

class Daemon(object):
    """
    A generic daemon class.
    
    Usage: subclass the Daemon class and override the run() method
    """
    atexit_hooks = []
    
    def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr
        self.pidfile = pidfile
    
    @staticmethod
    def register_atexit_hook(hook):
        Daemon.atexit_hooks.append(hook)
        
    @staticmethod
    def _atexit():
        for hook in Daemon.atexit_hooks:
            try:
                hook()
            except Exception:
                content = traceback.format_exc()
                err = 'Exception when calling atexit hook[%s]\n%s' % (hook.__name__, content)
                logger.error(err)
                
    def daemonize(self):
        """
        do 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
        """
        try: 
            pid = os.fork() 
            if pid > 0:
                # exit first parent
                sys.exit(0) 
        except OSError, e: 
            sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1)
    
        # decouple from parent environment
        os.chdir("/") 
        os.setsid() 
        os.umask(0) 
    
        # do second fork
        try: 
            pid = os.fork() 
            if pid > 0:
                # exit from second parent
                sys.exit(0) 
        except OSError, e: 
            sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1) 
    
        # reap child automatically
        signal.signal(signal.SIGCHLD, signal.SIG_IGN)

        # 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 pidfile
        Daemon.register_atexit_hook(self.delpid)
        atexit.register(Daemon._atexit)
        pid = str(os.getpid())
        file(self.pidfile,'w').write("%s\n" % pid)
    
    def delpid(self):
        os.remove(self.pidfile)

    def start(self):
        """
        Start the daemon
        """
        print "Start Daemon..."
        # Check for a pidfile to see if the daemon already runs
        try:
            pf = file(self.pidfile,'r')
            pid = int(pf.read().strip())
            pf.close()
        except IOError:
            pid = None
    
        if pid:
            pscmd = shell.ShellCmd('ps -p %s > /dev/null' % pid)
            pscmd(is_exception=False)
            if pscmd.return_code == 0:
                message = "Daemon already running, pid is %s\n"
                sys.stderr.write(message % pid)
                sys.exit(0)
        
        # Start the daemon
        self.daemonize()
        try:
            self.run()
        except Exception:
            content = traceback.format_exc()
            logger.error(content)
            sys.exit(1)

        print "Start Daemon Successfully"

    def stop(self):
        """
        Stop the daemon
        """
        print "Stop Daemon..."
        #wait 2s for gracefully shutdown, then will force kill
        wait_stop = 2
        # Get the pid from the pidfile
        try:
            pf = file(self.pidfile,'r')
            pid = int(pf.read().strip())
            pf.close()
        except IOError:
            pid = None
    
        if not pid:
            message = "pidfile %s does not exist. Daemon not running?\n"
            sys.stderr.write(message % self.pidfile)
            return # not an error in a restart

        # Try killing the daemon process    
        start_time = time.time()
        while 1:
            if os.path.exists('/proc/' + str(pid)):
                curr_time = time.time()
                if (curr_time - start_time) > wait_stop:
                    os.kill(pid, signal.SIGKILL)
                else:
                    os.kill(pid, signal.SIGTERM)
                time.sleep(0.3)
            else:
                if os.path.exists(self.pidfile):
                    self.delpid()
                break

        print "Stop Daemon Successfully"<commit_msg>Revert "Ignore SIGCHLD to reap child process automatically"<commit_after>#!/usr/bin/env python

import sys, os, time, atexit
import traceback
from signal import SIGTERM,SIGKILL 
from zstacklib.utils import log
from zstacklib.utils import shell

logger = log.get_logger(__name__)

class Daemon(object):
    """
    A generic daemon class.
    
    Usage: subclass the Daemon class and override the run() method
    """
    atexit_hooks = []
    
    def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr
        self.pidfile = pidfile
    
    @staticmethod
    def register_atexit_hook(hook):
        Daemon.atexit_hooks.append(hook)
        
    @staticmethod
    def _atexit():
        for hook in Daemon.atexit_hooks:
            try:
                hook()
            except Exception:
                content = traceback.format_exc()
                err = 'Exception when calling atexit hook[%s]\n%s' % (hook.__name__, content)
                logger.error(err)
                
    def daemonize(self):
        """
        do 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
        """
        try: 
            pid = os.fork() 
            if pid > 0:
                # exit first parent
                sys.exit(0) 
        except OSError, e: 
            sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1)
    
        # decouple from parent environment
        os.chdir("/") 
        os.setsid() 
        os.umask(0) 
    
        # do second fork
        try: 
            pid = os.fork() 
            if pid > 0:
                # exit from second parent
                sys.exit(0) 
        except OSError, e: 
            sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1) 
    
        # 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 pidfile
        Daemon.register_atexit_hook(self.delpid)
        atexit.register(Daemon._atexit)
        pid = str(os.getpid())
        file(self.pidfile,'w').write("%s\n" % pid)
    
    def delpid(self):
        os.remove(self.pidfile)

    def start(self):
        """
        Start the daemon
        """
        print "Start Daemon..."
        # Check for a pidfile to see if the daemon already runs
        try:
            pf = file(self.pidfile,'r')
            pid = int(pf.read().strip())
            pf.close()
        except IOError:
            pid = None
    
        if pid:
            pscmd = shell.ShellCmd('ps -p %s > /dev/null' % pid)
            pscmd(is_exception=False)
            if pscmd.return_code == 0:
                message = "Daemon already running, pid is %s\n"
                sys.stderr.write(message % pid)
                sys.exit(0)
        
        # Start the daemon
        self.daemonize()
        try:
            self.run()
        except Exception:
            content = traceback.format_exc()
            logger.error(content)
            sys.exit(1)

        print "Start Daemon Successfully"

    def stop(self):
        """
        Stop the daemon
        """
        print "Stop Daemon..."
        #wait 2s for gracefully shutdown, then will force kill
        wait_stop = 2
        # Get the pid from the pidfile
        try:
            pf = file(self.pidfile,'r')
            pid = int(pf.read().strip())
            pf.close()
        except IOError:
            pid = None
    
        if not pid:
            message = "pidfile %s does not exist. Daemon not running?\n"
            sys.stderr.write(message % self.pidfile)
            return # not an error in a restart

        # Try killing the daemon process    
        start_time = time.time()
        while 1:
            if os.path.exists('/proc/' + str(pid)):
                curr_time = time.time()
                if (curr_time - start_time) > wait_stop:
                    os.kill(pid, SIGKILL)
                else:
                    os.kill(pid, SIGTERM)
                time.sleep(0.3)
            else:
                if os.path.exists(self.pidfile):
                    self.delpid()
                break

        print "Stop Daemon Successfully"
{'fraction_non_alphanumeric': 0.11009758167161646, 'fraction_numerical': 0.007212558336868901, 'mean_word_length': 1.4754528747702809, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7373592', 'n_tokens_mistral': 2924, 'n_tokens_neox': 2772, 'n_words': 693}