/usr/lib/python2.7/dist-packages/kmodpy/kmod.py is in python-kmodpy 0.1.10-2.1~deb9u1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2014 Chrysostomos Nanakos <chris@include.gr>
#
# This file is part of kmodpy.
#
# kmodpy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with kmodpy. If not, see <http://www.gnu.org/licenses/>.
from ctypes import (
POINTER,
byref,
addressof
)
from _libkmod import *
from error import KmodError
import errno
class Kmod(object):
"Kmod Class -- Python interface to kmod API"
def __init__(self, mod_dir=None):
self._kmod_ctx = kmod_new(mod_dir, None)
if self._kmod_ctx:
kmod_load_resources(self._kmod_ctx)
def __offsetof(self, typ, membname):
return getattr(typ, membname).offset
def __container_of(self, membaddr, typ, membname):
return typ.from_address(addressof(membaddr) -
self.__offsetof(typ, membname))
def loaded(self):
"""
Generate currently loaded modules, use_count and sizes
"""
klist = POINTER(kmod_list)()
err = kmod_module_new_from_loaded(self._kmod_ctx, byref(klist))
if err:
raise KmodError('Could not get loaded modules')
itr = klist.contents.node.next
while True:
itrnode = self.__container_of(itr.contents, kmod_list, "node")
mod = kmod_module_get_module(itrnode)
name = kmod_module_get_name(mod)
use_count = kmod_module_get_refcnt(mod)
size = kmod_module_get_size(mod)
kmod_module_unref(mod)
yield (name, use_count, size)
if addressof(klist.contents) == addressof(itr.contents):
break
itr = itr.contents.next
def list(self):
"""
Generate currently loaded modules
"""
for mod in self.loaded():
yield (mod[0], mod[2])
def lookup(self, alias_name):
"""
Generate modules matching 'alias_name'
"""
if hasattr(alias_name, 'encode'):
alias_name = alias_name.encode('ascii')
mlist = POINTER(kmod_list)()
err = kmod_module_new_from_lookup(self._kmod_ctx, alias_name,
byref(mlist))
if err < 0:
raise KmodError('Could not modprobe')
if not mlist:
return
itr = mlist.contents.node.next
while True:
itrnode = self.__container_of(itr.contents, kmod_list, "node")
mod = kmod_module_get_module(itrnode)
yield mod
if addressof(mlist.contents) == addressof(itr.contents):
break
itr = itr.contents.next
def modinfo(self, name):
"""
Generate modules parameters and info
"""
mods = list(self.lookup(name))
if not mods:
raise KmodError("Could not modinfo '%s'" % name)
ilist = POINTER(kmod_list)()
err = kmod_module_get_info(mods[0], byref(ilist))
if err < 0:
raise KmodError("Could not modinfo '%s'" % name)
if not ilist:
return
itr = ilist.contents.node.next
while True:
itrnode = self.__container_of(itr.contents, kmod_list, "node")
key = kmod_module_info_get_key(itrnode)
value = kmod_module_info_get_value(itrnode)
yield (key, value)
if addressof(ilist.contents) == addressof(itr.contents):
kmod_module_info_free_list(ilist)
break
itr = itr.contents.next
def show_depends(self, name):
"""
Generate module dependencies
"""
mods = list(self.lookup(name))
if not mods:
raise KmodError("Could not modprobe '%s'" % name)
flist = kmod_module_get_dependencies(mods[0])
if not flist:
return
itr = flist.contents.node.next
while True:
itrnode = self.__container_of(itr.contents, kmod_list, "node")
m = kmod_module_get_module(itrnode)
name = kmod_module_get_name(m)
yield name
if addressof(flist.contents) == addressof(itr.contents):
break
itr = itr.contents.next
def __insert(self, mod, flags=0, extra_options=None):
err = kmod_module_insert_module(mod, flags, extra_options)
if err == -errno.EEXIST:
raise KmodError('Module already loaded')
elif err < 0:
raise KmodError('Could not load module')
def __remove(self, mod, flags=0):
err = kmod_module_remove_module(mod, flags)
if err < 0:
raise KmodError('Could not remove module')
def modprobe(self, name, quiet=False, *args, **kwargs):
"""
Load a module and all dependent modules.
The 'quiet' options defaults to False; set True to mimic the behavior
of the '--quiet' commandline options.
"""
mods = list(self.lookup(name))
if not mods and not quiet:
raise KmodError("Could not modprobe '%s'" % name)
for mod in mods:
self.__insert(mod, *args, **kwargs)
def rmmod(self, name, *args, **kwargs):
"Remove module from current tree"
mods = list(self.lookup(name))
if not mods:
raise KmodError("Could not rmmod '%s'" % name)
for mod in mods:
self.__remove(mod, *args, **kwargs)
|