Exemplo n.º 1
0
    def start(self):
        """
        Load config, daemonize, connect to serial port, listen on socket
        """
        opensyslog(ident = self.name, facility = LOG_DAEMON)
        
        self.__load_config()
        if self.pidfile_path is not None:
            self.daemon_context.pidfile = lockfile.FileLock(self.pidfile_path)
            
        if _pidfile_isbusy(self.daemon_context.pidfile):
            logsyslog(LOG_ERR, 'Already running (pidfile is locked)')
            closesyslog()
            return
        
        if _socket_isbusy(self.socket_path):
            logsyslog(LOG_ERR, 'Already running (socket is in use)')
            closesyslog()
            return
        
        self.daemon_context.open()
        with _openfile(self.daemon_context.pidfile.path, 'w',
                      fail = self.__stop) as file:
            file.write('{pid}'.format(pid = os.getpid()))
        # opening the serial port here doesn't work
        # open it in __run instead
        # self.serial_context.open()
        logsyslog(LOG_INFO, 'Started')

        self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        self.socket.bind(self.socket_path)
        self.socket.listen(1)
        logsyslog(LOG_INFO, ('Listening on socket {socket}').format(
            socket = self.socket_path))
        self.__run()
Exemplo n.º 2
0
    def __stop(self):
        pid = _get_pid(self.daemon_context.pidfile.path)
        if pid is None:
            return

        logsyslog(LOG_INFO, 'Stopping')
        
        os.remove(self.socket.getsockname())
        os.remove(self.daemon_context.pidfile.path)
        
        self.socket.close()
        
        if self.serial_context.isOpen():
            self.serial_context.close()
        self.daemon_context.close()
        
        try:
            os.kill(pid, signal.SIGKILL)
        except OSError:
            logsyslog(LOG_ERR, 'Could not stop process id {pid}'.format(
                pid = pid))
        closesyslog()