[ACCEPTED]-Linux function to get mount points-libc

Accepted answer
Score: 45

Please see the clarification at the bottom 27 of the answer for the reasoning being used 26 in this answer.

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

#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) {
  while (NULL != (ent = getmntent(aFile))) {
    printf("%s %s\n", ent->mnt_fsname, ent->mnt_dir);


Considering 21 that the OP clarified about trying to do 20 this without having /proc mounted, I'm going to clarify:

There 19 is no facility outside of /proc for getting the 18 fully qualified list of mounted file systems 17 from the linux kernel. There is no system 16 call, there is no ioctl. The /proc interface 15 is the agreed upon interface.

With that said, if 14 you don't have /proc mounted, you will have to 13 parse the /etc/mtab file - pass in /etc/mtab instead of /proc/mounts to 12 the initial setmntent call.

It is an agreed upon protocol 11 that the mount and unmount commands will maintain a list of currently mounted filesystems in the file /etc/mtab. This is detailed 10 in almost all linux/unix/bsd manual pages for these 9 commands. So if you don't have /proc you can 8 sort of rely on the contents of this file. It's 7 not guaranteed to be a source of truth, but 6 conventions are conventions for these things.

So, if 5 you don't have /proc, you would use /etc/mtab in the getmntent libc 4 library call below to get the list of file 3 systems; otherwise you could use one of 2 /proc/mounts or /proc/self/mountinfo (which is recommended nowadays over 1 /proc/mounts).

Score: 2

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

More Related questions