0

I have a cpp code that I'm trying to run with faketime command. I'm running it on two identical computers. They're both running RHEL 7. I noticed that when I run my code, on one computer, it totally skips my popen call.

My code is essentially

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);
}

I added a print statement of

cout<<"reached here"<<endl;

but when I run it with "faketime 'last friday 5pm' ./code", on one computer it never reaches the print statement for some reason while on the other it does. I searched online to no success (I'm not running a approximating algorithm, they have the same compiler and make file, etc. I'm literally doing a git pull of the code and running it).

Does anyone know why?

Thanks

Wyck
  • 10,311
  • 6
  • 39
  • 60
bchang32
  • 27
  • 3
  • So what does `popen` return? What is the value of `errno`? – KamilCuk Feb 09 '21 at 14:51
  • @KamilCuk popen returns the value of chronyc sources. errno is 0 so it doesnt seem to be error-ing – bchang32 Feb 09 '21 at 15:26
  • You should show the code with the print statement in place - in case there is a flow-control issue preventing it from executing. – Wyck Feb 09 '21 at 16:02
  • @Wyck done... (it was literally just after the while statement essentially printing the fgetsline). What is odd is that if I dont do the faketime command, itll print the line. – bchang32 Feb 09 '21 at 16:58
  • Where is the `if (jitter == NULL) perror("popen failed");` line? – Jeremy Friesner Feb 09 '21 at 16:59
  • @JeremyFriesner I put one above the cout< – bchang32 Feb 09 '21 at 17:40
  • Is your claim that it is not executing the statement `jitter = popen("chronyc sources", "r");`? Or is your claim that it is not executing the statement `cout<<"line is\n"< – Wyck Feb 09 '21 at 19:22
  • @Wyck When I run my code prefaced with faketime (so "faketime "5 days ago" ./code_run") it ignores the while fgets loop, so both. The actual code is the above plus it writing the chronyc sources call to a csv file so I didn't think you needed to see "myfile << line" statements" – bchang32 Feb 09 '21 at 19:53
  • To say it "ignores the loop" is incorrect. Surely it calls fgets, passing in `jitter` as an argument (jitter is assigned the result of popen, which is either the file handle or nullptr). The documentation for fgets says that it returns nullptr if A) there was an error (e.g. if popen failed) or B) if the EOF of the input stream is reached. Which would make the condition for the loop false. Probably A is happening. That's why I said to try again with popen("ls","r") or because it's less likely to fail. – Wyck Feb 09 '21 at 21:01
  • @Wyck ok so doing "ls" "works", but that still doesn't explain why chronyc sources "fails" when I prepend it with faketime on one computer but not the other nor why its causing it to not read in anything vs reading in chronyc sources when I don't prepend it. I need the chronycc sources call and not ls. I added a `if(jitter==NULL){cout<<"popen failed< – bchang32 Feb 10 '21 at 12:39
  • chronyc could be failing and writing its error message to stderr, which popen won't read (it only reads stdout). Try appending `2>&1` to the command. See https://stackoverflow.com/questions/6900577/c-popen-wont-catch-stderr – Wyck Feb 10 '21 at 19:06
  • @Wyck when I do that, it outputs that faketime failed "In ft_shm_init(), sem open failed: function not implemented" I have no idea what that means or how to resolve this – bchang32 Feb 11 '21 at 17:04
  • time to add a chrony tag to this question I think. Also, update the question with all the relevant new information you have discovered. Can you just run the command manually from the command line and see if it works? – Wyck Feb 11 '21 at 20:32

1 Answers1

0

So it appears the issue was with SELinux. Apparently chrony doesn't interact well when SELinux is in enforced mode. I switched it to permissive and it behaves as expected now. I can call faketime 'last friday 5pm' chronyc sources as well as use it in my popen code

bchang32
  • 27
  • 3