1

I am launching pppd using popen in my program to make obtaining the IP address and interface name a little bit easier. My code runs fine independently and is a pretty typical implementation. The problem begins when it runs in the full program (too large to post)... the loop seems to hang for quite a while at the fgets() line. The popen is launched in its own thread that is then managed based on the output.

The popen/pppd code is essentially the following.

int main(void){
    pthread_create(&thread, NULL, pppd, (char *)NULL);
    pthread_join(thread, NULL);

    return 0;
}

void *pppd(char *args){
    char* command = malloc(32);

    sprintf(command, "pppd %s call %s", dev, provider);
    pppd_stream = popen(command, "r");

    if(pppd_stream == NULL){
        pppd_terminated = TRUE;
        return;
    }

    free(command);

    while(fgets(buffer, 128, d->pppd_stream) != NULL){

        //handle_output
    }

}

CPU usage isnt a problem, the system and the other parts of the program are still responsive and running as expected.

Any thoughts on what could be causing this slow down?

DarkRyuu
  • 169
  • 2
  • 3
  • 13
  • did you check the return value wasn't `NULL`? – Flexo Aug 12 '12 at 20:42
  • Yep, sorry this exists in the complete version. I will update now with a more accurate implementation. – DarkRyuu Aug 12 '12 at 20:52
  • How big is your process and how much RAM do you have? Also, you can use `strace -f` and follow child processes to see if there is a system call that makes `pppd` hang. Does it hang when you run the `pppd` command from the command line? – jxh Aug 12 '12 at 20:58
  • I have about 128MB of RAM out of 2GB free. Running pppd from the terminal wasn't having the same problem, however I noticed a pppd session that wasn't exiting even after trying kill and killall. One reboot later the command line is now behaving in the same way. strace -f seems to hang as well at "ioctl(8, TIOCSETD" – DarkRyuu Aug 12 '12 at 21:26
  • It seems to be a problem with pppd? I was having my program kill an existing pppd session if it had a lock on the device I wanted to remove. I was also manually cleaning up the lock file. PPPD no longer seems to be leaving the lock file when it exits and it is now working as expected... I am even more confused now. I haven't changed the pppd option file to stop it locking either. – DarkRyuu Aug 12 '12 at 22:05

1 Answers1

0

Ensure that your command is null terminated string:

#define COMMAND_BUFFER_SIZE    256   /* Modify this if you need */

snprintf(command, COMMAND_BUFFER_SIZE, "pppd %s call %s", dev, provider);
command[COMMAND_BUFFER_SIZE - 1] = '\0';
pppd_stream = popen(command, "r");

EDIT:

Check your fgets:

while(fgets(buffer, 128, d->pppd_stream) != NULL){

You may want this:

while(fgets(buffer, 128, pppd_stream) != NULL){
TOC
  • 4,326
  • 18
  • 21