博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dup和dup2重定向标准输出到文件
阅读量:4602 次
发布时间:2019-06-09

本文共 2300 字,大约阅读时间需要 7 分钟。

extern "C" int lsof_entry( int argc, char *argv[] );bool OSRunLsof( const wxString &strTempPath ){  // create a temp file to retrieve the lsof result.  wxString strLsofFile = OSCreateTempFileName( strTempPath + "lof" );  if( strLsofFile.IsEmpty() )  {    LogDebug( "OSRunLsof() failed to create temp lsof file." );    return false;  }  LogDebug( "OSRunLsof() successfully created temp lsof file: %s.", strLsofFile.c_str() );  wxGetApp().AddFileNameToDeleteList( strLsofFile );    bool bResult = false;  // Open the created temp file.  wxFile fileLsof( strLsofFile, wxFile::read_write );  if( fileLsof.IsOpened() )  {    // backup stdout and stderr.    int iStdout = dup(STDOUT_FILENO);    int iStderr = dup(STDERR_FILENO);    if( ( -1 != iStderr ) && ( -1 != iStdout ) )    {      int fdNull = open("/dev/null", O_RDWR);      if( -1 != fdNull )      {        // redirect stdout and stderr.        if( ( -1 != dup2( fileLsof.fd(), STDOUT_FILENO ) ) &&             ( -1 != dup2( fdNull, STDERR_FILENO ) ) )        {          // format arguments: losf -i -P -n          const int ARG_NUM = 3;          char strArg0[5] = { "lsof" };          char strArg1[3] = { "-i" };          char strArg2[3] = { "-P" };          char strArg3[3] = { "-n" };          char* strArgv[ ARG_NUM + 1 ] = { strArg0, strArg1, strArg2, strArg3 };          // run lsof.          if( 0 == lsof_entry( ARG_NUM, strArgv ) )          {            bResult = true;          }          // restore stdout and stderr.          dup2( iStdout, STDOUT_FILENO );          dup2( iStderr, STDERR_FILENO );        }        else        {          LogDebug( "OSRunLsof() failed to dup2 STD_FILENO." );        }      }      else      {        LogDebug( "OSRunLsof() failed to open /dev/null." );      }      // close backup.      close( iStdout );      close( iStderr );    }    else    {      LogDebug( "OSRunLsof() failed to dup STD_FILENO." );    }    fileLsof.Close();  }  else  {    LogDebug( "OSRunLsof() failed to open temp lsof file: %s.", strLsofFile.c_str() );  }  // Process lsof file.  //if( bResult )  //{  //  // temporary process  //  wxCopyFile( strLsofFile, "/root/lsof.txt" );  //}  wxGetApp().RemoveFileNameFromDeleteList( strLsofFile );  return bResult;}

  

转载于:https://www.cnblogs.com/playerken/archive/2013/01/25/2876669.html

你可能感兴趣的文章
bzoj 5289: [Hnoi2018]排列
查看>>
joomla处境堪忧
查看>>
Jquery-AJAX
查看>>
mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by
查看>>
LeetCode55 Jump Game
查看>>
poj 3764 The xor-longest Path (01 Trie)
查看>>
预备作业01
查看>>
【Spark】Spark-Redis连接池
查看>>
【云计算】使用supervisor管理Docker多进程-ntpd+uwsgi+nginx示例最佳实践
查看>>
Ubuntu16.04下配置ssh免密登录
查看>>
实验二 2
查看>>
will-change属性
查看>>
android学习笔记54——ContentProvider
查看>>
Unity3d android开发之触摸操作识别-双击,滑动去噪处理
查看>>
Custom view * is not using the 2- or 3-argument View constructors; XML attributes will not work
查看>>
模型选择准则
查看>>
安卓动态增加按钮
查看>>
iOS7程序后台运行
查看>>
maven+testng+reportng的pom设置
查看>>
IT telephone interview
查看>>