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)
def client1(): """ <Purpose> Thread for client 1 to connect to server, and send/recv data... """ lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300) lind_test_server.send_syscall(clientsockfd, "test", 0) #Short sleeps are not working, give enough time... emultimer.sleep(1) assert lind_test_server.recv_syscall(clientsockfd, 100, 0) == "test", \ "Write failed in select while processing client 1..." lind_test_server.close_syscall(clientsockfd)
def tester(): # need simultaneous communication... emultimer.createthread(helper) emultimer.sleep(0.1) lind_test_server.send_syscall(sockets[1], "SocketPair test.", 0) msg = None msg = lind_test_server.recv_syscall(sockets[1], 1024, 0) assert msg != None, "Sockpair recv failed in tester..." emultimer.sleep(0.1) lind_test_server.close_syscall(sockets[1]) lind_test_server.close_syscall(sockets[0]) return
def _find_all_paths_recursively(startingpath): # helper for list_all_lind_paths. It recursively looks at all child dirs knownitems = [] # I need to open the dir to use getdents... dirfd = lind_test_server.open_syscall(startingpath,0,0) # build a list of all dents. These have an odd format: # [(inode, filename, d_type (DT_DIR, DR_REG, etc.), length of entry), ...] # We only care about the filename and d_type. mydentslist = [] # Note: the length parameter is odd, it's related to data structure space, so # it doesn't map to python cleanly. So long as it's > the largest possible # entry size, this code should work though. thesedents = lind_test_server.getdents_syscall(dirfd,10000) while thesedents: mydentslist += thesedents thesedents = lind_test_server.getdents_syscall(dirfd,10000) lind_test_server.close_syscall(dirfd) # to make the output correct, if the dir is '/', don't print it. if startingpath == '/': startingpath = '' for dent in mydentslist: # ignore '.' and '..' because they aren't interesting and we don't want # to loop forever. if dent[1]=='.' or dent[1]=='..': continue thisitem = (dent[0], startingpath+'/'+dent[1]) # add it... knownitems.append(thisitem) print thisitem # if it's a directory, recurse... if dent[2]==DT_DIR: knownitems = knownitems + _find_all_paths_recursively(thisitem[1]) return knownitems
def deltree_lind(dirname): # took part of the code from _find_all_paths_recursively method below. # It recursively looks at all child dirs # I need to open the dir to use getdents... dirfd = lind_test_server.open_syscall(dirname,0,0) # build a list of all dents. These have an odd format: # [(inode, filename, d_type (DT_DIR, DR_REG, etc.), length of entry), ...] # We only care about the filename and d_type. mydentslist = [] # Note: the length parameter is odd, it's related to data structure space, so # it doesn't map to python cleanly. So long as it's > the largest possible # entry size, this code should work though. thesedents = lind_test_server.getdents_syscall(dirfd,10000) while thesedents: mydentslist += thesedents thesedents = lind_test_server.getdents_syscall(dirfd,10000) lind_test_server.close_syscall(dirfd) for dent in mydentslist: # ignore '.' and '..' because they aren't interesting and we don't want # to loop forever. if dent[1]=='.' or dent[1]=='..': continue thisitem = (dent[0], dirname+'/'+dent[1]) print "deleting",thisitem[1] # if it's a directory, recurse... if dent[2]==DT_DIR: deltree_lind(thisitem[1]) else: lind_test_server.unlink_syscall(thisitem[1]) lind_test_server.rmdir_syscall(dirname) return
import lind_test_server from lind_net_constants import * SyscallError = lind_test_server.SyscallError # let's do a few basic things with connect. This will be UDP only for now... sockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0) lind_test_server.bind_syscall(sockfd, '127.0.0.1', 50102) # should be okay... lind_test_server.connect_syscall(sockfd, '127.0.0.1', 50300) # bind will not be interesting... try: lind_test_server.bind_syscall(sockfd, '127.0.0.1', 50102) except SyscallError: pass else: print "==============" print "TEST Failed..." print "==============" lind_test_server.close_syscall(sockfd)
from lind_fs_constants import * import lind_test_server lind_test_server._blank_fs_init() myfd = lind_test_server.creat_syscall('/hello', S_IRWXA) 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
def release(self, flags): log("close", self.fd, flags) return lind.close_syscall(self.fd)
# Okay, I have made the path now. Only one thing remains, adding the file posixfo = open(posixfn) filecontents = posixfo.read() posixfo.close() # make the new file, truncating any existing contents... lindfd = lind_test_server.open_syscall(normalizedlindfn, O_CREAT|O_EXCL|O_TRUNC|O_WRONLY, S_IRWXA) # should write all at once... datalen = lind_test_server.write_syscall(lindfd, filecontents) assert(datalen == len(filecontents)) lind_test_server.close_syscall(lindfd) # fix stat, etc. _mirror_stat_data(posixfn, normalizedlindfn) def _cp_dir_into_lind(fullfilename, rootpath='.', createmissingdirs=True): # check for the file. posixfn = os.path.join(rootpath, fullfilename) if not os.path.exists(posixfn): raise IOError("Cannot locate dire on POSIX FS: '" + posixfn + "'") if os.path.isfile(posixfn): raise IOError("POSIX FS path is not a directory: '" + posixfn + "'")
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)
+ " got " + str(len(msg[2])) + " bytes." except Exception, e: print 'Test:- W:100, Peek several times : ', e emultimer.sleep(0.2) #Process fourth test... try: msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK) assert len(msg[2]) == 50, "Length expected 50 bytes to peek, but only" \ + " got " + str(len(msg[2])) + " bytes." except Exception, e: print 'Test:- W:100 P:50 : ', e #Gracefully close the socket lind_test_server.close_syscall(newsockfd) rip, rport, newsockfd = lind_test_server.accept_syscall(serversockfd) #Process first test... try: msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK) assert len(msg[2]) == 100, "Length expected 100 bytes to peek, but only" \ + " got " + str(len(msg[2])) + " bytes." msg = lind_test_server.recvfrom_syscall(newsockfd, 100, 0) assert len(msg[2]) == 100, "Length expected 100 bytes to read, but only" \ + " got " + str(len(msg[2])) + " bytes." except Exception, e: print 'TEST:- W:100, P:100, R:100 : ', e emultimer.sleep(0.2)
lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300) lind_test_server.send_syscall(clientsockfd, "test", 0) #Short sleeps are not working, give enough time... emultimer.sleep(1) assert lind_test_server.recv_syscall(clientsockfd, 100, 0) == "test", \ "Write failed in select while processing client 1..." lind_test_server.close_syscall(clientsockfd) #Thread for running client 1, I did so because to show that server can handle #multiple connections simultaneously... emultimer.createthread(client1) emultimer.sleep(.1) #Client 2 connects to server, and send/recv data... lind_test_server.connect_syscall(client2sockfd, '127.0.0.1', 50300) lind_test_server.send_syscall(client2sockfd, "test", 0) #Short sleeps are not working, give enough time... emultimer.sleep(1) assert lind_test_server.recv_syscall(client2sockfd, 100, 0) == "test", \ "Write failed in select while processing client 2..." lind_test_server.close_syscall(client2sockfd) #Exit all threads, no better way to close server thread which is in infinity #loop... emulmisc.exitall()
#Both client and server are run from this file, hence opening sockets for both listensockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0) sendsockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0) #Bind the socket to an address, this is important in repy, because the recvfrom #_syscall() doesn't work properly without binding the address first. lind_test_server.bind_syscall(listensockfd, '127.0.0.1', 50300) def process_request(): msg = None # Read the data in the socket. try: msg = lind_test_server.recvfrom_syscall(listensockfd, 1024, 0) assert msg != None, "Socket failed to recv message." except Exception, e: print "UDP Connect Test : ", e #Run the listen socket in seperate thread, since send/listen should be started #simultaneously. emultimer.createthread(process_request) emultimer.sleep(0.1) #This is another way to send message in UDP, instead of sendto_syscall(). lind_test_server.connect_syscall(sendsockfd, '127.0.0.1', 50300) lind_test_server.send_syscall(sendsockfd, "UDP Connect Test", 0) #close send & listen sockets... lind_test_server.close_syscall(listensockfd) lind_test_server.close_syscall(sendsockfd)
import lind_test_server from lind_fs_constants import * # Let's add a few files, etc. to the system and see if it works... lind_test_server._blank_fs_init() filefd = lind_test_server.open_syscall('/foo',O_CREAT | O_EXCL | O_WRONLY,\ S_IRWXA) assert lind_test_server.stat_syscall('/foo')[2] == S_IRWXA | S_IFREG, \ "Failed to have full access to all users." lind_test_server.chmod_syscall('/foo', S_IRUSR | S_IRGRP) assert lind_test_server.stat_syscall('/foo')[2] == S_IRUSR | S_IRGRP | S_IFREG\ , "Failed to set Read access to user and group." lind_test_server.chmod_syscall('/foo', S_IRWXA) assert lind_test_server.stat_syscall('/foo')[2] == S_IRWXA | S_IFREG, \ "Failed to set back full access to all users." lind_test_server.close_syscall(filefd)
+ " got " + str(len(msg[2])) + " bytes." except Exception, e: print 'Test:- W:100, Peek several times : ', e emultimer.sleep(0.2) #Process fourth test... try: msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK) assert len(msg[2]) == 50, "Length expected 50 bytes to peek, but only" \ + " got " + str(len(msg[2])) + " bytes." except Exception, e: print 'Test:- W:100 P:50 : ', e #Gracefully close the socket lind_test_server.close_syscall(newsockfd) #Run the server in a seperate thread, since client/server should be started #simultaneously. emultimer.createthread(process_request) #connect to server lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300) #send each test with some delay, so that server processes each test cleanly. lind_test_server.send_syscall(clientsockfd, "A" * 100, 0) emultimer.sleep(0.1) lind_test_server.send_syscall(clientsockfd, "A" * 100, 0) emultimer.sleep(0.1) lind_test_server.send_syscall(clientsockfd, "A" * 100, 0) emultimer.sleep(0.1)
return if lind_exists and not lind_isfile: print fullfilename+" on lind file system is not a regular file, skipping" return samefile = True if host_size == lind_size: fd = open(posixfn, "rb") host_content = fd.read() fd.close() lindfd = lind_test_server.open_syscall(fullfilename, O_RDONLY, 0) lind_content = lind_test_server.read_syscall(lindfd, lind_size) lind_test_server.close_syscall(lindfd) samefile = (host_content == lind_content) else: samefile = False if not samefile: if lind_exists: print "removing "+fullfilename lind_test_server.unlink_syscall(fullfilename) cp_into_lind(fullfilename, rootpath, True) else: print "same file, skipping"
# 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 lind_test_server.close_syscall(myfd) # the other should still work... assert (lind_test_server.write_syscall(myfd2, 'raar') == 4) lind_test_server.lseek_syscall(myfd2, 0, SEEK_SET) assert (lind_test_server.read_syscall(myfd2, 10) == 'hiyoraar') lind_test_server.close_syscall(myfd2)
import lind_test_server from lind_net_constants import * SyscallError = lind_test_server.SyscallError # let's do a few basic things with connect. This will be UDP only for now... sockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0) lind_test_server.bind_syscall(sockfd, '127.0.0.1', 50102) # should be okay... lind_test_server.connect_syscall(sockfd,'127.0.0.1',50300) # bind will not be interesting... try: lind_test_server.bind_syscall(sockfd,'127.0.0.1',50102) except SyscallError: pass else: print "==============" print "TEST Failed..." print "==============" lind_test_server.close_syscall(sockfd)
# 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 lind_test_server.close_syscall(myfd) # the other should still work... assert(lind_test_server.write_syscall(myfd2,'raar')==4) lind_test_server.lseek_syscall(myfd2,0,SEEK_SET) assert(lind_test_server.read_syscall(myfd2,10)=='hiyoraar') lind_test_server.close_syscall(myfd2)
import lind_test_server from lind_fs_constants import * lind_test_server._blank_fs_init() # Let's add a few directories to the system and see if it works... lind_test_server.mkdir_syscall('/bar',S_IRWXA) lind_test_server.mkdir_syscall('/bar/baz',S_IRWXA) lind_test_server.mkdir_syscall('/bar/bap',0) # Create a new file... fd = lind_test_server.open_syscall('/bar/bam',O_CREAT,0) lind_test_server.close_syscall(fd) # Read the root directory... rootfd = lind_test_server.open_syscall('/',0,0) val = lind_test_server.getdents_syscall(rootfd, 100) assert (val==[(3, 'bar', DT_DIR, 24), (1, '..', DT_DIR, 24),\ (1, '.', DT_DIR, 24)]), "Found: %s"%(str(val)) # Read the /bar directory... barfd = lind_test_server.open_syscall('/bar',0,0) # The buffer size is given small, only few entries are read. val = lind_test_server.getdents_syscall(barfd, 80) assert (val == [(6, 'bam', DT_REG, 24), (4, 'baz', DT_DIR, 24),\ (5, 'bap', DT_DIR, 24)]), "Found: %s"%(str(val)) # Again call on the same FD, should continue parsing the /bar directory. val = lind_test_server.getdents_syscall(barfd, 80)
sendsockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0) #Bind the socket to an address, this is important in repy, because the recvfrom #_syscall() doesn't work properly without binding the address first. lind_test_server.bind_syscall(listensockfd, '127.0.0.1', 50300) def process_request(): msg = None # Read the data in the socket. try: msg = lind_test_server.recvfrom_syscall(listensockfd, 1024, 0) assert msg != None, "Socket failed to recv message." except Exception, e: print "UDP Connect Test : ", e #Run the listen socket in seperate thread, since send/listen should be started #simultaneously. emultimer.createthread(process_request) emultimer.sleep(0.1) #This is another way to send message in UDP, instead of sendto_syscall(). lind_test_server.connect_syscall(sendsockfd, '127.0.0.1', 50300) lind_test_server.send_syscall(sendsockfd, "UDP Connect Test", 0) #close send & listen sockets... lind_test_server.close_syscall(listensockfd) lind_test_server.close_syscall(sendsockfd)