Python ctypes 在 Linux 上从 libc 调用 reboot()

标签 python c linux ctypes libc

我试图通过 ctypes 从 Python 中的 libc 调用 reboot 函数,但我无法让它工作。我一直在引用 man 2 reboot 页面 ( http://linux.die.net/man/2/reboot )。我的内核版本是 2.6.35。

下面是来自交互式 Python 提示的控制台日志,我在其中尝试让我的机器重新启动 - 我做错了什么?

为什么 ctypes.get_errno() 不工作?

>>> from ctypes import CDLL, get_errno
>>> libc = CDLL('libc.so.6')
>>> libc.reboot(0xfee1dead, 537993216, 0x1234567, 0)
-1
>>> get_errno()
0
>>> libc.reboot(0xfee1dead, 537993216, 0x1234567)
-1
>>> get_errno()
0
>>> from ctypes import c_uint32
>>> libc.reboot(c_uint32(0xfee1dead), c_uint32(672274793), c_uint32(0x1234567), c_uint32(0))
-1
>>> get_errno()
0
>>> libc.reboot(c_uint32(0xfee1dead), c_uint32(672274793), c_uint32(0x1234567))
-1
>>> get_errno()
0
>>>

编辑:

通过 Nemos 提醒 - 我可以让 get_errno 返回 22(无效参数)。不足为奇。我应该如何调用 reboot()?我显然没有传递函数期望的参数。 =)

最佳答案

尝试:

>>> libc = CDLL('libc.so.6', use_errno=True)

这应该允许 get_errno() 工作。

[更新]

此外,最后一个参数是 void *。如果这是 64 位系统,则整数 0 不是 NULL 的有效表示。我会尝试 None 或者 c_void_p(None)。 (不过不确定在这种情况下这有什么关系。)

[更新 2]

显然 reboot(0x1234567) 可以解决问题(见评论)。

关于Python ctypes 在 Linux 上从 libc 调用 reboot(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6195575/

相关文章:

python脚本读取文本文件并将其解析为csv格式

python - 数据框中的 n 个最高值

c - 在 switch 语句中初始化变量

python - 从二维矩阵获取坐标列表

python - H2O服务器错误 : Cluster reports unhealthy status

c - 我如何保证在释放内存时,操作系统会回收该内存供其使用?

c - 在结构中分配二维数组的值

linux - 编译ffprobe linux库

ruby - 在本地文件夹中安装 gem

linux - 用于制作数据库备份的 Shell 脚本只能在提示符下运行