| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifndef UTIL_H |
| 25 |
#define UTIL_H |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <pthread.h> |
| 32 |
#include <semaphore.h> |
| 33 |
#include <stdint.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
|
| 38 |
#ifdef __APPLE__ |
| 39 |
typedef struct { |
| 40 |
pthread_mutex_t mutex; |
| 41 |
pthread_cond_t cond; |
| 42 |
int count; |
| 43 |
} sys_sem_t; |
| 44 |
#else |
| 45 |
typedef sem_t sys_sem_t; |
| 46 |
#endif |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
char *path_append(const char *path, const char *filename); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
void pthread_mutex_init_wrapper(pthread_mutex_t *x, |
| 60 |
const pthread_mutexattr_t *attr, |
| 61 |
const char *file, const char *func, int line); |
| 62 |
#define PTHREAD_MUTEX_INIT(x, attr) \ |
| 63 |
pthread_mutex_init_wrapper(x, attr, __FILE__, __func__, __LINE__) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
void pthread_mutex_destroy_wrapper(pthread_mutex_t *x, const char *file, |
| 69 |
const char *func, int line); |
| 70 |
#define PTHREAD_MUTEX_DESTROY(x) \ |
| 71 |
pthread_mutex_destroy_wrapper(x, __FILE__, __func__, __LINE__) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
void pthread_mutex_lock_wrapper(const char *file, const char *func, int line, |
| 77 |
pthread_mutex_t *x); |
| 78 |
#define PTHREAD_MUTEX_LOCK(x) \ |
| 79 |
pthread_mutex_lock_wrapper(__FILE__, __func__, __LINE__, x) |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
void pthread_mutex_unlock_wrapper(const char *file, const char *func, int line, |
| 85 |
pthread_mutex_t *x); |
| 86 |
#define PTHREAD_MUTEX_UNLOCK(x) \ |
| 87 |
pthread_mutex_unlock_wrapper(__FILE__, __func__, __LINE__, x) |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
void sem_init_wrapper(sys_sem_t *sem, int pshared, unsigned int value, |
| 93 |
const char *file, const char *func, int line); |
| 94 |
#define SEM_INIT(sem, pshared, value) \ |
| 95 |
sem_init_wrapper(sem, pshared, value, __FILE__, __func__, __LINE__) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
void sem_destroy_wrapper(sys_sem_t *sem, const char *file, const char *func, |
| 101 |
int line); |
| 102 |
#define SEM_DESTROY(sem) sem_destroy_wrapper(sem, __FILE__, __func__, __LINE__) |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
void sem_wait_wrapper(const char *file, const char *func, int line, |
| 108 |
sys_sem_t *sem); |
| 109 |
#define SEM_WAIT(sem) sem_wait_wrapper(__FILE__, __func__, __LINE__, sem) |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
void sem_post_wrapper(const char *file, const char *func, int line, |
| 115 |
sys_sem_t *sem); |
| 116 |
#define SEM_POST(sem) sem_post_wrapper(__FILE__, __func__, __LINE__, sem) |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
int sys_sem_trywait_wrapper(const char *file, const char *func, int line, |
| 123 |
sys_sem_t *sem); |
| 124 |
#define SEM_TRYWAIT(sem) \ |
| 125 |
sys_sem_trywait_wrapper(__FILE__, __func__, __LINE__, sem) |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
void pthread_cond_init_wrapper(pthread_cond_t *cond, |
| 131 |
const pthread_condattr_t *attr, const char *file, |
| 132 |
const char *func, int line); |
| 133 |
#define PTHREAD_COND_INIT(cond, attr) \ |
| 134 |
pthread_cond_init_wrapper(cond, attr, __FILE__, __func__, __LINE__) |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
void pthread_cond_destroy_wrapper(pthread_cond_t *cond, const char *file, |
| 140 |
const char *func, int line); |
| 141 |
#define PTHREAD_COND_DESTROY(cond) \ |
| 142 |
pthread_cond_destroy_wrapper(cond, __FILE__, __func__, __LINE__) |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
void pthread_cond_broadcast_wrapper(const char *file, const char *func, |
| 148 |
int line, pthread_cond_t *cond); |
| 149 |
#define PTHREAD_COND_BROADCAST(cond) \ |
| 150 |
pthread_cond_broadcast_wrapper(__FILE__, __func__, __LINE__, cond) |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
void pthread_cond_wait_wrapper(const char *file, const char *func, int line, |
| 156 |
pthread_cond_t *cond, pthread_mutex_t *mutex); |
| 157 |
#define PTHREAD_COND_WAIT(cond, mutex) \ |
| 158 |
pthread_cond_wait_wrapper(__FILE__, __func__, __LINE__, cond, mutex) |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
_Noreturn void exit_failure(void); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
void erase_string(FILE *file, size_t max_len, char *s); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
char *generate_salt(void); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
char *generate_md5sum(const char *str); |
| 183 |
|
| 184 |
#ifdef DEBUG |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
typedef struct MemNode { |
| 190 |
void *ptr; |
| 191 |
size_t size; |
| 192 |
const char *file; |
| 193 |
const char *func; |
| 194 |
int line; |
| 195 |
struct MemNode *next; |
| 196 |
} MemNode; |
| 197 |
|
| 198 |
#endif |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
void *CALLOC_wrapper(size_t nmemb, size_t size, const char *file, |
| 204 |
const char *func, int line); |
| 205 |
#define CALLOC(nmemb, size) \ |
| 206 |
CALLOC_wrapper(nmemb, size, __FILE__, __func__, __LINE__) |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
void FREE_wrapper(void *ptr, const char *file, const char *func, int line); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
#define FREE(ptr) \ |
| 224 |
do { \ |
| 225 |
__extension__ __auto_type __ptr_to_free = &(ptr); \ |
| 226 |
FREE_wrapper((void *)*__ptr_to_free, __FILE__, __func__, __LINE__); \ |
| 227 |
*__ptr_to_free = NULL; \ |
| 228 |
} while (0) |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
char *STRDUP_wrapper(const char *s, const char *file, const char *func, |
| 234 |
int line); |
| 235 |
#define STRDUP(s) STRDUP_wrapper(s, __FILE__, __func__, __LINE__) |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
char *STRNDUP_wrapper(const char *s, size_t n, const char *file, |
| 241 |
const char *func, int line); |
| 242 |
#define STRNDUP(s, n) STRNDUP_wrapper(s, n, __FILE__, __func__, __LINE__) |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
void *REALLOC_wrapper(void *ptr, size_t size, const char *file, |
| 248 |
const char *func, int line); |
| 249 |
#define REALLOC(ptr, size) \ |
| 250 |
REALLOC_wrapper(ptr, size, __FILE__, __func__, __LINE__) |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
char *REALPATH_wrapper(const char *path, char *resolved_path, const char *file, |
| 256 |
const char *func, int line); |
| 257 |
#define REALPATH(path, resolved_path) \ |
| 258 |
REALPATH_wrapper(path, resolved_path, __FILE__, __func__, __LINE__) |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
void mem_cleanup(void); |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
char *str_to_hex(char *s); |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
void Config_init(void); |
| 274 |
|
| 275 |
#endif |