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