示例#1
0
文件: rrfs.py 项目: 99plus2/CS323
def catchExecution(copies):
	global curFileDesc, curSyscallResult 
	while True:
		rc, pid, event, num, arg0, arg1, arg2, arg3, arg4, arg5, rslt = Systap.getNextTraceEvent()
		if rc >= 0: return rc 

		if event == Systap.ST_EVENT_SYSCALL_ENTERED:
			if num == Systap.ST_SYS_OPEN:
				pathMemAddr, flags = arg0, arg1
				path = Systap.readProcessMemNullTerm(pid, pathMemAddr, 4096)
				type = 0
				flag = None
			
				if matchLocal.search(path):
					curSyscallResult,flag = checkLocal(pid,path[6:],flags,Systap.ST_SYS_OPEN)
					type = 1
				elif matchHost.search(path):
					curSyscallResult = checkRemote(pid,flags,Systap.ST_SYS_OPEN)
					type = 2
				else: curSyscallResult = None
				if curSyscallResult == None and type:
					if type == 1:
						try: file = open(path[6:],flag)
						except:
							curSyscallResult = Systap.ST_EACCES
							continue
					elif type == 2:
						try:
							file = urllib2.urlopen(urllib2.Request(path))
						except:
							curSyscallResult = Systap.ST_ENOENT
							continue
					curSyscallResult = curFileDesc
					globalFileTable[curFileDesc] = file
					curFileDesc += 1
			elif num == Systap.ST_SYS_READ:
				fd, buf, size = arg0, arg1, arg2
				if fd in globalFileTable:
					try:
						output = globalFileTable[fd].read(size*copies)
					except:
						curSyscallResult = Systap.ST_EBADF
						continue
					listOutput, newOutput = list(output), []
					for i in range(0,len(listOutput),copies):
						tempList = []
						for j in range(copies):
							if i+j < len(listOutput): tempList.append(listOutput[i+j])
							else: tempList.append(None)
						newOutput.append(majority(tempList))
					Systap.writeProcessMem(pid,buf,''.join(newOutput))
					curSyscallResult = len(output) / copies
			elif num == Systap.ST_SYS_WRITE:
				fd, buf, size = arg0, arg1, arg2
				if fd in globalFileTable:
					output = list(Systap.readProcessMem(pid,buf,size))
					curSyscallResult = len(output) * copies
					try:
						for char in output:
							for i in range(copies):
								globalFileTable[fd].write(char)
					except:
						curSyscallResult = Systap.ST_EBADF
						continue
			elif num == Systap.ST_SYS_CLOSE:
				fd = arg0
				if fd in globalFileTable:
					try: globalFileTable[fd].close()
					except:
						curSyscallResult = Systap.EBADF
						continue
					curSyscallResult = 0
			else: curSyscallResult = None
		elif event == Systap.ST_EVENT_SYSCALL_EXITED:
			if curSyscallResult != None:
				Systap.setSyscallResult(pid, curSyscallResult)
				curSyscallResult = None