pppd popen hanging in C

1.4k views Asked by At

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?

1

There are 1 answers

2
TOC On

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){