| 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"cxx . . $ `<file>/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"cxx . . $ `<file>/include/aide.h`/ |
| 32 | #include "progress.h"cxx . . $ `<file>/include/progress.h`/ |
| 33 | #include "log.h"cxx . . $ `<file>/include/log.h`/ |
| 34 | #include "errorcodes.h"cxx . . $ `<file>/include/errorcodes.h`/ |
| 35 | #include "util.h"cxx . . $ `<file>/include/util.h`/ |
| 36 | |
| 37 | #define BILLIONcxx . . $ `src/progress.c:37:9`! 1000000000L |
| 38 | |
| 39 | pthread_tcxx . . $ pthread_t# progress_updater_threadcxx . . $ progress_updater_thread. = 0LU; |
| 40 | pthread_mutex_tcxx . . $ pthread_mutex_t# progress_update_mutexcxx . . $ progress_update_mutex. = PTHREAD_MUTEX_INITIALIZERcxx . . $ `<external>/14712363683955484316/pthread.h:90:9`!; |
| 41 | |
| 42 | static progress_statecxx . . $ progress_state# statecxx . . $ state. = PROGRESS_NONEcxx . . $ PROGRESS_NONE.; |
| 43 | static struct timespeccxx . . $ timespec# time_startcxx . . $ time_start.; |
| 44 | static long unsigned num_entriescxx . . $ num_entries. = 0; |
| 45 | static long unsigned num_skippedcxx . . $ num_skipped. = 0LU; |
| 46 | static char* pathcxx . . $ path. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 47 | |
| 48 | static char *get_state_stringcxx . . $ get_state_string(2f101243134cb539).(progress_statecxx . . $ progress_state# s) { |
| 49 | switch (s) { |
| 50 | case PROGRESS_OLDDBcxx . . $ PROGRESS_OLDDB.: |
| 51 | return "read old db"; |
| 52 | case PROGRESS_NEWDBcxx . . $ PROGRESS_NEWDB.: |
| 53 | return "read new db"; |
| 54 | case PROGRESS_DISKcxx . . $ PROGRESS_DISK.: |
| 55 | return "scan file system"; |
| 56 | case PROGRESS_WRITEDBcxx . . $ PROGRESS_WRITEDB.: |
| 57 | return "write db"; |
| 58 | case PROGRESS_CONFIGcxx . . $ PROGRESS_CONFIG.: |
| 59 | return "parse config"; |
| 60 | case PROGRESS_SKIPPEDcxx . . $ PROGRESS_SKIPPED.: |
| 61 | case PROGRESS_CLEARcxx . . $ PROGRESS_CLEAR.: |
| 62 | case PROGRESS_NONEcxx . . $ PROGRESS_NONE.: |
| 63 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 64 | } |
| 65 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 66 | } |
| 67 | |
| 68 | static void * progress_updatercxx . . $ progress_updater(9f162e79bfc6c74).( __attribute__((unused)) void *arg) { |
| 69 | const char *whoami = "(progress)"; |
| 70 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_THREADcxx . . $ LOG_LEVEL_THREAD., "%10s: initialized progress_updater thread", whoami); |
| 71 | |
| 72 | mask_sigcxx . . $ mask_sig(85c52e162fed56f9).(whoami); |
| 73 | |
| 74 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! _continue = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 75 | while (_continue) { |
| 76 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&progress_update_mutexcxx . . $ progress_update_mutex.); |
| 77 | time_tcxx . . $ time_t# now = timecxx . . $ time(7f38134db6521d23).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 78 | int elapsed = (unsigned long) now - (unsigned long) confcxx . . $ conf.->start_timecxx . . $ db_config#start_time.; |
| 79 | char *progress_bar = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 80 | switch (statecxx . . $ state.) { |
| 81 | case PROGRESS_CONFIGcxx . . $ PROGRESS_CONFIG.: |
| 82 | case PROGRESS_NEWDBcxx . . $ PROGRESS_NEWDB.: |
| 83 | case PROGRESS_SKIPPEDcxx . . $ PROGRESS_SKIPPED.: |
| 84 | case PROGRESS_DISKcxx . . $ PROGRESS_DISK.: |
| 85 | case PROGRESS_WRITEDBcxx . . $ PROGRESS_WRITEDB.: |
| 86 | case PROGRESS_OLDDBcxx . . $ PROGRESS_OLDDB.: |
| 87 | progress_bar = get_progress_bar_stringcxx . . $ get_progress_bar_string(567eccb82e01d0a1).(get_state_stringcxx . . $ get_state_string(2f101243134cb539).(statecxx . . $ state.), pathcxx . . $ path., num_entriescxx . . $ num_entries., num_skippedcxx . . $ num_skipped., elapsed, confcxx . . $ conf.->progresscxx . . $ db_config#progress.); |
| 88 | stderr_msgcxx . . $ stderr_msg(326aeb464d16410c).("%s\r", progress_bar); |
| 89 | freecxx . . $ free(5d22bdacc48458e8).(progress_bar); |
| 90 | progress_bar = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 91 | break; |
| 92 | case PROGRESS_CLEARcxx . . $ PROGRESS_CLEAR.: |
| 93 | _continue = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 94 | break; |
| 95 | case PROGRESS_NONEcxx . . $ PROGRESS_NONE.: |
| 96 | /* do nothing */ |
| 97 | ;; |
| 98 | |
| 99 | } |
| 100 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&progress_update_mutexcxx . . $ progress_update_mutex.); |
| 101 | usleepcxx . . $ usleep(4c679297bf94b9eb).(100000); |
| 102 | } |
| 103 | return (void *) pthread_selfcxx . . $ pthread_self(f708dce9a8856c70).(); |
| 104 | } |
| 105 | |
| 106 | static void update_statecxx . . $ update_state(f1cc6ad68df43989).(progress_statecxx . . $ progress_state# new_state) { |
| 107 | LOG_LEVELcxx . . $ LOG_LEVEL# log_level = LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO.; |
| 108 | |
| 109 | struct timespeccxx . . $ timespec# time_now; |
| 110 | clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).( CLOCK_REALTIMEcxx . . $ `<external>/89459239771508533/time.h:46:10`!, &time_now); |
| 111 | |
| 112 | double elapsed = (time_now.tv_seccxx . . $ timespec#tv_sec. - time_startcxx . . $ time_start..tv_seccxx . . $ timespec#tv_sec.) + (time_now.tv_nseccxx . . $ timespec#tv_nsec. - time_startcxx . . $ time_start..tv_nseccxx . . $ timespec#tv_nsec.)/(double)BILLIONcxx . . $ `src/progress.c:37:9`!; |
| 113 | |
| 114 | long elapsed_minutes = (long)floorcxx . . $ floor(447c67c6676f75c3).(elapsed)/60; |
| 115 | double elapsed_seconds = elapsed - elapsed_minutes*60; |
| 116 | |
| 117 | unsigned long performance = (num_entriescxx . . $ num_entries.+num_skippedcxx . . $ num_skipped.)/elapsed; |
| 118 | char * entries_string = num_entriescxx . . $ num_entries. == 1 ? "entry" : "entries"; |
| 119 | |
| 120 | char *skipped_str = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 121 | if (num_skippedcxx . . $ num_skipped.) { |
| 122 | const char *skipped_format = " (%lu skipped)"; |
| 123 | int n = snprintfcxx . . $ snprintf(ea502d73e74c754c).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, skipped_format, num_skippedcxx . . $ num_skipped.); |
| 124 | skipped_str = checked_malloccxx . . $ checked_malloc(bfcfa4d6b7f7ef64).(n+1); |
| 125 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(skipped_str, n+1, skipped_format, num_skippedcxx . . $ num_skipped.); |
| 126 | } |
| 127 | switch (statecxx . . $ state.) { |
| 128 | case PROGRESS_OLDDBcxx . . $ PROGRESS_OLDDB.: |
| 129 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(log_level, "read %lu %s%s [%lu entries/s] from %s in %ldm %.4lfs", num_entriescxx . . $ num_entries., entries_string, skipped_str?skipped_str:"", performance, (confcxx . . $ conf.->database_incxx . . $ db_config#database_in..urlcxx . . $ database#url.)->rawcxx . . $ url_t#raw., elapsed_minutes, elapsed_seconds); |
| 130 | break; |
| 131 | case PROGRESS_NEWDBcxx . . $ PROGRESS_NEWDB.: |
| 132 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(log_level, "read %lu %s%s [%lu entries/s] from %s in %ldm %.4lfs", num_entriescxx . . $ num_entries., entries_string, skipped_str?skipped_str:"", performance, (confcxx . . $ conf.->database_newcxx . . $ db_config#database_new..urlcxx . . $ database#url.)->rawcxx . . $ url_t#raw., elapsed_minutes, elapsed_seconds); |
| 133 | break; |
| 134 | case PROGRESS_DISKcxx . . $ PROGRESS_DISK.: |
| 135 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(log_level, "read %lu %s [%lu entries/s] from file system in %ldm %.4lfs", num_entriescxx . . $ num_entries., entries_string, performance, elapsed_minutes, elapsed_seconds); |
| 136 | break; |
| 137 | case PROGRESS_CONFIGcxx . . $ PROGRESS_CONFIG.: |
| 138 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(log_level, "parsed %lu config %s [%lu files/s] in %ldm %.4lfs", num_entriescxx . . $ num_entries., num_entriescxx . . $ num_entries. == 1 ? "file" : "files", performance, elapsed_minutes, elapsed_seconds); |
| 139 | break; |
| 140 | case PROGRESS_WRITEDBcxx . . $ PROGRESS_WRITEDB.: |
| 141 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(log_level, "wrote %lu %s [%lu entries/s] to %s in %ldm %.4lfs", num_entriescxx . . $ num_entries., entries_string, performance, (confcxx . . $ conf.->database_outcxx . . $ db_config#database_out..urlcxx . . $ database#url.)->rawcxx . . $ url_t#raw., elapsed_minutes, elapsed_seconds); |
| 142 | break; |
| 143 | case PROGRESS_SKIPPEDcxx . . $ PROGRESS_SKIPPED.: |
| 144 | case PROGRESS_CLEARcxx . . $ PROGRESS_CLEAR.: |
| 145 | case PROGRESS_NONEcxx . . $ PROGRESS_NONE.: |
| 146 | /* no logging */ |
| 147 | break; |
| 148 | } |
| 149 | if (skipped_str) { |
| 150 | freecxx . . $ free(5d22bdacc48458e8).(skipped_str); |
| 151 | skipped_str = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 152 | } |
| 153 | time_startcxx . . $ time_start. = time_now; |
| 154 | statecxx . . $ state. = new_state; |
| 155 | } |
| 156 | |
| 157 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! progress_startcxx . . $ progress_start(c9857a73d68e59ba).(void) { |
| 158 | struct winsizecxx . . $ winsize# winsize; |
| 159 | |
| 160 | if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(STDERR_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:212:9`!, TIOCGWINSZcxx . . $ `<external>/123502480044527469/ioctls.h:38:9`!, &winsize) == -1) { |
| 161 | confcxx . . $ conf.->progresscxx . . $ db_config#progress. = 80; |
| 162 | } else { |
| 163 | confcxx . . $ conf.->progresscxx . . $ db_config#progress. = winsize.ws_colcxx . . $ winsize#ws_col.; |
| 164 | } |
| 165 | if (pthread_createcxx . . $ pthread_create(2141a84bcac899e5).(&progress_updater_threadcxx . . $ progress_updater_thread., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &progress_updatercxx . . $ progress_updater(9f162e79bfc6c74)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) != 0) { |
| 166 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., "failed to start progress_updater thread (disable progress bar)"); |
| 167 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 168 | } |
| 169 | stderr_set_line_erasurecxx . . $ stderr_set_line_erasure(b45ddcbc8423892c).(truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!); |
| 170 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 171 | } |
| 172 | |
| 173 | void progress_stopcxx . . $ progress_stop(49f6e7a06ebc5aa8).(void) { |
| 174 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&progress_update_mutexcxx . . $ progress_update_mutex.); |
| 175 | update_statecxx . . $ update_state(f1cc6ad68df43989).(PROGRESS_CLEARcxx . . $ PROGRESS_CLEAR.); |
| 176 | stderr_set_line_erasurecxx . . $ stderr_set_line_erasure(b45ddcbc8423892c).(falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!); |
| 177 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&progress_update_mutexcxx . . $ progress_update_mutex.); |
| 178 | if (progress_updater_threadcxx . . $ progress_updater_thread.) { |
| 179 | if (pthread_joincxx . . $ pthread_join(269902cf949f12c5).(progress_updater_threadcxx . . $ progress_updater_thread., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) != 0) { |
| 180 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "failed to join progress_updater thread"); |
| 181 | exitcxx . . $ exit(3ca292c338df9bb3).(THREAD_ERRORcxx . . $ `include/errorcodes.h:34:9`!); |
| 182 | } |
| 183 | log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_THREADcxx . . $ LOG_LEVEL_THREAD., "%10s: progress_updater thread finished", "(main)"); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void progress_statuscxx . . $ progress_status(a36c9239dfdba10f).(progress_statecxx . . $ progress_state# new_state, const char* data) { |
| 188 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&progress_update_mutexcxx . . $ progress_update_mutex.); |
| 189 | switch (new_state) { |
| 190 | case PROGRESS_CONFIGcxx . . $ PROGRESS_CONFIG.: |
| 191 | case PROGRESS_OLDDBcxx . . $ PROGRESS_OLDDB.: |
| 192 | case PROGRESS_NEWDBcxx . . $ PROGRESS_NEWDB.: |
| 193 | case PROGRESS_DISKcxx . . $ PROGRESS_DISK.: |
| 194 | case PROGRESS_WRITEDBcxx . . $ PROGRESS_WRITEDB.: |
| 195 | if (statecxx . . $ state. == new_state) { |
| 196 | num_entriescxx . . $ num_entries.++; |
| 197 | } else { |
| 198 | update_statecxx . . $ update_state(f1cc6ad68df43989).(new_state); |
| 199 | num_entriescxx . . $ num_entries. = 0LU; |
| 200 | num_skippedcxx . . $ num_skipped. = 0LU; |
| 201 | } |
| 202 | freecxx . . $ free(5d22bdacc48458e8).(pathcxx . . $ path.); |
| 203 | pathcxx . . $ path. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 204 | if (data) { |
| 205 | pathcxx . . $ path. = stresccxx . . $ stresc(a8b8e32347182212).(data); |
| 206 | } |
| 207 | break; |
| 208 | case PROGRESS_SKIPPEDcxx . . $ PROGRESS_SKIPPED.: |
| 209 | num_skippedcxx . . $ num_skipped.++; |
| 210 | break; |
| 211 | case PROGRESS_CLEARcxx . . $ PROGRESS_CLEAR.: |
| 212 | case PROGRESS_NONEcxx . . $ PROGRESS_NONE.: |
| 213 | update_statecxx . . $ update_state(f1cc6ad68df43989).(new_state); |
| 214 | break; |
| 215 | } |
| 216 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&progress_update_mutexcxx . . $ progress_update_mutex.); |
| 217 | } |