Detect the running Linux distribution at compile time and/or runtime?

615 Views Asked by At

I am making a program in Kali Linux, and I want to make it work only in Kali. I can use C to do it, just like :

#if defined(_WIN32) || defined(_WIN64)
   #define os_platform THISisWINDOWS

For checking if the OS of the user is windows and later in the script checking the defined variable os_platform, but I searched Google and everywhere, but I just can't find the macro for Kali Linux. If I do :

#if defined(__linux__)
   #define os_platform THISisLINUX

It can be any type of Linux, like Ubuntu, and Linuxes like those. What is the macro for Kali Linux?

2

There are 2 best solutions below

2
On BEST ANSWER

There's no macro for distributions. You can only do a "sniff test" to see what distribution it is by looking in /etc for particular files, and even then it can be faked.

The reason the macros exist in the first place is because Linux is radically different from BSD and Windows, so it's a necessary distinction. The difference between Kali and Debian is pretty subjective, Kali is based on Debian. Most Linux distributions are actually quite similar as far as compilers are concerned because they all use the same kernel and headers.

Things like /etc/redhat-release are distribution specific, so look for something like that. This can't be tested as an #ifdef though, it's something you'll need to put in your configure script.

0
On

Probably You can check /etc/issue file for particular distribution Name and then using file pointers extract the exact name & then Compare If it's a Ubuntu or Kali or any other Linux Machine.