Coverage for linuxpy/ioctl.py: 91%
37 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-21 07:58 +0200
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-21 07:58 +0200
1#
2# This file is part of the linuxpy project
3#
4# Copyright (c) 2023 Tiago Coutinho
5# Distributed under the GPLv3 license. See LICENSE for more info.
7"""ioctl helper functions"""
9import fcntl
10import logging
12from .ctypes import calcsize, i32, sizeof
14NRBITS = 8
15TYPEBITS = 8
16SIZEBITS = 14
17DIRBITS = 2
19NRSHIFT = 0
20TYPESHIFT = NRSHIFT + NRBITS
21SIZESHIFT = TYPESHIFT + TYPEBITS
22DIRSHIFT = SIZESHIFT + SIZEBITS
24NONE = 0
25WRITE = 1
26READ = 2
28log = logging.getLogger(__name__)
31def IOC(direction, magic, number, size):
32 """Generic IOC call"""
33 if isinstance(magic, str):
34 magic = ord(magic)
35 if isinstance(size, str): 35 ↛ 36line 35 didn't jump to line 36 because the condition on line 35 was never true
36 size = calcsize(size)
37 elif size == int: 37 ↛ 38line 37 didn't jump to line 38 because the condition on line 37 was never true
38 size = 4
39 elif not isinstance(size, int):
40 size = sizeof(size)
41 return (
42 i32(direction << DIRSHIFT).value
43 | i32(magic << TYPESHIFT).value
44 | i32(number << NRSHIFT).value
45 | i32(size << SIZESHIFT).value
46 )
49def IO(magic, number):
50 return IOC(NONE, magic, number, 0)
53def IOW(magic, number, size):
54 return IOC(WRITE, magic, number, size)
57def IOR(magic, number, size):
58 return IOC(READ, magic, number, size)
61def IOWR(magic, number, size):
62 return IOC(READ | WRITE, magic, number, size)
65def ioctl(fd, request, *args):
66 log.debug("%s, request=%s, arg=%s", fd, request, args)
67 fcntl.ioctl(fd, request, *args)
68 return args and args[0] or None