| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "rsync.h" |
| 24 |
#include "itypes.h" |
| 25 |
#include "inums.h" |
| 26 |
|
| 27 |
extern size_t max_alloc; |
| 28 |
|
| 29 |
char *do_calloc = "42"; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
int msleep(int t) |
| 37 |
{ |
| 38 |
#ifdef HAVE_NANOSLEEP |
| 39 |
struct timespec ts; |
| 40 |
|
| 41 |
ts.tv_sec = t / 1000; |
| 42 |
ts.tv_nsec = (t % 1000) * 1000000L; |
| 43 |
|
| 44 |
while (nanosleep(&ts, &ts) < 0 && errno == EINTR) {} |
| 45 |
|
| 46 |
#elif defined HAVE_USLEEP |
| 47 |
usleep(t*1000); |
| 48 |
|
| 49 |
#else |
| 50 |
int tdiff = 0; |
| 51 |
struct timeval tval, t1, t2; |
| 52 |
|
| 53 |
gettimeofday(&t1, NULL); |
| 54 |
|
| 55 |
while (tdiff < t) { |
| 56 |
tval.tv_sec = (t-tdiff)/1000; |
| 57 |
tval.tv_usec = 1000*((t-tdiff)%1000); |
| 58 |
|
| 59 |
errno = 0; |
| 60 |
select(0,NULL,NULL, NULL, &tval); |
| 61 |
|
| 62 |
gettimeofday(&t2, NULL); |
| 63 |
tdiff = (t2.tv_sec - t1.tv_sec)*1000 + |
| 64 |
(t2.tv_usec - t1.tv_usec)/1000; |
| 65 |
if (tdiff < 0) |
| 66 |
t1 = t2; |
| 67 |
} |
| 68 |
#endif |
| 69 |
|
| 70 |
return True; |
| 71 |
} |
| 72 |
|
| 73 |
void *my_alloc(void *ptr, size_t num, size_t size, const char *file, int line) |
| 74 |
{ |
| 75 |
if (num >= max_alloc/size) { |
| 76 |
if (!file) |
| 77 |
return NULL; |
| 78 |
rprintf(FERROR, "[%s] exceeded --max-alloc=%s setting (file=%s, line=%d)\n", |
| 79 |
who_am_i(), do_big_num(max_alloc, 0, NULL), src_file(file), line); |
| 80 |
exit_cleanup(RERR_MALLOC); |
| 81 |
} |
| 82 |
if (!ptr) |
| 83 |
ptr = malloc(num * size); |
| 84 |
else if (ptr == do_calloc) |
| 85 |
ptr = calloc(num, size); |
| 86 |
else |
| 87 |
ptr = realloc(ptr, num * size); |
| 88 |
if (!ptr && file) |
| 89 |
_out_of_memory("my_alloc caller", file, line); |
| 90 |
return ptr; |
| 91 |
} |
| 92 |
|
| 93 |
const char *sum_as_hex(int csum_type, const char *sum, int flist_csum) |
| 94 |
{ |
| 95 |
static char buf[MAX_DIGEST_LEN*2+1]; |
| 96 |
int i, x1, x2; |
| 97 |
int canonical = canonical_checksum(csum_type); |
| 98 |
int sum_len = csum_len_for_type(csum_type, flist_csum); |
| 99 |
char *c; |
| 100 |
|
| 101 |
if (!canonical) |
| 102 |
return NULL; |
| 103 |
|
| 104 |
assert(sum_len*2 < (int)sizeof buf); |
| 105 |
|
| 106 |
for (i = sum_len, c = buf; --i >= 0; ) { |
| 107 |
int ndx = canonical < 0 ? sum_len - i - 1 : i; |
| 108 |
x2 = CVAL(sum, ndx); |
| 109 |
x1 = x2 >> 4; |
| 110 |
x2 &= 0xF; |
| 111 |
*c++ = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10; |
| 112 |
*c++ = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10; |
| 113 |
} |
| 114 |
|
| 115 |
*c = '\0'; |
| 116 |
|
| 117 |
return buf; |
| 118 |
} |
| 119 |
|
| 120 |
NORETURN void _out_of_memory(const char *msg, const char *file, int line) |
| 121 |
{ |
| 122 |
rprintf(FERROR, "[%s] out of memory: %s (file=%s, line=%d)\n", who_am_i(), msg, src_file(file), line); |
| 123 |
exit_cleanup(RERR_MALLOC); |
| 124 |
} |
| 125 |
|
| 126 |
NORETURN void _overflow_exit(const char *msg, const char *file, int line) |
| 127 |
{ |
| 128 |
rprintf(FERROR, "[%s] buffer overflow: %s (file=%s, line=%d)\n", who_am_i(), msg, src_file(file), line); |
| 129 |
exit_cleanup(RERR_MALLOC); |
| 130 |
} |
| 131 |
|
| 132 |
const char *src_file(const char *file) |
| 133 |
{ |
| 134 |
static const char *util2 = __FILE__; |
| 135 |
static int prefix = -1; |
| 136 |
|
| 137 |
if (prefix < 0) { |
| 138 |
const char *cp = strrchr(util2, '/'); |
| 139 |
prefix = cp ? cp - util2 + 1 : 0; |
| 140 |
} |
| 141 |
|
| 142 |
if (prefix && strncmp(file, util2, prefix) == 0) |
| 143 |
return file + prefix; |
| 144 |
return file; |
| 145 |
} |