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