ehandle.c 8.4 KB

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