Python的ctypes的调用由libc中重新启动()在Linux上 [英] Python ctypes calling reboot() from libc on Linux

查看:585
本文介绍了Python的ctypes的调用由libc中重新启动()在Linux上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 ctypes的来调用在Python libc中的重新启动功能,我只是不能它的工作。我一直在引用 2人重新启动页(http://linux.die.net/man/2/reboot)。我的内核版本2.6.35是

I'm trying to call the reboot function from libc in Python via ctypes and I just can not get it to work. I've been referencing the man 2 reboot page (http://linux.die.net/man/2/reboot). My kernel version is 2.6.35.

下面是从交互式Python提示符,我试图让我的机器Reboot-重新什么我做错了控制台日志?

Below is the console log from the interactive Python prompt where I'm trying to get my machine to reboot- what am I doing wrong?

为什么不是 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 reminder-我可以得到 get_errno 返回22(无效参数)。不是一个惊喜。我应该如何调用重新启动()?我显然不是传递参数的函数需要。 =)

Via Nemos reminder- I can get get_errno to return 22 (invalid argument). Not a surprise. How should I be calling reboot()? I'm clearly not passing arguments the function expects. =)

推荐答案

尝试:

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

这应该让 get_errno()工作

[更新]

此外,最后一个参数是一个无效* 。如果这是一个64位的系统,那么整数0不是空的有效repesentation。我会尝试或者 c_void_p(无)。 (不知道怎么会在这种情况下无​​所谓,但。)

Also, the last argument is a void *. If this is a 64-bit system, then the integer 0 is not a valid repesentation for NULL. I would try None or maybe c_void_p(None). (Not sure how that could matter in this context, though.)

[更新2]

显然重新启动(0x1234567)的伎俩(见注释)。

Apparently reboot(0x1234567) does the trick (see comments).

这篇关于Python的ctypes的调用由libc中重新启动()在Linux上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆