gpt4 book ai didi

c - 如何使用 apr 从在新进程中运行的程序捕获 stdout/stderr 输出?

转载 作者:行者123 更新时间:2023-12-04 00:31:15 25 4
gpt4 key购买 nike

问题很简单:我如何捕获子程序产生的 stderr/stdout 输出并将其重定向到一个文件,该子程序在新进程中运行,使用 Apache Portable Runtime 的 apr_proc_create 创建.

最终任务是在单独的进程中运行带有一些参数的外部命令(最好,但不是强制性的)并将其输出重定向到文件。

到目前为止,我得到了以下代码(为清楚起见删除了检查):

apr_procattr_t *attr;
apr_proc_t newproc;

const char *progname;
const char *args[100];

// progname and args are populated with data here

apr_procattr_create(&attr, p);
apr_procattr_io_set(attr, APR_CHILD_BLOCK, APR_CHILD_BLOCK, APR_CHILD_BLOCK);
apr_procattr_cmdtype_set(attr, APR_PROGRAM_PATH);
apr_proc_create(&newproc, progname, args, NULL, attr, p);

使用这段代码,子进程的输出被抑制(至少我没有在控制台中看到它),但它没有被重定向到一个文件。使用 APR_NO_PIPE 而不是 APR_CHILD_BLOCK 会导致将子级的输出刷新到父级 stdout/stderr

APR 似乎只有很少的“食谱”食谱,所以我无法用谷歌搜索。

有什么提示或建议吗?欢迎使用非 APR 解决方案,前提是它们像我发布的代码一样简单明了。

附言在 Debian 6(Linux 2.6.32-5-686) 上运行,gcc 4.4.5,但我认为这无关紧要。 :)

最佳答案

test cases for APR包含很多有用的例子。在你的情况下,你应该看看 testproc.c :

版权声明:此代码受 APL 2.0 保护

/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

static void test_file_redir(abts_case *tc, void *data)
{
apr_file_t *testout = NULL;
apr_file_t *testerr = NULL;
apr_off_t offset;
apr_status_t rv;
const char *args[2];
apr_procattr_t *attr;
apr_file_t *testfile = NULL;
apr_size_t length;
char *buf;

testfile = NULL;
rv = apr_file_open(&testfile, "data/stdin",
APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
APR_OS_DEFAULT, p);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_file_open(&testout, "data/stdout",
APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
APR_OS_DEFAULT, p);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_file_open(&testerr, "data/stderr",
APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
APR_OS_DEFAULT, p);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);

length = strlen(TESTSTR);
apr_file_write(testfile, TESTSTR, &length);
offset = 0;
rv = apr_file_seek(testfile, APR_SET, &offset);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
ABTS_ASSERT(tc, "File position mismatch, expected 0", offset == 0);

rv = apr_procattr_create(&attr, p);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_procattr_child_in_set(attr, testfile, NULL);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_procattr_child_out_set(attr, testout, NULL);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_procattr_child_err_set(attr, testerr, NULL);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_procattr_dir_set(attr, "data");
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_procattr_cmdtype_set(attr, APR_PROGRAM_ENV);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);

args[0] = "proc_child";
args[1] = NULL;

rv = apr_proc_create(&newproc, proc_child, args, NULL,
attr, p);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);

rv = apr_proc_wait(&newproc, NULL, NULL, APR_WAIT);
ABTS_INT_EQUAL(tc, APR_CHILD_DONE, rv);

offset = 0;
rv = apr_file_seek(testout, APR_SET, &offset);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);

length = 256;
buf = apr_pcalloc(p, length);
rv = apr_file_read(testout, buf, &length);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
ABTS_STR_EQUAL(tc, TESTSTR, buf);


apr_file_close(testfile);
apr_file_close(testout);
apr_file_close(testerr);

rv = apr_file_remove("data/stdin", p);;
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_file_remove("data/stdout", p);;
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
rv = apr_file_remove("data/stderr", p);;
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
}

关于c - 如何使用 apr 从在新进程中运行的程序捕获 stdout/stderr 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10501084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com