Wednesday, November 17, 2021

[SOLVED] List mount points in C++ on Linux without using /proc/mounts, /etc/fstab, system() or popen()

Issue

I am looking for a solution to get the list of all tmpfs mount points available on my Linux system and I need to get it from a C/C++ program. I need a generic solution that is not distro dependent. I don't want to access /proc/mounts or /etc/fstab. I don't want to use a system() or popen().

Is there another way?

Thanks for your help!


Solution

The way mount implements this is to read /etc/mtab - strace mount is the fastest way to verify this if you don't have the source at hand. But as /etc/mtab is updated by mount/umount, and /proc/mounts is managed by the kernel itself, /proc/mounts is the better idea of the 2, and it's there on every linux system, independent of distro (hmm, maybe my Suse 4.4.2 which dates back to 1996 is an exception).

Or, if you want to be portable to non-linux Unixes, use the getmntent family of functions - but as the manual page states, other unixes have functions with the same name perform differently from the linux implementation, so while your code may compile on non-linux unixes, it won't neccesarily work correctly there.



Answered By - Guntram Blohm