mlib.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include <md5.h>
  33. /* Incremented when we do some kind of generally necessary operation, so that
  34. * loops &c know to quit if we take an error exit. Decremented again afterwards.
  35. */
  36. volatile int onerr_abort= 0;
  37. void *m_malloc(size_t amount) {
  38. #ifdef MDEBUG
  39. unsigned short *r2, x;
  40. #endif
  41. void *r;
  42. onerr_abort++;
  43. r= malloc(amount);
  44. if (!r) ohshite(_("malloc failed (%ld bytes)"),(long)amount);
  45. onerr_abort--;
  46. #ifdef MDEBUG
  47. r2= r; x= (unsigned short)amount ^ 0xf000;
  48. while (amount >= 2) { *r2++= x; amount -= 2; }
  49. #endif
  50. return r;
  51. }
  52. void *m_realloc(void *r, size_t amount) {
  53. onerr_abort++;
  54. r= realloc(r,amount);
  55. if (!r) ohshite(_("realloc failed (%ld bytes)"),(long)amount);
  56. onerr_abort--;
  57. return r;
  58. }
  59. static void print_error_forked(const char *emsg, const char *contextstring) {
  60. fprintf(stderr, _("%s (subprocess): %s\n"), thisname, emsg);
  61. }
  62. static void cu_m_fork(int argc, void **argv) NONRETURNING;
  63. static void cu_m_fork(int argc, void **argv) {
  64. exit(2);
  65. /* Don't do the other cleanups, because they'll be done by/in the parent
  66. * process.
  67. */
  68. }
  69. int m_fork(void) {
  70. pid_t r;
  71. r= fork();
  72. if (r == -1) { onerr_abort++; ohshite(_("fork failed")); }
  73. if (r > 0) return r;
  74. push_cleanup(cu_m_fork,~0, 0,0, 0);
  75. set_error_display(print_error_forked,0);
  76. return r;
  77. }
  78. void m_dup2(int oldfd, int newfd) {
  79. const char *const stdstrings[]= { "in", "out", "err" };
  80. if (dup2(oldfd,newfd) == newfd) return;
  81. onerr_abort++;
  82. if (newfd < 3) ohshite(_("failed to dup for std%s"),stdstrings[newfd]);
  83. ohshite(_("failed to dup for fd %d"),newfd);
  84. }
  85. void m_pipe(int *fds) {
  86. if (!pipe(fds)) return;
  87. onerr_abort++;
  88. ohshite(_("failed to create pipe"));
  89. }
  90. int checksubprocerr(int status, const char *description, int sigpipeok, int warn) {
  91. int n;
  92. if (WIFEXITED(status)) {
  93. n= WEXITSTATUS(status); if (!n) return n;
  94. if(warn)
  95. fprintf(stderr, _("dpkg: warning - %s returned error exit status %d\n"),description,n);
  96. else
  97. ohshit(_("subprocess %s returned error exit status %d"),description,n);
  98. } else if (WIFSIGNALED(status)) {
  99. n= WTERMSIG(status); if (!n || (sigpipeok && n==SIGPIPE)) return 0;
  100. if (warn)
  101. ohshit(_("dpkg: warning - %s killed by signal (%s)%s\n"),
  102. description, strsignal(n), WCOREDUMP(status) ? ", core dumped" : "");
  103. else
  104. ohshit(_("subprocess %s killed by signal (%s)%s"),
  105. description, strsignal(n), WCOREDUMP(status) ? ", core dumped" : "");
  106. } else {
  107. ohshit(_("subprocess %s failed with wait status code %d"),description,status);
  108. }
  109. return -1;
  110. }
  111. int waitsubproc(pid_t pid, const char *description, int sigpipeok, int warn) {
  112. pid_t r;
  113. int status;
  114. while ((r= waitpid(pid,&status,0)) == -1 && errno == EINTR);
  115. if (r != pid) { onerr_abort++; ohshite(_("wait for %s failed"),description); }
  116. return checksubprocerr(status,description,sigpipeok, warn);
  117. }
  118. ssize_t buffer_write(buffer_data_t data, void *buf, ssize_t length, const char *desc) {
  119. ssize_t ret= length;
  120. if(data->type & BUFFER_WRITE_SETUP) {
  121. switch(data->type ^ BUFFER_WRITE_SETUP) {
  122. case BUFFER_WRITE_MD5:
  123. {
  124. unsigned char *hash= data->data.ptr;
  125. void ** array= malloc(sizeof(void *) * 2);
  126. data->data.ptr= array;
  127. array[0]= hash;
  128. array[1]= malloc(sizeof(struct MD5Context));
  129. MD5Init((struct MD5Context *)array[1]);
  130. }
  131. break;
  132. }
  133. return 0;
  134. }
  135. if(data->type & BUFFER_WRITE_SHUTDOWN) {
  136. switch(data->type ^ BUFFER_WRITE_SHUTDOWN) {
  137. case BUFFER_WRITE_MD5:
  138. {
  139. int i;
  140. unsigned char digest[16], *p = digest;
  141. unsigned char *hash= (unsigned char *)((void **)data->data.ptr)[0];
  142. MD5Final(digest, (struct MD5Context *)((void **)data->data.ptr)[1]);
  143. for (i = 0; i < 16; ++i) {
  144. sprintf(hash, "%02x", *p++);
  145. hash += 2;
  146. }
  147. }
  148. break;
  149. }
  150. return 0;
  151. }
  152. switch(data->type) {
  153. case BUFFER_WRITE_BUF:
  154. memcpy(data->data.ptr, buf, length);
  155. (char*)data->data.ptr += length;
  156. break;
  157. case BUFFER_WRITE_VBUF:
  158. varbufaddbuf((struct varbuf *)data->data.ptr, buf, length);
  159. break;
  160. case BUFFER_WRITE_FD:
  161. if((ret= write(data->data.i, buf, length)) < 0 && errno != EINTR)
  162. ohshite(_("failed in buffer_write(fd) (%i, ret=%zi %s)"), data->data.i, ret, desc);
  163. break;
  164. case BUFFER_WRITE_NULL:
  165. break;
  166. case BUFFER_WRITE_STREAM:
  167. ret= fwrite(buf, 1, length, (FILE *)data->data.ptr);
  168. if(feof((FILE *)data->data.ptr))
  169. ohshite(_("eof in buffer_write(stream): %s"), desc);
  170. if(ferror((FILE *)data->data.ptr))
  171. ohshite(_("error in buffer_write(stream): %s"), desc);
  172. break;
  173. case BUFFER_WRITE_MD5:
  174. MD5Update((struct MD5Context *)((void **)data->data.ptr)[1], buf, length);
  175. break;
  176. default:
  177. fprintf(stderr, _("unknown data type `%i' in buffer_write\n"), data->type);
  178. }
  179. return ret;
  180. }
  181. ssize_t buffer_read(buffer_data_t data, void *buf, ssize_t length, const char *desc) {
  182. ssize_t ret= length;
  183. if(data->type & BUFFER_READ_SETUP) {
  184. return 0;
  185. }
  186. if(data->type & BUFFER_READ_SHUTDOWN) {
  187. return 0;
  188. }
  189. switch(data->type) {
  190. case BUFFER_READ_FD:
  191. if((ret= read(data->data.i, buf, length)) < 0 && errno != EINTR)
  192. ohshite(_("failed in buffer_read(fd): %s"), desc);
  193. break;
  194. case BUFFER_READ_STREAM:
  195. ret= fread(buf, 1, length, (FILE *)data->data.ptr);
  196. if(feof((FILE *)data->data.ptr))
  197. return ret;
  198. if(ferror((FILE *)data->data.ptr))
  199. ohshite(_("error in buffer_read(stream): %s"), desc);
  200. break;
  201. default:
  202. fprintf(stderr, _("unknown data type `%i' in buffer_read\n"), data->type);
  203. }
  204. return ret;
  205. }
  206. #define buffer_copy_setup_dual(name, type1, name1, type2, name2) \
  207. inline ssize_t buffer_copy_setup_##name(type1 n1, int typeIn, void *procIn,\
  208. type2 n2, int typeOut, void *procOut,\
  209. ssize_t limit, const char *desc, ...)\
  210. {\
  211. va_list al;\
  212. buffer_arg a1, a2;\
  213. struct varbuf v;\
  214. ssize_t ret;\
  215. a1.name1 = n1; a2.name2 = n2;\
  216. varbufinit(&v);\
  217. va_start(al,desc);\
  218. varbufvprintf(&v, desc, al);\
  219. va_end(al);\
  220. ret = buffer_copy_setup(a1, typeIn, procIn,\
  221. a2, typeOut, procOut,\
  222. limit, v.buf);\
  223. varbuffree(&v);\
  224. return ret;\
  225. }
  226. buffer_copy_setup_dual(IntInt, int, i, int, i);
  227. buffer_copy_setup_dual(IntPtr, int, i, void *, ptr);
  228. buffer_copy_setup_dual(PtrInt, void *, ptr, int, i);
  229. buffer_copy_setup_dual(PtrPtr, void *, ptr, void *, ptr);
  230. ssize_t buffer_copy_setup(buffer_arg argIn, int typeIn, void *procIn,
  231. buffer_arg argOut, int typeOut, void *procOut,
  232. ssize_t limit, const char *desc)
  233. {
  234. struct buffer_data read_data = { procIn, argIn, typeIn },
  235. write_data = { procOut, argOut, typeOut };
  236. ssize_t ret;
  237. if ( procIn == NULL )
  238. read_data.proc = buffer_read;
  239. if ( procOut == NULL )
  240. write_data.proc = buffer_write;
  241. read_data.type |= BUFFER_READ_SETUP;
  242. read_data.proc(&read_data, NULL, 0, desc);
  243. read_data.type = typeIn;
  244. write_data.type |= BUFFER_WRITE_SETUP;
  245. write_data.proc(&write_data, NULL, 0, desc);
  246. write_data.type = typeOut;
  247. ret = buffer_copy(&read_data, &write_data, limit, desc);
  248. write_data.type |= BUFFER_WRITE_SHUTDOWN;
  249. write_data.proc(&write_data, NULL, 0, desc);
  250. read_data.type |= BUFFER_READ_SHUTDOWN;
  251. read_data.proc(&read_data, NULL, 0, desc);
  252. return ret;
  253. }
  254. ssize_t buffer_copy(buffer_data_t read_data, buffer_data_t write_data, ssize_t limit, const char *desc) {
  255. char *buf, *writebuf;
  256. long bytesread= 0, byteswritten= 0;
  257. int bufsize= 32768;
  258. ssize_t totalread= 0, totalwritten= 0;
  259. if((limit != -1) && (limit < bufsize)) bufsize= limit;
  260. if(bufsize == 0)
  261. return 0;
  262. writebuf= buf= malloc(bufsize);
  263. if(buf== NULL) ohshite(_("failed to allocate buffer in buffer_copy (%s)"), desc);
  264. while(bytesread >= 0 && byteswritten >= 0) {
  265. bytesread= read_data->proc(read_data, buf, bufsize, desc);
  266. if (bytesread<0) {
  267. if (errno==EINTR) continue;
  268. break;
  269. }
  270. if (bytesread==0)
  271. break;
  272. totalread+= bytesread;
  273. if(limit!=-1) {
  274. limit-= bytesread;
  275. if(limit<bufsize)
  276. bufsize=limit;
  277. }
  278. writebuf= buf;
  279. while(bytesread) {
  280. byteswritten= write_data->proc(write_data, writebuf, bytesread, desc);
  281. if(byteswritten == -1) {
  282. if(errno == EINTR) continue;
  283. break;
  284. }
  285. if(byteswritten==0)
  286. break;
  287. bytesread-= byteswritten;
  288. totalwritten+= byteswritten;
  289. writebuf+= byteswritten;
  290. }
  291. }
  292. if (bytesread<0 || byteswritten<0) ohshite(_("failed in buffer_copy (%s)"), desc);
  293. free(buf);
  294. return totalread;
  295. }