libkmod: fix return error when opening index
[platform/upstream/kmod.git] / testsuite / test-loaded.c
1 /*
2  * Copyright (C) 2012-2013  ProFUSION embedded systems
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <errno.h>
19 #include <inttypes.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <libkmod/libkmod.h>
27
28 #include "testsuite.h"
29
30 static int loaded_1(const struct test *t)
31 {
32         struct kmod_ctx *ctx;
33         const char *null_config = NULL;
34         struct kmod_list *list, *itr;
35         int err;
36
37         ctx = kmod_new(NULL, &null_config);
38         if (ctx == NULL)
39                 exit(EXIT_FAILURE);
40
41         err = kmod_module_new_from_loaded(ctx, &list);
42         if (err < 0) {
43                 fprintf(stderr, "%s\n", strerror(-err));
44                 kmod_unref(ctx);
45                 exit(EXIT_FAILURE);
46         }
47
48         printf("Module                  Size  Used by\n");
49
50         kmod_list_foreach(itr, list) {
51                 struct kmod_module *mod = kmod_module_get_module(itr);
52                 const char *name = kmod_module_get_name(mod);
53                 int use_count = kmod_module_get_refcnt(mod);
54                 long size = kmod_module_get_size(mod);
55                 struct kmod_list *holders, *hitr;
56                 int first = 1;
57
58                 printf("%-19s %8ld  %d ", name, size, use_count);
59                 holders = kmod_module_get_holders(mod);
60                 kmod_list_foreach(hitr, holders) {
61                         struct kmod_module *hm = kmod_module_get_module(hitr);
62
63                         if (!first)
64                                 putchar(',');
65                         else
66                                 first = 0;
67
68                         fputs(kmod_module_get_name(hm), stdout);
69                         kmod_module_unref(hm);
70                 }
71                 putchar('\n');
72                 kmod_module_unref_list(holders);
73                 kmod_module_unref(mod);
74         }
75         kmod_module_unref_list(list);
76
77         kmod_unref(ctx);
78
79         return EXIT_SUCCESS;
80 }
81 DEFINE_TEST(loaded_1,
82         .description = "check if list of module is created",
83         .config = {
84                 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-loaded/",
85         },
86         .need_spawn = true,
87         .output = {
88                 .out = TESTSUITE_ROOTFS "test-loaded/correct.txt",
89         });
90
91 TESTSUITE_MAIN();