PDA

View Full Version : Linux distro name



hosseinyounesi
07-26-2009, 10:19 AM
Hi everybody,
I want to get linux distribution name. I googled and found lsb_release :)
Does it a standard way? Does it installed on linux distributions as default?
Is there any library in C or C++ to use that or I have to use system(); ?

And is there any other way?

Thanks for your consideration

hosseinyounesi
07-26-2009, 10:25 AM
Maybe I had to mention this:
There are /etc/debian_version and /etc/issue files in debian based linux.

But any standard way to do this in all linux?

Salem
07-26-2009, 11:22 AM
uname is the command.

But whether there is a system call behind that, I've no idea.

MK27
07-26-2009, 11:29 AM
/etc/issue is probably on most systems.

The distro is not that relevant to C programming since all distos are GNU based.

If you are doing something a certain way and are not sure if it will work on all distros, rather than check the distro just test your method (eg, are the files you presume present, present?). In other words, aim to make it work where ever possible, and fail gracefully where it will not.

The other issue is that just because a system is based on a given distro does not mean you can rely on it having certain characteristics. A lot can be modified; if you presume that the system is a certain way just because it is "debian", you will easily get caught with your pants down. You must test for the characteristics themselves.

hosseinyounesi
07-26-2009, 11:35 AM
Thank you MK27 :)
My program is a service and I should add it to system services and it is distro dependant!!!
For debian based, redhat based and openSuse !!!!
I thought that the best way to do that is to get distro name and do the compatible commands for each of them!!!!

Is there any other way (or standard way to do this on all Linux)?

cyberfish
07-27-2009, 06:55 AM
Then test the presence of, say, /etc/init.d /etc/rcx.d, etc.

I believe that's what MK27 meant by testing for characteristics.

hosseinyounesi
07-27-2009, 08:48 AM
Hi,
I wrote this function :( It's not beauty!!! but it can work on most conditions, of course not all. Add it or improve it please. Hope that we can write something useful, other can use and enjoy :)

( I categorized Linux distributions (as my need) in: REDHAT, SUSE and DEBIAN based )




int DistroName()
{
//0 redhat, 1 suse, 2 debian, 3 NONE
system("cat /etc/issue > .distro");
FILE* fp=NULL;
fp=fopen(".distro","r");
char distro[100];
fscanf(fp,"%s",distro);
int i=0;
for(i=0;distro[i]!='\0';i++)
distro[i]=tolower(distro[i]);
fclose(fp);

if( access("/etc/redhat-release",F_OK)==0 || strcmp(distro,"redhat")==0 || strcmp(distro,"centos")==0
|| strcmp(distro,"fedora")==0 ) return 0;

else if( access("/etc/suse_release",F_OK)==0 || strcmp(distro,"opensuse")==0 || strcmp(distro,"suse")==0 )
return 1;

else if( access("/etc/debian_version",F_OK)==0 || strcmp(distro,"ubuntu")==0 || strcmp(distro,"debian")==0 )
return 2;

system("lsb_release -si > .distro");
fp=fopen(".distro","r");
fscanf(fp,"%s",distro);
for(i=0;distro[i]!='\0';i++)
distro[i]=tolower(distro[i]);
fclose(fp);

unlink(".distro");

if( strcmp(distro,"centos")==0 || strcmp(distro,"redhat")==0 || strcmp(distro,"fedora")==0 )
return 0;

else if( strcmp(distro,"opensuse")==0 || strcmp(distro,"suse")==0 )
return 1;

else if( strcmp(distro,"ubuntu")==0 || strcmp(distro,"debian")==0 )
return 2;

return 3;
}

cyberfish
07-27-2009, 08:54 AM
There are many similar distros that probably have similar directory structures. On the Redhat side, there's SuSE, Mandriva, CentOS, Fedora... and on the Debian side, there's Ubuntu (and it's various variants - kubuntu, xubuntu, edubuntu, etc), knoppix, Xandros, Linspire (or whatever it's called now).... Those are just the ones I can name. There are MANY MANY others. That's why we have been suggesting that it is impractical to test for distributions, and it would be a better idea to test for specific features you need.

hosseinyounesi
07-27-2009, 09:02 AM
I said that this is according to my need. I just need to 3 sections I mentioned, everyone can add to this function and use more.

MK27
07-27-2009, 10:01 AM
I said that this is according to my need. I just need to 3 sections I mentioned, everyone can add to this function and use more.

Yes, but as I warned you these things can be modified. So if you test for the distro and someone has modified the filesystem structure, which could easily be the case -- for example, some available server set-ups may be different than the desktop set-up and these differences can be more or less extensive -- then what you *think* is supposed to be true just because a system is "redhat" is NOT NECESSARILY SO.

YOU MUST TEST SPECIFICALLY FOR THE FILES AND OTHER RESOURCES YOU WANT TO WORK WITH. JUST SAYING "OH, LOOK, IT'S DEBIAN" IS A VERY BAD IDEA AND BOUND TO LEAD TO PROBLEMS.

If you intention is to distribute this or have it used on other computers, anyone with half a brain will take one look at that method, know it for what it is (a very poor hack) and simply not use the software. If you are just writing something for use by yourself, or in an environment like an office where you will always be the one doing the maintenance, *you* might get away with it, but as I said it will not fly further than that this way.

Plus, you are then saying "oh if another distro could look like this we could add more functions". Again, you are obviously trying to find ways to compensate for *your* laziness by proposing work-arounds for the consequences.

Learn to use the system properly. It may take you some more time, but it will mean the difference between something that works (properly) and something that works via a series of fortuitous coincidences based on "well it worked for me that one time" etc. Which the last one is quite likely to not work at all when production tested.

Salem
07-27-2009, 10:26 AM
> My program is a service and I should add it to system services and it is distro dependant!!!
I would suggest you find the source / installer for other common services, and see how they manage it.

As MK27 says, some half-baked detection of the current OS is going to come unstuck, if there is a more generally accepted way of solving the problem.

hosseinyounesi
07-28-2009, 02:51 AM
Thank you MK27 :) You are right, but I thought that at least "lsb_release" will work!!!

This program works in my office, but now I'm going to search more and read some services code to detect the way!!!!

But I have to say this too:
most of programs and distributions are in .deb, .rpm and tar.gz files. I think with these extensions the distribution is chosen by the user itself?!

Thanks again & again