[英]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.