ehandle.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * ehandle.c - error handling
  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 <assert.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <stdarg.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <dpkg/macros.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/dpkg.h>
  32. #include <dpkg/dpkg-db.h>
  33. static const char *errmsg; /* points to errmsgbuf or malloc'd */
  34. static char errmsgbuf[4096];
  35. /* 6x255 for inserted strings (%.255s &c in fmt; also %s with limited length arg)
  36. * 1x255 for constant text
  37. * 1x255 for strerror()
  38. * same again just in case.
  39. */
  40. /* Incremented when we do some kind of generally necessary operation,
  41. * so that loops &c know to quit if we take an error exit. Decremented
  42. * again afterwards.
  43. */
  44. volatile int onerr_abort = 0;
  45. #define NCALLS 2
  46. struct cleanupentry {
  47. struct cleanupentry *next;
  48. struct {
  49. int mask;
  50. void (*call)(int argc, void **argv);
  51. } calls[NCALLS];
  52. int cpmask, cpvalue;
  53. int argc;
  54. void *argv[1];
  55. };
  56. struct errorcontext {
  57. struct errorcontext *next;
  58. jmp_buf *jbufp;
  59. struct cleanupentry *cleanups;
  60. void (*printerror)(const char *emsg, const char *contextstring);
  61. const char *contextstring;
  62. };
  63. static struct errorcontext *volatile econtext= NULL;
  64. static struct { struct cleanupentry ce; void *args[20]; } emergency;
  65. void set_error_display(error_printer *printerror,
  66. const char *contextstring) {
  67. assert(econtext);
  68. econtext->printerror= printerror;
  69. econtext->contextstring= contextstring;
  70. }
  71. static void run_error_handler(void) DPKG_ATTR_NORET;
  72. static void
  73. run_error_handler(void)
  74. {
  75. if (onerr_abort) {
  76. /* We arrived here due to a fatal error from which we cannot recover,
  77. * and trying to do so would most probably get us here again. That's
  78. * why we will not try to do any error unwinding either. We'll just
  79. * abort. Hopefully the user can fix the situation (out of disk, out
  80. * of memory, etc).
  81. */
  82. fprintf(stderr, _("%s: unrecoverable fatal error, aborting:\n %s\n"),
  83. thisname, errmsg);
  84. exit(2);
  85. } else {
  86. longjmp(*econtext->jbufp, 1);
  87. }
  88. }
  89. void push_error_handler(jmp_buf *jbufp,
  90. error_printer *printerror,
  91. const char *contextstring) {
  92. struct errorcontext *necp;
  93. necp= malloc(sizeof(struct errorcontext));
  94. if (!necp) {
  95. int e= errno;
  96. snprintf(errmsgbuf, sizeof(errmsgbuf), "%s%s",
  97. _("out of memory pushing error handler: "), strerror(e));
  98. errmsg= errmsgbuf;
  99. if (econtext)
  100. run_error_handler();
  101. fprintf(stderr, "%s: %s\n", thisname, errmsgbuf); exit(2);
  102. }
  103. necp->next= econtext;
  104. necp->jbufp= jbufp;
  105. necp->cleanups= NULL;
  106. necp->printerror= printerror;
  107. necp->contextstring= contextstring;
  108. econtext= necp;
  109. onerr_abort= 0;
  110. }
  111. static void print_error_cleanup(const char *emsg, const char *contextstring) {
  112. fprintf(stderr, _("%s: error while cleaning up:\n %s\n"),thisname,emsg);
  113. }
  114. static void run_cleanups(struct errorcontext *econ, int flagsetin) {
  115. static volatile int preventrecurse= 0;
  116. struct cleanupentry *volatile cep;
  117. struct cleanupentry *ncep;
  118. struct errorcontext recurserr, *oldecontext;
  119. jmp_buf recurejbuf;
  120. volatile int i, flagset;
  121. if (econ->printerror) econ->printerror(errmsg,econ->contextstring);
  122. if (++preventrecurse > 3) {
  123. onerr_abort++;
  124. fprintf(stderr, _("dpkg: too many nested errors during error recovery !!\n"));
  125. flagset= 0;
  126. } else {
  127. flagset= flagsetin;
  128. }
  129. cep= econ->cleanups;
  130. oldecontext= econtext;
  131. while (cep) {
  132. for (i=0; i<NCALLS; i++) {
  133. if (cep->calls[i].call && cep->calls[i].mask & flagset) {
  134. if (setjmp(recurejbuf)) {
  135. run_cleanups(&recurserr, ehflag_bombout | ehflag_recursiveerror);
  136. } else {
  137. recurserr.jbufp= &recurejbuf;
  138. recurserr.cleanups= NULL;
  139. recurserr.next= NULL;
  140. recurserr.printerror= print_error_cleanup;
  141. recurserr.contextstring= NULL;
  142. econtext= &recurserr;
  143. cep->calls[i].call(cep->argc,cep->argv);
  144. }
  145. econtext= oldecontext;
  146. }
  147. }
  148. flagset &= cep->cpmask;
  149. flagset |= cep->cpvalue;
  150. ncep= cep->next;
  151. if (cep != &emergency.ce) free(cep);
  152. cep= ncep;
  153. }
  154. preventrecurse--;
  155. }
  156. void error_unwind(int flagset) {
  157. struct errorcontext *tecp;
  158. tecp= econtext;
  159. econtext= tecp->next;
  160. run_cleanups(tecp,flagset);
  161. free(tecp);
  162. }
  163. void push_checkpoint(int mask, int value) {
  164. /* This will arrange that when error_unwind() is called,
  165. * all previous cleanups will be executed with
  166. * flagset= (original_flagset & mask) | value
  167. * where original_flagset is the argument to error_unwind
  168. * (as modified by any checkpoint which was pushed later).
  169. */
  170. struct cleanupentry *cep;
  171. int i;
  172. cep= m_malloc(sizeof(struct cleanupentry) + sizeof(char*));
  173. for (i=0; i<NCALLS; i++) { cep->calls[i].call=NULL; cep->calls[i].mask=0; }
  174. cep->cpmask= mask; cep->cpvalue= value;
  175. cep->argc= 0; cep->argv[0]= NULL;
  176. cep->next= econtext->cleanups;
  177. econtext->cleanups= cep;
  178. }
  179. void push_cleanup(void (*call1)(int argc, void **argv), int mask1,
  180. void (*call2)(int argc, void **argv), int mask2,
  181. unsigned int nargs, ...) {
  182. struct cleanupentry *cep;
  183. void **args;
  184. int e;
  185. va_list al;
  186. onerr_abort++;
  187. cep= malloc(sizeof(struct cleanupentry) + sizeof(char*)*(nargs+1));
  188. if (!cep) {
  189. if (nargs > sizeof_array(emergency.args))
  190. ohshite(_("out of memory for new cleanup entry with many arguments"));
  191. e= errno; cep= &emergency.ce;
  192. }
  193. cep->calls[0].call= call1; cep->calls[0].mask= mask1;
  194. cep->calls[1].call= call2; cep->calls[1].mask= mask2;
  195. cep->cpmask=~0; cep->cpvalue=0; cep->argc= nargs;
  196. va_start(al,nargs);
  197. args= cep->argv; while (nargs-- >0) *args++= va_arg(al,void*);
  198. *args++= NULL;
  199. va_end(al);
  200. cep->next= econtext->cleanups;
  201. econtext->cleanups= cep;
  202. if (cep == &emergency.ce) { e= errno; ohshite(_("out of memory for new cleanup entry")); }
  203. onerr_abort--;
  204. }
  205. void pop_cleanup(int flagset) {
  206. struct cleanupentry *cep;
  207. int i;
  208. cep= econtext->cleanups;
  209. econtext->cleanups= cep->next;
  210. for (i=0; i<NCALLS; i++) {
  211. if (cep->calls[i].call && cep->calls[i].mask & flagset)
  212. cep->calls[i].call(cep->argc,cep->argv);
  213. }
  214. if (cep != &emergency.ce) free(cep);
  215. }
  216. void ohshit(const char *fmt, ...) {
  217. va_list al;
  218. va_start(al,fmt);
  219. vsnprintf(errmsgbuf,sizeof(errmsgbuf),fmt,al);
  220. va_end(al);
  221. errmsg= errmsgbuf;
  222. run_error_handler();
  223. }
  224. void print_error_fatal(const char *emsg, const char *contextstring) {
  225. fprintf(stderr, "%s: %s\n",thisname,emsg);
  226. }
  227. void ohshitv(const char *fmt, va_list al) {
  228. vsnprintf(errmsgbuf,sizeof(errmsgbuf),fmt,al);
  229. errmsg= errmsgbuf;
  230. run_error_handler();
  231. }
  232. void ohshite(const char *fmt, ...) {
  233. int e;
  234. va_list al;
  235. char buf[1024];
  236. e=errno;
  237. va_start(al,fmt);
  238. vsnprintf(buf,sizeof(buf),fmt,al);
  239. va_end(al);
  240. snprintf(errmsgbuf,sizeof(errmsgbuf),"%s: %s",buf,strerror(e));
  241. errmsg= errmsgbuf;
  242. run_error_handler();
  243. }
  244. void
  245. warning(const char *fmt, ...)
  246. {
  247. va_list al;
  248. char buf[1024];
  249. va_start(al,fmt);
  250. vsnprintf(buf,sizeof(buf),fmt,al);
  251. va_end(al);
  252. fprintf(stderr, _("%s: warning: %s\n"), thisname, buf);
  253. }
  254. void werr(const char *fn) {
  255. ohshite(_("error writing `%s'"),fn);
  256. }
  257. void
  258. do_internerr(const char *file, int line, const char *fmt, ...)
  259. {
  260. va_list al;
  261. char buf[1024];
  262. va_start(al, fmt);
  263. vsnprintf(buf, sizeof(buf), fmt, al);
  264. va_end(al);
  265. fprintf(stderr, _("%s:%s:%d: internal error: %s\n"),
  266. thisname, file, line, buf);
  267. abort();
  268. }