mirror of
https://github.com/adulau/aha.git
synced 2024-12-28 03:36:19 +00:00
Focus only on decisions and retrieval of messages; rest went to ahalib
This commit is contained in:
parent
317b01bcf4
commit
3859a6d83b
1 changed files with 35 additions and 76 deletions
89
aha/aha.py
89
aha/aha.py
|
@ -5,110 +5,69 @@
|
||||||
import os,sys,random
|
import os,sys,random
|
||||||
from pyinotify import *
|
from pyinotify import *
|
||||||
from ctypes import *
|
from ctypes import *
|
||||||
|
from ahalib import *
|
||||||
KERNEL_OUT="/home/gerard/kernel/linux-2.6/out"
|
KERNEL_OUT="/home/gerard/kernel/linux-2.6/out"
|
||||||
KERNEL_IN="/home/gerard/kernel/linux-2.6/in"
|
KERNEL_IN="/home/gerard/kernel/linux-2.6/in"
|
||||||
|
insultmaxidx = 3
|
||||||
|
|
||||||
class KERNEL_ERRORS():
|
|
||||||
EPERM = -1
|
|
||||||
ENOENT = -2
|
|
||||||
EIO = -5
|
|
||||||
ENOMEM = -12
|
|
||||||
EACESS = -13
|
|
||||||
EFAULT = -14
|
|
||||||
EPIPE = -32
|
|
||||||
ETXTBSY = -26
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.evec = (EPERM,ENOENT,EIO,ENOMEM,EACESS,EFAULT,EPIPE,ETXTBSY)
|
|
||||||
|
|
||||||
|
|
||||||
class ReplyMessage(Structure):
|
|
||||||
_fields_ = [ ("block" , c_int), ("exitcode" , c_int),
|
|
||||||
("substitue" ,c_int),("insult" , c_int) ]
|
|
||||||
|
|
||||||
class KernelEvents(ProcessEvent):
|
class KernelEvents(ProcessEvent):
|
||||||
def silent_clean(self,filename):
|
def __init__(self,inqueue,outqueue,insultmaxidx):
|
||||||
try:
|
self.ahaa = AHAActions(inqueue,outqueue)
|
||||||
os.unlink(filename)
|
|
||||||
except OSError,e:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def create_message(self,filename,block,exitcode,substitue,insult):
|
|
||||||
reply = ReplyMessage(block=block,exitcode=exitcode,substitue=substitue,
|
|
||||||
insult = insult)
|
|
||||||
fn = KERNEL_IN + os.sep + filename
|
|
||||||
f = open (fn,'wb')
|
|
||||||
f.write(reply)
|
|
||||||
f.close()
|
|
||||||
reply="(key=%s, block=%d,exitcode=%d,substitue=%d,insult=%d)"\
|
|
||||||
%(filename,block,exitcode, substitue,insult)
|
|
||||||
print reply
|
|
||||||
|
|
||||||
def load_file(self,filename):
|
|
||||||
msg = {}
|
|
||||||
fp = open(filename,'r')
|
|
||||||
for i in fp.read().split('\n'):
|
|
||||||
try:
|
|
||||||
(key,value) = i.split('=')
|
|
||||||
except ValueError,e:
|
|
||||||
pass
|
|
||||||
if msg.has_key(key) == False:
|
|
||||||
msg[key]=[]
|
|
||||||
msg[key].append(value)
|
|
||||||
|
|
||||||
fp.close()
|
|
||||||
return msg
|
|
||||||
|
|
||||||
def decision(self,filekey,msg):
|
def decision(self,filekey,msg):
|
||||||
insultmaxidx = 3
|
|
||||||
print msg
|
|
||||||
try:
|
try:
|
||||||
command = msg['file'][0]
|
command = msg['file'][0]
|
||||||
print "Got command: ",command
|
print "Got command: ",command
|
||||||
if msg['file'][0] == '/usr/bin/bvi':
|
if msg['file'][0] == '/usr/bin/bvi':
|
||||||
self.create_message(filekey, block=1,
|
self.ahaa.create_message(filekey, block=1,
|
||||||
exitcode=KERNEL_ERRORS.ENOMEM,
|
exitcode=KERNEL_ERRORS.ENOMEM,
|
||||||
insult = 0, substitue=0)
|
insult = 0, substitue=0)
|
||||||
return
|
return
|
||||||
if msg['file'][0] == '/usr/bin/vi':
|
if msg['file'][0] == '/usr/bin/vi':
|
||||||
# The index 0 is reserved
|
# The index 0 is reserved
|
||||||
idx = random.randint(1,insultmaxidx)
|
idx = random.randint(1,insultmaxidx)
|
||||||
self.create_message(filekey, block=0, exitcode=0, insult=idx, substitue=0)
|
self.ahaa.create_message(filekey, block=0, exitcode=0,
|
||||||
|
insult=idx, substitue=0)
|
||||||
return
|
return
|
||||||
except KeyError,e:
|
except KeyError,e:
|
||||||
pass
|
pass
|
||||||
except IndexError,w:
|
except IndexError,w:
|
||||||
pass
|
pass
|
||||||
#Default action; allow-> out of memory
|
#Default action; allow-> out of memory
|
||||||
self.create_message(filekey,block=0,exitcode=0,insult=0,substitue=0)
|
self.ahaa.create_message(filekey,block=0,exitcode=0,insult=0,
|
||||||
|
substitue=0)
|
||||||
|
|
||||||
def process_IN_CLOSE_WRITE(self, event):
|
def process_IN_CLOSE_WRITE(self, event):
|
||||||
|
try:
|
||||||
filename = os.path.join(event.path,event.name)
|
filename = os.path.join(event.path,event.name)
|
||||||
msg = self.load_file(filename)
|
msg = self.ahaa.load_file(filename)
|
||||||
#Send back a message
|
#Send back a message
|
||||||
self.decision(event.name,msg)
|
self.decision(event.name,msg)
|
||||||
#Cleanup the file
|
except IOError,e:
|
||||||
self.silent_clean(filename)
|
sys.stderr.write("Kernel message (%s) could not be loaded or \
|
||||||
|
decison failed\n"%event.name)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print "Setting up listeners..."
|
||||||
|
|
||||||
wm = WatchManager()
|
wm = WatchManager()
|
||||||
|
mask = IN_CLOSE_WRITE # watched events
|
||||||
|
|
||||||
mask = IN_CLOSE_WRITE # watched events
|
notifier = Notifier(wm, KernelEvents(KERNEL_IN,KERNEL_OUT,insultmaxidx))
|
||||||
|
wdd = wm.add_watch(KERNEL_OUT, mask, rec=True)
|
||||||
|
|
||||||
notifier = Notifier(wm, KernelEvents())
|
print "Waiting for events..."
|
||||||
wdd = wm.add_watch(KERNEL_OUT, mask, rec=True)
|
while True:
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
try:
|
||||||
# process the queue of events as explained above
|
# process the queue of events as explained above
|
||||||
notifier.process_events()
|
notifier.process_events()
|
||||||
if notifier.check_events():
|
if notifier.check_events():
|
||||||
# read notified events and enqeue them
|
# read notified events and enqeue them
|
||||||
notifier.read_events()
|
notifier.read_events()
|
||||||
#TODO manage a global queue of unfinished events
|
|
||||||
#If inotify on close works this should not be necessary
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
# destroy the inotify's instance on this interrupt (stop monitoring)
|
# destroy the inotify's instance on this interrupt (stop monitoring)
|
||||||
|
print "Stop listening..."
|
||||||
notifier.stop()
|
notifier.stop()
|
||||||
break
|
break
|
||||||
|
sys.exit(0)
|
||||||
|
|
Loading…
Reference in a new issue