Linux函数获取挂载点

时间:2022-03-14 15:30:37

Is there a function (or interface; ioctl, netlink etc) in the standard Linux libs that will return the current mounts directly from the kernel without parsing /proc? straceing the mount command, it looks like it parses files in /proc

是否有函数(或接口;在标准的Linux libs中,将会直接从内核返回当前挂载,而不需要解析/proc?跨挂载命令,它看起来像是解析/proc中的文件

2 个解决方案

#1


32  

Is there any reason that you would not use the getmntent libc library call? I do realize that it's not the same as an 'all in one' system call, but it should allow you to get the relevant information.

有什么理由不使用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);
}

#2


2  

There is no syscall to list this information; instead, you can find it in the file /etc/mtab

没有syscall列出此信息;相反,您可以在文件/etc/mtab中找到它。

#1


32  

Is there any reason that you would not use the getmntent libc library call? I do realize that it's not the same as an 'all in one' system call, but it should allow you to get the relevant information.

有什么理由不使用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);
}

#2


2  

There is no syscall to list this information; instead, you can find it in the file /etc/mtab

没有syscall列出此信息;相反,您可以在文件/etc/mtab中找到它。