Search code examples
c++linuxbatteryprocfsdisk-partitioning

Linux c++: apis vs /proc files?


Im working on an app to collect and send various bits of system info (partition space/free, laptop battery info, etc). Im not having much success getting this information in the form of direct c++ api.. though its all available via files in /proc (or similar).

So - I'm wondering whether reading/parsing these files in my c++ app is the appropriate way to get this info or should I keep trying to discover APIs? ( NOTE: I am working with statvfs ).

So far it looks like it's easier to gather this sort of info in Win32. Seems strange.


Solution

  • It is best practice by far to stick with an API in the following order of precedence.

    • Your language API (not much help for you here, but say for strings, a C99 string function is better to use than a library string facility specified by a Posix or other OS standard.)

    • Posix operating software APIs

    • Documented kernel API's

    • Undocumented kernel APIs (at least these will break, say, ioctl users if they change, so they probably won't change)

    • /proc

    • /dev/kmem, /dev/mem

    There is no reason to believe that /proc trolling will be portable or even the same from release to release. Not every system will even have a /proc mounted!

    Having said all that, it is much easier to just scrape stuff off of /proc and if it's the only available interface then you should go ahead and use it.qa

    Finally, the ordering of the last two isn't completely clear, because /proc isn't available for a post-mortem kernel crash dump analysis, but tools that can peek in the core dump will still work.