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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/*
* libdpkg - Debian packaging suite library routines
* mlib.c - `must' library: routines will succeed or longjmp
*
* 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 <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <dpkg.h>
#include <dpkg-db.h>
#include <md5.h>
/* Incremented when we do some kind of generally necessary operation, so that
* loops &c know to quit if we take an error exit. Decremented again afterwards.
*/
volatile int onerr_abort= 0;
void *m_malloc(size_t amount) {
#ifdef MDEBUG
unsigned short *r2, x;
#endif
void *r;
onerr_abort++;
r= malloc(amount);
if (!r) ohshite(_("malloc failed (%ld bytes)"),(long)amount);
onerr_abort--;
#ifdef MDEBUG
r2= r; x= (unsigned short)amount ^ 0xf000;
while (amount >= 2) { *r2++= x; amount -= 2; }
#endif
return r;
}
void *m_realloc(void *r, size_t amount) {
onerr_abort++;
r= realloc(r,amount);
if (!r) ohshite(_("realloc failed (%ld bytes)"),(long)amount);
onerr_abort--;
return r;
}
char *
m_strdup(const char *str)
{
char *new_str;
onerr_abort++;
new_str = strdup(str);
if (!new_str)
ohshite(_("failed to allocate memory"));
onerr_abort--;
return new_str;
}
static void print_error_forked(const char *emsg, const char *contextstring) {
fprintf(stderr, _("%s (subprocess): %s\n"), thisname, emsg);
}
static void cu_m_fork(int argc, void **argv) NONRETURNING;
static void cu_m_fork(int argc, void **argv) {
exit(2);
/* Don't do the other cleanups, because they'll be done by/in the parent
* process.
*/
}
int m_fork(void) {
pid_t r;
r= fork();
if (r == -1) { onerr_abort++; ohshite(_("fork failed")); }
if (r > 0) return r;
push_cleanup(cu_m_fork,~0, NULL,0, 0);
set_error_display(print_error_forked,NULL);
return r;
}
void m_dup2(int oldfd, int newfd) {
const char *const stdstrings[]= { "in", "out", "err" };
if (dup2(oldfd,newfd) == newfd) return;
onerr_abort++;
if (newfd < 3) ohshite(_("failed to dup for std%s"),stdstrings[newfd]);
ohshite(_("failed to dup for fd %d"),newfd);
}
void m_pipe(int *fds) {
if (!pipe(fds)) return;
onerr_abort++;
ohshite(_("failed to create pipe"));
}
int checksubprocerr(int status, const char *description, int flags) {
int n;
if (WIFEXITED(status)) {
n= WEXITSTATUS(status); if (!n) return n;
if(!(flags & PROCNOERR)) {
if(flags & PROCWARN)
fprintf(stderr, _("dpkg: warning - %s returned error exit status %d\n"),description,n);
else
ohshit(_("subprocess %s returned error exit status %d"),description,n);
}
} else if (WIFSIGNALED(status)) {
n= WTERMSIG(status); if (!n || ((flags & PROCPIPE) && n==SIGPIPE)) return 0;
if (flags & PROCWARN)
fprintf(stderr, _("dpkg: warning - %s killed by signal (%s)%s\n"),
description, strsignal(n), WCOREDUMP(status) ? _(", core dumped") : "");
else
ohshit(_("subprocess %s killed by signal (%s)%s"),
description, strsignal(n), WCOREDUMP(status) ? _(", core dumped") : "");
} else {
ohshit(_("subprocess %s failed with wait status code %d"),description,status);
}
return -1;
}
int waitsubproc(pid_t pid, const char *description, int flags) {
pid_t r;
int status;
while ((r= waitpid(pid,&status,0)) == -1 && errno == EINTR);
if (r != pid) { onerr_abort++; ohshite(_("wait for %s failed"),description); }
return checksubprocerr(status,description,flags);
}
void setcloexec(int fd, const char* fn) {
int f;
if ((f=fcntl(fd, F_GETFD))==-1)
ohshite(_("unable to read filedescriptor flags for %.250s"),fn);
if (fcntl(fd, F_SETFD, (f|FD_CLOEXEC))==-1)
ohshite(_("unable to set close-on-exec flag for %.250s"),fn);
}
struct buffer_write_md5ctx {
struct MD5Context ctx;
unsigned char **hash;
};
off_t buffer_write(buffer_data_t data, void *buf, off_t length, const char *desc) {
off_t ret= length;
if(data->type & BUFFER_WRITE_SETUP) {
switch(data->type ^ BUFFER_WRITE_SETUP) {
case BUFFER_WRITE_MD5:
{
struct buffer_write_md5ctx *ctx;
ctx = m_malloc(sizeof(struct buffer_write_md5ctx));
ctx->hash = data->data.ptr;
data->data.ptr = ctx;
MD5Init(&ctx->ctx);
}
break;
}
return 0;
}
if(data->type & BUFFER_WRITE_SHUTDOWN) {
switch(data->type ^ BUFFER_WRITE_SHUTDOWN) {
case BUFFER_WRITE_MD5:
{
int i;
unsigned char digest[16], *p = digest;
struct buffer_write_md5ctx *ctx = (struct buffer_write_md5ctx *)data->data.ptr;
unsigned char *hash = *ctx->hash = m_malloc(33);
MD5Final(digest, &ctx->ctx);
for (i = 0; i < 16; ++i) {
sprintf((char *)hash, "%02x", *p++);
hash += 2;
}
*hash = 0;
free(ctx);
}
break;
}
return 0;
}
switch(data->type) {
case BUFFER_WRITE_BUF:
memcpy(data->data.ptr, buf, length);
data->data.ptr += length;
break;
case BUFFER_WRITE_VBUF:
varbufaddbuf((struct varbuf *)data->data.ptr, buf, length);
break;
case BUFFER_WRITE_FD:
if((ret= write(data->data.i, buf, length)) < 0 && errno != EINTR)
ohshite(_("failed in buffer_write(fd) (%i, ret=%li): %s"), data->data.i, (long)ret, desc);
break;
case BUFFER_WRITE_NULL:
break;
case BUFFER_WRITE_STREAM:
ret= fwrite(buf, 1, length, (FILE *)data->data.ptr);
if(feof((FILE *)data->data.ptr))
ohshite(_("eof in buffer_write(stream): %s"), desc);
if(ferror((FILE *)data->data.ptr))
ohshite(_("error in buffer_write(stream): %s"), desc);
break;
case BUFFER_WRITE_MD5:
MD5Update(&(((struct buffer_write_md5ctx *)data->data.ptr)->ctx), buf, length);
break;
default:
fprintf(stderr, _("unknown data type `%i' in buffer_write\n"), data->type);
}
return ret;
}
off_t buffer_read(buffer_data_t data, void *buf, off_t length, const char *desc) {
off_t ret= length;
if(data->type & BUFFER_READ_SETUP) {
return 0;
}
if(data->type & BUFFER_READ_SHUTDOWN) {
return 0;
}
switch(data->type) {
case BUFFER_READ_FD:
if((ret= read(data->data.i, buf, length)) < 0 && errno != EINTR)
ohshite(_("failed in buffer_read(fd): %s"), desc);
break;
case BUFFER_READ_STREAM:
ret= fread(buf, 1, length, (FILE *)data->data.ptr);
if(feof((FILE *)data->data.ptr))
return ret;
if(ferror((FILE *)data->data.ptr))
ohshite(_("error in buffer_read(stream): %s"), desc);
break;
default:
fprintf(stderr, _("unknown data type `%i' in buffer_read\n"), data->type);
}
return ret;
}
#define buffer_copy_setup_dual(name, type1, name1, type2, name2) \
off_t buffer_copy_setup_##name(type1 n1, int typeIn, void *procIn,\
type2 n2, int typeOut, void *procOut,\
off_t limit, const char *desc, ...)\
{\
va_list al;\
buffer_arg a1, a2;\
struct varbuf v;\
off_t ret;\
a1.name1 = n1; a2.name2 = n2;\
varbufinit(&v);\
va_start(al,desc);\
varbufvprintf(&v, desc, al);\
va_end(al);\
ret = buffer_copy_setup(a1, typeIn, procIn,\
a2, typeOut, procOut,\
limit, v.buf);\
varbuffree(&v);\
return ret;\
}
buffer_copy_setup_dual(IntInt, int, i, int, i);
buffer_copy_setup_dual(IntPtr, int, i, void *, ptr);
buffer_copy_setup_dual(PtrInt, void *, ptr, int, i);
buffer_copy_setup_dual(PtrPtr, void *, ptr, void *, ptr);
off_t buffer_copy_setup(buffer_arg argIn, int typeIn, void *procIn,
buffer_arg argOut, int typeOut, void *procOut,
off_t limit, const char *desc)
{
struct buffer_data read_data = { procIn, argIn, typeIn },
write_data = { procOut, argOut, typeOut };
off_t ret;
if ( procIn == NULL )
read_data.proc = buffer_read;
if ( procOut == NULL )
write_data.proc = buffer_write;
read_data.type |= BUFFER_READ_SETUP;
read_data.proc(&read_data, NULL, 0, desc);
read_data.type = typeIn;
write_data.type |= BUFFER_WRITE_SETUP;
write_data.proc(&write_data, NULL, 0, desc);
write_data.type = typeOut;
ret = buffer_copy(&read_data, &write_data, limit, desc);
write_data.type |= BUFFER_WRITE_SHUTDOWN;
write_data.proc(&write_data, NULL, 0, desc);
read_data.type |= BUFFER_READ_SHUTDOWN;
read_data.proc(&read_data, NULL, 0, desc);
return ret;
}
off_t buffer_copy(buffer_data_t read_data, buffer_data_t write_data, off_t limit, const char *desc) {
char *buf, *writebuf;
long bytesread= 0, byteswritten= 0;
int bufsize= 32768;
off_t totalread= 0, totalwritten= 0;
if((limit!=-1) && (limit < bufsize)) bufsize= limit;
if(bufsize == 0)
return 0;
writebuf = buf= m_malloc(bufsize);
while(bytesread >= 0 && byteswritten >= 0 && bufsize > 0) {
bytesread= read_data->proc(read_data, buf, bufsize, desc);
if (bytesread<0) {
if (errno==EINTR || errno==EAGAIN) continue;
break;
}
if (bytesread==0)
break;
totalread+= bytesread;
if(limit != -1) {
limit-= bytesread;
if(limit<bufsize)
bufsize=limit;
}
writebuf= buf;
while(bytesread) {
byteswritten= write_data->proc(write_data, writebuf, bytesread, desc);
if(byteswritten == -1) {
if(errno == EINTR || errno==EAGAIN) continue;
break;
}
if(byteswritten==0)
break;
bytesread-= byteswritten;
totalwritten+= byteswritten;
writebuf+= byteswritten;
}
}
if (bytesread<0 || byteswritten<0) ohshite(_("failed in buffer_copy (%s)"), desc);
if (limit > 0) ohshit(_("short read in buffer_copy (%s)"), desc);
free(buf);
return totalread;
}
|