def kill(self, message=""): """Kills the frame""" log.info("Request recieved: kill") if self.frameAttendantThread is None: log.warning( "Kill requested before frameAttendantThread is created " "for: %s" % self.frameId) elif self.frameAttendantThread.isAlive() and self.pid is None: log.warning("Kill requested before pid is available for: %s" % self.frameId) elif self.frameAttendantThread.isAlive(): try: if not self.killMessage and message: self.killMessage = message rqutil.permissionsHigh() try: if platform.system() == "Windows": subprocess.Popen('taskkill /F /T /PID %i' % self.pid, shell=True) else: os.killpg(self.pid, rqconstants.KILL_SIGNAL) finally: rqutil.permissionsLow() except OSError, e: log.warning("kill() tried to kill a non-existant pid for: %s " "Error: %s" % (self.frameId, e)) except Exception, e: log.warning("kill() encountered an unknown error: %s" % e)
def runDarwin(self): """The steps required to handle a frame under mac""" frameInfo = self.frameInfo self.__createEnvVariables() self.__writeHeader() rqutil.permissionsHigh() try: tempCommand = [ "/usr/bin/su", frameInfo.runFrame.user_name, "-c", '"' + self._createCommandFile(frameInfo.runFrame.command) + '"' ] frameInfo.forkedCommand = subprocess.Popen( tempCommand, env=self.frameEnv, cwd=self.rqCore.machine.getTempPath(), stdin=subprocess.PIPE, stdout=self.rqlog, stderr=self.rqlog, preexec_fn=os.setsid) finally: rqutil.permissionsLow() frameInfo.pid = frameInfo.forkedCommand.pid if not self.rqCore.updateRssThread.isAlive(): self.rqCore.updateRssThread = threading.Timer( rqconstants.RSS_UPDATE_INTERVAL, self.rqCore.updateRss) self.rqCore.updateRssThread.start() frameInfo.forkedCommand.wait() # Find exitStatus and exitSignal returncode = frameInfo.forkedCommand.returncode if os.WIFEXITED(returncode): frameInfo.exitStatus = os.WEXITSTATUS(returncode) else: frameInfo.exitStatus = 1 if os.WIFSIGNALED(returncode): frameInfo.exitSignal = os.WTERMSIG(returncode) self.__writeFooter() self.__cleanup()
def _openEvents(self): """Opens the /dev/input/event* files so nimby can monitor them""" self._closeEvents() rqutil.permissionsHigh() try: for device in os.listdir("/dev/input/"): if device.startswith("event") or device.startswith("mice"): log.debug("Found device: %s" % device) try: self.fileObjList.append( open("/dev/input/%s" % device, "rb")) except IOError, e: # Bad device found log.debug("IOError: Failed to open %s, %s" % ("/dev/input/%s" % device, e)) finally: rqutil.permissionsLow()
def __cleanup(self): """Cleans up temporary files""" rqutil.permissionsHigh() try: for location in self._tempLocations: if os.path.isfile(location): try: os.remove(location) except Exception, e: log.warning("Unable to delete file: %s due to %s at %s" % ( location, e, traceback.extract_tb(sys.exc_info()[2]))) finally: rqutil.permissionsLow() # Close log file try: self.rqlog.close() except Exception, e: log.warning("Unable to close file: %s due to %s at %s" % ( self.runFrame.log_file, e, traceback.extract_tb(sys.exc_info()[2])))
def runLinux(self): """The steps required to handle a frame under linux""" frameInfo = self.frameInfo runFrame = self.runFrame self.__createEnvVariables() self.__writeHeader() if rqconstants.RQD_CREATE_USER_IF_NOT_EXISTS: rqutil.permissionsHigh() rqutil.checkAndCreateUser(runFrame.user_name) rqutil.permissionsLow() tempStatFile = "%srqd-stat-%s-%s" % (self.rqCore.machine.getTempPath(), frameInfo.frameId, time.time()) self._tempLocations.append(tempStatFile) tempCommand = [] if self.rqCore.machine.isDesktop(): tempCommand += ["/bin/nice"] tempCommand += ["/usr/bin/time", "-p", "-o", tempStatFile] if 'CPU_LIST' in runFrame.attributes: tempCommand += ['taskset', '-c', runFrame.attributes['CPU_LIST']] rqutil.permissionsHigh() try: tempCommand += ["/bin/su", runFrame.user_name, rqconstants.SU_ARGUEMENT, '"' + self._createCommandFile(runFrame.command) + '"'] # Actual cwd is set by /shots/SHOW/home/perl/etc/qwrap.cuerun frameInfo.forkedCommand = subprocess.Popen(tempCommand, env=self.frameEnv, cwd=self.rqCore.machine.getTempPath(), stdin=subprocess.PIPE, stdout=self.rqlog, stderr=self.rqlog, close_fds=True, preexec_fn=os.setsid) finally: rqutil.permissionsLow() frameInfo.pid = frameInfo.forkedCommand.pid if not self.rqCore.updateRssThread.isAlive(): self.rqCore.updateRssThread = threading.Timer(rqconstants.RSS_UPDATE_INTERVAL, self.rqCore.updateRss) self.rqCore.updateRssThread.start() returncode = frameInfo.forkedCommand.wait() # Find exitStatus and exitSignal if returncode < 0: # Exited with a signal frameInfo.exitStatus = 1 frameInfo.exitSignal = -returncode else: frameInfo.exitStatus = returncode frameInfo.exitSignal = 0 try: statFile = open(tempStatFile,"r") frameInfo.realtime = statFile.readline().split()[1] frameInfo.utime = statFile.readline().split()[1] frameInfo.stime = statFile.readline().split()[1] statFile.close() except Exception: pass # This happens when frames are killed self.__writeFooter() self.__cleanup()