diff options
author | Alexander Bersenev <bay@hackerdom.ru> | 2011-06-08 22:48:19 +0000 |
---|---|---|
committer | Alexander Bersenev <bay@hackerdom.ru> | 2011-06-08 22:48:19 +0000 |
commit | 3498865028063d381f35e05cc646c77dedaaa109 (patch) | |
tree | c21d0b04783bde0b736a157851fedbf1a191a128 | |
parent | file access blocking support (diff) | |
download | autodep-3498865028063d381f35e05cc646c77dedaaa109.tar.gz autodep-3498865028063d381f35e05cc646c77dedaaa109.tar.bz2 autodep-3498865028063d381f35e05cc646c77dedaaa109.zip |
fixed bugs in multithreading in fuse, add some test for fuse, make unmount more reliable
-rw-r--r-- | logger/src/autodep/logfs/logger_fusefs.py | 26 | ||||
-rw-r--r-- | logger/src/autodep/logfs/test_fstracer.py | 46 | ||||
-rw-r--r-- | logger/src/hook_fusefs/hookfs.c | 11 |
3 files changed, 69 insertions, 14 deletions
diff --git a/logger/src/autodep/logfs/logger_fusefs.py b/logger/src/autodep/logfs/logger_fusefs.py index 76516bc..11a6f39 100644 --- a/logger/src/autodep/logfs/logger_fusefs.py +++ b/logger/src/autodep/logfs/logger_fusefs.py @@ -59,6 +59,18 @@ class logger: #we will delete the object manually after execprog pass + # launches command, if it returns not 0 waits for 1 second and launches again + # for various umounts + def smartcommandlauncher(self,args): + for waittime in (1,1,2): + ret=subprocess.call(args) + if ret==0: + return + print "Auto-retrying after %d sec" % waittime + time.sleep(waittime) + print "Giving up. Command %s failed" % args + + def execprog(self,prog_name,arguments): pid=os.fork() if pid==0: @@ -78,18 +90,12 @@ class logger: else: exitcode=os.wait()[1]; try: - # unmount all manually + print "Unmounting partitions" self.mountlist.reverse() for mount in self.mountlist: - ret=subprocess.call(['umount',self.rootmountpath+mount]) - if ret!=0: - print "failed to umount bind %s directory. Check messages above" % ( self.rootmountpath+mount) - ret=subprocess.call(['fusermount','-u',self.rootmountpath]); - if ret!=0: - print "Error while unmounting fuse filesystem. Check messages above" - ret=subprocess.call(['umount',self.rootmountpath]); - if ret!=0: - print "Error while unmounting %s Check messages above" % (self.rootmountpath) + self.smartcommandlauncher(['umount',self.rootmountpath+mount]) + self.smartcommandlauncher(['fusermount','-u',self.rootmountpath]); + self.smartcommandlauncher(['umount',self.rootmountpath]); os.rmdir(self.rootmountpath) except OSError, e: diff --git a/logger/src/autodep/logfs/test_fstracer.py b/logger/src/autodep/logfs/test_fstracer.py index 9a3df67..c254bce 100644 --- a/logger/src/autodep/logfs/test_fstracer.py +++ b/logger/src/autodep/logfs/test_fstracer.py @@ -2,7 +2,7 @@ import unittest import fstracer -class simple_tests(unittest.TestCase): +class hooklib_simple_tests(unittest.TestCase): def test_open_unexists(self): self.assertEqual(fstracer.getfsevents('/bin/cat', ['/bin/cat','f1','f2']), @@ -46,11 +46,49 @@ class simple_tests(unittest.TestCase): for f in xrange(0,filesnum): self.assertTrue(resultarray.count(['open', 'file_%d_%d' % (p,f)])==1) - #self.assertEqual(resultarray, - # map(lambda x: ['open','file'+str(x)],range(0,filesnum))) +class fusefs_simple_tests(unittest.TestCase): + def test_open_unexists(self): + eventslist=fstracer.getfsevents('/bin/cat', ['/bin/cat','/f1','/f2'],approach="fusefs") + self.assertTrue(eventslist.count(['stat', '/f1'])==1) + def test_open_exists(self): + eventslist=fstracer.getfsevents('/bin/cat', ['/bin/cat','/etc/passwd'],approach="fusefs") + self.assertTrue(eventslist.count(['stat', '/etc/passwd'])>=1) + + def test_open_many(self): + filesnum=200 + eventslist=fstracer.getfsevents('/bin/cat',['/bin/cat']+ + map(lambda x: '/file'+str(x),range(0,filesnum)), approach="fusefs") + for f in map(lambda x: ['stat','/file'+str(x)],range(0,filesnum)): + self.assertTrue(f in eventslist) + + def test_parralel(self): + filesnum=200 + procnum=6 + + # create command + command="" + for p in xrange(0,procnum): + command+="/bin/cat " + for f in xrange(0,filesnum): + command+="/file_%d_%d " % (p,f) + command+="& " + command+=" >/dev/null 2>&1" + #command+=" "+"A"*65536 + + resultarray=fstracer.getfsevents('/bin/sh', ['/bin/sh','-c',command],approach="fusefs") + + #self.assertTrue(resultarray.count(['execve', '/bin/cat'])==procnum) + + print resultarray + + for p in xrange(0,procnum): + for f in xrange(0,filesnum): + self.assertTrue(resultarray.count(['stat', '/file_%d_%d' % (p,f)])==1) if __name__ == '__main__': - unittest.main()
\ No newline at end of file + #unittest.main() + suite = unittest.TestLoader().loadTestsFromTestCase(fusefs_simple_tests) + unittest.TextTestRunner(verbosity=2).run(suite)
\ No newline at end of file diff --git a/logger/src/hook_fusefs/hookfs.c b/logger/src/hook_fusefs/hookfs.c index 96d8958..af3e97a 100644 --- a/logger/src/hook_fusefs/hookfs.c +++ b/logger/src/hook_fusefs/hookfs.c @@ -36,6 +36,7 @@ #endif #include <sys/socket.h> #include <sys/un.h> +#include <pthread.h> #define MIN(a, b) (((a) < (b)) ? (a) : (b)) @@ -47,6 +48,8 @@ struct hookfs_config { }; +pthread_mutex_t socketblock = PTHREAD_MUTEX_INITIALIZER; + int mountpoint_fd = -1; char *mountpoint = NULL; FILE * log_file = NULL; @@ -78,7 +81,9 @@ void __print_escaped(FILE *fh ,const char *s){ * Format of log string: time event file flags result parents */ void log_event(const char *event_type, const char *filename, char *result, int err, pid_t pid) { + pthread_mutex_lock( &socketblock ); + fprintf(log_file,"%lld ",(unsigned long long)time(NULL)); __print_escaped(log_file, event_type); @@ -86,6 +91,9 @@ void log_event(const char *event_type, const char *filename, char *result, int e __print_escaped(log_file, filename); fprintf(log_file," %d %s\n", pid, result); fflush(log_file); + + pthread_mutex_unlock( &socketblock ); + } /* @@ -96,7 +104,10 @@ int is_event_allowed(const char *event_type,const char *filename, pid_t pid) { // sending asking log_event log_event(event_type,filename,"ASKING",0,pid); char answer[8]; + + pthread_mutex_lock( &socketblock ); fscanf(log_file,"%7s",answer); + pthread_mutex_unlock( &socketblock ); if(strcmp(answer,"ALLOW")==0) return 1; |