def read(self, length, offset): log("read", self.fd, length, offset) try: lind.lseek_syscall(self.fd, offset, 0) ret = lind.read_syscall(self.fd, length) except lind.SyscallError, e: ret = -errno[e[1]]
def write(self, buf, offset): log("write", self.fd, buf, offset) try: lind.lseek_syscall(self.fd, offset, 0) ret = lind.write_syscall(self.fd, buf) except lind.SyscallError, e: ret = -errno[e[1]]
def process_request(): """ <Purpose> Server thread for processing connections using select ... """ while True: #Pass list of Inputs, Outputs for select which returns if any activity #occurs on any socket. ret = lind_test_server.select_syscall(11, inputs, outputs, excepts, 5) #Check for any activity in any of the Input sockets... for sock in ret[1]: #If the socket returned was listerner socket, then there's a new conn. #so we accept it, and put the client socket in the list of Inputs. if sock is serversockfd: newsockfd = lind_test_server.accept_syscall(sock) inputs.append(newsockfd[2]) #Write to a file... elif sock is filefd: emultimer.sleep(1) assert lind_test_server.write_syscall(sock, 'test') == 4, \ "Failed to write into a file..." lind_test_server.lseek_syscall(sock, 0, SEEK_SET) inputs.remove(sock) #If the socket is in established conn., then we recv the data, if #there's no data, then close the client socket. else: data = lind_test_server.recv_syscall(sock, 100, 0) if data: assert data == "test", "Recv failed in select..." #We make the ouput ready, so that it sends out data... if sock not in outputs: outputs.append(sock) else: #No data means remote socket closed, hence close the client socket #in server, also remove this socket from readfd's. lind_test_server.close_syscall(sock) inputs.remove(sock) #Check for any activity in any of the output sockets... for sock in ret[2]: if sock is filefd: assert lind_test_server.read_syscall(sock, 4) == "test", \ "Failed to read from a file..." #test for file finished, remove from monitoring. outputs.remove(sock) else: lind_test_server.send_syscall(sock, data, 0) #Data is sent out this socket, it's no longer ready for writing #remove this socket from writefd's. outputs.remove(sock) lind_test_server.close_syscall(serversockfd)
lind_test_server.close_syscall(myfd) # Read only file descriptor opened rdfd = lind_test_server.open_syscall('/hello', O_RDONLY, S_IRWXA) try: # File written lind_test_server.write_syscall(rdfd, 'Hello everyone!') except lind_test_server.SyscallError: # should be an error pass else: print "Error! Should have been blocked from writing with O_RDONLY" lind_test_server.lseek_syscall(rdfd, 0, SEEK_SET) # should work... lind_test_server.read_syscall(rdfd, 100) lind_test_server.close_syscall(rdfd) # Alternately wrfd = lind_test_server.open_syscall('/hello', O_WRONLY, S_IRWXA) try: # Can I read a write only file? lind_test_server.read_syscall(wrfd, 50) except lind_test_server.SyscallError: # should be an error
import lind_test_server from lind_fs_constants import * # Try read / write of a file and see if it works... lind_test_server._blank_fs_init() myfd = lind_test_server.open_syscall('/foo', O_CREAT | O_EXCL | O_RDWR, S_IRWXA) # write should succeed assert (lind_test_server.write_syscall(myfd, 'hello there!') == 12) # seek to the beginning... assert (lind_test_server.lseek_syscall(myfd, 0, SEEK_SET) == 0) # read the first 5 bytes (hello) assert (lind_test_server.read_syscall(myfd, 5) == 'hello') # change it to hello world! assert (lind_test_server.write_syscall(myfd, ' world') == 6) # seek to the beginning again... assert (lind_test_server.lseek_syscall(myfd, 0, SEEK_SET) == 0) # and read it all... assert (lind_test_server.read_syscall(myfd, 100) == 'hello world!') lind_test_server.close_syscall(myfd)
def process_request(): """ <Purpose> Server thread for processing connections using poll ... """ while True: #Pass list of sockets that needs for polling which returns if any activity #occurs on any socket. poll_vals = lind_test_server.poll_syscall(polled, 5) ret = [poll_vals[0],[],[],[]] ret[1] = [x['fd'] for x in poll_vals[1] if x['revents'] & lind_test_server.POLLIN] ret[2] = [x['fd'] for x in poll_vals[1] if x['revents'] & lind_test_server.POLLOUT] #Check for any activity in any of the Input sockets... for sock in ret[1]: #If the socket returned was listerner socket, then there's a new conn. #so we accept it, and put the client socket in the list of Inputs. if sock is serversockfd: newsockfd = lind_test_server.accept_syscall(sock) new_poll = {'fd':newsockfd[2], 'events':lind_test_server.POLLIN, 'revents':0} polled.append(new_poll) #Write to a file... elif sock is filefd: assert lind_test_server.write_syscall(sock, 'test') == 4, \ "Failed to write into a file..." lind_test_server.lseek_syscall(sock, 0, SEEK_SET) #Once the write is successful into a file, Modify the file descriptor #so that its ready for reading out of the file. [x for x in polled if x['fd'] == sock][0]['events'] = \ lind_test_server.POLLOUT #If the socket is in established conn., then we recv the data, if #there's no data, then close the client socket. else: data = lind_test_server.recv_syscall(sock, 100, 0) if data: assert data == "test", "Recv failed in select..." #This socket is ready for writing, modify the socket descriptor #to be in read-write mode. This socket can write data out to network [x for x in polled if x['fd'] == sock][0]['events'] = \ lind_test_server.POLLIN | lind_test_server.POLLOUT else: #No data means remote socket closed, hence close the client socket #in server, also remove this socket from polling. lind_test_server.close_syscall(sock) to_go = [x for x in polled if x['fd'] == sock] map(polled.remove, to_go) #Check for any activity in any of the output sockets... for sock in ret[2]: if sock is filefd: assert lind_test_server.read_syscall(sock, 4) == "test", \ "Failed to read from a file..." #test for file finished, remove from polling. to_go = [x for x in polled if x['fd'] == sock] map(polled.remove, to_go) else: lind_test_server.send_syscall(sock, data, 0) #Data is sent out of this socket, it's no longer ready for writing, #modify it only read mode. [x for x in polled if x['fd'] == sock][0]['events'] = \ lind_test_server.POLLIN lind_test_server.close_syscall(serversockfd)
flags = O_TRUNC | O_CREAT | O_RDWR mode = 438 # 0666 name = 'double_open_file' myfd = lind_test_server.open_syscall(name, flags, mode) assert (lind_test_server.write_syscall(myfd, 'hi') == 2) # duplicate the file descriptor... myfd2 = lind_test_server.dup2_syscall(myfd, myfd + 1) # this should actually be essentially a no-op. It closes myfd+1 and sets it # to be the same as the current fd (which was done by the prior call) myfd2 = lind_test_server.dup2_syscall(myfd, myfd + 1) # should be at the same place... assert (lind_test_server.lseek_syscall( myfd, 0, SEEK_CUR) == lind_test_server.lseek_syscall(myfd2, 0, SEEK_CUR)) # write some data to move the first position assert (lind_test_server.write_syscall(myfd, 'yo') == 2) # _still_ should be at the same place... assert (lind_test_server.lseek_syscall( myfd, 0, SEEK_CUR) == lind_test_server.lseek_syscall(myfd2, 0, SEEK_CUR)) # reset the position within the file lind_test_server.lseek_syscall(myfd2, 0, SEEK_SET) # read from the other fd assert (lind_test_server.read_syscall(myfd, 10) == 'hiyo') # close one fd
fd = lind_test_server.open_syscall('/foo', O_CREAT | O_EXCL | O_RDWR, S_IRWXA) # Make a big file msg = "Hello" * 100 size = len(msg) # how small will we make the file TRUNCATE_SIZE = 10 NEW_TRUNCATE_SIZE = 200 lind_test_server.write_syscall(fd, msg) lind_test_server.ftruncate_syscall(fd, TRUNCATE_SIZE) # seek to the beginning... assert(lind_test_server.lseek_syscall(fd, 0, SEEK_SET) == 0) data = lind_test_server.read_syscall(fd, 100) assert len(data) == TRUNCATE_SIZE, "File must be smaller after truncate" assert data == msg[:TRUNCATE_SIZE], "New files contents must match: \n%s\n%s" \ % (data, msg[:TRUNCATE_SIZE]) lind_test_server.ftruncate_syscall(fd, NEW_TRUNCATE_SIZE) # seek to the beginning... assert(lind_test_server.lseek_syscall(fd, 0, SEEK_SET) == 0)
flags = O_TRUNC | O_CREAT | O_RDWR mode = 438 # 0666 name = 'double_open_file' myfd = lind_test_server.open_syscall(name, flags, mode) assert(lind_test_server.write_syscall(myfd,'hi')==2) # duplicate the file descriptor... myfd2 = lind_test_server.dup2_syscall(myfd,myfd+1) # this should actually be essentially a no-op. It closes myfd+1 and sets it # to be the same as the current fd (which was done by the prior call) myfd2 = lind_test_server.dup2_syscall(myfd,myfd+1) # should be at the same place... assert(lind_test_server.lseek_syscall(myfd,0,SEEK_CUR) == lind_test_server.lseek_syscall(myfd2,0,SEEK_CUR)) # write some data to move the first position assert(lind_test_server.write_syscall(myfd,'yo')==2) # _still_ should be at the same place... assert(lind_test_server.lseek_syscall(myfd,0,SEEK_CUR) == lind_test_server.lseek_syscall(myfd2,0,SEEK_CUR)) # reset the position within the file lind_test_server.lseek_syscall(myfd2,0,SEEK_SET) # read from the other fd assert(lind_test_server.read_syscall(myfd,10)=='hiyo')
TEST_FILENAME = "/someSimpleFileName" TEST2_FILENAME = "/otherSimpleFileName2" if __name__ == "__main__": lind_test_server._blank_fs_init() # Everything is okay, so now make a file myfd = lind_test_server.open_syscall(TEST_FILENAME, O_CREAT | O_EXCL | O_RDWR, S_IRWXA) # write should succeed assert lind_test_server.write_syscall(myfd, "hello there!") == 12 # seek to the beginning... assert lind_test_server.lseek_syscall(myfd, 0, SEEK_SET) == 0 # read the first 5 bytes (hello) assert lind_test_server.read_syscall(myfd, 5) == "hello" # change it to hello world! assert lind_test_server.write_syscall(myfd, " world") == 6 # seek to the beginning again... assert lind_test_server.lseek_syscall(myfd, 0, SEEK_SET) == 0 # and read it all... assert lind_test_server.read_syscall(myfd, 100) == "hello world!" lind_test_server.close_syscall(myfd)