Debian Codegraph @

src/progress.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 2023-2025 Hannes von Haugwitz
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "config.h"
22 #include <pthread.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <math.h>
28 #include <sys/ioctl.h>
29 #include <unistd.h>
30
31 #include "aide.h"
32 #include "progress.h"
33 #include "log.h"
34 #include "errorcodes.h"
35 #include "util.h"
36
37 #define BILLION 1000000000L
38
39 pthread_t progress_updater_thread = 0LU;
40 pthread_mutex_t progress_update_mutex = PTHREAD_MUTEX_INITIALIZER;
41
42 static progress_state state = PROGRESS_NONE;
43 static struct timespec time_start;
44 static long unsigned num_entries = 0;
45 static long unsigned num_skipped = 0LU;
46 static char* path = NULL;
47
48 static char *get_state_string(progress_state s) {
49 switch (s) {
50 case PROGRESS_OLDDB:
51 return "read old db";
52 case PROGRESS_NEWDB:
53 return "read new db";
54 case PROGRESS_DISK:
55 return "scan file system";
56 case PROGRESS_WRITEDB:
57 return "write db";
58 case PROGRESS_CONFIG:
59 return "parse config";
60 case PROGRESS_SKIPPED:
61 case PROGRESS_CLEAR:
62 case PROGRESS_NONE:
63 return NULL;
64 }
65 return NULL;
66 }
67
68 static void * progress_updater( __attribute__((unused)) void *arg) {
69 const char *whoami = "(progress)";
70 log_msg(LOG_LEVEL_THREAD, "%10s: initialized progress_updater thread", whoami);
71
72 mask_sig(whoami);
73
74 bool _continue = true;
75 while (_continue) {
76 pthread_mutex_lock(&progress_update_mutex);
77 time_t now = time(NULL);
78 int elapsed = (unsigned long) now - (unsigned long) conf->start_time;
79 char *progress_bar = NULL;
80 switch (state) {
81 case PROGRESS_CONFIG:
82 case PROGRESS_NEWDB:
83 case PROGRESS_SKIPPED:
84 case PROGRESS_DISK:
85 case PROGRESS_WRITEDB:
86 case PROGRESS_OLDDB:
87 progress_bar = get_progress_bar_string(get_state_string(state), path, num_entries, num_skipped, elapsed, conf->progress);
88 stderr_msg("%s\r", progress_bar);
89 free(progress_bar);
90 progress_bar = NULL;
91 break;
92 case PROGRESS_CLEAR:
93 _continue = false;
94 break;
95 case PROGRESS_NONE:
96 /* do nothing */
97 ;;
98
99 }
100 pthread_mutex_unlock(&progress_update_mutex);
101 usleep(100000);
102 }
103 return (void *) pthread_self();
104 }
105
106 static void update_state(progress_state new_state) {
107 LOG_LEVEL log_level = LOG_LEVEL_INFO;
108
109 struct timespec time_now;
110 clock_gettime( CLOCK_REALTIME, &time_now);
111
112 double elapsed = (time_now.tv_sec - time_start.tv_sec) + (time_now.tv_nsec - time_start.tv_nsec)/(double)BILLION;
113
114 long elapsed_minutes = (long)floor(elapsed)/60;
115 double elapsed_seconds = elapsed - elapsed_minutes*60;
116
117 unsigned long performance = (num_entries+num_skipped)/elapsed;
118 char * entries_string = num_entries == 1 ? "entry" : "entries";
119
120 char *skipped_str = NULL;
121 if (num_skipped) {
122 const char *skipped_format = " (%lu skipped)";
123 int n = snprintf(NULL, 0, skipped_format, num_skipped);
124 skipped_str = checked_malloc(n+1);
125 snprintf(skipped_str, n+1, skipped_format, num_skipped);
126 }
127 switch (state) {
128 case PROGRESS_OLDDB:
129 log_msg(log_level, "read %lu %s%s [%lu entries/s] from %s in %ldm %.4lfs", num_entries, entries_string, skipped_str?skipped_str:"", performance, (conf->database_in.url)->raw, elapsed_minutes, elapsed_seconds);
130 break;
131 case PROGRESS_NEWDB:
132 log_msg(log_level, "read %lu %s%s [%lu entries/s] from %s in %ldm %.4lfs", num_entries, entries_string, skipped_str?skipped_str:"", performance, (conf->database_new.url)->raw, elapsed_minutes, elapsed_seconds);
133 break;
134 case PROGRESS_DISK:
135 log_msg(log_level, "read %lu %s [%lu entries/s] from file system in %ldm %.4lfs", num_entries, entries_string, performance, elapsed_minutes, elapsed_seconds);
136 break;
137 case PROGRESS_CONFIG:
138 log_msg(log_level, "parsed %lu config %s [%lu files/s] in %ldm %.4lfs", num_entries, num_entries == 1 ? "file" : "files", performance, elapsed_minutes, elapsed_seconds);
139 break;
140 case PROGRESS_WRITEDB:
141 log_msg(log_level, "wrote %lu %s [%lu entries/s] to %s in %ldm %.4lfs", num_entries, entries_string, performance, (conf->database_out.url)->raw, elapsed_minutes, elapsed_seconds);
142 break;
143 case PROGRESS_SKIPPED:
144 case PROGRESS_CLEAR:
145 case PROGRESS_NONE:
146 /* no logging */
147 break;
148 }
149 if (skipped_str) {
150 free(skipped_str);
151 skipped_str = NULL;
152 }
153 time_start = time_now;
154 state = new_state;
155 }
156
157 bool progress_start(void) {
158 struct winsize winsize;
159
160 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &winsize) == -1) {
161 conf->progress = 80;
162 } else {
163 conf->progress = winsize.ws_col;
164 }
165 if (pthread_create(&progress_updater_thread, NULL, &progress_updater, NULL) != 0) {
166 log_msg(LOG_LEVEL_WARNING, "failed to start progress_updater thread (disable progress bar)");
167 return false;
168 }
169 stderr_set_line_erasure(true);
170 return true;
171 }
172
173 void progress_stop(void) {
174 pthread_mutex_lock(&progress_update_mutex);
175 update_state(PROGRESS_CLEAR);
176 stderr_set_line_erasure(false);
177 pthread_mutex_unlock(&progress_update_mutex);
178 if (progress_updater_thread) {
179 if (pthread_join(progress_updater_thread, NULL) != 0) {
180 log_msg(LOG_LEVEL_ERROR, "failed to join progress_updater thread");
181 exit(THREAD_ERROR);
182 }
183 log_msg(LOG_LEVEL_THREAD, "%10s: progress_updater thread finished", "(main)");
184 }
185 }
186
187 void progress_status(progress_state new_state, const char* data) {
188 pthread_mutex_lock(&progress_update_mutex);
189 switch (new_state) {
190 case PROGRESS_CONFIG:
191 case PROGRESS_OLDDB:
192 case PROGRESS_NEWDB:
193 case PROGRESS_DISK:
194 case PROGRESS_WRITEDB:
195 if (state == new_state) {
196 num_entries++;
197 } else {
198 update_state(new_state);
199 num_entries = 0LU;
200 num_skipped = 0LU;
201 }
202 free(path);
203 path = NULL;
204 if (data) {
205 path = stresc(data);
206 }
207 break;
208 case PROGRESS_SKIPPED:
209 num_skipped++;
210 break;
211 case PROGRESS_CLEAR:
212 case PROGRESS_NONE:
213 update_state(new_state);
214 break;
215 }
216 pthread_mutex_unlock(&progress_update_mutex);
217 }