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