为什么 faketime 命令会跳过代码段

问题描述 投票:0回答:1

我有一个 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”运行它时,在一台计算机上由于某种原因它永远不会到达打印语句,而在另一台计算机上却到达了。我在网上搜索没有成功(我没有运行近似算法,它们具有相同的编译器和 make 文件等。我实际上是在执行代码的 git pull 并运行它)。

有谁知道为什么吗?

谢谢

c++ popen usefaketimers
1个回答
0
投票

看来问题出在 SELinux 上。显然,当 SELinux 处于强制模式时,chrony 不能很好地交互。我将其切换为宽容,现在它的行为符合预期。我可以调用

faketime 'last friday 5pm' chronyc sources
以及在我的 popen 代码中使用它

© www.soinside.com 2019 - 2024. All rights reserved.