Lucas De Marchi | 61e9433 | 2012-01-25 18:16:45 -0200 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stddef.h> |
| 4 | #include <errno.h> |
| 5 | #include <unistd.h> |
| 6 | #include <inttypes.h> |
| 7 | #include <string.h> |
| 8 | #include <libkmod.h> |
| 9 | |
| 10 | #include "testsuite.h" |
| 11 | |
| 12 | static int loaded_1(const struct test *t) |
| 13 | { |
| 14 | struct kmod_ctx *ctx; |
| 15 | const char *null_config = NULL; |
| 16 | struct kmod_list *list, *itr; |
| 17 | int err; |
| 18 | |
| 19 | ctx = kmod_new(NULL, &null_config); |
| 20 | if (ctx == NULL) |
| 21 | exit(EXIT_FAILURE); |
| 22 | |
| 23 | err = kmod_module_new_from_loaded(ctx, &list); |
| 24 | if (err < 0) { |
| 25 | fprintf(stderr, "%s\n", strerror(-err)); |
| 26 | kmod_unref(ctx); |
| 27 | exit(EXIT_FAILURE); |
| 28 | } |
| 29 | |
| 30 | printf("Module Size Used by\n"); |
| 31 | |
| 32 | kmod_list_foreach(itr, list) { |
| 33 | struct kmod_module *mod = kmod_module_get_module(itr); |
| 34 | const char *name = kmod_module_get_name(mod); |
| 35 | int use_count = kmod_module_get_refcnt(mod); |
| 36 | long size = kmod_module_get_size(mod); |
| 37 | struct kmod_list *holders, *hitr; |
| 38 | int first = 1; |
| 39 | |
| 40 | printf("%-19s %8ld %d ", name, size, use_count); |
| 41 | holders = kmod_module_get_holders(mod); |
| 42 | kmod_list_foreach(hitr, holders) { |
| 43 | struct kmod_module *hm = kmod_module_get_module(hitr); |
| 44 | |
| 45 | if (!first) |
| 46 | putchar(','); |
| 47 | else |
| 48 | first = 0; |
| 49 | |
| 50 | fputs(kmod_module_get_name(hm), stdout); |
| 51 | kmod_module_unref(hm); |
| 52 | } |
| 53 | putchar('\n'); |
| 54 | kmod_module_unref_list(holders); |
| 55 | kmod_module_unref(mod); |
| 56 | } |
| 57 | kmod_module_unref_list(list); |
| 58 | |
| 59 | kmod_unref(ctx); |
| 60 | |
| 61 | return EXIT_SUCCESS; |
| 62 | } |
| 63 | static const struct test sloaded_1 = { |
| 64 | .name = "sloaded_1", |
| 65 | .description = "check if list of module is created", |
| 66 | .func = loaded_1, |
| 67 | .config = { |
| 68 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-loaded/", |
| 69 | }, |
| 70 | .need_spawn = true, |
| 71 | .output = { |
| 72 | .stdout = TESTSUITE_ROOTFS "test-loaded/correct.txt", |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | static const struct test *tests[] = { |
| 77 | &sloaded_1, |
| 78 | NULL, |
| 79 | }; |
| 80 | |
| 81 | int main(int argc, char *argv[]) |
| 82 | { |
| 83 | const struct test *t; |
| 84 | int arg; |
| 85 | size_t i; |
| 86 | |
| 87 | arg = test_init(argc, argv, tests); |
| 88 | if (arg == 0) |
| 89 | return 0; |
| 90 | |
| 91 | if (arg < argc) { |
| 92 | t = test_find(tests, argv[arg]); |
| 93 | if (t == NULL) { |
| 94 | fprintf(stderr, "could not find test %s\n", argv[arg]); |
| 95 | exit(EXIT_FAILURE); |
| 96 | } |
| 97 | |
| 98 | return test_run(t); |
| 99 | } |
| 100 | |
| 101 | for (i = 0; tests[i] != NULL; i++) { |
| 102 | if (test_run(tests[i]) != 0) |
| 103 | exit(EXIT_FAILURE); |
| 104 | } |
| 105 | |
| 106 | exit(EXIT_SUCCESS); |
| 107 | } |