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