当前位置: 技术问答>linux和unix
gethostbyname_r内部出错到问题
来源: 互联网 发布时间:2016-07-31
本文导语: 我写了个获取主机 ip的程序,其中用到来gethostbyname_r这个调用,由于这些函数不能设置超时,所以 又用个信号,每次调用这个函数 600多次到时候 这个函数就会报错,希望大家能帮忙解决以下: 代码: static sigjmp_...
我写了个获取主机 ip的程序,其中用到来gethostbyname_r这个调用,由于这些函数不能设置超时,所以 又用个信号,每次调用这个函数 600多次到时候 这个函数就会报错,希望大家能帮忙解决以下:
代码:
static sigjmp_buf jmpbuf;
static void host_handler(int signo)//跳转函数
{
siglongjmp(jmpbuf,1);
}
char *get_ip(char *host)
{
struct sigaction act;
act.sa_flags=0;
act.sa_handler=host_handler;
char *ip=(char*)malloc(sizeof(char)*20);
if(ip ==NULL)
{
printf("malloc errorn");
exit(1);
}
memset(ip,0,20);
if((sigemptyset(&act.sa_mask)==-1)|| (sigaction(SIGALRM,&act,NULL)==-1))
{
perror("fail set signaln");
return NULL;
}
if(sigsetjmp(jmpbuf,1)==1)
{
alarm(0);
printf("error to get ipn");
goto end;//跳转语句
}
struct hostent hostinfo,*result;
char buf[1024];
int rc;
alarm(3);//设置超时
if(gethostbyname_r(host,&hostinfo,buf,1024,&result,&rc)==0)
{
alarm(0);//cancell alarm
if(NULL == result)
return NULL;
}
else
{
alarm(0);//cancell alarm
return NULL;
}
end:
/
strcpy(ip,inet_ntoa(*((struct in_addr*)hostinfo.h_addr)));
return ip;
}
首先声明 程序 在初期可以正常运行 可是运行来600多次以后——我是用单线程抓网页然后处理ip信息的,就会报错,错误如下:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x17d0b70 (LWP 4055)]
0x002ac908 in free@plt () from /lib/tls/i686/cmov/libc.so.6
(gdb) bt
#0 0x002ac908 in free@plt () from /lib/tls/i686/cmov/libc.so.6
#1 0x00370ce2 in __res_iclose () from /lib/tls/i686/cmov/libc.so.6
#2 0x009e613f in ?? () from /lib/tls/i686/cmov/libresolv.so.2
#3 0x009e38c3 in __libc_res_nquery () from /lib/tls/i686/cmov/libresolv.so.2
#4 0x009e3eeb in ?? () from /lib/tls/i686/cmov/libresolv.so.2
#5 0x009e416d in __libc_res_nsearch () from /lib/tls/i686/cmov/libresolv.so.2
#6 0x00249d17 in _nss_dns_gethostbyname3_r ()
from /lib/tls/i686/cmov/libnss_dns.so.2
#7 0x00000000 in ?? ()
希望遇到同种类型到朋友能够帮个忙 谢谢拉
代码:
static sigjmp_buf jmpbuf;
static void host_handler(int signo)//跳转函数
{
siglongjmp(jmpbuf,1);
}
char *get_ip(char *host)
{
struct sigaction act;
act.sa_flags=0;
act.sa_handler=host_handler;
char *ip=(char*)malloc(sizeof(char)*20);
if(ip ==NULL)
{
printf("malloc errorn");
exit(1);
}
memset(ip,0,20);
if((sigemptyset(&act.sa_mask)==-1)|| (sigaction(SIGALRM,&act,NULL)==-1))
{
perror("fail set signaln");
return NULL;
}
if(sigsetjmp(jmpbuf,1)==1)
{
alarm(0);
printf("error to get ipn");
goto end;//跳转语句
}
struct hostent hostinfo,*result;
char buf[1024];
int rc;
alarm(3);//设置超时
if(gethostbyname_r(host,&hostinfo,buf,1024,&result,&rc)==0)
{
alarm(0);//cancell alarm
if(NULL == result)
return NULL;
}
else
{
alarm(0);//cancell alarm
return NULL;
}
end:
/
strcpy(ip,inet_ntoa(*((struct in_addr*)hostinfo.h_addr)));
return ip;
}
首先声明 程序 在初期可以正常运行 可是运行来600多次以后——我是用单线程抓网页然后处理ip信息的,就会报错,错误如下:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x17d0b70 (LWP 4055)]
0x002ac908 in free@plt () from /lib/tls/i686/cmov/libc.so.6
(gdb) bt
#0 0x002ac908 in free@plt () from /lib/tls/i686/cmov/libc.so.6
#1 0x00370ce2 in __res_iclose () from /lib/tls/i686/cmov/libc.so.6
#2 0x009e613f in ?? () from /lib/tls/i686/cmov/libresolv.so.2
#3 0x009e38c3 in __libc_res_nquery () from /lib/tls/i686/cmov/libresolv.so.2
#4 0x009e3eeb in ?? () from /lib/tls/i686/cmov/libresolv.so.2
#5 0x009e416d in __libc_res_nsearch () from /lib/tls/i686/cmov/libresolv.so.2
#6 0x00249d17 in _nss_dns_gethostbyname3_r ()
from /lib/tls/i686/cmov/libnss_dns.so.2
#7 0x00000000 in ?? ()
希望遇到同种类型到朋友能够帮个忙 谢谢拉
|
我写了一个测试代码,至少到现在,没有出现你说的问题,可能是
别的地方代码出错.
别的地方代码出错.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void *prt_ip(void *);
static sigjmp_buf jmpbuf;
static void
host_handler (int signo)
{
siglongjmp (jmpbuf, 1);
}
char *
get_ip (char *host)
{
struct sigaction act;
act.sa_flags = 0;
act.sa_handler = host_handler;
char *ip = (char *) malloc (sizeof (char) * 20);
if (ip == NULL)
{
printf ("malloc errorn");
exit (1);
}
memset (ip, 0, 20);
if ((sigemptyset (&act.sa_mask) == -1)
|| (sigaction (SIGALRM, &act, NULL) == -1))
{
perror ("fail set signaln");
return NULL;
}
if (sigsetjmp (jmpbuf, 1) == 1)
{
alarm (0);
printf ("error to get ipn");
goto end;
}
struct hostent hostinfo, *result;
char buf[1024];
int rc;
alarm (3);
if (gethostbyname_r (host, &hostinfo, buf, 1024, &result, &rc) == 0)
{
alarm (0); //cancell alarm
if (NULL == result)
return NULL;
}
else
{
alarm (0); //cancell alarm
return NULL;
}
end:
strcpy (ip, inet_ntoa (*((struct in_addr *) hostinfo.h_addr)));
return ip;
}
void *
prt_ip(void *arg)
{
int i;
for (i = 0; i