简体   繁体   中英

Get stdout of already running process in Linux in C

Right now, I'm having to start an external process in C. I'm currently using posix_spawn to create the process. It is necessary that I can monitor whether or not the process has terminated. I need to also have a link to the standard out of the process. I've looked at using popen, however, it does not provide an "easy" way of getting the pid. I'm slowly going insane as it can't possibly be this hard to get the stdout of a running process in Linux.

Also, on a further note, I need help deciphering what the file_actions parameter is supposed to mean. man(3) for posix_spawn on this topic says:

If file_actions is not NULL, then the file descriptors open in the child process shall be those open in the calling process as modified by the spawn file actions object pointed to by file_actions and the FD_CLOEXEC flag of each remaining open file descriptor after the spawn file actions have been processed.

If that isn't the definition of a run-on sentence, I have no idea what is.

Since you have the PID (returned from posix_spawn ) and you are running Linux, you will find the stdout of the process at /proc/<pid>/fd/1 . Just open (or fopen ) the file for reading.

The standard way is to use fork though. Use pipe and dup2 to get a file descriptor for reading the child's output, as in this question .

You can use posix_spawn for this, without having to use race-condition-prone, Linux-specific /proc/<pid>/fd/N . You can keep all the benefits of posix_spawn.

You were on the right track thinking about file_actions . Below is an example that prints out the child's stdout in Python-style triple quotes, as well as the child's exit code, from the parent process using posix_spawn and file_actions.

Here is an example of the example output.

child pid: 17468
child exit status: 0
child stdout:
"""Hello World!
"""

Here is the example.

#define _DEFAULT_SOURCE
#include <spawn.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

extern char **environ;

static void dump_child_stdout(int filedes)
{
    ssize_t num_read;
    char buf[1];

    printf("child stdout:\n\"\"\"");
    for (;;)
    {
        num_read = read(filedes, buf, sizeof(buf));
        if (num_read > 0)
        {
            printf("%c", buf[0]);
        }
        else
        {
            break;
        }
    }
    printf("\"\"\"\n");
}

int main(int argc, char *argv[])
{
    int status;
    pid_t pid;
    int out[2];
    posix_spawn_file_actions_t action;
    char *args[] = {"/bin/echo", "Hello World!", NULL };

    posix_spawn_file_actions_init(&action);

    pipe(out);

    posix_spawn_file_actions_adddup2(&action, out[1], STDOUT_FILENO);
    posix_spawn_file_actions_addclose(&action, out[0]);

    status = posix_spawn(&pid, args[0], &action, NULL, args, environ);
    if (status == 0)
    {
        printf("child pid: %d\n", pid);
        if (waitpid(pid, &status, 0) < 0)
        {
            perror("waitpid");
        }
        else
        {
            if (WIFEXITED(status))
            {
                printf("child exit status: %d\n", WEXITSTATUS(status));
            }
            else
            {
                printf("child died an unnatural death.\n");
            }

            close(out[1]);
            dump_child_stdout(out[0]);
        }
    }
    else
    {
        fprintf(stderr, "posix_spawn: %s\n", strerror(status));
        close(out[1]);
    }

    posix_spawn_file_actions_destroy(&action);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM