-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_thread.c
138 lines (122 loc) · 3.46 KB
/
logger_thread.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
ssid-logger is a simple software to log SSID you encounter in your vicinity
Copyright © 2020-2022 solsTiCe d'Hiver
*/
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <sqlite3.h>
#include <time.h>
#include <stdbool.h>
#include <unistd.h>
#ifdef HAS_SYS_PRCTL_H
#include <sys/prctl.h>
#endif
#include <semaphore.h>
#include <libwifi.h>
#include "parsers.h"
#include "queue.h"
#include "logger_thread.h"
#include "gps_thread.h"
#include "db.h"
#include "lruc.h"
#include "config.h"
extern pthread_mutex_t mutex_queue;
extern pthread_mutex_t mutex_gloc;
extern queue_t *queue;
extern sem_t queue_empty;
extern sem_t queue_full;
extern struct gps_loc gloc; // global variable to hold retrieved gps data
extern sqlite3 *db;
struct timespec start_ts_cache;
extern bool format_csv;
extern option_gps_t option_gps;
extern FILE *file_ptr;
lruc *authmode_pk_cache = NULL, *ap_pk_cache = NULL;
void cleanup_caches(void *arg)
{
lruc_free(authmode_pk_cache);
lruc_free(ap_pk_cache);
return;
}
// worker thread that will process the queue filled by process_packet()
void *process_queue(void *args)
{
struct libwifi_bss *bss;
struct timespec now;
struct gps_loc tmp_gloc;
#ifdef HAS_SYS_PRCTL_H
// name our thread; using prctl instead of pthread_setname_np to avoid defining _GNU_SOURCE
prctl(PR_SET_NAME, "logger");
#endif
// init caches
authmode_pk_cache = lruc_new(AUTHMODE_CACHE_SIZE, 1);
ap_pk_cache = lruc_new(AP_CACHE_SIZE, 1);
// push cleanup code when exiting thread
pthread_cleanup_push(cleanup_caches, NULL);
clock_gettime(CLOCK_MONOTONIC, &now);
start_ts_cache = now;
while (true) {
sem_wait(&queue_empty);
pthread_mutex_lock(&mutex_queue);
bss = (struct libwifi_bss *) dequeue(queue);
pthread_mutex_unlock(&mutex_queue);
sem_post(&queue_full);
// process the bss
pthread_mutex_lock(&mutex_gloc);
tmp_gloc = gloc;
pthread_mutex_unlock(&mutex_gloc);
bool log = false;
// process gps data in gloc
if (option_gps == GPS_LOG_ZERO) {
tmp_gloc.lat = tmp_gloc.lon = tmp_gloc.alt = tmp_gloc.acc = 0.0;
// use system time because we can't use gps fix time
clock_gettime(CLOCK_REALTIME, &now);
tmp_gloc.ftime = now;
log = true;
} else {
if (!tmp_gloc.updated) {
if (option_gps == GPS_LOG_ONZ) {
log = false;
} else {
tmp_gloc.lat = tmp_gloc.lon = tmp_gloc.alt = tmp_gloc.acc = 0.0;
log = true;
}
} else {
log = true;
}
}
if (log) {
if (option_gps == GPS_LOG_ONZ && tmp_gloc.lat == 0.0 && tmp_gloc.lon == 0.0) {
// that's useless and against what is intended: GPS_LOG_ONZ means only log non zero
goto nolog;
}
if (!format_csv) {
insert_beacon(*bss, tmp_gloc, db, authmode_pk_cache, ap_pk_cache);
} else {
char *tmp = bss_to_str(*bss, tmp_gloc);
fprintf(file_ptr, "%s\n", tmp);
free(tmp);
}
}
nolog:
libwifi_free_bss(bss);
// commit our data if time elapsed is greater than DB_CACHE_TIME
clock_gettime(CLOCK_MONOTONIC, &now);
if (now.tv_sec - start_ts_cache.tv_sec >= DB_CACHE_TIME) {
if (format_csv) {
fflush(file_ptr);
fsync(fileno(file_ptr)); // over-kill ?
} else {
// commit to db
commit_txn(db);
begin_txn(db);
}
start_ts_cache = now;
}
}
pthread_cleanup_pop(1);
return NULL;
}