process.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MIT
  4. *
  5. * Portions created by Alan Antonuk are Copyright (c) 2012-2013
  6. * Alan Antonuk. All Rights Reserved.
  7. *
  8. * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
  9. * All Rights Reserved.
  10. *
  11. * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
  12. * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person
  15. * obtaining a copy of this software and associated documentation
  16. * files (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy,
  18. * modify, merge, publish, distribute, sublicense, and/or sell copies
  19. * of the Software, and to permit persons to whom the Software is
  20. * furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. * ***** END LICENSE BLOCK *****
  34. */
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #include <errno.h>
  39. #include <spawn.h>
  40. #include <sys/wait.h>
  41. #include <unistd.h>
  42. #include "common.h"
  43. #include "process.h"
  44. extern char **environ;
  45. void pipeline(const char *const *argv, struct pipeline *pl) {
  46. posix_spawn_file_actions_t file_acts;
  47. int pipefds[2];
  48. if (pipe(pipefds)) {
  49. die_errno(errno, "pipe");
  50. }
  51. die_errno(posix_spawn_file_actions_init(&file_acts),
  52. "posix_spawn_file_actions_init");
  53. die_errno(posix_spawn_file_actions_adddup2(&file_acts, pipefds[0], 0),
  54. "posix_spawn_file_actions_adddup2");
  55. die_errno(posix_spawn_file_actions_addclose(&file_acts, pipefds[0]),
  56. "posix_spawn_file_actions_addclose");
  57. die_errno(posix_spawn_file_actions_addclose(&file_acts, pipefds[1]),
  58. "posix_spawn_file_actions_addclose");
  59. die_errno(posix_spawnp(&pl->pid, argv[0], &file_acts, NULL,
  60. (char *const *)argv, environ),
  61. "posix_spawnp: %s", argv[0]);
  62. die_errno(posix_spawn_file_actions_destroy(&file_acts),
  63. "posix_spawn_file_actions_destroy");
  64. if (close(pipefds[0])) {
  65. die_errno(errno, "close");
  66. }
  67. pl->infd = pipefds[1];
  68. }
  69. int finish_pipeline(struct pipeline *pl) {
  70. int status;
  71. if (close(pl->infd)) {
  72. die_errno(errno, "close");
  73. }
  74. if (waitpid(pl->pid, &status, 0) < 0) {
  75. die_errno(errno, "waitpid");
  76. }
  77. return WIFEXITED(status) && WEXITSTATUS(status) == 0;
  78. }