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