ehandle.c 8.3 KB

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