Guest User

Untitled

a guest
Jul 14th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. How do I capture stdout/stderr output from program running in a new process using apr?
  2. apr_procattr_t *attr;
  3. apr_proc_t newproc;
  4.  
  5. const char *progname;
  6. const char *args[100];
  7.  
  8. // progname and args are populated with data here
  9.  
  10. apr_procattr_create(&attr, p);
  11. apr_procattr_io_set(attr, APR_CHILD_BLOCK, APR_CHILD_BLOCK, APR_CHILD_BLOCK);
  12. apr_procattr_cmdtype_set(attr, APR_PROGRAM_PATH);
  13. apr_proc_create(&newproc, progname, args, NULL, attr, p);
  14.  
  15. /* Licensed to the Apache Software Foundation (ASF) under one or more
  16. * contributor license agreements. See the NOTICE file distributed with
  17. * this work for additional information regarding copyright ownership.
  18. * The ASF licenses this file to You under the Apache License, Version 2.0
  19. * (the "License"); you may not use this file except in compliance with
  20. * the License. You may obtain a copy of the License at
  21. *
  22. * http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS,
  26. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing permissions and
  28. * limitations under the License.
  29. */
  30.  
  31. static void test_file_redir(abts_case *tc, void *data)
  32. {
  33. apr_file_t *testout = NULL;
  34. apr_file_t *testerr = NULL;
  35. apr_off_t offset;
  36. apr_status_t rv;
  37. const char *args[2];
  38. apr_procattr_t *attr;
  39. apr_file_t *testfile = NULL;
  40. apr_size_t length;
  41. char *buf;
  42.  
  43. testfile = NULL;
  44. rv = apr_file_open(&testfile, "data/stdin",
  45. APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
  46. APR_OS_DEFAULT, p);
  47. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  48. rv = apr_file_open(&testout, "data/stdout",
  49. APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
  50. APR_OS_DEFAULT, p);
  51. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  52. rv = apr_file_open(&testerr, "data/stderr",
  53. APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
  54. APR_OS_DEFAULT, p);
  55. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  56.  
  57. length = strlen(TESTSTR);
  58. apr_file_write(testfile, TESTSTR, &length);
  59. offset = 0;
  60. rv = apr_file_seek(testfile, APR_SET, &offset);
  61. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  62. ABTS_ASSERT(tc, "File position mismatch, expected 0", offset == 0);
  63.  
  64. rv = apr_procattr_create(&attr, p);
  65. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  66. rv = apr_procattr_child_in_set(attr, testfile, NULL);
  67. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  68. rv = apr_procattr_child_out_set(attr, testout, NULL);
  69. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  70. rv = apr_procattr_child_err_set(attr, testerr, NULL);
  71. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  72. rv = apr_procattr_dir_set(attr, "data");
  73. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  74. rv = apr_procattr_cmdtype_set(attr, APR_PROGRAM_ENV);
  75. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  76.  
  77. args[0] = "proc_child";
  78. args[1] = NULL;
  79.  
  80. rv = apr_proc_create(&newproc, proc_child, args, NULL,
  81. attr, p);
  82. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  83.  
  84. rv = apr_proc_wait(&newproc, NULL, NULL, APR_WAIT);
  85. ABTS_INT_EQUAL(tc, APR_CHILD_DONE, rv);
  86.  
  87. offset = 0;
  88. rv = apr_file_seek(testout, APR_SET, &offset);
  89. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  90.  
  91. length = 256;
  92. buf = apr_pcalloc(p, length);
  93. rv = apr_file_read(testout, buf, &length);
  94. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  95. ABTS_STR_EQUAL(tc, TESTSTR, buf);
  96.  
  97.  
  98. apr_file_close(testfile);
  99. apr_file_close(testout);
  100. apr_file_close(testerr);
  101.  
  102. rv = apr_file_remove("data/stdin", p);;
  103. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  104. rv = apr_file_remove("data/stdout", p);;
  105. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  106. rv = apr_file_remove("data/stderr", p);;
  107. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment