Coverage for src/lib/python/isc/bind10/sockcreator : 82%
        
        
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
| 
 # Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC") # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 
 
 """ Module that comunicates with the privileged socket creator (b10-sockcreator). """ 
 """ Exception for socket creator related errors. 
 It has two members: fatal and errno and they are just holding the values passed to the __init__ function. """ 
 """ Creates the exception. The message argument is the usual string. The fatal one tells if the error is fatal (eg. the creator crashed) and errno is the errno value returned from socket creator, if applicable. """ 
 """ This class knows the sockcreator language. It creates commands, sends them and receives the answers and parses them. 
 It does not start it, the communication channel must be provided. 
 In theory, anything here can throw a fatal CreatorError exception, but it happens only in case something like the creator process crashes. Any other occasions are mentioned explicitly. """ 
 """ Creates the parser. The creator_socket is socket to the socket creator process that will be used for communication. However, the object must have a read_fd() method to read the file descriptor. This slightly unusual trick with modifying an object is used to easy up testing. 
 You can use WrappedSocket in production code to add the method to any ordinary socket. """ 
 """ Asks the creator process to terminate and waits for it to close the socket. Does not return anything. Raises a CreatorError if there is still data on the socket, if there is an error closing the socket, or if the socket had already been closed. """ # Wait for an EOF - it will return empty data True) 
 """ Asks the socket creator process to create a socket. Pass an address (the isc.net.IPaddr object), port number and socket type (either string "UDP", "TCP" or constant socket.SOCK_DGRAM or socket.SOCK_STREAM. 
 Blocks until it is provided by the socket creator process (which should be fast, as it is on localhost) and returns the file descriptor number. It raises a CreatorError exception if the creation fails. """ # First, assemble the request from parts else: else: raise ValueError('Unknown address family in address') # Send the request # Success! # There was an error, read the error as well self.__read_all(len(struct.pack('i', 0)))) elif error == b'B': cause = 'bind' else: self.__socket = None logger.fatal(BIND10_SOCKCREATOR_BAD_CAUSE, error) raise CreatorError('Unknown error cause' + str(answer), True) os.strerror(errno[0])) errno[0]) else: 
 """ Keeps reading until length data is read or EOF or error happens. 
 EOF is considered error as well and throws a CreatorError. """ self.__socket = None logger.fatal(BIND10_SOCKCREATOR_EOF) raise CreatorError('Unexpected EOF', True) 
 """ This class wraps a socket and adds a read_fd method, so it can be used for the Parser class conveniently. It simply copies all its guts into itself and implements the method. """ # Copy whatever can be copied from the socket # Keep the socket, so we can prevent it from being garbage-collected # and closed before we are removed ourself 
 """ Read the file descriptor from the socket. """ 
 # FIXME: Any idea how to test this? Starting an external process doesn't sound # OK """ This starts the socket creator and allows asking for the sockets. 
 Note: __process shouldn't be reset once created. See the note of the SockCreator class for details. """ (local, remote) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM) # Popen does not like, for some reason, having the same socket for # stdin as well as stdout, so we dup it before passing it there. remote2 = socket.fromfd(remote.fileno(), socket.AF_UNIX, socket.SOCK_STREAM) env = copy.deepcopy(os.environ) env['PATH'] = path self.__process = subprocess.Popen(['b10-sockcreator'], env=env, stdin=remote.fileno(), stdout=remote2.fileno(), preexec_fn=self.__preexec_work) remote.close() remote2.close() Parser.__init__(self, WrappedSocket(local)) 
 """Function used before running a program that needs to run as a different user.""" # Put us into a separate process group so we don't get # SIGINT signals on Ctrl-C (the boss will shut everthing down by # other means). os.setpgrp() 
 return self.__process.pid 
 logger.warn(BIND10_SOCKCREATOR_KILL) if self.__process is not None: self.__process.kill()  |