subproc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <dpkg-i18n.h>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <signal.h>
  30. #include <dpkg.h>
  31. #include <dpkg-priv.h>
  32. static int catch_signals[] = { SIGQUIT, SIGINT };
  33. static struct sigaction uncatch_signals[sizeof_array(catch_signals)];
  34. void
  35. setup_subproc_signals(const char *name)
  36. {
  37. size_t i;
  38. struct sigaction catchsig;
  39. onerr_abort++;
  40. memset(&catchsig, 0, sizeof(catchsig));
  41. catchsig.sa_handler = SIG_IGN;
  42. sigemptyset(&catchsig.sa_mask);
  43. catchsig.sa_flags = 0;
  44. for (i = 0; i < sizeof_array(catch_signals); i++)
  45. if (sigaction(catch_signals[i], &catchsig, &uncatch_signals[i]))
  46. ohshite(_("unable to ignore signal %s before running %.250s"),
  47. strsignal(catch_signals[i]), name);
  48. push_cleanup(cu_subproc_signals, ~0, NULL, 0, 0);
  49. onerr_abort--;
  50. }
  51. void
  52. cu_subproc_signals(int argc, void **argv)
  53. {
  54. size_t i;
  55. for (i = 0; i < sizeof_array(catch_signals); i++) {
  56. if (sigaction(catch_signals[i], &uncatch_signals[i], NULL)) {
  57. fprintf(stderr, _("error un-catching signal %s: %s\n"),
  58. strsignal(catch_signals[i]), strerror(errno));
  59. onerr_abort++;
  60. }
  61. }
  62. }
  63. int
  64. checksubprocerr(int status, const char *description, int flags)
  65. {
  66. int n;
  67. if (WIFEXITED(status)) {
  68. n = WEXITSTATUS(status);
  69. if (!n)
  70. return 0;
  71. if (flags & PROCNOERR)
  72. return -1;
  73. if (flags & PROCWARN)
  74. warning(_("%s returned error exit status %d"),
  75. description, n);
  76. else
  77. ohshit(_("subprocess %s returned error exit status %d"),
  78. description, n);
  79. } else if (WIFSIGNALED(status)) {
  80. n = WTERMSIG(status);
  81. if (!n)
  82. return 0;
  83. if ((flags & PROCPIPE) && n == SIGPIPE)
  84. return 0;
  85. if (flags & PROCWARN)
  86. warning(_("%s killed by signal (%s)%s"),
  87. description, strsignal(n),
  88. WCOREDUMP(status) ? _(", core dumped") : "");
  89. else
  90. ohshit(_("subprocess %s killed by signal (%s)%s"),
  91. description, strsignal(n),
  92. WCOREDUMP(status) ? _(", core dumped") : "");
  93. } else {
  94. ohshit(_("subprocess %s failed with wait status code %d"),
  95. description, status);
  96. }
  97. return -1;
  98. }
  99. int
  100. waitsubproc(pid_t pid, const char *description, int flags)
  101. {
  102. pid_t r;
  103. int status;
  104. while ((r = waitpid(pid, &status, 0)) == -1 && errno == EINTR) ;
  105. if (r != pid) {
  106. onerr_abort++;
  107. ohshite(_("wait for %s failed"), description);
  108. }
  109. return checksubprocerr(status, description, flags);
  110. }