监视C中stdin,stdout和stderr中的字节

时间:2011-05-26 21:24:21

标签: c linux system

我需要计算通过stdin发送给子进程的字节数,以及子进程写入stdout和stderr的字节数。子进程调用execvp,因此我无法从进程本身监视这些统计信息。我目前的策略是创建3个额外的子进程,每个进程通过管道监视每个std流(或者在stdin的情况下,只是从stdin读取)。

这种策略看起来真的很脆弱,我正在做一些奇怪的事情,这使得监视stdout / err的进程无法从管道各自的末端读取(并使它们无限期挂起)。代码如下。

这会创建三个辅助子进程,并允许它们统计统计信息:

void controles(struct fds *des)
{
    int ex[2];
    int err[2];

    int n_in = 0;
    int c_in;

    int n_ex = 0;
    int c_ex;

    int n_err = 0;
    int c_err;

    pipe(ex);
    pipe(err);

    /*has two fields, for the write end of the stdout pipe and the stderr pipe. */
    des->err = err[1];
    des->ex = ex[1];
    switch (fork()) {
    case 0:     /*stdin */
        while (read(0, &c_in, 1) == 1)
            n_in++;
        if (n_in > 0)
            printf("%d bytes to stdin\n", n_in);
        exit(n_in);
    default:
        break;
    }

    switch (fork()) {
    case 0:     /*stdout */
        close(ex[1]);
        /*pretty sure this is wrong */
        while (read(ex[0], &c_ex, 1) == 1) {
            n_ex++;
            write(1, &c_ex, 1);
        }
        if (n_ex > 0)
            printf("%d bytes to stdout\n", n_ex);
        close(ex[0]);
        exit(n_ex);
    default:
        close(ex[0]);
    }
    switch (fork()) {
    case 0:     /*error */
        close(err[1]);
        /*also probably have a problem here */
        while (read(err[0], &c_err, 1) == 1) {
            n_err++;
            write(2, &c_err, 1);
        }
        if (n_err > 0)
            printf("%d bytes to stderr\n", n_err);
        close(err[0]);
        exit(n_err);
    default:
        close(err[0]);
    }
}

这是一个代码片段(在子进程中),它从fds结构中设置两个fd,以便子进程应该写入管道而不是stdin / stderr。

    dup2(des.ex, 1);
dup2(des.err, 2);
close(des.ex); close(des.err); /*Is this right?*/
execvp(opts->exec, opts->options); /*sure this is working fine*/

我迷路了,任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:4)

我认为你的代码可以通过稍微分开来改进;会计和复制程序基本上都是相同的任务,如果你选择继续使用多个流程,可以简单地写一下:

void handle_fd_pair(char *name, int in, int out) {
    char buf[1024];
    int count = 0, n;
    char fn[PATH_MAX];
    snprintf(fn, PATH_MAX - 1, "/tmp/%s_count", name);
    fn[PATH_MAX-1] = '\0';
    FILE *output = fopen(fn, "w");
    /* handle error */

    while((n = read(in, buf, 1024)) > 0) {
        count+=n;
        writen(out, buf, n); /* see below */
    }

    fprintf(output, "%s copied %d bytes\n", name, count);
    fclose(output);
}

我们可以使用Advanced Programming in the Unix Environment源代码中的writen()函数处理部分写入,而不是一次性使用char,这对于适量的数据来说是低效的:

ssize_t             /* Write "n" bytes to a descriptor  */
writen(int fd, const void *ptr, size_t n)
{
  size_t      nleft;
  ssize_t     nwritten;

  nleft = n;
  while (nleft > 0) {
      if ((nwritten = write(fd, ptr, nleft)) < 0) {
          if (nleft == n)
              return(-1); /* error, return -1 */
          else
              break;      /* error, return amount written so far */
      } else if (nwritten == 0) {
          break;
      }
      nleft -= nwritten;
      ptr   += nwritten;
  }
  return(n - nleft);      /* return >= 0 */
}

有了帮手,我认为其余的可以更轻松。叉子a 每个流的新子节点,并提供in[0]读取端out[1]和。{ 管道的err[1]写入端给孩子。

每个孩子的所有close()次电话都非常丑陋,但是还是试着 在所有fds的数组周围写一个小包装器,并免除 作为参数传入的,似乎也很麻烦。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#ifndef PATH_MAX
#define PATH_MAX 128
#endif

void handle_fd_pair(char *name, int in, int out) {
    char buf[1024];
    int count = 0, n;
    char fn[PATH_MAX];
    snprintf(fn, PATH_MAX - 1, "/tmp/%s_count", name);
    fn[PATH_MAX-1] = '\0';
    FILE *output = fopen(fn, "w");
    /* handle error */

    while((n = read(in, buf, 1024)) > 0) {
        count+=n;
        writen(out, buf, n); /* see below */
    }

    fprintf(output, "%s copied %d bytes\n", name, count);
    fclose(output);
}

int main(int argc, char* argv[]) {
    int in[2], out[2], err[2];
    pid_t c1, c2, c3;

    pipe(in);
    pipe(out);
    pipe(err);

    if ((c1 = fork()) < 0) {
        perror("can't fork first child");
        exit(1);
    } else if (c1 == 0) {
        close(in[0]);
        close(out[0]);
        close(out[1]);
        close(err[0]);
        close(err[1]);
        handle_fd_pair("stdin", 0, in[1]);
        exit(0);
    }

    if ((c2 = fork()) < 0) {
        perror("can't fork second child");
        exit(1);
    } else if (c2 == 0) {
        close(in[0]);
        close(in[1]);
        close(out[1]);
        close(err[0]);
        close(err[1]);
        handle_fd_pair("stdout", out[0], 1);
        exit(0);
    }

    if ((c3 = fork()) < 0) {
        perror("can't fork third child");
        exit(1);
    } else if (c3 == 0) {
        close(in[0]);
        close(in[1]);
        close(out[0]);
        close(out[1]);
        close(err[1]);
        handle_fd_pair("stderr", err[0], 2);
        exit(0);
    }

    /* parent falls through to here, no children */

   close(in[1]);
   close(out[0]);
   close(err[0]);
   close(0);
   close(1);
   close(2);
   dup2(in[0], 0);
   dup2(out[1], 1);
   dup2(err[1], 2);
   system(argv[1]);
   exit(1); /* can't reach */
}

无论如何它似乎适用于玩具应用程序:)

$ ./dup cat
hello
hello
$ ls -l *count
-rw-r--r-- 1 sarnold sarnold 22 2011-05-26 17:41 stderr_count
-rw-r--r-- 1 sarnold sarnold 21 2011-05-26 17:41 stdin_count
-rw-r--r-- 1 sarnold sarnold 22 2011-05-26 17:41 stdout_count
$ cat *count
stderr copied 0 bytes
stdin copied 6 bytes
stdout copied 6 bytes

我认为值得指出的是,你也可以实现这一点 只有一个进程的程序,并使用select(2)来确定哪个 文件描述符需要读写。

答案 1 :(得分:0)

总的来说,我认为你走在正确的轨道上。

一个问题是,在你的stderr和stdout处理程序中,应该从管道中挑选字节并写入真正的stderr / stdout,你要回写到同一个管道。

了解如何启动子进程也很有帮助。你给了一个代码片段来关闭真正的stderr,然后将管道fd重复到stderr的fd,但你可能想要在父进程中(在fork之后和exec之前),这样你就不需要修改源代码了孩子的过程。你应该能够从父母一直这样做。