块读取直到Memfd_create中可用的数据

发布于 2025-02-05 08:47:00 字数 509 浏览 3 评论 0原文

我正在尝试在内存中创建一个文件,该文件的行为就像字符设备一样。我已经使用了memfd_create(Path,0),但是我开始认为这不是我需要的。 从本质上讲,我想做的是通过ld_preload打开钩子,寻找open(“/dev/input/event0”),然后将返回的FD更改为我的MEMFD。然后,我想创建一种以挂钩应用程序来读取的队列,就像它是读取设备文件一样。

但是,即使有0个字节要读取,在我的memFD上使用时似乎也不会阻止()。你能把我指向正确的方向吗?

编辑:

按照要求,我提供代码,但是我不确定这是否有任何帮助,因为它实际上是两行...

struct input_event ev;
int kbd = memfd_create("/dev/input/event0", 0);
read(kbd, &ev, sizeof(ev)); //<-- How can I make this block by default?

I'm trying to create a file in memory, which behaves just like a character device. I've used memfd_create(path, 0), but I'm starting to think this is not what I need.
In essence what I want to do is create a hook via LD_PRELOAD on open, look for open("/dev/input/event0") and change the returned fd to my memfd. Then I want to create a sort of queue for the hooked app to read from just like it would if it were a read device file.

However read() seems to not block when used on my memfd even if there are 0 bytes to be read. Could you point me in the right direction?

EDIT:

As requested I'm providing the code, however I'm not quite sure if this will be of any help since it is literally two lines...

struct input_event ev;
int kbd = memfd_create("/dev/input/event0", 0);
read(kbd, &ev, sizeof(ev)); //<-- How can I make this block by default?

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

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

发布评论

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

评论(1

梦里泪两行 2025-02-12 08:47:00

哦。使用管道代替memfd。
然后从一端写入并在挂钩应用程序中阅读。

int kbd[2];
struct input_event ev;
pipe(kbd);
read(kbd[0], &ev, sizeof(ev));

D'oh. Use a pipe instead of a memfd.
Then write from one end and read in the hooked app.

int kbd[2];
struct input_event ev;
pipe(kbd);
read(kbd[0], &ev, sizeof(ev));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文