Пример #1
0
	def close( self ):
		'''Dispose.'''
		log.debug("Closing...")
		if self.disposed:
			log.debug("Already closed.")
			return
		self.disposed = True
		if os.path.exists( str( self.sockfile ) ):
			log.info( "Stopping server: unlinking socket file..." )
			os.unlink( self.sockfile )
		log.debug("Closed OK.")
Пример #2
0
	def stop(self, sock, mask):
		'''Regd stop handler. The program is normally stopped only through this.
		It's called when the sigsocket receives input ( including notification 
		about a signal ). '''
		log.info("Stopping server...")
		if app.glCont:
			app.glCont = False
			app.glSignal.acquire()
			app.glSignal.notify()
			app.glSignal.release()
		self.close()
Пример #3
0
def startRegistry(servername,
                  sockfile=None,
                  host=None,
                  port=None,
                  acc=defs.PL_PRIVATE,
                  datafile=None,
                  binsecfile=None):
    srv = connStor = None

    def shutdown():
        log.info("Registry is shutting down...")
        cmds.CmdSwitcher.switchCmd({"cmd": fs.FS_STOP})
        log.info("Shutdown complete.")

    def signal_handler(signal, frame):
        #shutdown()
        # Returning normally, so that the signal notification be forwarded to
        # the wakeup socket.
        return

    sigHandler.push(signal.SIGINT, signal_handler)
    sigHandler.push(signal.SIGTERM, signal_handler)
    sigHandler.push(signal.SIGHUP, signal_handler)
    sigHandler.push(signal.SIGABRT, signal_handler)
    sigHandler.push(signal.SIGALRM, signal_handler)

    try:
        # Storage
        log.debug("Creating storage")
        connStor, sigStor = fs.startStorage(acc, datafile, binsecfile)
        # Server
        log.debug("Creating server instance")
        srv = serv.RegdServer(servername, sockfile, host, port, acc)
        # Info
        log.debug("Creating info")
        info.Info()
        log.debug("Starting server")
        srv.start_loop(sigStor)
    except Exception as e:
        log.error("Failed to start server: {0}".format(e))
    else:
        #glSignal.acquire()
        # Wait till notification on df.SERVER_STOP
        #glSignal.wait()
        shutdown()
        log.info("Exiting.")
Пример #4
0
    def start_loop(conn, sigConn, acc, datafile, binsectfile):
        '''Create FS instance and start loop'''
        util.setLog("DEBUG")
        fs = FS(conn, acc, datafile, binsectfile)
        log.info("Starting listening for messages...")
        try:
            fs.listenForMessages(fs.serialize)
        except Exception as e:
            log.error("Exception received: {0}".format(e))
            #fs.conn.shutdown(SHUT_RDWR)
        conn.close()
        sigConn.send("Exception in storage".encode())
        sigConn.shutdown(SHUT_RDWR)
        sigConn.close()

        log.info("Ended listening for messages. Quitting...")
        return 0
Пример #5
0
    def startServer(self):
        res, _ = self.checkServer()
        if res:
            return False, "Server with such name is already running."

        log.info("Creating registry %s" % self.servName)
        try:
            args = [rc, defs.START_SERVER]
            if self.servName:
                args.append(clp(defs.SERVER_NAME))
                args.append(self.servName)
            if self.host:
                args.append("--host")
                args.append(self.host)
            if self.port:
                args.append("--port")
                args.append(self.port)
            if self.acc:
                args.append("--access")
                args.append(self.acc)
            if self.datafile:
                args.append("--datafile")
                args.append(self.datafile)

            sp.Popen(args)
            time.sleep(2)
            res = True
            ret = "Server started successfully."
        except sp.CalledProcessError as e:
            res = False
            ret = "{0} {1} {2} {3}".format(rc, defs.START_SERVER,
                                           "returned non-zero:", e.output)

        if res:
            res, _ = self.checkServer()
            if not res:
                return False, "Server failed to start."

        return res, ret
Пример #6
0
def startStorage(acc, datafile, binsectfile):
    '''Starts storage in new process'''
    connHere, connThere = Pipe(True)
    sigHere, sigThere = socketpair()
    sigHere.setblocking(False)
    sigThere.setblocking(False)
    os.set_inheritable(sigThere.fileno(), True)
    util.connLock = Lock()

    def sendMsgToStorage(cmd):
        '''Forwarder of messages to storage'''
        log.debug("In sendMsgToStorage - cmd: {0}".format(cmd))
        try:
            if not util.connLock.acquire(True, 5):
                raise IKException(
                    ErrorCode.operationFailed,
                    "Failed to acquire the lock for connecting to storage.")

            log.debug("Sending...")
            connHere.send(cmd)
            log.debug("Sent. Receiving...")
            if connHere.poll(15):
                ret = connHere.recv()
                log.debug("In sendMsgToStorage - ret: {0}".format(ret))
            else:
                ret = composeResponse(
                    "0", "Socket timed out or no data to receive.")
                log.debug("Nothing to receive")

            util.connLock.release()
        except Exception as e:
            ret = composeResponse("0", str(e))
            log.error(
                "In sendMsgToStorage exception received: {0}".format(ret))

        return ret

    FS.registerGroupHandlers(sendMsgToStorage)

    log.info("Starting storage process...")
    # Timeout for sending from fs back to connectionHandler
    #connThere.settimeout( 5 )
    p = Process(target=FS.start_loop,
                args=(connThere, sigThere, acc, datafile, binsectfile),
                name="Regd Storage")
    p.start()
    time.sleep(1)
    if p.is_alive():
        log.info("Storage started OK.")
    else:
        log.info("Failed to start storage.")
        raise IKException(ErrorCode.operationFailed,
                          "Failed to start storage.")

    return connHere, sigHere
Пример #7
0
 def shutdown():
     log.info("Registry is shutting down...")
     cmds.CmdSwitcher.switchCmd({"cmd": fs.FS_STOP})
     log.info("Shutdown complete.")
Пример #8
0
	def start_loop( self, sigStor ):
		'''Start loop.'''

		# Check for the previous instance

		if not self.host and os.path.exists( self.sockfile ):
			log.info( "Socket file for a server with name already exists. Checking for the server process." )
			'''Socket file may remain after an unclean exit. Check if another server is running.'''
			try:
				# If the server is restarted, give the previous instance time to exit cleanly.
				time.sleep( 2 )
				if self.servername and self.servername != "regd":
					s = "ps -ef | grep '{0}(/cli.py)? start .*{1} {2}' | grep -v grep".format( 
												app.APPNAME,
												app.clp( defs.SERVER_NAME ),
												self.servername )
				else:
					s = "ps -ef | grep -E '{0}(/cli.py)? start' | grep -v '{1}' | grep -v grep".format( 
												app.APPNAME,
												app.clc( defs.SERVER_NAME ) )


				res = subprocess.check_output( s, shell = True ).decode( 'utf-8' )

			except subprocess.CalledProcessError as e:
				if e.returncode != 1:
					log.error( "Check for already running server instance failed: {0} ".format( e.output ) )
					return -1
				else:
					res = ""

			if len( res ):
				# TODO
				if res.count( "\n" ) > 2:
					'''Server is already running.'''
					log.warning( "Server is already running:\n{0}".format( res ) )
					return 1
			log.info( "Server process is not found. Unlinking the existing socket file." )
			try:
				os.unlink( self.sockfile )
			except OSError:
				if os.path.exists( self.sockfile ):
					raise

		self.useruid = os.getuid()
		if self.host:
			log.info( "Starting regd server. useruid: {0} ; host: {1} ; port: {2}.".format( 
															self.useruid, self.host, self.port ) )
		else:
			log.info( "Starting regd server. useruid: {0} ; sockfile: {1} ; servername: {2}.".format( 
												self.useruid, self.sockfile, self.servername ) )
		self.info["time_started"] = str( datetime.datetime.now() ).rpartition( "." )[0]
		
		# Set up sockets
		try:
			if self.host:
				self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
				self.sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
				self.sock.bind( ( self.host, int( self.port ) ) )
				with open( self.sockfile, "w" ) as f:
					f.write( '' )

			else:
				self.sock = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM )
				self.sock.bind( self.sockfile )
				os.chmod( self.sockfile, mode = 0o777 )

		except OSError as e:
			log.error( "Cannot create or bind socket: %s" % ( e ) )
			return -1

		self.sock.listen( 1 )
		self.sock.settimeout( 30 )
		self.sel = selectors.DefaultSelector()
		self.sigsock_r, self.sigsock_w = socket.socketpair()
		self.sigsock_r.setblocking( False )
		self.sigsock_w.setblocking( False )
		os.set_inheritable( self.sigsock_w.fileno(), True )
		self.sock.setblocking( False )
		signal.set_wakeup_fd( self.sigsock_w.fileno() )
		self.sel.register( self.sock, selectors.EVENT_READ, self.accept )
		self.sel.register( self.sigsock_r, selectors.EVENT_READ, self.stop )
		self.sel.register( sigStor, selectors.EVENT_READ, self.stop )
		self.loop( self.sock, )
Пример #9
0
 def chFsStop(self, cmd):
     self.cont = False
     log.info("Stopping storage... Serializing...")
     self.serialize()
     log.info("Storage exiting.")
     return composeResponse()