From 227000cf9713ed3a654899320763f0505c2d9f01 Mon Sep 17 00:00:00 2001 From: 0x35c <> Date: Thu, 29 May 2025 15:01:57 +0200 Subject: [PATCH] feature: use pthread_join() instead of pthread_detach and weird counter thing --- src/thread.c | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/src/thread.c b/src/thread.c index 27815f7..520f690 100644 --- a/src/thread.c +++ b/src/thread.c @@ -12,9 +12,7 @@ #include "scan.h" #include "thread.h" -uint8_t g_nb_threads; bool g_start = false; -pthread_mutex_t g_nb_threads_mtx; pthread_mutex_t g_start_mtx; void *routine(void *p_data) @@ -49,24 +47,20 @@ void *routine(void *p_data) printf("%d has state: %d\n", port, scan_data.response->state); } - pthread_mutex_lock(&g_nb_threads_mtx); - g_nb_threads--; - pthread_mutex_unlock(&g_nb_threads_mtx); - return NULL; } static struct thread *init_threads_data(const struct option_lst *options, char *ip_addr, const struct host *host, struct response *responses, - uint8_t g_nb_threads) + uint8_t nb_threads) { - struct thread *threads = malloc(sizeof(struct thread) * g_nb_threads); + struct thread *threads = malloc(sizeof(struct thread) * nb_threads); if (!threads) { dprintf(2, "ft_nmap: allocation of threads failed\n"); return NULL; } - for (uint8_t i = 0; i < g_nb_threads; i++) { + for (uint8_t i = 0; i < nb_threads; i++) { memcpy(&threads[i].host, host, sizeof(struct host)); const char *ports = get_option_arg(options, FL_PORTS); if (parse_ports(ports, &threads[i].port_start, @@ -108,24 +102,22 @@ int create_threads(const struct option_lst *options, char *ip_addr, return 0; } - g_nb_threads = atoi(arg); + uint8_t nb_threads = atoi(arg); struct thread *threads_data = - init_threads_data(options, ip_addr, &host, responses, g_nb_threads); + init_threads_data(options, ip_addr, &host, responses, nb_threads); if (!threads_data) return -1; - pthread_t *threads = malloc(sizeof(pthread_t) * g_nb_threads); + pthread_t *threads = malloc(sizeof(pthread_t) * nb_threads); if (!threads) { free(threads_data); return -1; } - pthread_mutex_init(&g_nb_threads_mtx, NULL); pthread_mutex_init(&g_start_mtx, NULL); - const uint16_t ports_per_thread = - (port_end - port_start) / g_nb_threads; - uint16_t remaining_ports = (port_end - port_start) % g_nb_threads; - for (uint8_t i = 0; i < g_nb_threads; i++) { + const uint16_t ports_per_thread = (port_end - port_start) / nb_threads; + uint16_t remaining_ports = (port_end - port_start) % nb_threads; + for (uint8_t i = 0; i < nb_threads; i++) { threads_data[i].port_start = port_start + i * ports_per_thread; threads_data[i].port_end = port_start + (i + 1) * ports_per_thread + @@ -139,19 +131,16 @@ int create_threads(const struct option_lst *options, char *ip_addr, dprintf(2, "ft_nmap: error during pthread_create()\n"); return -1; } - pthread_detach(threads[i]); } pthread_mutex_lock(&g_start_mtx); g_start = true; pthread_mutex_unlock(&g_start_mtx); - while (1) { - pthread_mutex_lock(&g_nb_threads_mtx); - bool nb_threads = g_nb_threads; - pthread_mutex_unlock(&g_nb_threads_mtx); - if (nb_threads == 0) - break; - usleep(100); + for (uint8_t i = 0; i < nb_threads; i++) { + if (pthread_join(threads[i], NULL)) { + dprintf(2, "ft_nmap: error during pthread_join()\n"); + return -1; + } } free(threads_data); free(threads);