| 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 |
#include "util.h" |
| 30 |
|
| 31 |
#include "config.h" |
| 32 |
#include "log.h" |
| 33 |
|
| 34 |
#ifdef DEBUG |
| 35 |
#include "cache.h" |
| 36 |
#include "link.h" |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <curl/curl.h> |
| 40 |
#include <errno.h> |
| 41 |
#include <execinfo.h> |
| 42 |
#include <openssl/evp.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
#include <unistd.h> |
| 46 |
#include <uuid/uuid.h> |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#define BT_BUF_SIZE 100 |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#define MD5_HASH_LEN 32 |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#define SALT_LEN 36 |
| 64 |
|
| 65 |
#ifdef DEBUG |
| 66 |
#include <stdint.h> |
| 67 |
#define MEM_HASH_SIZE 8192 |
| 68 |
static MemNode *mem_hash_table[MEM_HASH_SIZE] = {NULL}; |
| 69 |
static pthread_mutex_t mem_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 70 |
|
| 71 |
static inline size_t hash_ptr(const void *ptr) |
| 72 |
{ |
| 73 |
uintptr_t v = (uintptr_t)ptr; |
| 74 |
return ((v >> 3) ^ (v >> 16)) & (MEM_HASH_SIZE - 1); |
| 75 |
} |
| 76 |
#endif |
| 77 |
|
| 78 |
char *path_append(const char *path, const char *filename) |
| 79 |
{ |
| 80 |
if (!path || !filename) { |
| 81 |
lprintf(fatal, "path_append: path or filename is NULL\n"); |
| 82 |
} |
| 83 |
size_t ul = strnlen(path, PATH_MAX); |
| 84 |
size_t fl = strnlen(filename, PATH_MAX); |
| 85 |
size_t skip = 0; |
| 86 |
int needs_separator = 0; |
| 87 |
const char *f = filename; |
| 88 |
|
| 89 |
if (ul > 0) { |
| 90 |
if (path[ul - 1] != '/') { |
| 91 |
needs_separator = 1; |
| 92 |
} |
| 93 |
while (skip < fl && filename[skip] == '/') { |
| 94 |
skip++; |
| 95 |
} |
| 96 |
f = filename + skip; |
| 97 |
} |
| 98 |
|
| 99 |
char *str; |
| 100 |
size_t sl = fl - skip; |
| 101 |
str = CALLOC(ul + sl + needs_separator + 1, sizeof(char)); |
| 102 |
memcpy(str, path, ul); |
| 103 |
if (needs_separator) { |
| 104 |
str[ul] = '/'; |
| 105 |
} |
| 106 |
memcpy(str + ul + needs_separator, f, sl); |
| 107 |
return str; |
| 108 |
} |
| 109 |
|
| 110 |
void pthread_mutex_init_wrapper(pthread_mutex_t *x, |
| 111 |
const pthread_mutexattr_t *attr, |
| 112 |
const char *file, const char *func, int line) |
| 113 |
{ |
| 114 |
pthread_mutexattr_t mutex_attr; |
| 115 |
if (attr == NULL) { |
| 116 |
pthread_mutexattr_init(&mutex_attr); |
| 117 |
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK); |
| 118 |
attr = &mutex_attr; |
| 119 |
} |
| 120 |
int ret = pthread_mutex_init(x, attr); |
| 121 |
if (ret) { |
| 122 |
fatal_log_printf(file, func, line, "%lx pthread_mutex_init: %d, %s\n", |
| 123 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 124 |
} |
| 125 |
if (attr == &mutex_attr) { |
| 126 |
pthread_mutexattr_destroy(&mutex_attr); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void pthread_mutex_destroy_wrapper(pthread_mutex_t *x, const char *file, |
| 131 |
const char *func, int line) |
| 132 |
{ |
| 133 |
int ret; |
| 134 |
ret = pthread_mutex_destroy(x); |
| 135 |
if (ret) { |
| 136 |
fatal_log_printf(file, func, line, |
| 137 |
"%lx pthread_mutex_destroy: %d, %s\n", |
| 138 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void pthread_mutex_unlock_wrapper(const char *file, const char *func, int line, |
| 143 |
pthread_mutex_t *x) |
| 144 |
{ |
| 145 |
int i; |
| 146 |
i = pthread_mutex_unlock(x); |
| 147 |
if (i) { |
| 148 |
fatal_log_printf(file, func, line, "%lx pthread_mutex_unlock: %d, %s\n", |
| 149 |
(unsigned long)pthread_self(), i, strerror(i)); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
void pthread_mutex_lock_wrapper(const char *file, const char *func, int line, |
| 154 |
pthread_mutex_t *x) |
| 155 |
{ |
| 156 |
int i; |
| 157 |
i = pthread_mutex_lock(x); |
| 158 |
if (i) { |
| 159 |
fatal_log_printf(file, func, line, "%lx pthread_mutex_lock: %d, %s\n", |
| 160 |
(unsigned long)pthread_self(), i, strerror(i)); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
#ifdef __APPLE__ |
| 165 |
|
| 166 |
void sem_init_wrapper(sys_sem_t *sem, int pshared, unsigned int value, |
| 167 |
const char *file, const char *func, int line) |
| 168 |
{ |
| 169 |
(void)pshared; |
| 170 |
int ret = pthread_mutex_init(&sem->mutex, NULL); |
| 171 |
if (ret) { |
| 172 |
fatal_log_printf( |
| 173 |
file, func, line, |
| 174 |
"%lx pthread_mutex_init for semaphore failed: %d, %s\n", |
| 175 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 176 |
} |
| 177 |
ret = pthread_cond_init(&sem->cond, NULL); |
| 178 |
if (ret) { |
| 179 |
pthread_mutex_destroy(&sem->mutex); |
| 180 |
fatal_log_printf(file, func, line, |
| 181 |
"%lx pthread_cond_init for semaphore failed: %d, %s\n", |
| 182 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 183 |
} |
| 184 |
sem->count = (int)value; |
| 185 |
} |
| 186 |
|
| 187 |
void sem_destroy_wrapper(sys_sem_t *sem, const char *file, const char *func, |
| 188 |
int line) |
| 189 |
{ |
| 190 |
int ret = pthread_mutex_destroy(&sem->mutex); |
| 191 |
if (ret) { |
| 192 |
fatal_log_printf( |
| 193 |
file, func, line, |
| 194 |
"%lx pthread_mutex_destroy for semaphore failed: %d, %s\n", |
| 195 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 196 |
} |
| 197 |
ret = pthread_cond_destroy(&sem->cond); |
| 198 |
if (ret) { |
| 199 |
fatal_log_printf( |
| 200 |
file, func, line, |
| 201 |
"%lx pthread_cond_destroy for semaphore failed: %d, %s\n", |
| 202 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void sem_wait_wrapper(const char *file, const char *func, int line, |
| 207 |
sys_sem_t *sem) |
| 208 |
{ |
| 209 |
int ret = pthread_mutex_lock(&sem->mutex); |
| 210 |
if (ret) { |
| 211 |
fatal_log_printf( |
| 212 |
file, func, line, |
| 213 |
"%lx pthread_mutex_lock for semaphore failed: %d, %s\n", |
| 214 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 215 |
} |
| 216 |
while (sem->count <= 0) { |
| 217 |
ret = pthread_cond_wait(&sem->cond, &sem->mutex); |
| 218 |
if (ret) { |
| 219 |
pthread_mutex_unlock(&sem->mutex); |
| 220 |
fatal_log_printf( |
| 221 |
file, func, line, |
| 222 |
"%lx pthread_cond_wait for semaphore failed: %d, %s\n", |
| 223 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 224 |
} |
| 225 |
} |
| 226 |
sem->count--; |
| 227 |
ret = pthread_mutex_unlock(&sem->mutex); |
| 228 |
if (ret) { |
| 229 |
fatal_log_printf( |
| 230 |
file, func, line, |
| 231 |
"%lx pthread_mutex_unlock for semaphore failed: %d, %s\n", |
| 232 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
void sem_post_wrapper(const char *file, const char *func, int line, |
| 237 |
sys_sem_t *sem) |
| 238 |
{ |
| 239 |
int ret = pthread_mutex_lock(&sem->mutex); |
| 240 |
if (ret) { |
| 241 |
fatal_log_printf( |
| 242 |
file, func, line, |
| 243 |
"%lx pthread_mutex_lock for semaphore failed: %d, %s\n", |
| 244 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 245 |
} |
| 246 |
sem->count++; |
| 247 |
ret = pthread_cond_signal(&sem->cond); |
| 248 |
if (ret) { |
| 249 |
pthread_mutex_unlock(&sem->mutex); |
| 250 |
fatal_log_printf( |
| 251 |
file, func, line, |
| 252 |
"%lx pthread_cond_signal for semaphore failed: %d, %s\n", |
| 253 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 254 |
} |
| 255 |
ret = pthread_mutex_unlock(&sem->mutex); |
| 256 |
if (ret) { |
| 257 |
fatal_log_printf( |
| 258 |
file, func, line, |
| 259 |
"%lx pthread_mutex_unlock for semaphore failed: %d, %s\n", |
| 260 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
int sys_sem_trywait_wrapper(const char *file, const char *func, int line, |
| 265 |
sys_sem_t *sem) |
| 266 |
{ |
| 267 |
int ret = 0; |
| 268 |
int err = pthread_mutex_lock(&sem->mutex); |
| 269 |
if (err) { |
| 270 |
fatal_log_printf( |
| 271 |
file, func, line, |
| 272 |
"%lx pthread_mutex_lock for semaphore failed: %d, %s\n", |
| 273 |
(unsigned long)pthread_self(), err, strerror(err)); |
| 274 |
} |
| 275 |
if (sem->count > 0) { |
| 276 |
sem->count--; |
| 277 |
} else { |
| 278 |
ret = -1; |
| 279 |
} |
| 280 |
err = pthread_mutex_unlock(&sem->mutex); |
| 281 |
if (err) { |
| 282 |
fatal_log_printf( |
| 283 |
file, func, line, |
| 284 |
"%lx pthread_mutex_unlock for semaphore failed: %d, %s\n", |
| 285 |
(unsigned long)pthread_self(), err, strerror(err)); |
| 286 |
} |
| 287 |
if (ret == -1) { |
| 288 |
errno = EAGAIN; |
| 289 |
} |
| 290 |
return ret; |
| 291 |
} |
| 292 |
|
| 293 |
#else |
| 294 |
|
| 295 |
void sem_init_wrapper(sys_sem_t *sem, int pshared, unsigned int value, |
| 296 |
const char *file, const char *func, int line) |
| 297 |
{ |
| 298 |
int i; |
| 299 |
i = sem_init(sem, pshared, value); |
| 300 |
if (i) { |
| 301 |
int saved_errno = errno; |
| 302 |
fatal_log_printf(file, func, line, "%lx sem_init: %d, %s\n", |
| 303 |
(unsigned long)pthread_self(), saved_errno, |
| 304 |
strerror(saved_errno)); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
void sem_destroy_wrapper(sys_sem_t *sem, const char *file, const char *func, |
| 309 |
int line) |
| 310 |
{ |
| 311 |
int i; |
| 312 |
i = sem_destroy(sem); |
| 313 |
if (i) { |
| 314 |
int saved_errno = errno; |
| 315 |
fatal_log_printf(file, func, line, "%lx sem_destroy: %d, %s\n", |
| 316 |
(unsigned long)pthread_self(), saved_errno, |
| 317 |
strerror(saved_errno)); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
void sem_wait_wrapper(const char *file, const char *func, int line, |
| 322 |
sys_sem_t *sem) |
| 323 |
{ |
| 324 |
int i; |
| 325 |
i = sem_wait(sem); |
| 326 |
if (i) { |
| 327 |
int saved_errno = errno; |
| 328 |
fatal_log_printf(file, func, line, "%lx sem_wait: %d, %s\n", |
| 329 |
(unsigned long)pthread_self(), saved_errno, |
| 330 |
strerror(saved_errno)); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
void sem_post_wrapper(const char *file, const char *func, int line, |
| 335 |
sys_sem_t *sem) |
| 336 |
{ |
| 337 |
int i; |
| 338 |
i = sem_post(sem); |
| 339 |
if (i) { |
| 340 |
int saved_errno = errno; |
| 341 |
fatal_log_printf(file, func, line, "%lx sem_post: %d, %s\n", |
| 342 |
(unsigned long)pthread_self(), saved_errno, |
| 343 |
strerror(saved_errno)); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
int sys_sem_trywait_wrapper(const char *file, const char *func, int line, |
| 348 |
sys_sem_t *sem) |
| 349 |
{ |
| 350 |
int i = sem_trywait(sem); |
| 351 |
if (i) { |
| 352 |
int saved_errno = errno; |
| 353 |
if (saved_errno != EAGAIN) { |
| 354 |
fatal_log_printf(file, func, line, "%lx sem_trywait: %d, %s\n", |
| 355 |
(unsigned long)pthread_self(), saved_errno, |
| 356 |
strerror(saved_errno)); |
| 357 |
} |
| 358 |
} |
| 359 |
return i; |
| 360 |
} |
| 361 |
|
| 362 |
#endif |
| 363 |
|
| 364 |
void pthread_cond_init_wrapper(pthread_cond_t *cond, |
| 365 |
const pthread_condattr_t *attr, const char *file, |
| 366 |
const char *func, int line) |
| 367 |
{ |
| 368 |
int ret = pthread_cond_init(cond, attr); |
| 369 |
if (ret) { |
| 370 |
fatal_log_printf(file, func, line, "%lx pthread_cond_init: %d, %s\n", |
| 371 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
void pthread_cond_destroy_wrapper(pthread_cond_t *cond, const char *file, |
| 376 |
const char *func, int line) |
| 377 |
{ |
| 378 |
int ret = pthread_cond_destroy(cond); |
| 379 |
if (ret) { |
| 380 |
fatal_log_printf(file, func, line, "%lx pthread_cond_destroy: %d, %s\n", |
| 381 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
void pthread_cond_broadcast_wrapper(const char *file, const char *func, |
| 386 |
int line, pthread_cond_t *cond) |
| 387 |
{ |
| 388 |
int ret = pthread_cond_broadcast(cond); |
| 389 |
if (ret) { |
| 390 |
fatal_log_printf(file, func, line, |
| 391 |
"%lx pthread_cond_broadcast: %d, %s\n", |
| 392 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
void pthread_cond_wait_wrapper(const char *file, const char *func, int line, |
| 397 |
pthread_cond_t *cond, pthread_mutex_t *mutex) |
| 398 |
{ |
| 399 |
int ret = pthread_cond_wait(cond, mutex); |
| 400 |
if (ret) { |
| 401 |
fatal_log_printf(file, func, line, "%lx pthread_cond_wait: %d, %s\n", |
| 402 |
(unsigned long)pthread_self(), ret, strerror(ret)); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
void exit_failure(void) |
| 407 |
{ |
| 408 |
int nptrs; |
| 409 |
void *buffer[BT_BUF_SIZE]; |
| 410 |
|
| 411 |
nptrs = backtrace(buffer, BT_BUF_SIZE); |
| 412 |
fprintf(stderr, "\nOops! HTTPDirFS crashed! :(\n"); |
| 413 |
fprintf(stderr, "backtrace() returned the following %d addresses:\n", |
| 414 |
nptrs); |
| 415 |
backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO); |
| 416 |
|
| 417 |
exit(EXIT_FAILURE); |
| 418 |
} |
| 419 |
|
| 420 |
void erase_string(FILE *file, size_t max_len, char *s) |
| 421 |
{ |
| 422 |
size_t l = strnlen(s, max_len); |
| 423 |
for (size_t k = 0; k < l; k++) { |
| 424 |
fprintf(file, "\b"); |
| 425 |
} |
| 426 |
for (size_t k = 0; k < l; k++) { |
| 427 |
fprintf(file, " "); |
| 428 |
} |
| 429 |
for (size_t k = 0; k < l; k++) { |
| 430 |
fprintf(file, "\b"); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
char *generate_salt(void) |
| 435 |
{ |
| 436 |
char *out; |
| 437 |
out = CALLOC(SALT_LEN + 1, sizeof(char)); |
| 438 |
uuid_t uu; |
| 439 |
uuid_generate(uu); |
| 440 |
uuid_unparse(uu, out); |
| 441 |
return out; |
| 442 |
} |
| 443 |
|
| 444 |
char *generate_md5sum(const char *str) |
| 445 |
{ |
| 446 |
size_t len = strnlen(str, PATH_MAX); |
| 447 |
char *out = CALLOC(MD5_HASH_LEN + 1, sizeof(char)); |
| 448 |
|
| 449 |
EVP_MD_CTX *mdctx; |
| 450 |
unsigned char *md5_digest; |
| 451 |
unsigned int md5_digest_len = EVP_MD_size(EVP_md5()); |
| 452 |
|
| 453 |
|
| 454 |
mdctx = EVP_MD_CTX_new(); |
| 455 |
EVP_DigestInit_ex(mdctx, EVP_md5(), NULL); |
| 456 |
|
| 457 |
|
| 458 |
EVP_DigestUpdate(mdctx, str, len); |
| 459 |
|
| 460 |
|
| 461 |
md5_digest = (unsigned char *)OPENSSL_malloc(md5_digest_len); |
| 462 |
EVP_DigestFinal_ex(mdctx, md5_digest, &md5_digest_len); |
| 463 |
EVP_MD_CTX_free(mdctx); |
| 464 |
|
| 465 |
for (unsigned int i = 0; i < md5_digest_len; i++) { |
| 466 |
sprintf(out + (2 * (size_t)i), "%02x", md5_digest[i]); |
| 467 |
} |
| 468 |
|
| 469 |
OPENSSL_free(md5_digest); |
| 470 |
return out; |
| 471 |
} |
| 472 |
|
| 473 |
static void *malloc_wrapper_internal(size_t size, const char *file, |
| 474 |
const char *func, int line) |
| 475 |
{ |
| 476 |
void *ptr = malloc(size); |
| 477 |
if (size != 0 && !ptr) { |
| 478 |
fatal_log_printf(file, func, line, "%s!\n", strerror(errno)); |
| 479 |
} |
| 480 |
|
| 481 |
#ifdef DEBUG |
| 482 |
if (ptr) { |
| 483 |
MemNode *node = calloc(1, sizeof(MemNode)); |
| 484 |
if (!node) { |
| 485 |
fatal_log_printf(file, func, line, |
| 486 |
"Could not allocate MemNode: %s!\n", |
| 487 |
strerror(errno)); |
| 488 |
} |
| 489 |
node->ptr = ptr; |
| 490 |
node->size = size; |
| 491 |
node->file = file; |
| 492 |
node->func = func; |
| 493 |
node->line = line; |
| 494 |
|
| 495 |
pthread_mutex_lock(&mem_mutex); |
| 496 |
size_t idx = hash_ptr(ptr); |
| 497 |
node->next = mem_hash_table[idx]; |
| 498 |
mem_hash_table[idx] = node; |
| 499 |
pthread_mutex_unlock(&mem_mutex); |
| 500 |
} |
| 501 |
#endif |
| 502 |
|
| 503 |
return ptr; |
| 504 |
} |
| 505 |
|
| 506 |
void *CALLOC_wrapper(size_t nmemb, size_t size, const char *file, |
| 507 |
const char *func, int line) |
| 508 |
{ |
| 509 |
void *ptr = calloc(nmemb, size); |
| 510 |
if (nmemb != 0 && size != 0 && !ptr) { |
| 511 |
fatal_log_printf(file, func, line, "%s!\n", strerror(errno)); |
| 512 |
} |
| 513 |
|
| 514 |
#ifdef DEBUG |
| 515 |
if (ptr) { |
| 516 |
MemNode *node = calloc(1, sizeof(MemNode)); |
| 517 |
if (!node) { |
| 518 |
fatal_log_printf(file, func, line, |
| 519 |
"Could not allocate MemNode: %s!\n", |
| 520 |
strerror(errno)); |
| 521 |
} |
| 522 |
node->ptr = ptr; |
| 523 |
node->size = nmemb * size; |
| 524 |
node->file = file; |
| 525 |
node->func = func; |
| 526 |
node->line = line; |
| 527 |
|
| 528 |
pthread_mutex_lock(&mem_mutex); |
| 529 |
size_t idx = hash_ptr(ptr); |
| 530 |
node->next = mem_hash_table[idx]; |
| 531 |
mem_hash_table[idx] = node; |
| 532 |
pthread_mutex_unlock(&mem_mutex); |
| 533 |
} |
| 534 |
#endif |
| 535 |
|
| 536 |
return ptr; |
| 537 |
} |
| 538 |
|
| 539 |
char *STRDUP_wrapper(const char *s, const char *file, const char *func, |
| 540 |
int line) |
| 541 |
{ |
| 542 |
if (!s) { |
| 543 |
return NULL; |
| 544 |
} |
| 545 |
size_t len = strlen(s) + 1; |
| 546 |
char *ptr = malloc_wrapper_internal(len, file, func, line); |
| 547 |
memcpy(ptr, s, len); |
| 548 |
return ptr; |
| 549 |
} |
| 550 |
|
| 551 |
char *STRNDUP_wrapper(const char *s, size_t n, const char *file, |
| 552 |
const char *func, int line) |
| 553 |
{ |
| 554 |
if (!s) { |
| 555 |
return NULL; |
| 556 |
} |
| 557 |
size_t len = strnlen(s, n); |
| 558 |
char *ptr = malloc_wrapper_internal(len + 1, file, func, line); |
| 559 |
memcpy(ptr, s, len); |
| 560 |
ptr[len] = '\0'; |
| 561 |
return ptr; |
| 562 |
} |
| 563 |
|
| 564 |
void *REALLOC_wrapper(void *ptr, size_t size, const char *file, |
| 565 |
const char *func, int line) |
| 566 |
{ |
| 567 |
if (!ptr) { |
| 568 |
return malloc_wrapper_internal(size, file, func, line); |
| 569 |
} |
| 570 |
|
| 571 |
if (size == 0) { |
| 572 |
FREE_wrapper(ptr, file, func, line); |
| 573 |
return NULL; |
| 574 |
} |
| 575 |
|
| 576 |
#ifdef DEBUG |
| 577 |
|
| 578 |
pthread_mutex_lock(&mem_mutex); |
| 579 |
size_t idx = hash_ptr(ptr); |
| 580 |
MemNode **curr = &mem_hash_table[idx]; |
| 581 |
MemNode *found_node = NULL; |
| 582 |
while (*curr) { |
| 583 |
if ((*curr)->ptr == ptr) { |
| 584 |
found_node = *curr; |
| 585 |
*curr = found_node->next; |
| 586 |
break; |
| 587 |
} |
| 588 |
curr = &((*curr)->next); |
| 589 |
} |
| 590 |
|
| 591 |
if (found_node) { |
| 592 |
pthread_mutex_unlock(&mem_mutex); |
| 593 |
void *new_ptr = realloc(ptr, size); |
| 594 |
if (!new_ptr) { |
| 595 |
pthread_mutex_lock(&mem_mutex); |
| 596 |
found_node->next = mem_hash_table[idx]; |
| 597 |
mem_hash_table[idx] = found_node; |
| 598 |
pthread_mutex_unlock(&mem_mutex); |
| 599 |
|
| 600 |
fatal_log_printf(file, func, line, "realloc failed: %s!\n", |
| 601 |
strerror(errno)); |
| 602 |
return NULL; |
| 603 |
} |
| 604 |
|
| 605 |
found_node->ptr = new_ptr; |
| 606 |
found_node->size = size; |
| 607 |
found_node->file = file; |
| 608 |
found_node->func = func; |
| 609 |
found_node->line = line; |
| 610 |
|
| 611 |
pthread_mutex_lock(&mem_mutex); |
| 612 |
size_t new_idx = hash_ptr(new_ptr); |
| 613 |
found_node->next = mem_hash_table[new_idx]; |
| 614 |
mem_hash_table[new_idx] = found_node; |
| 615 |
pthread_mutex_unlock(&mem_mutex); |
| 616 |
|
| 617 |
return new_ptr; |
| 618 |
} |
| 619 |
pthread_mutex_unlock(&mem_mutex); |
| 620 |
|
| 621 |
|
| 622 |
log_printf(warning, file, func, line, "REALLOC: %p not found in tracker!\n", |
| 623 |
ptr); |
| 624 |
|
| 625 |
void *new_ptr = realloc(ptr, size); |
| 626 |
if (!new_ptr) { |
| 627 |
fatal_log_printf(file, func, line, "realloc failed: %s!\n", |
| 628 |
strerror(errno)); |
| 629 |
return NULL; |
| 630 |
} |
| 631 |
|
| 632 |
MemNode *node = calloc(1, sizeof(MemNode)); |
| 633 |
if (!node) { |
| 634 |
fatal_log_printf(file, func, line, "Could not allocate MemNode: %s!\n", |
| 635 |
strerror(errno)); |
| 636 |
} |
| 637 |
node->ptr = new_ptr; |
| 638 |
node->size = size; |
| 639 |
node->file = file; |
| 640 |
node->func = func; |
| 641 |
node->line = line; |
| 642 |
|
| 643 |
pthread_mutex_lock(&mem_mutex); |
| 644 |
size_t new_idx = hash_ptr(new_ptr); |
| 645 |
node->next = mem_hash_table[new_idx]; |
| 646 |
mem_hash_table[new_idx] = node; |
| 647 |
pthread_mutex_unlock(&mem_mutex); |
| 648 |
|
| 649 |
return new_ptr; |
| 650 |
#else |
| 651 |
void *new_ptr = realloc(ptr, size); |
| 652 |
if (!new_ptr) { |
| 653 |
fatal_log_printf(file, func, line, "realloc failed: %s!\n", |
| 654 |
strerror(errno)); |
| 655 |
} |
| 656 |
return new_ptr; |
| 657 |
#endif |
| 658 |
} |
| 659 |
|
| 660 |
char *REALPATH_wrapper(const char *path, char *resolved_path, const char *file, |
| 661 |
const char *func, int line) |
| 662 |
{ |
| 663 |
char *res = realpath(path, resolved_path); |
| 664 |
#ifdef DEBUG |
| 665 |
if (res && !resolved_path) { |
| 666 |
MemNode *node = calloc(1, sizeof(MemNode)); |
| 667 |
if (!node) { |
| 668 |
fatal_log_printf(file, func, line, |
| 669 |
"Could not allocate MemNode: %s!\n", |
| 670 |
strerror(errno)); |
| 671 |
} |
| 672 |
node->ptr = res; |
| 673 |
node->size = strlen(res) + 1; |
| 674 |
node->file = file; |
| 675 |
node->func = func; |
| 676 |
node->line = line; |
| 677 |
|
| 678 |
pthread_mutex_lock(&mem_mutex); |
| 679 |
size_t idx = hash_ptr(res); |
| 680 |
node->next = mem_hash_table[idx]; |
| 681 |
mem_hash_table[idx] = node; |
| 682 |
pthread_mutex_unlock(&mem_mutex); |
| 683 |
} |
| 684 |
#else |
| 685 |
(void)file; |
| 686 |
(void)func; |
| 687 |
(void)line; |
| 688 |
#endif |
| 689 |
return res; |
| 690 |
} |
| 691 |
|
| 692 |
void FREE_wrapper(void *ptr, const char *file, const char *func, int line) |
| 693 |
{ |
| 694 |
if (!ptr) { |
| 695 |
return; |
| 696 |
} |
| 697 |
|
| 698 |
#ifdef DEBUG |
| 699 |
pthread_mutex_lock(&mem_mutex); |
| 700 |
size_t idx = hash_ptr(ptr); |
| 701 |
MemNode **curr = &mem_hash_table[idx]; |
| 702 |
MemNode *found_node = NULL; |
| 703 |
while (*curr) { |
| 704 |
if ((*curr)->ptr == ptr) { |
| 705 |
found_node = *curr; |
| 706 |
*curr = found_node->next; |
| 707 |
break; |
| 708 |
} |
| 709 |
curr = &((*curr)->next); |
| 710 |
} |
| 711 |
pthread_mutex_unlock(&mem_mutex); |
| 712 |
|
| 713 |
if (found_node) { |
| 714 |
free(found_node); |
| 715 |
free(ptr); |
| 716 |
return; |
| 717 |
} |
| 718 |
|
| 719 |
log_printf(warning, file, func, line, "FREE: %p not found in tracker!\n", |
| 720 |
ptr); |
| 721 |
#else |
| 722 |
(void)file; |
| 723 |
(void)func; |
| 724 |
(void)line; |
| 725 |
#endif |
| 726 |
free(ptr); |
| 727 |
} |
| 728 |
|
| 729 |
void mem_cleanup(void) |
| 730 |
{ |
| 731 |
if (CONFIG.http_headers) { |
| 732 |
curl_slist_free_all(CONFIG.http_headers); |
| 733 |
CONFIG.http_headers = NULL; |
| 734 |
} |
| 735 |
|
| 736 |
#ifdef DEBUG |
| 737 |
|
| 738 |
if (ROOT_LINK_TBL) { |
| 739 |
LinkTable_free(ROOT_LINK_TBL); |
| 740 |
ROOT_LINK_TBL = NULL; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
CacheSystem_cleanup(); |
| 745 |
|
| 746 |
|
| 747 |
Config_cleanup(); |
| 748 |
|
| 749 |
|
| 750 |
pthread_mutex_lock(&mem_mutex); |
| 751 |
int leak_count = 0; |
| 752 |
for (size_t i = 0; i < MEM_HASH_SIZE; i++) { |
| 753 |
MemNode *curr = mem_hash_table[i]; |
| 754 |
while (curr) { |
| 755 |
leak_count++; |
| 756 |
curr = curr->next; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
if (leak_count > 0) { |
| 761 |
lprintf(error, "=======================================================" |
| 762 |
"===============\n"); |
| 763 |
lprintf(error, " MEMORY LEAK REPORT\n"); |
| 764 |
lprintf(error, "=======================================================" |
| 765 |
"===============\n"); |
| 766 |
for (size_t i = 0; i < MEM_HASH_SIZE; i++) { |
| 767 |
MemNode *curr = mem_hash_table[i]; |
| 768 |
while (curr) { |
| 769 |
lprintf(error, "[LEAK] Address: %p | Size: %zu bytes\n", |
| 770 |
curr->ptr, curr->size); |
| 771 |
lprintf(error, " Allocated at: %s:%d in %s()\n", |
| 772 |
curr->file, curr->line, curr->func); |
| 773 |
lprintf(error, |
| 774 |
"---------------------------------------------------" |
| 775 |
"-------------------\n"); |
| 776 |
curr = curr->next; |
| 777 |
} |
| 778 |
} |
| 779 |
lprintf(error, "Total Leaks: %d\n", leak_count); |
| 780 |
lprintf(error, "=======================================================" |
| 781 |
"===============\n"); |
| 782 |
} else { |
| 783 |
lprintf(info, "========================================================" |
| 784 |
"==============\n"); |
| 785 |
lprintf(info, "No memory leaks detected!\n"); |
| 786 |
lprintf(info, "========================================================" |
| 787 |
"==============\n"); |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
for (size_t i = 0; i < MEM_HASH_SIZE; i++) { |
| 792 |
MemNode *curr = mem_hash_table[i]; |
| 793 |
while (curr) { |
| 794 |
MemNode *next = curr->next; |
| 795 |
free(curr->ptr); |
| 796 |
free(curr); |
| 797 |
curr = next; |
| 798 |
} |
| 799 |
mem_hash_table[i] = NULL; |
| 800 |
} |
| 801 |
pthread_mutex_unlock(&mem_mutex); |
| 802 |
pthread_mutex_destroy(&mem_mutex); |
| 803 |
#endif |
| 804 |
} |
| 805 |
|
| 806 |
char *str_to_hex(char *s) |
| 807 |
{ |
| 808 |
size_t len = strnlen(s, PATH_MAX); |
| 809 |
char *hex = CALLOC((len * 2) + 1, sizeof(char)); |
| 810 |
char *h = hex; |
| 811 |
for (size_t i = 0; i < len; i++, h += 2) { |
| 812 |
snprintf(h, 3, "%02x", (unsigned char)s[i]); |
| 813 |
} |
| 814 |
return hex; |
| 815 |
} |