mlib.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * mlib.c - `must' library: routines will succeed or longjmp
  4. *
  5. * Copyright © 1994,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 <errno.h>
  24. #include <string.h>
  25. #include <fcntl.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/dpkg-db.h>
  33. void *m_malloc(size_t amount) {
  34. #ifdef MDEBUG
  35. unsigned short *r2, x;
  36. #endif
  37. void *r;
  38. onerr_abort++;
  39. r= malloc(amount);
  40. if (!r) ohshite(_("malloc failed (%ld bytes)"),(long)amount);
  41. onerr_abort--;
  42. #ifdef MDEBUG
  43. r2= r; x= (unsigned short)amount ^ 0xf000;
  44. while (amount >= 2) { *r2++= x; amount -= 2; }
  45. #endif
  46. return r;
  47. }
  48. void *m_realloc(void *r, size_t amount) {
  49. onerr_abort++;
  50. r= realloc(r,amount);
  51. if (!r) ohshite(_("realloc failed (%ld bytes)"),(long)amount);
  52. onerr_abort--;
  53. return r;
  54. }
  55. char *
  56. m_strdup(const char *str)
  57. {
  58. char *new_str;
  59. onerr_abort++;
  60. new_str = strdup(str);
  61. if (!new_str)
  62. ohshite(_("failed to allocate memory"));
  63. onerr_abort--;
  64. return new_str;
  65. }
  66. static void print_error_forked(const char *emsg, const char *contextstring) {
  67. fprintf(stderr, _("%s (subprocess): %s\n"), thisname, emsg);
  68. }
  69. static void cu_m_fork(int argc, void **argv) DPKG_ATTR_NORET;
  70. static void cu_m_fork(int argc, void **argv) {
  71. exit(2);
  72. /* Don't do the other cleanups, because they'll be done by/in the parent
  73. * process.
  74. */
  75. }
  76. int m_fork(void) {
  77. pid_t r;
  78. r= fork();
  79. if (r == -1) { onerr_abort++; ohshite(_("fork failed")); }
  80. if (r > 0) return r;
  81. push_cleanup(cu_m_fork,~0, NULL,0, 0);
  82. set_error_display(print_error_forked,NULL);
  83. return r;
  84. }
  85. void m_dup2(int oldfd, int newfd) {
  86. const char *const stdstrings[]= { "in", "out", "err" };
  87. if (dup2(oldfd,newfd) == newfd) return;
  88. onerr_abort++;
  89. if (newfd < 3) ohshite(_("failed to dup for std%s"),stdstrings[newfd]);
  90. ohshite(_("failed to dup for fd %d"),newfd);
  91. }
  92. void m_pipe(int *fds) {
  93. if (!pipe(fds)) return;
  94. onerr_abort++;
  95. ohshite(_("failed to create pipe"));
  96. }
  97. void
  98. m_output(FILE *f, const char *name)
  99. {
  100. fflush(f);
  101. if (ferror(f))
  102. ohshite(_("error writing to '%s'"), name);
  103. }
  104. void setcloexec(int fd, const char* fn) {
  105. int f;
  106. if ((f=fcntl(fd, F_GETFD))==-1)
  107. ohshite(_("unable to read filedescriptor flags for %.250s"),fn);
  108. if (fcntl(fd, F_SETFD, (f|FD_CLOEXEC))==-1)
  109. ohshite(_("unable to set close-on-exec flag for %.250s"),fn);
  110. }