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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
from Plugins.Plugin import PluginDescriptor
from Components.Harddisk import harddiskmanager
from Tools.Directories import fileExists
hotplugNotifier = [ ]
bdpoll = None
def processHotplugData(self, v):
print "hotplug:", v
action = v.get("ACTION")
device = v.get("DEVPATH")
physdevpath = v.get("PHYSDEVPATH")
media_state = v.get("X_E2_MEDIA_STATUS")
dev = device.split('/')[-1]
if action is not None and action == "add":
error, blacklisted, removable, is_cdrom, partitions, medium_found = harddiskmanager.addHotplugPartition(dev, physdevpath)
if bdpoll and removable or is_cdrom:
bdpoll.addDevice(dev, is_cdrom, medium_found)
elif action is not None and action == "remove":
if bdpoll:
bdpoll.removeDevice(dev)
harddiskmanager.removeHotplugPartition(dev)
elif media_state is not None:
if media_state == '1':
harddiskmanager.removeHotplugPartition(dev)
harddiskmanager.addHotplugPartition(dev, physdevpath)
elif media_state == '0':
harddiskmanager.removeHotplugPartition(dev)
for callback in hotplugNotifier:
try:
callback(dev, action or media_state)
except AttributeError:
hotplugNotifier.remove(callback)
CDROM_DRIVE_STATUS = 0x5326
CDROM_MEDIA_CHANGED = 0x5325
CDSL_CURRENT = ((int)(~0>>1))
CDS_NO_INFO = 0
CDS_NO_DISC = 1
CDS_TRAY_OPEN = 2
CDS_DRIVE_NOT_READY = 3
CDS_DISC_OK = 4
ENOMEDIUM = 159
IOC_NRBITS = 8
IOC_NRSHIFT = 0
IOC_TYPESHIFT = (IOC_NRSHIFT+IOC_NRBITS)
BLKRRPART = ((0x12<<IOC_TYPESHIFT) | (95<<IOC_NRSHIFT))
def autostart(reason, **kwargs):
if reason == 0:
print "starting hotplug handler"
if fileExists('/dev/.udev'):
global netlink
global bdpoll
from enigma import eSocketNotifier, eTimer, ePythonMessagePump
import socket
from select import POLLIN, POLLPRI
class Netlink:
def __init__(self):
self.netlink = socket.socket(socket.AF_NETLINK, socket.SOCK_DGRAM, 15)
self.netlink.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 65536)
self.netlink.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 65536)
self.netlink.bind((0, 1))
self.sn = eSocketNotifier(self.netlink.fileno(), POLLIN|POLLPRI)
self.sn.callback.append(self.dataAvail)
def dataAvail(self, what):
received = self.netlink.recvfrom(16384)
# print "HOTPLUG(%d):" %(what), received
data = received[0].split('\0')[:-1]
v = {}
for x in data:
i = x.find('=')
var, val = x[:i], x[i+1:]
v[var] = val
if v['SUBSYSTEM'] == 'block' and v['ACTION'] in ('add', 'remove'):
processHotplugData(self, v)
from threading import Thread, Semaphore, Lock
class ThreadQueue:
def __init__(self):
self.__list = [ ]
self.__lock = Lock()
def push(self, val):
list = self.__list
lock = self.__lock
lock.acquire()
list.append(val)
lock.release()
def pop(self):
list = self.__list
lock = self.__lock
lock.acquire()
ret = list[0]
del list[0]
lock.release()
return ret
import os
import errno
import fcntl
class BDPoll(Thread):
CHECK_INTERVAL = 2000
MSG_MEDIUM_REMOVED = 1
MSG_MEDIUM_INSERTED = 2
MSG_POLL_FINISHED = 4
def __init__(self):
Thread.__init__(self)
self.__sema = Semaphore(0)
self.__lock = Lock()
self.running = False
self.devices_to_poll = { }
self.messages = ThreadQueue()
self.checkTimer = eTimer()
self.checkTimer.callback.append(self.timeout)
self.checkTimer.start(BDPoll.CHECK_INTERVAL, True)
self.mp = ePythonMessagePump()
self.mp.recv_msg.get().append(self.gotThreadMsg)
self.start()
def gotThreadMsg(self, msg):
msg = self.messages.pop()
if msg[0] == BDPoll.MSG_MEDIUM_REMOVED:
print "MSG_MEDIUM_REMOVED"
harddiskmanager.removeHotplugPartition(msg[1])
elif msg[0] == BDPoll.MSG_MEDIUM_INSERTED:
print "MSG_MEDIUM_INSERTED"
harddiskmanager.addHotplugPartition(msg[1])
elif msg[0] == BDPoll.MSG_POLL_FINISHED:
self.checkTimer.start(BDPoll.CHECK_INTERVAL, True)
def timeout(self):
self.__sema.release() # start bdpoll loop in thread
def is_mounted(self, dev):
mounts = file('/proc/mounts').read()
return mounts.find(dev) != -1
def run(self):
sema = self.__sema
lock = self.__lock
messages = self.messages
mp = self.mp
self.running = True
while self.running:
sema.acquire()
self.__lock.acquire()
devices_to_poll = self.devices_to_poll.items()
self.__lock.release()
devices_to_poll_processed = [ ]
for device, state in devices_to_poll:
got_media = False
is_cdrom, prev_media_state = state
if is_cdrom:
try:
fd = os.open("/dev/" + device, os.O_RDONLY | os.O_NONBLOCK | os.O_EXCL)
except OSError, err:
if err.errno == errno.EBUSY:
print "open cdrom exclusive failed:",
if not self.is_mounted(device):
print "not mounted"
continue
try:
print "mounted... try non exclusive"
fd = os.open("/dev/" + device, os.O_RDONLY | os.O_NONBLOCK)
except OSError, err:
print "open cdrom not exclusive failed", os.strerror(err.errno)
continue
#here the fs must be valid!
try:
ret = fcntl.ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT)
except IOError, err:
print "ioctl CDROM_DRIVE_STATUS failed", os.strerror(err.errno)
else:
if ret in (CDS_NO_INFO, CDS_NO_DISC, CDS_TRAY_OPEN, CDS_DRIVE_NOT_READY):
pass
elif ret == CDS_DISC_OK:
#todo new kernels support events to userspace event on media change
#but not 2.6.18.... see hotplug-ng bdpoll.c
got_media = True
os.close(fd)
else:
try:
fd = os.open("/dev/" + device, os.O_RDONLY)
except OSError, err:
if err.errno == ENOMEDIUM:
pass
else:
print "open non cdrom failed", os.strerror(err.errno)
continue
else:
got_media = True
os.close(fd)
if prev_media_state:
if not got_media:
print "media removal detected on", device
try:
fd = os.open("/dev/" + device, os.O_RDONLY | os.O_NONBLOCK)
except OSError, err:
print "open device for blkrrpart ioctl failed", os.strerror(err.errno)
else:
try:
fcntl.ioctl(fd, BLKRRPART)
except IOError, err:
print "ioctl BLKRRPART failed", os.strerror(err.errno)
os.close(fd)
else:
if got_media:
print "media insertion detected on", device
devices_to_poll_processed.append((device, is_cdrom, got_media))
self.__lock.acquire()
for device, is_cdrom, state in devices_to_poll_processed:
old_state = self.devices_to_poll.get(device)
if old_state is not None and old_state[1] != state:
msg = state and BDPoll.MSG_MEDIUM_INSERTED or BDPoll.MSG_MEDIUM_REMOVED
self.devices_to_poll[device] = (is_cdrom, state)
messages.push((msg, device))
mp.send(0)
self.__lock.release()
messages.push((self.MSG_POLL_FINISHED,))
mp.send(0)
def addDevice(self, device, is_cdrom, inserted):
self.__lock.acquire()
if device in self.devices_to_poll:
print "device", device, "already in bdpoll"
else:
print "add device", device, "to bdpoll current state:",
if inserted:
print "medium inserted"
else:
print "medium removed"
self.devices_to_poll[device] = (is_cdrom, inserted)
self.__lock.release()
def removeDevice(self, device):
self.__lock.acquire()
if device in self.devices_to_poll:
print "device", device, "removed from bdpoll"
del self.devices_to_poll[device]
else:
print "try to del not exist device", device, "from bdpoll"
self.__lock.release()
netlink = Netlink()
bdpoll = BDPoll()
for blockdev, removable, is_cdrom, medium_found in harddiskmanager.devices_scanned_on_init:
if removable or is_cdrom:
bdpoll.addDevice(blockdev, is_cdrom, medium_found)
else:
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
try:
import os
os.remove("/tmp/hotplug.socket")
except OSError:
pass
class Hotplug(Protocol):
def connectionMade(self):
print "HOTPLUG connection!"
self.received = ""
def dataReceived(self, data):
print "hotplug:", data
self.received += data
print "complete", self.received
def connectionLost(self, reason):
print "HOTPLUG connection lost!"
data = self.received.split('\0')[:-1]
v = {}
for x in data:
i = x.find('=')
var, val = x[:i], x[i+1:]
v[var] = val
processHotplugData(self, v)
factory = Factory()
factory.protocol = Hotplug
reactor.listenUNIX("/tmp/hotplug.socket", factory)
def Plugins(**kwargs):
return PluginDescriptor(name = "Hotplug", description = "listens to hotplug events", where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)
|