Getting the PID from popen

14k Views Asked by At

I have a program that uses popen() in order to open and read the output from a shell command. The problem is, as far as I can tell, there is no easy way to get the PID of the running process, and hence, you can't kill it if it gets stuck. So the question is, how can you retrieve the PID from a process opened with popen?

3

There are 3 best solutions below

16
On

The solution I came up with (and the general consensus) is to create a new popen function that allows me to retrieve the PID. Since I was unable to find a simple example of this on SO, I wanted to post my implementation in the hopes that it helps somebody else. Feedback and alternate solutions are welcome.

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <string>
#include <sstream>

using namespace std;

#define READ   0
#define WRITE  1
FILE * popen2(string command, string type, int & pid)
{
    pid_t child_pid;
    int fd[2];
    pipe(fd);

    if((child_pid = fork()) == -1)
    {
        perror("fork");
        exit(1);
    }

    /* child process */
    if (child_pid == 0)
    {
        if (type == "r")
        {
            close(fd[READ]);    //Close the READ end of the pipe since the child's fd is write-only
            dup2(fd[WRITE], 1); //Redirect stdout to pipe
        }
        else
        {
            close(fd[WRITE]);    //Close the WRITE end of the pipe since the child's fd is read-only
            dup2(fd[READ], 0);   //Redirect stdin to pipe
        }

        setpgid(child_pid, child_pid); //Needed so negative PIDs can kill children of /bin/sh
        execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
        exit(0);
    }
    else
    {
        if (type == "r")
        {
            close(fd[WRITE]); //Close the WRITE end of the pipe since parent's fd is read-only
        }
        else
        {
            close(fd[READ]); //Close the READ end of the pipe since parent's fd is write-only
        }
    }

    pid = child_pid;

    if (type == "r")
    {
        return fdopen(fd[READ], "r");
    }

    return fdopen(fd[WRITE], "w");
}

int pclose2(FILE * fp, pid_t pid)
{
    int stat;

    fclose(fp);
    while (waitpid(pid, &stat, 0) == -1)
    {
        if (errno != EINTR)
        {
            stat = -1;
            break;
        }
    }

    return stat;
}

int main()
{
    int pid;
    string command = "ping 8.8.8.8"; 
    FILE * fp = popen2(command, "r", pid);
    char command_out[100] = {0};
    stringstream output;

    //Using read() so that I have the option of using select() if I want non-blocking flow
    while (read(fileno(fp), command_out, sizeof(command_out)-1) != 0)
    {
        output << string(command_out);
        kill(-pid, 9);
        memset(&command_out, 0, sizeof(command_out));
    }

    string token;
    while (getline(output, token, '\n'))
        printf("OUT: %s\n", token.c_str());

    pclose2(fp, pid);

    return 0;
}
0
On

I just found the "original" implementation here: link and extended it a little to "return" the pid in the parameter.

#define READ    0
#define WRITE   1

FILE *popen(const char *command, const char *mode, pid_t* pid)
{
   int  pfp[2];     /* the pipe and the process */
    FILE *fp;       /* fdopen makes a fd a stream   */
    int parent_end, child_end;  /* of pipe          */

    if ( *mode == 'r' ){        /* figure out direction     */
        parent_end = READ;
        child_end = WRITE ;
    } else if ( *mode == 'w' ){
        parent_end = WRITE;
            child_end = READ ;
    } else return NULL ;

    if ( pipe(pfp) == -1 )          /* get a pipe       */
        return NULL;
    if ( (*pid = fork()) == -1 ){       /* and a process    */
        close(pfp[0]);          /* or dispose of pipe   */
        close(pfp[1]);
        return NULL;
    }

    /* --------------- parent code here ------------------- */
    /*   need to close one end and fdopen other end     */

    if ( *pid > 0 ){    
        if (close( pfp[child_end] ) == -1 )
            return NULL;
        return fdopen( pfp[parent_end] , mode); /* same mode */
    }

    /* --------------- child code here --------------------- */
    /*   need to redirect stdin or stdout then exec the cmd  */

    if ( close(pfp[parent_end]) == -1 ) /* close the other end  */
        exit(1);            /* do NOT return    */

    if ( dup2(pfp[child_end], child_end) == -1 )
        exit(1);

    if ( close(pfp[child_end]) == -1 )  /* done with this one   */
        exit(1);
                        /* all set to run cmd   */
    execl( "/bin/sh", "sh", "-c", command, NULL );
    exit(1);
}
1
On

CLARIFICATION

I tried to use the defined functions by @Gillespie's answer but found out that the pid in the C/C++ program was different from the one returned by the terminal command pgrep and looking at the output of ps -aux | grep myNameProc it seemed the process of the C program was forked once more.

I think because execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL); is actually equivalent to /bin/sh cmd string. So basically the child process of your C (or C++) program is creating a new process that does /bin/sh yourRealProcess where yourRealProcess is the one specified in the command string.

I solved doing the following: execl(command.c_str(), command.c_str(), (char*)NULL);. However, as specified by @Gillespie in the previous comments, in this way you will not be able to pass arguments to your process.

C IMPLEMENTATION

According to my needs I readapted @Gillespie's functions to include the above discussed modification and to work in the C programming language:

FILE * custom_popen(char* command, char type, pid_t* pid)
{
    pid_t child_pid;
    int fd[2];
    pipe(fd);

    if((child_pid = fork()) == -1)
    {
        perror("fork");
        exit(1);
    }

    /* child process */
    if (child_pid == 0)
    {
        if (type == 'r')
        {
            close(fd[0]);    //Close the READ end of the pipe since the child's fd is write-only
            dup2(fd[1], 1); //Redirect stdout to pipe
        }
        else
        {
            close(fd[1]);    //Close the WRITE end of the pipe since the child's fd is read-only
            dup2(fd[0], 0);   //Redirect stdin to pipe
        }

        setpgid(child_pid, child_pid); //Needed so negative PIDs can kill children of /bin/sh
        execl(command, command, (char*)NULL);
        exit(0);
    }
    else
    {
        printf("child pid %d\n", child_pid);
        if (type == 'r')
        {
            close(fd[1]); //Close the WRITE end of the pipe since parent's fd is read-only
        }
        else
        {
            close(fd[0]); //Close the READ end of the pipe since parent's fd is write-only
        }
    }

    *pid = child_pid;

    if (type == 'r')
    {
        return fdopen(fd[0], "r");
    }

    return fdopen(fd[1], "w");
}

int custom_pclose(FILE * fp, pid_t pid)
{
    int stat;

    fclose(fp);
    while (waitpid(pid, &stat, 0) == -1)
    {
        if (errno != EINTR)
        {
            stat = -1;
            break;
        }
    }

    return stat;
}