subproc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * subproc.c - subprocess helper routines
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <signal.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/dpkg.h>
  32. #include <dpkg/subproc.h>
  33. static int signo_ignores[] = { SIGQUIT, SIGINT };
  34. static struct sigaction sa_save[array_count(signo_ignores)];
  35. void
  36. subproc_signals_setup(const char *name)
  37. {
  38. struct sigaction sa;
  39. size_t i;
  40. onerr_abort++;
  41. memset(&sa, 0, sizeof(sa));
  42. sigemptyset(&sa.sa_mask);
  43. sa.sa_handler = SIG_IGN;
  44. sa.sa_flags = 0;
  45. for (i = 0; i < array_count(signo_ignores); i++)
  46. if (sigaction(signo_ignores[i], &sa, &sa_save[i]))
  47. ohshite(_("unable to ignore signal %s before running %.250s"),
  48. strsignal(signo_ignores[i]), name);
  49. push_cleanup(subproc_signals_cleanup, ~0, NULL, 0, 0);
  50. onerr_abort--;
  51. }
  52. void
  53. subproc_signals_cleanup(int argc, void **argv)
  54. {
  55. size_t i;
  56. for (i = 0; i < array_count(signo_ignores); i++) {
  57. if (sigaction(signo_ignores[i], &sa_save[i], NULL)) {
  58. fprintf(stderr, _("error un-catching signal %s: %s\n"),
  59. strsignal(signo_ignores[i]), strerror(errno));
  60. onerr_abort++;
  61. }
  62. }
  63. }
  64. static void
  65. print_subproc_error(const char *emsg, const char *contextstring)
  66. {
  67. fprintf(stderr, _("%s (subprocess): %s\n"), dpkg_get_progname(), emsg);
  68. }
  69. pid_t
  70. subproc_fork(void)
  71. {
  72. pid_t pid;
  73. pid = fork();
  74. if (pid == -1) {
  75. onerr_abort++;
  76. ohshite(_("fork failed"));
  77. }
  78. if (pid > 0)
  79. return pid;
  80. /* Push a new error context, so that we don't do the other cleanups,
  81. * because they'll be done by/in the parent process. */
  82. push_error_context_func(catch_fatal_error, print_subproc_error, NULL);
  83. return pid;
  84. }
  85. int
  86. subproc_check(int status, const char *desc, int flags)
  87. {
  88. void (*out)(const char *fmt, ...) DPKG_ATTR_PRINTF(1);
  89. int n;
  90. if (flags & PROCWARN)
  91. out = warning;
  92. else
  93. out = ohshit;
  94. if (WIFEXITED(status)) {
  95. n = WEXITSTATUS(status);
  96. if (!n)
  97. return 0;
  98. if (flags & PROCNOERR)
  99. return n;
  100. out(_("subprocess %s returned error exit status %d"), desc, n);
  101. } else if (WIFSIGNALED(status)) {
  102. n = WTERMSIG(status);
  103. if (!n)
  104. return 0;
  105. if ((flags & PROCPIPE) && n == SIGPIPE)
  106. return 0;
  107. if (n == SIGINT)
  108. out(_("subprocess %s was interrupted"), desc);
  109. else
  110. out(_("subprocess %s was killed by signal (%s)%s"),
  111. desc, strsignal(n),
  112. WCOREDUMP(status) ? _(", core dumped") : "");
  113. } else {
  114. out(_("subprocess %s failed with wait status code %d"), desc,
  115. status);
  116. }
  117. return -1;
  118. }
  119. int
  120. subproc_wait(pid_t pid, const char *desc)
  121. {
  122. pid_t r;
  123. int status;
  124. while ((r = waitpid(pid, &status, 0)) == -1 && errno == EINTR) ;
  125. if (r != pid) {
  126. onerr_abort++;
  127. ohshite(_("wait for subprocess %s failed"), desc);
  128. }
  129. return status;
  130. }
  131. int
  132. subproc_wait_check(pid_t pid, const char *desc, int flags)
  133. {
  134. int status;
  135. status = subproc_wait(pid, desc);
  136. return subproc_check(status, desc, flags);
  137. }