mlib.c 10 KB

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