LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-05-2011, 05:21 AM   #1
pmil
Member
 
Registered: Jul 2008
Posts: 43

Rep: Reputation: 15
Question How Linux configuration files work?


Dear all,

I've been struggling for a couple of days to understand how configuration files work in Linux but haven't really found any clear information regarding that.

My main question, as there is not a standard configuration file format in Linux, is how the format of a configuration file is understood by a Linux program? How the program reads its configuration file and how it understands its syntax (file format)? Also, a further question is how the program knows where its configuration file is being located?

For example inittab has its own "Identifier:RunLevel:Action:Command" format. How this is recognised by the "init" process?

Any help on the topic will be much appreciated.

Kind regards.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-05-2011, 05:31 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
You're kind of answering your own question. If there was a standard then you'd have a standard library which would interface with the config files and that would probably be how a program understands the config files. But as there is no such thing, then the files are just read however each developer / dev team decided to do it, using whatever code they feel like. So there is no answer to give you, it's just what the code does. Generally I'd say that you shouldn't really care, as there is no benefit in understanding this really, as far as I see.
 
1 members found this post helpful.
Old 02-05-2011, 05:40 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
I'll answer your questions in reverse order...

Quote:
Originally Posted by pmil
Also, a further question is how the program knows where its configuration file is being located?
Generally, there are two ways a program knows where to find its configuration file.
1. The location is "hard-coded" into the program. All that means is the programmer explicitly coded the filesystem location and name for the configuration file inside the program itself.
2. The configuration file can be given as a command line argument. In this case, the program looks at its command line options, and if an option to specify an alternate configuration file is used, the program will use the filename on the command line.

Quote:
Originally Posted by pmil
My main question, as there is not a standard configuration file format in Linux, is how the format of a configuration file is understood by a Linux program? How the program reads its configuration file and how it understands its syntax (file format)?
Writing code to allow a program to understand data is called "parsing" and the programmer must explicitly tell the program how to do it. A program reads a file just like you read a document. The document is a "stream of letter/characters/symbols." When you see a sequence you recognize, you associate a meaning with that sequence and begin looking for the next sequence you recognize. That same process is used by a program. The program will look for recognized sequences of characters. If the sequence matches something the programmer told the program to look for, then the program executes another set of instructions based on the recognized sequence. Then the program goes back to look for another recognizable sequence until the end of the file is reached.
 
3 members found this post helpful.
Old 02-05-2011, 07:59 AM   #4
pmil
Member
 
Registered: Jul 2008
Posts: 43

Original Poster
Rep: Reputation: 15
Thank you very much for your replies!

Thanks very much Dark_Helmet, your answer really helped me.
So, if I understood correctly, the program is fully responsible for reading (parsing) the configuration file correctly.
For instance, If I was about to write my own configuration file for an existing program (say "init" process), I should have modified the program accordingly so that it performs correctly according to the new configuration file. Is that right?

I was searching for source code of a Linux program that reads a configuration file and acts accordingly to see how this works in practice but I couldn't find any. Would you happen to know such a program?

One of my intentions is to write a simple program that uses a configuration file but it would help if there was a simple guide on that (mostly for the 'program' site).

Kind regards.

Last edited by pmil; 02-05-2011 at 08:01 AM.
 
Old 02-05-2011, 08:15 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
Configuration files are not scripts, they don't necessarily "tell" a program to do anything at all. So either a program that "reads a configuration file and acts accordingly" is either, depending on your interpretation, ANY program that has a config file, or no program ever, as that would be a script, not a config file, and would be a different thing. In fact... an example of this would be bash. which can take a *script* as input and "act accordingly", i.e. run a sequence of bash commands. Same for other scripting languages, perl, python etc, but in order provide what could be examples, we've moved a long way from "configuration files". If you want an arbitrary example, I wrote a bizarrely popular program called acidrip, and in that I invent my own way to read and write attributes from a config file. Maybe have a look at my perl source code for it? I don't actually parse a file in that code, I maintain a set of variables in a data structure and simply print that data structure out to a file to update my config, and similarily just execute the config file on startup to make the variables in the file be available to my code. But really, there are a million different ways to achieve this, and in all of this, no one has actually mentioned what language you want to do this in... c? java? c++? perl? bash? ksh? python? perl? ruby? haskel? php? fortran? I expect that many of these languages will have third party (or maybe even built in) libraries for abstraction of config to a file - xml is a fairly common generic way of doing this, but they are always going to be voluntary.

depending on what you want your simple program to do, you can just open a file, read the contents and then do something because of what ever single word is it in. That would technically be a "configuration file" if you wish to see it as one, and could just be 3 or 4 lines of bash code.

Last edited by acid_kewpie; 02-05-2011 at 08:16 AM.
 
Old 02-06-2011, 10:57 AM   #6
pmil
Member
 
Registered: Jul 2008
Posts: 43

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by acid_kewpie View Post
Configuration files are not scripts, they don't necessarily "tell" a program to do anything at all
I don't think I said that! I said that, as I understood, the program should decide how to parse a configuration file (if it is to parse any). The configuration file's layout/format does't instruct the program on how the latter should parse it.

Quote:
Originally Posted by acid_kewpie View Post
If you want an arbitrary example, I wrote a bizarrely popular program called acidrip, and in that I invent my own way to read and write attributes from a config file. Maybe have a look at my perl source code for it?
Thanks, that would be much appreciated, although, I would prefer to see something written in C. Btw, I can't find the sources of your tool and to be honest, I would prefer a simpler example than that.

Quote:
Originally Posted by acid_kewpie View Post
what language you want to do this in..
As I said, I would preferably use C language for doing it. What I basically want to know is how a program can successfully parse a configuration file.

Finally, a kind of general question: I was trying to find out the source code of the "init" program used on a System-V startup mechanism. I keep finding quotes like

Code:
“System V init examines the /etc/inittab file for an :initdefault: entry, which tells init 
whether there is a default runlevel.”
but I can't find the actual code of the "init" program to examine how it parses the "inittab" file.

Sorry for the big number of questions and thanks again for all you help.
 
Old 02-06-2011, 01:12 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
Whilst I totally understand what you're talking about, I don't understand WHY you're talking about it. Configuration files are not in themselves interesting, it's just a text file that you write. There are a thousand different reasons you might read or write data to or from a file, and you're at liberty to call any method you feel like using as your method of configuration files. You could absolutely store your data in the form of a PNG image if you so felt like... There are no specific standards, so you don't need to know about how to write config files, but how to read and write files in general.
 
Old 02-06-2011, 08:36 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
Just to concur with acid_kewpie; a cfg file can be in any format you like, IF YOU are also writing the program that needs to 'parse' ie understand/use it.
If you want to create a copy of an existing cfg file and use it with the orig program, then you have to obey the current rules for that program/cfg file.

Typically a basic cfg file wuld be

var=value

& the prog needs to be able to read the file and assign the value specified to the equiv var inside the prog.

HTH
 
Old 02-07-2011, 02:49 AM   #9
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
For chrism01's example, a very simple example code.

Code:
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{

FILE *fp;
char buffer[512], *ptr;

    fp = fopen("test.cfg","rt");
    if(fp == NULL)
    {
        perror("can't read config");
    }


    while(fgets(buffer,sizeof(buffer)-1,fp))
    {
        puts(buffer);
        ptr = strchr(buffer, '=');
        if(ptr==NULL)
        {
            // ignorea if no space found
            continue;
        }
        *ptr = '\0';
        printf("variable = %s\n",buffer);
        printf("value = %s\n",++ptr);
/*
        if(strcmp(buffer,"firstname")==0)
        {
            do something with firstname
            like copying to a variable
            
        }
*/
    }

    fclose(fp);
    return 0;
}
and test.cfg
Code:
firstname = wim
lastname = sturkenboom

PS I did not care about trimming whitespace characters, so you must do that yourself

Last edited by Wim Sturkenboom; 02-07-2011 at 02:51 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
does files with .html extension work on linux server? prasanth_kumar Linux - Newbie 7 12-26-2008 12:27 AM
Do Linux Binary Files work across all platforms/CPU types? (i.e. Between PC/Mac) bookerg Linux - Newbie 2 07-31-2007 05:48 PM
I need the port nos for the configuration files on Linux sathish80 Linux - Newbie 1 11-14-2006 01:00 AM
Where are programs files and configuration files stored? nutshell Linux - General 2 03-09-2002 10:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration