1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/*
* libdpkg - Debian packaging suite library routines
* ehandle.c - error handling
*
* Copyright (C) 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
* This is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with dpkg; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <assert.h>
#include <dpkg.h>
#include <dpkg-db.h>
static const char *errmsg; /* points to errmsgbuf or malloc'd */
static char errmsgbuf[4096];
/* 6x255 for inserted strings (%.255s &c in fmt; also %s with limited length arg)
* 1x255 for constant text
* 1x255 for strerror()
* same again just in case.
*/
#define NCALLS 2
struct cleanupentry {
struct cleanupentry *next;
struct {
int mask;
void (*call)(int argc, void **argv);
} calls[NCALLS];
int cpmask, cpvalue;
int argc;
void *argv[1];
};
struct errorcontext {
struct errorcontext *next;
jmp_buf *jbufp;
struct cleanupentry *cleanups;
void (*printerror)(const char *emsg, const char *contextstring);
const char *contextstring;
};
static struct errorcontext *volatile econtext= NULL;
static struct { struct cleanupentry ce; void *args[20]; } emergency;
void set_error_display(error_printer *printerror,
const char *contextstring) {
assert(econtext);
econtext->printerror= printerror;
econtext->contextstring= contextstring;
}
static void run_error_handler(void) NONRETURNING;
static void
run_error_handler(void)
{
if (onerr_abort) {
/* We arrived here due to a fatal error from which we cannot recover,
* and trying to do so would most probably get us here again. That's
* why we will not try to do any error unwinding either. We'll just
* abort. Hopefully the user can fix the situation (out of disk, out
* of memory, etc).
*/
fprintf(stderr, _("%s: unrecoverable fatal error, aborting:\n %s\n"),
thisname, errmsg);
exit(2);
} else {
longjmp(*econtext->jbufp, 1);
}
}
void push_error_handler(jmp_buf *jbufp,
error_printer *printerror,
const char *contextstring) {
struct errorcontext *necp;
necp= malloc(sizeof(struct errorcontext));
if (!necp) {
int e= errno;
snprintf(errmsgbuf, sizeof(errmsgbuf), "%s%s",
_("out of memory pushing error handler: "), strerror(e));
errmsg= errmsgbuf;
if (econtext)
run_error_handler();
fprintf(stderr, "%s: %s\n", thisname, errmsgbuf); exit(2);
}
necp->next= econtext;
necp->jbufp= jbufp;
necp->cleanups= NULL;
necp->printerror= printerror;
necp->contextstring= contextstring;
econtext= necp;
onerr_abort= 0;
}
static void print_error_cleanup(const char *emsg, const char *contextstring) {
fprintf(stderr, _("%s: error while cleaning up:\n %s\n"),thisname,emsg);
}
static void run_cleanups(struct errorcontext *econ, int flagsetin) {
static volatile int preventrecurse= 0;
struct cleanupentry *volatile cep;
struct cleanupentry *ncep;
struct errorcontext recurserr, *oldecontext;
jmp_buf recurejbuf;
volatile int i, flagset;
if (econ->printerror) econ->printerror(errmsg,econ->contextstring);
if (++preventrecurse > 3) {
onerr_abort++;
fprintf(stderr, _("dpkg: too many nested errors during error recovery !!\n"));
flagset= 0;
} else {
flagset= flagsetin;
}
cep= econ->cleanups;
oldecontext= econtext;
while (cep) {
for (i=0; i<NCALLS; i++) {
if (cep->calls[i].call && cep->calls[i].mask & flagset) {
if (setjmp(recurejbuf)) {
run_cleanups(&recurserr, ehflag_bombout | ehflag_recursiveerror);
} else {
recurserr.jbufp= &recurejbuf;
recurserr.cleanups= NULL;
recurserr.next= NULL;
recurserr.printerror= print_error_cleanup;
recurserr.contextstring= NULL;
econtext= &recurserr;
cep->calls[i].call(cep->argc,cep->argv);
}
econtext= oldecontext;
}
}
flagset &= cep->cpmask;
flagset |= cep->cpvalue;
ncep= cep->next;
if (cep != &emergency.ce) free(cep);
cep= ncep;
}
preventrecurse--;
}
void error_unwind(int flagset) {
struct cleanupentry *cep;
struct errorcontext *tecp;
tecp= econtext;
cep= tecp->cleanups;
econtext= tecp->next;
run_cleanups(tecp,flagset);
free(tecp);
}
void push_checkpoint(int mask, int value) {
/* This will arrange that when error_unwind() is called,
* all previous cleanups will be executed with
* flagset= (original_flagset & mask) | value
* where original_flagset is the argument to error_unwind
* (as modified by any checkpoint which was pushed later).
*/
struct cleanupentry *cep;
int i;
cep= m_malloc(sizeof(struct cleanupentry) + sizeof(char*));
for (i=0; i<NCALLS; i++) { cep->calls[i].call=NULL; cep->calls[i].mask=0; }
cep->cpmask= mask; cep->cpvalue= value;
cep->argc= 0; cep->argv[0]= NULL;
cep->next= econtext->cleanups;
econtext->cleanups= cep;
}
void push_cleanup(void (*call1)(int argc, void **argv), int mask1,
void (*call2)(int argc, void **argv), int mask2,
unsigned int nargs, ...) {
struct cleanupentry *cep;
void **args;
int e;
va_list al;
onerr_abort++;
cep= malloc(sizeof(struct cleanupentry) + sizeof(char*)*(nargs+1));
if (!cep) {
if (nargs > sizeof(emergency.args)/sizeof(void*))
ohshite(_("out of memory for new cleanup entry with many arguments"));
e= errno; cep= &emergency.ce;
}
cep->calls[0].call= call1; cep->calls[0].mask= mask1;
cep->calls[1].call= call2; cep->calls[1].mask= mask2;
cep->cpmask=~0; cep->cpvalue=0; cep->argc= nargs;
va_start(al,nargs);
args= cep->argv; while (nargs-- >0) *args++= va_arg(al,void*);
*args++= NULL;
va_end(al);
cep->next= econtext->cleanups;
econtext->cleanups= cep;
if (cep == &emergency.ce) { e= errno; ohshite(_("out of memory for new cleanup entry")); }
onerr_abort--;
}
void pop_cleanup(int flagset) {
struct cleanupentry *cep;
int i;
cep= econtext->cleanups;
econtext->cleanups= cep->next;
for (i=0; i<NCALLS; i++) {
if (cep->calls[i].call && cep->calls[i].mask & flagset)
cep->calls[i].call(cep->argc,cep->argv);
}
if (cep != &emergency.ce) free(cep);
}
void ohshit(const char *fmt, ...) {
va_list al;
va_start(al,fmt);
vsnprintf(errmsgbuf,sizeof(errmsgbuf),fmt,al);
va_end(al);
errmsg= errmsgbuf;
run_error_handler();
}
void print_error_fatal(const char *emsg, const char *contextstring) {
fprintf(stderr, "%s: %s\n",thisname,emsg);
}
void ohshitvb(struct varbuf *vb) {
char *m;
varbufaddc(vb,0);
m= m_malloc(strlen(vb->buf));
strcpy(m,vb->buf);
errmsg= m;
run_error_handler();
}
void ohshitv(const char *fmt, va_list al) {
vsnprintf(errmsgbuf,sizeof(errmsgbuf),fmt,al);
errmsg= errmsgbuf;
run_error_handler();
}
void ohshite(const char *fmt, ...) {
int e;
va_list al;
char buf[1024];
e=errno;
va_start(al,fmt);
vsnprintf(buf,sizeof(buf),fmt,al);
va_end(al);
snprintf(errmsgbuf,sizeof(errmsgbuf),"%s: %s",buf,strerror(e));
errmsg= errmsgbuf;
run_error_handler();
}
void warningf(const char *fmt, ...) {
int e;
va_list al;
char buf[1024];
e=errno;
va_start(al,fmt);
vsnprintf(buf,sizeof(buf),fmt,al);
va_end(al);
fprintf(stderr,"%s: %s\n",buf,strerror(e));
}
void badusage(const char *fmt, ...) {
char buf[1024];
va_list al;
va_start(al,fmt);
vsnprintf(buf,sizeof(buf), fmt,al);
va_end(al);
snprintf(errmsgbuf,sizeof(errmsgbuf),"%s\n\n%s", buf, gettext(printforhelp));
errmsg= errmsgbuf;
longjmp(*econtext->jbufp,1);
}
void werr(const char *fn) {
ohshite(_("error writing `%s'"),fn);
}
void do_internerr(const char *string, int line, const char *file) {
fprintf(stderr,_("%s:%d: internal error `%s'\n"),file,line,string);
abort();
}
|