标准 Linux 库中是否有函数(或接口(interface);ioctl、netlink 等)可以直接从内核返回当前挂载而不解析/proc? strace
挂载命令,看起来是解析/proc中的文件
最佳答案
Please see the clarification at the bottom of the answer for the reasoning being used in this answer.
您有什么理由不使用 getmntent
libc 库调用?我确实意识到它与“一体式”系统调用不同,但它应该能让您获得相关信息。
#include <stdio.h>
#include <stdlib.h>
#include <mntent.h>
int main(void)
{
struct mntent *ent;
FILE *aFile;
aFile = setmntent("/proc/mounts", "r");
if (aFile == NULL) {
perror("setmntent");
exit(1);
}
while (NULL != (ent = getmntent(aFile))) {
printf("%s %s\n", ent->mnt_fsname, ent->mnt_dir);
}
endmntent(aFile);
}
澄清
考虑到 OP 澄清了如何安装 /proc
尝试执行此操作,我将澄清:
There is no facility outside of
/proc
for getting the fully qualified list of mounted file systems from the linux kernel. There is no system call, there is no ioctl. The/proc
interface is the agreed upon interface.
话虽如此,如果你没有安装/proc
,你将不得不解析/etc/mtab
文件——传入/etc/mtab
而不是 /proc/mounts
到初始 setmntent
调用。
mount
和 unmount
命令 将在文件中维护当前已安装文件系统的列表/etc/mtab.这在几乎所有 linux 中都有详细说明/unix/bsd这些命令的手册页。因此,如果您没有 /proc
,您可以某种程度上依赖此文件的内容。它不能保证是真理的来源,但惯例就是这些事情的惯例。
因此,如果您没有/proc
,您可以在下面的getmntent
libc 库调用中使用/etc/mtab
来获取文件系统列表;否则,您可以使用 /proc/mounts
或 /proc/self/mountinfo
之一(现在推荐使用 /proc/mounts
)。
关于c - linux函数获取挂载点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9280759/