Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 1 | /* |
| 2 | * libkmod - interface to kernel module operations |
| 3 | * |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 8 | * 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 Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 10 | * |
| 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 Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 21 | #include <assert.h> |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 22 | #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 Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 31 | #include <limits.h> |
| 32 | #include <dirent.h> |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/mman.h> |
Kees Cook | 144d182 | 2013-02-18 12:02:32 -0800 | [diff] [blame] | 36 | #include <sys/syscall.h> |
Thierry Vignaud | eff917c | 2012-01-17 17:32:48 -0200 | [diff] [blame] | 37 | #include <sys/wait.h> |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 38 | #include <string.h> |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 39 | #include <fnmatch.h> |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 40 | |
Kees Cook | 144d182 | 2013-02-18 12:02:32 -0800 | [diff] [blame] | 41 | #ifdef HAVE_LINUX_MODULE_H |
| 42 | #include <linux/module.h> |
| 43 | #endif |
| 44 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 45 | #include "libkmod.h" |
Lucas De Marchi | 83b855a | 2013-07-04 16:13:11 -0300 | [diff] [blame] | 46 | #include "libkmod-internal.h" |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 47 | |
| 48 | /** |
Lucas De Marchi | 6681951 | 2012-01-09 04:47:40 -0200 | [diff] [blame] | 49 | * SECTION:libkmod-module |
| 50 | * @short_description: operate on kernel modules |
| 51 | */ |
| 52 | |
| 53 | /** |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 54 | * kmod_module: |
| 55 | * |
| 56 | * Opaque object representing a module. |
| 57 | */ |
| 58 | struct kmod_module { |
| 59 | struct kmod_ctx *ctx; |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 60 | char *hashkey; |
Lucas De Marchi | 219f9c3 | 2011-12-13 13:07:40 -0200 | [diff] [blame] | 61 | char *name; |
Gustavo Sverzut Barbieri | f1fb6f8 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 62 | char *path; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 63 | struct kmod_list *dep; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 64 | char *options; |
Lucas De Marchi | 60f6760 | 2011-12-16 03:33:26 -0200 | [diff] [blame] | 65 | const char *install_commands; /* owned by kmod_config */ |
| 66 | const char *remove_commands; /* owned by kmod_config */ |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 67 | char *alias; /* only set if this module was created from an alias */ |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 68 | struct kmod_file *file; |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 69 | int n_dep; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 70 | int refcount; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 71 | struct { |
| 72 | bool dep : 1; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 73 | bool options : 1; |
| 74 | bool install_commands : 1; |
| 75 | bool remove_commands : 1; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 76 | } init; |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * private field used by kmod_module_get_probe_list() to detect |
| 80 | * dependency loops |
| 81 | */ |
Lucas De Marchi | ece09aa | 2012-01-18 01:26:44 -0200 | [diff] [blame] | 82 | bool visited : 1; |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * set by kmod_module_get_probe_list: indicates for probe_insert() |
| 86 | * whether the module's command and softdep should be ignored |
| 87 | */ |
| 88 | bool ignorecmd : 1; |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * if module was created by searching the modules.builtin file, this |
| 92 | * is set. There's nothing much useful one can do with such a |
| 93 | * "module", except knowing it's builtin. |
| 94 | */ |
| 95 | bool builtin : 1; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 96 | }; |
| 97 | |
Lucas De Marchi | c35347f | 2011-12-12 10:48:02 -0200 | [diff] [blame] | 98 | static inline const char *path_join(const char *path, size_t prefixlen, |
| 99 | char buf[PATH_MAX]) |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 100 | { |
| 101 | size_t pathlen; |
| 102 | |
| 103 | if (path[0] == '/') |
| 104 | return path; |
| 105 | |
| 106 | pathlen = strlen(path); |
| 107 | if (prefixlen + pathlen + 1 >= PATH_MAX) |
| 108 | return NULL; |
| 109 | |
| 110 | memcpy(buf + prefixlen, path, pathlen + 1); |
| 111 | return buf; |
| 112 | } |
| 113 | |
Dave Reisner | af9572c | 2012-02-02 11:07:33 -0500 | [diff] [blame] | 114 | static inline bool module_is_inkernel(struct kmod_module *mod) |
| 115 | { |
| 116 | int state = kmod_module_get_initstate(mod); |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 117 | |
Dave Reisner | af9572c | 2012-02-02 11:07:33 -0500 | [diff] [blame] | 118 | if (state == KMOD_MODULE_LIVE || |
Dave Reisner | af9572c | 2012-02-02 11:07:33 -0500 | [diff] [blame] | 119 | state == KMOD_MODULE_BUILTIN) |
| 120 | return true; |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 121 | |
| 122 | return false; |
Dave Reisner | af9572c | 2012-02-02 11:07:33 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 125 | int kmod_module_parse_depline(struct kmod_module *mod, char *line) |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 126 | { |
| 127 | struct kmod_ctx *ctx = mod->ctx; |
| 128 | struct kmod_list *list = NULL; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 129 | const char *dirname; |
| 130 | char buf[PATH_MAX]; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 131 | char *p, *saveptr; |
Lucas De Marchi | 45f2778 | 2011-12-12 17:23:04 -0200 | [diff] [blame] | 132 | int err = 0, n = 0; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 133 | size_t dirnamelen; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 134 | |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 135 | if (mod->init.dep) |
| 136 | return mod->n_dep; |
| 137 | assert(mod->dep == NULL); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 138 | mod->init.dep = true; |
| 139 | |
| 140 | p = strchr(line, ':'); |
| 141 | if (p == NULL) |
| 142 | return 0; |
| 143 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 144 | *p = '\0'; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 145 | dirname = kmod_get_dirname(mod->ctx); |
| 146 | dirnamelen = strlen(dirname); |
| 147 | if (dirnamelen + 2 >= PATH_MAX) |
| 148 | return 0; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 149 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 150 | memcpy(buf, dirname, dirnamelen); |
| 151 | buf[dirnamelen] = '/'; |
| 152 | dirnamelen++; |
| 153 | buf[dirnamelen] = '\0'; |
| 154 | |
| 155 | if (mod->path == NULL) { |
| 156 | const char *str = path_join(line, dirnamelen, buf); |
| 157 | if (str == NULL) |
| 158 | return 0; |
| 159 | mod->path = strdup(str); |
| 160 | if (mod->path == NULL) |
| 161 | return 0; |
| 162 | } |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 163 | |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 164 | p++; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 165 | for (p = strtok_r(p, " \t", &saveptr); p != NULL; |
| 166 | p = strtok_r(NULL, " \t", &saveptr)) { |
Lucas De Marchi | 1fc1c9a | 2011-12-02 10:00:03 -0200 | [diff] [blame] | 167 | struct kmod_module *depmod; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 168 | const char *path; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 169 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 170 | path = path_join(p, dirnamelen, buf); |
| 171 | if (path == NULL) { |
| 172 | ERR(ctx, "could not join path '%s' and '%s'.\n", |
| 173 | dirname, p); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 174 | goto fail; |
| 175 | } |
| 176 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 177 | err = kmod_module_new_from_path(ctx, path, &depmod); |
| 178 | if (err < 0) { |
| 179 | ERR(ctx, "ctx=%p path=%s error=%s\n", |
| 180 | ctx, path, strerror(-err)); |
| 181 | goto fail; |
| 182 | } |
| 183 | |
| 184 | DBG(ctx, "add dep: %s\n", path); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 185 | |
Lucas De Marchi | b94a737 | 2011-12-26 20:10:49 -0200 | [diff] [blame] | 186 | list = kmod_list_prepend(list, depmod); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 187 | n++; |
| 188 | } |
| 189 | |
| 190 | DBG(ctx, "%d dependencies for %s\n", n, mod->name); |
| 191 | |
| 192 | mod->dep = list; |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 193 | mod->n_dep = n; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 194 | return n; |
| 195 | |
| 196 | fail: |
| 197 | kmod_module_unref_list(list); |
| 198 | mod->init.dep = false; |
| 199 | return err; |
| 200 | } |
| 201 | |
Lucas De Marchi | ece09aa | 2012-01-18 01:26:44 -0200 | [diff] [blame] | 202 | void kmod_module_set_visited(struct kmod_module *mod, bool visited) |
| 203 | { |
| 204 | mod->visited = visited; |
| 205 | } |
| 206 | |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 207 | void kmod_module_set_builtin(struct kmod_module *mod, bool builtin) |
| 208 | { |
| 209 | mod->builtin = builtin; |
| 210 | } |
| 211 | |
Lucas De Marchi | 9c7f3ad | 2012-01-29 15:40:58 -0200 | [diff] [blame] | 212 | /* |
| 213 | * Memory layout with alias: |
| 214 | * |
| 215 | * struct kmod_module { |
| 216 | * hashkey -----. |
| 217 | * alias -----. | |
| 218 | * name ----. | | |
| 219 | * } | | | |
| 220 | * name <----------' | | |
| 221 | * alias <-----------' | |
| 222 | * name\alias <--------' |
| 223 | * |
| 224 | * Memory layout without alias: |
| 225 | * |
| 226 | * struct kmod_module { |
| 227 | * hashkey ---. |
| 228 | * alias -----|----> NULL |
| 229 | * name ----. | |
| 230 | * } | | |
| 231 | * name <----------'-' |
| 232 | * |
| 233 | * @key is "name\alias" or "name" (in which case alias == NULL) |
| 234 | */ |
| 235 | static int kmod_module_new(struct kmod_ctx *ctx, const char *key, |
| 236 | const char *name, size_t namelen, |
| 237 | const char *alias, size_t aliaslen, |
| 238 | struct kmod_module **mod) |
| 239 | { |
| 240 | struct kmod_module *m; |
| 241 | size_t keylen; |
| 242 | |
| 243 | m = kmod_pool_get_module(ctx, key); |
| 244 | if (m != NULL) { |
| 245 | *mod = kmod_module_ref(m); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | if (alias == NULL) |
| 250 | keylen = namelen; |
| 251 | else |
| 252 | keylen = namelen + aliaslen + 1; |
| 253 | |
| 254 | m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1)); |
| 255 | if (m == NULL) { |
| 256 | free(m); |
| 257 | return -ENOMEM; |
| 258 | } |
| 259 | |
| 260 | memset(m, 0, sizeof(*m)); |
| 261 | |
| 262 | m->ctx = kmod_ref(ctx); |
| 263 | m->name = (char *)m + sizeof(*m); |
| 264 | memcpy(m->name, key, keylen + 1); |
| 265 | if (alias == NULL) { |
| 266 | m->hashkey = m->name; |
| 267 | m->alias = NULL; |
| 268 | } else { |
| 269 | m->name[namelen] = '\0'; |
| 270 | m->alias = m->name + namelen + 1; |
| 271 | m->hashkey = m->name + keylen + 1; |
| 272 | memcpy(m->hashkey, key, keylen + 1); |
| 273 | } |
| 274 | |
| 275 | m->refcount = 1; |
| 276 | kmod_pool_add_module(ctx, m, m->hashkey); |
| 277 | *mod = m; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 282 | /** |
| 283 | * kmod_module_new_from_name: |
| 284 | * @ctx: kmod library context |
| 285 | * @name: name of the module |
| 286 | * @mod: where to save the created struct kmod_module |
| 287 | * |
| 288 | * Create a new struct kmod_module using the module name. @name can not be an |
| 289 | * alias, file name or anything else; it must be a module name. There's no |
Dan McGee | 9a252c2 | 2012-02-03 20:29:07 -0600 | [diff] [blame] | 290 | * check if the module exists in the system. |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 291 | * |
| 292 | * This function is also used internally by many others that return a new |
| 293 | * struct kmod_module or a new list of modules. |
| 294 | * |
| 295 | * The initial refcount is 1, and needs to be decremented to release the |
| 296 | * resources of the kmod_module. Since libkmod keeps track of all |
| 297 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 298 | * not unref @ctx before all the desired operations with the returned |
| 299 | * kmod_module are done. |
| 300 | * |
| 301 | * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid |
| 302 | * module name or if memory allocation failed. |
| 303 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 304 | KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx, |
| 305 | const char *name, |
| 306 | struct kmod_module **mod) |
| 307 | { |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 308 | size_t namelen; |
Lucas De Marchi | 6daceb2 | 2012-01-08 01:02:29 -0200 | [diff] [blame] | 309 | char name_norm[PATH_MAX]; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 310 | |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 311 | if (ctx == NULL || name == NULL || mod == NULL) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 312 | return -ENOENT; |
| 313 | |
Lucas De Marchi | 9c7f3ad | 2012-01-29 15:40:58 -0200 | [diff] [blame] | 314 | modname_normalize(name, name_norm, &namelen); |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 315 | |
Lucas De Marchi | 9c7f3ad | 2012-01-29 15:40:58 -0200 | [diff] [blame] | 316 | return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 317 | } |
| 318 | |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 319 | int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias, |
| 320 | const char *name, struct kmod_module **mod) |
| 321 | { |
| 322 | int err; |
Lucas De Marchi | 6daceb2 | 2012-01-08 01:02:29 -0200 | [diff] [blame] | 323 | char key[PATH_MAX]; |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 324 | size_t namelen = strlen(name); |
| 325 | size_t aliaslen = strlen(alias); |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 326 | |
Lucas De Marchi | 6daceb2 | 2012-01-08 01:02:29 -0200 | [diff] [blame] | 327 | if (namelen + aliaslen + 2 > PATH_MAX) |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 328 | return -ENAMETOOLONG; |
| 329 | |
| 330 | memcpy(key, name, namelen); |
| 331 | memcpy(key + namelen + 1, alias, aliaslen + 1); |
Lucas De Marchi | 9c7f3ad | 2012-01-29 15:40:58 -0200 | [diff] [blame] | 332 | key[namelen] = '\\'; |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 333 | |
Lucas De Marchi | 9c7f3ad | 2012-01-29 15:40:58 -0200 | [diff] [blame] | 334 | err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod); |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 335 | if (err < 0) |
| 336 | return err; |
| 337 | |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 338 | return 0; |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 339 | } |
| 340 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 341 | /** |
| 342 | * kmod_module_new_from_path: |
| 343 | * @ctx: kmod library context |
| 344 | * @path: path where to find the given module |
| 345 | * @mod: where to save the created struct kmod_module |
| 346 | * |
| 347 | * Create a new struct kmod_module using the module path. @path must be an |
| 348 | * existent file with in the filesystem and must be accessible to libkmod. |
| 349 | * |
| 350 | * The initial refcount is 1, and needs to be decremented to release the |
| 351 | * resources of the kmod_module. Since libkmod keeps track of all |
| 352 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 353 | * not unref @ctx before all the desired operations with the returned |
| 354 | * kmod_module are done. |
| 355 | * |
| 356 | * If @path is relative, it's treated as relative to the current working |
| 357 | * directory. Otherwise, give an absolute path. |
| 358 | * |
| 359 | * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if |
| 360 | * it's not a valid file for a kmod_module or if memory allocation failed. |
| 361 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 362 | KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx, |
| 363 | const char *path, |
| 364 | struct kmod_module **mod) |
| 365 | { |
| 366 | struct kmod_module *m; |
| 367 | int err; |
| 368 | struct stat st; |
Lucas De Marchi | 6daceb2 | 2012-01-08 01:02:29 -0200 | [diff] [blame] | 369 | char name[PATH_MAX]; |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 370 | char *abspath; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 371 | size_t namelen; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 372 | |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 373 | if (ctx == NULL || path == NULL || mod == NULL) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 374 | return -ENOENT; |
| 375 | |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 376 | abspath = path_make_absolute_cwd(path); |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 377 | if (abspath == NULL) { |
| 378 | DBG(ctx, "no absolute path for %s\n", path); |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 379 | return -ENOMEM; |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 380 | } |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 381 | |
| 382 | err = stat(abspath, &st); |
| 383 | if (err < 0) { |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 384 | err = -errno; |
| 385 | DBG(ctx, "stat %s: %s\n", path, strerror(errno)); |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 386 | free(abspath); |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 387 | return err; |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 388 | } |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 389 | |
Gustavo Sverzut Barbieri | 973c80b | 2011-12-12 18:28:52 -0200 | [diff] [blame] | 390 | if (path_to_modname(path, name, &namelen) == NULL) { |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 391 | DBG(ctx, "could not get modname from path %s\n", path); |
Gustavo Sverzut Barbieri | 973c80b | 2011-12-12 18:28:52 -0200 | [diff] [blame] | 392 | free(abspath); |
| 393 | return -ENOENT; |
| 394 | } |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 395 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 396 | m = kmod_pool_get_module(ctx, name); |
| 397 | if (m != NULL) { |
Lucas De Marchi | 6bd0b8d | 2011-12-07 14:08:01 -0200 | [diff] [blame] | 398 | if (m->path == NULL) |
| 399 | m->path = abspath; |
| 400 | else if (streq(m->path, abspath)) |
| 401 | free(abspath); |
| 402 | else { |
Lucas De Marchi | ebaa7be | 2011-12-27 18:10:19 -0200 | [diff] [blame] | 403 | ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n", |
Lucas De Marchi | 9c7f3ad | 2012-01-29 15:40:58 -0200 | [diff] [blame] | 404 | name, abspath, m->path); |
Lucas De Marchi | 6bd0b8d | 2011-12-07 14:08:01 -0200 | [diff] [blame] | 405 | free(abspath); |
| 406 | return -EEXIST; |
| 407 | } |
| 408 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 409 | *mod = kmod_module_ref(m); |
| 410 | return 0; |
| 411 | } |
| 412 | |
Lucas De Marchi | 9c7f3ad | 2012-01-29 15:40:58 -0200 | [diff] [blame] | 413 | err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m); |
| 414 | if (err < 0) |
| 415 | return err; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 416 | |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 417 | m->path = abspath; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 418 | *mod = m; |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 423 | /** |
| 424 | * kmod_module_unref: |
| 425 | * @mod: kmod module |
| 426 | * |
| 427 | * Drop a reference of the kmod module. If the refcount reaches zero, its |
| 428 | * resources are released. |
| 429 | * |
| 430 | * Returns: NULL if @mod is NULL or if the module was released. Otherwise it |
| 431 | * returns the passed @mod with its refcount decremented. |
| 432 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 433 | KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod) |
| 434 | { |
| 435 | if (mod == NULL) |
| 436 | return NULL; |
| 437 | |
| 438 | if (--mod->refcount > 0) |
| 439 | return mod; |
| 440 | |
| 441 | DBG(mod->ctx, "kmod_module %p released\n", mod); |
| 442 | |
Lucas De Marchi | 4084c17 | 2011-12-15 13:43:22 -0200 | [diff] [blame] | 443 | kmod_pool_del_module(mod->ctx, mod, mod->hashkey); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 444 | kmod_module_unref_list(mod->dep); |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 445 | |
| 446 | if (mod->file) |
| 447 | kmod_file_unref(mod->file); |
| 448 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 449 | kmod_unref(mod->ctx); |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 450 | free(mod->options); |
Gustavo Sverzut Barbieri | f1fb6f8 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 451 | free(mod->path); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 452 | free(mod); |
| 453 | return NULL; |
| 454 | } |
| 455 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 456 | /** |
| 457 | * kmod_module_ref: |
| 458 | * @mod: kmod module |
| 459 | * |
| 460 | * Take a reference of the kmod module, incrementing its refcount. |
| 461 | * |
| 462 | * Returns: the passed @module with its refcount incremented. |
| 463 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 464 | KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod) |
| 465 | { |
| 466 | if (mod == NULL) |
| 467 | return NULL; |
| 468 | |
| 469 | mod->refcount++; |
| 470 | |
| 471 | return mod; |
| 472 | } |
| 473 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 474 | #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \ |
| 475 | do { \ |
| 476 | if ((_err) < 0) \ |
| 477 | goto _label_err; \ |
| 478 | if (*(_list) != NULL) \ |
| 479 | goto finish; \ |
| 480 | } while (0) |
| 481 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 482 | /** |
| 483 | * kmod_module_new_from_lookup: |
| 484 | * @ctx: kmod library context |
| 485 | * @given_alias: alias to look for |
| 486 | * @list: an empty list where to save the list of modules matching |
| 487 | * @given_alias |
| 488 | * |
| 489 | * Create a new list of kmod modules using an alias or module name and lookup |
| 490 | * libkmod's configuration files and indexes in order to find the module. |
| 491 | * Once it's found in one of the places, it stops searching and create the |
| 492 | * list of modules that is saved in @list. |
| 493 | * |
| 494 | * The search order is: 1. aliases in configuration file; 2. module names in |
| 495 | * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases |
| 496 | * in modules.alias index. |
| 497 | * |
| 498 | * The initial refcount is 1, and needs to be decremented to release the |
| 499 | * resources of the kmod_module. The returned @list must be released by |
| 500 | * calling kmod_module_unref_list(). Since libkmod keeps track of all |
| 501 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 502 | * not unref @ctx before all the desired operations with the returned list are |
| 503 | * completed. |
| 504 | * |
| 505 | * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup |
| 506 | * methods failed, which is basically due to memory allocation fail. If module |
| 507 | * is not found, it still returns 0, but @list is an empty list. |
| 508 | */ |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 509 | KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx, |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 510 | const char *given_alias, |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 511 | struct kmod_list **list) |
| 512 | { |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 513 | int err; |
Lucas De Marchi | 6daceb2 | 2012-01-08 01:02:29 -0200 | [diff] [blame] | 514 | char alias[PATH_MAX]; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 515 | |
Lucas De Marchi | 4308b17 | 2011-12-13 10:26:04 -0200 | [diff] [blame] | 516 | if (ctx == NULL || given_alias == NULL) |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 517 | return -ENOENT; |
| 518 | |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 519 | if (list == NULL || *list != NULL) { |
| 520 | ERR(ctx, "An empty list is needed to create lookup\n"); |
| 521 | return -ENOSYS; |
| 522 | } |
| 523 | |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 524 | if (alias_normalize(given_alias, alias, NULL) < 0) { |
| 525 | DBG(ctx, "invalid alias: %s\n", given_alias); |
Lucas De Marchi | d470db1 | 2011-12-13 10:28:00 -0200 | [diff] [blame] | 526 | return -EINVAL; |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias); |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 530 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 531 | /* Aliases from config file override all the others */ |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 532 | err = kmod_lookup_alias_from_config(ctx, alias, list); |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 533 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 534 | |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 535 | DBG(ctx, "lookup modules.dep %s\n", alias); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 536 | err = kmod_lookup_alias_from_moddep_file(ctx, alias, list); |
| 537 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
| 538 | |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 539 | DBG(ctx, "lookup modules.symbols %s\n", alias); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 540 | err = kmod_lookup_alias_from_symbols_file(ctx, alias, list); |
| 541 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 542 | |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 543 | DBG(ctx, "lookup install and remove commands %s\n", alias); |
Lucas De Marchi | f4fc552 | 2011-12-16 03:57:12 -0200 | [diff] [blame] | 544 | err = kmod_lookup_alias_from_commands(ctx, alias, list); |
| 545 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 546 | |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 547 | DBG(ctx, "lookup modules.aliases %s\n", alias); |
Lucas De Marchi | 49e61ca | 2011-12-01 16:27:04 -0200 | [diff] [blame] | 548 | err = kmod_lookup_alias_from_aliases_file(ctx, alias, list); |
| 549 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
| 550 | |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 551 | DBG(ctx, "lookup modules.builtin %s\n", alias); |
| 552 | err = kmod_lookup_alias_from_builtin_file(ctx, alias, list); |
| 553 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
| 554 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 555 | finish: |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 556 | DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list); |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 557 | return err; |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 558 | fail: |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame] | 559 | DBG(ctx, "Failed to lookup %s\n", alias); |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 560 | kmod_module_unref_list(*list); |
| 561 | *list = NULL; |
Lucas De Marchi | 84f4220 | 2011-12-02 10:03:34 -0200 | [diff] [blame] | 562 | return err; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 563 | } |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 564 | #undef CHECK_ERR_AND_FINISH |
| 565 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 566 | /** |
Lucas De Marchi | 91428ae | 2011-12-15 13:09:46 -0200 | [diff] [blame] | 567 | * kmod_module_unref_list: |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 568 | * @list: list of kmod modules |
| 569 | * |
| 570 | * Drop a reference of each kmod module in @list and releases the resources |
| 571 | * taken by the list itself. |
| 572 | * |
Chengwei Yang | 491c490 | 2013-05-04 17:07:02 +0800 | [diff] [blame] | 573 | * Returns: 0 |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 574 | */ |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 575 | KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list) |
| 576 | { |
| 577 | for (; list != NULL; list = kmod_list_remove(list)) |
| 578 | kmod_module_unref(list->data); |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
Lucas De Marchi | 0d46743 | 2011-12-31 11:15:52 -0200 | [diff] [blame] | 583 | /** |
| 584 | * kmod_module_get_filtered_blacklist: |
| 585 | * @ctx: kmod library context |
| 586 | * @input: list of kmod_module to be filtered with blacklist |
| 587 | * @output: where to save the new list |
| 588 | * |
Kay Sievers | 471a7d0 | 2012-04-14 20:47:47 +0200 | [diff] [blame] | 589 | * This function should not be used. Use kmod_module_apply_filter instead. |
Dave Reisner | d80b103 | 2012-02-24 10:05:11 -0500 | [diff] [blame] | 590 | * |
Lucas De Marchi | 0d46743 | 2011-12-31 11:15:52 -0200 | [diff] [blame] | 591 | * Given a list @input, this function filter it out with config's blacklist |
Dave Reisner | d80b103 | 2012-02-24 10:05:11 -0500 | [diff] [blame] | 592 | * and save it in @output. |
Lucas De Marchi | 0d46743 | 2011-12-31 11:15:52 -0200 | [diff] [blame] | 593 | * |
| 594 | * Returns: 0 on success or < 0 otherwise. @output is saved with the updated |
| 595 | * list. |
| 596 | */ |
| 597 | KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx, |
| 598 | const struct kmod_list *input, |
| 599 | struct kmod_list **output) |
| 600 | { |
Dave Reisner | d80b103 | 2012-02-24 10:05:11 -0500 | [diff] [blame] | 601 | return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output); |
Lucas De Marchi | 0d46743 | 2011-12-31 11:15:52 -0200 | [diff] [blame] | 602 | } |
| 603 | |
Lucas De Marchi | b72f74b | 2011-12-27 04:51:05 -0200 | [diff] [blame] | 604 | static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod) |
| 605 | { |
| 606 | if (!mod->init.dep) { |
| 607 | /* lazy init */ |
| 608 | char *line = kmod_search_moddep(mod->ctx, mod->name); |
| 609 | |
| 610 | if (line == NULL) |
| 611 | return NULL; |
| 612 | |
| 613 | kmod_module_parse_depline((struct kmod_module *)mod, line); |
| 614 | free(line); |
| 615 | |
| 616 | if (!mod->init.dep) |
| 617 | return NULL; |
| 618 | } |
| 619 | |
| 620 | return mod->dep; |
| 621 | } |
| 622 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 623 | /** |
Lucas De Marchi | 91428ae | 2011-12-15 13:09:46 -0200 | [diff] [blame] | 624 | * kmod_module_get_dependencies: |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 625 | * @mod: kmod module |
| 626 | * |
| 627 | * Search the modules.dep index to find the dependencies of the given @mod. |
| 628 | * The result is cached in @mod, so subsequent calls to this function will |
| 629 | * return the already searched list of modules. |
| 630 | * |
Chengwei Yang | 491c490 | 2013-05-04 17:07:02 +0800 | [diff] [blame] | 631 | * Returns: NULL on failure. Otherwise it returns a list of kmod modules |
| 632 | * that can be released by calling kmod_module_unref_list(). |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 633 | */ |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 634 | KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod) |
Lucas De Marchi | 0835fc3 | 2011-12-01 20:06:08 -0200 | [diff] [blame] | 635 | { |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 636 | struct kmod_list *l, *l_new, *list_new = NULL; |
| 637 | |
| 638 | if (mod == NULL) |
| 639 | return NULL; |
| 640 | |
Lucas De Marchi | b72f74b | 2011-12-27 04:51:05 -0200 | [diff] [blame] | 641 | module_get_dependencies_noref(mod); |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 642 | |
| 643 | kmod_list_foreach(l, mod->dep) { |
| 644 | l_new = kmod_list_append(list_new, kmod_module_ref(l->data)); |
| 645 | if (l_new == NULL) { |
| 646 | kmod_module_unref(l->data); |
| 647 | goto fail; |
| 648 | } |
| 649 | |
| 650 | list_new = l_new; |
| 651 | } |
| 652 | |
| 653 | return list_new; |
| 654 | |
| 655 | fail: |
| 656 | ERR(mod->ctx, "out of memory\n"); |
| 657 | kmod_module_unref_list(list_new); |
| 658 | return NULL; |
Lucas De Marchi | 0835fc3 | 2011-12-01 20:06:08 -0200 | [diff] [blame] | 659 | } |
| 660 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 661 | /** |
| 662 | * kmod_module_get_module: |
| 663 | * @entry: an entry in a list of kmod modules. |
| 664 | * |
| 665 | * Get the kmod module of this @entry in the list, increasing its refcount. |
| 666 | * After it's used, unref it. Since the refcount is incremented upon return, |
| 667 | * you still have to call kmod_module_unref_list() to release the list of kmod |
| 668 | * modules. |
| 669 | * |
| 670 | * Returns: NULL on failure or the kmod_module contained in this list entry |
| 671 | * with its refcount incremented. |
| 672 | */ |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 673 | KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry) |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 674 | { |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 675 | if (entry == NULL) |
| 676 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 677 | |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 678 | return kmod_module_ref(entry->data); |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 679 | } |
| 680 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 681 | /** |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 682 | * kmod_module_get_name: |
| 683 | * @mod: kmod module |
| 684 | * |
| 685 | * Get the name of this kmod module. Name is always available, independently |
| 686 | * if it was created by kmod_module_new_from_name() or another function and |
| 687 | * it's always normalized (dashes are replaced with underscores). |
| 688 | * |
| 689 | * Returns: the name of this kmod module. |
| 690 | */ |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 691 | KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod) |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 692 | { |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 693 | if (mod == NULL) |
| 694 | return NULL; |
| 695 | |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 696 | return mod->name; |
| 697 | } |
| 698 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 699 | /** |
| 700 | * kmod_module_get_path: |
| 701 | * @mod: kmod module |
| 702 | * |
| 703 | * Get the path of this kmod module. If this kmod module was not created by |
| 704 | * path, it can search the modules.dep index in order to find out the module |
Lucas De Marchi | db74cee | 2012-01-09 03:45:19 -0200 | [diff] [blame] | 705 | * under context's dirname. |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 706 | * |
| 707 | * Returns: the path of this kmod module or NULL if such information is not |
| 708 | * available. |
| 709 | */ |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 710 | KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod) |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 711 | { |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 712 | char *line; |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 713 | |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 714 | if (mod == NULL) |
| 715 | return NULL; |
| 716 | |
Gustavo Sverzut Barbieri | d01c67e | 2011-12-11 19:42:02 -0200 | [diff] [blame] | 717 | DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path); |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 718 | |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 719 | if (mod->path != NULL) |
| 720 | return mod->path; |
| 721 | if (mod->init.dep) |
| 722 | return NULL; |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 723 | |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 724 | /* lazy init */ |
| 725 | line = kmod_search_moddep(mod->ctx, mod->name); |
| 726 | if (line == NULL) |
| 727 | return NULL; |
| 728 | |
| 729 | kmod_module_parse_depline((struct kmod_module *) mod, line); |
| 730 | free(line); |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 731 | |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 732 | return mod->path; |
| 733 | } |
| 734 | |
| 735 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 736 | extern long delete_module(const char *name, unsigned int flags); |
| 737 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 738 | /** |
| 739 | * kmod_module_remove_module: |
| 740 | * @mod: kmod module |
Chengwei Yang | d7152f6 | 2013-05-04 17:07:03 +0800 | [diff] [blame] | 741 | * @flags: flags to pass to Linux kernel when removing the module, valid flags are |
| 742 | * KMOD_REMOVE_FORCE: force remove module regardless if it's still in |
| 743 | * use by a kernel subsystem or other process; |
| 744 | * KMOD_REMOVE_NOWAIT: return immediately. It will fail if the module |
| 745 | * is in using and KMOD_REMOVE_FORCE is not specified. |
| 746 | * If this module is in use by any kernel subsystem or process, not using |
| 747 | * this flag will cause the call to block indefinitely, until the module |
| 748 | * is not in use anymore. Always use this flag, it's deprecated not using |
| 749 | * it and the default behavior might change in future to always set it. |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 750 | * |
| 751 | * Remove a module from Linux kernel. |
| 752 | * |
| 753 | * Returns: 0 on success or < 0 on failure. |
| 754 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 755 | KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, |
| 756 | unsigned int flags) |
| 757 | { |
| 758 | int err; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 759 | |
| 760 | if (mod == NULL) |
| 761 | return -ENOENT; |
| 762 | |
| 763 | /* Filter out other flags */ |
| 764 | flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT); |
| 765 | |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 766 | err = delete_module(mod->name, flags); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 767 | if (err != 0) { |
Lucas De Marchi | ba998b9 | 2012-01-11 00:08:14 -0200 | [diff] [blame] | 768 | err = -errno; |
| 769 | ERR(mod->ctx, "could not remove '%s': %m\n", mod->name); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 770 | } |
| 771 | |
Lucas De Marchi | ba998b9 | 2012-01-11 00:08:14 -0200 | [diff] [blame] | 772 | return err; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 773 | } |
| 774 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 775 | extern long init_module(const void *mem, unsigned long len, const char *args); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 776 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 777 | /** |
| 778 | * kmod_module_insert_module: |
| 779 | * @mod: kmod module |
Lucas De Marchi | 142db57 | 2011-12-20 23:39:30 -0200 | [diff] [blame] | 780 | * @flags: flags are not passed to Linux Kernel, but instead they dictate the |
Chengwei Yang | d7152f6 | 2013-05-04 17:07:03 +0800 | [diff] [blame] | 781 | * behavior of this function, valid flags are |
| 782 | * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic; |
| 783 | * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes. |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 784 | * @options: module's options to pass to Linux Kernel. |
| 785 | * |
| 786 | * Insert a module in Linux kernel. It opens the file pointed by @mod, |
| 787 | * mmap'ing it and passing to kernel. |
| 788 | * |
Lucas De Marchi | bbf5932 | 2011-12-30 14:13:33 -0200 | [diff] [blame] | 789 | * Returns: 0 on success or < 0 on failure. If module is already loaded it |
| 790 | * returns -EEXIST. |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 791 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 792 | KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod, |
Gustavo Sverzut Barbieri | 3a721bb | 2011-12-10 21:02:39 -0200 | [diff] [blame] | 793 | unsigned int flags, |
| 794 | const char *options) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 795 | { |
| 796 | int err; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 797 | const void *mem; |
Gustavo Sverzut Barbieri | 3d8226e | 2011-12-16 16:08:53 -0200 | [diff] [blame] | 798 | off_t size; |
| 799 | struct kmod_file *file; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 800 | struct kmod_elf *elf = NULL; |
| 801 | const char *path; |
Gustavo Sverzut Barbieri | 3a721bb | 2011-12-10 21:02:39 -0200 | [diff] [blame] | 802 | const char *args = options ? options : ""; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 803 | |
| 804 | if (mod == NULL) |
| 805 | return -ENOENT; |
| 806 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 807 | path = kmod_module_get_path(mod); |
| 808 | if (path == NULL) { |
Dave Reisner | b787b56 | 2012-01-04 10:59:49 -0500 | [diff] [blame] | 809 | ERR(mod->ctx, "could not find module by name='%s'\n", mod->name); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 810 | return -ENOSYS; |
| 811 | } |
| 812 | |
Lucas De Marchi | c68e92f | 2012-01-04 08:19:34 -0200 | [diff] [blame] | 813 | file = kmod_file_open(mod->ctx, path); |
Gustavo Sverzut Barbieri | 3d8226e | 2011-12-16 16:08:53 -0200 | [diff] [blame] | 814 | if (file == NULL) { |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 815 | err = -errno; |
| 816 | return err; |
| 817 | } |
| 818 | |
Kees Cook | 144d182 | 2013-02-18 12:02:32 -0800 | [diff] [blame] | 819 | if (kmod_file_get_direct(file)) { |
| 820 | unsigned int kernel_flags = 0; |
| 821 | |
Kees Cook | 144d182 | 2013-02-18 12:02:32 -0800 | [diff] [blame] | 822 | if (flags & KMOD_INSERT_FORCE_VERMAGIC) |
| 823 | kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC; |
| 824 | if (flags & KMOD_INSERT_FORCE_MODVERSION) |
| 825 | kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS; |
Kees Cook | 144d182 | 2013-02-18 12:02:32 -0800 | [diff] [blame] | 826 | |
| 827 | err = finit_module(kmod_file_get_fd(file), args, kernel_flags); |
| 828 | if (err == 0 || errno != ENOSYS) |
| 829 | goto init_finished; |
| 830 | } |
| 831 | |
Gustavo Sverzut Barbieri | 3d8226e | 2011-12-16 16:08:53 -0200 | [diff] [blame] | 832 | size = kmod_file_get_size(file); |
| 833 | mem = kmod_file_get_contents(file); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 834 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 835 | if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) { |
| 836 | elf = kmod_elf_new(mem, size); |
| 837 | if (elf == NULL) { |
| 838 | err = -errno; |
| 839 | goto elf_failed; |
| 840 | } |
| 841 | |
| 842 | if (flags & KMOD_INSERT_FORCE_MODVERSION) { |
| 843 | err = kmod_elf_strip_section(elf, "__versions"); |
| 844 | if (err < 0) |
| 845 | INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err)); |
| 846 | } |
| 847 | |
| 848 | if (flags & KMOD_INSERT_FORCE_VERMAGIC) { |
| 849 | err = kmod_elf_strip_vermagic(elf); |
| 850 | if (err < 0) |
| 851 | INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err)); |
| 852 | } |
| 853 | |
| 854 | mem = kmod_elf_get_memory(elf); |
| 855 | } |
| 856 | |
Gustavo Sverzut Barbieri | 3d8226e | 2011-12-16 16:08:53 -0200 | [diff] [blame] | 857 | err = init_module(mem, size, args); |
Kees Cook | 144d182 | 2013-02-18 12:02:32 -0800 | [diff] [blame] | 858 | init_finished: |
Lucas De Marchi | bbf5932 | 2011-12-30 14:13:33 -0200 | [diff] [blame] | 859 | if (err < 0) { |
| 860 | err = -errno; |
| 861 | INFO(mod->ctx, "Failed to insert module '%s': %m\n", path); |
| 862 | } |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 863 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 864 | if (elf != NULL) |
| 865 | kmod_elf_unref(elf); |
| 866 | elf_failed: |
Gustavo Sverzut Barbieri | 3d8226e | 2011-12-16 16:08:53 -0200 | [diff] [blame] | 867 | kmod_file_unref(file); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 868 | |
| 869 | return err; |
| 870 | } |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 871 | |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 872 | static bool module_is_blacklisted(struct kmod_module *mod) |
| 873 | { |
| 874 | struct kmod_ctx *ctx = mod->ctx; |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 875 | const struct kmod_config *config = kmod_get_config(ctx); |
| 876 | const struct kmod_list *bl = config->blacklists; |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 877 | const struct kmod_list *l; |
| 878 | |
| 879 | kmod_list_foreach(l, bl) { |
| 880 | const char *modname = kmod_blacklist_get_modname(l); |
| 881 | |
| 882 | if (streq(modname, mod->name)) |
| 883 | return true; |
| 884 | } |
| 885 | |
| 886 | return false; |
| 887 | } |
| 888 | |
Dave Reisner | d80b103 | 2012-02-24 10:05:11 -0500 | [diff] [blame] | 889 | /** |
| 890 | * kmod_module_apply_filter |
| 891 | * @ctx: kmod library context |
Chengwei Yang | d7152f6 | 2013-05-04 17:07:03 +0800 | [diff] [blame] | 892 | * @filter_type: bitmask to filter modules out, valid types are |
| 893 | * KMOD_FILTER_BLACKLIST: filter modules in blacklist out; |
| 894 | * KMOD_FILTER_BUILTIN: filter builtin modules out. |
Dave Reisner | d80b103 | 2012-02-24 10:05:11 -0500 | [diff] [blame] | 895 | * @input: list of kmod_module to be filtered |
| 896 | * @output: where to save the new list |
| 897 | * |
| 898 | * Given a list @input, this function filter it out by the filter mask |
| 899 | * and save it in @output. |
| 900 | * |
| 901 | * Returns: 0 on success or < 0 otherwise. @output is saved with the updated |
| 902 | * list. |
| 903 | */ |
| 904 | KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx, |
| 905 | enum kmod_filter filter_type, |
| 906 | const struct kmod_list *input, |
| 907 | struct kmod_list **output) |
| 908 | { |
| 909 | const struct kmod_list *li; |
| 910 | |
| 911 | if (ctx == NULL || output == NULL) |
| 912 | return -ENOENT; |
| 913 | |
| 914 | *output = NULL; |
| 915 | if (input == NULL) |
| 916 | return 0; |
| 917 | |
| 918 | kmod_list_foreach(li, input) { |
| 919 | struct kmod_module *mod = li->data; |
| 920 | struct kmod_list *node; |
| 921 | |
| 922 | if ((filter_type & KMOD_FILTER_BLACKLIST) && |
| 923 | module_is_blacklisted(mod)) |
| 924 | continue; |
| 925 | |
Dave Reisner | bdda7e1 | 2012-02-24 23:02:06 -0500 | [diff] [blame] | 926 | if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin) |
Dave Reisner | d80b103 | 2012-02-24 10:05:11 -0500 | [diff] [blame] | 927 | continue; |
| 928 | |
| 929 | node = kmod_list_append(*output, mod); |
| 930 | if (node == NULL) |
| 931 | goto fail; |
| 932 | |
| 933 | *output = node; |
| 934 | kmod_module_ref(mod); |
| 935 | } |
| 936 | |
| 937 | return 0; |
| 938 | |
| 939 | fail: |
| 940 | kmod_module_unref_list(*output); |
| 941 | *output = NULL; |
| 942 | return -ENOMEM; |
| 943 | } |
| 944 | |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 945 | static int command_do(struct kmod_module *mod, const char *type, |
| 946 | const char *cmd) |
| 947 | { |
| 948 | const char *modname = kmod_module_get_name(mod); |
| 949 | int err; |
| 950 | |
| 951 | DBG(mod->ctx, "%s %s\n", type, cmd); |
| 952 | |
| 953 | setenv("MODPROBE_MODULE", modname, 1); |
| 954 | err = system(cmd); |
| 955 | unsetenv("MODPROBE_MODULE"); |
| 956 | |
| 957 | if (err == -1 || WEXITSTATUS(err)) { |
| 958 | ERR(mod->ctx, "Error running %s command for %s\n", |
| 959 | type, modname); |
| 960 | if (err != -1) |
| 961 | err = -WEXITSTATUS(err); |
| 962 | } |
| 963 | |
| 964 | return err; |
| 965 | } |
| 966 | |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 967 | struct probe_insert_cb { |
| 968 | int (*run_install)(struct kmod_module *m, const char *cmd, void *data); |
| 969 | void *data; |
| 970 | }; |
| 971 | |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 972 | static int module_do_install_commands(struct kmod_module *mod, |
| 973 | const char *options, |
| 974 | struct probe_insert_cb *cb) |
| 975 | { |
| 976 | const char *command = kmod_module_get_install_commands(mod); |
| 977 | char *p, *cmd; |
| 978 | int err; |
| 979 | size_t cmdlen, options_len, varlen; |
| 980 | |
| 981 | assert(command); |
| 982 | |
| 983 | if (options == NULL) |
| 984 | options = ""; |
| 985 | |
| 986 | options_len = strlen(options); |
| 987 | cmdlen = strlen(command); |
| 988 | varlen = sizeof("$CMDLINE_OPTS") - 1; |
| 989 | |
| 990 | cmd = memdup(command, cmdlen + 1); |
| 991 | if (cmd == NULL) |
| 992 | return -ENOMEM; |
| 993 | |
| 994 | while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) { |
| 995 | size_t prefixlen = p - cmd; |
| 996 | size_t suffixlen = cmdlen - prefixlen - varlen; |
| 997 | size_t slen = cmdlen - varlen + options_len; |
| 998 | char *suffix = p + varlen; |
| 999 | char *s = malloc(slen + 1); |
| 1000 | if (s == NULL) { |
| 1001 | free(cmd); |
| 1002 | return -ENOMEM; |
| 1003 | } |
| 1004 | memcpy(s, cmd, p - cmd); |
| 1005 | memcpy(s + prefixlen, options, options_len); |
| 1006 | memcpy(s + prefixlen + options_len, suffix, suffixlen); |
| 1007 | s[slen] = '\0'; |
| 1008 | |
| 1009 | free(cmd); |
| 1010 | cmd = s; |
| 1011 | cmdlen = slen; |
| 1012 | } |
| 1013 | |
| 1014 | if (cb->run_install != NULL) |
| 1015 | err = cb->run_install(mod, cmd, cb->data); |
| 1016 | else |
| 1017 | err = command_do(mod, "install", cmd); |
| 1018 | |
| 1019 | free(cmd); |
| 1020 | |
| 1021 | return err; |
| 1022 | } |
| 1023 | |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1024 | static char *module_options_concat(const char *opt, const char *xopt) |
| 1025 | { |
| 1026 | // TODO: we might need to check if xopt overrides options on opt |
| 1027 | size_t optlen = opt == NULL ? 0 : strlen(opt); |
| 1028 | size_t xoptlen = xopt == NULL ? 0 : strlen(xopt); |
| 1029 | char *r; |
| 1030 | |
| 1031 | if (optlen == 0 && xoptlen == 0) |
| 1032 | return NULL; |
| 1033 | |
| 1034 | r = malloc(optlen + xoptlen + 2); |
| 1035 | |
| 1036 | if (opt != NULL) { |
| 1037 | memcpy(r, opt, optlen); |
| 1038 | r[optlen] = ' '; |
| 1039 | optlen++; |
| 1040 | } |
| 1041 | |
| 1042 | if (xopt != NULL) |
| 1043 | memcpy(r + optlen, xopt, xoptlen); |
| 1044 | |
| 1045 | r[optlen + xoptlen] = '\0'; |
| 1046 | |
| 1047 | return r; |
| 1048 | } |
| 1049 | |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1050 | static int __kmod_module_get_probe_list(struct kmod_module *mod, |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1051 | bool ignorecmd, |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1052 | struct kmod_list **list); |
| 1053 | |
| 1054 | /* re-entrant */ |
| 1055 | static int __kmod_module_fill_softdep(struct kmod_module *mod, |
| 1056 | struct kmod_list **list) |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1057 | { |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1058 | struct kmod_list *pre = NULL, *post = NULL, *l; |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1059 | int err; |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1060 | |
| 1061 | err = kmod_module_get_softdeps(mod, &pre, &post); |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1062 | if (err < 0) { |
Lucas De Marchi | 050db08 | 2012-02-18 03:56:21 -0200 | [diff] [blame] | 1063 | ERR(mod->ctx, "could not get softdep: %s\n", |
| 1064 | strerror(-err)); |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1065 | goto fail; |
| 1066 | } |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1067 | |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1068 | kmod_list_foreach(l, pre) { |
| 1069 | struct kmod_module *m = l->data; |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1070 | err = __kmod_module_get_probe_list(m, false, list); |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1071 | if (err < 0) |
| 1072 | goto fail; |
| 1073 | } |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1074 | |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1075 | l = kmod_list_append(*list, kmod_module_ref(mod)); |
| 1076 | if (l == NULL) { |
| 1077 | kmod_module_unref(mod); |
| 1078 | err = -ENOMEM; |
| 1079 | goto fail; |
| 1080 | } |
| 1081 | *list = l; |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1082 | mod->ignorecmd = (pre != NULL || post != NULL); |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1083 | |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1084 | kmod_list_foreach(l, post) { |
| 1085 | struct kmod_module *m = l->data; |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1086 | err = __kmod_module_get_probe_list(m, false, list); |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1087 | if (err < 0) |
| 1088 | goto fail; |
| 1089 | } |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1090 | |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1091 | fail: |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1092 | kmod_module_unref_list(pre); |
| 1093 | kmod_module_unref_list(post); |
| 1094 | |
| 1095 | return err; |
| 1096 | } |
| 1097 | |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1098 | /* re-entrant */ |
| 1099 | static int __kmod_module_get_probe_list(struct kmod_module *mod, |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1100 | bool ignorecmd, |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1101 | struct kmod_list **list) |
| 1102 | { |
| 1103 | struct kmod_list *dep, *l; |
| 1104 | int err = 0; |
| 1105 | |
| 1106 | if (mod->visited) { |
| 1107 | DBG(mod->ctx, "Ignore module '%s': already visited\n", |
| 1108 | mod->name); |
| 1109 | return 0; |
| 1110 | } |
Lucas De Marchi | 8cd0f9e | 2012-02-11 19:45:29 -0200 | [diff] [blame] | 1111 | mod->visited = true; |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1112 | |
| 1113 | dep = kmod_module_get_dependencies(mod); |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1114 | kmod_list_foreach(l, dep) { |
| 1115 | struct kmod_module *m = l->data; |
| 1116 | err = __kmod_module_fill_softdep(m, list); |
| 1117 | if (err < 0) |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1118 | goto finish; |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1119 | } |
| 1120 | |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1121 | if (ignorecmd) { |
| 1122 | l = kmod_list_append(*list, kmod_module_ref(mod)); |
| 1123 | if (l == NULL) { |
| 1124 | kmod_module_unref(mod); |
| 1125 | err = -ENOMEM; |
| 1126 | goto finish; |
| 1127 | } |
| 1128 | *list = l; |
| 1129 | mod->ignorecmd = true; |
| 1130 | } else |
| 1131 | err = __kmod_module_fill_softdep(mod, list); |
| 1132 | |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1133 | finish: |
| 1134 | kmod_module_unref_list(dep); |
| 1135 | return err; |
| 1136 | } |
| 1137 | |
| 1138 | static int kmod_module_get_probe_list(struct kmod_module *mod, |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1139 | bool ignorecmd, |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1140 | struct kmod_list **list) |
| 1141 | { |
| 1142 | int err; |
| 1143 | |
| 1144 | assert(mod != NULL); |
| 1145 | assert(list != NULL && *list == NULL); |
| 1146 | |
| 1147 | /* |
| 1148 | * Make sure we don't get screwed by previous calls to this function |
| 1149 | */ |
| 1150 | kmod_set_modules_visited(mod->ctx, false); |
| 1151 | |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1152 | err = __kmod_module_get_probe_list(mod, ignorecmd, list); |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1153 | if (err < 0) { |
| 1154 | kmod_module_unref_list(*list); |
| 1155 | *list = NULL; |
| 1156 | } |
| 1157 | |
| 1158 | return err; |
| 1159 | } |
| 1160 | |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1161 | /** |
| 1162 | * kmod_module_probe_insert_module: |
| 1163 | * @mod: kmod module |
| 1164 | * @flags: flags are not passed to Linux Kernel, but instead they dictate the |
Chengwei Yang | d7152f6 | 2013-05-04 17:07:03 +0800 | [diff] [blame] | 1165 | * behavior of this function, valid flags are |
| 1166 | * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic; |
| 1167 | * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes; |
| 1168 | * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install |
| 1169 | * commands and softdeps configured in the system; |
| 1170 | * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already |
| 1171 | * live in kernel or not; |
| 1172 | * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the |
| 1173 | * associated callback function; |
| 1174 | * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified |
| 1175 | * and the module is already live in kernel, the function will fail if this |
| 1176 | * flag is specified; |
| 1177 | * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST |
| 1178 | * filter to this module and its dependencies. If any of the dependencies (or |
| 1179 | * the module) is blacklisted, the probe will fail, unless the blacklisted |
| 1180 | * module is already live in kernel; |
| 1181 | * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted; |
| 1182 | * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an |
| 1183 | * alias and is blacklisted. |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1184 | * @extra_options: module's options to pass to Linux Kernel. It applies only |
| 1185 | * to @mod, not to its dependencies. |
| 1186 | * @run_install: function to run when @mod is backed by an install command. |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1187 | * @data: data to give back to @run_install callback |
Lucas De Marchi | 6bd0713 | 2012-01-30 17:02:06 -0200 | [diff] [blame] | 1188 | * @print_action: function to call with the action being taken (install or |
| 1189 | * insmod). It's useful for tools like modprobe when running with verbose |
| 1190 | * output or in dry-run mode. |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1191 | * |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1192 | * Insert a module in Linux kernel resolving dependencies, soft dependencies, |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1193 | * install commands and applying blacklist. |
| 1194 | * |
Lucas De Marchi | 7aed460 | 2012-01-31 12:05:36 -0200 | [diff] [blame] | 1195 | * If @run_install is NULL, this function will fork and exec by calling |
| 1196 | * system(3). Don't pass a NULL argument in @run_install if your binary is |
| 1197 | * setuid/setgid (see warning in system(3)). If you need control over the |
| 1198 | * execution of an install command, give a callback function instead. |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1199 | * |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1200 | * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on |
| 1201 | * failure. |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1202 | */ |
| 1203 | KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod, |
| 1204 | unsigned int flags, const char *extra_options, |
| 1205 | int (*run_install)(struct kmod_module *m, |
| 1206 | const char *cmd, void *data), |
Lucas De Marchi | 6bd0713 | 2012-01-30 17:02:06 -0200 | [diff] [blame] | 1207 | const void *data, |
| 1208 | void (*print_action)(struct kmod_module *m, |
| 1209 | bool install, |
| 1210 | const char *options)) |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1211 | { |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1212 | struct kmod_list *list = NULL, *l; |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1213 | struct probe_insert_cb cb; |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1214 | int err; |
| 1215 | |
| 1216 | if (mod == NULL) |
| 1217 | return -ENOENT; |
| 1218 | |
Lucas De Marchi | 269de2e | 2012-02-07 09:48:59 -0200 | [diff] [blame] | 1219 | if (!(flags & KMOD_PROBE_IGNORE_LOADED) |
| 1220 | && module_is_inkernel(mod)) { |
Lucas De Marchi | 814a57b | 2012-02-06 12:46:39 -0200 | [diff] [blame] | 1221 | if (flags & KMOD_PROBE_FAIL_ON_LOADED) |
Dave Reisner | af9572c | 2012-02-02 11:07:33 -0500 | [diff] [blame] | 1222 | return -EEXIST; |
| 1223 | else |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
Lucas De Marchi | 6882017 | 2012-08-17 09:38:05 -0300 | [diff] [blame] | 1227 | /* |
| 1228 | * Ugly assignement + check. We need to check if we were told to check |
| 1229 | * blacklist and also return the reason why we failed. |
| 1230 | * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the |
| 1231 | * module is an alias, so we also need to check it |
| 1232 | */ |
| 1233 | if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY))) |
| 1234 | || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) |
| 1235 | || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) { |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1236 | if (module_is_blacklisted(mod)) |
| 1237 | return err; |
| 1238 | } |
| 1239 | |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1240 | err = kmod_module_get_probe_list(mod, |
| 1241 | !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list); |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1242 | if (err < 0) |
| 1243 | return err; |
| 1244 | |
| 1245 | if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) { |
| 1246 | struct kmod_list *filtered = NULL; |
| 1247 | |
Dave Reisner | d80b103 | 2012-02-24 10:05:11 -0500 | [diff] [blame] | 1248 | err = kmod_module_apply_filter(mod->ctx, |
| 1249 | KMOD_FILTER_BLACKLIST, list, &filtered); |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1250 | if (err < 0) |
| 1251 | return err; |
| 1252 | |
| 1253 | kmod_module_unref_list(list); |
| 1254 | if (filtered == NULL) |
| 1255 | return KMOD_PROBE_APPLY_BLACKLIST_ALL; |
| 1256 | |
| 1257 | list = filtered; |
| 1258 | } |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1259 | |
| 1260 | cb.run_install = run_install; |
| 1261 | cb.data = (void *) data; |
| 1262 | |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1263 | kmod_list_foreach(l, list) { |
| 1264 | struct kmod_module *m = l->data; |
| 1265 | const char *moptions = kmod_module_get_options(m); |
| 1266 | const char *cmd = kmod_module_get_install_commands(m); |
Lucas De Marchi | abd5557 | 2012-02-19 04:20:30 -0200 | [diff] [blame] | 1267 | char *options; |
| 1268 | |
| 1269 | if (!(flags & KMOD_PROBE_IGNORE_LOADED) |
| 1270 | && module_is_inkernel(m)) { |
| 1271 | DBG(mod->ctx, "Ignoring module '%s': already loaded\n", |
| 1272 | m->name); |
| 1273 | err = -EEXIST; |
| 1274 | goto finish_module; |
| 1275 | } |
| 1276 | |
| 1277 | options = module_options_concat(moptions, |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1278 | m == mod ? extra_options : NULL); |
| 1279 | |
Lucas De Marchi | 89e9248 | 2012-01-29 02:35:46 -0200 | [diff] [blame] | 1280 | if (cmd != NULL && !m->ignorecmd) { |
Lucas De Marchi | 6bd0713 | 2012-01-30 17:02:06 -0200 | [diff] [blame] | 1281 | if (print_action != NULL) |
| 1282 | print_action(m, true, options ?: ""); |
| 1283 | |
Lucas De Marchi | 4c1ffb7 | 2012-01-30 19:00:58 -0200 | [diff] [blame] | 1284 | if (!(flags & KMOD_PROBE_DRY_RUN)) |
| 1285 | err = module_do_install_commands(m, options, |
| 1286 | &cb); |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1287 | } else { |
Lucas De Marchi | 6bd0713 | 2012-01-30 17:02:06 -0200 | [diff] [blame] | 1288 | if (print_action != NULL) |
| 1289 | print_action(m, false, options ?: ""); |
| 1290 | |
Lucas De Marchi | 4c1ffb7 | 2012-01-30 19:00:58 -0200 | [diff] [blame] | 1291 | if (!(flags & KMOD_PROBE_DRY_RUN)) |
| 1292 | err = kmod_module_insert_module(m, flags, |
| 1293 | options); |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | free(options); |
| 1297 | |
Lucas De Marchi | abd5557 | 2012-02-19 04:20:30 -0200 | [diff] [blame] | 1298 | finish_module: |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1299 | /* |
Lucas De Marchi | 5f35147 | 2012-01-30 16:26:52 -0200 | [diff] [blame] | 1300 | * Treat "already loaded" error. If we were told to stop on |
Lucas De Marchi | 3bc92e8 | 2012-01-31 11:29:06 -0200 | [diff] [blame] | 1301 | * already loaded and the module being loaded is not a softdep |
| 1302 | * or dep, bail out. Otherwise, just ignore and continue. |
Lucas De Marchi | 5f35147 | 2012-01-30 16:26:52 -0200 | [diff] [blame] | 1303 | * |
| 1304 | * We need to check here because of race conditions. We |
| 1305 | * checked first if module was already loaded but it may have |
| 1306 | * been loaded between the check and the moment we try to |
| 1307 | * insert it. |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1308 | */ |
Lucas De Marchi | 5f35147 | 2012-01-30 16:26:52 -0200 | [diff] [blame] | 1309 | if (err == -EEXIST && m == mod && |
Lucas De Marchi | 814a57b | 2012-02-06 12:46:39 -0200 | [diff] [blame] | 1310 | (flags & KMOD_PROBE_FAIL_ON_LOADED)) |
Lucas De Marchi | 5f35147 | 2012-01-30 16:26:52 -0200 | [diff] [blame] | 1311 | break; |
Lucas De Marchi | 5f35147 | 2012-01-30 16:26:52 -0200 | [diff] [blame] | 1312 | |
Lucas De Marchi | 3bc92e8 | 2012-01-31 11:29:06 -0200 | [diff] [blame] | 1313 | if (err == -EEXIST) |
| 1314 | err = 0; |
| 1315 | else if (err < 0) |
Lucas De Marchi | b1a5125 | 2012-01-29 01:49:09 -0200 | [diff] [blame] | 1316 | break; |
| 1317 | } |
| 1318 | |
| 1319 | kmod_module_unref_list(list); |
| 1320 | return err; |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1321 | } |
| 1322 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1323 | /** |
| 1324 | * kmod_module_get_options: |
| 1325 | * @mod: kmod module |
| 1326 | * |
| 1327 | * Get options of this kmod module. Options come from the configuration file |
| 1328 | * and are cached in @mod. The first call to this function will search for |
| 1329 | * this module in configuration and subsequent calls return the cached string. |
| 1330 | * |
| 1331 | * Returns: a string with all the options separated by spaces. This string is |
| 1332 | * owned by @mod, do not free it. |
| 1333 | */ |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1334 | KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod) |
| 1335 | { |
| 1336 | if (mod == NULL) |
| 1337 | return NULL; |
| 1338 | |
| 1339 | if (!mod->init.options) { |
| 1340 | /* lazy init */ |
| 1341 | struct kmod_module *m = (struct kmod_module *)mod; |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1342 | const struct kmod_list *l; |
| 1343 | const struct kmod_config *config; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1344 | char *opts = NULL; |
| 1345 | size_t optslen = 0; |
| 1346 | |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1347 | config = kmod_get_config(mod->ctx); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1348 | |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1349 | kmod_list_foreach(l, config->options) { |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1350 | const char *modname = kmod_option_get_modname(l); |
| 1351 | const char *str; |
| 1352 | size_t len; |
| 1353 | void *tmp; |
| 1354 | |
Lucas De Marchi | 07b8c82 | 2011-12-13 14:21:24 -0200 | [diff] [blame] | 1355 | DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias); |
| 1356 | if (!(streq(modname, mod->name) || (mod->alias != NULL && |
| 1357 | streq(modname, mod->alias)))) |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1358 | continue; |
| 1359 | |
Lucas De Marchi | 07b8c82 | 2011-12-13 14:21:24 -0200 | [diff] [blame] | 1360 | DBG(mod->ctx, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1361 | str = kmod_option_get_options(l); |
| 1362 | len = strlen(str); |
| 1363 | if (len < 1) |
| 1364 | continue; |
| 1365 | |
| 1366 | tmp = realloc(opts, optslen + len + 2); |
| 1367 | if (tmp == NULL) { |
| 1368 | free(opts); |
| 1369 | goto failed; |
| 1370 | } |
| 1371 | |
| 1372 | opts = tmp; |
| 1373 | |
| 1374 | if (optslen > 0) { |
| 1375 | opts[optslen] = ' '; |
| 1376 | optslen++; |
| 1377 | } |
| 1378 | |
| 1379 | memcpy(opts + optslen, str, len); |
| 1380 | optslen += len; |
| 1381 | opts[optslen] = '\0'; |
| 1382 | } |
| 1383 | |
| 1384 | m->init.options = true; |
| 1385 | m->options = opts; |
| 1386 | } |
| 1387 | |
| 1388 | return mod->options; |
| 1389 | |
| 1390 | failed: |
| 1391 | ERR(mod->ctx, "out of memory\n"); |
| 1392 | return NULL; |
| 1393 | } |
| 1394 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1395 | /** |
| 1396 | * kmod_module_get_install_commands: |
| 1397 | * @mod: kmod module |
| 1398 | * |
| 1399 | * Get install commands for this kmod module. Install commands come from the |
| 1400 | * configuration file and are cached in @mod. The first call to this function |
| 1401 | * will search for this module in configuration and subsequent calls return |
| 1402 | * the cached string. The install commands are returned as they were in the |
| 1403 | * configuration, concatenated by ';'. No other processing is made in this |
| 1404 | * string. |
| 1405 | * |
| 1406 | * Returns: a string with all install commands separated by semicolons. This |
| 1407 | * string is owned by @mod, do not free it. |
| 1408 | */ |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1409 | KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod) |
| 1410 | { |
| 1411 | if (mod == NULL) |
| 1412 | return NULL; |
| 1413 | |
| 1414 | if (!mod->init.install_commands) { |
| 1415 | /* lazy init */ |
| 1416 | struct kmod_module *m = (struct kmod_module *)mod; |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1417 | const struct kmod_list *l; |
| 1418 | const struct kmod_config *config; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1419 | |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1420 | config = kmod_get_config(mod->ctx); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1421 | |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1422 | kmod_list_foreach(l, config->install_commands) { |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1423 | const char *modname = kmod_command_get_modname(l); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1424 | |
Gustavo Sverzut Barbieri | a6bf249 | 2011-12-16 22:43:04 -0200 | [diff] [blame] | 1425 | if (fnmatch(modname, mod->name, 0) != 0) |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1426 | continue; |
| 1427 | |
Lucas De Marchi | 60f6760 | 2011-12-16 03:33:26 -0200 | [diff] [blame] | 1428 | m->install_commands = kmod_command_get_command(l); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1429 | |
Lucas De Marchi | 60f6760 | 2011-12-16 03:33:26 -0200 | [diff] [blame] | 1430 | /* |
| 1431 | * find only the first command, as modprobe from |
| 1432 | * module-init-tools does |
| 1433 | */ |
| 1434 | break; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | m->init.install_commands = true; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | return mod->install_commands; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1441 | } |
| 1442 | |
Lucas De Marchi | f4fc552 | 2011-12-16 03:57:12 -0200 | [diff] [blame] | 1443 | void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd) |
| 1444 | { |
| 1445 | mod->init.install_commands = true; |
| 1446 | mod->install_commands = cmd; |
| 1447 | } |
| 1448 | |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 1449 | static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count) |
| 1450 | { |
| 1451 | struct kmod_list *ret = NULL; |
| 1452 | unsigned i; |
| 1453 | |
| 1454 | for (i = 0; i < count; i++) { |
| 1455 | const char *depname = array[i]; |
| 1456 | struct kmod_list *lst = NULL; |
| 1457 | int err; |
| 1458 | |
| 1459 | err = kmod_module_new_from_lookup(ctx, depname, &lst); |
| 1460 | if (err < 0) { |
| 1461 | ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname); |
| 1462 | continue; |
| 1463 | } else if (lst != NULL) |
| 1464 | ret = kmod_list_append_list(ret, lst); |
| 1465 | } |
| 1466 | return ret; |
| 1467 | } |
| 1468 | |
| 1469 | /** |
| 1470 | * kmod_module_get_softdeps: |
| 1471 | * @mod: kmod module |
| 1472 | * @pre: where to save the list of preceding soft dependencies. |
| 1473 | * @post: where to save the list of post soft dependencies. |
| 1474 | * |
| 1475 | * Get soft dependencies for this kmod module. Soft dependencies come |
Lucas De Marchi | 2bd7cbf | 2011-12-27 10:15:40 -0200 | [diff] [blame] | 1476 | * from configuration file and are not cached in @mod because it may include |
| 1477 | * dependency cycles that would make we leak kmod_module. Any call |
| 1478 | * to this function will search for this module in configuration, allocate a |
| 1479 | * list and return the result. |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 1480 | * |
| 1481 | * Both @pre and @post are newly created list of kmod_module and |
| 1482 | * should be unreferenced with kmod_module_unref_list(). |
| 1483 | * |
| 1484 | * Returns: 0 on success or < 0 otherwise. |
| 1485 | */ |
Lucas De Marchi | 2bd7cbf | 2011-12-27 10:15:40 -0200 | [diff] [blame] | 1486 | KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod, |
| 1487 | struct kmod_list **pre, |
| 1488 | struct kmod_list **post) |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 1489 | { |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1490 | const struct kmod_list *l; |
| 1491 | const struct kmod_config *config; |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 1492 | |
| 1493 | if (mod == NULL || pre == NULL || post == NULL) |
| 1494 | return -ENOENT; |
| 1495 | |
| 1496 | assert(*pre == NULL); |
| 1497 | assert(*post == NULL); |
| 1498 | |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1499 | config = kmod_get_config(mod->ctx); |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 1500 | |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1501 | kmod_list_foreach(l, config->softdeps) { |
Lucas De Marchi | 2bd7cbf | 2011-12-27 10:15:40 -0200 | [diff] [blame] | 1502 | const char *modname = kmod_softdep_get_name(l); |
| 1503 | const char * const *array; |
| 1504 | unsigned count; |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 1505 | |
Lucas De Marchi | 2bd7cbf | 2011-12-27 10:15:40 -0200 | [diff] [blame] | 1506 | if (fnmatch(modname, mod->name, 0) != 0) |
| 1507 | continue; |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 1508 | |
Lucas De Marchi | 2bd7cbf | 2011-12-27 10:15:40 -0200 | [diff] [blame] | 1509 | array = kmod_softdep_get_pre(l, &count); |
| 1510 | *pre = lookup_softdep(mod->ctx, array, count); |
| 1511 | array = kmod_softdep_get_post(l, &count); |
| 1512 | *post = lookup_softdep(mod->ctx, array, count); |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 1513 | |
Lucas De Marchi | 2bd7cbf | 2011-12-27 10:15:40 -0200 | [diff] [blame] | 1514 | /* |
| 1515 | * find only the first command, as modprobe from |
| 1516 | * module-init-tools does |
| 1517 | */ |
| 1518 | break; |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | return 0; |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 1522 | } |
| 1523 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1524 | /** |
| 1525 | * kmod_module_get_remove_commands: |
| 1526 | * @mod: kmod module |
| 1527 | * |
| 1528 | * Get remove commands for this kmod module. Remove commands come from the |
| 1529 | * configuration file and are cached in @mod. The first call to this function |
| 1530 | * will search for this module in configuration and subsequent calls return |
| 1531 | * the cached string. The remove commands are returned as they were in the |
| 1532 | * configuration, concatenated by ';'. No other processing is made in this |
| 1533 | * string. |
| 1534 | * |
| 1535 | * Returns: a string with all remove commands separated by semicolons. This |
| 1536 | * string is owned by @mod, do not free it. |
| 1537 | */ |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1538 | KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod) |
| 1539 | { |
| 1540 | if (mod == NULL) |
| 1541 | return NULL; |
| 1542 | |
| 1543 | if (!mod->init.remove_commands) { |
| 1544 | /* lazy init */ |
| 1545 | struct kmod_module *m = (struct kmod_module *)mod; |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1546 | const struct kmod_list *l; |
| 1547 | const struct kmod_config *config; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1548 | |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1549 | config = kmod_get_config(mod->ctx); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1550 | |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 1551 | kmod_list_foreach(l, config->remove_commands) { |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1552 | const char *modname = kmod_command_get_modname(l); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1553 | |
Gustavo Sverzut Barbieri | a6bf249 | 2011-12-16 22:43:04 -0200 | [diff] [blame] | 1554 | if (fnmatch(modname, mod->name, 0) != 0) |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1555 | continue; |
| 1556 | |
Lucas De Marchi | 60f6760 | 2011-12-16 03:33:26 -0200 | [diff] [blame] | 1557 | m->remove_commands = kmod_command_get_command(l); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1558 | |
Lucas De Marchi | 60f6760 | 2011-12-16 03:33:26 -0200 | [diff] [blame] | 1559 | /* |
| 1560 | * find only the first command, as modprobe from |
| 1561 | * module-init-tools does |
| 1562 | */ |
| 1563 | break; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | m->init.remove_commands = true; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | return mod->remove_commands; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1570 | } |
| 1571 | |
Lucas De Marchi | f4fc552 | 2011-12-16 03:57:12 -0200 | [diff] [blame] | 1572 | void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd) |
| 1573 | { |
| 1574 | mod->init.remove_commands = true; |
| 1575 | mod->remove_commands = cmd; |
| 1576 | } |
| 1577 | |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1578 | /** |
| 1579 | * SECTION:libkmod-loaded |
| 1580 | * @short_description: currently loaded modules |
| 1581 | * |
| 1582 | * Information about currently loaded modules, as reported by Linux kernel. |
| 1583 | * These information are not cached by libkmod and are always read from /sys |
| 1584 | * and /proc/modules. |
| 1585 | */ |
| 1586 | |
| 1587 | /** |
| 1588 | * kmod_module_new_from_loaded: |
| 1589 | * @ctx: kmod library context |
| 1590 | * @list: where to save the list of loaded modules |
| 1591 | * |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1592 | * Create a new list of kmod modules with all modules currently loaded in |
| 1593 | * kernel. It uses /proc/modules to get the names of loaded modules and to |
| 1594 | * create kmod modules by calling kmod_module_new_from_name() in each of them. |
Chengwei Yang | 491c490 | 2013-05-04 17:07:02 +0800 | [diff] [blame] | 1595 | * They are put in @list in no particular order. |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1596 | * |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1597 | * The initial refcount is 1, and needs to be decremented to release the |
| 1598 | * resources of the kmod_module. The returned @list must be released by |
| 1599 | * calling kmod_module_unref_list(). Since libkmod keeps track of all |
| 1600 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 1601 | * not unref @ctx before all the desired operations with the returned list are |
| 1602 | * completed. |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1603 | * |
| 1604 | * Returns: 0 on success or < 0 on error. |
| 1605 | */ |
| 1606 | KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx, |
| 1607 | struct kmod_list **list) |
| 1608 | { |
| 1609 | struct kmod_list *l = NULL; |
| 1610 | FILE *fp; |
| 1611 | char line[4096]; |
| 1612 | |
| 1613 | if (ctx == NULL || list == NULL) |
| 1614 | return -ENOENT; |
| 1615 | |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 1616 | fp = fopen("/proc/modules", "re"); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1617 | if (fp == NULL) { |
| 1618 | int err = -errno; |
| 1619 | ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno)); |
| 1620 | return err; |
| 1621 | } |
| 1622 | |
| 1623 | while (fgets(line, sizeof(line), fp)) { |
| 1624 | struct kmod_module *m; |
| 1625 | struct kmod_list *node; |
| 1626 | int err; |
| 1627 | char *saveptr, *name = strtok_r(line, " \t", &saveptr); |
| 1628 | |
| 1629 | err = kmod_module_new_from_name(ctx, name, &m); |
| 1630 | if (err < 0) { |
| 1631 | ERR(ctx, "could not get module from name '%s': %s\n", |
| 1632 | name, strerror(-err)); |
| 1633 | continue; |
| 1634 | } |
| 1635 | |
| 1636 | node = kmod_list_append(l, m); |
| 1637 | if (node) |
| 1638 | l = node; |
| 1639 | else { |
| 1640 | ERR(ctx, "out of memory\n"); |
| 1641 | kmod_module_unref(m); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | fclose(fp); |
| 1646 | *list = l; |
| 1647 | |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1651 | /** |
| 1652 | * kmod_module_initstate_str: |
| 1653 | * @state: the state as returned by kmod_module_get_initstate() |
| 1654 | * |
| 1655 | * Translate a initstate to a string. |
| 1656 | * |
| 1657 | * Returns: the string associated to the @state. This string is statically |
| 1658 | * allocated, do not free it. |
| 1659 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1660 | KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state) |
| 1661 | { |
Dave Reisner | 7bede7b | 2012-02-02 15:05:57 -0500 | [diff] [blame] | 1662 | switch (state) { |
| 1663 | case KMOD_MODULE_BUILTIN: |
| 1664 | return "builtin"; |
| 1665 | case KMOD_MODULE_LIVE: |
| 1666 | return "live"; |
| 1667 | case KMOD_MODULE_COMING: |
| 1668 | return "coming"; |
| 1669 | case KMOD_MODULE_GOING: |
| 1670 | return "going"; |
| 1671 | default: |
| 1672 | return NULL; |
| 1673 | } |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1674 | } |
| 1675 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1676 | /** |
| 1677 | * kmod_module_get_initstate: |
| 1678 | * @mod: kmod module |
| 1679 | * |
| 1680 | * Get the initstate of this @mod, as returned by Linux Kernel, by reading |
| 1681 | * /sys filesystem. |
| 1682 | * |
Chengwei Yang | d7152f6 | 2013-05-04 17:07:03 +0800 | [diff] [blame] | 1683 | * Returns: < 0 on error or module state if module is found in kernel, valid states are |
| 1684 | * KMOD_MODULE_BUILTIN: module is builtin; |
| 1685 | * KMOD_MODULE_LIVE: module is live in kernel; |
| 1686 | * KMOD_MODULE_COMING: module is being loaded; |
| 1687 | * KMOD_MODULE_GOING: module is being unloaded. |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1688 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1689 | KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod) |
| 1690 | { |
| 1691 | char path[PATH_MAX], buf[32]; |
| 1692 | int fd, err, pathlen; |
| 1693 | |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 1694 | if (mod == NULL) |
| 1695 | return -ENOENT; |
| 1696 | |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 1697 | if (mod->builtin) |
| 1698 | return KMOD_MODULE_BUILTIN; |
| 1699 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1700 | pathlen = snprintf(path, sizeof(path), |
| 1701 | "/sys/module/%s/initstate", mod->name); |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 1702 | fd = open(path, O_RDONLY|O_CLOEXEC); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1703 | if (fd < 0) { |
| 1704 | err = -errno; |
| 1705 | |
Lucas De Marchi | ddbda02 | 2011-12-27 11:40:10 -0200 | [diff] [blame] | 1706 | DBG(mod->ctx, "could not open '%s': %s\n", |
| 1707 | path, strerror(-err)); |
| 1708 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1709 | if (pathlen > (int)sizeof("/initstate") - 1) { |
| 1710 | struct stat st; |
| 1711 | path[pathlen - (sizeof("/initstate") - 1)] = '\0'; |
| 1712 | if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) |
| 1713 | return KMOD_MODULE_BUILTIN; |
| 1714 | } |
| 1715 | |
Gustavo Sverzut Barbieri | 926f67a | 2011-12-11 19:33:03 -0200 | [diff] [blame] | 1716 | DBG(mod->ctx, "could not open '%s': %s\n", |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1717 | path, strerror(-err)); |
| 1718 | return err; |
| 1719 | } |
| 1720 | |
| 1721 | err = read_str_safe(fd, buf, sizeof(buf)); |
| 1722 | close(fd); |
| 1723 | if (err < 0) { |
| 1724 | ERR(mod->ctx, "could not read from '%s': %s\n", |
| 1725 | path, strerror(-err)); |
| 1726 | return err; |
| 1727 | } |
| 1728 | |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 1729 | if (streq(buf, "live\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1730 | return KMOD_MODULE_LIVE; |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 1731 | else if (streq(buf, "coming\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1732 | return KMOD_MODULE_COMING; |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 1733 | else if (streq(buf, "going\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1734 | return KMOD_MODULE_GOING; |
| 1735 | |
| 1736 | ERR(mod->ctx, "unknown %s: '%s'\n", path, buf); |
| 1737 | return -EINVAL; |
| 1738 | } |
| 1739 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1740 | /** |
Lucas De Marchi | 2d7bab5 | 2011-12-14 12:10:02 -0200 | [diff] [blame] | 1741 | * kmod_module_get_size: |
| 1742 | * @mod: kmod module |
| 1743 | * |
Dave Reisner | 486f901 | 2012-06-28 09:09:48 -0400 | [diff] [blame] | 1744 | * Get the size of this kmod module as returned by Linux kernel. If supported, |
| 1745 | * the size is read from the coresize attribute in /sys/module. For older |
| 1746 | * kernels, this falls back on /proc/modules and searches for the specified |
| 1747 | * module to get its size. |
Lucas De Marchi | 2d7bab5 | 2011-12-14 12:10:02 -0200 | [diff] [blame] | 1748 | * |
| 1749 | * Returns: the size of this kmod module. |
| 1750 | */ |
| 1751 | KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod) |
| 1752 | { |
Lucas De Marchi | 2d7bab5 | 2011-12-14 12:10:02 -0200 | [diff] [blame] | 1753 | FILE *fp; |
| 1754 | char line[4096]; |
| 1755 | int lineno = 0; |
| 1756 | long size = -ENOENT; |
Dave Reisner | 486f901 | 2012-06-28 09:09:48 -0400 | [diff] [blame] | 1757 | int dfd, cfd; |
Lucas De Marchi | 2d7bab5 | 2011-12-14 12:10:02 -0200 | [diff] [blame] | 1758 | |
| 1759 | if (mod == NULL) |
| 1760 | return -ENOENT; |
| 1761 | |
Dave Reisner | 486f901 | 2012-06-28 09:09:48 -0400 | [diff] [blame] | 1762 | /* try to open the module dir in /sys. If this fails, don't |
| 1763 | * bother trying to find the size as we know the module isn't |
| 1764 | * loaded. |
| 1765 | */ |
| 1766 | snprintf(line, sizeof(line), "/sys/module/%s", mod->name); |
| 1767 | dfd = open(line, O_RDONLY); |
| 1768 | if (dfd < 0) |
| 1769 | return -errno; |
| 1770 | |
| 1771 | /* available as of linux 3.3.x */ |
| 1772 | cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC); |
| 1773 | if (cfd >= 0) { |
| 1774 | if (read_str_long(cfd, &size, 10) < 0) |
| 1775 | ERR(mod->ctx, "failed to read coresize from %s\n", line); |
| 1776 | close(cfd); |
| 1777 | goto done; |
| 1778 | } |
| 1779 | |
| 1780 | /* fall back on parsing /proc/modules */ |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 1781 | fp = fopen("/proc/modules", "re"); |
Lucas De Marchi | 2d7bab5 | 2011-12-14 12:10:02 -0200 | [diff] [blame] | 1782 | if (fp == NULL) { |
| 1783 | int err = -errno; |
| 1784 | ERR(mod->ctx, |
| 1785 | "could not open /proc/modules: %s\n", strerror(errno)); |
| 1786 | return err; |
| 1787 | } |
| 1788 | |
| 1789 | while (fgets(line, sizeof(line), fp)) { |
| 1790 | char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr); |
| 1791 | long value; |
| 1792 | |
| 1793 | lineno++; |
| 1794 | if (tok == NULL || !streq(tok, mod->name)) |
| 1795 | continue; |
| 1796 | |
| 1797 | tok = strtok_r(NULL, " \t", &saveptr); |
| 1798 | if (tok == NULL) { |
| 1799 | ERR(mod->ctx, |
| 1800 | "invalid line format at /proc/modules:%d\n", lineno); |
| 1801 | break; |
| 1802 | } |
| 1803 | |
| 1804 | value = strtol(tok, &endptr, 10); |
| 1805 | if (endptr == tok || *endptr != '\0') { |
| 1806 | ERR(mod->ctx, |
| 1807 | "invalid line format at /proc/modules:%d\n", lineno); |
| 1808 | break; |
| 1809 | } |
| 1810 | |
| 1811 | size = value; |
| 1812 | break; |
| 1813 | } |
| 1814 | fclose(fp); |
Dave Reisner | 486f901 | 2012-06-28 09:09:48 -0400 | [diff] [blame] | 1815 | |
| 1816 | done: |
| 1817 | close(dfd); |
Lucas De Marchi | 2d7bab5 | 2011-12-14 12:10:02 -0200 | [diff] [blame] | 1818 | return size; |
| 1819 | } |
| 1820 | |
| 1821 | /** |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1822 | * kmod_module_get_refcnt: |
| 1823 | * @mod: kmod module |
| 1824 | * |
| 1825 | * Get the ref count of this @mod, as returned by Linux Kernel, by reading |
| 1826 | * /sys filesystem. |
| 1827 | * |
| 1828 | * Returns: 0 on success or < 0 on failure. |
| 1829 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1830 | KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) |
| 1831 | { |
| 1832 | char path[PATH_MAX]; |
| 1833 | long refcnt; |
| 1834 | int fd, err; |
| 1835 | |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 1836 | if (mod == NULL) |
| 1837 | return -ENOENT; |
| 1838 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1839 | snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name); |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 1840 | fd = open(path, O_RDONLY|O_CLOEXEC); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1841 | if (fd < 0) { |
| 1842 | err = -errno; |
Lucas De Marchi | 9c5f057 | 2012-03-01 14:04:29 -0300 | [diff] [blame] | 1843 | DBG(mod->ctx, "could not open '%s': %s\n", |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1844 | path, strerror(errno)); |
| 1845 | return err; |
| 1846 | } |
| 1847 | |
| 1848 | err = read_str_long(fd, &refcnt, 10); |
| 1849 | close(fd); |
| 1850 | if (err < 0) { |
| 1851 | ERR(mod->ctx, "could not read integer from '%s': '%s'\n", |
| 1852 | path, strerror(-err)); |
| 1853 | return err; |
| 1854 | } |
| 1855 | |
| 1856 | return (int)refcnt; |
| 1857 | } |
| 1858 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1859 | /** |
| 1860 | * kmod_module_get_holders: |
| 1861 | * @mod: kmod module |
| 1862 | * |
| 1863 | * Get a list of kmod modules that are holding this @mod, as returned by Linux |
| 1864 | * Kernel. After use, free the @list by calling kmod_module_unref_list(). |
| 1865 | * |
| 1866 | * Returns: a new list of kmod modules on success or NULL on failure. |
| 1867 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1868 | KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod) |
| 1869 | { |
| 1870 | char dname[PATH_MAX]; |
| 1871 | struct kmod_list *list = NULL; |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1872 | struct dirent *dent; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1873 | DIR *d; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1874 | |
Lucas De Marchi | b9a7da3 | 2013-04-23 20:47:28 -0300 | [diff] [blame] | 1875 | if (mod == NULL || mod->ctx == NULL) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1876 | return NULL; |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 1877 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1878 | snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name); |
| 1879 | |
| 1880 | d = opendir(dname); |
| 1881 | if (d == NULL) { |
| 1882 | ERR(mod->ctx, "could not open '%s': %s\n", |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1883 | dname, strerror(errno)); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1884 | return NULL; |
| 1885 | } |
| 1886 | |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1887 | for (dent = readdir(d); dent != NULL; dent = readdir(d)) { |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1888 | struct kmod_module *holder; |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1889 | struct kmod_list *l; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1890 | int err; |
| 1891 | |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1892 | if (dent->d_name[0] == '.') { |
| 1893 | if (dent->d_name[1] == '\0' || |
| 1894 | (dent->d_name[1] == '.' && dent->d_name[2] == '\0')) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1895 | continue; |
| 1896 | } |
| 1897 | |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1898 | err = kmod_module_new_from_name(mod->ctx, dent->d_name, |
| 1899 | &holder); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1900 | if (err < 0) { |
| 1901 | ERR(mod->ctx, "could not create module for '%s': %s\n", |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1902 | dent->d_name, strerror(-err)); |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1903 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1904 | } |
| 1905 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1906 | l = kmod_list_append(list, holder); |
| 1907 | if (l != NULL) { |
| 1908 | list = l; |
| 1909 | } else { |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1910 | ERR(mod->ctx, "out of memory\n"); |
| 1911 | kmod_module_unref(holder); |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1912 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | closedir(d); |
| 1917 | return list; |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1918 | |
| 1919 | fail: |
| 1920 | closedir(d); |
| 1921 | kmod_module_unref_list(list); |
| 1922 | return NULL; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | struct kmod_module_section { |
| 1926 | unsigned long address; |
| 1927 | char name[]; |
| 1928 | }; |
| 1929 | |
| 1930 | static void kmod_module_section_free(struct kmod_module_section *section) |
| 1931 | { |
| 1932 | free(section); |
| 1933 | } |
| 1934 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1935 | /** |
| 1936 | * kmod_module_get_sections: |
| 1937 | * @mod: kmod module |
| 1938 | * |
| 1939 | * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The |
| 1940 | * structure contained in this list is internal to libkmod and their fields |
| 1941 | * can be obtained by calling kmod_module_section_get_name() and |
| 1942 | * kmod_module_section_get_address(). |
| 1943 | * |
| 1944 | * After use, free the @list by calling kmod_module_section_free_list(). |
| 1945 | * |
| 1946 | * Returns: a new list of kmod module sections on success or NULL on failure. |
| 1947 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1948 | KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod) |
| 1949 | { |
| 1950 | char dname[PATH_MAX]; |
| 1951 | struct kmod_list *list = NULL; |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1952 | struct dirent *dent; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1953 | DIR *d; |
| 1954 | int dfd; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1955 | |
| 1956 | if (mod == NULL) |
| 1957 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1958 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1959 | snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name); |
| 1960 | |
| 1961 | d = opendir(dname); |
| 1962 | if (d == NULL) { |
| 1963 | ERR(mod->ctx, "could not open '%s': %s\n", |
| 1964 | dname, strerror(errno)); |
| 1965 | return NULL; |
| 1966 | } |
| 1967 | |
| 1968 | dfd = dirfd(d); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1969 | |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1970 | for (dent = readdir(d); dent; dent = readdir(d)) { |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1971 | struct kmod_module_section *section; |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1972 | struct kmod_list *l; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1973 | unsigned long address; |
| 1974 | size_t namesz; |
| 1975 | int fd, err; |
| 1976 | |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1977 | if (dent->d_name[0] == '.') { |
| 1978 | if (dent->d_name[1] == '\0' || |
| 1979 | (dent->d_name[1] == '.' && dent->d_name[2] == '\0')) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1980 | continue; |
| 1981 | } |
| 1982 | |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1983 | fd = openat(dfd, dent->d_name, O_RDONLY|O_CLOEXEC); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1984 | if (fd < 0) { |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1985 | ERR(mod->ctx, "could not open '%s/%s': %m\n", |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1986 | dname, dent->d_name); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1987 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | err = read_str_ulong(fd, &address, 16); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1991 | close(fd); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1992 | if (err < 0) { |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1993 | ERR(mod->ctx, "could not read long from '%s/%s': %m\n", |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1994 | dname, dent->d_name); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1995 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1996 | } |
| 1997 | |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 1998 | namesz = strlen(dent->d_name) + 1; |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1999 | section = malloc(sizeof(*section) + namesz); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2000 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 2001 | if (section == NULL) { |
| 2002 | ERR(mod->ctx, "out of memory\n"); |
| 2003 | goto fail; |
| 2004 | } |
| 2005 | |
| 2006 | section->address = address; |
Lucas De Marchi | 7e0385c | 2013-08-27 23:29:47 -0300 | [diff] [blame^] | 2007 | memcpy(section->name, dent->d_name, namesz); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 2008 | |
| 2009 | l = kmod_list_append(list, section); |
| 2010 | if (l != NULL) { |
| 2011 | list = l; |
| 2012 | } else { |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2013 | ERR(mod->ctx, "out of memory\n"); |
| 2014 | free(section); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 2015 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2016 | } |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | closedir(d); |
| 2020 | return list; |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 2021 | |
| 2022 | fail: |
| 2023 | closedir(d); |
| 2024 | kmod_module_unref_list(list); |
| 2025 | return NULL; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2026 | } |
| 2027 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 2028 | /** |
| 2029 | * kmod_module_section_get_module_name: |
| 2030 | * @entry: a list entry representing a kmod module section |
| 2031 | * |
| 2032 | * Get the name of a kmod module section. |
| 2033 | * |
| 2034 | * After use, free the @list by calling kmod_module_section_free_list(). |
| 2035 | * |
| 2036 | * Returns: the name of this kmod module section on success or NULL on |
| 2037 | * failure. The string is owned by the section, do not free it. |
| 2038 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2039 | KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry) |
| 2040 | { |
| 2041 | struct kmod_module_section *section; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 2042 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2043 | if (entry == NULL) |
| 2044 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 2045 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2046 | section = entry->data; |
| 2047 | return section->name; |
| 2048 | } |
| 2049 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 2050 | /** |
| 2051 | * kmod_module_section_get_address: |
| 2052 | * @entry: a list entry representing a kmod module section |
| 2053 | * |
| 2054 | * Get the address of a kmod module section. |
| 2055 | * |
| 2056 | * After use, free the @list by calling kmod_module_section_free_list(). |
| 2057 | * |
| 2058 | * Returns: the address of this kmod module section on success or ULONG_MAX |
| 2059 | * on failure. |
| 2060 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2061 | KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry) |
| 2062 | { |
| 2063 | struct kmod_module_section *section; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 2064 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2065 | if (entry == NULL) |
| 2066 | return (unsigned long)-1; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 2067 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2068 | section = entry->data; |
| 2069 | return section->address; |
| 2070 | } |
| 2071 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 2072 | /** |
| 2073 | * kmod_module_section_free_list: |
| 2074 | * @list: kmod module section list |
| 2075 | * |
| 2076 | * Release the resources taken by @list |
| 2077 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 2078 | KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list) |
| 2079 | { |
| 2080 | while (list) { |
| 2081 | kmod_module_section_free(list->data); |
| 2082 | list = kmod_list_remove(list); |
| 2083 | } |
| 2084 | } |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2085 | |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 2086 | static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod) |
| 2087 | { |
| 2088 | if (mod->file == NULL) { |
| 2089 | const char *path = kmod_module_get_path(mod); |
| 2090 | |
| 2091 | if (path == NULL) { |
| 2092 | errno = ENOENT; |
| 2093 | return NULL; |
| 2094 | } |
| 2095 | |
| 2096 | ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx, |
| 2097 | path); |
| 2098 | if (mod->file == NULL) |
| 2099 | return NULL; |
| 2100 | } |
| 2101 | |
| 2102 | return kmod_file_get_elf(mod->file); |
| 2103 | } |
| 2104 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2105 | struct kmod_module_info { |
| 2106 | char *key; |
| 2107 | char value[]; |
| 2108 | }; |
| 2109 | |
| 2110 | static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen) |
| 2111 | { |
| 2112 | struct kmod_module_info *info; |
| 2113 | |
| 2114 | info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2); |
| 2115 | if (info == NULL) |
| 2116 | return NULL; |
| 2117 | |
| 2118 | info->key = (char *)info + sizeof(struct kmod_module_info) |
Lucas De Marchi | 818af4f | 2013-04-23 20:33:13 -0300 | [diff] [blame] | 2119 | + valuelen + 1; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2120 | memcpy(info->key, key, keylen); |
| 2121 | info->key[keylen] = '\0'; |
| 2122 | memcpy(info->value, value, valuelen); |
| 2123 | info->value[valuelen] = '\0'; |
| 2124 | return info; |
| 2125 | } |
| 2126 | |
| 2127 | static void kmod_module_info_free(struct kmod_module_info *info) |
| 2128 | { |
| 2129 | free(info); |
| 2130 | } |
| 2131 | |
Michal Marek | f64458c | 2013-01-16 10:18:17 +0100 | [diff] [blame] | 2132 | static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen) |
| 2133 | { |
| 2134 | struct kmod_module_info *info; |
| 2135 | struct kmod_list *n; |
| 2136 | |
| 2137 | info = kmod_module_info_new(key, keylen, value, valuelen); |
Michal Marek | 6333934 | 2013-01-16 17:51:57 +0100 | [diff] [blame] | 2138 | if (info == NULL) |
Michal Marek | f64458c | 2013-01-16 10:18:17 +0100 | [diff] [blame] | 2139 | return NULL; |
Michal Marek | f64458c | 2013-01-16 10:18:17 +0100 | [diff] [blame] | 2140 | n = kmod_list_append(*list, info); |
Michal Marek | 6333934 | 2013-01-16 17:51:57 +0100 | [diff] [blame] | 2141 | if (n != NULL) |
| 2142 | *list = n; |
| 2143 | else |
Michal Marek | f64458c | 2013-01-16 10:18:17 +0100 | [diff] [blame] | 2144 | kmod_module_info_free(info); |
Michal Marek | f64458c | 2013-01-16 10:18:17 +0100 | [diff] [blame] | 2145 | return n; |
| 2146 | } |
| 2147 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2148 | /** |
| 2149 | * kmod_module_get_info: |
| 2150 | * @mod: kmod module |
| 2151 | * @list: where to return list of module information. Use |
| 2152 | * kmod_module_info_get_key() and |
| 2153 | * kmod_module_info_get_value(). Release this list with |
Lucas De Marchi | db74cee | 2012-01-09 03:45:19 -0200 | [diff] [blame] | 2154 | * kmod_module_info_free_list() |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2155 | * |
| 2156 | * Get a list of entries in ELF section ".modinfo", these contain |
| 2157 | * alias, license, depends, vermagic and other keys with respective |
Michal Marek | 8fe1681 | 2013-01-16 09:52:01 +0100 | [diff] [blame] | 2158 | * values. If the module is signed (CONFIG_MODULE_SIG), information |
| 2159 | * about the module signature is included as well: signer, |
| 2160 | * sig_key and sig_hashalgo. |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2161 | * |
| 2162 | * After use, free the @list by calling kmod_module_info_free_list(). |
| 2163 | * |
| 2164 | * Returns: 0 on success or < 0 otherwise. |
| 2165 | */ |
| 2166 | KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list) |
| 2167 | { |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2168 | struct kmod_elf *elf; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2169 | char **strings; |
Michal Marek | f64458c | 2013-01-16 10:18:17 +0100 | [diff] [blame] | 2170 | int i, count, ret = -ENOMEM; |
Michal Marek | 8fe1681 | 2013-01-16 09:52:01 +0100 | [diff] [blame] | 2171 | struct kmod_signature_info sig_info; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2172 | |
| 2173 | if (mod == NULL || list == NULL) |
| 2174 | return -ENOENT; |
| 2175 | |
| 2176 | assert(*list == NULL); |
| 2177 | |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 2178 | elf = kmod_module_get_elf(mod); |
| 2179 | if (elf == NULL) |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2180 | return -errno; |
| 2181 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2182 | count = kmod_elf_get_strings(elf, ".modinfo", &strings); |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 2183 | if (count < 0) |
| 2184 | return count; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2185 | |
| 2186 | for (i = 0; i < count; i++) { |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2187 | struct kmod_list *n; |
| 2188 | const char *key, *value; |
| 2189 | size_t keylen, valuelen; |
| 2190 | |
| 2191 | key = strings[i]; |
| 2192 | value = strchr(key, '='); |
| 2193 | if (value == NULL) { |
| 2194 | keylen = strlen(key); |
| 2195 | valuelen = 0; |
Lucas De Marchi | 818af4f | 2013-04-23 20:33:13 -0300 | [diff] [blame] | 2196 | value = key; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2197 | } else { |
| 2198 | keylen = value - key; |
| 2199 | value++; |
| 2200 | valuelen = strlen(value); |
| 2201 | } |
| 2202 | |
Michal Marek | f64458c | 2013-01-16 10:18:17 +0100 | [diff] [blame] | 2203 | n = kmod_module_info_append(list, key, keylen, value, valuelen); |
| 2204 | if (n == NULL) |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2205 | goto list_error; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2206 | } |
Michal Marek | 8fe1681 | 2013-01-16 09:52:01 +0100 | [diff] [blame] | 2207 | |
| 2208 | if (kmod_module_signature_info(mod->file, &sig_info)) { |
| 2209 | struct kmod_list *n; |
| 2210 | char *key_hex; |
| 2211 | |
| 2212 | n = kmod_module_info_append(list, "signer", strlen("signer"), |
| 2213 | sig_info.signer, sig_info.signer_len); |
| 2214 | if (n == NULL) |
| 2215 | goto list_error; |
| 2216 | count++; |
| 2217 | |
| 2218 | /* Display the key id as 01:12:DE:AD:BE:EF:... */ |
| 2219 | key_hex = malloc(sig_info.key_id_len * 3); |
| 2220 | if (key_hex == NULL) |
| 2221 | goto list_error; |
| 2222 | for (i = 0; i < (int)sig_info.key_id_len; i++) { |
| 2223 | sprintf(key_hex + i * 3, "%02X", |
| 2224 | (unsigned char)sig_info.key_id[i]); |
| 2225 | if (i < (int)sig_info.key_id_len - 1) |
| 2226 | key_hex[i * 3 + 2] = ':'; |
| 2227 | } |
| 2228 | n = kmod_module_info_append(list, "sig_key", strlen("sig_key"), |
| 2229 | key_hex, sig_info.key_id_len * 3 - 1); |
| 2230 | free(key_hex); |
| 2231 | if (n == NULL) |
| 2232 | goto list_error; |
| 2233 | count++; |
| 2234 | |
| 2235 | n = kmod_module_info_append(list, |
| 2236 | "sig_hashalgo", strlen("sig_hashalgo"), |
| 2237 | sig_info.hash_algo, strlen(sig_info.hash_algo)); |
| 2238 | if (n == NULL) |
| 2239 | goto list_error; |
| 2240 | count++; |
| 2241 | |
| 2242 | /* |
| 2243 | * Omit sig_info.id_type and sig_info.algo for now, as these |
| 2244 | * are currently constant. |
| 2245 | */ |
| 2246 | } |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2247 | ret = count; |
| 2248 | |
| 2249 | list_error: |
Michal Marek | 6333934 | 2013-01-16 17:51:57 +0100 | [diff] [blame] | 2250 | if (ret < 0) { |
| 2251 | kmod_module_info_free_list(*list); |
| 2252 | *list = NULL; |
| 2253 | } |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2254 | free(strings); |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2255 | return ret; |
| 2256 | } |
| 2257 | |
| 2258 | /** |
| 2259 | * kmod_module_info_get_key: |
| 2260 | * @entry: a list entry representing a kmod module info |
| 2261 | * |
| 2262 | * Get the key of a kmod module info. |
| 2263 | * |
| 2264 | * Returns: the key of this kmod module info on success or NULL on |
| 2265 | * failure. The string is owned by the info, do not free it. |
| 2266 | */ |
| 2267 | KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry) |
| 2268 | { |
| 2269 | struct kmod_module_info *info; |
| 2270 | |
| 2271 | if (entry == NULL) |
| 2272 | return NULL; |
| 2273 | |
| 2274 | info = entry->data; |
| 2275 | return info->key; |
| 2276 | } |
| 2277 | |
| 2278 | /** |
| 2279 | * kmod_module_info_get_value: |
| 2280 | * @entry: a list entry representing a kmod module info |
| 2281 | * |
| 2282 | * Get the value of a kmod module info. |
| 2283 | * |
| 2284 | * Returns: the value of this kmod module info on success or NULL on |
| 2285 | * failure. The string is owned by the info, do not free it. |
| 2286 | */ |
| 2287 | KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry) |
| 2288 | { |
| 2289 | struct kmod_module_info *info; |
| 2290 | |
| 2291 | if (entry == NULL) |
| 2292 | return NULL; |
| 2293 | |
| 2294 | info = entry->data; |
| 2295 | return info->value; |
| 2296 | } |
| 2297 | |
| 2298 | /** |
| 2299 | * kmod_module_info_free_list: |
| 2300 | * @list: kmod module info list |
| 2301 | * |
| 2302 | * Release the resources taken by @list |
| 2303 | */ |
| 2304 | KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list) |
| 2305 | { |
| 2306 | while (list) { |
| 2307 | kmod_module_info_free(list->data); |
| 2308 | list = kmod_list_remove(list); |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | struct kmod_module_version { |
| 2313 | uint64_t crc; |
| 2314 | char symbol[]; |
| 2315 | }; |
| 2316 | |
| 2317 | static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol) |
| 2318 | { |
| 2319 | struct kmod_module_version *mv; |
| 2320 | size_t symbollen = strlen(symbol) + 1; |
| 2321 | |
| 2322 | mv = malloc(sizeof(struct kmod_module_version) + symbollen); |
| 2323 | if (mv == NULL) |
| 2324 | return NULL; |
| 2325 | |
| 2326 | mv->crc = crc; |
| 2327 | memcpy(mv->symbol, symbol, symbollen); |
| 2328 | return mv; |
| 2329 | } |
| 2330 | |
| 2331 | static void kmod_module_version_free(struct kmod_module_version *version) |
| 2332 | { |
| 2333 | free(version); |
| 2334 | } |
| 2335 | |
| 2336 | /** |
| 2337 | * kmod_module_get_versions: |
| 2338 | * @mod: kmod module |
| 2339 | * @list: where to return list of module versions. Use |
Lucas De Marchi | db74cee | 2012-01-09 03:45:19 -0200 | [diff] [blame] | 2340 | * kmod_module_version_get_symbol() and |
| 2341 | * kmod_module_version_get_crc(). Release this list with |
| 2342 | * kmod_module_versions_free_list() |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2343 | * |
| 2344 | * Get a list of entries in ELF section "__versions". |
| 2345 | * |
| 2346 | * After use, free the @list by calling kmod_module_versions_free_list(). |
| 2347 | * |
| 2348 | * Returns: 0 on success or < 0 otherwise. |
| 2349 | */ |
| 2350 | KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list) |
| 2351 | { |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2352 | struct kmod_elf *elf; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2353 | struct kmod_modversion *versions; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2354 | int i, count, ret = 0; |
| 2355 | |
| 2356 | if (mod == NULL || list == NULL) |
| 2357 | return -ENOENT; |
| 2358 | |
| 2359 | assert(*list == NULL); |
| 2360 | |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 2361 | elf = kmod_module_get_elf(mod); |
| 2362 | if (elf == NULL) |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2363 | return -errno; |
| 2364 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2365 | count = kmod_elf_get_modversions(elf, &versions); |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 2366 | if (count < 0) |
| 2367 | return count; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2368 | |
| 2369 | for (i = 0; i < count; i++) { |
| 2370 | struct kmod_module_version *mv; |
| 2371 | struct kmod_list *n; |
| 2372 | |
| 2373 | mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol); |
| 2374 | if (mv == NULL) { |
| 2375 | ret = -errno; |
| 2376 | kmod_module_versions_free_list(*list); |
| 2377 | *list = NULL; |
| 2378 | goto list_error; |
| 2379 | } |
| 2380 | |
| 2381 | n = kmod_list_append(*list, mv); |
| 2382 | if (n != NULL) |
| 2383 | *list = n; |
| 2384 | else { |
| 2385 | kmod_module_version_free(mv); |
| 2386 | kmod_module_versions_free_list(*list); |
| 2387 | *list = NULL; |
| 2388 | ret = -ENOMEM; |
| 2389 | goto list_error; |
| 2390 | } |
| 2391 | } |
| 2392 | ret = count; |
| 2393 | |
| 2394 | list_error: |
| 2395 | free(versions); |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2396 | return ret; |
| 2397 | } |
| 2398 | |
| 2399 | /** |
Chengwei Yang | 491c490 | 2013-05-04 17:07:02 +0800 | [diff] [blame] | 2400 | * kmod_module_version_get_symbol: |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 2401 | * @entry: a list entry representing a kmod module versions |
| 2402 | * |
| 2403 | * Get the symbol of a kmod module versions. |
| 2404 | * |
| 2405 | * Returns: the symbol of this kmod module versions on success or NULL |
| 2406 | * on failure. The string is owned by the versions, do not free it. |
| 2407 | */ |
| 2408 | KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry) |
| 2409 | { |
| 2410 | struct kmod_module_version *version; |
| 2411 | |
| 2412 | if (entry == NULL) |
| 2413 | return NULL; |
| 2414 | |
| 2415 | version = entry->data; |
| 2416 | return version->symbol; |
| 2417 | } |
| 2418 | |
| 2419 | /** |
| 2420 | * kmod_module_version_get_crc: |
| 2421 | * @entry: a list entry representing a kmod module version |
| 2422 | * |
| 2423 | * Get the crc of a kmod module version. |
| 2424 | * |
| 2425 | * Returns: the crc of this kmod module version on success or NULL on |
| 2426 | * failure. The string is owned by the version, do not free it. |
| 2427 | */ |
| 2428 | KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry) |
| 2429 | { |
| 2430 | struct kmod_module_version *version; |
| 2431 | |
| 2432 | if (entry == NULL) |
| 2433 | return 0; |
| 2434 | |
| 2435 | version = entry->data; |
| 2436 | return version->crc; |
| 2437 | } |
| 2438 | |
| 2439 | /** |
| 2440 | * kmod_module_versions_free_list: |
| 2441 | * @list: kmod module versions list |
| 2442 | * |
| 2443 | * Release the resources taken by @list |
| 2444 | */ |
| 2445 | KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list) |
| 2446 | { |
| 2447 | while (list) { |
| 2448 | kmod_module_version_free(list->data); |
| 2449 | list = kmod_list_remove(list); |
| 2450 | } |
| 2451 | } |
Gustavo Sverzut Barbieri | 45e6db9 | 2011-12-19 21:23:13 -0200 | [diff] [blame] | 2452 | |
| 2453 | struct kmod_module_symbol { |
| 2454 | uint64_t crc; |
| 2455 | char symbol[]; |
| 2456 | }; |
| 2457 | |
| 2458 | static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol) |
| 2459 | { |
| 2460 | struct kmod_module_symbol *mv; |
| 2461 | size_t symbollen = strlen(symbol) + 1; |
| 2462 | |
| 2463 | mv = malloc(sizeof(struct kmod_module_symbol) + symbollen); |
| 2464 | if (mv == NULL) |
| 2465 | return NULL; |
| 2466 | |
| 2467 | mv->crc = crc; |
| 2468 | memcpy(mv->symbol, symbol, symbollen); |
| 2469 | return mv; |
| 2470 | } |
| 2471 | |
| 2472 | static void kmod_module_symbol_free(struct kmod_module_symbol *symbol) |
| 2473 | { |
| 2474 | free(symbol); |
| 2475 | } |
| 2476 | |
| 2477 | /** |
| 2478 | * kmod_module_get_symbols: |
| 2479 | * @mod: kmod module |
| 2480 | * @list: where to return list of module symbols. Use |
Lucas De Marchi | db74cee | 2012-01-09 03:45:19 -0200 | [diff] [blame] | 2481 | * kmod_module_symbol_get_symbol() and |
| 2482 | * kmod_module_symbol_get_crc(). Release this list with |
| 2483 | * kmod_module_symbols_free_list() |
Gustavo Sverzut Barbieri | 45e6db9 | 2011-12-19 21:23:13 -0200 | [diff] [blame] | 2484 | * |
| 2485 | * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings". |
| 2486 | * |
| 2487 | * After use, free the @list by calling kmod_module_symbols_free_list(). |
| 2488 | * |
| 2489 | * Returns: 0 on success or < 0 otherwise. |
| 2490 | */ |
| 2491 | KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list) |
| 2492 | { |
Gustavo Sverzut Barbieri | 45e6db9 | 2011-12-19 21:23:13 -0200 | [diff] [blame] | 2493 | struct kmod_elf *elf; |
Gustavo Sverzut Barbieri | 45e6db9 | 2011-12-19 21:23:13 -0200 | [diff] [blame] | 2494 | struct kmod_modversion *symbols; |
Gustavo Sverzut Barbieri | 45e6db9 | 2011-12-19 21:23:13 -0200 | [diff] [blame] | 2495 | int i, count, ret = 0; |
| 2496 | |
| 2497 | if (mod == NULL || list == NULL) |
| 2498 | return -ENOENT; |
| 2499 | |
| 2500 | assert(*list == NULL); |
| 2501 | |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 2502 | elf = kmod_module_get_elf(mod); |
| 2503 | if (elf == NULL) |
Gustavo Sverzut Barbieri | 45e6db9 | 2011-12-19 21:23:13 -0200 | [diff] [blame] | 2504 | return -errno; |
| 2505 | |
Gustavo Sverzut Barbieri | 45e6db9 | 2011-12-19 21:23:13 -0200 | [diff] [blame] | 2506 | count = kmod_elf_get_symbols(elf, &symbols); |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 2507 | if (count < 0) |
| 2508 | return count; |
Gustavo Sverzut Barbieri | 45e6db9 | 2011-12-19 21:23:13 -0200 | [diff] [blame] | 2509 | |
| 2510 | for (i = 0; i < count; i++) { |
| 2511 | struct kmod_module_symbol *mv; |
| 2512 | struct kmod_list *n; |
| 2513 | |
| 2514 | mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol); |
| 2515 | if (mv == NULL) { |
| 2516 | ret = -errno; |
| 2517 | kmod_module_symbols_free_list(*list); |
| 2518 | *list = NULL; |
| 2519 | goto list_error; |
| 2520 | } |
| 2521 | |
| 2522 | n = kmod_list_append(*list, mv); |
| 2523 | if (n != NULL) |
| 2524 | *list = n; |
| 2525 | else { |
| 2526 | kmod_module_symbol_free(mv); |
| 2527 | kmod_module_symbols_free_list(*list); |
| 2528 | *list = NULL; |
| 2529 | ret = -ENOMEM; |
| 2530 | goto list_error; |
| 2531 | } |
| 2532 | } |
| 2533 | ret = count; |
| 2534 | |
| 2535 | list_error: |
| 2536 | free(symbols); |
Gustavo Sverzut Barbieri | 45e6db9 | 2011-12-19 21:23:13 -0200 | [diff] [blame] | 2537 | return ret; |
| 2538 | } |
| 2539 | |
| 2540 | /** |
Lucas De Marchi | db74cee | 2012-01-09 03:45:19 -0200 | [diff] [blame] | 2541 | * kmod_module_symbol_get_symbol: |
Gustavo Sverzut Barbieri | 45e6db9 | 2011-12-19 21:23:13 -0200 | [diff] [blame] | 2542 | * @entry: a list entry representing a kmod module symbols |
| 2543 | * |
| 2544 | * Get the symbol of a kmod module symbols. |
| 2545 | * |
| 2546 | * Returns: the symbol of this kmod module symbols on success or NULL |
| 2547 | * on failure. The string is owned by the symbols, do not free it. |
| 2548 | */ |
| 2549 | KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry) |
| 2550 | { |
| 2551 | struct kmod_module_symbol *symbol; |
| 2552 | |
| 2553 | if (entry == NULL) |
| 2554 | return NULL; |
| 2555 | |
| 2556 | symbol = entry->data; |
| 2557 | return symbol->symbol; |
| 2558 | } |
| 2559 | |
| 2560 | /** |
| 2561 | * kmod_module_symbol_get_crc: |
| 2562 | * @entry: a list entry representing a kmod module symbol |
| 2563 | * |
| 2564 | * Get the crc of a kmod module symbol. |
| 2565 | * |
| 2566 | * Returns: the crc of this kmod module symbol on success or NULL on |
| 2567 | * failure. The string is owned by the symbol, do not free it. |
| 2568 | */ |
| 2569 | KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry) |
| 2570 | { |
| 2571 | struct kmod_module_symbol *symbol; |
| 2572 | |
| 2573 | if (entry == NULL) |
| 2574 | return 0; |
| 2575 | |
| 2576 | symbol = entry->data; |
| 2577 | return symbol->crc; |
| 2578 | } |
| 2579 | |
| 2580 | /** |
| 2581 | * kmod_module_symbols_free_list: |
| 2582 | * @list: kmod module symbols list |
| 2583 | * |
| 2584 | * Release the resources taken by @list |
| 2585 | */ |
| 2586 | KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list) |
| 2587 | { |
| 2588 | while (list) { |
| 2589 | kmod_module_symbol_free(list->data); |
| 2590 | list = kmod_list_remove(list); |
| 2591 | } |
| 2592 | } |
Gustavo Sverzut Barbieri | 674f859 | 2011-12-20 11:54:53 -0200 | [diff] [blame] | 2593 | |
| 2594 | struct kmod_module_dependency_symbol { |
| 2595 | uint64_t crc; |
| 2596 | uint8_t bind; |
| 2597 | char symbol[]; |
| 2598 | }; |
| 2599 | |
| 2600 | static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol) |
| 2601 | { |
| 2602 | struct kmod_module_dependency_symbol *mv; |
| 2603 | size_t symbollen = strlen(symbol) + 1; |
| 2604 | |
| 2605 | mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen); |
| 2606 | if (mv == NULL) |
| 2607 | return NULL; |
| 2608 | |
| 2609 | mv->crc = crc; |
| 2610 | mv->bind = bind; |
| 2611 | memcpy(mv->symbol, symbol, symbollen); |
| 2612 | return mv; |
| 2613 | } |
| 2614 | |
| 2615 | static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol) |
| 2616 | { |
| 2617 | free(dependency_symbol); |
| 2618 | } |
| 2619 | |
| 2620 | /** |
| 2621 | * kmod_module_get_dependency_symbols: |
| 2622 | * @mod: kmod module |
| 2623 | * @list: where to return list of module dependency_symbols. Use |
| 2624 | * kmod_module_dependency_symbol_get_symbol() and |
| 2625 | * kmod_module_dependency_symbol_get_crc(). Release this list with |
| 2626 | * kmod_module_dependency_symbols_free_list() |
| 2627 | * |
| 2628 | * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings". |
| 2629 | * |
| 2630 | * After use, free the @list by calling |
| 2631 | * kmod_module_dependency_symbols_free_list(). |
| 2632 | * |
| 2633 | * Returns: 0 on success or < 0 otherwise. |
| 2634 | */ |
| 2635 | KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list) |
| 2636 | { |
Gustavo Sverzut Barbieri | 674f859 | 2011-12-20 11:54:53 -0200 | [diff] [blame] | 2637 | struct kmod_elf *elf; |
Gustavo Sverzut Barbieri | 674f859 | 2011-12-20 11:54:53 -0200 | [diff] [blame] | 2638 | struct kmod_modversion *symbols; |
Gustavo Sverzut Barbieri | 674f859 | 2011-12-20 11:54:53 -0200 | [diff] [blame] | 2639 | int i, count, ret = 0; |
| 2640 | |
| 2641 | if (mod == NULL || list == NULL) |
| 2642 | return -ENOENT; |
| 2643 | |
| 2644 | assert(*list == NULL); |
| 2645 | |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 2646 | elf = kmod_module_get_elf(mod); |
| 2647 | if (elf == NULL) |
Gustavo Sverzut Barbieri | 674f859 | 2011-12-20 11:54:53 -0200 | [diff] [blame] | 2648 | return -errno; |
| 2649 | |
Gustavo Sverzut Barbieri | 674f859 | 2011-12-20 11:54:53 -0200 | [diff] [blame] | 2650 | count = kmod_elf_get_dependency_symbols(elf, &symbols); |
Lucas De Marchi | 1eff942 | 2012-10-18 01:36:33 -0300 | [diff] [blame] | 2651 | if (count < 0) |
| 2652 | return count; |
Gustavo Sverzut Barbieri | 674f859 | 2011-12-20 11:54:53 -0200 | [diff] [blame] | 2653 | |
| 2654 | for (i = 0; i < count; i++) { |
| 2655 | struct kmod_module_dependency_symbol *mv; |
| 2656 | struct kmod_list *n; |
| 2657 | |
| 2658 | mv = kmod_module_dependency_symbols_new(symbols[i].crc, |
| 2659 | symbols[i].bind, |
| 2660 | symbols[i].symbol); |
| 2661 | if (mv == NULL) { |
| 2662 | ret = -errno; |
| 2663 | kmod_module_dependency_symbols_free_list(*list); |
| 2664 | *list = NULL; |
| 2665 | goto list_error; |
| 2666 | } |
| 2667 | |
| 2668 | n = kmod_list_append(*list, mv); |
| 2669 | if (n != NULL) |
| 2670 | *list = n; |
| 2671 | else { |
| 2672 | kmod_module_dependency_symbol_free(mv); |
| 2673 | kmod_module_dependency_symbols_free_list(*list); |
| 2674 | *list = NULL; |
| 2675 | ret = -ENOMEM; |
| 2676 | goto list_error; |
| 2677 | } |
| 2678 | } |
| 2679 | ret = count; |
| 2680 | |
| 2681 | list_error: |
| 2682 | free(symbols); |
Gustavo Sverzut Barbieri | 674f859 | 2011-12-20 11:54:53 -0200 | [diff] [blame] | 2683 | return ret; |
| 2684 | } |
| 2685 | |
| 2686 | /** |
| 2687 | * kmod_module_dependency_symbol_get_symbol: |
| 2688 | * @entry: a list entry representing a kmod module dependency_symbols |
| 2689 | * |
| 2690 | * Get the dependency symbol of a kmod module |
| 2691 | * |
| 2692 | * Returns: the symbol of this kmod module dependency_symbols on success or NULL |
| 2693 | * on failure. The string is owned by the dependency_symbols, do not free it. |
| 2694 | */ |
| 2695 | KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry) |
| 2696 | { |
| 2697 | struct kmod_module_dependency_symbol *dependency_symbol; |
| 2698 | |
| 2699 | if (entry == NULL) |
| 2700 | return NULL; |
| 2701 | |
| 2702 | dependency_symbol = entry->data; |
| 2703 | return dependency_symbol->symbol; |
| 2704 | } |
| 2705 | |
| 2706 | /** |
| 2707 | * kmod_module_dependency_symbol_get_crc: |
| 2708 | * @entry: a list entry representing a kmod module dependency_symbol |
| 2709 | * |
| 2710 | * Get the crc of a kmod module dependency_symbol. |
| 2711 | * |
| 2712 | * Returns: the crc of this kmod module dependency_symbol on success or NULL on |
| 2713 | * failure. The string is owned by the dependency_symbol, do not free it. |
| 2714 | */ |
| 2715 | KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry) |
| 2716 | { |
| 2717 | struct kmod_module_dependency_symbol *dependency_symbol; |
| 2718 | |
| 2719 | if (entry == NULL) |
| 2720 | return 0; |
| 2721 | |
| 2722 | dependency_symbol = entry->data; |
| 2723 | return dependency_symbol->crc; |
| 2724 | } |
| 2725 | |
| 2726 | /** |
| 2727 | * kmod_module_dependency_symbol_get_bind: |
| 2728 | * @entry: a list entry representing a kmod module dependency_symbol |
| 2729 | * |
| 2730 | * Get the bind type of a kmod module dependency_symbol. |
| 2731 | * |
| 2732 | * Returns: the bind of this kmod module dependency_symbol on success |
| 2733 | * or < 0 on failure. |
| 2734 | */ |
| 2735 | KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry) |
| 2736 | { |
| 2737 | struct kmod_module_dependency_symbol *dependency_symbol; |
| 2738 | |
| 2739 | if (entry == NULL) |
| 2740 | return 0; |
| 2741 | |
| 2742 | dependency_symbol = entry->data; |
| 2743 | return dependency_symbol->bind; |
| 2744 | } |
| 2745 | |
| 2746 | /** |
| 2747 | * kmod_module_dependency_symbols_free_list: |
| 2748 | * @list: kmod module dependency_symbols list |
| 2749 | * |
| 2750 | * Release the resources taken by @list |
| 2751 | */ |
| 2752 | KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list) |
| 2753 | { |
| 2754 | while (list) { |
| 2755 | kmod_module_dependency_symbol_free(list->data); |
| 2756 | list = kmod_list_remove(list); |
| 2757 | } |
| 2758 | } |