How to determine present operating system (including specific distribution in the case of Linux) in a Vala program?

161 views Asked by At

I am interested in writing a Vala program that will determine the present operating system and act accordingly (exactly how it will act I have not decided yet, but is not relevant to this question). So what I would like to know is how I might determine the present operating system (including the specific distribution in the case of Linux) in a Vala program at runtime.

2

There are 2 answers

0
Jens Mühlenhoff On BEST ANSWER

Unless you are writing system level code (like package manager or OS configuration code), you shouldn't. A much better alternative is to use a library that already abstracts the distribution specifics for you.

If you absolutely have to there are two main ways to do it:

  1. At build time

    Here your build system should be responsible to detect the OS / distribution and either pass a define to the compiler (like -DDISTRO_UBUNTU) or write a config.vala file (possibly from a template config.vala.in with replacements, e.g. autotools has the AC_CONFIG_FILES facility to do this).

  2. At runtime

    Here your tool does the detection itself when it's running.

Which fits your application better is a design choice.

As to how to do it there are several things you can check:

  • uname -a (or other parameters, see man uname) will give you the kernel that is currently running.

  • lsb_release -a (not available on every distro, sometimes an optional package which you might have a package dependency to) will give you information on what distro and what distro version you are running on.

  • On Debian/Ubuntu derivates there is a file called /etc/debian_version which gives an indication of what release is currently installed. That information is not totally accurate though.

  • Some people are trying to read /etc/issue, but that is dangerous, since that file could be modified by the admin / the user.

  • You could ask the user which OS she is running.

There are also some os info libraries that you could use.

0
Basile Starynkevitch On

You might use the uname(2) syscall (how to do that in Vala is left as an exercise to the user), or read /proc/version (see proc(5)), or read /etc/issues or follow Linux Standard Base conventions (e.g.popen the output of lsb_release -a).

But as Jens Mühlenhoff answered, you should not do that, and avoid writing code depending on some particular distribution.

And some users might want to fake or hide that information (think of someone having some "Linux From Scratch" system).