def _AddFd(self, fd): """Wrapper around add-fd qmp command Use fdsend to send fd to a running process via SCM_RIGHTS and then add-fd qmp command to add it to an fdset so that it can be used later by disk hotplugging. @type fd: int @param fd: The file descriptor to pass @return: The fdset ID that the fd has been added to @raise errors.HypervisorError: If add-fd fails for some reason """ self._check_connection() try: fdsend.sendfds(self.sock, " ", fds=[fd]) # Omit fdset-id and let qemu create a new one (see qmp-commands.hx) response = self.Execute("add-fd") fdset = response["fdset-id"] except errors.HypervisorError as err: logging.info("Passing fd %s via SCM_RIGHTS failed: %s", fd, err) raise return fdset
def AddFd(self, fds): """Pass file descriptor to kvm process via qmp socket using SCM_RIGHTS Add the fds to an fdset so that they can be used later by hot-add commands @type fds: list @param fds: The list of file descriptors to pass @return: The fdset ID that the fds have been added to (None if operation fails) """ self._check_connection() if not fdsend or "add-fd" not in self.supported_commands: return None try: # Omit fdset-id and let qemu create a new one (see qmp-commands.hx) command = {"execute": "add-fd"} fdsend.sendfds(self.sock, serializer.Dump(command), fds=fds) # Get the response out of the buffer response = self._GetResponse("add-fd") fdset = response["fdset-id"] logging.info("Sent fds %s and added to fdset %s", fds, fdset) except errors.HypervisorError, err: # In case _GetResponse() fails fdset = None logging.info("Sending fds %s failed: %s", fds, err)
def GetFd(self, fds, kvm_devid): """Pass file descriptor to kvm process via monitor socket using SCM_RIGHTS """ self._check_connection() command = "getfd %s\n" % kvm_devid logging.info("Passing %s fds to %s", fds, self.monitor_filename) fdsend.sendfds(self.sock, command, fds=fds)
def send_data(self): """Creates a new UNIX socket named `sock_fn` and a temporary file and transmits some data and the file descriptor of the temporary file through that socket. """ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.bind(self.sock_fn) sock.listen(1) client_sock, _ = sock.accept() sock.close() tmp_fp = open(os.path.join(self.temp_dir, 'somefile'), 'w+') tmp_fp.write(self.FILE_DATA) tmp_fp.flush() fdsend.sendfds(client_sock, self.DIRECT_DATA, fds=[tmp_fp]) tmp_fp.close() # tmp_fp's file will be cleaned up by deleting the temporary directory. client_sock.close()
def _GetFd(self, fd, fdname): """Wrapper around the getfd qmp command Use fdsend to send an fd to a running process via SCM_RIGHTS and then use the getfd qmp command to name it properly so that it can be used later by NIC hotplugging. @type fd: int @param fd: The file descriptor to pass @raise errors.HypervisorError: If getfd fails for some reason """ self._check_connection() try: fdsend.sendfds(self.sock, " ", fds=[fd]) arguments = { "fdname": fdname, } self.Execute("getfd", arguments) except errors.HypervisorError, err: logging.info("Passing fd %s via SCM_RIGHTS failed: %s", fd, err) raise
def _AddFd(self, fd): """Wrapper around add-fd qmp command Use fdsend to send fd to a running process via SCM_RIGHTS and then add-fd qmp command to add it to an fdset so that it can be used later by disk hotplugging. @type fd: int @param fd: The file descriptor to pass @return: The fdset ID that the fd has been added to @raise errors.HypervisorError: If add-fd fails for some reason """ self._check_connection() try: fdsend.sendfds(self.sock, " ", fds=[fd]) # Omit fdset-id and let qemu create a new one (see qmp-commands.hx) response = self.Execute("add-fd") fdset = response["fdset-id"] except errors.HypervisorError, err: logging.info("Passing fd %s via SCM_RIGHTS failed: %s", fd, err) raise
def run(): if getpass.getuser() != "mytardis" or "SUDO_USER" not in os.environ: print "Usage: sudo -u mytardis _datafiledescriptord " + \ "mytardis_install_dir auth_provider " + \ "socket_path exp_id datafile_id" sys.exit(1) if len(sys.argv) < 6: print "Usage: sudo -u mytardis _datafiledescriptord " + \ "mytardis_install_dir auth_provider " + \ "socket_path exp_id datafile_id" sys.exit(1) _mytardis_install_dir = sys.argv[1].strip('"') _auth_provider = sys.argv[2] _socket_path = sys.argv[3] _experiment_id = int(sys.argv[4]) _datafile_id = int(sys.argv[5]) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: os.remove(_socket_path) except OSError: pass sock.bind(_socket_path) os.chmod(_socket_path, 0666) sock.listen(1) conn, addr = sock.accept() utils.setup_mytardis_paths(_mytardis_install_dir) from tardis.tardis_portal.models import Dataset_File, Experiment found_user = False mytardis_user = None exp_public = False exp_owned_or_shared = False staff_or_superuser = False found_datafile_in_experiment = False try: mytardis_user = utils.get_user(os.environ['SUDO_USER'], _auth_provider) # logger.debug("Primary MyTardis username: "******"Success" elif not found_datafile_in_experiment: fds = [] message = "Datafile (ID %s) does not belong " + \ "to experiment (ID %s)." % \ (str(_datafile_id), str(_experiment_id)) else: fds = [] # message = "Access to datafile %s denied for user %s." % # (str(_datafile_id),os.environ['SUDO_USER']) message = "Access denied for user " + \ os.environ['SUDO_USER'] + " " + \ str(sys.argv) fdsend.sendfds(conn, message, fds=fds) except ObjectDoesNotExist: message = "User " + os.environ['SUDO_USER'] + \ " was not found in MyTardis." fdsend.sendfds(conn, message, fds=[]) except: message = traceback.format_exc() fdsend.sendfds(conn, message, fds=[]) conn.close() try: os.remove(_socket_path) except OSError: pass
def run(): if getpass.getuser() != "mytardis" or "SUDO_USER" not in os.environ: print "Usage: sudo -u mytardis _datafiledescriptord " + \ "mytardis_install_dir auth_provider " + \ "socket_path exp_id datafile_id" sys.exit(1) if len(sys.argv) < 6: print "Usage: sudo -u mytardis _datafiledescriptord " + \ "mytardis_install_dir auth_provider " + \ "socket_path exp_id datafile_id" sys.exit(1) _mytardis_install_dir = sys.argv[1].strip('"') _auth_provider = sys.argv[2] _socket_path = sys.argv[3] _experiment_id = int(sys.argv[4]) _datafile_id = int(sys.argv[5]) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: os.remove(_socket_path) except OSError: pass sock.bind(_socket_path) os.chmod(_socket_path, 0666) sock.listen(1) conn, addr = sock.accept() sys.path.append(_mytardis_install_dir) for egg in os.listdir(os.path.join(_mytardis_install_dir, "eggs")): sys.path.append(os.path.join(_mytardis_install_dir, "eggs", egg)) os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'tardis.settings') from tardis.tardis_portal.models import DataFile, Experiment from tardis.tardis_portal.models import UserAuthentication found_user = False mytardis_user = None exp_public = False exp_owned_or_shared = False staff_or_superuser = False found_datafile_in_experiment = False try: user_auth = UserAuthentication.objects\ .get(username=os.environ['SUDO_USER'], authenticationMethod=_auth_provider) mytardis_user = user_auth.userProfile.user # logger.debug("Primary MyTardis username: "******"Success" elif not found_datafile_in_experiment: fds = [] message = "Datafile (ID %s) does not belong " + \ "to experiment (ID %s)." % \ (str(_datafile_id), str(_experiment_id)) else: fds = [] # message = "Access to datafile %s denied for user %s." % # (str(_datafile_id),os.environ['SUDO_USER']) message = "Access denied for user " + \ os.environ['SUDO_USER'] + " " + \ str(sys.argv) fdsend.sendfds(conn, message, fds=fds) except ObjectDoesNotExist: # FIXME: It could actually be the datafile which # is not found. message = "User " + os.environ['SUDO_USER'] + \ " was not found in MyTardis " \ "for authentication method " + _auth_provider fdsend.sendfds(conn, message, fds=[]) except: message = traceback.format_exc() fdsend.sendfds(conn, message, fds=[]) conn.close() try: os.remove(_socket_path) except OSError: pass