diff options
Diffstat (limited to 'src/ot_accesslist.c')
| -rw-r--r-- | src/ot_accesslist.c | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/src/ot_accesslist.c b/src/ot_accesslist.c index 86f9a7d..5d4f059 100644 --- a/src/ot_accesslist.c +++ b/src/ot_accesslist.c | |||
| @@ -33,7 +33,7 @@ const char *g_accesslist_filename = NULL; | |||
| 33 | const char *g_accesslist_pipe_add = NULL; | 33 | const char *g_accesslist_pipe_add = NULL; |
| 34 | const char *g_accesslist_pipe_delete = NULL; | 34 | const char *g_accesslist_pipe_delete = NULL; |
| 35 | #endif | 35 | #endif |
| 36 | static pthread_mutex_t g_accesslist_mutex; | 36 | static pthread_mutex_t g_accesslist_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 37 | 37 | ||
| 38 | /* Accesslists are lock free linked lists. We can not make them locking, because every announce | 38 | /* Accesslists are lock free linked lists. We can not make them locking, because every announce |
| 39 | would try to acquire the mutex, making it the most contested mutex in the whole of opentracker, | 39 | would try to acquire the mutex, making it the most contested mutex in the whole of opentracker, |
| @@ -91,7 +91,7 @@ static ot_accesslist *accesslist_make(ot_accesslist *next, size_t size) { | |||
| 91 | This will never delete head, because that might still be in use. */ | 91 | This will never delete head, because that might still be in use. */ |
| 92 | static void accesslist_clean(ot_accesslist *accesslist) { | 92 | static void accesslist_clean(ot_accesslist *accesslist) { |
| 93 | while (accesslist && accesslist->next) { | 93 | while (accesslist && accesslist->next) { |
| 94 | if (accesslist->next->base + 5 < time_now_minutes()) | 94 | if (time_since_minutes(accesslist->next->base) > 5) |
| 95 | accesslist->next = accesslist_free(accesslist->next); | 95 | accesslist->next = accesslist_free(accesslist->next); |
| 96 | accesslist = accesslist->next; | 96 | accesslist = accesslist->next; |
| 97 | } | 97 | } |
| @@ -257,15 +257,14 @@ static void *accesslist_worker(void *args) { | |||
| 257 | (void)args; | 257 | (void)args; |
| 258 | 258 | ||
| 259 | while (1) { | 259 | while (1) { |
| 260 | /* Wait for signals */ | ||
| 261 | while (sigwait(&signal_mask, &sig) != 0) | ||
| 262 | ; | ||
| 263 | |||
| 260 | if (!g_opentracker_running) | 264 | if (!g_opentracker_running) |
| 261 | return NULL; | 265 | return NULL; |
| 262 | 266 | ||
| 263 | /* Initial attempt to read accesslist */ | ||
| 264 | accesslist_readfile(); | 267 | accesslist_readfile(); |
| 265 | |||
| 266 | /* Wait for signals */ | ||
| 267 | while (sigwait(&signal_mask, &sig) != 0 && sig != SIGHUP) | ||
| 268 | ; | ||
| 269 | } | 268 | } |
| 270 | return NULL; | 269 | return NULL; |
| 271 | } | 270 | } |
| @@ -282,8 +281,7 @@ static void *accesslist_adddel_worker(const char *fifoname, ot_accesslist *_ | |||
| 282 | return NULL; | 281 | return NULL; |
| 283 | } | 282 | } |
| 284 | } else { | 283 | } else { |
| 285 | int error = mkfifo(fifoname, 0755); | 284 | if (mkfifo(fifoname, 0600) && errno != EEXIST) { |
| 286 | if (error && error != EEXIST) { | ||
| 287 | fprintf(stderr, "Error when starting dynamic accesslists: Couldn't create FIFO at %s, error: %s\n", fifoname, strerror(errno)); | 285 | fprintf(stderr, "Error when starting dynamic accesslists: Couldn't create FIFO at %s, error: %s\n", fifoname, strerror(errno)); |
| 288 | return NULL; | 286 | return NULL; |
| 289 | } | 287 | } |
| @@ -354,11 +352,17 @@ static void *accesslist_adddel_worker(const char *fifoname, ot_accesslist *_ | |||
| 354 | /* Only if the info hash is not in the adding_to list, create a new head with that entry */ | 352 | /* Only if the info hash is not in the adding_to list, create a new head with that entry */ |
| 355 | if (!exactmatch) { | 353 | if (!exactmatch) { |
| 356 | ot_accesslist *accesslist_new = accesslist_make(*adding_to, (*adding_to)->size + 1); | 354 | ot_accesslist *accesslist_new = accesslist_make(*adding_to, (*adding_to)->size + 1); |
| 357 | ptrdiff_t off = insert_point - (*adding_to)->list; | ||
| 358 | if (accesslist_new) { | 355 | if (accesslist_new) { |
| 359 | memcpy(accesslist_new->list, (*adding_to)->list, sizeof(ot_hash) * off); | 356 | ptrdiff_t off = (*adding_to)->size ? insert_point - (*adding_to)->list : 0; |
| 357 | size_t after = (*adding_to)->size - off; | ||
| 358 | |||
| 359 | /* A reload leaves an empty list with no array at all, and memcpy | ||
| 360 | may not be handed a null pointer even for nothing */ | ||
| 361 | if (off) | ||
| 362 | memcpy(accesslist_new->list, (*adding_to)->list, sizeof(ot_hash) * off); | ||
| 360 | memcpy(accesslist_new->list + off, info_hash, sizeof(info_hash)); | 363 | memcpy(accesslist_new->list + off, info_hash, sizeof(info_hash)); |
| 361 | memcpy(accesslist_new->list + off + 1, (*adding_to)->list + off, sizeof(ot_hash) * ((*adding_to)->size - off)); | 364 | if (after) |
| 365 | memcpy(accesslist_new->list + off + 1, (*adding_to)->list + off, sizeof(ot_hash) * after); | ||
| 362 | (*adding_to)->base = time_now_minutes(); | 366 | (*adding_to)->base = time_now_minutes(); |
| 363 | *adding_to = accesslist_new; | 367 | *adding_to = accesslist_new; |
| 364 | } | 368 | } |
| @@ -405,13 +409,16 @@ static void accesslist_stop_fifo_worker(pthread_t thread, _Atomic int *left, con | |||
| 405 | 409 | ||
| 406 | static pthread_t thread_id; | 410 | static pthread_t thread_id; |
| 407 | void accesslist_init(void) { | 411 | void accesslist_init(void) { |
| 408 | pthread_mutex_init(&g_accesslist_mutex, NULL); | 412 | /* Everything that consults the list starts after this returns */ |
| 409 | pthread_create(&thread_id, NULL, accesslist_worker, NULL); | 413 | accesslist_readfile(); |
| 414 | |||
| 415 | if (pthread_create(&thread_id, NULL, accesslist_worker, NULL)) | ||
| 416 | exerr("Cannot create accesslist worker thread."); | ||
| 410 | #ifdef WANT_DYNAMIC_ACCESSLIST | 417 | #ifdef WANT_DYNAMIC_ACCESSLIST |
| 411 | if (g_accesslist_pipe_add) | 418 | if (g_accesslist_pipe_add && pthread_create(&thread_adder_id, NULL, accesslist_adder_worker, NULL)) |
| 412 | pthread_create(&thread_adder_id, NULL, accesslist_adder_worker, NULL); | 419 | exerr("Cannot create accesslist adder thread."); |
| 413 | if (g_accesslist_pipe_delete) | 420 | if (g_accesslist_pipe_delete && pthread_create(&thread_deleter_id, NULL, accesslist_deleter_worker, NULL)) |
| 414 | pthread_create(&thread_deleter_id, NULL, accesslist_deleter_worker, NULL); | 421 | exerr("Cannot create accesslist deleter thread."); |
| 415 | #endif | 422 | #endif |
| 416 | } | 423 | } |
| 417 | 424 | ||
| @@ -436,7 +443,6 @@ void accesslist_deinit(void) { | |||
| 436 | #endif | 443 | #endif |
| 437 | 444 | ||
| 438 | pthread_mutex_unlock(&g_accesslist_mutex); | 445 | pthread_mutex_unlock(&g_accesslist_mutex); |
| 439 | pthread_mutex_destroy(&g_accesslist_mutex); | ||
| 440 | } | 446 | } |
| 441 | 447 | ||
| 442 | void accesslist_cleanup(void) { | 448 | void accesslist_cleanup(void) { |
