Debian Codegraph @

src/common/debug.c

raw symbols SCIP
1
2 /*
3 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
4 * All Rights Reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Lukas Kuklinek <[email protected]>
22 */
23
24 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
25 #include <config.h>cxx . . $ `<file>/build/config.h`/
26 #endif
27
28 # include <stdio.h>
29 # include <stdarg.h>
30 # include <string.h>
31 # include <stdlib.h>
32 # include <sys/types.h>
33 #ifdef HAVE_FLOCK
34 # include <sys/file.h>
35 #endif
36 #ifdef OS_WINDOWS
37 #include <io.h>
38 #else
39 #include <unistd.h>
40 #endif
41 # include <time.h>
42 # include <errno.h>
43 # include <fcntl.h>
44 # include <sys/stat.h>
45
46 #if defined(OVAL_PROBES_ENABLEDcxx . . $ `build/config.h:67:9`!)
47 # include <sexp.h>cxx . . $ `<file>/src/OVAL/probes/SEAP/public/sexp.h`/
48 # include <sexp-output.h>cxx . . $ `<file>/src/OVAL/probes/SEAP/public/sexp-output.h`/
49 #endif
50
51 # include "debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/
52
53 #include <oscap_debug.h>cxx . . $ `<file>/src/common/public/oscap_debug.h`/
54 #include "_error.h"cxx . . $ `<file>/src/common/_error.h`/
55
56 #ifndef PATH_SEPARATOR
57 # define PATH_SEPARATORcxx . . $ `src/common/debug.c:57:10`! '/'
58 #endif
59
60 #if defined(OS_WINDOWS)
61 # include <windows.h>
62 # define GET_PROGRAM_NAME get_program_name()
63 #elif defined(OS_APPLE) || defined(OS_FREEBSD)
64 # define GET_PROGRAM_NAME getprogname()
65 #else
66 # define GET_PROGRAM_NAMEcxx . . $ `src/common/debug.c:66:10`! program_invocation_short_name
67 #endif
68
69 static const struct oscap_string_mapcxx . . $ oscap_string_map# OSCAP_VERBOSITY_LEVELScxx . . $ OSCAP_VERBOSITY_LEVELS.[] = {
70 {DBG_Ecxx . . $ DBG_E., "ERROR"},
71 {DBG_Wcxx . . $ DBG_W., "WARNING"},
72 {DBG_Icxx . . $ DBG_I., "INFO"},
73 {DBG_Dcxx . . $ DBG_D., "DEVEL"},
74 {DBG_UNKNOWNcxx . . $ DBG_UNKNOWN., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!}
75 };
76
77 # if defined(OSCAP_THREAD_SAFEcxx . . $ `build/config.h:13:9`!)
78 # include <pthread.h>
79 # if defined(OS_FREEBSD)
80 # include <pthread_np.h>
81 # endif /* OS_FREEBSD */
82 static pthread_mutex_tcxx . . $ pthread_mutex_t# __debuglog_mutexcxx . . $ __debuglog_mutex. = PTHREAD_MUTEX_INITIALIZERcxx . . $ `<external>/14712363683955484316/pthread.h:90:9`!;
83 # endif /* OSCAP_THREAD_SAFE */
84
85 FILEcxx . . $ FILE# *__debuglog_fpcxx . . $ __debuglog_fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
86 oscap_verbosity_levelscxx . . $ oscap_verbosity_levels# __debuglog_levelcxx . . $ __debuglog_level. = DBG_UNKNOWNcxx . . $ DBG_UNKNOWN.;
87
88 #if defined(OSCAP_THREAD_SAFEcxx . . $ `build/config.h:13:9`!)
89 # define __LOCK_FPcxx . . $ `src/common/debug.c:89:10`! do { if (pthread_mutex_lock (&__debuglog_mutex) != 0) abort(); } while(0)
90 # define __UNLOCK_FPcxx . . $ `src/common/debug.c:90:10`! do { if (pthread_mutex_unlock (&__debuglog_mutex) != 0) abort(); } while(0)
91 #else
92 # define __LOCK_FP while(0)
93 # define __UNLOCK_FP while(0)
94 #endif
95
96 #define THREAD_NAME_LENcxx . . $ `src/common/debug.c:96:9`! 16
97
98 #if defined(OS_WINDOWS)
99 static char * get_program_name()
100 {
101 char path[PATH_MAX + 1];
102 int path_size = GetModuleFileName(NULL, path, sizeof(path) - 1);
103
104 if(path_size < 0)
105 return NULL;
106 if(path_size == sizeof(path) - 1)
107 path[path_size] = '\0';
108 return strdup(path);
109 }
110
111 int setenv(const char *name, const char *value, int overwrite)
112 {
113 int errorcode = 0;
114
115 if(!overwrite) {
116 size_t envsize = 0;
117 errorcode = getenv_s(&envsize, NULL, 0, name);
118 if(errorcode || envsize)
119 return errorcode;
120 }
121
122 return _putenv_s(name, value);
123 }
124 #endif
125
126 static void __oscap_debuglog_closecxx . . $ __oscap_debuglog_close(49f6e7a06ebc5aa8).(void)
127 {
128 fclosecxx . . $ fclose(89bc1efcdac0ca22).(__debuglog_fpcxx . . $ __debuglog_fp.);
129 }
130
131 oscap_verbosity_levelscxx . . $ oscap_verbosity_levels# oscap_verbosity_level_from_cstrcxx . . $ oscap_verbosity_level_from_cstr(d17a359e3ebc1c52).(const char *level_name)
132 {
133 return oscap_string_to_enumcxx . . $ oscap_string_to_enum(851d6ce10579b7d8).(OSCAP_VERBOSITY_LEVELScxx . . $ OSCAP_VERBOSITY_LEVELS., level_name);
134 }
135
136 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_set_verbosecxx . . $ oscap_set_verbose(b7a7aa4a824b7595).(const char *verbosity_level, const char *filename)
137 {
138 if (verbosity_level == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
139 verbosity_level = "WARNING";
140 }
141 __debuglog_levelcxx . . $ __debuglog_level. = oscap_verbosity_level_from_cstrcxx . . $ oscap_verbosity_level_from_cstr(d17a359e3ebc1c52).(verbosity_level);
142 if (__debuglog_levelcxx . . $ __debuglog_level. == DBG_UNKNOWNcxx . . $ DBG_UNKNOWN.) {
143 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
144 }
145 if (filename == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
146 __debuglog_fpcxx . . $ __debuglog_fp. = stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!;
147 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
148 }
149 int fd;
150 /* Open a file. If the file doesn't exist, create it.
151 * If the file exists, erase its content.
152 * File is opened in "append" mode.
153 * Append mode is necessary when more processes write to same file.
154 * Every process using the log file must open it in append mode,
155 * because otherwise some data may be missing on output.
156 */
157 #ifdef OS_WINDOWS
158 fd = open(filename, O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
159 #else
160 fd = opencxx . . $ open(e156e3aa1e5c4ad1).(filename, O_APPENDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:59:10`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`! | O_TRUNCcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:56:10`! | O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`!,
161 S_IRUSRcxx . . $ `<external>/531885356569766532/stat.h:168:9`! | S_IWUSRcxx . . $ `<external>/531885356569766532/stat.h:169:9`! | S_IRGRPcxx . . $ `<external>/531885356569766532/stat.h:180:9`! | S_IWGRPcxx . . $ `<external>/531885356569766532/stat.h:181:9`! | S_IROTHcxx . . $ `<external>/531885356569766532/stat.h:186:9`! | S_IWOTHcxx . . $ `<external>/531885356569766532/stat.h:187:9`!);
162 #endif
163
164 if (fd == -1) {
165 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Failed to open file %s: %s.", filename, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
166 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
167 }
168 __debuglog_fpcxx . . $ __debuglog_fp. = fdopencxx . . $ fdopen(8f4e2d7350ebd077).(fd, "a");
169 if (__debuglog_fpcxx . . $ __debuglog_fp. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
170 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Failed to associate stream with file %s: %s.", filename, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
171 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
172 }
173 setbufcxx . . $ setbuf(a7c976899de42135).(__debuglog_fpcxx . . $ __debuglog_fp., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
174 atexitcxx . . $ atexit(8919c3b35e68c42d).(&__oscap_debuglog_closecxx . . $ __oscap_debuglog_close(49f6e7a06ebc5aa8).);
175 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
176 }
177
178
179 static const char *__oscap_path_rstripcxx . . $ __oscap_path_rstrip(fb58d2e09a2a1ce6).(const char *path)
180 {
181 const char *separator = strrchrcxx . . $ strrchr(2050848944e00d54).(path, PATH_SEPARATORcxx . . $ `src/common/debug.c:57:10`!);
182 if (separator) {
183 return (separator + 1);
184 }
185
186 return (path);
187 }
188
189
190 static void debug_message_startcxx . . $ debug_message_start(3e454b8ccf442868).(int level, int delta_indent)
191 {
192 char l;
193 static int indent = 0;
194
195 __LOCK_FPcxx . . $ `src/common/debug.c:89:10`!;
196
197 indent += delta_indent;
198
199 switch (level) {
200 case DBG_Ecxx . . $ DBG_E.:
201 l = 'E';
202 break;
203 case DBG_Wcxx . . $ DBG_W.:
204 l = 'W';
205 break;
206 case DBG_Icxx . . $ DBG_I.:
207 l = 'I';
208 break;
209 case DBG_Dcxx . . $ DBG_D.:
210 l = 'D';
211 break;
212 default:
213 l = '0';
214 }
215 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(__debuglog_fpcxx . . $ __debuglog_fp., "%c: %s: ", l, GET_PROGRAM_NAMEcxx . . $ `src/common/debug.c:66:10`!);
216 for (int i = 0; i < indent; i++) {
217 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(__debuglog_fpcxx . . $ __debuglog_fp., " ");
218 }
219 }
220
221 static void debug_message_devel_metadatacxx . . $ debug_message_devel_metadata(2ed9965f6227ab92).(const char *file, const char *fn, size_t line)
222 {
223 const char *f = __oscap_path_rstripcxx . . $ __oscap_path_rstrip(fb58d2e09a2a1ce6).(file);
224 #if defined(OSCAP_THREAD_SAFEcxx . . $ `build/config.h:13:9`!)
225 char thread_name[THREAD_NAME_LENcxx . . $ `src/common/debug.c:96:9`!];
226 pthread_tcxx . . $ pthread_t# thread = pthread_selfcxx . . $ pthread_self(f708dce9a8856c70).();
227 #if defined(HAVE_PTHREAD_GETNAME_NPcxx . . $ `build/config.h:54:9`!)
228 pthread_getname_npcxx . . $ pthread_getname_np(2324ad226b7e9d8f).(thread, thread_name, THREAD_NAME_LENcxx . . $ `src/common/debug.c:96:9`!);
229 #else
230 snprintf(thread_name, THREAD_NAME_LEN, "unknown");
231 #endif
232 #if !defined(__MINGW32__) && defined(OS_WINDOWS)
233 unsigned long long tid = pthread_getw32threadid_np(thread);
234 #else
235 /* XXX: non-portable usage of pthread_t */
236 unsigned long long tid = (unsigned long long) thread;
237 #endif
238 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(__debuglog_fpcxx . . $ __debuglog_fp., " [%s(%ld):%s(%llx):%s:%zu:%s]",
239 GET_PROGRAM_NAMEcxx . . $ `src/common/debug.c:66:10`!, (long) getpidcxx . . $ getpid(b126dc7c1de90089).(), thread_name,
240 tid, f, line, fn);
241 #else
242 fprintf(__debuglog_fp, " [%ld:%s:%zu:%s]", (long) getpid(),
243 f, line, fn);
244 #endif
245 }
246
247 static void debug_message_endcxx . . $ debug_message_end(d878ad36e69247c7).()
248 {
249 fputccxx . . $ fputc(b1cd03d6c470d1bd).('\n', __debuglog_fpcxx . . $ __debuglog_fp.);
250 __UNLOCK_FPcxx . . $ `src/common/debug.c:90:10`!;
251 return;
252 }
253
254 void __oscap_dlprintfcxx . . $ __oscap_dlprintf(46abcccd4f14a385).(int level, const char *file, const char *fn, size_t line, int delta_indent, const char *fmt, ...)
255 {
256 va_listcxx . . $ va_list# ap;
257
258 if (__debuglog_fpcxx . . $ __debuglog_fp. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
259 return;
260 }
261 if (fmt == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
262 return;
263 }
264 if (__debuglog_levelcxx . . $ __debuglog_level. < level) {
265 return;
266 }
267 va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`!(ap, fmt);
268 debug_message_startcxx . . $ debug_message_start(3e454b8ccf442868).(level, delta_indent);
269 vfprintfcxx . . $ vfprintf(bded311e54d67f9c).(__debuglog_fpcxx . . $ __debuglog_fp., fmt, ap);
270 if (__debuglog_levelcxx . . $ __debuglog_level. == DBG_Dcxx . . $ DBG_D.) {
271 debug_message_devel_metadatacxx . . $ debug_message_devel_metadata(2ed9965f6227ab92).(file, fn, line);
272 }
273 debug_message_endcxx . . $ debug_message_end(d878ad36e69247c7).();
274 va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`!(ap);
275 }
276
277 void __oscap_debuglog_objectcxx . . $ __oscap_debuglog_object(7728107c3ca98b23). (const char *file, const char *fn, size_t line, int objtype, void *obj)
278 {
279 if (__debuglog_fpcxx . . $ __debuglog_fp. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
280 return;
281 }
282 if (__debuglog_levelcxx . . $ __debuglog_level. < DBG_Dcxx . . $ DBG_D.) {
283 return;
284 }
285 debug_message_startcxx . . $ debug_message_start(3e454b8ccf442868).(DBG_Dcxx . . $ DBG_D., 0);
286 switch (objtype) {
287 case OSCAP_DEBUGOBJ_SEXPcxx . . $ `src/common/debug_priv.h:40:9`!:
288 #if defined(OVAL_PROBES_ENABLEDcxx . . $ `build/config.h:67:9`!)
289 SEXP_fprintfacxx . . $ SEXP_fprintfa(ee3a4058871e0f88).(__debuglog_fpcxx . . $ __debuglog_fp., (SEXP_tcxx . . $ SEXP_t# *)obj);
290 #endif
291 break;
292 default:
293 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(__debuglog_fpcxx . . $ __debuglog_fp., "Attempt to dump a not supported object.");
294 }
295 debug_message_devel_metadatacxx . . $ debug_message_devel_metadata(2ed9965f6227ab92).(file, fn, line);
296 debug_message_endcxx . . $ debug_message_end(d878ad36e69247c7).();
297 }
298
299 void oscap_print_env_varscxx . . $ oscap_print_env_vars(49f6e7a06ebc5aa8).()
300 {
301 const char *known_env_vars[] = {
302 "OSCAP_CHECK_ENGINE_PLUGIN_DIR",
303 "OSCAP_CONTAINER_VARS",
304 "OSCAP_EVALUATION_TARGET",
305 "OSCAP_FULL_VALIDATION",
306 "OSCAP_OVAL_COMMAND_OPTIONS",
307 "OSCAP_PCRE_EXEC_RECURSION_LIMIT",
308 "OSCAP_PROBE_ROOT",
309 "SEXP_VALIDATE_DISABLE",
310 "SOURCE_DATE_EPOCH",
311 "OSCAP_PROBE_MEMORY_USAGE_RATIO",
312 "OSCAP_PROBE_MAX_COLLECTED_ITEMS",
313 "OSCAP_PROBE_IGNORE_PATHS",
314 "OSCAP_PREFERRED_ENGINE",
315 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!
316 };
317 dIcxx . . $ `src/common/debug_priv.h:61:9`!("Using environment variables:");
318 for (int i = 0; known_env_vars[i]; i++) {
319 char *env_var_val = getenvcxx . . $ getenv(a8b8e32347182212).(known_env_vars[i]);
320 dIcxx . . $ `src/common/debug_priv.h:61:9`!("%s='%s'", known_env_vars[i], env_var_val ? env_var_val : "");
321 }
322 }