1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# Distributed under the terms of the GNU General Public License v2
import io
import sys
import stat
import socket
import select
import tempfile
import threading
from portage import os
class EventsLogger(threading.Thread):
def default_filter(eventname, filename, stage):
return True
def __init__(self, socket_dir="/tmp/", filter_proc=default_filter):
threading.Thread.__init__(self) # init the Thread
self.alive=False
self.main_thread=threading.currentThread()
self.socket_dir=socket_dir
self.filter_proc=filter_proc
self.socket_name=None
self.socket_logger=None
self.events={}
try:
socket_dir_name = tempfile.mkdtemp(dir=self.socket_dir,
prefix="log_socket_")
socket_name = os.path.join(socket_dir_name, 'socket')
except OSError as e:
return
self.socket_name=socket_name
#print(self.socket_name)
try:
socket_logger=socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
socket_logger.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
socket_logger.bind(self.socket_name)
socket_logger.listen(64)
except socket.error as e:
return
self.socket_logger=socket_logger
try:
# Allow connecting to socket for anyone
os.chmod(socket_dir_name,
stat.S_IRUSR|stat.S_IWUSR|stat.S_IXUSR|
stat.S_IROTH|stat.S_IWOTH|stat.S_IXOTH)
os.chmod(socket_name,
stat.S_IRUSR|stat.S_IWUSR|stat.S_IXUSR|
stat.S_IROTH|stat.S_IWOTH|stat.S_IXOTH)
except OSError as e:
return
def run(self):
""" Starts the log server """
self.alive=True
self.listen_thread=threading.currentThread()
clients={}
epoll=select.epoll()
epoll.register(self.socket_logger.fileno(), select.EPOLLIN)
while self.alive:
try:
sock_events = epoll.poll(3)
for fileno, sock_event in sock_events:
if fileno == self.socket_logger.fileno():
ret = self.socket_logger.accept()
if ret is None:
pass
else:
(client,addr)=ret
epoll.register(client.fileno(), select.EPOLLIN)
clients[client.fileno()]=client
elif sock_event & select.EPOLLIN:
s=clients[fileno]
record=s.recv(8192)
if not record: # if connection was closed
epoll.unregister(fileno)
clients[fileno].close()
del clients[fileno]
continue
#import pdb; pdb.set_trace()
#try:
message=record.split(b"\0")
#except UnicodeDecodeError:
# print("Bad message %s" % record)
# continue
# continue
#print(message)
try:
eventname,filename,stage,result=message[1:5]
eventname=eventname.decode("utf-8")
stage=stage.decode("utf-8")
result=result.decode("utf-8")
except IndexError:
print("IndexError while parsing %s" % record)
except ValueError:
print("ValueError while parsing %s" % record)
except UnicodeDecodeError:
print("UnicodeDecodeError while parsing %s" % record)
if result=="ASKING":
if self.filter_proc(eventname,filename,stage):
s.sendall(b"ALLOW\0")
else:
# TODO: log through portage infrastructure
#print("Blocking an access to %s" % message[2])
s.sendall(b"DENY\0")
else:
if not stage in self.events:
self.events[stage]=[{},{}]
hashofsucesses=self.events[stage][0]
hashoffailures=self.events[stage][1]
if result=="DENIED":
print("Blocking an access to %s" % filename)
if result=="OK":
if not filename in hashofsucesses:
hashofsucesses[filename]=[False,False]
readed_or_writed=hashofsucesses[filename]
if eventname=="read":
readed_or_writed[0]=True
elif eventname=="write":
readed_or_writed[1]=True
elif result[0:3]=="ERR" or result=="DENIED":
if not filename in hashoffailures:
hashoffailures[filename]=[False,False]
notfound_or_blocked=hashoffailures[filename]
if result=="ERR/2":
notfound_or_blocked[0]=True
elif result=="DENIED":
notfound_or_blocked[1]=True
else:
print("Error in logger module<->analyser protocol")
except IOError as e:
if e.errno!=4: # handling "Interrupted system call" errors
raise
# if main thread doesnt exists then exit
if not self.main_thread.is_alive():
break
epoll.unregister(self.socket_logger.fileno())
epoll.close()
self.socket_logger.close()
def stop(self):
""" Stops the log server. Returns all events """
self.alive=False
# Block the main thread until listener exists
self.listen_thread.join()
# We assume portage clears tmp folder, so no deleting a socket file
# We assume that no new socket data will arrive after this moment
#print(self.events)
return self.events
|