mlib.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * mlib.c - `must' library: routines will succeed or longjmp
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.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 <unistd.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <signal.h>
  26. #include <errno.h>
  27. #include <sys/wait.h>
  28. #include <sys/types.h>
  29. #include <config.h>
  30. #include <dpkg.h>
  31. /* Incremented when we do some kind of generally necessary operation, so that
  32. * loops &c know to quit if we take an error exit. Decremented again afterwards.
  33. */
  34. volatile int onerr_abort= 0;
  35. void *m_malloc(size_t amount) {
  36. #ifdef MDEBUG
  37. unsigned short *r2, x;
  38. #endif
  39. void *r;
  40. onerr_abort++;
  41. r= malloc(amount);
  42. if (!r) ohshite(_("malloc failed (%ld bytes)"),(long)amount);
  43. onerr_abort--;
  44. #ifdef MDEBUG
  45. r2= r; x= (unsigned short)amount ^ 0xf000;
  46. while (amount >= 2) { *r2++= x; amount -= 2; }
  47. #endif
  48. return r;
  49. }
  50. void *m_realloc(void *r, size_t amount) {
  51. onerr_abort++;
  52. r= realloc(r,amount);
  53. if (!r) ohshite(_("realloc failed (%ld bytes)"),(long)amount);
  54. onerr_abort--;
  55. return r;
  56. }
  57. static void print_error_forked(const char *emsg, const char *contextstring) {
  58. fprintf(stderr, _("%s (subprocess): %s\n"), thisname, emsg);
  59. }
  60. static void cu_m_fork(int argc, void **argv) {
  61. exit(2);
  62. /* Don't do the other cleanups, because they'll be done by/in the parent
  63. * process.
  64. */
  65. }
  66. int m_fork(void) {
  67. pid_t r;
  68. r= fork();
  69. if (r == -1) { onerr_abort++; ohshite(_("fork failed")); }
  70. if (r > 0) return r;
  71. push_cleanup(cu_m_fork,~0, 0,0, 0);
  72. set_error_display(print_error_forked,0);
  73. return r;
  74. }
  75. void m_dup2(int oldfd, int newfd) {
  76. const char *const stdstrings[]= { "in", "out", "err" };
  77. if (dup2(oldfd,newfd) == newfd) return;
  78. onerr_abort++;
  79. if (newfd < 3) ohshite(_("failed to dup for std%s"),stdstrings[newfd]);
  80. ohshite(_("failed to dup for fd %d"),newfd);
  81. }
  82. void m_pipe(int *fds) {
  83. if (!pipe(fds)) return;
  84. onerr_abort++;
  85. ohshite(_("failed to create pipe"));
  86. }
  87. void checksubprocerr(int status, const char *description, int sigpipeok) {
  88. int n;
  89. if (WIFEXITED(status)) {
  90. n= WEXITSTATUS(status); if (!n) return;
  91. ohshit(_("subprocess %s returned error exit status %d"),description,n);
  92. } else if (WIFSIGNALED(status)) {
  93. n= WTERMSIG(status); if (!n || (sigpipeok && n==SIGPIPE)) return;
  94. ohshit(_("subprocess %s killed by signal (%s)%s"),
  95. description, strsignal(n), WCOREDUMP(status) ? ", core dumped" : "");
  96. } else {
  97. ohshit(_("subprocess %s failed with wait status code %d"),description,status);
  98. }
  99. }
  100. void waitsubproc(pid_t pid, const char *description, int sigpipeok) {
  101. pid_t r;
  102. int status;
  103. while ((r= waitpid(pid,&status,0)) == -1 && errno == EINTR);
  104. if (r != pid) { onerr_abort++; ohshite(_("wait for %s failed"),description); }
  105. checksubprocerr(status,description,sigpipeok);
  106. }
  107. int do_fd_copy(int fd1, int fd2, char *desc) {
  108. char *buf, *sbuf;
  109. int count;
  110. char *er_msg_1 = _("failed to allocate buffer for copy (%s)");
  111. char *er_msg_2 = _("failed in copy on write (%s)");
  112. char *er_msg_3 = _("failed in copy on read (%s)");
  113. count = strlen(er_msg_1) + strlen(desc) + 1;
  114. sbuf = malloc(count);
  115. if(sbuf == NULL)
  116. ohshite(_("failed to allocate buffer for snprintf 1"));
  117. snprintf(sbuf, count, er_msg_1, desc);
  118. sbuf[count-1] = 0;
  119. buf = malloc(32768);
  120. if(buf == NULL)
  121. ohshite(sbuf);
  122. free(sbuf);
  123. count = strlen(er_msg_2) + strlen(desc) + 1;
  124. sbuf = malloc(count);
  125. if(sbuf == NULL)
  126. ohshite(_("failed to allocate buffer for snprintf 2"));
  127. snprintf(sbuf, count, er_msg_2, desc);
  128. sbuf[count-1] = 0;
  129. while((count = read(fd1, buf, 32768)) > 0)
  130. if(write(fd2, buf, count) < count)
  131. ohshite(sbuf);
  132. free(sbuf);
  133. count = strlen(er_msg_3) + strlen(desc) + 1;
  134. sbuf = malloc(count);
  135. if(sbuf == NULL)
  136. ohshite(_("failed to allocate buffer for snprintf 2"));
  137. snprintf(sbuf, count, er_msg_3, desc);
  138. sbuf[count-1] = 0;
  139. if(count < 0)
  140. ohshite(_("failed in copy on read (control)"));
  141. free(sbuf);
  142. free(buf);
  143. }