]> git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-loaded.c
6914e57a7719ae7e08d5924dfd29bf5156ef81b7
[thirdparty/kmod.git] / libkmod / libkmod-loaded.c
1 /*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011 ProFUSION embedded systems
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 version 2.1.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <inttypes.h>
29
30 #include "libkmod.h"
31 #include "libkmod-private.h"
32
33 /**
34 * SECTION:libkmod-loaded
35 * @short_description: currently loaded modules
36 *
37 * Information about currently loaded modules, as reported by Linux kernel
38 */
39 KMOD_EXPORT int kmod_loaded_get_list(struct kmod_ctx *ctx,
40 struct kmod_list **list)
41 {
42 struct kmod_list *l = NULL;
43 FILE *fp;
44 char line[4096];
45
46 if (ctx == NULL || list == NULL)
47 return -ENOENT;
48
49 fp = fopen("/proc/modules", "r");
50 if (fp == NULL) {
51 int err = -errno;
52 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
53 return err;
54 }
55
56 while (fgets(line, sizeof(line), fp)) {
57 struct kmod_module *m;
58 struct kmod_list *node;
59 int err;
60 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
61
62 err = kmod_module_new_from_name(ctx, name, &m);
63 if (err < 0) {
64 ERR(ctx, "could not get module from name '%s': %s\n",
65 name, strerror(-err));
66 continue;
67 }
68
69 node = kmod_list_append(l, m);
70 if (node)
71 l = node;
72 else {
73 ERR(ctx, "out of memory\n");
74 kmod_module_unref(m);
75 }
76 }
77
78 fclose(fp);
79 *list = l;
80
81 return 0;
82 }