version 1.1.1.1, 2013/10/14 07:51:14
|
version 1.1.1.3, 2021/03/17 00:32:36
|
Line 4
|
Line 4
|
* Copyright (C) 1996-2000 Andrew Tridgell |
* Copyright (C) 1996-2000 Andrew Tridgell |
* Copyright (C) 1996 Paul Mackerras |
* Copyright (C) 1996 Paul Mackerras |
* Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> |
* Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> |
* Copyright (C) 2003-2013 Wayne Davison | * Copyright (C) 2003-2020 Wayne Davison |
* |
* |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
Line 21
|
Line 21
|
*/ |
*/ |
|
|
#include "rsync.h" |
#include "rsync.h" |
#include "ifuncs.h" |
|
#include "itypes.h" |
#include "itypes.h" |
#include "inums.h" |
#include "inums.h" |
|
|
extern int checksum_len; | extern size_t max_alloc; |
|
|
|
char *do_calloc = "42"; |
|
|
/** |
/** |
* Sleep for a specified number of milliseconds. |
* Sleep for a specified number of milliseconds. |
* |
* |
* Always returns TRUE. (In the future it might return FALSE if | * Always returns True. |
* interrupted.) | |
**/ |
**/ |
int msleep(int t) |
int msleep(int t) |
{ |
{ |
|
#ifdef HAVE_NANOSLEEP |
|
struct timespec ts; |
|
|
|
ts.tv_sec = t / 1000; |
|
ts.tv_nsec = (t % 1000) * 1000000L; |
|
|
|
while (nanosleep(&ts, &ts) < 0 && errno == EINTR) {} |
|
|
|
#elif defined HAVE_USLEEP |
|
usleep(t*1000); |
|
|
|
#else |
int tdiff = 0; |
int tdiff = 0; |
struct timeval tval, t1, t2; |
struct timeval tval, t1, t2; |
|
|
Line 48 int msleep(int t)
|
Line 60 int msleep(int t)
|
select(0,NULL,NULL, NULL, &tval); |
select(0,NULL,NULL, NULL, &tval); |
|
|
gettimeofday(&t2, NULL); |
gettimeofday(&t2, NULL); |
if (t2.tv_sec < t1.tv_sec) |
|
t1 = t2; /* Time went backwards, so start over. */ |
|
tdiff = (t2.tv_sec - t1.tv_sec)*1000 + |
tdiff = (t2.tv_sec - t1.tv_sec)*1000 + |
(t2.tv_usec - t1.tv_usec)/1000; |
(t2.tv_usec - t1.tv_usec)/1000; |
|
if (tdiff < 0) |
|
t1 = t2; /* Time went backwards, so start over. */ |
} |
} |
|
#endif |
|
|
return True; |
return True; |
} |
} |
|
|
#define MALLOC_MAX 0x40000000 | void *my_alloc(void *ptr, size_t num, size_t size, const char *file, int line) |
| |
void *_new_array(unsigned long num, unsigned int size, int use_calloc) | |
{ |
{ |
if (num >= MALLOC_MAX/size) | if (max_alloc && num >= max_alloc/size) { |
return NULL; | if (!file) |
return use_calloc ? calloc(num, size) : malloc(num * size); | return NULL; |
} | rprintf(FERROR, "[%s] exceeded --max-alloc=%s setting (file=%s, line=%d)\n", |
| who_am_i(), do_big_num(max_alloc, 0, NULL), src_file(file), line); |
void *_realloc_array(void *ptr, unsigned int size, size_t num) | exit_cleanup(RERR_MALLOC); |
{ | } |
if (num >= MALLOC_MAX/size) | |
return NULL; | |
if (!ptr) |
if (!ptr) |
return malloc(size * num); | ptr = malloc(num * size); |
return realloc(ptr, size * num); | else if (ptr == do_calloc) |
| ptr = calloc(num, size); |
| else |
| ptr = realloc(ptr, num * size); |
| if (!ptr && file) |
| _out_of_memory("my_alloc caller", file, line); |
| return ptr; |
} |
} |
|
|
const char *sum_as_hex(const char *sum) | const char *sum_as_hex(int csum_type, const char *sum, int flist_csum) |
{ |
{ |
static char buf[MAX_DIGEST_LEN*2+1]; |
static char buf[MAX_DIGEST_LEN*2+1]; |
int i, x1, x2; |
int i, x1, x2; |
char *c = buf + checksum_len*2; | int canonical = canonical_checksum(csum_type); |
| int sum_len = csum_len_for_type(csum_type, flist_csum); |
| char *c; |
|
|
assert(c - buf < (int)sizeof buf); | if (!canonical) |
| return NULL; |
|
|
*c = '\0'; | assert(sum_len*2 < (int)sizeof buf); |
|
|
for (i = checksum_len; --i >= 0; ) { | for (i = sum_len, c = buf; --i >= 0; ) { |
x1 = CVAL(sum, i); | int ndx = canonical < 0 ? sum_len - i - 1 : i; |
x2 = x1 >> 4; | x2 = CVAL(sum, ndx); |
x1 &= 0xF; | x1 = x2 >> 4; |
*--c = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10; | x2 &= 0xF; |
*--c = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10; | *c++ = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10; |
| *c++ = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10; |
} |
} |
|
|
|
*c = '\0'; |
|
|
return buf; |
return buf; |
} |
} |
|
|
NORETURN void out_of_memory(const char *str) | NORETURN void _out_of_memory(const char *msg, const char *file, int line) |
{ |
{ |
rprintf(FERROR, "ERROR: out of memory in %s [%s]\n", str, who_am_i()); | rprintf(FERROR, "[%s] out of memory: %s (file=%s, line=%d)\n", who_am_i(), msg, src_file(file), line); |
exit_cleanup(RERR_MALLOC); |
exit_cleanup(RERR_MALLOC); |
} |
} |
|
|
NORETURN void overflow_exit(const char *str) | NORETURN void _overflow_exit(const char *msg, const char *file, int line) |
{ |
{ |
rprintf(FERROR, "ERROR: buffer overflow in %s [%s]\n", str, who_am_i()); | rprintf(FERROR, "[%s] buffer overflow: %s (file=%s, line=%d)\n", who_am_i(), msg, src_file(file), line); |
exit_cleanup(RERR_MALLOC); |
exit_cleanup(RERR_MALLOC); |
|
} |
|
|
|
const char *src_file(const char *file) |
|
{ |
|
static const char *util2 = __FILE__; |
|
static int prefix = -1; |
|
|
|
if (prefix < 0) { |
|
const char *cp = strrchr(util2, '/'); |
|
prefix = cp ? cp - util2 + 1 : 0; |
|
} |
|
|
|
if (prefix && strncmp(file, util2, prefix) == 0) |
|
return file + prefix; |
|
return file; |
} |
} |