feature: use pthread_join() instead of pthread_detach and weird counter thing
This commit is contained in:
parent
a94bbf0671
commit
227000cf97
39
src/thread.c
39
src/thread.c
@ -12,9 +12,7 @@
|
|||||||
#include "scan.h"
|
#include "scan.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
|
||||||
uint8_t g_nb_threads;
|
|
||||||
bool g_start = false;
|
bool g_start = false;
|
||||||
pthread_mutex_t g_nb_threads_mtx;
|
|
||||||
pthread_mutex_t g_start_mtx;
|
pthread_mutex_t g_start_mtx;
|
||||||
|
|
||||||
void *routine(void *p_data)
|
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);
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct thread *init_threads_data(const struct option_lst *options,
|
static struct thread *init_threads_data(const struct option_lst *options,
|
||||||
char *ip_addr, const struct host *host,
|
char *ip_addr, const struct host *host,
|
||||||
struct response *responses,
|
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) {
|
if (!threads) {
|
||||||
dprintf(2, "ft_nmap: allocation of threads failed\n");
|
dprintf(2, "ft_nmap: allocation of threads failed\n");
|
||||||
return NULL;
|
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));
|
memcpy(&threads[i].host, host, sizeof(struct host));
|
||||||
const char *ports = get_option_arg(options, FL_PORTS);
|
const char *ports = get_option_arg(options, FL_PORTS);
|
||||||
if (parse_ports(ports, &threads[i].port_start,
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_nb_threads = atoi(arg);
|
uint8_t nb_threads = atoi(arg);
|
||||||
struct thread *threads_data =
|
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)
|
if (!threads_data)
|
||||||
return -1;
|
return -1;
|
||||||
pthread_t *threads = malloc(sizeof(pthread_t) * g_nb_threads);
|
pthread_t *threads = malloc(sizeof(pthread_t) * nb_threads);
|
||||||
if (!threads) {
|
if (!threads) {
|
||||||
free(threads_data);
|
free(threads_data);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_init(&g_nb_threads_mtx, NULL);
|
|
||||||
pthread_mutex_init(&g_start_mtx, NULL);
|
pthread_mutex_init(&g_start_mtx, NULL);
|
||||||
|
|
||||||
const uint16_t ports_per_thread =
|
const uint16_t ports_per_thread = (port_end - port_start) / nb_threads;
|
||||||
(port_end - port_start) / g_nb_threads;
|
uint16_t remaining_ports = (port_end - port_start) % nb_threads;
|
||||||
uint16_t remaining_ports = (port_end - port_start) % g_nb_threads;
|
for (uint8_t i = 0; i < nb_threads; i++) {
|
||||||
for (uint8_t i = 0; i < g_nb_threads; i++) {
|
|
||||||
threads_data[i].port_start = port_start + i * ports_per_thread;
|
threads_data[i].port_start = port_start + i * ports_per_thread;
|
||||||
threads_data[i].port_end = port_start +
|
threads_data[i].port_end = port_start +
|
||||||
(i + 1) * ports_per_thread +
|
(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");
|
dprintf(2, "ft_nmap: error during pthread_create()\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
pthread_detach(threads[i]);
|
|
||||||
}
|
}
|
||||||
pthread_mutex_lock(&g_start_mtx);
|
pthread_mutex_lock(&g_start_mtx);
|
||||||
g_start = true;
|
g_start = true;
|
||||||
pthread_mutex_unlock(&g_start_mtx);
|
pthread_mutex_unlock(&g_start_mtx);
|
||||||
|
|
||||||
while (1) {
|
for (uint8_t i = 0; i < nb_threads; i++) {
|
||||||
pthread_mutex_lock(&g_nb_threads_mtx);
|
if (pthread_join(threads[i], NULL)) {
|
||||||
bool nb_threads = g_nb_threads;
|
dprintf(2, "ft_nmap: error during pthread_join()\n");
|
||||||
pthread_mutex_unlock(&g_nb_threads_mtx);
|
return -1;
|
||||||
if (nb_threads == 0)
|
}
|
||||||
break;
|
|
||||||
usleep(100);
|
|
||||||
}
|
}
|
||||||
free(threads_data);
|
free(threads_data);
|
||||||
free(threads);
|
free(threads);
|
||||||
|
Loading…
Reference in New Issue
Block a user