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