安装保险丝在 python 中给出无效参数错误

发布于 2024-11-16 22:24:30 字数 1572 浏览 2 评论 0原文

我已将 python 代码写入点安装上的安装保险丝。它总是给出无效参数错误。我已经在 C 语言中尝试过相同的程序,并且运行良好。任何 python 大师都可以帮我找出问题所在吗,我已将代码粘贴到这里。

#!/usr/bin/python

import stat
import os
import ctypes
from ctypes.util import find_library

libc = ctypes.CDLL (find_library ("c"))

def fuse_mount_sys (mountpoint, fsname):
        fd = file.fileno (file ("/dev/fuse", 'w+'))
        if fd < 0:
                raise OSError("Could not open /dev/fuse")

        mnt_param = "%s,fd=%i,rootmode=%o,user_id=%i,group_id=%i" \
                        % ("allow_other,default_permissions,max_read=131072", \
                        fd, stat.S_IFDIR, os.getuid(), os.getgid())

        ret = libc.mount ("fuse", "/mount", "fuse", 0, mnt_param)
        if ret < 0:
                raise OSError("mount failed with code " + str(ret))
        return fd

fds = fuse_mount_sys ("/mount", "fuse")

安装语法是:

int mount(const char *source, const char *target,
                 const char *filesystemtype, unsigned long mountflags,
                 const void *data);

我尝试使用 swig 并使用 C 编写程序,然后从中创建一个 .so,它们工作了。但我对用纯 python 编写很感兴趣。提前致谢。

strace 的输出:

$ strace -s 100 -v -e mount python fuse-mount.py 
mount("fuse", "/mount", "fuse", 0, "allow_other,default_permissions,max_read=131072,fd=3,rootmode=40000,user_id=0,group_id=0") = -1 EINVAL (Invalid argument)


$ strace -s 100 -v -e mount ./a.out 
mount("fuse", "/mount", "fuse", 0, "allow_other,default_permissions,max_read=131072,fd=3,rootmode=40000,user_id=0,group_id=0") = 0

I have written python code to a mount fuse on a point mount. It always gives invalid argument error. I have tried the same program in C and it works fine. Can any of python Guru's help me out in finding what the problem is, I have pasted the code here.

#!/usr/bin/python

import stat
import os
import ctypes
from ctypes.util import find_library

libc = ctypes.CDLL (find_library ("c"))

def fuse_mount_sys (mountpoint, fsname):
        fd = file.fileno (file ("/dev/fuse", 'w+'))
        if fd < 0:
                raise OSError("Could not open /dev/fuse")

        mnt_param = "%s,fd=%i,rootmode=%o,user_id=%i,group_id=%i" \
                        % ("allow_other,default_permissions,max_read=131072", \
                        fd, stat.S_IFDIR, os.getuid(), os.getgid())

        ret = libc.mount ("fuse", "/mount", "fuse", 0, mnt_param)
        if ret < 0:
                raise OSError("mount failed with code " + str(ret))
        return fd

fds = fuse_mount_sys ("/mount", "fuse")

mount syntax is:

int mount(const char *source, const char *target,
                 const char *filesystemtype, unsigned long mountflags,
                 const void *data);

I tried using swig and also writing the program using in C and then creating a .so from it and they worked. But I am interested in writing in pure python. Thanks in advance.

output of strace:

$ strace -s 100 -v -e mount python fuse-mount.py 
mount("fuse", "/mount", "fuse", 0, "allow_other,default_permissions,max_read=131072,fd=3,rootmode=40000,user_id=0,group_id=0") = -1 EINVAL (Invalid argument)


$ strace -s 100 -v -e mount ./a.out 
mount("fuse", "/mount", "fuse", 0, "allow_other,default_permissions,max_read=131072,fd=3,rootmode=40000,user_id=0,group_id=0") = 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2024-11-23 22:24:30

ctypes.c_void_p 无法使用细绳。相反,只需使用不带 c_void_p 的字符串。

然后您可以比较 和 的输出

strace -v -e mount python mymount.py

strace -v -e mount ./mymount-c

直到它们匹配。

另外,请确保在调用 mount 时文件句柄 fd 仍然打开。 file("/dev/fuse", 'w+') 将被某些 Python 实现(包括 cpython)自动进行垃圾收集并关闭。您可以通过将 file("/dev/fuse") 的结果分配给变量来防止这种情况。

ctypes.c_void_p cannot be initialized with a string. Instead, just use a string without c_void_p.

You can then compare the output of

strace -v -e mount python mymount.py

and

strace -v -e mount ./mymount-c

until they match.

Also, make sure the file handle fd is still open when you call mount. file("/dev/fuse", 'w+') will be automatically garbage-collected and closed by some Python implementations, including cpython. You can prevent this by assigning the result of file("/dev/fuse") to a variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文