blob: 79a49c3489d1ff743f21cd9abf84067539886c10 [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 Marchi8bdeca12011-12-15 13:11:51 -020048 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020049 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020050 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020051 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020052 char *options;
53 char *install_commands;
54 char *remove_commands;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020055 char *alias; /* only set if this module was created from an alias */
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020056 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020057 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020058 struct {
59 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020060 bool options : 1;
61 bool install_commands : 1;
62 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020063 } init;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020064};
65
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -020066inline char *modname_normalize(const char *modname, char buf[NAME_MAX],
Lucas De Marchi6c343b12011-12-06 09:01:01 -020067 size_t *len)
Lucas De Marchi8f788d52011-11-25 01:22:56 -020068{
Lucas De Marchid753b8c2011-12-05 18:14:51 -020069 size_t s;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020070
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020071 for (s = 0; s < NAME_MAX - 1; s++) {
72 const char c = modname[s];
73 if (c == '-')
74 buf[s] = '_';
75 else if (c == '\0' || c == '.')
76 break;
77 else
78 buf[s] = c;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020079 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -020080
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020081 buf[s] = '\0';
Lucas De Marchid753b8c2011-12-05 18:14:51 -020082
83 if (len)
84 *len = s;
85
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020086 return buf;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020087}
88
Lucas De Marchi6c343b12011-12-06 09:01:01 -020089static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len)
90{
91 char *modname;
92
93 modname = basename(path);
94 if (modname == NULL || modname[0] == '\0')
95 return NULL;
96
97 return modname_normalize(modname, buf, len);
98}
99
Lucas De Marchic35347f2011-12-12 10:48:02 -0200100static inline const char *path_join(const char *path, size_t prefixlen,
101 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200102{
103 size_t pathlen;
104
105 if (path[0] == '/')
106 return path;
107
108 pathlen = strlen(path);
109 if (prefixlen + pathlen + 1 >= PATH_MAX)
110 return NULL;
111
112 memcpy(buf + prefixlen, path, pathlen + 1);
113 return buf;
114}
115
Lucas De Marchi671d4892011-12-05 20:23:05 -0200116int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200117{
118 struct kmod_ctx *ctx = mod->ctx;
119 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200120 const char *dirname;
121 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200122 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200123 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200124 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200125
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200126 if (mod->init.dep)
127 return mod->n_dep;
128 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200129 mod->init.dep = true;
130
131 p = strchr(line, ':');
132 if (p == NULL)
133 return 0;
134
Lucas De Marchi671d4892011-12-05 20:23:05 -0200135 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200136 dirname = kmod_get_dirname(mod->ctx);
137 dirnamelen = strlen(dirname);
138 if (dirnamelen + 2 >= PATH_MAX)
139 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200140
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200141 memcpy(buf, dirname, dirnamelen);
142 buf[dirnamelen] = '/';
143 dirnamelen++;
144 buf[dirnamelen] = '\0';
145
146 if (mod->path == NULL) {
147 const char *str = path_join(line, dirnamelen, buf);
148 if (str == NULL)
149 return 0;
150 mod->path = strdup(str);
151 if (mod->path == NULL)
152 return 0;
153 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200154
Lucas De Marchi7636e722011-12-01 17:56:03 -0200155 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200156 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
157 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200158 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200159 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200160
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200161 path = path_join(p, dirnamelen, buf);
162 if (path == NULL) {
163 ERR(ctx, "could not join path '%s' and '%s'.\n",
164 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200165 goto fail;
166 }
167
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200168 err = kmod_module_new_from_path(ctx, path, &depmod);
169 if (err < 0) {
170 ERR(ctx, "ctx=%p path=%s error=%s\n",
171 ctx, path, strerror(-err));
172 goto fail;
173 }
174
175 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200176
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200177 list = kmod_list_append(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200178 n++;
179 }
180
181 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
182
183 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200184 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200185 return n;
186
187fail:
188 kmod_module_unref_list(list);
189 mod->init.dep = false;
190 return err;
191}
192
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200193/**
194 * kmod_module_new_from_name:
195 * @ctx: kmod library context
196 * @name: name of the module
197 * @mod: where to save the created struct kmod_module
198 *
199 * Create a new struct kmod_module using the module name. @name can not be an
200 * alias, file name or anything else; it must be a module name. There's no
201 * check if the module does exists in the system.
202 *
203 * This function is also used internally by many others that return a new
204 * struct kmod_module or a new list of modules.
205 *
206 * The initial refcount is 1, and needs to be decremented to release the
207 * resources of the kmod_module. Since libkmod keeps track of all
208 * kmod_modules created, they are all released upon @ctx destruction too. Do
209 * not unref @ctx before all the desired operations with the returned
210 * kmod_module are done.
211 *
212 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
213 * module name or if memory allocation failed.
214 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200215KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
216 const char *name,
217 struct kmod_module **mod)
218{
219 struct kmod_module *m;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200220 size_t namelen;
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200221 char name_norm[NAME_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200222 char *namesep;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200223
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200224 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200225 return -ENOENT;
226
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200227 alias_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200228
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200229 m = kmod_pool_get_module(ctx, name_norm);
230 if (m != NULL) {
231 *mod = kmod_module_ref(m);
232 return 0;
233 }
234
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200235 namesep = strchr(name_norm, '/');
236 m = malloc(sizeof(*m) + (namesep == NULL ? 1 : 2) * namelen + 2);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200237 if (m == NULL) {
238 free(m);
239 return -ENOMEM;
240 }
241
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200242 memset(m, 0, sizeof(*m));
243
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200244 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200245 m->name = (char *)m + sizeof(*m);
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200246 memcpy(m->name, name_norm, namelen + 1);
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200247
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200248 if (namesep) {
249 size_t len = namesep - name_norm;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200250
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200251 m->name[len] = '\0';
252 m->alias = m->name + len + 1;
253 m->hashkey = m->name + namelen + 1;
254 memcpy(m->hashkey, name_norm, namelen + 1);
255 } else {
256 m->hashkey = m->name;
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200257 }
258
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200259 m->refcount = 1;
260 kmod_pool_add_module(ctx, m, m->hashkey);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200261 *mod = m;
262
263 return 0;
264}
265
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200266int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
267 const char *name, struct kmod_module **mod)
268{
269 int err;
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200270 char key[NAME_MAX];
271 size_t namelen = strlen(name);
272 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200273
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200274 if (namelen + aliaslen + 2 > NAME_MAX)
275 return -ENAMETOOLONG;
276
277 memcpy(key, name, namelen);
278 memcpy(key + namelen + 1, alias, aliaslen + 1);
279 key[namelen] = '/';
280
281 err = kmod_module_new_from_name(ctx, key, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200282 if (err < 0)
283 return err;
284
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200285 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200286}
287
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200288/**
289 * kmod_module_new_from_path:
290 * @ctx: kmod library context
291 * @path: path where to find the given module
292 * @mod: where to save the created struct kmod_module
293 *
294 * Create a new struct kmod_module using the module path. @path must be an
295 * existent file with in the filesystem and must be accessible to libkmod.
296 *
297 * The initial refcount is 1, and needs to be decremented to release the
298 * resources of the kmod_module. Since libkmod keeps track of all
299 * kmod_modules created, they are all released upon @ctx destruction too. Do
300 * not unref @ctx before all the desired operations with the returned
301 * kmod_module are done.
302 *
303 * If @path is relative, it's treated as relative to the current working
304 * directory. Otherwise, give an absolute path.
305 *
306 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
307 * it's not a valid file for a kmod_module or if memory allocation failed.
308 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200309KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
310 const char *path,
311 struct kmod_module **mod)
312{
313 struct kmod_module *m;
314 int err;
315 struct stat st;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200316 char name[NAME_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200317 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200318 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200319
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200320 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200321 return -ENOENT;
322
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200323 abspath = path_make_absolute_cwd(path);
324 if (abspath == NULL)
325 return -ENOMEM;
326
327 err = stat(abspath, &st);
328 if (err < 0) {
329 free(abspath);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200330 return -errno;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200331 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200332
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200333 if (path_to_modname(path, name, &namelen) == NULL) {
334 free(abspath);
335 return -ENOENT;
336 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200337
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200338 m = kmod_pool_get_module(ctx, name);
339 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200340 if (m->path == NULL)
341 m->path = abspath;
342 else if (streq(m->path, abspath))
343 free(abspath);
344 else {
345 ERR(ctx, "kmod_module '%s' already exists with different path\n",
346 name);
347 free(abspath);
348 return -EEXIST;
349 }
350
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200351 *mod = kmod_module_ref(m);
352 return 0;
353 }
354
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200355 m = malloc(sizeof(*m) + namelen + 1);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200356 if (m == NULL)
357 return -errno;
358
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200359 memset(m, 0, sizeof(*m));
360
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200361 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200362 m->name = (char *)m + sizeof(*m);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200363 memcpy(m->name, name, namelen);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200364 m->path = abspath;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200365 m->hashkey = m->name;
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200366 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200367
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200368 kmod_pool_add_module(ctx, m, m->hashkey);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200369
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200370 *mod = m;
371
372 return 0;
373}
374
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200375/**
376 * kmod_module_unref:
377 * @mod: kmod module
378 *
379 * Drop a reference of the kmod module. If the refcount reaches zero, its
380 * resources are released.
381 *
382 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
383 * returns the passed @mod with its refcount decremented.
384 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200385KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
386{
387 if (mod == NULL)
388 return NULL;
389
390 if (--mod->refcount > 0)
391 return mod;
392
393 DBG(mod->ctx, "kmod_module %p released\n", mod);
394
Lucas De Marchi7636e722011-12-01 17:56:03 -0200395 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200396 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200397 free(mod->options);
398 free(mod->install_commands);
399 free(mod->remove_commands);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200400 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200401 free(mod);
402 return NULL;
403}
404
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200405/**
406 * kmod_module_ref:
407 * @mod: kmod module
408 *
409 * Take a reference of the kmod module, incrementing its refcount.
410 *
411 * Returns: the passed @module with its refcount incremented.
412 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200413KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
414{
415 if (mod == NULL)
416 return NULL;
417
418 mod->refcount++;
419
420 return mod;
421}
422
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200423#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
424 do { \
425 if ((_err) < 0) \
426 goto _label_err; \
427 if (*(_list) != NULL) \
428 goto finish; \
429 } while (0)
430
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200431/**
432 * kmod_module_new_from_lookup:
433 * @ctx: kmod library context
434 * @given_alias: alias to look for
435 * @list: an empty list where to save the list of modules matching
436 * @given_alias
437 *
438 * Create a new list of kmod modules using an alias or module name and lookup
439 * libkmod's configuration files and indexes in order to find the module.
440 * Once it's found in one of the places, it stops searching and create the
441 * list of modules that is saved in @list.
442 *
443 * The search order is: 1. aliases in configuration file; 2. module names in
444 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
445 * in modules.alias index.
446 *
447 * The initial refcount is 1, and needs to be decremented to release the
448 * resources of the kmod_module. The returned @list must be released by
449 * calling kmod_module_unref_list(). Since libkmod keeps track of all
450 * kmod_modules created, they are all released upon @ctx destruction too. Do
451 * not unref @ctx before all the desired operations with the returned list are
452 * completed.
453 *
454 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
455 * methods failed, which is basically due to memory allocation fail. If module
456 * is not found, it still returns 0, but @list is an empty list.
457 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200458KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200459 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200460 struct kmod_list **list)
461{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200462 int err;
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200463 char alias[NAME_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200464
Lucas De Marchi4308b172011-12-13 10:26:04 -0200465 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200466 return -ENOENT;
467
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200468 if (list == NULL || *list != NULL) {
469 ERR(ctx, "An empty list is needed to create lookup\n");
470 return -ENOSYS;
471 }
472
Lucas De Marchid470db12011-12-13 10:28:00 -0200473 if (alias_normalize(given_alias, alias, NULL) < 0)
474 return -EINVAL;
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200475
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200476 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200477 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200478 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200479
Lucas De Marchi64700e42011-12-01 15:57:53 -0200480 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
481 CHECK_ERR_AND_FINISH(err, fail, list, finish);
482
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200483 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
484 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200485
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200486// TODO: add lookup for install commands here.
487
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200488 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
489 CHECK_ERR_AND_FINISH(err, fail, list, finish);
490
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200491finish:
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200492
493 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200494fail:
495 kmod_module_unref_list(*list);
496 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200497 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200498}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200499#undef CHECK_ERR_AND_FINISH
500
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200501/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200502 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200503 * @list: list of kmod modules
504 *
505 * Drop a reference of each kmod module in @list and releases the resources
506 * taken by the list itself.
507 *
508 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
509 * returns the passed @mod with its refcount decremented.
510 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200511KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
512{
513 for (; list != NULL; list = kmod_list_remove(list))
514 kmod_module_unref(list->data);
515
516 return 0;
517}
518
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200519/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200520 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200521 * @mod: kmod module
522 *
523 * Search the modules.dep index to find the dependencies of the given @mod.
524 * The result is cached in @mod, so subsequent calls to this function will
525 * return the already searched list of modules.
526 *
527 * Returns: NULL on failure or if there are any dependencies. Otherwise it
528 * returns a list of kmod modules that can be released by calling
529 * kmod_module_unref_list().
530 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200531KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200532{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200533 struct kmod_list *l, *l_new, *list_new = NULL;
534
535 if (mod == NULL)
536 return NULL;
537
Lucas De Marchi671d4892011-12-05 20:23:05 -0200538 if (!mod->init.dep) {
539 /* lazy init */
540 char *line = kmod_search_moddep(mod->ctx, mod->name);
541
542 if (line == NULL)
543 return NULL;
544
545 kmod_module_parse_depline((struct kmod_module *)mod, line);
546 free(line);
547
548 if (!mod->init.dep)
549 return NULL;
550 }
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200551
552 kmod_list_foreach(l, mod->dep) {
553 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
554 if (l_new == NULL) {
555 kmod_module_unref(l->data);
556 goto fail;
557 }
558
559 list_new = l_new;
560 }
561
562 return list_new;
563
564fail:
565 ERR(mod->ctx, "out of memory\n");
566 kmod_module_unref_list(list_new);
567 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200568}
569
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200570/**
571 * kmod_module_get_module:
572 * @entry: an entry in a list of kmod modules.
573 *
574 * Get the kmod module of this @entry in the list, increasing its refcount.
575 * After it's used, unref it. Since the refcount is incremented upon return,
576 * you still have to call kmod_module_unref_list() to release the list of kmod
577 * modules.
578 *
579 * Returns: NULL on failure or the kmod_module contained in this list entry
580 * with its refcount incremented.
581 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200582KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200583{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200584 if (entry == NULL)
585 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200586
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200587 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200588}
589
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200590/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200591 * kmod_module_get_name:
592 * @mod: kmod module
593 *
594 * Get the name of this kmod module. Name is always available, independently
595 * if it was created by kmod_module_new_from_name() or another function and
596 * it's always normalized (dashes are replaced with underscores).
597 *
598 * Returns: the name of this kmod module.
599 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200600KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200601{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200602 if (mod == NULL)
603 return NULL;
604
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200605 return mod->name;
606}
607
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200608/**
609 * kmod_module_get_path:
610 * @mod: kmod module
611 *
612 * Get the path of this kmod module. If this kmod module was not created by
613 * path, it can search the modules.dep index in order to find out the module
614 * under context's dirname (see kmod_get_dirname()).
615 *
616 * Returns: the path of this kmod module or NULL if such information is not
617 * available.
618 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200619KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200620{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200621 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200622
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200623 if (mod == NULL)
624 return NULL;
625
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200626 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200627
Lucas De Marchie005fac2011-12-08 10:42:34 -0200628 if (mod->path != NULL)
629 return mod->path;
630 if (mod->init.dep)
631 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200632
Lucas De Marchie005fac2011-12-08 10:42:34 -0200633 /* lazy init */
634 line = kmod_search_moddep(mod->ctx, mod->name);
635 if (line == NULL)
636 return NULL;
637
638 kmod_module_parse_depline((struct kmod_module *) mod, line);
639 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200640
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200641 return mod->path;
642}
643
644
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200645extern long delete_module(const char *name, unsigned int flags);
646
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200647/**
648 * kmod_module_remove_module:
649 * @mod: kmod module
650 * @flags: flags to pass to Linux kernel when removing the module
651 *
652 * Remove a module from Linux kernel.
653 *
654 * Returns: 0 on success or < 0 on failure.
655 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200656KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
657 unsigned int flags)
658{
659 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200660
661 if (mod == NULL)
662 return -ENOENT;
663
664 /* Filter out other flags */
665 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
666
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200667 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200668 if (err != 0) {
Lucas De Marchi63af0612011-12-14 12:07:37 -0200669 ERR(mod->ctx, "Could not remove '%s': %s\n", mod->name,
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200670 strerror(-err));
671 return err;
672 }
673
674 return 0;
675}
676
677extern long init_module(void *mem, unsigned long len, const char *args);
678
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200679/**
680 * kmod_module_insert_module:
681 * @mod: kmod module
682 * @flags: flags are not passed to Linux Kernel, but instead it dictates the
683 * behavior of this function. They are not implemented yet.
684 * @options: module's options to pass to Linux Kernel.
685 *
686 * Insert a module in Linux kernel. It opens the file pointed by @mod,
687 * mmap'ing it and passing to kernel.
688 *
689 * Returns: 0 on success or < 0 on failure.
690 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200691KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200692 unsigned int flags,
693 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200694{
695 int err;
696 void *mmaped_file;
697 struct stat st;
698 int fd;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200699 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200700
701 if (mod == NULL)
702 return -ENOENT;
703
704 if (mod->path == NULL) {
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200705 ERR(mod->ctx, "Not supported to load a module by name yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200706 return -ENOSYS;
707 }
708
709 if (flags != 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200710 INFO(mod->ctx, "Flags are not implemented yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200711
712 if ((fd = open(mod->path, O_RDONLY)) < 0) {
713 err = -errno;
714 return err;
715 }
716
Lucas De Marchib418a822011-12-01 23:13:27 -0200717 fstat(fd, &st);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200718
719 if ((mmaped_file = mmap(0, st.st_size, PROT_READ,
720 MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
721 close(fd);
722 return -errno;
723 }
724
725 err = init_module(mmaped_file, st.st_size, args);
726 if (err < 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200727 ERR(mod->ctx, "Failed to insert module '%s'\n", mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200728
729 munmap(mmaped_file, st.st_size);
730 close(fd);
731
732 return err;
733}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200734
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200735/**
736 * kmod_module_get_options:
737 * @mod: kmod module
738 *
739 * Get options of this kmod module. Options come from the configuration file
740 * and are cached in @mod. The first call to this function will search for
741 * this module in configuration and subsequent calls return the cached string.
742 *
743 * Returns: a string with all the options separated by spaces. This string is
744 * owned by @mod, do not free it.
745 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200746KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
747{
748 if (mod == NULL)
749 return NULL;
750
751 if (!mod->init.options) {
752 /* lazy init */
753 struct kmod_module *m = (struct kmod_module *)mod;
754 const struct kmod_list *l, *ctx_options;
755 char *opts = NULL;
756 size_t optslen = 0;
757
758 ctx_options = kmod_get_options(mod->ctx);
759
760 kmod_list_foreach(l, ctx_options) {
761 const char *modname = kmod_option_get_modname(l);
762 const char *str;
763 size_t len;
764 void *tmp;
765
Lucas De Marchi07b8c822011-12-13 14:21:24 -0200766 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
767 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
768 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200769 continue;
770
Lucas De Marchi07b8c822011-12-13 14:21:24 -0200771 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 -0200772 str = kmod_option_get_options(l);
773 len = strlen(str);
774 if (len < 1)
775 continue;
776
777 tmp = realloc(opts, optslen + len + 2);
778 if (tmp == NULL) {
779 free(opts);
780 goto failed;
781 }
782
783 opts = tmp;
784
785 if (optslen > 0) {
786 opts[optslen] = ' ';
787 optslen++;
788 }
789
790 memcpy(opts + optslen, str, len);
791 optslen += len;
792 opts[optslen] = '\0';
793 }
794
795 m->init.options = true;
796 m->options = opts;
797 }
798
799 return mod->options;
800
801failed:
802 ERR(mod->ctx, "out of memory\n");
803 return NULL;
804}
805
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200806/**
807 * kmod_module_get_install_commands:
808 * @mod: kmod module
809 *
810 * Get install commands for this kmod module. Install commands come from the
811 * configuration file and are cached in @mod. The first call to this function
812 * will search for this module in configuration and subsequent calls return
813 * the cached string. The install commands are returned as they were in the
814 * configuration, concatenated by ';'. No other processing is made in this
815 * string.
816 *
817 * Returns: a string with all install commands separated by semicolons. This
818 * string is owned by @mod, do not free it.
819 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200820KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
821{
822 if (mod == NULL)
823 return NULL;
824
825 if (!mod->init.install_commands) {
826 /* lazy init */
827 struct kmod_module *m = (struct kmod_module *)mod;
828 const struct kmod_list *l, *ctx_install_commands;
829 char *cmds = NULL;
830 size_t cmdslen = 0;
831
832 ctx_install_commands = kmod_get_install_commands(mod->ctx);
833
834 kmod_list_foreach(l, ctx_install_commands) {
835 const char *modname = kmod_command_get_modname(l);
836 const char *str;
837 size_t len;
838 void *tmp;
839
840 if (strcmp(modname, mod->name) != 0)
841 continue;
842
843 str = kmod_command_get_command(l);
844 len = strlen(str);
845 if (len < 1)
846 continue;
847
848 tmp = realloc(cmds, cmdslen + len + 2);
849 if (tmp == NULL) {
850 free(cmds);
851 goto failed;
852 }
853
854 cmds = tmp;
855
856 if (cmdslen > 0) {
857 cmds[cmdslen] = ';';
858 cmdslen++;
859 }
860
861 memcpy(cmds + cmdslen, str, len);
862 cmdslen += len;
863 cmds[cmdslen] = '\0';
864 }
865
866 m->init.install_commands = true;
867 m->install_commands = cmds;
868 }
869
870 return mod->install_commands;
871
872failed:
873 ERR(mod->ctx, "out of memory\n");
874 return NULL;
875}
876
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200877/**
878 * kmod_module_get_remove_commands:
879 * @mod: kmod module
880 *
881 * Get remove commands for this kmod module. Remove commands come from the
882 * configuration file and are cached in @mod. The first call to this function
883 * will search for this module in configuration and subsequent calls return
884 * the cached string. The remove commands are returned as they were in the
885 * configuration, concatenated by ';'. No other processing is made in this
886 * string.
887 *
888 * Returns: a string with all remove commands separated by semicolons. This
889 * string is owned by @mod, do not free it.
890 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200891KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
892{
893 if (mod == NULL)
894 return NULL;
895
896 if (!mod->init.remove_commands) {
897 /* lazy init */
898 struct kmod_module *m = (struct kmod_module *)mod;
899 const struct kmod_list *l, *ctx_remove_commands;
900 char *cmds = NULL;
901 size_t cmdslen = 0;
902
903 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
904
905 kmod_list_foreach(l, ctx_remove_commands) {
906 const char *modname = kmod_command_get_modname(l);
907 const char *str;
908 size_t len;
909 void *tmp;
910
911 if (strcmp(modname, mod->name) != 0)
912 continue;
913
914 str = kmod_command_get_command(l);
915 len = strlen(str);
916 if (len < 1)
917 continue;
918
919 tmp = realloc(cmds, cmdslen + len + 2);
920 if (tmp == NULL) {
921 free(cmds);
922 goto failed;
923 }
924
925 cmds = tmp;
926
927 if (cmdslen > 0) {
928 cmds[cmdslen] = ';';
929 cmdslen++;
930 }
931
932 memcpy(cmds + cmdslen, str, len);
933 cmdslen += len;
934 cmds[cmdslen] = '\0';
935 }
936
937 m->init.remove_commands = true;
938 m->remove_commands = cmds;
939 }
940
941 return mod->remove_commands;
942
943failed:
944 ERR(mod->ctx, "out of memory\n");
945 return NULL;
946}
947
948/**
949 * SECTION:libkmod-loaded
950 * @short_description: currently loaded modules
951 *
952 * Information about currently loaded modules, as reported by Linux kernel.
953 * These information are not cached by libkmod and are always read from /sys
954 * and /proc/modules.
955 */
956
957/**
958 * kmod_module_new_from_loaded:
959 * @ctx: kmod library context
960 * @list: where to save the list of loaded modules
961 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200962 * Create a new list of kmod modules with all modules currently loaded in
963 * kernel. It uses /proc/modules to get the names of loaded modules and to
964 * create kmod modules by calling kmod_module_new_from_name() in each of them.
965 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200966 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200967 * The initial refcount is 1, and needs to be decremented to release the
968 * resources of the kmod_module. The returned @list must be released by
969 * calling kmod_module_unref_list(). Since libkmod keeps track of all
970 * kmod_modules created, they are all released upon @ctx destruction too. Do
971 * not unref @ctx before all the desired operations with the returned list are
972 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200973 *
974 * Returns: 0 on success or < 0 on error.
975 */
976KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
977 struct kmod_list **list)
978{
979 struct kmod_list *l = NULL;
980 FILE *fp;
981 char line[4096];
982
983 if (ctx == NULL || list == NULL)
984 return -ENOENT;
985
986 fp = fopen("/proc/modules", "r");
987 if (fp == NULL) {
988 int err = -errno;
989 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
990 return err;
991 }
992
993 while (fgets(line, sizeof(line), fp)) {
994 struct kmod_module *m;
995 struct kmod_list *node;
996 int err;
997 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
998
999 err = kmod_module_new_from_name(ctx, name, &m);
1000 if (err < 0) {
1001 ERR(ctx, "could not get module from name '%s': %s\n",
1002 name, strerror(-err));
1003 continue;
1004 }
1005
1006 node = kmod_list_append(l, m);
1007 if (node)
1008 l = node;
1009 else {
1010 ERR(ctx, "out of memory\n");
1011 kmod_module_unref(m);
1012 }
1013 }
1014
1015 fclose(fp);
1016 *list = l;
1017
1018 return 0;
1019}
1020
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001021/**
1022 * kmod_module_initstate_str:
1023 * @state: the state as returned by kmod_module_get_initstate()
1024 *
1025 * Translate a initstate to a string.
1026 *
1027 * Returns: the string associated to the @state. This string is statically
1028 * allocated, do not free it.
1029 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001030KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1031{
1032 switch (state) {
1033 case KMOD_MODULE_BUILTIN:
1034 return "builtin";
1035 case KMOD_MODULE_LIVE:
1036 return "live";
1037 case KMOD_MODULE_COMING:
1038 return "coming";
1039 case KMOD_MODULE_GOING:
1040 return "going";
1041 default:
1042 return NULL;
1043 }
1044}
1045
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001046/**
1047 * kmod_module_get_initstate:
1048 * @mod: kmod module
1049 *
1050 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1051 * /sys filesystem.
1052 *
1053 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1054 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001055KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1056{
1057 char path[PATH_MAX], buf[32];
1058 int fd, err, pathlen;
1059
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001060 if (mod == NULL)
1061 return -ENOENT;
1062
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001063 pathlen = snprintf(path, sizeof(path),
1064 "/sys/module/%s/initstate", mod->name);
1065 fd = open(path, O_RDONLY);
1066 if (fd < 0) {
1067 err = -errno;
1068
1069 if (pathlen > (int)sizeof("/initstate") - 1) {
1070 struct stat st;
1071 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1072 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1073 return KMOD_MODULE_BUILTIN;
1074 }
1075
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001076 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001077 path, strerror(-err));
1078 return err;
1079 }
1080
1081 err = read_str_safe(fd, buf, sizeof(buf));
1082 close(fd);
1083 if (err < 0) {
1084 ERR(mod->ctx, "could not read from '%s': %s\n",
1085 path, strerror(-err));
1086 return err;
1087 }
1088
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001089 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001090 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001091 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001092 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001093 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001094 return KMOD_MODULE_GOING;
1095
1096 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1097 return -EINVAL;
1098}
1099
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001100/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001101 * kmod_module_get_size:
1102 * @mod: kmod module
1103 *
1104 * Get the size of this kmod module as returned by Linux kernel. It reads the
1105 * file /proc/modules to search for this module and get its size.
1106 *
1107 * Returns: the size of this kmod module.
1108 */
1109KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1110{
1111 // FIXME TODO: this should be available from /sys/module/foo
1112 FILE *fp;
1113 char line[4096];
1114 int lineno = 0;
1115 long size = -ENOENT;
1116
1117 if (mod == NULL)
1118 return -ENOENT;
1119
1120 fp = fopen("/proc/modules", "r");
1121 if (fp == NULL) {
1122 int err = -errno;
1123 ERR(mod->ctx,
1124 "could not open /proc/modules: %s\n", strerror(errno));
1125 return err;
1126 }
1127
1128 while (fgets(line, sizeof(line), fp)) {
1129 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1130 long value;
1131
1132 lineno++;
1133 if (tok == NULL || !streq(tok, mod->name))
1134 continue;
1135
1136 tok = strtok_r(NULL, " \t", &saveptr);
1137 if (tok == NULL) {
1138 ERR(mod->ctx,
1139 "invalid line format at /proc/modules:%d\n", lineno);
1140 break;
1141 }
1142
1143 value = strtol(tok, &endptr, 10);
1144 if (endptr == tok || *endptr != '\0') {
1145 ERR(mod->ctx,
1146 "invalid line format at /proc/modules:%d\n", lineno);
1147 break;
1148 }
1149
1150 size = value;
1151 break;
1152 }
1153 fclose(fp);
1154 return size;
1155}
1156
1157/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001158 * kmod_module_get_refcnt:
1159 * @mod: kmod module
1160 *
1161 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1162 * /sys filesystem.
1163 *
1164 * Returns: 0 on success or < 0 on failure.
1165 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001166KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1167{
1168 char path[PATH_MAX];
1169 long refcnt;
1170 int fd, err;
1171
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001172 if (mod == NULL)
1173 return -ENOENT;
1174
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001175 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
1176 fd = open(path, O_RDONLY);
1177 if (fd < 0) {
1178 err = -errno;
1179 ERR(mod->ctx, "could not open '%s': %s\n",
1180 path, strerror(errno));
1181 return err;
1182 }
1183
1184 err = read_str_long(fd, &refcnt, 10);
1185 close(fd);
1186 if (err < 0) {
1187 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1188 path, strerror(-err));
1189 return err;
1190 }
1191
1192 return (int)refcnt;
1193}
1194
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001195/**
1196 * kmod_module_get_holders:
1197 * @mod: kmod module
1198 *
1199 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1200 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1201 *
1202 * Returns: a new list of kmod modules on success or NULL on failure.
1203 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001204KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1205{
1206 char dname[PATH_MAX];
1207 struct kmod_list *list = NULL;
1208 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001209
1210 if (mod == NULL)
1211 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001212
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001213 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1214
1215 d = opendir(dname);
1216 if (d == NULL) {
1217 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001218 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001219 return NULL;
1220 }
1221
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001222 for (;;) {
1223 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001224 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001225 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001226 int err;
1227
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001228 err = readdir_r(d, &de, &entp);
1229 if (err != 0) {
1230 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1231 mod->name, strerror(-err));
1232 goto fail;
1233 }
1234
1235 if (entp == NULL)
1236 break;
1237
1238 if (de.d_name[0] == '.') {
1239 if (de.d_name[1] == '\0' ||
1240 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001241 continue;
1242 }
1243
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001244 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001245 if (err < 0) {
1246 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001247 de.d_name, strerror(-err));
1248 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001249 }
1250
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001251 l = kmod_list_append(list, holder);
1252 if (l != NULL) {
1253 list = l;
1254 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001255 ERR(mod->ctx, "out of memory\n");
1256 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001257 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001258 }
1259 }
1260
1261 closedir(d);
1262 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001263
1264fail:
1265 closedir(d);
1266 kmod_module_unref_list(list);
1267 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001268}
1269
1270struct kmod_module_section {
1271 unsigned long address;
1272 char name[];
1273};
1274
1275static void kmod_module_section_free(struct kmod_module_section *section)
1276{
1277 free(section);
1278}
1279
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001280/**
1281 * kmod_module_get_sections:
1282 * @mod: kmod module
1283 *
1284 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1285 * structure contained in this list is internal to libkmod and their fields
1286 * can be obtained by calling kmod_module_section_get_name() and
1287 * kmod_module_section_get_address().
1288 *
1289 * After use, free the @list by calling kmod_module_section_free_list().
1290 *
1291 * Returns: a new list of kmod module sections on success or NULL on failure.
1292 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001293KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1294{
1295 char dname[PATH_MAX];
1296 struct kmod_list *list = NULL;
1297 DIR *d;
1298 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001299
1300 if (mod == NULL)
1301 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001302
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001303 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1304
1305 d = opendir(dname);
1306 if (d == NULL) {
1307 ERR(mod->ctx, "could not open '%s': %s\n",
1308 dname, strerror(errno));
1309 return NULL;
1310 }
1311
1312 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001313
1314 for (;;) {
1315 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001316 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001317 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001318 unsigned long address;
1319 size_t namesz;
1320 int fd, err;
1321
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001322 err = readdir_r(d, &de, &entp);
1323 if (err != 0) {
1324 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1325 mod->name, strerror(-err));
1326 goto fail;
1327 }
1328
1329 if (de.d_name[0] == '.') {
1330 if (de.d_name[1] == '\0' ||
1331 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001332 continue;
1333 }
1334
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001335 fd = openat(dfd, de.d_name, O_RDONLY);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001336 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001337 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1338 dname, de.d_name);
1339 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001340 }
1341
1342 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001343 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001344 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001345 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1346 dname, de.d_name);
1347 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001348 }
1349
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001350 namesz = strlen(de.d_name) + 1;
1351 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001352
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001353 if (section == NULL) {
1354 ERR(mod->ctx, "out of memory\n");
1355 goto fail;
1356 }
1357
1358 section->address = address;
1359 memcpy(section->name, de.d_name, namesz);
1360
1361 l = kmod_list_append(list, section);
1362 if (l != NULL) {
1363 list = l;
1364 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001365 ERR(mod->ctx, "out of memory\n");
1366 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001367 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001368 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001369 }
1370
1371 closedir(d);
1372 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001373
1374fail:
1375 closedir(d);
1376 kmod_module_unref_list(list);
1377 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001378}
1379
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001380/**
1381 * kmod_module_section_get_module_name:
1382 * @entry: a list entry representing a kmod module section
1383 *
1384 * Get the name of a kmod module section.
1385 *
1386 * After use, free the @list by calling kmod_module_section_free_list().
1387 *
1388 * Returns: the name of this kmod module section on success or NULL on
1389 * failure. The string is owned by the section, do not free it.
1390 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001391KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1392{
1393 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001394
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001395 if (entry == NULL)
1396 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001397
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001398 section = entry->data;
1399 return section->name;
1400}
1401
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001402/**
1403 * kmod_module_section_get_address:
1404 * @entry: a list entry representing a kmod module section
1405 *
1406 * Get the address of a kmod module section.
1407 *
1408 * After use, free the @list by calling kmod_module_section_free_list().
1409 *
1410 * Returns: the address of this kmod module section on success or ULONG_MAX
1411 * on failure.
1412 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001413KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1414{
1415 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001416
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001417 if (entry == NULL)
1418 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001419
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001420 section = entry->data;
1421 return section->address;
1422}
1423
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001424/**
1425 * kmod_module_section_free_list:
1426 * @list: kmod module section list
1427 *
1428 * Release the resources taken by @list
1429 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001430KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1431{
1432 while (list) {
1433 kmod_module_section_free(list->data);
1434 list = kmod_list_remove(list);
1435 }
1436}