|
10 | 10 | #ifdef USE_LIBCAP |
11 | 11 | #include <sys/capability.h> |
12 | 12 | #endif |
13 | | -#include <sys/utsname.h> |
14 | 13 | #include <sys/vfs.h> |
15 | 14 |
|
16 | 15 | #include <linux/filter.h> |
17 | 16 | #include <linux/limits.h> |
18 | 17 |
|
19 | 18 | #include <bpf/bpf.h> |
20 | 19 | #include <bpf/libbpf.h> |
21 | | -#include <zlib.h> |
22 | 20 |
|
23 | 21 | #include "main.h" |
24 | 22 |
|
@@ -327,40 +325,9 @@ static void probe_jit_limit(void) |
327 | 325 | } |
328 | 326 | } |
329 | 327 |
|
330 | | -static bool read_next_kernel_config_option(gzFile file, char *buf, size_t n, |
331 | | - char **value) |
332 | | -{ |
333 | | - char *sep; |
334 | | - |
335 | | - while (gzgets(file, buf, n)) { |
336 | | - if (strncmp(buf, "CONFIG_", 7)) |
337 | | - continue; |
338 | | - |
339 | | - sep = strchr(buf, '='); |
340 | | - if (!sep) |
341 | | - continue; |
342 | | - |
343 | | - /* Trim ending '\n' */ |
344 | | - buf[strlen(buf) - 1] = '\0'; |
345 | | - |
346 | | - /* Split on '=' and ensure that a value is present. */ |
347 | | - *sep = '\0'; |
348 | | - if (!sep[1]) |
349 | | - continue; |
350 | | - |
351 | | - *value = sep + 1; |
352 | | - return true; |
353 | | - } |
354 | | - |
355 | | - return false; |
356 | | -} |
357 | | - |
358 | 328 | static void probe_kernel_image_config(const char *define_prefix) |
359 | 329 | { |
360 | | - static const struct { |
361 | | - const char * const name; |
362 | | - bool macro_dump; |
363 | | - } options[] = { |
| 330 | + struct kernel_config_option options[] = { |
364 | 331 | /* Enable BPF */ |
365 | 332 | { "CONFIG_BPF", }, |
366 | 333 | /* Enable bpf() syscall */ |
@@ -435,63 +402,18 @@ static void probe_kernel_image_config(const char *define_prefix) |
435 | 402 | { "CONFIG_HZ", true, } |
436 | 403 | }; |
437 | 404 | char *values[ARRAY_SIZE(options)] = { }; |
438 | | - struct utsname utsn; |
439 | | - char path[PATH_MAX]; |
440 | | - gzFile file = NULL; |
441 | | - char buf[4096]; |
442 | | - char *value; |
443 | 405 | size_t i; |
444 | 406 |
|
445 | | - if (!uname(&utsn)) { |
446 | | - snprintf(path, sizeof(path), "/boot/config-%s", utsn.release); |
447 | | - |
448 | | - /* gzopen also accepts uncompressed files. */ |
449 | | - file = gzopen(path, "r"); |
450 | | - } |
451 | | - |
452 | | - if (!file) { |
453 | | - /* Some distributions build with CONFIG_IKCONFIG=y and put the |
454 | | - * config file at /proc/config.gz. |
455 | | - */ |
456 | | - file = gzopen("/proc/config.gz", "r"); |
457 | | - } |
458 | | - if (!file) { |
459 | | - p_info("skipping kernel config, can't open file: %s", |
460 | | - strerror(errno)); |
461 | | - goto end_parse; |
462 | | - } |
463 | | - /* Sanity checks */ |
464 | | - if (!gzgets(file, buf, sizeof(buf)) || |
465 | | - !gzgets(file, buf, sizeof(buf))) { |
466 | | - p_info("skipping kernel config, can't read from file: %s", |
467 | | - strerror(errno)); |
468 | | - goto end_parse; |
469 | | - } |
470 | | - if (strcmp(buf, "# Automatically generated file; DO NOT EDIT.\n")) { |
471 | | - p_info("skipping kernel config, can't find correct file"); |
472 | | - goto end_parse; |
473 | | - } |
474 | | - |
475 | | - while (read_next_kernel_config_option(file, buf, sizeof(buf), &value)) { |
476 | | - for (i = 0; i < ARRAY_SIZE(options); i++) { |
477 | | - if ((define_prefix && !options[i].macro_dump) || |
478 | | - values[i] || strcmp(buf, options[i].name)) |
479 | | - continue; |
480 | | - |
481 | | - values[i] = strdup(value); |
482 | | - } |
483 | | - } |
| 407 | + if (read_kernel_config(options, ARRAY_SIZE(options), values, |
| 408 | + define_prefix)) |
| 409 | + return; |
484 | 410 |
|
485 | 411 | for (i = 0; i < ARRAY_SIZE(options); i++) { |
486 | 412 | if (define_prefix && !options[i].macro_dump) |
487 | 413 | continue; |
488 | 414 | print_kernel_option(options[i].name, values[i], define_prefix); |
489 | 415 | free(values[i]); |
490 | 416 | } |
491 | | - |
492 | | -end_parse: |
493 | | - if (file) |
494 | | - gzclose(file); |
495 | 417 | } |
496 | 418 |
|
497 | 419 | static bool probe_bpf_syscall(const char *define_prefix) |
|
0 commit comments