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