subproc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * 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 <stdio.h>
  28. #include <dpkg/i18n.h>
  29. #include <dpkg/dpkg.h>
  30. #include <dpkg/subproc.h>
  31. static int catch_signals[] = { SIGQUIT, SIGINT };
  32. static struct sigaction uncatch_signals[sizeof_array(catch_signals)];
  33. void
  34. subproc_signals_setup(const char *name)
  35. {
  36. size_t i;
  37. struct sigaction catchsig;
  38. onerr_abort++;
  39. memset(&catchsig, 0, sizeof(catchsig));
  40. catchsig.sa_handler = SIG_IGN;
  41. sigemptyset(&catchsig.sa_mask);
  42. catchsig.sa_flags = 0;
  43. for (i = 0; i < sizeof_array(catch_signals); i++)
  44. if (sigaction(catch_signals[i], &catchsig, &uncatch_signals[i]))
  45. ohshite(_("unable to ignore signal %s before running %.250s"),
  46. strsignal(catch_signals[i]), name);
  47. push_cleanup(subproc_signals_cleanup, ~0, NULL, 0, 0);
  48. onerr_abort--;
  49. }
  50. void
  51. subproc_signals_cleanup(int argc, void **argv)
  52. {
  53. size_t i;
  54. for (i = 0; i < sizeof_array(catch_signals); i++) {
  55. if (sigaction(catch_signals[i], &uncatch_signals[i], NULL)) {
  56. fprintf(stderr, _("error un-catching signal %s: %s\n"),
  57. strsignal(catch_signals[i]), strerror(errno));
  58. onerr_abort++;
  59. }
  60. }
  61. }
  62. int
  63. subproc_check(int status, const char *desc, int flags)
  64. {
  65. int n;
  66. if (WIFEXITED(status)) {
  67. n = WEXITSTATUS(status);
  68. if (!n)
  69. return 0;
  70. if (flags & PROCNOERR)
  71. return n;
  72. if (flags & PROCWARN)
  73. warning(_("%s returned error exit status %d"),
  74. desc, n);
  75. else
  76. ohshit(_("subprocess %s returned error exit status %d"),
  77. desc, n);
  78. } else if (WIFSIGNALED(status)) {
  79. n = WTERMSIG(status);
  80. if (!n)
  81. return 0;
  82. if ((flags & PROCPIPE) && n == SIGPIPE)
  83. return 0;
  84. if (flags & PROCWARN)
  85. warning(_("%s killed by signal (%s)%s"),
  86. desc, strsignal(n),
  87. WCOREDUMP(status) ? _(", core dumped") : "");
  88. else
  89. ohshit(_("subprocess %s killed by signal (%s)%s"),
  90. desc, strsignal(n),
  91. WCOREDUMP(status) ? _(", core dumped") : "");
  92. } else {
  93. ohshit(_("subprocess %s failed with wait status code %d"),
  94. desc, status);
  95. }
  96. return -1;
  97. }
  98. int
  99. subproc_wait(pid_t pid, const char *desc)
  100. {
  101. pid_t r;
  102. int status;
  103. while ((r = waitpid(pid, &status, 0)) == -1 && errno == EINTR) ;
  104. if (r != pid) {
  105. onerr_abort++;
  106. ohshite(_("wait for %s failed"), desc);
  107. }
  108. return status;
  109. }
  110. int
  111. subproc_wait_check(pid_t pid, const char *desc, int flags)
  112. {
  113. int status;
  114. status = subproc_wait(pid, desc);
  115. return subproc_check(status, desc, flags);
  116. }