ITPub博客

首页 > IT基础架构 > 网络安全 > 网络断线检测中用到的popen

网络断线检测中用到的popen

原创 网络安全 作者:sgy618 时间:2011-01-17 15:37:01 0 删除 编辑

网络断线检测中用到的popen

[@more@]

/**
* @fn netPing
* @brief 获取Ping状态
* @param[in]
* @param[out]
* @return 0: down, 1: up
*/
int netPing(void)
{
int i = 0;
int ret = APP_FAIL;
uint networkByteOrder;
char buf[MAX_CMD_LEN] = {0};
char strbuf[100] = {0};
FILE *fd = NULL;

networkByteOrder = htonl(pSetupSetPing->detectIpAddr);
sprintf(strbuf, "/usr/local/sbin/ping -c 4 -t 1 %s", inet_ntoa(*(struct in_addr*)(&networkByteOrder)));

for(i = 0; i < pSetupSetPing->retryTimes; i++)
{
fd = popen(strbuf, "r");
if (NULL == fd)
{
perror("popen");
dbgPrintfl(DETAIL, "popen ping fail!n");
ret = APP_FAIL;
goto out;
}

dataNoBlockFgets(buf, sizeof(buf), fd);

if(0 == strlen(buf)) //ping失败
{
dbgPrintfl(DETAIL, "ping fail!n");
ret = APP_FAIL;
continue;
}
else
{
ret = APP_OK;
break;
}
}

out:
if(fd != NULL)
{
pclose(fd);
fd = NULL;
}

return ret;
}


/**
* @fn dataNoBlockFgets
* @brief 本地实现替换fgets的非阻塞版本
* @param[in] int size:待读取长度
* FILE* stream:读取的流
* @param[out] char* s:读取输出
* @return
*/
static char* dataNoBlockFgets(char *s, int size, FILE* stream)
{
int ret = 0;
int fno = -1;
fd_set rfds;
int maxFd;
struct timeval timeout;

fno = fileno(stream);
memset(s, 0, size);
do{
maxFd = 0;
FD_ZERO(&rfds);
FD_SET(fno, &rfds);

maxFd = maxFd > fno ? maxFd : fno;
maxFd += 1;

timeout.tv_sec = 2;
timeout.tv_usec = 0;

ret = select(maxFd, &rfds, NULL, NULL, &timeout);
if(ret > 0)
{
if(FD_ISSET(fno, &rfds))
{
read(fno, s, size);
}
}
}while(0);

dbgPrint(DETAIL, "%sn", s);

return s;
}

popen:
  进程I/O函数,与pclose函数一起使用。
表头文件
  #include
函数定义
  FILE * popen ( const char * command , const char * type );
  int pclose ( FILE * stream );
函数说明
  popen() 函数通过创建一个管道,调用 fork 产生一个子进程,执行一个 shell 以运行命令来开启一个进程。这个进程必须由 pclose() 函数关闭,而不是 fclose() 函数。pclose() 函数关闭标准 I/O 流,等待命令执行结束,然后返回 shell 的终止状态。如果 shell 不能被执行,则 pclose() 返回的终止状态与 shell 已执行 exit 一样。
   type 参数只能是读或者写中的一种,得到的返回值(标准 I/O 流)也具有和 type 相应的只读或只写类型。如果 type 是 "r" 则文件指针连接到 command 的标准输出;如果 type 是 "w" 则文件指针连接到 command 的标准输入。
   command 参数是一个指向以 NULL 结束的 shell 命令字符串的指针。这行命令将被传到 bin/sh 并使用-c 标志,shell 将执行这个命令。
   popen 的返回值是个标准 I/O 流,必须由 pclose 来终止。前面提到这个流是单向的。所以向这个流写内容相当于写入该命令的标准输入;命令的标准输出和调用 popen 的进程相同。与之相反的,从流中读数据相当于读取命令的标准输出;命令的标准输入和调用 popen 的进程相同。
返回值
  如果调用 fork() 或 pipe() 失败,或者不能分配内存将返回NULL,否则返回标准 I/O 流。
返回错误
  popen 没有为内存分配失败设置 errno 值。
  如果调用 fork() 或 pipe() 时出现错误,errno 被设为相应的错误类型。
  如果 type 参数不合法,errno将返回EINVAL。
使用举例
  if((fp=popen("/usr/bin/uptime","r"))==NULL);
  {
  sprintf(buf,"error: %sn", strerror(errno));
  ....//异常处理
  }
  else
  {
  ....
  pclose(fp);
  }

函数原型:int _fileno( FILE *stream );
函数功能:fileno()用来取得参数stream指定的文件流所使用的文件描述符
返回值:某个数据流的文件句柄
头文件:stdio.h

例子:
int main(void)
  {
  FILE *fp;
  int fd;
  fp = fopen("/etc/passwd", "r");
  fd = fileno(fp);
  printf("fd = %dn", fd);
  fclose(fp);
  return 0;
  }

#define fileno(__p) ((__p)->_file)
  因为FILE 结构体定义为
  类似
  typedef struct {
  unsigned char *_ptr;
  int _cnt;
  unsigned char *_base;
  unsigned char *_bufendp;
  short _flag;
  short _file;
  int __stdioid;
  char *__newbase;
  void *_lock;
  } FILE;
  其中 _file就是文件描述符。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23168012/viewspace-1044778/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Ethereal使用入门
请登录后发表评论 登录
全部评论

注册时间:2010-01-12

  • 博文量
    104
  • 访问量
    396307