Thursday, August 7, 2014

Structure within structure in C

Nested structure in C is nothing but structure within structure. One structure can be declared inside other structure as we declare structure members inside a structure. The structure variables can be a normal structure variable or a pointer variable to access the data. You can learn below concepts in this section.
    1. Structure within structure in C using normal variable
    2. Structure within structure in C using pointer variable

1. Structure within structure in C using normal variable:

           This program explains how to use structure within structure in C using normal variable. “student_college_detail’ structure is declared inside “student_detail” structure in this program. Both structure variables are normal structure variables.
Please note that members of “student_college_detail” structure are accessed by 2 dot(.) operator and members of “student_detail” structure are accessed by single dot(.) operator.
#include 
#include 

struct student_college_detail
{
    int college_id;
    char college_name[50];
};

struct student_detail 
{
    int id;
    char name[20];
    float percentage;
    // structure within structure
    struct student_college_detail clg_data;
}stu_data;

int main() 
{
    struct student_detail stu_data = {1, "Raju", 90.5, 71145,
                                       "Anna University"};
    printf(" Id is: %d \n", stu_data.id);
    printf(" Name is: %s \n", stu_data.name);
    printf(" Percentage is: %f \n\n", stu_data.percentage);

    printf(" College Id is: %d \n", 
                    stu_data.clg_data.college_id);
    printf(" College Name is: %s \n", 
                    stu_data.clg_data.college_name);
    return 0;
}

Output:

Id is: 1
Name is: Raju
Percentage is: 90.500000
College Id is: 71145
College Name is: Anna University

Structure within structure in C using pointer variable:

           This program explains how to use structure within structure in C using pointer variable. “student_college_detail’ structure is declared inside “student_detail” structure in this program. one normal structure variable and one pointer structure variable is used in this program.
Please note that combination of .(dot) and ->(arrow) operators are used to access the structure member which is declared inside the structure.
#include 
#include 

struct student_college_detail
{
    int college_id;
    char college_name[50];
};

struct student_detail 
{
    int id;
    char name[20];
    float percentage;
    // structure within structure
    struct student_college_detail clg_data; 
}stu_data, *stu_data_ptr;

int main() 
{
  struct student_detail stu_data = {1, "Raju", 90.5, 71145, 
                                    "Anna University"};
    stu_data_ptr = &stu_data;

    printf(" Id is: %d \n", stu_data_ptr->id);
    printf(" Name is: %s \n", stu_data_ptr->name);
    printf(" Percentage is: %f \n\n", 
                         stu_data_ptr->percentage);

    printf(" College Id is: %d \n", 
                         stu_data_ptr->clg_data.college_id);
    printf(" College Name is: %s \n", 
                      stu_data_ptr->clg_data.college_name);

    return 0;
}

Output:

Id is: 1
Name is: Raju
Percentage is: 90.500000
College Id is: 71145
College Name is: Anna University

C – Passing structure to function

  • A structure can be passed to any function from main function or from any sub function.
  • Structure definition will be available within the function only.
  • It won’t be available to other functions unless it is passed to those functions by value or by address(reference).
  • Else, we have to declare structure variable as global variable. That means, structure variable should be declared outside the main function. So, this structure will be visible to all the functions in a C program.

Passing structure to function in C:

It can be done in below 3 ways.
    1. Passing structure to a function by value
    2. Passing structure to a function by address(reference)
    3. No need to pass a structure – Declare structure variable as global

Example program – passing structure to function in C by value:

           In this program, the whole structure is passed to another function by value. It means the whole structure is passed to another function with all members and their values. So, this structure can be accessed from called function. This concept is very useful while writing very big programs in C.
#include 
#include 

struct student 
{
            int id;
            char name[20];
            float percentage;
};

void func(struct student record);

int main() 
{
            struct student record;

            record.id=1;
            strcpy(record.name, "Raju");
            record.percentage = 86.5;

            func(record);
            return 0;
}

void func(struct student record)
{
            printf(" Id is: %d \n", record.id);
            printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
}

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Example program – Passing structure to function in C by address:

           In this program, the whole structure is passed to another function by address. It means only the address of the structure is passed to another function. The whole structure is not passed to another function with all members and their values. So, this structure can be accessed from called function by its address.
#include 
#include 

struct student 
{
           int id;
           char name[20];
           float percentage;
};

void func(struct student *record);

int main() 
{
          struct student record;

          record.id=1;
          strcpy(record.name, "Raju");
          record.percentage = 86.5;

          func(&record);
          return 0;
}

void func(struct student *record)
{
          printf(" Id is: %d \n", record->id);
          printf(" Name is: %s \n", record->name);
          printf(" Percentage is: %f \n", record->percentage);
}

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Example program to declare a structure variable as global in C:

           Structure variables also can be declared as global variables as we declare other variables in C. So, When a structure variable is declared as global, then it is visible to all the functions in a program. In this scenario, we don’t need to pass the structure to any function separately.
#include 
#include 

struct student 
{
            int id;
            char name[20];
            float percentage;
};
struct student record; // Global declaration of structure

void structure_demo();

int main() 
{
            record.id=1;
            strcpy(record.name, "Raju");
            record.percentage = 86.5;

            structure_demo();
            return 0;
}

void structure_demo()
{
            printf(" Id is: %d \n", record.id);
            printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
}

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Wednesday, August 6, 2014

Memory : Stack vs Heap

7. Memory : Stack vs Heap






Stack vs Heap

So far we have seen how to declare basic type variables such as intdouble, etc, and complex types such as arrays and structs. The way we have been declaring them so far, with a syntax that is like other languages such as MATLAB, Python, etc, puts these variables on the stack in C.

The Stack

What is the stack? It's a special region of your computer's memory that stores temporary variables created by each function (including the main() function). The stack is a "FILO" (first in, last out) data structure, that is managed and optimized by the CPU quite closely. Every time a function declares a new variable, it is "pushed" onto the stack. Then every time a function exits, all of the variables pushed onto the stack by that function, are freed (that is to say, they are deleted). Once a stack variable is freed, that region of memory becomes available for other stack variables.
The advantage of using the stack to store variables, is that memory is managed for you. You don't have to allocate memory by hand, or free it once you don't need it any more. What's more, because the CPU organizes stack memory so efficiently, reading from and writing to stack variables is very fast.
A key to understanding the stack is the notion that when a function exits, all of its variables are popped off of the stack (and hence lost forever). Thus stack variables are local in nature. This is related to a concept we saw earlier known as variable scope, or local vs global variables. A common bug in C programming is attempting to access a variable that was created on the stack inside some function, from a place in your program outside of that function (i.e. after that function has exited).
Another feature of the stack to keep in mind, is that there is a limit (varies with OS) on the size of variables that can be store on the stack. This is not the case for variables allocated on the heap.
To summarize the stack:
  • the stack grows and shrinks as functions push and pop local variables
  • there is no need to manage the memory yourself, variables are allocated and freed automatically
  • the stack has size limits
  • stack variables only exist while the function that created them, is running

The Heap

The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU. It is a more free-floating region of memory (and is larger). To allocate memory on the heap, you must use malloc() or calloc(), which are built-in C functions. Once you have allocated memory on the heap, you are responsible for using free() to deallocate that memory once you don't need it any more. If you fail to do this, your program will have what is known as a memory leak. That is, memory on the heap will still be set aside (and won't be available to other processes). As we will see in the debugging section, there is a tool called valgrind that can help you detect memory leaks.
Unlike the stack, the heap does not have size restrictions on variable size (apart from the obvious physical limitations of your computer). Heap memory is slightly slower to be read from and written to, because one has to use pointers to access memory on the heap. We will talk about pointers shortly.
Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope.

Stack vs Heap Pros and Cons

Stack

  • very fast access
  • don't have to explicitly de-allocate variables
  • space is managed efficiently by CPU, memory will not become fragmented
  • local variables only
  • limit on stack size (OS-dependent)
  • variables cannot be resized

Heap

  • variables can be accessed globally
  • no limit on memory size
  • (relatively) slower access
  • no guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed
  • you must manage memory (you're in charge of allocating and freeing variables)
  • variables can be resized using realloc()

Examples

Here is a short program that creates its variables on the stack. It looks like the other programs we have seen so far.
#include 

double multiplyByTwo (double input) {
  double twice = input * 2.0;
  return twice;
}

int main (int argc, char *argv[])
{
  int age = 30;
  double salary = 12345.67;
  double myList[3] = {1.2, 2.3, 3.4};

  printf("double your salary is %.3f\n", multiplyByTwo(salary));

  return 0;
}
double your salary is 24691.340
On lines 10, 11 and 12 we declare variables: an int, a double, and an array of three doubles. These three variables are pushed onto the stack as soon as themain() function allocates them. When the main() function exits (and the program stops) these variables are popped off of the stack. Similarly, in the functionmultiplyByTwo(), the twice variable, which is a double, is pushed onto the stack as soon as the multiplyByTwo() function allocates it. As soon as themultiplyByTwo() function exits, the twice variable is popped off of the stack, and is gone forever.
As a side note, there is a way to tell C to keep a stack variable around, even after its creator function exits, and that is to use the static keyword when declaring the variable. A variable declared with the static keyword thus becomes something like a global variable, but one that is only visible inside the function that created it. It's a strange construction, one that you probably won't need except under very specific circumstances.
Here is another version of this program that allocates all of its variables on the heap instead of the stack:
#include 
#include 

double *multiplyByTwo (double *input) {
  double *twice = malloc(sizeof(double));
  *twice = *input * 2.0;
  return twice;
}

int main (int argc, char *argv[])
{
  int *age = malloc(sizeof(int));
  *age = 30;
  double *salary = malloc(sizeof(double));
  *salary = 12345.67;
  double *myList = malloc(3 * sizeof(double));
  myList[0] = 1.2;
  myList[1] = 2.3;
  myList[2] = 3.4;

  double *twiceSalary = multiplyByTwo(salary);

  printf("double your salary is %.3f\n", *twiceSalary);

  free(age);
  free(salary);
  free(myList);
  free(twiceSalary);

  return 0;
}
As you can see, using malloc() to allocate memory on the heap and then using free() to deallocate it, is no big deal, but is a bit cumbersome. The other thing to notice is that there are a bunch of star symbols * all over the place now. What are those? The answer is, they are pointers. The malloc() (and calloc() andfree()) functions deal with pointers not actual values. We will talk more about pointers shortly. The bottom line though: pointers are a special data type in C that store addresses in memory instead of storing actual values. Thus on line 5 above, the twice variable is not a double, but is a pointer to a double. It's an address in memory where the double is stored.

When to use the Heap?

When should you use the heap, and when should you use the stack? If you need to allocate a large block of memory (e.g. a large array, or a big struct), and you need to keep that variable around a long time (like a global), then you should allocate it on the heap. If you are dealing with realtively small variables that only need to persist as long as the function using them is alive, then you should use the stack, it's easier and faster. If you need variables like arrays and structs that can change size dynamically (e.g. arrays that can grow or shrink as needed) then you will likely need to allocate them on the heap, and use dynamic memory allocation functions like malloc()calloc()realloc() and free() to manage that memory "by hand". We will talk about dynamically allocated data structures after we talk about pointers.

Return multiple values in C

Method 1 : Using Array
  • If more than two variables of same type is to be returned then we can use array .
  • Store each and every value to be returned in an array and return base address of that array.
Method 2 : Using Pointer and One Return Statement
  • Pointer Variable can updated directly using  Value at ['*'] Operator.
  • Usually Function can return single value.
  • If we need to return more than two variables then update 1 variable directly using pointer and return second variable using ‘return Statement‘. [ Call by Value + Call by Reference ]
EX:

#include

int* function2(int a){
    int* array = (int*)malloc(2);
    int b=(65*a)/100;
    int c=5*8;
    array[0]=b;
    array[1]=c;
    return array;
}

int main(void)
{
    int a=18,i;
    int* array;
    array=function2(a);

   for(i=0;i<2 div="" i="">
   {
        printf("array[%d]: %d\n",i,array[i]);
    }
    free(array);
    
     getch();  
}

Method 3 : Using Structure 
  • Construct a Structure containing values to be returned and then return the base address of the structure to the calling function.
EX:

#include

struct dont { int x; double y; };

struct dont fred(void)
{
        struct dont b;
        b.x = 1;
        b.y = 91.99919;
        return b;
}

int main(int argc, char **argv)
{
        struct dont look = fred();
        printf("look.x = %d, look.y = %lf\n", look.x, look.y);
        getch();
        return 0;
}

Array of Pointers in C

#include
#include

struct name {
   int a;
   float b;
   char c[30];
};

int main(){
   struct name *ptr;
 
   int i,n;
   printf("Enter n: ");
   scanf("%d",&n);
 
   ptr=(struct name*)malloc(n*sizeof(struct name));
/* Above statement allocates the memory for n structures with pointer ptr pointing to base address */

   for(i=0;i       printf("Enter string, integer and floating number  respectively:\n");
       scanf("%s%d%f",&(ptr+i)->c,&(ptr+i)->a,&(ptr+i)->b);
     
   }
   printf("Displaying Infromation:\n");
   for(i=0;i       printf("%s\t%d\t%.2f\n",(ptr+i)->c,(ptr+i)->a,(ptr+i)->b);
     
       getch();
   return 0;
}


Friday, July 18, 2014

10 Tips to Secure Your Apache Web Server on UNIX / Linux

If you are a sysadmin, you should secure your Apache web server by following the 10 tips mentioned in this article.

1. Disable unnecessary modules

If you are planning to install apache from source, you should disable the following modules. If you do ./configure –help, you’ll see all available modules that you can disable/enable.
  • userdir – Mapping of requests to user-specific directories. i.e ~username in URL will get translated to a directory in the server
  • autoindex – Displays directory listing when no index.html file is present
  • status – Displays server stats
  • env – Clearing/setting of ENV vars
  • setenvif – Placing ENV vars on headers
  • cgi – CGI scripts
  • actions – Action triggering on requests
  • negotiation – Content negotiation
  • alias – Mapping of requests to different filesystem parts
  • include – Server Side Includes
  • filter – Smart filtering of request
  • version – Handling version information in config files using IfVersion
  • as-is – as-is filetypes
Disable all of the above modules as shown below when you do ./configure
./configure \
--enable-ssl \
--enable-so \
--disable-userdir \
--disable-autoindex \
--disable-status \
--disable-env \
--disable-setenvif \
--disable-cgi \
--disable-actions \
--disable-negotiation \
--disable-alias \
--disable-include \
--disable-filter \
--disable-version \
--disable-asis
If you enable ssl, and disable mod_setenv, you’ll get the following error.
  • Error: Syntax error on line 223 of /usr/local/apache2/conf/extra/httpd-ssl.conf: Invalid command ‘BrowserMatch’, perhaps misspelled or defined by a module not included in the server configuration
  • Solution: If you use ssl, don’t disable setenvif. Or, comment out the BrowserMatch in your httpd-ssl.conf, if you disable mod_setenvif.
After the installation, when you do httpd -l, you’ll see all installed modules.
# /usr/local/apache2/bin/httpd -l
Compiled in modules:
  core.c
  mod_authn_file.c
  mod_authn_default.c
  mod_authz_host.c
  mod_authz_groupfile.c
  mod_authz_user.c
  mod_authz_default.c
  mod_auth_basic.c
  mod_log_config.c
  mod_ssl.c
  prefork.c
  http_core.c
  mod_mime.c
  mod_dir.c
  mod_so.c
In this example, we have the following apache modules installed.
  • core.c – Apache core module
  • mod_auth* – For various authentication modules
  • mod_log_config.c – Log client request. provides additional log flexibilities.
  • mod_ssl.c – For SSL
  • prefork.c – For MPM (Multi-Processing Module) module
  • httpd_core.c – Apache core module
  • mod_mime.c – For setting document MIME types
  • mod_dir.c – For trailing slash redirect on directory paths. if you specify url/test/, it goes to url/test/index.html
  • mod_so.c – For loading modules during start or restart

2. Run Apache as separate user and group

By default, apache might run as nobody or daemon. It is good to run apache in its own non-privileged account. For example: apache.
Create apache group and user.
groupadd apache
useradd -d /usr/local/apache2/htdocs -g apache -s /bin/false apache
Modify the httpd.conf, and set User and Group appropriately.
# vi httpd.conf
User apache
Group apache
After this, if you restart apache, and do ps -ef, you’ll see that the apache is running as “apache” (Except the 1st httpd process, which will always run as root).
# ps -ef | grep -i http | awk '{print $1}'
root
apache
apache
apache
apache
apache

3. Restrict access to root directory (Use Allow and Deny)

Secure the root directory by setting the following in the httpd.conf

    Options None
    Order deny,allow
    Deny from all
In the above:
  • Options None – Set this to None, which will not enable any optional extra features.
  • Order deny,allow – This is the order in which the “Deny” and “Allow” directivites should be processed. This processes the “deny” first and “allow” next.
  • Deny from all – This denies request from everybody to the root directory. There is no Allow directive for the root directory. So, nobody can access it.

4. Set appropriate permissions for conf and bin directory

bin and conf directory should be viewed only by authorized users. It is good idea to create a group, and add all users who are allowed to view/modify the apache configuration files to this group.
Let us call this group: apacheadmin
Create the group.
groupadd apacheadmin
Allow access to bin directory for this group.
chown -R root:apacheadmin /usr/local/apache2/bin
chmod -R 770 /usr/local/apache2/bin
Allow access to conf directory for this group.
chown -R root:apacheadmin /usr/local/apache2/conf
chmod -R 770 /usr/local/apache2/conf
Add appropriate members to this group. In this example, both ramesh and john are part of apacheadmin
# vi /etc/group
apacheadmin:x:1121:ramesh,john

5. Disable Directory Browsing

If you don’t do this, users will be able to see all the files (and directories) under your root (or any sub-directory).
For example, if they go to http://{your-ip}/images/ and if you don’t have an index.html under images, they’ll see all the image files (and the sub-directories) listed in the browser (just like a ls -1 output). From here, they can click on the individual image file to view it, or click on a sub-directory to see its content.
To disable directory browsing, you can either set the value of Options directive to “None” or “-Indexes”. A – in front of the option name will remove it from the current list of options enforced for that directory.
Indexes will display a list of available files and sub-directories inside a directory in the browser (only when no index.html is present inside that folder). So, Indexes should not be allowed.

  Options None
  Order allow,deny
  Allow from all


(or)


  Options -Indexes
  Order allow,deny
  Allow from all

6. Don’t allow .htaccess

Using .htaccess file inside a specific sub-directory under the htdocs (or anywhere ouside), users can overwrite the default apache directives. On certain situations, this is not good, and should be avoided. You should disable this feature.
You should not allow users to use the .htaccess file and override apache directives. To do this, set “AllowOverride None” in the root directory.

  Options None
  AllowOverride None
  Order allow,deny
  Allow from all

7. Disable other Options

Following are the available values for Options directive:
  • Options All – All options are enabled (except MultiViews). If you don’t specify Options directive, this is the default value.
  • Options ExecCGI – Execute CGI scripts (uses mod_cgi)
  • Options FollowSymLinks – If you have symbolic links in this directory, it will be followed.
  • Options Includes – Allow server side includes (uses mod_include)
  • Options IncludesNOEXEC – Allow server side includes without the ability to execute a command or cgi.
  • Options Indexes – Disable directory listing
  • Options MultiViews - Allow content negotiated multiviews (uses mod_negotiation)
  • Options SymLinksIfOwnerMatch – Similar to FollowSymLinks. But, this will follow only when the owner is same between the link and the original directory to which it is linked.
Never specify ‘Options All’. Always specify one (or more) of the options mentioned above. You can combine multiple options in one line as shown below.
Options Includes FollowSymLinks
The + and – in front of an option value is helpful when you have nested direcotires, and would like to overwrite an option from the parent Directory directive.
In this example, for /site directory, it has both Includes and Indexes:

  Options Includes Indexes
  AllowOverride None
  Order allow,deny
  Allow from all
For /site/en directory, if you need Only Indexes from /site (And not the Includes), and if you want to FollowSymLinks only to this directory, do the following.

  Options -Includes +FollowSymLink
  AllowOverride None
  Order allow,deny
  Allow from all
  • /site will have Includes and Indexes
  • /site/en will have Indexes and FollowSymLink

8. Remove unwanted DSO modules

If you have loaded any dynamic shared object modules to the apache, they’ll be present inside the httpd.conf under “LoadModule” directive.
Please note that the statically compiled apache modules will not be listed as “LoadModule” directive.
Comment out any unwanted “LoadModules” in the httpd.conf
grep LoadModule /usr/local/apache2/conf/httpd.conf

9. Restrict access to a specific network (or ip-address)

If you want your site to be viewed only by a specific ip-address or network, do the following:
To allow a specific network to access your site, give the network address in the Allow directive.

  Options None
  AllowOverride None
  Order deny,allow
  Deny from all
  Allow from 10.10.0.0/24
To allow a specific ip-address to access your site, give the ip-address in the Allow directive.

  Options None
  AllowOverride None
  Order deny,allow
  Deny from all
  Allow from 10.10.1.21

10. Don’t display or send Apache version (Set ServerTokens)

By default, the server HTTP response header will contains apache and php version. Something similar to the following. This is harmful, as we don’t want an attacker to know about the specific version number.
Server: Apache/2.2.17 (Unix) PHP/5.3.5
To avoid this, set the ServerTokens to Prod in httpd.conf. This will display “Server: Apache” without any version information.
# vi httpd.conf
ServerTokens Prod
Following are possible ServerTokens values:
  • ServerTokens Prod displays “Server: Apache”
  • ServerTokens Major displays “Server: Apache/2″
  • ServerTokens Minor displays “Server: Apache/2.2″
  • ServerTokens Min displays “Server: Apache/2.2.17″
  • ServerTokens OS displays “Server: Apache/2.2.17 (Unix)”
  • ServerTokens Full displays “Server: Apache/2.2.17 (Unix) PHP/5.3.5″ (If you don’t specify any ServerTokens value, this is the default)
Apart from all the above 10 tips, make sure to secure your UNIX / Linux operating system. There is no point in securing your apache, if your OS is not secure. Also, always keep your apache version upto date. The latest version of the apache contains fixes for all the known security issues. Make sure to review your apache log files frequently.

Wednesday, July 9, 2014

Google public DNS will solve """yum "Couldn't resolve host 'mirrorlist.centos.org'" for CentOS 6""" Problem

yum "Couldn't resolve host 'mirrorlist.centos.org'" for CentOS 6

I ran into an issue recently where I could ping URL's just fine, but when I ran the "yum update" command yum could not resolve anything. I would get error messages like the following:
[root@viviotech ~]# yum -y update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os error was
14: PYCURL ERROR 6 - "Couldn't resolve host 'mirrorlist.centos.org'"
Error: Cannot find a valid baseurl for repo: base
After a bit of digging, and absolutely no help from my friend Google, I asked one of the Vivio technicians about it and he said he'd seen it on other servers - specifically servers with VirtualMin.
Take a look at the resolve.conf file:
nameserver 127.0.0.1
nameserver 8.8.8.8
nameserver 8.8.4.4
See how VirtualMin adds the 127.0.0.1 address first? Apparently YUM will ONLY check the very first entry in the /etc/resolve.conf file when it looks for the server to resolve IP Addresses. In this case, the local DNS resolver was not configured to use forwarders, so the YUM process would ask the local DNS server where to find "mirrorlist.centos.org", and when the local resolver didn't know, YUM would simply report an error instead of looking at the other resolvers in the /etc/resolve.conf list. This is why I could ping URL's just fine, but YUM could not find them.
The solution to this was to simply place the 127.0.0.1 entry at the botton of the /etc/resolv.conf file, like so:
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 127.0.0.1
After that, eveything worked great.
IMPORTANT: The above examples of the /etc/resolv.conf file use the IP Addresses of the Google Public DNS


CHECK THIS ALSO


I have been using several VMs for simulating a multi-node environment. Most of my VMs are CentOS.
After installing CentOS 6.4 I got the following error when I tried “yum update“:
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os error was
14: PYCURL ERROR 6 - "Couldn't resolve host 'mirrorlist.centos.org'"
Error: Cannot find a valid baseurl for repo: base
To fix this error I updated NM_CONTROLLED to “no” in the file /etc/sysconfig/network-scripts/ifcfg-eth0
After this I restarted the network interface using the following commands:
ifdown eth0
ifup eth0
After doing the above the yum update started working.