+
+def isBinderfsLoaded(args):
+ with open("/proc/filesystems", "r") as handle:
+ for line in handle:
+ words = line.split()
+ if len(words) >= 2 and words[1] == "binder":
+ return True
+
+ return False
+
+def allocBinderNodes(args, binder_dev_nodes):
+ NRBITS = 8
+ TYPEBITS = 8
+ SIZEBITS = 14
+ NRSHIFT = 0
+ TYPESHIFT = NRSHIFT + NRBITS
+ SIZESHIFT = TYPESHIFT + TYPEBITS
+ DIRSHIFT = SIZESHIFT + SIZEBITS
+ WRITE = 0x1
+ READ = 0x2
+
+ def IOC(direction, _type, nr, size):
+ return (direction << DIRSHIFT) | (_type << TYPESHIFT) | (nr << NRSHIFT) | (size << SIZESHIFT)
+
+ def IOWR(_type, nr, size):
+ return IOC(READ|WRITE, _type, nr, size)
+
+ BINDER_CTL_ADD = IOWR(98, 1, 264)
+ binderctrlfd = open('/dev/binderfs/binder-control','rb')
+
+ for node in binder_dev_nodes:
+ node_struct = struct.pack(
+ '256sII', bytes(node, 'utf-8'), 0, 0)
+ try:
+ fcntl.ioctl(binderctrlfd.fileno(), BINDER_CTL_ADD, node_struct)
+ except FileExistsError:
+ pass
+