[英]Python ctypes calling reboot() from libc on Linux
我正在尝试通过ctypes
从 Python 中的 libc 调用reboot
function,但我无法让它工作。 我一直在参考man 2 reboot
页面( http://linux.die.net/man/2/reboot )。 我的 kernel 版本是 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()
? 我显然没有通过 arguments 预期的 function 。 =)
尝试:
>>> libc = CDLL('libc.so.6', use_errno=True)
这应该允许get_errno()
工作。
[更新]
此外,最后一个参数是void *
。 如果这是 64 位系统,则 integer 0 不是 NULL 的有效表示。 我会尝试None
或者c_void_p(None)
。 (不过,不确定这在这种情况下有何影响。)
[更新 2]
显然reboot(0x1234567)
可以解决问题(见评论)。
libc
中的reboot()
是系统调用的包装器,它只接受cmd
参数。 所以试试:
libc.reboot(0x1234567)
请注意,您通常应该通过将SIGINT
发送到 PID 1 来启动重新启动 - 告诉 kernel 重新启动不会让任何系统守护进程有机会干净地关闭,甚至不会将文件系统缓存同步到磁盘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.