def _reinitScratch(self, fnLog, fUseTheForce): """ Wipes the scratch directories and re-initializes them. No exceptions raise, returns success indicator instead. """ if fUseTheForce is None: fUseTheForce = self._fFirstSignOn; class ErrorCallback(object): # pylint: disable=R0903 """ Callbacks + state for the cleanup. """ def __init__(self): self.fRc = True; def onErrorCallback(self, sFnName, sPath, aXcptInfo): """ Logs error during shutil.rmtree operation. """ fnLog('Error removing "%s": fn=%s %s' % (sPath, sFnName, aXcptInfo[1])); self.fRc = False; oRc = ErrorCallback(); # # Cleanup. # for sName in os.listdir(self._oOptions.sScratchRoot): sFullName = os.path.join(self._oOptions.sScratchRoot, sName); try: if os.path.isdir(sFullName): shutil.rmtree(sFullName, False, oRc.onErrorCallback); else: os.remove(sFullName); if os.path.exists(sFullName): raise Exception('Still exists after deletion, weird.'); except Exception, oXcpt: if fUseTheForce is True \ and utils.getHostOs() not in ['win', 'os2'] \ and len(sFullName) >= 8 \ and sFullName[0] == '/' \ and sFullName[1] != '/' \ and sFullName.find('/../') < 0: fnLog('Problems deleting "%s" (%s) using the force...' % (sFullName, oXcpt)); try: if os.path.isdir(sFullName): iRc = utils.sudoProcessCall(['/bin/rm', '-Rf', sFullName]) else: iRc = utils.sudoProcessCall(['/bin/rm', '-f', sFullName]) if iRc != 0: raise Exception('exit code %s' % iRc); if os.path.exists(sFullName): raise Exception('Still exists after forced deletion, weird^2.'); except: fnLog('Error sudo deleting "%s": %s' % (sFullName, oXcpt)); oRc.fRc = False; else: fnLog('Error deleting "%s": %s' % (sFullName, oXcpt)); oRc.fRc = False;
def reinitScratch(self, fnLog = testboxcommons.log, fUseTheForce = None): """ Wipes the scratch directories and re-initializes them. No exceptions raise, returns success indicator instead. """ if fUseTheForce is None: fUseTheForce = self._fFirstSignOn; class ErrorCallback(object): # pylint: disable=R0903 """ Callbacks + state for the cleanup. """ def __init__(self): self.fRc = True; def onErrorCallback(self, sFnName, sPath, aXcptInfo): """ Logs error during shutil.rmtree operation. """ fnLog('Error removing "%s": fn=%s %s' % (sPath, sFnName, aXcptInfo[1])); self.fRc = False; oRc = ErrorCallback(); # # Cleanup. # for sName in os.listdir(self._oOptions.sScratchRoot): sFullName = os.path.join(self._oOptions.sScratchRoot, sName); try: if os.path.isdir(sFullName): shutil.rmtree(sFullName, False, oRc.onErrorCallback); else: os.remove(sFullName); if os.path.exists(sFullName): raise Exception('Still exists after deletion, weird.'); except Exception, oXcpt: if fUseTheForce is True \ and utils.getHostOs() not in ['win', 'os2'] \ and len(sFullName) >= 8 \ and sFullName[0] == '/' \ and sFullName[1] != '/' \ and sFullName.find('/../') < 0: fnLog('Problems deleting "%s" (%s) using the force...' % (sFullName, oXcpt)); try: if os.path.isdir(sFullName): iRc = utils.sudoProcessCall(['/bin/rm', '-Rf', sFullName]) else: iRc = utils.sudoProcessCall(['/bin/rm', '-f', sFullName]) if iRc != 0: raise Exception('exit code %s' % iRc); if os.path.exists(sFullName): raise Exception('Still exists after forced deletion, weird^2.'); except: fnLog('Error sudo deleting "%s": %s' % (sFullName, oXcpt)); oRc.fRc = False; else: fnLog('Error deleting "%s": %s' % (sFullName, oXcpt)); oRc.fRc = False;
def _sudoExecuteSync(self, asArgs): """ Executes a sudo child process synchronously. Returns True if the process executed successfully and returned 0, otherwise False is returned. """ reporter.log('Executing [sudo]: %s' % (asArgs, )); try: iRc = utils.sudoProcessCall(asArgs, shell = False, close_fds = False); except: reporter.errorXcpt(); return False; reporter.log('Exit code [sudo]: %s (%s)' % (iRc, asArgs)); return iRc is 0;
def _sudoExecuteSync(self, asArgs): """ Executes a sudo child process synchronously. Returns True if the process executed successfully and returned 0, otherwise False is returned. """ reporter.log('Executing [sudo]: %s' % (asArgs, )) try: iRc = utils.sudoProcessCall(asArgs, shell=False, close_fds=False) except: reporter.errorXcpt() return False reporter.log('Exit code [sudo]: %s (%s)' % (iRc, asArgs)) return iRc is 0
def _sudoExecuteSync(self, asArgs): """ Executes a sudo child process synchronously. Returns a tuple [True, 0] if the process executed successfully and returned 0, otherwise [False, rc] is returned. """ reporter.log('Executing [sudo]: %s' % (asArgs, )); reporter.flushall(); iRc = 0; try: iRc = utils.sudoProcessCall(asArgs, shell = False, close_fds = False); except: reporter.errorXcpt(); return (False, 0); reporter.log('Exit code [sudo]: %s (%s)' % (iRc, asArgs)); return (iRc is 0, iRc);
def _sudoExecuteSync(self, asArgs): """ Executes a sudo child process synchronously. Returns a tuple [True, 0] if the process executed successfully and returned 0, otherwise [False, rc] is returned. """ reporter.log('Executing [sudo]: %s' % (asArgs, )) reporter.flushall() iRc = 0 try: iRc = utils.sudoProcessCall(asArgs, shell=False, close_fds=False) except: reporter.errorXcpt() return (False, 0) reporter.log('Exit code [sudo]: %s (%s)' % (iRc, asArgs)) return (iRc is 0, iRc)
def _mountShare(self, sMountPoint, sType, sServer, sShare, sUser, sPassword, sWhat): """ Mounts the specified share if needed. Raises exception on failure. """ # Only mount if the type is specified. if sType is None: return True; # Test if already mounted. sTestFile = os.path.join(sMountPoint + os.path.sep, sShare + '.txt'); if os.path.isfile(sTestFile): return True; # # Platform specific mount code. # sHostOs = utils.getHostOs() if sHostOs in ('darwin', 'freebsd'): utils.sudoProcessCall(['/sbin/umount', sMountPoint]); utils.sudoProcessCall(['/bin/mkdir', '-p', sMountPoint]); utils.sudoProcessCall(['/usr/sbin/chown', str(os.getuid()), sMountPoint]); # pylint: disable=E1101 if sType == 'cifs': # Note! no smb://server/share stuff here, 10.6.8 didn't like it. utils.processOutputChecked(['/sbin/mount_smbfs', '-o', 'automounted,nostreams,soft,noowners,noatime,rdonly', '-f', '0555', '-d', '0555', '//%s:%s@%s/%s' % (sUser, sPassword, sServer, sShare), sMountPoint]); else: raise TestBoxScriptException('Unsupported server type %s.' % (sType,)); elif sHostOs == 'linux': utils.sudoProcessCall(['/bin/umount', sMountPoint]); utils.sudoProcessCall(['/bin/mkdir', '-p', sMountPoint]); if sType == 'cifs': utils.sudoProcessOutputChecked(['/bin/mount', '-t', 'cifs', '-o', 'user='******',password='******',sec=ntlmv2' + ',uid=' + str(os.getuid()) # pylint: disable=E1101 + ',gid=' + str(os.getgid()) # pylint: disable=E1101 + ',nounix,file_mode=0555,dir_mode=0555,soft,ro', '//%s/%s' % (sServer, sShare), sMountPoint]); elif sType == 'nfs': utils.sudoProcessOutputChecked(['/bin/mount', '-t', 'nfs', '-o', 'soft,ro', '%s:%s' % (sServer, sShare if sShare.find('/') >= 0 else ('/export/' + sShare)), sMountPoint]); else: raise TestBoxScriptException('Unsupported server type %s.' % (sType,)); elif sHostOs == 'solaris': utils.sudoProcessCall(['/sbin/umount', sMountPoint]); utils.sudoProcessCall(['/bin/mkdir', '-p', sMountPoint]); if sType == 'cifs': ## @todo This stuff doesn't work on wei01-x4600b.de.oracle.com running 11.1. FIXME! oPasswdFile = tempfile.TemporaryFile(); oPasswdFile.write(sPassword + '\n'); oPasswdFile.flush(); utils.sudoProcessOutputChecked(['/sbin/mount', '-F', 'smbfs', '-o', 'user='******',uid=' + str(os.getuid()) # pylint: disable=E1101 + ',gid=' + str(os.getgid()) # pylint: disable=E1101 + ',fileperms=0555,dirperms=0555,noxattr,ro', '//%s/%s' % (sServer, sShare), sMountPoint], stdin = oPasswdFile); oPasswdFile.close(); elif sType == 'nfs': utils.sudoProcessOutputChecked(['/sbin/mount', '-F', 'nfs', '-o', 'noxattr,ro', '%s:%s' % (sServer, sShare if sShare.find('/') >= 0 else ('/export/' + sShare)), sMountPoint]); else: raise TestBoxScriptException('Unsupported server type %s.' % (sType,)); elif sHostOs == 'win': if sType != 'cifs': raise TestBoxScriptException('Only CIFS mounts are supported on Windows.'); utils.processCall(['net', 'use', sMountPoint, '/d']); utils.processOutputChecked(['net', 'use', sMountPoint, '\\\\' + sServer + '\\' + sShare, sPassword, '/USER:'******'Unsupported host %s' % (sHostOs,)); # # Re-test. # if not os.path.isfile(sTestFile): raise TestBoxException('Failed to mount %s (%s[%s]) at %s: %s not found' % (sWhat, sServer, sShare, sMountPoint, sTestFile)); return True;
def _mountShare(self, sMountPoint, sType, sServer, sShare, sUser, sPassword, sWhat): """ Mounts the specified share if needed. Raises exception on failure. """ # Only mount if the type is specified. if sType is None: return True; # Test if already mounted. sTestFile = os.path.join(sMountPoint + os.path.sep, sShare + '-new.txt'); if os.path.isfile(sTestFile): return True; # # Platform specific mount code. # sHostOs = utils.getHostOs() if sHostOs in ('darwin', 'freebsd'): utils.sudoProcessCall(['/sbin/umount', sMountPoint]); utils.sudoProcessCall(['/bin/mkdir', '-p', sMountPoint]); utils.sudoProcessCall(['/usr/sbin/chown', str(os.getuid()), sMountPoint]); # pylint: disable=E1101 if sType == 'cifs': # Note! no smb://server/share stuff here, 10.6.8 didn't like it. utils.processOutputChecked(['/sbin/mount_smbfs', '-o', 'automounted,nostreams,soft,noowners,noatime,rdonly', '-f', '0555', '-d', '0555', '//%s:%s@%s/%s' % (sUser, sPassword, sServer, sShare), sMountPoint]); else: raise TestBoxScriptException('Unsupported server type %s.' % (sType,)); elif sHostOs == 'linux': utils.sudoProcessCall(['/bin/umount', sMountPoint]); utils.sudoProcessCall(['/bin/mkdir', '-p', sMountPoint]); if sType == 'cifs': utils.sudoProcessOutputChecked(['/bin/mount', '-t', 'cifs', '-o', 'user='******',password='******',sec=ntlmv2' + ',uid=' + str(os.getuid()) # pylint: disable=E1101 + ',gid=' + str(os.getgid()) # pylint: disable=E1101 + ',nounix,file_mode=0555,dir_mode=0555,soft,ro', '//%s/%s' % (sServer, sShare), sMountPoint]); elif sType == 'nfs': utils.sudoProcessOutputChecked(['/bin/mount', '-t', 'nfs', '-o', 'soft,ro', '%s:%s' % (sServer, sShare if sShare.find('/') >= 0 else ('/export/' + sShare)), sMountPoint]); else: raise TestBoxScriptException('Unsupported server type %s.' % (sType,)); elif sHostOs == 'solaris': utils.sudoProcessCall(['/sbin/umount', sMountPoint]); utils.sudoProcessCall(['/bin/mkdir', '-p', sMountPoint]); if sType == 'cifs': ## @todo This stuff doesn't work on wei01-x4600b.de.oracle.com running 11.1. FIXME! oPasswdFile = tempfile.TemporaryFile(); oPasswdFile.write(sPassword + '\n'); oPasswdFile.flush(); utils.sudoProcessOutputChecked(['/sbin/mount', '-F', 'smbfs', '-o', 'user='******',uid=' + str(os.getuid()) # pylint: disable=E1101 + ',gid=' + str(os.getgid()) # pylint: disable=E1101 + ',fileperms=0555,dirperms=0555,noxattr,ro', '//%s/%s' % (sServer, sShare), sMountPoint], stdin = oPasswdFile); oPasswdFile.close(); elif sType == 'nfs': utils.sudoProcessOutputChecked(['/sbin/mount', '-F', 'nfs', '-o', 'noxattr,ro', '%s:%s' % (sServer, sShare if sShare.find('/') >= 0 else ('/export/' + sShare)), sMountPoint]); else: raise TestBoxScriptException('Unsupported server type %s.' % (sType,)); elif sHostOs == 'win': if sType != 'cifs': raise TestBoxScriptException('Only CIFS mounts are supported on Windows.'); utils.processCall(['net', 'use', sMountPoint, '/d']); utils.processOutputChecked(['net', 'use', sMountPoint, '\\\\' + sServer + '\\' + sShare, sPassword, '/USER:'******'Unsupported host %s' % (sHostOs,)); # # Re-test. # if not os.path.isfile(sTestFile): raise TestBoxException('Failed to mount %s (%s[%s]) at %s: %s not found' % (sWhat, sServer, sShare, sMountPoint, sTestFile)); return True;
def _reinitScratch(self, fnLog, fUseTheForce): """ Wipes the scratch directories and re-initializes them. No exceptions raise, returns success indicator instead. """ if fUseTheForce is None: fUseTheForce = self._fFirstSignOn class ErrorCallback(object): # pylint: disable=R0903 """ Callbacks + state for the cleanup. """ def __init__(self): self.fRc = True def onErrorCallback(self, sFnName, sPath, aXcptInfo): """ Logs error during shutil.rmtree operation. """ fnLog('Error removing "%s": fn=%s %s' % (sPath, sFnName, aXcptInfo[1])) self.fRc = False oRc = ErrorCallback() # # Cleanup. # for sName in os.listdir(self._oOptions.sScratchRoot): sFullName = os.path.join(self._oOptions.sScratchRoot, sName) try: if os.path.isdir(sFullName): shutil.rmtree(sFullName, False, oRc.onErrorCallback) else: os.remove(sFullName) if os.path.exists(sFullName): raise Exception('Still exists after deletion, weird.') except Exception as oXcpt: if fUseTheForce is True \ and utils.getHostOs() not in ['win', 'os2'] \ and len(sFullName) >= 8 \ and sFullName[0] == '/' \ and sFullName[1] != '/' \ and sFullName.find('/../') < 0: fnLog('Problems deleting "%s" (%s) using the force...' % (sFullName, oXcpt)) try: if os.path.isdir(sFullName): iRc = utils.sudoProcessCall( ['/bin/rm', '-Rf', sFullName]) else: iRc = utils.sudoProcessCall( ['/bin/rm', '-f', sFullName]) if iRc != 0: raise Exception('exit code %s' % iRc) if os.path.exists(sFullName): raise Exception( 'Still exists after forced deletion, weird^2.') except: fnLog('Error sudo deleting "%s": %s' % (sFullName, oXcpt)) oRc.fRc = False else: fnLog('Error deleting "%s": %s' % (sFullName, oXcpt)) oRc.fRc = False # Display files left behind. def dirEnumCallback(sName, oStat): """ callback for dirEnumerateTree """ fnLog(u'%s %s' % (utils.formatFileStat(oStat) if oStat is not None else '????????????', sName)) utils.dirEnumerateTree(self._oOptions.sScratchRoot, dirEnumCallback) # # Re-create the directories. # for sDir in [ self._oOptions.sScratchRoot, self._sScratchSpill, self._sScratchScripts, self._sScratchState ]: if not os.path.isdir(sDir): try: os.makedirs(sDir, 0o700) except Exception as oXcpt: fnLog('Error creating "%s": %s' % (sDir, oXcpt)) oRc.fRc = False if oRc.fRc is True: self._cReinitScratchErrors = 0 else: self._cReinitScratchErrors += 1 return oRc.fRc
def _reinitScratch(self, fnLog, fUseTheForce): """ Wipes the scratch directories and re-initializes them. No exceptions raise, returns success indicator instead. """ if fUseTheForce is None: fUseTheForce = self._fFirstSignOn; class ErrorCallback(object): # pylint: disable=R0903 """ Callbacks + state for the cleanup. """ def __init__(self): self.fRc = True; def onErrorCallback(self, sFnName, sPath, aXcptInfo): """ Logs error during shutil.rmtree operation. """ fnLog('Error removing "%s": fn=%s %s' % (sPath, sFnName, aXcptInfo[1])); self.fRc = False; oRc = ErrorCallback(); # # Cleanup. # for sName in os.listdir(self._oOptions.sScratchRoot): sFullName = os.path.join(self._oOptions.sScratchRoot, sName); try: if os.path.isdir(sFullName): shutil.rmtree(sFullName, False, oRc.onErrorCallback); else: os.remove(sFullName); if os.path.exists(sFullName): raise Exception('Still exists after deletion, weird.'); except Exception as oXcpt: if fUseTheForce is True \ and utils.getHostOs() not in ['win', 'os2'] \ and len(sFullName) >= 8 \ and sFullName[0] == '/' \ and sFullName[1] != '/' \ and sFullName.find('/../') < 0: fnLog('Problems deleting "%s" (%s) using the force...' % (sFullName, oXcpt)); try: if os.path.isdir(sFullName): iRc = utils.sudoProcessCall(['/bin/rm', '-Rf', sFullName]) else: iRc = utils.sudoProcessCall(['/bin/rm', '-f', sFullName]) if iRc != 0: raise Exception('exit code %s' % iRc); if os.path.exists(sFullName): raise Exception('Still exists after forced deletion, weird^2.'); except: fnLog('Error sudo deleting "%s": %s' % (sFullName, oXcpt)); oRc.fRc = False; else: fnLog('Error deleting "%s": %s' % (sFullName, oXcpt)); oRc.fRc = False; # Display files left behind. def dirEnumCallback(sName, oStat): """ callback for dirEnumerateTree """ fnLog(u'%s %s' % (utils.formatFileStat(oStat) if oStat is not None else '????????????', sName)); utils.dirEnumerateTree(self._oOptions.sScratchRoot, dirEnumCallback); # # Re-create the directories. # for sDir in [self._oOptions.sScratchRoot, self._sScratchSpill, self._sScratchScripts, self._sScratchState]: if not os.path.isdir(sDir): try: os.makedirs(sDir, 0o700); except Exception as oXcpt: fnLog('Error creating "%s": %s' % (sDir, oXcpt)); oRc.fRc = False; if oRc.fRc is True: self._cReinitScratchErrors = 0; else: self._cReinitScratchErrors += 1; return oRc.fRc;
def _killAllVBoxProcesses(self): """ Kills all virtual box related processes we find in the system. """ sHostOs = utils.getHostOs() asDebuggers = [ 'cdb', 'windbg', ] if sHostOs == 'windows' else [ 'gdb', 'gdb-i386-apple-darwin', 'lldb' ] for iIteration in range(22): # Gather processes to kill. aoTodo = [] aoDebuggers = [] for oProcess in utils.processListAll(): sBase = oProcess.getBaseImageNameNoExeSuff() if sBase is None: continue sBase = sBase.lower() if sBase in [ 'vboxsvc', 'vboxsds', 'virtualbox', 'virtualboxvm', 'vboxheadless', 'vboxmanage', 'vboxsdl', 'vboxwebsrv', 'vboxautostart', 'vboxballoonctrl', 'vboxbfe', 'vboxextpackhelperapp', 'vboxnetdhcp', 'vboxnetnat', 'vboxnetadpctl', 'vboxtestogl', 'vboxtunctl', 'vboxvmmpreload', 'vboxxpcomipcd', ]: aoTodo.append(oProcess) if sBase.startswith('virtualbox-') and sBase.endswith( '-multiarch.exe'): aoTodo.append(oProcess) if sBase in asDebuggers: aoDebuggers.append(oProcess) if iIteration in [0, 21]: reporter.log('Warning: debugger running: %s (%s %s)' % (oProcess.iPid, sBase, oProcess.asArgs)) if not aoTodo: return True # Are any of the debugger processes hooked up to a VBox process? if sHostOs == 'windows': # On demand debugging windows: windbg -p <decimal-pid> -e <decimal-event> -g for oDebugger in aoDebuggers: for oProcess in aoTodo: # The whole command line is asArgs[0] here. Fix if that changes. if oDebugger.asArgs and oDebugger.asArgs[0].find( '-p %s ' % (oProcess.iPid, )) >= 0: aoTodo.append(oDebugger) break else: for oDebugger in aoDebuggers: for oProcess in aoTodo: # Simplistic approach: Just check for argument equaling our pid. if oDebugger.asArgs and ( '%s' % oProcess.iPid) in oDebugger.asArgs: aoTodo.append(oDebugger) break # Kill. for oProcess in aoTodo: reporter.log('Loop #%d - Killing %s (%s, uid=%s)' % ( iIteration, oProcess.iPid, oProcess.sImage if oProcess.sName is None else oProcess.sName, oProcess.iUid, )) if not utils.processKill(oProcess.iPid) \ and sHostOs != 'windows' \ and utils.processExists(oProcess.iPid): # Many of the vbox processes are initially set-uid-to-root and associated debuggers are running # via sudo, so we might not be able to kill them unless we sudo and use /bin/kill. try: utils.sudoProcessCall( ['/bin/kill', '-9', '%s' % (oProcess.iPid, )]) except: reporter.logXcpt() # Check if they're all dead like they should be. time.sleep(0.1) for oProcess in aoTodo: if utils.processExists(oProcess.iPid): time.sleep(2) break return False