blob: d26ce0ab7ce237e916cc31a5e20082a8753eb9a3 [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -02001/*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011 ProFUSION embedded systems
Lucas De Marchi8f788d52011-11-25 01:22:56 -02005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02008 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchi8f788d52011-11-25 01:22:56 -020010 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Lucas De Marchi7636e722011-12-01 17:56:03 -020021#include <assert.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020022#include <stdio.h>
23#include <stdlib.h>
24#include <stddef.h>
25#include <stdarg.h>
26#include <unistd.h>
27#include <errno.h>
28#include <string.h>
29#include <ctype.h>
30#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020031#include <limits.h>
32#include <dirent.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020033#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/mman.h>
36#include <string.h>
37
38#include "libkmod.h"
39#include "libkmod-private.h"
40
41/**
42 * kmod_module:
43 *
44 * Opaque object representing a module.
45 */
46struct kmod_module {
47 struct kmod_ctx *ctx;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020048 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020049 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020050 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020051 char *options;
52 char *install_commands;
53 char *remove_commands;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020054 char *alias; /* only set if this module was created from an alias */
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020055 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020056 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020057 struct {
58 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020059 bool options : 1;
60 bool install_commands : 1;
61 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020062 } init;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020063};
64
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -020065inline char *modname_normalize(const char *modname, char buf[NAME_MAX],
Lucas De Marchi6c343b12011-12-06 09:01:01 -020066 size_t *len)
Lucas De Marchi8f788d52011-11-25 01:22:56 -020067{
Lucas De Marchid753b8c2011-12-05 18:14:51 -020068 size_t s;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020069
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020070 for (s = 0; s < NAME_MAX - 1; s++) {
71 const char c = modname[s];
72 if (c == '-')
73 buf[s] = '_';
74 else if (c == '\0' || c == '.')
75 break;
76 else
77 buf[s] = c;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020078 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -020079
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020080 buf[s] = '\0';
Lucas De Marchid753b8c2011-12-05 18:14:51 -020081
82 if (len)
83 *len = s;
84
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020085 return buf;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020086}
87
Lucas De Marchi6c343b12011-12-06 09:01:01 -020088static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len)
89{
90 char *modname;
91
92 modname = basename(path);
93 if (modname == NULL || modname[0] == '\0')
94 return NULL;
95
96 return modname_normalize(modname, buf, len);
97}
98
Lucas De Marchic35347f2011-12-12 10:48:02 -020099static inline const char *path_join(const char *path, size_t prefixlen,
100 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200101{
102 size_t pathlen;
103
104 if (path[0] == '/')
105 return path;
106
107 pathlen = strlen(path);
108 if (prefixlen + pathlen + 1 >= PATH_MAX)
109 return NULL;
110
111 memcpy(buf + prefixlen, path, pathlen + 1);
112 return buf;
113}
114
Lucas De Marchi671d4892011-12-05 20:23:05 -0200115int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200116{
117 struct kmod_ctx *ctx = mod->ctx;
118 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200119 const char *dirname;
120 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200121 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200122 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200123 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200124
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200125 if (mod->init.dep)
126 return mod->n_dep;
127 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200128 mod->init.dep = true;
129
130 p = strchr(line, ':');
131 if (p == NULL)
132 return 0;
133
Lucas De Marchi671d4892011-12-05 20:23:05 -0200134 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200135 dirname = kmod_get_dirname(mod->ctx);
136 dirnamelen = strlen(dirname);
137 if (dirnamelen + 2 >= PATH_MAX)
138 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200139
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200140 memcpy(buf, dirname, dirnamelen);
141 buf[dirnamelen] = '/';
142 dirnamelen++;
143 buf[dirnamelen] = '\0';
144
145 if (mod->path == NULL) {
146 const char *str = path_join(line, dirnamelen, buf);
147 if (str == NULL)
148 return 0;
149 mod->path = strdup(str);
150 if (mod->path == NULL)
151 return 0;
152 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200153
Lucas De Marchi7636e722011-12-01 17:56:03 -0200154 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200155 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
156 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200157 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200158 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200159
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200160 path = path_join(p, dirnamelen, buf);
161 if (path == NULL) {
162 ERR(ctx, "could not join path '%s' and '%s'.\n",
163 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200164 goto fail;
165 }
166
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200167 err = kmod_module_new_from_path(ctx, path, &depmod);
168 if (err < 0) {
169 ERR(ctx, "ctx=%p path=%s error=%s\n",
170 ctx, path, strerror(-err));
171 goto fail;
172 }
173
174 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200175
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200176 list = kmod_list_append(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200177 n++;
178 }
179
180 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
181
182 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200183 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200184 return n;
185
186fail:
187 kmod_module_unref_list(list);
188 mod->init.dep = false;
189 return err;
190}
191
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200192/**
193 * kmod_module_new_from_name:
194 * @ctx: kmod library context
195 * @name: name of the module
196 * @mod: where to save the created struct kmod_module
197 *
198 * Create a new struct kmod_module using the module name. @name can not be an
199 * alias, file name or anything else; it must be a module name. There's no
200 * check if the module does exists in the system.
201 *
202 * This function is also used internally by many others that return a new
203 * struct kmod_module or a new list of modules.
204 *
205 * The initial refcount is 1, and needs to be decremented to release the
206 * resources of the kmod_module. Since libkmod keeps track of all
207 * kmod_modules created, they are all released upon @ctx destruction too. Do
208 * not unref @ctx before all the desired operations with the returned
209 * kmod_module are done.
210 *
211 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
212 * module name or if memory allocation failed.
213 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200214KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
215 const char *name,
216 struct kmod_module **mod)
217{
218 struct kmod_module *m;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200219 size_t namelen;
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200220 char name_norm[NAME_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200221 char *namesep;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200222
223 if (ctx == NULL || name == NULL)
224 return -ENOENT;
225
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200226 alias_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200227
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200228 m = kmod_pool_get_module(ctx, name_norm);
229 if (m != NULL) {
230 *mod = kmod_module_ref(m);
231 return 0;
232 }
233
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200234 m = malloc(sizeof(*m) + namelen + 1);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200235 if (m == NULL) {
236 free(m);
237 return -ENOMEM;
238 }
239
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200240 memset(m, 0, sizeof(*m));
241
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200242 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200243 m->name = (char *)m + sizeof(*m);
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200244 memcpy(m->name, name_norm, namelen + 1);
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200245
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200246 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200247
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200248 /* set alias later, so m->name is still modname/modalias */
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200249 kmod_pool_add_module(ctx, m);
250
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200251 namesep = strchr(m->name, '/');
252 if (namesep != NULL) {
253 *namesep = '\0';
254 m->alias = namesep + 1;
255 }
256
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200257 *mod = m;
258
259 return 0;
260}
261
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200262int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
263 const char *name, struct kmod_module **mod)
264{
265 int err;
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200266 char key[NAME_MAX];
267 size_t namelen = strlen(name);
268 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200269
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200270 if (namelen + aliaslen + 2 > NAME_MAX)
271 return -ENAMETOOLONG;
272
273 memcpy(key, name, namelen);
274 memcpy(key + namelen + 1, alias, aliaslen + 1);
275 key[namelen] = '/';
276
277 err = kmod_module_new_from_name(ctx, key, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200278 if (err < 0)
279 return err;
280
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200281 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200282}
283
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200284/**
285 * kmod_module_new_from_path:
286 * @ctx: kmod library context
287 * @path: path where to find the given module
288 * @mod: where to save the created struct kmod_module
289 *
290 * Create a new struct kmod_module using the module path. @path must be an
291 * existent file with in the filesystem and must be accessible to libkmod.
292 *
293 * The initial refcount is 1, and needs to be decremented to release the
294 * resources of the kmod_module. Since libkmod keeps track of all
295 * kmod_modules created, they are all released upon @ctx destruction too. Do
296 * not unref @ctx before all the desired operations with the returned
297 * kmod_module are done.
298 *
299 * If @path is relative, it's treated as relative to the current working
300 * directory. Otherwise, give an absolute path.
301 *
302 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
303 * it's not a valid file for a kmod_module or if memory allocation failed.
304 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200305KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
306 const char *path,
307 struct kmod_module **mod)
308{
309 struct kmod_module *m;
310 int err;
311 struct stat st;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200312 char name[NAME_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200313 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200314 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200315
316 if (ctx == NULL || path == NULL)
317 return -ENOENT;
318
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200319 abspath = path_make_absolute_cwd(path);
320 if (abspath == NULL)
321 return -ENOMEM;
322
323 err = stat(abspath, &st);
324 if (err < 0) {
325 free(abspath);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200326 return -errno;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200327 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200328
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200329 if (path_to_modname(path, name, &namelen) == NULL) {
330 free(abspath);
331 return -ENOENT;
332 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200333
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200334 m = kmod_pool_get_module(ctx, name);
335 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200336 if (m->path == NULL)
337 m->path = abspath;
338 else if (streq(m->path, abspath))
339 free(abspath);
340 else {
341 ERR(ctx, "kmod_module '%s' already exists with different path\n",
342 name);
343 free(abspath);
344 return -EEXIST;
345 }
346
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200347 *mod = kmod_module_ref(m);
348 return 0;
349 }
350
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200351 m = malloc(sizeof(*m) + namelen + 1);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200352 if (m == NULL)
353 return -errno;
354
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200355 memset(m, 0, sizeof(*m));
356
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200357 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200358 m->name = (char *)m + sizeof(*m);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200359 memcpy(m->name, name, namelen);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200360 m->path = abspath;
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200361 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200362
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200363 kmod_pool_add_module(ctx, m);
364
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200365 *mod = m;
366
367 return 0;
368}
369
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200370/**
371 * kmod_module_unref:
372 * @mod: kmod module
373 *
374 * Drop a reference of the kmod module. If the refcount reaches zero, its
375 * resources are released.
376 *
377 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
378 * returns the passed @mod with its refcount decremented.
379 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200380KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
381{
382 if (mod == NULL)
383 return NULL;
384
385 if (--mod->refcount > 0)
386 return mod;
387
388 DBG(mod->ctx, "kmod_module %p released\n", mod);
389
Lucas De Marchi7636e722011-12-01 17:56:03 -0200390 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200391 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200392 free(mod->options);
393 free(mod->install_commands);
394 free(mod->remove_commands);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200395 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200396 free(mod);
397 return NULL;
398}
399
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200400/**
401 * kmod_module_ref:
402 * @mod: kmod module
403 *
404 * Take a reference of the kmod module, incrementing its refcount.
405 *
406 * Returns: the passed @module with its refcount incremented.
407 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200408KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
409{
410 if (mod == NULL)
411 return NULL;
412
413 mod->refcount++;
414
415 return mod;
416}
417
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200418#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
419 do { \
420 if ((_err) < 0) \
421 goto _label_err; \
422 if (*(_list) != NULL) \
423 goto finish; \
424 } while (0)
425
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200426/**
427 * kmod_module_new_from_lookup:
428 * @ctx: kmod library context
429 * @given_alias: alias to look for
430 * @list: an empty list where to save the list of modules matching
431 * @given_alias
432 *
433 * Create a new list of kmod modules using an alias or module name and lookup
434 * libkmod's configuration files and indexes in order to find the module.
435 * Once it's found in one of the places, it stops searching and create the
436 * list of modules that is saved in @list.
437 *
438 * The search order is: 1. aliases in configuration file; 2. module names in
439 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
440 * in modules.alias index.
441 *
442 * The initial refcount is 1, and needs to be decremented to release the
443 * resources of the kmod_module. The returned @list must be released by
444 * calling kmod_module_unref_list(). Since libkmod keeps track of all
445 * kmod_modules created, they are all released upon @ctx destruction too. Do
446 * not unref @ctx before all the desired operations with the returned list are
447 * completed.
448 *
449 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
450 * methods failed, which is basically due to memory allocation fail. If module
451 * is not found, it still returns 0, but @list is an empty list.
452 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200453KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200454 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200455 struct kmod_list **list)
456{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200457 int err;
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200458 char alias[NAME_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200459
Lucas De Marchi4308b172011-12-13 10:26:04 -0200460 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200461 return -ENOENT;
462
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200463 if (list == NULL || *list != NULL) {
464 ERR(ctx, "An empty list is needed to create lookup\n");
465 return -ENOSYS;
466 }
467
Lucas De Marchid470db12011-12-13 10:28:00 -0200468 if (alias_normalize(given_alias, alias, NULL) < 0)
469 return -EINVAL;
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200470
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200471 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200472 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200473 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200474
Lucas De Marchi64700e42011-12-01 15:57:53 -0200475 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
476 CHECK_ERR_AND_FINISH(err, fail, list, finish);
477
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200478 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
479 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200480
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200481// TODO: add lookup for install commands here.
482
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200483 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
484 CHECK_ERR_AND_FINISH(err, fail, list, finish);
485
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200486finish:
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200487
488 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200489fail:
490 kmod_module_unref_list(*list);
491 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200492 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200493}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200494#undef CHECK_ERR_AND_FINISH
495
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200496/**
497 * kmod_module_unref:
498 * @list: list of kmod modules
499 *
500 * Drop a reference of each kmod module in @list and releases the resources
501 * taken by the list itself.
502 *
503 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
504 * returns the passed @mod with its refcount decremented.
505 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200506KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
507{
508 for (; list != NULL; list = kmod_list_remove(list))
509 kmod_module_unref(list->data);
510
511 return 0;
512}
513
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200514/**
515 * kmod_module_unref:
516 * @mod: kmod module
517 *
518 * Search the modules.dep index to find the dependencies of the given @mod.
519 * The result is cached in @mod, so subsequent calls to this function will
520 * return the already searched list of modules.
521 *
522 * Returns: NULL on failure or if there are any dependencies. Otherwise it
523 * returns a list of kmod modules that can be released by calling
524 * kmod_module_unref_list().
525 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200526KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200527{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200528 struct kmod_list *l, *l_new, *list_new = NULL;
529
530 if (mod == NULL)
531 return NULL;
532
Lucas De Marchi671d4892011-12-05 20:23:05 -0200533 if (!mod->init.dep) {
534 /* lazy init */
535 char *line = kmod_search_moddep(mod->ctx, mod->name);
536
537 if (line == NULL)
538 return NULL;
539
540 kmod_module_parse_depline((struct kmod_module *)mod, line);
541 free(line);
542
543 if (!mod->init.dep)
544 return NULL;
545 }
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200546
547 kmod_list_foreach(l, mod->dep) {
548 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
549 if (l_new == NULL) {
550 kmod_module_unref(l->data);
551 goto fail;
552 }
553
554 list_new = l_new;
555 }
556
557 return list_new;
558
559fail:
560 ERR(mod->ctx, "out of memory\n");
561 kmod_module_unref_list(list_new);
562 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200563}
564
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200565/**
566 * kmod_module_get_module:
567 * @entry: an entry in a list of kmod modules.
568 *
569 * Get the kmod module of this @entry in the list, increasing its refcount.
570 * After it's used, unref it. Since the refcount is incremented upon return,
571 * you still have to call kmod_module_unref_list() to release the list of kmod
572 * modules.
573 *
574 * Returns: NULL on failure or the kmod_module contained in this list entry
575 * with its refcount incremented.
576 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200577KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200578{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200579 if (entry == NULL)
580 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200581
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200582 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200583}
584
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200585/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200586 * kmod_module_get_name:
587 * @mod: kmod module
588 *
589 * Get the name of this kmod module. Name is always available, independently
590 * if it was created by kmod_module_new_from_name() or another function and
591 * it's always normalized (dashes are replaced with underscores).
592 *
593 * Returns: the name of this kmod module.
594 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200595KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200596{
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200597 return mod->name;
598}
599
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200600/**
601 * kmod_module_get_path:
602 * @mod: kmod module
603 *
604 * Get the path of this kmod module. If this kmod module was not created by
605 * path, it can search the modules.dep index in order to find out the module
606 * under context's dirname (see kmod_get_dirname()).
607 *
608 * Returns: the path of this kmod module or NULL if such information is not
609 * available.
610 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200611KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200612{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200613 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200614
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200615 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200616
Lucas De Marchie005fac2011-12-08 10:42:34 -0200617 if (mod->path != NULL)
618 return mod->path;
619 if (mod->init.dep)
620 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200621
Lucas De Marchie005fac2011-12-08 10:42:34 -0200622 /* lazy init */
623 line = kmod_search_moddep(mod->ctx, mod->name);
624 if (line == NULL)
625 return NULL;
626
627 kmod_module_parse_depline((struct kmod_module *) mod, line);
628 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200629
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200630 return mod->path;
631}
632
633
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200634extern long delete_module(const char *name, unsigned int flags);
635
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200636/**
637 * kmod_module_remove_module:
638 * @mod: kmod module
639 * @flags: flags to pass to Linux kernel when removing the module
640 *
641 * Remove a module from Linux kernel.
642 *
643 * Returns: 0 on success or < 0 on failure.
644 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200645KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
646 unsigned int flags)
647{
648 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200649
650 if (mod == NULL)
651 return -ENOENT;
652
653 /* Filter out other flags */
654 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
655
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200656 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200657 if (err != 0) {
Lucas De Marchi63af0612011-12-14 12:07:37 -0200658 ERR(mod->ctx, "Could not remove '%s': %s\n", mod->name,
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200659 strerror(-err));
660 return err;
661 }
662
663 return 0;
664}
665
666extern long init_module(void *mem, unsigned long len, const char *args);
667
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200668/**
669 * kmod_module_insert_module:
670 * @mod: kmod module
671 * @flags: flags are not passed to Linux Kernel, but instead it dictates the
672 * behavior of this function. They are not implemented yet.
673 * @options: module's options to pass to Linux Kernel.
674 *
675 * Insert a module in Linux kernel. It opens the file pointed by @mod,
676 * mmap'ing it and passing to kernel.
677 *
678 * Returns: 0 on success or < 0 on failure.
679 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200680KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200681 unsigned int flags,
682 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200683{
684 int err;
685 void *mmaped_file;
686 struct stat st;
687 int fd;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200688 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200689
690 if (mod == NULL)
691 return -ENOENT;
692
693 if (mod->path == NULL) {
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200694 ERR(mod->ctx, "Not supported to load a module by name yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200695 return -ENOSYS;
696 }
697
698 if (flags != 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200699 INFO(mod->ctx, "Flags are not implemented yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200700
701 if ((fd = open(mod->path, O_RDONLY)) < 0) {
702 err = -errno;
703 return err;
704 }
705
Lucas De Marchib418a822011-12-01 23:13:27 -0200706 fstat(fd, &st);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200707
708 if ((mmaped_file = mmap(0, st.st_size, PROT_READ,
709 MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
710 close(fd);
711 return -errno;
712 }
713
714 err = init_module(mmaped_file, st.st_size, args);
715 if (err < 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200716 ERR(mod->ctx, "Failed to insert module '%s'\n", mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200717
718 munmap(mmaped_file, st.st_size);
719 close(fd);
720
721 return err;
722}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200723
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200724/**
725 * kmod_module_get_options:
726 * @mod: kmod module
727 *
728 * Get options of this kmod module. Options come from the configuration file
729 * and are cached in @mod. The first call to this function will search for
730 * this module in configuration and subsequent calls return the cached string.
731 *
732 * Returns: a string with all the options separated by spaces. This string is
733 * owned by @mod, do not free it.
734 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200735KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
736{
737 if (mod == NULL)
738 return NULL;
739
740 if (!mod->init.options) {
741 /* lazy init */
742 struct kmod_module *m = (struct kmod_module *)mod;
743 const struct kmod_list *l, *ctx_options;
744 char *opts = NULL;
745 size_t optslen = 0;
746
747 ctx_options = kmod_get_options(mod->ctx);
748
749 kmod_list_foreach(l, ctx_options) {
750 const char *modname = kmod_option_get_modname(l);
751 const char *str;
752 size_t len;
753 void *tmp;
754
Lucas De Marchi07b8c822011-12-13 14:21:24 -0200755 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
756 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
757 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200758 continue;
759
Lucas De Marchi07b8c822011-12-13 14:21:24 -0200760 DBG(mod->ctx, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200761 str = kmod_option_get_options(l);
762 len = strlen(str);
763 if (len < 1)
764 continue;
765
766 tmp = realloc(opts, optslen + len + 2);
767 if (tmp == NULL) {
768 free(opts);
769 goto failed;
770 }
771
772 opts = tmp;
773
774 if (optslen > 0) {
775 opts[optslen] = ' ';
776 optslen++;
777 }
778
779 memcpy(opts + optslen, str, len);
780 optslen += len;
781 opts[optslen] = '\0';
782 }
783
784 m->init.options = true;
785 m->options = opts;
786 }
787
788 return mod->options;
789
790failed:
791 ERR(mod->ctx, "out of memory\n");
792 return NULL;
793}
794
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200795/**
796 * kmod_module_get_install_commands:
797 * @mod: kmod module
798 *
799 * Get install commands for this kmod module. Install commands come from the
800 * configuration file and are cached in @mod. The first call to this function
801 * will search for this module in configuration and subsequent calls return
802 * the cached string. The install commands are returned as they were in the
803 * configuration, concatenated by ';'. No other processing is made in this
804 * string.
805 *
806 * Returns: a string with all install commands separated by semicolons. This
807 * string is owned by @mod, do not free it.
808 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200809KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
810{
811 if (mod == NULL)
812 return NULL;
813
814 if (!mod->init.install_commands) {
815 /* lazy init */
816 struct kmod_module *m = (struct kmod_module *)mod;
817 const struct kmod_list *l, *ctx_install_commands;
818 char *cmds = NULL;
819 size_t cmdslen = 0;
820
821 ctx_install_commands = kmod_get_install_commands(mod->ctx);
822
823 kmod_list_foreach(l, ctx_install_commands) {
824 const char *modname = kmod_command_get_modname(l);
825 const char *str;
826 size_t len;
827 void *tmp;
828
829 if (strcmp(modname, mod->name) != 0)
830 continue;
831
832 str = kmod_command_get_command(l);
833 len = strlen(str);
834 if (len < 1)
835 continue;
836
837 tmp = realloc(cmds, cmdslen + len + 2);
838 if (tmp == NULL) {
839 free(cmds);
840 goto failed;
841 }
842
843 cmds = tmp;
844
845 if (cmdslen > 0) {
846 cmds[cmdslen] = ';';
847 cmdslen++;
848 }
849
850 memcpy(cmds + cmdslen, str, len);
851 cmdslen += len;
852 cmds[cmdslen] = '\0';
853 }
854
855 m->init.install_commands = true;
856 m->install_commands = cmds;
857 }
858
859 return mod->install_commands;
860
861failed:
862 ERR(mod->ctx, "out of memory\n");
863 return NULL;
864}
865
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200866/**
867 * kmod_module_get_remove_commands:
868 * @mod: kmod module
869 *
870 * Get remove commands for this kmod module. Remove commands come from the
871 * configuration file and are cached in @mod. The first call to this function
872 * will search for this module in configuration and subsequent calls return
873 * the cached string. The remove commands are returned as they were in the
874 * configuration, concatenated by ';'. No other processing is made in this
875 * string.
876 *
877 * Returns: a string with all remove commands separated by semicolons. This
878 * string is owned by @mod, do not free it.
879 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200880KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
881{
882 if (mod == NULL)
883 return NULL;
884
885 if (!mod->init.remove_commands) {
886 /* lazy init */
887 struct kmod_module *m = (struct kmod_module *)mod;
888 const struct kmod_list *l, *ctx_remove_commands;
889 char *cmds = NULL;
890 size_t cmdslen = 0;
891
892 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
893
894 kmod_list_foreach(l, ctx_remove_commands) {
895 const char *modname = kmod_command_get_modname(l);
896 const char *str;
897 size_t len;
898 void *tmp;
899
900 if (strcmp(modname, mod->name) != 0)
901 continue;
902
903 str = kmod_command_get_command(l);
904 len = strlen(str);
905 if (len < 1)
906 continue;
907
908 tmp = realloc(cmds, cmdslen + len + 2);
909 if (tmp == NULL) {
910 free(cmds);
911 goto failed;
912 }
913
914 cmds = tmp;
915
916 if (cmdslen > 0) {
917 cmds[cmdslen] = ';';
918 cmdslen++;
919 }
920
921 memcpy(cmds + cmdslen, str, len);
922 cmdslen += len;
923 cmds[cmdslen] = '\0';
924 }
925
926 m->init.remove_commands = true;
927 m->remove_commands = cmds;
928 }
929
930 return mod->remove_commands;
931
932failed:
933 ERR(mod->ctx, "out of memory\n");
934 return NULL;
935}
936
937/**
938 * SECTION:libkmod-loaded
939 * @short_description: currently loaded modules
940 *
941 * Information about currently loaded modules, as reported by Linux kernel.
942 * These information are not cached by libkmod and are always read from /sys
943 * and /proc/modules.
944 */
945
946/**
947 * kmod_module_new_from_loaded:
948 * @ctx: kmod library context
949 * @list: where to save the list of loaded modules
950 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200951 * Create a new list of kmod modules with all modules currently loaded in
952 * kernel. It uses /proc/modules to get the names of loaded modules and to
953 * create kmod modules by calling kmod_module_new_from_name() in each of them.
954 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200955 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200956 * The initial refcount is 1, and needs to be decremented to release the
957 * resources of the kmod_module. The returned @list must be released by
958 * calling kmod_module_unref_list(). Since libkmod keeps track of all
959 * kmod_modules created, they are all released upon @ctx destruction too. Do
960 * not unref @ctx before all the desired operations with the returned list are
961 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200962 *
963 * Returns: 0 on success or < 0 on error.
964 */
965KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
966 struct kmod_list **list)
967{
968 struct kmod_list *l = NULL;
969 FILE *fp;
970 char line[4096];
971
972 if (ctx == NULL || list == NULL)
973 return -ENOENT;
974
975 fp = fopen("/proc/modules", "r");
976 if (fp == NULL) {
977 int err = -errno;
978 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
979 return err;
980 }
981
982 while (fgets(line, sizeof(line), fp)) {
983 struct kmod_module *m;
984 struct kmod_list *node;
985 int err;
986 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
987
988 err = kmod_module_new_from_name(ctx, name, &m);
989 if (err < 0) {
990 ERR(ctx, "could not get module from name '%s': %s\n",
991 name, strerror(-err));
992 continue;
993 }
994
995 node = kmod_list_append(l, m);
996 if (node)
997 l = node;
998 else {
999 ERR(ctx, "out of memory\n");
1000 kmod_module_unref(m);
1001 }
1002 }
1003
1004 fclose(fp);
1005 *list = l;
1006
1007 return 0;
1008}
1009
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001010/**
1011 * kmod_module_initstate_str:
1012 * @state: the state as returned by kmod_module_get_initstate()
1013 *
1014 * Translate a initstate to a string.
1015 *
1016 * Returns: the string associated to the @state. This string is statically
1017 * allocated, do not free it.
1018 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001019KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1020{
1021 switch (state) {
1022 case KMOD_MODULE_BUILTIN:
1023 return "builtin";
1024 case KMOD_MODULE_LIVE:
1025 return "live";
1026 case KMOD_MODULE_COMING:
1027 return "coming";
1028 case KMOD_MODULE_GOING:
1029 return "going";
1030 default:
1031 return NULL;
1032 }
1033}
1034
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001035/**
1036 * kmod_module_get_initstate:
1037 * @mod: kmod module
1038 *
1039 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1040 * /sys filesystem.
1041 *
1042 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1043 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001044KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1045{
1046 char path[PATH_MAX], buf[32];
1047 int fd, err, pathlen;
1048
1049 pathlen = snprintf(path, sizeof(path),
1050 "/sys/module/%s/initstate", mod->name);
1051 fd = open(path, O_RDONLY);
1052 if (fd < 0) {
1053 err = -errno;
1054
1055 if (pathlen > (int)sizeof("/initstate") - 1) {
1056 struct stat st;
1057 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1058 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1059 return KMOD_MODULE_BUILTIN;
1060 }
1061
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001062 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001063 path, strerror(-err));
1064 return err;
1065 }
1066
1067 err = read_str_safe(fd, buf, sizeof(buf));
1068 close(fd);
1069 if (err < 0) {
1070 ERR(mod->ctx, "could not read from '%s': %s\n",
1071 path, strerror(-err));
1072 return err;
1073 }
1074
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001075 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001076 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001077 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001078 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001079 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001080 return KMOD_MODULE_GOING;
1081
1082 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1083 return -EINVAL;
1084}
1085
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001086/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001087 * kmod_module_get_size:
1088 * @mod: kmod module
1089 *
1090 * Get the size of this kmod module as returned by Linux kernel. It reads the
1091 * file /proc/modules to search for this module and get its size.
1092 *
1093 * Returns: the size of this kmod module.
1094 */
1095KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1096{
1097 // FIXME TODO: this should be available from /sys/module/foo
1098 FILE *fp;
1099 char line[4096];
1100 int lineno = 0;
1101 long size = -ENOENT;
1102
1103 if (mod == NULL)
1104 return -ENOENT;
1105
1106 fp = fopen("/proc/modules", "r");
1107 if (fp == NULL) {
1108 int err = -errno;
1109 ERR(mod->ctx,
1110 "could not open /proc/modules: %s\n", strerror(errno));
1111 return err;
1112 }
1113
1114 while (fgets(line, sizeof(line), fp)) {
1115 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1116 long value;
1117
1118 lineno++;
1119 if (tok == NULL || !streq(tok, mod->name))
1120 continue;
1121
1122 tok = strtok_r(NULL, " \t", &saveptr);
1123 if (tok == NULL) {
1124 ERR(mod->ctx,
1125 "invalid line format at /proc/modules:%d\n", lineno);
1126 break;
1127 }
1128
1129 value = strtol(tok, &endptr, 10);
1130 if (endptr == tok || *endptr != '\0') {
1131 ERR(mod->ctx,
1132 "invalid line format at /proc/modules:%d\n", lineno);
1133 break;
1134 }
1135
1136 size = value;
1137 break;
1138 }
1139 fclose(fp);
1140 return size;
1141}
1142
1143/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001144 * kmod_module_get_refcnt:
1145 * @mod: kmod module
1146 *
1147 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1148 * /sys filesystem.
1149 *
1150 * Returns: 0 on success or < 0 on failure.
1151 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001152KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1153{
1154 char path[PATH_MAX];
1155 long refcnt;
1156 int fd, err;
1157
1158 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
1159 fd = open(path, O_RDONLY);
1160 if (fd < 0) {
1161 err = -errno;
1162 ERR(mod->ctx, "could not open '%s': %s\n",
1163 path, strerror(errno));
1164 return err;
1165 }
1166
1167 err = read_str_long(fd, &refcnt, 10);
1168 close(fd);
1169 if (err < 0) {
1170 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1171 path, strerror(-err));
1172 return err;
1173 }
1174
1175 return (int)refcnt;
1176}
1177
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001178/**
1179 * kmod_module_get_holders:
1180 * @mod: kmod module
1181 *
1182 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1183 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1184 *
1185 * Returns: a new list of kmod modules on success or NULL on failure.
1186 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001187KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1188{
1189 char dname[PATH_MAX];
1190 struct kmod_list *list = NULL;
1191 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001192
1193 if (mod == NULL)
1194 return NULL;
1195 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1196
1197 d = opendir(dname);
1198 if (d == NULL) {
1199 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001200 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001201 return NULL;
1202 }
1203
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001204 for (;;) {
1205 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001206 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001207 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001208 int err;
1209
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001210 err = readdir_r(d, &de, &entp);
1211 if (err != 0) {
1212 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1213 mod->name, strerror(-err));
1214 goto fail;
1215 }
1216
1217 if (entp == NULL)
1218 break;
1219
1220 if (de.d_name[0] == '.') {
1221 if (de.d_name[1] == '\0' ||
1222 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001223 continue;
1224 }
1225
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001226 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001227 if (err < 0) {
1228 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001229 de.d_name, strerror(-err));
1230 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001231 }
1232
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001233 l = kmod_list_append(list, holder);
1234 if (l != NULL) {
1235 list = l;
1236 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001237 ERR(mod->ctx, "out of memory\n");
1238 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001239 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001240 }
1241 }
1242
1243 closedir(d);
1244 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001245
1246fail:
1247 closedir(d);
1248 kmod_module_unref_list(list);
1249 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001250}
1251
1252struct kmod_module_section {
1253 unsigned long address;
1254 char name[];
1255};
1256
1257static void kmod_module_section_free(struct kmod_module_section *section)
1258{
1259 free(section);
1260}
1261
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001262/**
1263 * kmod_module_get_sections:
1264 * @mod: kmod module
1265 *
1266 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1267 * structure contained in this list is internal to libkmod and their fields
1268 * can be obtained by calling kmod_module_section_get_name() and
1269 * kmod_module_section_get_address().
1270 *
1271 * After use, free the @list by calling kmod_module_section_free_list().
1272 *
1273 * Returns: a new list of kmod module sections on success or NULL on failure.
1274 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001275KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1276{
1277 char dname[PATH_MAX];
1278 struct kmod_list *list = NULL;
1279 DIR *d;
1280 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001281
1282 if (mod == NULL)
1283 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001284
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001285 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1286
1287 d = opendir(dname);
1288 if (d == NULL) {
1289 ERR(mod->ctx, "could not open '%s': %s\n",
1290 dname, strerror(errno));
1291 return NULL;
1292 }
1293
1294 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001295
1296 for (;;) {
1297 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001298 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001299 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001300 unsigned long address;
1301 size_t namesz;
1302 int fd, err;
1303
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001304 err = readdir_r(d, &de, &entp);
1305 if (err != 0) {
1306 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1307 mod->name, strerror(-err));
1308 goto fail;
1309 }
1310
1311 if (de.d_name[0] == '.') {
1312 if (de.d_name[1] == '\0' ||
1313 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001314 continue;
1315 }
1316
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001317 fd = openat(dfd, de.d_name, O_RDONLY);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001318 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001319 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1320 dname, de.d_name);
1321 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001322 }
1323
1324 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001325 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001326 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001327 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1328 dname, de.d_name);
1329 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001330 }
1331
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001332 namesz = strlen(de.d_name) + 1;
1333 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001334
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001335 if (section == NULL) {
1336 ERR(mod->ctx, "out of memory\n");
1337 goto fail;
1338 }
1339
1340 section->address = address;
1341 memcpy(section->name, de.d_name, namesz);
1342
1343 l = kmod_list_append(list, section);
1344 if (l != NULL) {
1345 list = l;
1346 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001347 ERR(mod->ctx, "out of memory\n");
1348 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001349 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001350 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001351 }
1352
1353 closedir(d);
1354 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001355
1356fail:
1357 closedir(d);
1358 kmod_module_unref_list(list);
1359 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001360}
1361
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001362/**
1363 * kmod_module_section_get_module_name:
1364 * @entry: a list entry representing a kmod module section
1365 *
1366 * Get the name of a kmod module section.
1367 *
1368 * After use, free the @list by calling kmod_module_section_free_list().
1369 *
1370 * Returns: the name of this kmod module section on success or NULL on
1371 * failure. The string is owned by the section, do not free it.
1372 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001373KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1374{
1375 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001376
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001377 if (entry == NULL)
1378 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001379
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001380 section = entry->data;
1381 return section->name;
1382}
1383
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001384/**
1385 * kmod_module_section_get_address:
1386 * @entry: a list entry representing a kmod module section
1387 *
1388 * Get the address of a kmod module section.
1389 *
1390 * After use, free the @list by calling kmod_module_section_free_list().
1391 *
1392 * Returns: the address of this kmod module section on success or ULONG_MAX
1393 * on failure.
1394 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001395KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1396{
1397 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001398
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001399 if (entry == NULL)
1400 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001401
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001402 section = entry->data;
1403 return section->address;
1404}
1405
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001406/**
1407 * kmod_module_section_free_list:
1408 * @list: kmod module section list
1409 *
1410 * Release the resources taken by @list
1411 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001412KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1413{
1414 while (list) {
1415 kmod_module_section_free(list->data);
1416 list = kmod_list_remove(list);
1417 }
1418}