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