LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-16-2015, 01:19 AM   #1
dcds
LQ Newbie
 
Registered: Feb 2015
Posts: 4

Rep: Reputation: Disabled
race condition in gethostbyname_r


Hi,
I am using gethostbyname_r in one of my application. I have verified by helgrind tool of valgrind that there is some race condition in two threads for read and write.

I am spawing 10 threads from main and calling gethostbyname_r simply. As Man page of gethostbyname_r says its Thread Safe, but still I am seeing data race.

My RHEL version is: 5.11
and my glibc is - glibc-2.5-123.el5_11.1

Can anyone help me to find out work around or any rpm already exists to fix that race condition?

Last edited by dcds; 02-16-2015 at 01:31 AM. Reason: Add more info
 
Old 02-16-2015, 01:26 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
would be nice to give more information, otherwise we can give you only general hints, like: use mutex.
 
Old 02-16-2015, 02:25 AM   #3
dcds
LQ Newbie
 
Registered: Feb 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hi pan64, I have updated the question.
 
Old 02-16-2015, 02:35 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
still nothing. As you wrote gethostbyname_r is thread safe, so there should be something else (otherwise you found a bug in it).
So can you post your code to try to check it?
 
Old 02-16-2015, 02:37 AM   #5
dcds
LQ Newbie
 
Registered: Feb 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Here is my code:
Code:
#include <stdio.h>
#include <netdb.h>
#include <string.h>

int
resolvehost(
 const char* hostname,
 char* hostent_buff,
 int hostent_buff_len,
 struct hostent **p_hp)
{
 struct hostent hostbuf;
 struct hostent *hp = *p_hp = NULL;

 int herr = 0;
 int hres = 0;

 hres = gethostbyname_r(hostname, &hostbuf,
 hostent_buff, hostent_buff_len, &hp, &herr);

 if (NULL == hp)
 {
 fprintf(stderr, "gethostbyname_r() failed to resolve hostname [%s]", hostname);

 return NO_ADDRESS;
 }

 *p_hp = hp;
 return NETDB_SUCCESS;
}


int
mcc_sys_gethostbyname()
{
 char buff[4096];
 struct hostent *hp = NULL;
 if( resolvehost("192linvm35", buff, sizeof(buff), &hp) != NETDB_SUCCESS )
 return 1;

 /*char buff2[4096];
 struct hostent *hp2 = NULL;
 if( resolvehost("192linvm35", buff2, sizeof(buff2), &hp2) != NETDB_SUCCESS )
 return 1;*/

 char** ips = hp->h_addr_list;
 printf("%s : %s\n", hp->h_name, hp->h_aliases[0]);
 while( *ips )
 {
 unsigned char* ipfields = *ips;
 printf("%u.%u.%u.%u\n", ipfields[0], ipfields[1], ipfields[2], ipfields[3]);
 ++ips;
 }
 return 0;
}

#define NUM_THREADS 10 
int main()
{
    pthread_t threads[NUM_THREADS];
   int j;
   for( j=0; j < NUM_THREADS; j++ )
   {
      int ret = pthread_create(&threads[j],NULL,mcc_sys_gethostbyname,(void*)NULL);
      if(ret == 0)
      {
         printf("**************Currently thread %d running*************\n",j);
      }
   }
   for( j=0; j < NUM_THREADS; j++ )
   {
        int ret = pthread_join(threads[j], NULL);
        if(ret ==0)
        {
           printf("*************thread %d going to stop************\n",j);
        }

   }
   return (0);
}
I am compiling this code with below command:
gcc "filename" -g -o "executable name" -lpthread

And here is data race in helgrind report:

==6291== Possible data race during read of size 1 at 0x3989357f80 by thread #3
==6291== at 0x6E4D15E: _nss_files_gethostbyname_r (in /lib64/libnss_files-2.5.so)
==6291== by 0x39890EB5D3: gethostbyname_r@@GLIBC_2.2.5 (in /lib64/libc-2.5.so)
==6291== by 0x4006D0: resolvehost (gethostbyname_r.c:18)
==6291== by 0x400749: mcc_sys_gethostbyname (gethostbyname_r.c:38)
==6291== by 0x4A0B330: mythread_wrapper (hg_intercepts.c:201)
==6291== by 0x398A00683C: start_thread (in /lib64/libpthread-2.5.so)
==6291== by 0x39890D4FCC: clone (in /lib64/libc-2.5.so)
==6291== This conflicts with a previous write of size 8 by thread #2
==6291== at 0x398907A00A: memset (in /lib64/libc-2.5.so)
==6291== by 0x39890E3FD3: do_init (in /lib64/libc-2.5.so)
==6291== by 0x398A00C1D2: pthread_once (in /lib64/libpthread-2.5.so)
==6291== by 0x39890EB73D: gethostbyname_r@@GLIBC_2.2.5 (in /lib64/libc-2.5.so)
==6291== by 0x4006D0: resolvehost (gethostbyname_r.c:18)
==6291== by 0x400749: mcc_sys_gethostbyname (gethostbyname_r.c:38)
==6291== by 0x4A0B330: mythread_wrapper (hg_intercepts.c:201)
==6291== by 0x398A00683C: start_thread (in /lib64/libpthread-2.5.so)

Last edited by dcds; 02-16-2015 at 02:52 AM. Reason: helgrind report
 
Old 02-16-2015, 02:44 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
please use [code]here comes your code[/code] to keep formatting. Also please post your makefile or how did you compile this code and the error message too.
 
Old 02-16-2015, 03:18 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
you need to use -pthread instead of -lpthread. Also use -Wall:
gcc -Wall -pthread -o outfile -g infile.c
also missing
#include <pthread.h>
from the .c file.
 
Old 02-16-2015, 03:39 AM   #8
dcds
LQ Newbie
 
Registered: Feb 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hi pan64,

Now also I am seeing data race in gethostbyname_r.
 
  


Reply



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
new OpenSSL use-after-free race condition BenCollver Slackware 3 04-15-2014 11:42 AM
What is race condition? LinuxInfo Programming 1 09-15-2008 09:44 PM
Kded race condition during automounting SpelledJ Slackware 6 10-25-2007 01:51 PM
Race condition in /etc/rc.d/ with latest -current ? Yalla-One Slackware 1 08-06-2006 02:23 PM
race condition in close socket?? jwstric2 Programming 3 03-18-2005 05:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:09 PM.

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
Open Source Consulting | Domain Registration