]>
git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-loaded.c
b14976595cfc120912fe13d61e7195d4c007506a
2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011 ProFUSION embedded systems
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.
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.
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
31 #include "libkmod-private.h"
34 * SECTION:libkmod-loaded
35 * @short_description: currently loaded modules
37 * Information about currently loaded modules, as reported by Linux kernel
43 * Opaque object representing a loaded module.
48 struct kmod_list
*modules
;
52 struct kmod_loaded_module
{
60 KMOD_EXPORT
int kmod_loaded_new(struct kmod_ctx
*ctx
, struct kmod_loaded
**mod
)
62 struct kmod_loaded
*m
;
64 m
= calloc(1, sizeof(*m
));
69 m
->ctx
= kmod_ref(ctx
);
74 KMOD_EXPORT
struct kmod_loaded
*kmod_loaded_ref(struct kmod_loaded
*mod
)
82 static void loaded_modules_free_module(struct kmod_loaded_module
*m
)
89 static void loaded_modules_free(struct kmod_loaded
*mod
)
91 while (mod
->modules
!= NULL
) {
92 loaded_modules_free_module(mod
->modules
->data
);
93 mod
->modules
= kmod_list_remove(mod
->modules
);
97 KMOD_EXPORT
struct kmod_loaded
*kmod_loaded_unref(struct kmod_loaded
*mod
)
102 if (--mod
->refcount
> 0)
105 DBG(mod
->ctx
, "kmod_loaded %p released\n", mod
);
107 kmod_unref(mod
->ctx
);
108 loaded_modules_free(mod
);
113 static int loaded_modules_parse(struct kmod_loaded
*mod
,
114 struct kmod_list
**list
)
116 struct kmod_list
*l
= NULL
;
120 fp
= fopen("/proc/modules", "r");
124 while (fgets(line
, sizeof(line
), fp
)) {
126 struct kmod_loaded_module
*m
;
128 m
= calloc(1, sizeof(*m
));
132 tok
= strtok(line
, " \t");
133 m
->name
= strdup(tok
);
135 tok
= strtok(NULL
, " \t\n");
138 /* Null if no module unloading is supported */
139 tok
= strtok(NULL
, " \t\n");
143 m
->use_count
= atoi(tok
);
144 tok
= strtok(NULL
, "\n");
148 /* Strip trailing comma */
149 if (strchr(tok
, ',')) {
151 tok
= strtok(tok
, " \t");
152 end
= &tok
[strlen(tok
) - 1];
155 m
->deps
= strdup(tok
);
157 } else if (tok
[0] == '-' && tok
[1] == '\0')
159 else if (tok
[0] == '-' && isspace(tok
[1]))
162 tok
= strtok(tok
, " \t\n");
166 tok
= strtok(NULL
, " \t\n");
170 m
->addr
= strtoull(tok
, NULL
, 16);
173 l
= kmod_list_append(l
, m
);
185 loaded_modules_free(mod
);
190 KMOD_EXPORT
int kmod_loaded_get_list(struct kmod_loaded
*mod
,
191 struct kmod_list
**list
)
197 int err
= loaded_modules_parse(mod
, &mod
->modules
);
202 *list
= mod
->modules
;
207 KMOD_EXPORT
int kmod_loaded_get_module_info(const struct kmod_list
*entry
,
209 long *size
, int *use_count
,
213 struct kmod_loaded_module
*m
;
225 *use_count
= m
->use_count
;
234 extern long delete_module(const char *name
, unsigned int flags
);
236 KMOD_EXPORT
int kmod_loaded_remove_module(struct kmod_loaded
*mod
,
237 struct kmod_list
*entry
,
240 struct kmod_loaded_module
*m
;
251 /* Filter out other flags */
252 flags
&= (KMOD_REMOVE_FORCE
| KMOD_REMOVE_NOWAIT
);
254 err
= delete_module(m
->name
, flags
);
256 ERR(mod
->ctx
, "Removing '%s': %s\n", m
->name
,
261 loaded_modules_free_module(m
);
262 kmod_list_remove(entry
);