ehandle.c 7.9 KB

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