【问题标题】:Why is faketime command skipping section of code为什么 faketime 命令会跳过代码段
【发布时间】:2021-02-09 14:39:34
【问题描述】:

我有一个 cpp 代码,我正在尝试使用 faketime 命令运行。我在两台相同的计算机上运行它。他们都在运行 RHEL 7。我注意到当我在一台计算机上运行我的代码时,它完全跳过了我的 popen 调用。

我的代码本质上是

char ntp[]= "192.168.1.200";
    FILE *jitter;
    char line[100];
    char *start;
    char * eol;
    char pps[] = "NTPS";
    jitter = popen("chronyc sources", "r");
        int i;
cout<<"reached here"<<endl;
    while(fgets(line,sizeof(line),jitter))
{
cout<<"line is\n"<<line<<endl;
 if(strstr(line,pps)){
        start = strpbrk(line,"#0+-");    
        cout<<"PPS is "<<start<<endl; 
        //find new line character and replace it with comma
        eol = strrchr(start,'\n');
        i=eol-start;
        start[i]=',';
    
        myfile<<start;
    }

    if(strstr(line,ntp)){
        myfile<<start;  
    }
}
    pclose(jitter);
}

我添加了一个打印语句

cout<<"reached here"<<endl;

但是当我使用“faketime 'last friday 5pm' ./code”运行它时,在一台计算机上由于某种原因它永远不会到达 print 语句,而在另一台计算机上却会。我在网上搜索没有成功(我没有运行近似算法,它们具有相同的编译器和生成文件等。我实际上是在对代码进行 git pull 并运行它)。

有人知道为什么吗?

谢谢

【问题讨论】:

  • 那么popen 会返回什么? errno 的值是多少?
  • @KamilCuk popen 返回 chronyc 源的值。 errno 为 0,所以它似乎没有出错
  • 您应该显示带有 print 语句的代码 - 以防出现阻止其执行的流控制问题。
  • @Wyck done... (它实际上是在 while 语句之后基本上打印 fgetsline)。奇怪的是,如果我不执行 faketime 命令,它会打印该行。
  • if (jitter == NULL) perror("popen failed"); 行在哪里?

标签: c++ popen chrony usefaketimers


【解决方案1】:

看来问题出在 SELinux 上。显然,当 SELinux 处于强制模式时,chrony 不能很好地交互。我将其切换为许可,它现在的行为与预期一样。我可以打电话给faketime 'last friday 5pm' chronyc sources,也可以在我的popen代码中使用它

【讨论】:

    猜你喜欢
    • 2015-05-22
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多