mlib.c 9.9 KB

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