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

StackOverflow https://stackoverflow.com/questions/21318693

  •  01-10-2022
  •  | 
  •  

문제

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!

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top