blob: 2d20792b5866b65a41e418db95b276807ffcf2d5 [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>
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020037#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020038
39#include "libkmod.h"
40#include "libkmod-private.h"
41
42/**
43 * kmod_module:
44 *
45 * Opaque object representing a module.
46 */
47struct kmod_module {
48 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020049 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020050 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020051 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020052 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020053 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020054 const char *install_commands; /* owned by kmod_config */
55 const char *remove_commands; /* owned by kmod_config */
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020056 struct {
57 struct kmod_list *pre;
58 struct kmod_list *post;
59 } softdeps;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020060 char *alias; /* only set if this module was created from an alias */
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020061 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020062 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020063 struct {
64 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020065 bool options : 1;
66 bool install_commands : 1;
67 bool remove_commands : 1;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020068 bool softdeps : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020069 } init;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020070};
71
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -020072inline char *modname_normalize(const char *modname, char buf[NAME_MAX],
Lucas De Marchi6c343b12011-12-06 09:01:01 -020073 size_t *len)
Lucas De Marchi8f788d52011-11-25 01:22:56 -020074{
Lucas De Marchid753b8c2011-12-05 18:14:51 -020075 size_t s;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020076
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020077 for (s = 0; s < NAME_MAX - 1; s++) {
78 const char c = modname[s];
79 if (c == '-')
80 buf[s] = '_';
81 else if (c == '\0' || c == '.')
82 break;
83 else
84 buf[s] = c;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020085 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -020086
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020087 buf[s] = '\0';
Lucas De Marchid753b8c2011-12-05 18:14:51 -020088
89 if (len)
90 *len = s;
91
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020092 return buf;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020093}
94
Lucas De Marchi6c343b12011-12-06 09:01:01 -020095static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len)
96{
97 char *modname;
98
99 modname = basename(path);
100 if (modname == NULL || modname[0] == '\0')
101 return NULL;
102
103 return modname_normalize(modname, buf, len);
104}
105
Lucas De Marchic35347f2011-12-12 10:48:02 -0200106static inline const char *path_join(const char *path, size_t prefixlen,
107 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200108{
109 size_t pathlen;
110
111 if (path[0] == '/')
112 return path;
113
114 pathlen = strlen(path);
115 if (prefixlen + pathlen + 1 >= PATH_MAX)
116 return NULL;
117
118 memcpy(buf + prefixlen, path, pathlen + 1);
119 return buf;
120}
121
Lucas De Marchi671d4892011-12-05 20:23:05 -0200122int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200123{
124 struct kmod_ctx *ctx = mod->ctx;
125 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200126 const char *dirname;
127 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200128 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200129 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200130 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200131
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200132 if (mod->init.dep)
133 return mod->n_dep;
134 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200135 mod->init.dep = true;
136
137 p = strchr(line, ':');
138 if (p == NULL)
139 return 0;
140
Lucas De Marchi671d4892011-12-05 20:23:05 -0200141 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200142 dirname = kmod_get_dirname(mod->ctx);
143 dirnamelen = strlen(dirname);
144 if (dirnamelen + 2 >= PATH_MAX)
145 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200146
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200147 memcpy(buf, dirname, dirnamelen);
148 buf[dirnamelen] = '/';
149 dirnamelen++;
150 buf[dirnamelen] = '\0';
151
152 if (mod->path == NULL) {
153 const char *str = path_join(line, dirnamelen, buf);
154 if (str == NULL)
155 return 0;
156 mod->path = strdup(str);
157 if (mod->path == NULL)
158 return 0;
159 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200160
Lucas De Marchi7636e722011-12-01 17:56:03 -0200161 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200162 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
163 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200164 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200165 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200166
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200167 path = path_join(p, dirnamelen, buf);
168 if (path == NULL) {
169 ERR(ctx, "could not join path '%s' and '%s'.\n",
170 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200171 goto fail;
172 }
173
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200174 err = kmod_module_new_from_path(ctx, path, &depmod);
175 if (err < 0) {
176 ERR(ctx, "ctx=%p path=%s error=%s\n",
177 ctx, path, strerror(-err));
178 goto fail;
179 }
180
181 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200182
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200183 list = kmod_list_append(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200184 n++;
185 }
186
187 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
188
189 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200190 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200191 return n;
192
193fail:
194 kmod_module_unref_list(list);
195 mod->init.dep = false;
196 return err;
197}
198
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200199/**
200 * kmod_module_new_from_name:
201 * @ctx: kmod library context
202 * @name: name of the module
203 * @mod: where to save the created struct kmod_module
204 *
205 * Create a new struct kmod_module using the module name. @name can not be an
206 * alias, file name or anything else; it must be a module name. There's no
207 * check if the module does exists in the system.
208 *
209 * This function is also used internally by many others that return a new
210 * struct kmod_module or a new list of modules.
211 *
212 * The initial refcount is 1, and needs to be decremented to release the
213 * resources of the kmod_module. Since libkmod keeps track of all
214 * kmod_modules created, they are all released upon @ctx destruction too. Do
215 * not unref @ctx before all the desired operations with the returned
216 * kmod_module are done.
217 *
218 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
219 * module name or if memory allocation failed.
220 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200221KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
222 const char *name,
223 struct kmod_module **mod)
224{
225 struct kmod_module *m;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200226 size_t namelen;
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200227 char name_norm[NAME_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200228 char *namesep;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200229
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200230 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200231 return -ENOENT;
232
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200233 alias_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200234
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200235 m = kmod_pool_get_module(ctx, name_norm);
236 if (m != NULL) {
237 *mod = kmod_module_ref(m);
238 return 0;
239 }
240
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200241 namesep = strchr(name_norm, '/');
242 m = malloc(sizeof(*m) + (namesep == NULL ? 1 : 2) * namelen + 2);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200243 if (m == NULL) {
244 free(m);
245 return -ENOMEM;
246 }
247
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200248 memset(m, 0, sizeof(*m));
249
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200250 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200251 m->name = (char *)m + sizeof(*m);
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200252 memcpy(m->name, name_norm, namelen + 1);
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200253
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200254 if (namesep) {
255 size_t len = namesep - name_norm;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200256
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200257 m->name[len] = '\0';
258 m->alias = m->name + len + 1;
259 m->hashkey = m->name + namelen + 1;
260 memcpy(m->hashkey, name_norm, namelen + 1);
261 } else {
262 m->hashkey = m->name;
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200263 }
264
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200265 m->refcount = 1;
266 kmod_pool_add_module(ctx, m, m->hashkey);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200267 *mod = m;
268
269 return 0;
270}
271
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200272int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
273 const char *name, struct kmod_module **mod)
274{
275 int err;
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200276 char key[NAME_MAX];
277 size_t namelen = strlen(name);
278 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200279
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200280 if (namelen + aliaslen + 2 > NAME_MAX)
281 return -ENAMETOOLONG;
282
283 memcpy(key, name, namelen);
284 memcpy(key + namelen + 1, alias, aliaslen + 1);
285 key[namelen] = '/';
286
287 err = kmod_module_new_from_name(ctx, key, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200288 if (err < 0)
289 return err;
290
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200291 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200292}
293
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200294/**
295 * kmod_module_new_from_path:
296 * @ctx: kmod library context
297 * @path: path where to find the given module
298 * @mod: where to save the created struct kmod_module
299 *
300 * Create a new struct kmod_module using the module path. @path must be an
301 * existent file with in the filesystem and must be accessible to libkmod.
302 *
303 * The initial refcount is 1, and needs to be decremented to release the
304 * resources of the kmod_module. Since libkmod keeps track of all
305 * kmod_modules created, they are all released upon @ctx destruction too. Do
306 * not unref @ctx before all the desired operations with the returned
307 * kmod_module are done.
308 *
309 * If @path is relative, it's treated as relative to the current working
310 * directory. Otherwise, give an absolute path.
311 *
312 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
313 * it's not a valid file for a kmod_module or if memory allocation failed.
314 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200315KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
316 const char *path,
317 struct kmod_module **mod)
318{
319 struct kmod_module *m;
320 int err;
321 struct stat st;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200322 char name[NAME_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200323 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200324 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200325
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200326 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200327 return -ENOENT;
328
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200329 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200330 if (abspath == NULL) {
331 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200332 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200333 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200334
335 err = stat(abspath, &st);
336 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200337 err = -errno;
338 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200339 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200340 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200341 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200342
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200343 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200344 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200345 free(abspath);
346 return -ENOENT;
347 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200348
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200349 m = kmod_pool_get_module(ctx, name);
350 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200351 if (m->path == NULL)
352 m->path = abspath;
353 else if (streq(m->path, abspath))
354 free(abspath);
355 else {
356 ERR(ctx, "kmod_module '%s' already exists with different path\n",
357 name);
358 free(abspath);
359 return -EEXIST;
360 }
361
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200362 *mod = kmod_module_ref(m);
363 return 0;
364 }
365
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200366 m = malloc(sizeof(*m) + namelen + 1);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200367 if (m == NULL)
368 return -errno;
369
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200370 memset(m, 0, sizeof(*m));
371
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200372 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200373 m->name = (char *)m + sizeof(*m);
Lucas De Marchi9dec2442011-12-18 15:12:19 -0200374 memcpy(m->name, name, namelen + 1);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200375 m->path = abspath;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200376 m->hashkey = m->name;
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200377 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200378
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200379 kmod_pool_add_module(ctx, m, m->hashkey);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200380
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200381 *mod = m;
382
383 return 0;
384}
385
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200386/**
387 * kmod_module_unref:
388 * @mod: kmod module
389 *
390 * Drop a reference of the kmod module. If the refcount reaches zero, its
391 * resources are released.
392 *
393 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
394 * returns the passed @mod with its refcount decremented.
395 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200396KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
397{
398 if (mod == NULL)
399 return NULL;
400
401 if (--mod->refcount > 0)
402 return mod;
403
404 DBG(mod->ctx, "kmod_module %p released\n", mod);
405
Lucas De Marchi4084c172011-12-15 13:43:22 -0200406 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200407 kmod_module_unref_list(mod->softdeps.pre);
408 kmod_module_unref_list(mod->softdeps.post);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200409 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200410 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200411 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200412 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200413 free(mod);
414 return NULL;
415}
416
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200417/**
418 * kmod_module_ref:
419 * @mod: kmod module
420 *
421 * Take a reference of the kmod module, incrementing its refcount.
422 *
423 * Returns: the passed @module with its refcount incremented.
424 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200425KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
426{
427 if (mod == NULL)
428 return NULL;
429
430 mod->refcount++;
431
432 return mod;
433}
434
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200435#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
436 do { \
437 if ((_err) < 0) \
438 goto _label_err; \
439 if (*(_list) != NULL) \
440 goto finish; \
441 } while (0)
442
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200443/**
444 * kmod_module_new_from_lookup:
445 * @ctx: kmod library context
446 * @given_alias: alias to look for
447 * @list: an empty list where to save the list of modules matching
448 * @given_alias
449 *
450 * Create a new list of kmod modules using an alias or module name and lookup
451 * libkmod's configuration files and indexes in order to find the module.
452 * Once it's found in one of the places, it stops searching and create the
453 * list of modules that is saved in @list.
454 *
455 * The search order is: 1. aliases in configuration file; 2. module names in
456 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
457 * in modules.alias index.
458 *
459 * The initial refcount is 1, and needs to be decremented to release the
460 * resources of the kmod_module. The returned @list must be released by
461 * calling kmod_module_unref_list(). Since libkmod keeps track of all
462 * kmod_modules created, they are all released upon @ctx destruction too. Do
463 * not unref @ctx before all the desired operations with the returned list are
464 * completed.
465 *
466 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
467 * methods failed, which is basically due to memory allocation fail. If module
468 * is not found, it still returns 0, but @list is an empty list.
469 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200470KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200471 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200472 struct kmod_list **list)
473{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200474 int err;
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200475 char alias[NAME_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200476
Lucas De Marchi4308b172011-12-13 10:26:04 -0200477 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200478 return -ENOENT;
479
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200480 if (list == NULL || *list != NULL) {
481 ERR(ctx, "An empty list is needed to create lookup\n");
482 return -ENOSYS;
483 }
484
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200485 if (alias_normalize(given_alias, alias, NULL) < 0) {
486 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200487 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200488 }
489
490 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200491
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200492 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200493 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200494 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200495
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200496 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200497 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
498 CHECK_ERR_AND_FINISH(err, fail, list, finish);
499
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200500 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200501 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
502 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200503
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200504 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200505 err = kmod_lookup_alias_from_commands(ctx, alias, list);
506 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200507
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200508 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200509 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
510 CHECK_ERR_AND_FINISH(err, fail, list, finish);
511
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200512finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200513 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200514 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200515fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200516 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200517 kmod_module_unref_list(*list);
518 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200519 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200520}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200521#undef CHECK_ERR_AND_FINISH
522
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200523/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200524 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200525 * @list: list of kmod modules
526 *
527 * Drop a reference of each kmod module in @list and releases the resources
528 * taken by the list itself.
529 *
530 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
531 * returns the passed @mod with its refcount decremented.
532 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200533KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
534{
535 for (; list != NULL; list = kmod_list_remove(list))
536 kmod_module_unref(list->data);
537
538 return 0;
539}
540
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200541/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200542 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200543 * @mod: kmod module
544 *
545 * Search the modules.dep index to find the dependencies of the given @mod.
546 * The result is cached in @mod, so subsequent calls to this function will
547 * return the already searched list of modules.
548 *
549 * Returns: NULL on failure or if there are any dependencies. Otherwise it
550 * returns a list of kmod modules that can be released by calling
551 * kmod_module_unref_list().
552 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200553KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200554{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200555 struct kmod_list *l, *l_new, *list_new = NULL;
556
557 if (mod == NULL)
558 return NULL;
559
Lucas De Marchi671d4892011-12-05 20:23:05 -0200560 if (!mod->init.dep) {
561 /* lazy init */
562 char *line = kmod_search_moddep(mod->ctx, mod->name);
563
564 if (line == NULL)
565 return NULL;
566
567 kmod_module_parse_depline((struct kmod_module *)mod, line);
568 free(line);
569
570 if (!mod->init.dep)
571 return NULL;
572 }
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200573
574 kmod_list_foreach(l, mod->dep) {
575 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
576 if (l_new == NULL) {
577 kmod_module_unref(l->data);
578 goto fail;
579 }
580
581 list_new = l_new;
582 }
583
584 return list_new;
585
586fail:
587 ERR(mod->ctx, "out of memory\n");
588 kmod_module_unref_list(list_new);
589 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200590}
591
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200592/**
593 * kmod_module_get_module:
594 * @entry: an entry in a list of kmod modules.
595 *
596 * Get the kmod module of this @entry in the list, increasing its refcount.
597 * After it's used, unref it. Since the refcount is incremented upon return,
598 * you still have to call kmod_module_unref_list() to release the list of kmod
599 * modules.
600 *
601 * Returns: NULL on failure or the kmod_module contained in this list entry
602 * with its refcount incremented.
603 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200604KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200605{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200606 if (entry == NULL)
607 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200608
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200609 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200610}
611
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200612/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200613 * kmod_module_get_name:
614 * @mod: kmod module
615 *
616 * Get the name of this kmod module. Name is always available, independently
617 * if it was created by kmod_module_new_from_name() or another function and
618 * it's always normalized (dashes are replaced with underscores).
619 *
620 * Returns: the name of this kmod module.
621 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200622KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200623{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200624 if (mod == NULL)
625 return NULL;
626
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200627 return mod->name;
628}
629
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200630/**
631 * kmod_module_get_path:
632 * @mod: kmod module
633 *
634 * Get the path of this kmod module. If this kmod module was not created by
635 * path, it can search the modules.dep index in order to find out the module
636 * under context's dirname (see kmod_get_dirname()).
637 *
638 * Returns: the path of this kmod module or NULL if such information is not
639 * available.
640 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200641KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200642{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200643 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200644
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200645 if (mod == NULL)
646 return NULL;
647
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200648 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200649
Lucas De Marchie005fac2011-12-08 10:42:34 -0200650 if (mod->path != NULL)
651 return mod->path;
652 if (mod->init.dep)
653 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200654
Lucas De Marchie005fac2011-12-08 10:42:34 -0200655 /* lazy init */
656 line = kmod_search_moddep(mod->ctx, mod->name);
657 if (line == NULL)
658 return NULL;
659
660 kmod_module_parse_depline((struct kmod_module *) mod, line);
661 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200662
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200663 return mod->path;
664}
665
666
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200667extern long delete_module(const char *name, unsigned int flags);
668
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200669/**
670 * kmod_module_remove_module:
671 * @mod: kmod module
672 * @flags: flags to pass to Linux kernel when removing the module
673 *
674 * Remove a module from Linux kernel.
675 *
676 * Returns: 0 on success or < 0 on failure.
677 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200678KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
679 unsigned int flags)
680{
681 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200682
683 if (mod == NULL)
684 return -ENOENT;
685
686 /* Filter out other flags */
687 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
688
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200689 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200690 if (err != 0) {
Lucas De Marchi63af0612011-12-14 12:07:37 -0200691 ERR(mod->ctx, "Could not remove '%s': %s\n", mod->name,
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200692 strerror(-err));
693 return err;
694 }
695
696 return 0;
697}
698
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200699extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200700
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200701/**
702 * kmod_module_insert_module:
703 * @mod: kmod module
704 * @flags: flags are not passed to Linux Kernel, but instead it dictates the
705 * behavior of this function. They are not implemented yet.
706 * @options: module's options to pass to Linux Kernel.
707 *
708 * Insert a module in Linux kernel. It opens the file pointed by @mod,
709 * mmap'ing it and passing to kernel.
710 *
711 * Returns: 0 on success or < 0 on failure.
712 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200713KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200714 unsigned int flags,
715 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200716{
717 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200718 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200719 off_t size;
720 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200721 struct kmod_elf *elf = NULL;
722 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200723 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200724
725 if (mod == NULL)
726 return -ENOENT;
727
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200728 path = kmod_module_get_path(mod);
729 if (path == NULL) {
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200730 ERR(mod->ctx, "Not supported to load a module by name yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200731 return -ENOSYS;
732 }
733
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200734 file = kmod_file_open(path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200735 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200736 err = -errno;
737 return err;
738 }
739
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200740 size = kmod_file_get_size(file);
741 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200742
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200743 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
744 elf = kmod_elf_new(mem, size);
745 if (elf == NULL) {
746 err = -errno;
747 goto elf_failed;
748 }
749
750 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
751 err = kmod_elf_strip_section(elf, "__versions");
752 if (err < 0)
753 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
754 }
755
756 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
757 err = kmod_elf_strip_vermagic(elf);
758 if (err < 0)
759 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
760 }
761
762 mem = kmod_elf_get_memory(elf);
763 }
764
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200765 err = init_module(mem, size, args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200766 if (err < 0)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200767 ERR(mod->ctx, "Failed to insert module '%s'\n", path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200768
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200769 if (elf != NULL)
770 kmod_elf_unref(elf);
771elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200772 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200773
774 return err;
775}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200776
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200777/**
778 * kmod_module_get_options:
779 * @mod: kmod module
780 *
781 * Get options of this kmod module. Options come from the configuration file
782 * and are cached in @mod. The first call to this function will search for
783 * this module in configuration and subsequent calls return the cached string.
784 *
785 * Returns: a string with all the options separated by spaces. This string is
786 * owned by @mod, do not free it.
787 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200788KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
789{
790 if (mod == NULL)
791 return NULL;
792
793 if (!mod->init.options) {
794 /* lazy init */
795 struct kmod_module *m = (struct kmod_module *)mod;
796 const struct kmod_list *l, *ctx_options;
797 char *opts = NULL;
798 size_t optslen = 0;
799
800 ctx_options = kmod_get_options(mod->ctx);
801
802 kmod_list_foreach(l, ctx_options) {
803 const char *modname = kmod_option_get_modname(l);
804 const char *str;
805 size_t len;
806 void *tmp;
807
Lucas De Marchi07b8c822011-12-13 14:21:24 -0200808 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
809 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
810 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200811 continue;
812
Lucas De Marchi07b8c822011-12-13 14:21:24 -0200813 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 -0200814 str = kmod_option_get_options(l);
815 len = strlen(str);
816 if (len < 1)
817 continue;
818
819 tmp = realloc(opts, optslen + len + 2);
820 if (tmp == NULL) {
821 free(opts);
822 goto failed;
823 }
824
825 opts = tmp;
826
827 if (optslen > 0) {
828 opts[optslen] = ' ';
829 optslen++;
830 }
831
832 memcpy(opts + optslen, str, len);
833 optslen += len;
834 opts[optslen] = '\0';
835 }
836
837 m->init.options = true;
838 m->options = opts;
839 }
840
841 return mod->options;
842
843failed:
844 ERR(mod->ctx, "out of memory\n");
845 return NULL;
846}
847
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200848/**
849 * kmod_module_get_install_commands:
850 * @mod: kmod module
851 *
852 * Get install commands for this kmod module. Install commands come from the
853 * configuration file and are cached in @mod. The first call to this function
854 * will search for this module in configuration and subsequent calls return
855 * the cached string. The install commands are returned as they were in the
856 * configuration, concatenated by ';'. No other processing is made in this
857 * string.
858 *
859 * Returns: a string with all install commands separated by semicolons. This
860 * string is owned by @mod, do not free it.
861 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200862KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
863{
864 if (mod == NULL)
865 return NULL;
866
867 if (!mod->init.install_commands) {
868 /* lazy init */
869 struct kmod_module *m = (struct kmod_module *)mod;
870 const struct kmod_list *l, *ctx_install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200871
872 ctx_install_commands = kmod_get_install_commands(mod->ctx);
873
874 kmod_list_foreach(l, ctx_install_commands) {
875 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200876
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -0200877 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200878 continue;
879
Lucas De Marchi60f67602011-12-16 03:33:26 -0200880 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200881
Lucas De Marchi60f67602011-12-16 03:33:26 -0200882 /*
883 * find only the first command, as modprobe from
884 * module-init-tools does
885 */
886 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200887 }
888
889 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200890 }
891
892 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200893}
894
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200895void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
896{
897 mod->init.install_commands = true;
898 mod->install_commands = cmd;
899}
900
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200901static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
902{
903 struct kmod_list *ret = NULL;
904 unsigned i;
905
906 for (i = 0; i < count; i++) {
907 const char *depname = array[i];
908 struct kmod_list *lst = NULL;
909 int err;
910
911 err = kmod_module_new_from_lookup(ctx, depname, &lst);
912 if (err < 0) {
913 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
914 continue;
915 } else if (lst != NULL)
916 ret = kmod_list_append_list(ret, lst);
917 }
918 return ret;
919}
920
921/**
922 * kmod_module_get_softdeps:
923 * @mod: kmod module
924 * @pre: where to save the list of preceding soft dependencies.
925 * @post: where to save the list of post soft dependencies.
926 *
927 * Get soft dependencies for this kmod module. Soft dependencies come
928 * from configuration file and are cached in @mod. The first call
929 * to this function will search for this module in configuration and
930 * subsequent calls return the known results.
931 *
932 * Both @pre and @post are newly created list of kmod_module and
933 * should be unreferenced with kmod_module_unref_list().
934 *
935 * Returns: 0 on success or < 0 otherwise.
936 */
937KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod, struct kmod_list **pre, struct kmod_list **post)
938{
939 const struct kmod_list *l;
940 struct kmod_list *l_new;
941
942 if (mod == NULL || pre == NULL || post == NULL)
943 return -ENOENT;
944
945 assert(*pre == NULL);
946 assert(*post == NULL);
947
948 if (!mod->init.softdeps) {
949 /* lazy init */
950 struct kmod_module *m = (struct kmod_module *)mod;
951 const struct kmod_list *ctx_softdeps;
952
953 ctx_softdeps = kmod_get_softdeps(mod->ctx);
954
955 kmod_list_foreach(l, ctx_softdeps) {
956 const char *modname = kmod_softdep_get_name(l);
957 const char * const *array;
958 unsigned count;
959
960 if (fnmatch(modname, mod->name, 0) != 0)
961 continue;
962
963 array = kmod_softdep_get_pre(l, &count);
964 m->softdeps.pre = lookup_softdep(mod->ctx, array, count);
965 array = kmod_softdep_get_post(l, &count);
966 m->softdeps.post = lookup_softdep(mod->ctx, array, count);
967 /*
968 * find only the first command, as modprobe from
969 * module-init-tools does
970 */
971 break;
972 }
973
974 m->init.softdeps = true;
975 }
976
977 kmod_list_foreach(l, mod->softdeps.pre) {
978 l_new = kmod_list_append(*pre, kmod_module_ref(l->data));
979 if (l_new == NULL) {
980 kmod_module_unref(l->data);
981 goto fail;
982 }
983 *pre = l_new;
984 }
985
986 kmod_list_foreach(l, mod->softdeps.post) {
987 l_new = kmod_list_append(*post, kmod_module_ref(l->data));
988 if (l_new == NULL) {
989 kmod_module_unref(l->data);
990 goto fail;
991 }
992 *post = l_new;
993 }
994
995 return 0;
996
997fail:
998 kmod_module_unref_list(*pre);
999 *pre = NULL;
1000 kmod_module_unref_list(*post);
1001 *post = NULL;
1002 return -ENOMEM;
1003}
1004
1005
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001006/**
1007 * kmod_module_get_remove_commands:
1008 * @mod: kmod module
1009 *
1010 * Get remove commands for this kmod module. Remove commands come from the
1011 * configuration file and are cached in @mod. The first call to this function
1012 * will search for this module in configuration and subsequent calls return
1013 * the cached string. The remove commands are returned as they were in the
1014 * configuration, concatenated by ';'. No other processing is made in this
1015 * string.
1016 *
1017 * Returns: a string with all remove commands separated by semicolons. This
1018 * string is owned by @mod, do not free it.
1019 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001020KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1021{
1022 if (mod == NULL)
1023 return NULL;
1024
1025 if (!mod->init.remove_commands) {
1026 /* lazy init */
1027 struct kmod_module *m = (struct kmod_module *)mod;
1028 const struct kmod_list *l, *ctx_remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001029
1030 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1031
1032 kmod_list_foreach(l, ctx_remove_commands) {
1033 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001034
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001035 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001036 continue;
1037
Lucas De Marchi60f67602011-12-16 03:33:26 -02001038 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001039
Lucas De Marchi60f67602011-12-16 03:33:26 -02001040 /*
1041 * find only the first command, as modprobe from
1042 * module-init-tools does
1043 */
1044 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001045 }
1046
1047 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001048 }
1049
1050 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001051}
1052
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001053void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1054{
1055 mod->init.remove_commands = true;
1056 mod->remove_commands = cmd;
1057}
1058
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001059/**
1060 * SECTION:libkmod-loaded
1061 * @short_description: currently loaded modules
1062 *
1063 * Information about currently loaded modules, as reported by Linux kernel.
1064 * These information are not cached by libkmod and are always read from /sys
1065 * and /proc/modules.
1066 */
1067
1068/**
1069 * kmod_module_new_from_loaded:
1070 * @ctx: kmod library context
1071 * @list: where to save the list of loaded modules
1072 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001073 * Create a new list of kmod modules with all modules currently loaded in
1074 * kernel. It uses /proc/modules to get the names of loaded modules and to
1075 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1076 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001077 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001078 * The initial refcount is 1, and needs to be decremented to release the
1079 * resources of the kmod_module. The returned @list must be released by
1080 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1081 * kmod_modules created, they are all released upon @ctx destruction too. Do
1082 * not unref @ctx before all the desired operations with the returned list are
1083 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001084 *
1085 * Returns: 0 on success or < 0 on error.
1086 */
1087KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1088 struct kmod_list **list)
1089{
1090 struct kmod_list *l = NULL;
1091 FILE *fp;
1092 char line[4096];
1093
1094 if (ctx == NULL || list == NULL)
1095 return -ENOENT;
1096
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001097 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001098 if (fp == NULL) {
1099 int err = -errno;
1100 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1101 return err;
1102 }
1103
1104 while (fgets(line, sizeof(line), fp)) {
1105 struct kmod_module *m;
1106 struct kmod_list *node;
1107 int err;
1108 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1109
1110 err = kmod_module_new_from_name(ctx, name, &m);
1111 if (err < 0) {
1112 ERR(ctx, "could not get module from name '%s': %s\n",
1113 name, strerror(-err));
1114 continue;
1115 }
1116
1117 node = kmod_list_append(l, m);
1118 if (node)
1119 l = node;
1120 else {
1121 ERR(ctx, "out of memory\n");
1122 kmod_module_unref(m);
1123 }
1124 }
1125
1126 fclose(fp);
1127 *list = l;
1128
1129 return 0;
1130}
1131
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001132/**
1133 * kmod_module_initstate_str:
1134 * @state: the state as returned by kmod_module_get_initstate()
1135 *
1136 * Translate a initstate to a string.
1137 *
1138 * Returns: the string associated to the @state. This string is statically
1139 * allocated, do not free it.
1140 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001141KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1142{
1143 switch (state) {
1144 case KMOD_MODULE_BUILTIN:
1145 return "builtin";
1146 case KMOD_MODULE_LIVE:
1147 return "live";
1148 case KMOD_MODULE_COMING:
1149 return "coming";
1150 case KMOD_MODULE_GOING:
1151 return "going";
1152 default:
1153 return NULL;
1154 }
1155}
1156
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001157/**
1158 * kmod_module_get_initstate:
1159 * @mod: kmod module
1160 *
1161 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1162 * /sys filesystem.
1163 *
1164 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1165 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001166KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1167{
1168 char path[PATH_MAX], buf[32];
1169 int fd, err, pathlen;
1170
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001171 if (mod == NULL)
1172 return -ENOENT;
1173
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001174 pathlen = snprintf(path, sizeof(path),
1175 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001176 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001177 if (fd < 0) {
1178 err = -errno;
1179
1180 if (pathlen > (int)sizeof("/initstate") - 1) {
1181 struct stat st;
1182 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1183 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1184 return KMOD_MODULE_BUILTIN;
1185 }
1186
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001187 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001188 path, strerror(-err));
1189 return err;
1190 }
1191
1192 err = read_str_safe(fd, buf, sizeof(buf));
1193 close(fd);
1194 if (err < 0) {
1195 ERR(mod->ctx, "could not read from '%s': %s\n",
1196 path, strerror(-err));
1197 return err;
1198 }
1199
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001200 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001201 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001202 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001203 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001204 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001205 return KMOD_MODULE_GOING;
1206
1207 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1208 return -EINVAL;
1209}
1210
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001211/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001212 * kmod_module_get_size:
1213 * @mod: kmod module
1214 *
1215 * Get the size of this kmod module as returned by Linux kernel. It reads the
1216 * file /proc/modules to search for this module and get its size.
1217 *
1218 * Returns: the size of this kmod module.
1219 */
1220KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1221{
1222 // FIXME TODO: this should be available from /sys/module/foo
1223 FILE *fp;
1224 char line[4096];
1225 int lineno = 0;
1226 long size = -ENOENT;
1227
1228 if (mod == NULL)
1229 return -ENOENT;
1230
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001231 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001232 if (fp == NULL) {
1233 int err = -errno;
1234 ERR(mod->ctx,
1235 "could not open /proc/modules: %s\n", strerror(errno));
1236 return err;
1237 }
1238
1239 while (fgets(line, sizeof(line), fp)) {
1240 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1241 long value;
1242
1243 lineno++;
1244 if (tok == NULL || !streq(tok, mod->name))
1245 continue;
1246
1247 tok = strtok_r(NULL, " \t", &saveptr);
1248 if (tok == NULL) {
1249 ERR(mod->ctx,
1250 "invalid line format at /proc/modules:%d\n", lineno);
1251 break;
1252 }
1253
1254 value = strtol(tok, &endptr, 10);
1255 if (endptr == tok || *endptr != '\0') {
1256 ERR(mod->ctx,
1257 "invalid line format at /proc/modules:%d\n", lineno);
1258 break;
1259 }
1260
1261 size = value;
1262 break;
1263 }
1264 fclose(fp);
1265 return size;
1266}
1267
1268/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001269 * kmod_module_get_refcnt:
1270 * @mod: kmod module
1271 *
1272 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1273 * /sys filesystem.
1274 *
1275 * Returns: 0 on success or < 0 on failure.
1276 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001277KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1278{
1279 char path[PATH_MAX];
1280 long refcnt;
1281 int fd, err;
1282
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001283 if (mod == NULL)
1284 return -ENOENT;
1285
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001286 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001287 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001288 if (fd < 0) {
1289 err = -errno;
1290 ERR(mod->ctx, "could not open '%s': %s\n",
1291 path, strerror(errno));
1292 return err;
1293 }
1294
1295 err = read_str_long(fd, &refcnt, 10);
1296 close(fd);
1297 if (err < 0) {
1298 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1299 path, strerror(-err));
1300 return err;
1301 }
1302
1303 return (int)refcnt;
1304}
1305
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001306/**
1307 * kmod_module_get_holders:
1308 * @mod: kmod module
1309 *
1310 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1311 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1312 *
1313 * Returns: a new list of kmod modules on success or NULL on failure.
1314 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001315KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1316{
1317 char dname[PATH_MAX];
1318 struct kmod_list *list = NULL;
1319 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001320
1321 if (mod == NULL)
1322 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001323
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001324 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1325
1326 d = opendir(dname);
1327 if (d == NULL) {
1328 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001329 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001330 return NULL;
1331 }
1332
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001333 for (;;) {
1334 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001335 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001336 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001337 int err;
1338
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001339 err = readdir_r(d, &de, &entp);
1340 if (err != 0) {
1341 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1342 mod->name, strerror(-err));
1343 goto fail;
1344 }
1345
1346 if (entp == NULL)
1347 break;
1348
1349 if (de.d_name[0] == '.') {
1350 if (de.d_name[1] == '\0' ||
1351 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001352 continue;
1353 }
1354
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001355 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001356 if (err < 0) {
1357 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001358 de.d_name, strerror(-err));
1359 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001360 }
1361
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001362 l = kmod_list_append(list, holder);
1363 if (l != NULL) {
1364 list = l;
1365 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001366 ERR(mod->ctx, "out of memory\n");
1367 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001368 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001369 }
1370 }
1371
1372 closedir(d);
1373 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001374
1375fail:
1376 closedir(d);
1377 kmod_module_unref_list(list);
1378 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001379}
1380
1381struct kmod_module_section {
1382 unsigned long address;
1383 char name[];
1384};
1385
1386static void kmod_module_section_free(struct kmod_module_section *section)
1387{
1388 free(section);
1389}
1390
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001391/**
1392 * kmod_module_get_sections:
1393 * @mod: kmod module
1394 *
1395 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1396 * structure contained in this list is internal to libkmod and their fields
1397 * can be obtained by calling kmod_module_section_get_name() and
1398 * kmod_module_section_get_address().
1399 *
1400 * After use, free the @list by calling kmod_module_section_free_list().
1401 *
1402 * Returns: a new list of kmod module sections on success or NULL on failure.
1403 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001404KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1405{
1406 char dname[PATH_MAX];
1407 struct kmod_list *list = NULL;
1408 DIR *d;
1409 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001410
1411 if (mod == NULL)
1412 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001413
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001414 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1415
1416 d = opendir(dname);
1417 if (d == NULL) {
1418 ERR(mod->ctx, "could not open '%s': %s\n",
1419 dname, strerror(errno));
1420 return NULL;
1421 }
1422
1423 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001424
1425 for (;;) {
1426 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001427 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001428 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001429 unsigned long address;
1430 size_t namesz;
1431 int fd, err;
1432
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001433 err = readdir_r(d, &de, &entp);
1434 if (err != 0) {
1435 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1436 mod->name, strerror(-err));
1437 goto fail;
1438 }
1439
1440 if (de.d_name[0] == '.') {
1441 if (de.d_name[1] == '\0' ||
1442 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001443 continue;
1444 }
1445
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001446 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001447 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001448 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1449 dname, de.d_name);
1450 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001451 }
1452
1453 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001454 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001455 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001456 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1457 dname, de.d_name);
1458 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001459 }
1460
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001461 namesz = strlen(de.d_name) + 1;
1462 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001463
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001464 if (section == NULL) {
1465 ERR(mod->ctx, "out of memory\n");
1466 goto fail;
1467 }
1468
1469 section->address = address;
1470 memcpy(section->name, de.d_name, namesz);
1471
1472 l = kmod_list_append(list, section);
1473 if (l != NULL) {
1474 list = l;
1475 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001476 ERR(mod->ctx, "out of memory\n");
1477 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001478 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001479 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001480 }
1481
1482 closedir(d);
1483 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001484
1485fail:
1486 closedir(d);
1487 kmod_module_unref_list(list);
1488 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001489}
1490
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001491/**
1492 * kmod_module_section_get_module_name:
1493 * @entry: a list entry representing a kmod module section
1494 *
1495 * Get the name of a kmod module section.
1496 *
1497 * After use, free the @list by calling kmod_module_section_free_list().
1498 *
1499 * Returns: the name of this kmod module section on success or NULL on
1500 * failure. The string is owned by the section, do not free it.
1501 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001502KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1503{
1504 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001505
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001506 if (entry == NULL)
1507 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001508
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001509 section = entry->data;
1510 return section->name;
1511}
1512
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001513/**
1514 * kmod_module_section_get_address:
1515 * @entry: a list entry representing a kmod module section
1516 *
1517 * Get the address of a kmod module section.
1518 *
1519 * After use, free the @list by calling kmod_module_section_free_list().
1520 *
1521 * Returns: the address of this kmod module section on success or ULONG_MAX
1522 * on failure.
1523 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001524KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1525{
1526 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001527
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001528 if (entry == NULL)
1529 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001530
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001531 section = entry->data;
1532 return section->address;
1533}
1534
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001535/**
1536 * kmod_module_section_free_list:
1537 * @list: kmod module section list
1538 *
1539 * Release the resources taken by @list
1540 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001541KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1542{
1543 while (list) {
1544 kmod_module_section_free(list->data);
1545 list = kmod_list_remove(list);
1546 }
1547}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001548
1549struct kmod_module_info {
1550 char *key;
1551 char value[];
1552};
1553
1554static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
1555{
1556 struct kmod_module_info *info;
1557
1558 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
1559 if (info == NULL)
1560 return NULL;
1561
1562 info->key = (char *)info + sizeof(struct kmod_module_info)
1563 + valuelen + 1;
1564 memcpy(info->key, key, keylen);
1565 info->key[keylen] = '\0';
1566 memcpy(info->value, value, valuelen);
1567 info->value[valuelen] = '\0';
1568 return info;
1569}
1570
1571static void kmod_module_info_free(struct kmod_module_info *info)
1572{
1573 free(info);
1574}
1575
1576/**
1577 * kmod_module_get_info:
1578 * @mod: kmod module
1579 * @list: where to return list of module information. Use
1580 * kmod_module_info_get_key() and
1581 * kmod_module_info_get_value(). Release this list with
1582 * kmod_module_info_unref_list()
1583 *
1584 * Get a list of entries in ELF section ".modinfo", these contain
1585 * alias, license, depends, vermagic and other keys with respective
1586 * values.
1587 *
1588 * After use, free the @list by calling kmod_module_info_free_list().
1589 *
1590 * Returns: 0 on success or < 0 otherwise.
1591 */
1592KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
1593{
1594 struct kmod_file *file;
1595 struct kmod_elf *elf;
1596 const char *path;
1597 const void *mem;
1598 char **strings;
1599 size_t size;
1600 int i, count, ret = 0;
1601
1602 if (mod == NULL || list == NULL)
1603 return -ENOENT;
1604
1605 assert(*list == NULL);
1606
1607 path = kmod_module_get_path(mod);
1608 if (path == NULL)
1609 return -ENOENT;
1610
1611 file = kmod_file_open(path);
1612 if (file == NULL)
1613 return -errno;
1614
1615 size = kmod_file_get_size(file);
1616 mem = kmod_file_get_contents(file);
1617
1618 elf = kmod_elf_new(mem, size);
1619 if (elf == NULL) {
1620 ret = -errno;
1621 goto elf_open_error;
1622 }
1623
1624 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
1625 if (count < 0) {
1626 ret = count;
1627 goto get_strings_error;
1628 }
1629
1630 for (i = 0; i < count; i++) {
1631 struct kmod_module_info *info;
1632 struct kmod_list *n;
1633 const char *key, *value;
1634 size_t keylen, valuelen;
1635
1636 key = strings[i];
1637 value = strchr(key, '=');
1638 if (value == NULL) {
1639 keylen = strlen(key);
1640 valuelen = 0;
1641 } else {
1642 keylen = value - key;
1643 value++;
1644 valuelen = strlen(value);
1645 }
1646
1647 info = kmod_module_info_new(key, keylen, value, valuelen);
1648 if (info == NULL) {
1649 ret = -errno;
1650 kmod_module_info_free_list(*list);
1651 *list = NULL;
1652 goto list_error;
1653 }
1654
1655 n = kmod_list_append(*list, info);
1656 if (n != NULL)
1657 *list = n;
1658 else {
1659 kmod_module_info_free(info);
1660 kmod_module_info_free_list(*list);
1661 *list = NULL;
1662 ret = -ENOMEM;
1663 goto list_error;
1664 }
1665 }
1666 ret = count;
1667
1668list_error:
1669 free(strings);
1670get_strings_error:
1671 kmod_elf_unref(elf);
1672elf_open_error:
1673 kmod_file_unref(file);
1674
1675 return ret;
1676}
1677
1678/**
1679 * kmod_module_info_get_key:
1680 * @entry: a list entry representing a kmod module info
1681 *
1682 * Get the key of a kmod module info.
1683 *
1684 * Returns: the key of this kmod module info on success or NULL on
1685 * failure. The string is owned by the info, do not free it.
1686 */
1687KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
1688{
1689 struct kmod_module_info *info;
1690
1691 if (entry == NULL)
1692 return NULL;
1693
1694 info = entry->data;
1695 return info->key;
1696}
1697
1698/**
1699 * kmod_module_info_get_value:
1700 * @entry: a list entry representing a kmod module info
1701 *
1702 * Get the value of a kmod module info.
1703 *
1704 * Returns: the value of this kmod module info on success or NULL on
1705 * failure. The string is owned by the info, do not free it.
1706 */
1707KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
1708{
1709 struct kmod_module_info *info;
1710
1711 if (entry == NULL)
1712 return NULL;
1713
1714 info = entry->data;
1715 return info->value;
1716}
1717
1718/**
1719 * kmod_module_info_free_list:
1720 * @list: kmod module info list
1721 *
1722 * Release the resources taken by @list
1723 */
1724KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
1725{
1726 while (list) {
1727 kmod_module_info_free(list->data);
1728 list = kmod_list_remove(list);
1729 }
1730}
1731
1732struct kmod_module_version {
1733 uint64_t crc;
1734 char symbol[];
1735};
1736
1737static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
1738{
1739 struct kmod_module_version *mv;
1740 size_t symbollen = strlen(symbol) + 1;
1741
1742 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
1743 if (mv == NULL)
1744 return NULL;
1745
1746 mv->crc = crc;
1747 memcpy(mv->symbol, symbol, symbollen);
1748 return mv;
1749}
1750
1751static void kmod_module_version_free(struct kmod_module_version *version)
1752{
1753 free(version);
1754}
1755
1756/**
1757 * kmod_module_get_versions:
1758 * @mod: kmod module
1759 * @list: where to return list of module versions. Use
1760 * kmod_module_versions_get_symbol() and
1761 * kmod_module_versions_get_crc(). Release this list with
1762 * kmod_module_versions_unref_list()
1763 *
1764 * Get a list of entries in ELF section "__versions".
1765 *
1766 * After use, free the @list by calling kmod_module_versions_free_list().
1767 *
1768 * Returns: 0 on success or < 0 otherwise.
1769 */
1770KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
1771{
1772 struct kmod_file *file;
1773 struct kmod_elf *elf;
1774 const char *path;
1775 const void *mem;
1776 struct kmod_modversion *versions;
1777 size_t size;
1778 int i, count, ret = 0;
1779
1780 if (mod == NULL || list == NULL)
1781 return -ENOENT;
1782
1783 assert(*list == NULL);
1784
1785 path = kmod_module_get_path(mod);
1786 if (path == NULL)
1787 return -ENOENT;
1788
1789 file = kmod_file_open(path);
1790 if (file == NULL)
1791 return -errno;
1792
1793 size = kmod_file_get_size(file);
1794 mem = kmod_file_get_contents(file);
1795
1796 elf = kmod_elf_new(mem, size);
1797 if (elf == NULL) {
1798 ret = -errno;
1799 goto elf_open_error;
1800 }
1801
1802 count = kmod_elf_get_modversions(elf, &versions);
1803 if (count < 0) {
1804 ret = count;
1805 goto get_strings_error;
1806 }
1807
1808 for (i = 0; i < count; i++) {
1809 struct kmod_module_version *mv;
1810 struct kmod_list *n;
1811
1812 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
1813 if (mv == NULL) {
1814 ret = -errno;
1815 kmod_module_versions_free_list(*list);
1816 *list = NULL;
1817 goto list_error;
1818 }
1819
1820 n = kmod_list_append(*list, mv);
1821 if (n != NULL)
1822 *list = n;
1823 else {
1824 kmod_module_version_free(mv);
1825 kmod_module_versions_free_list(*list);
1826 *list = NULL;
1827 ret = -ENOMEM;
1828 goto list_error;
1829 }
1830 }
1831 ret = count;
1832
1833list_error:
1834 free(versions);
1835get_strings_error:
1836 kmod_elf_unref(elf);
1837elf_open_error:
1838 kmod_file_unref(file);
1839
1840 return ret;
1841}
1842
1843/**
1844 * kmod_module_versions_get_symbol:
1845 * @entry: a list entry representing a kmod module versions
1846 *
1847 * Get the symbol of a kmod module versions.
1848 *
1849 * Returns: the symbol of this kmod module versions on success or NULL
1850 * on failure. The string is owned by the versions, do not free it.
1851 */
1852KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
1853{
1854 struct kmod_module_version *version;
1855
1856 if (entry == NULL)
1857 return NULL;
1858
1859 version = entry->data;
1860 return version->symbol;
1861}
1862
1863/**
1864 * kmod_module_version_get_crc:
1865 * @entry: a list entry representing a kmod module version
1866 *
1867 * Get the crc of a kmod module version.
1868 *
1869 * Returns: the crc of this kmod module version on success or NULL on
1870 * failure. The string is owned by the version, do not free it.
1871 */
1872KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
1873{
1874 struct kmod_module_version *version;
1875
1876 if (entry == NULL)
1877 return 0;
1878
1879 version = entry->data;
1880 return version->crc;
1881}
1882
1883/**
1884 * kmod_module_versions_free_list:
1885 * @list: kmod module versions list
1886 *
1887 * Release the resources taken by @list
1888 */
1889KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
1890{
1891 while (list) {
1892 kmod_module_version_free(list->data);
1893 list = kmod_list_remove(list);
1894 }
1895}