Пример #1
0
	def _create_local_socket(self, init=0):
		"""Create local Unix-socket to listen commands instead of signals. If
		I{init} is set then close old socket"""

		if init and self.__local_sock:
			try:
				os.unlink(self.__local_sock.getsockname())
				self.__local_sock.shutdown(socket.SHUT_RDWR)
				logging.info('old local socket closed')
			except Exception as e:
				logging.warn('old local socket is already closed')

		try:
			os.unlink(self._config['server']['sock'])
		except:
			pass

		try:
			self.__local_sock = \
				socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
			# Non-blocking mode
			self.__local_sock.settimeout(0.0)
			self.__local_sock.bind(self._config['server']['sock'])
			self.__local_sock.listen(1)		# Max num of queued connections
		except Exception as e:
			logging.critical('cannot create local socket')
			logging.error(e)
			raise e

		logging.info('local socket created and binded on %s' % \
			self.__local_sock.getsockname())
Пример #2
0
	def stop(self):
		logging.info('stopping daemon')
		# Get the pid from the pidfile
		try:
			pf = open(self.pidfile, 'r')
			pid = int(pf.read().strip())
			pf.close()
		except IOError:
			pid = None

		#print (sys.stderr, sys.stdout)
		if not pid:
			logging.warn('daemon is already stopped')
			sys.stderr.write('daemon is already stopped\n')
			return

		# Try killing the daemon process
		try:
			while 1:
				os.kill(pid, signal.SIGTERM)
				sleep(0.1)
		except OSError as err:
			# PID file is present, but no process!
			err = '%s' %err
			if err.find('No such process') >0:
				if os.path.isfile(self.pidfile):
					os.remove(self.pidfile)
			else:
				sys.stderr.write(err)
				sys.exit(1)
Пример #3
0
	def handle_local_request(self):
		"""Accept local socket connections"""

		c = None
		is_quit = 0
		try:
			# Are we have connection try?
			r, w, e = select.select([self.__local_sock.fileno()], [], [],
				self._config['server']['wait'])
			if not r:
				return
			c, addr, = self.__local_sock.accept()
			logging.info('local connection accepted')

			# Check if socket is ready for reading
			msg = b''
			r, w, e = select.select([c.fileno()], [], [], 0)
			if r:
				msg = c.recv(32, socket.MSG_DONTWAIT)
			else:
				logging.warn('socket not ready for reading')
				raise socket.error(100, 'socket not ready for reading')

			reply = b'unknown command'
			if msg == b'config':
				logging.info('request server configuration')
				reply = cpickle_dumps(self._config)
			elif msg == b'status':
				logging.info('request server status')
				reply = b'started'
			elif msg == b'shutdown':
				logging.info('request server shutdown')
				is_quit = 1
				reply = b'stopped'
			elif not msg:
				logging.info('no request')
			else:
				logging.info('unknown request')

			# Check if socket is ready for writing
			r, w, e = select.select([], [c.fileno()], [], 0)
			if w:
				c.send(reply, socket.MSG_DONTWAIT)
			else:
				logging.warn('socket not ready for writing')
				raise socket.error(101, 'socket not ready for writing')

		except Exception as e:
			logging.error(e)

		if c and not c._closed:
			c.close()
			logging.info('local connection closed')

		return is_quit
Пример #4
0
	def handle_sigusr2(self, signum, frame):
		"""Handle signal SIGUSR2 to reload aliases configuration"""

		logging.info('reload aliases configuration (by signal)')
		# Catch received signal in the I{main} method
		self.__by_signal_do = 1

		aliases = get_aliases(self._config['get']['aliases_file'])
		try:
			check_aliases(self._config['get']['base_dir'], aliases)
			self._aliases = aliases.copy()
			logging.info('aliases configuration reloaded')
		except Exception as e:
			logging.warn('aliases configuration error')
		del aliases