mlib.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include <dpkg-db.h>
  32. /* Incremented when we do some kind of generally necessary operation, so that
  33. * loops &c know to quit if we take an error exit. Decremented again afterwards.
  34. */
  35. volatile int onerr_abort= 0;
  36. void *m_malloc(size_t amount) {
  37. #ifdef MDEBUG
  38. unsigned short *r2, x;
  39. #endif
  40. void *r;
  41. onerr_abort++;
  42. r= malloc(amount);
  43. if (!r) ohshite(_("malloc failed (%ld bytes)"),(long)amount);
  44. onerr_abort--;
  45. #ifdef MDEBUG
  46. r2= r; x= (unsigned short)amount ^ 0xf000;
  47. while (amount >= 2) { *r2++= x; amount -= 2; }
  48. #endif
  49. return r;
  50. }
  51. void *m_realloc(void *r, size_t amount) {
  52. onerr_abort++;
  53. r= realloc(r,amount);
  54. if (!r) ohshite(_("realloc failed (%ld bytes)"),(long)amount);
  55. onerr_abort--;
  56. return r;
  57. }
  58. static void print_error_forked(const char *emsg, const char *contextstring) {
  59. fprintf(stderr, _("%s (subprocess): %s\n"), thisname, emsg);
  60. }
  61. static void cu_m_fork(int argc, void **argv) {
  62. exit(2);
  63. /* Don't do the other cleanups, because they'll be done by/in the parent
  64. * process.
  65. */
  66. }
  67. int m_fork(void) {
  68. pid_t r;
  69. r= fork();
  70. if (r == -1) { onerr_abort++; ohshite(_("fork failed")); }
  71. if (r > 0) return r;
  72. push_cleanup(cu_m_fork,~0, 0,0, 0);
  73. set_error_display(print_error_forked,0);
  74. return r;
  75. }
  76. void m_dup2(int oldfd, int newfd) {
  77. const char *const stdstrings[]= { "in", "out", "err" };
  78. if (dup2(oldfd,newfd) == newfd) return;
  79. onerr_abort++;
  80. if (newfd < 3) ohshite(_("failed to dup for std%s"),stdstrings[newfd]);
  81. ohshite(_("failed to dup for fd %d"),newfd);
  82. }
  83. void m_pipe(int *fds) {
  84. if (!pipe(fds)) return;
  85. onerr_abort++;
  86. ohshite(_("failed to create pipe"));
  87. }
  88. void checksubprocerr(int status, const char *description, int sigpipeok) {
  89. int n;
  90. if (WIFEXITED(status)) {
  91. n= WEXITSTATUS(status); if (!n) return;
  92. ohshit(_("subprocess %s returned error exit status %d"),description,n);
  93. } else if (WIFSIGNALED(status)) {
  94. n= WTERMSIG(status); if (!n || (sigpipeok && n==SIGPIPE)) return;
  95. ohshit(_("subprocess %s killed by signal (%s)%s"),
  96. description, strsignal(n), WCOREDUMP(status) ? ", core dumped" : "");
  97. } else {
  98. ohshit(_("subprocess %s failed with wait status code %d"),description,status);
  99. }
  100. }
  101. void waitsubproc(pid_t pid, const char *description, int sigpipeok) {
  102. pid_t r;
  103. int status;
  104. while ((r= waitpid(pid,&status,0)) == -1 && errno == EINTR);
  105. if (r != pid) { onerr_abort++; ohshite(_("wait for %s failed"),description); }
  106. checksubprocerr(status,description,sigpipeok);
  107. }
  108. typedef struct do_fd_copy_data {
  109. int fd;
  110. } do_fd_copy_data_t;
  111. typedef struct do_fd_buf_data {
  112. void *buf;
  113. int type;
  114. } do_fd_buf_data_t;
  115. void do_fd_write_combined(char *buf, int length, void *proc_data, char *desc) {
  116. do_fd_buf_data_t *data = (do_fd_buf_data_t *)proc_data;
  117. switch(data->type) {
  118. case FD_WRITE_BUF:
  119. memcpy(data->buf, buf, length);
  120. data->buf += length;
  121. break;
  122. case FD_WRITE_VBUF:
  123. varbufaddbuf((struct varbuf *)data->buf, buf, length);
  124. data->buf += length;
  125. break;
  126. case FD_WRITE_FD:
  127. if(write((int)data->buf, buf, length) < length)
  128. ohshite(_("failed in do_fd_write_combined (%i, %s)"), FD_WRITE_FD, desc);
  129. break;
  130. default:
  131. fprintf(stderr, _("unknown data type `%i' in do_fd_write_buf\n"), data->type);
  132. }
  133. }
  134. int read_fd_combined(int fd, void *buf, int type, int limit, char *desc, ...) {
  135. do_fd_buf_data_t data = { buf, type };
  136. va_list al;
  137. struct varbuf v;
  138. varbufinit(&v);
  139. va_start(al,desc);
  140. varbufvprintf(&v, desc, al);
  141. va_end(al);
  142. do_fd_read(fd, limit, do_fd_write_combined, &data, v.buf);
  143. varbuffree(&v);
  144. }
  145. int do_fd_read(int fd1, int limit, do_fd_write_t write_proc, void *proc_data, char *desc) {
  146. char *buf;
  147. int count, bufsize= 32768, bytesread= 0;
  148. if((limit != -1) && (limit < bufsize)) bufsize= limit;
  149. buf= malloc(bufsize);
  150. if(buf== NULL) ohshite(_("failed to allocate buffer in do_fd_read (%s)"), desc);
  151. while(1) {
  152. count= read(fd1, buf, bufsize);
  153. if (count<0) {
  154. if (errno==EINTR) continue;
  155. break;
  156. }
  157. if (count==0)
  158. break;
  159. bytesread+= count;
  160. write_proc(buf, count, proc_data, desc);
  161. if(limit!=-1) {
  162. limit-= count;
  163. if(limit<bufsize)
  164. bufsize=limit;
  165. }
  166. }
  167. if (count<0) ohshite(_("failed in do_fd_read on read (%s)"), desc);
  168. free(buf);
  169. }