Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -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 | 586fc30 | 2011-11-21 14:35:35 -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 | 586fc30 | 2011-11-21 14:35:35 -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 | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 18 | */ |
| 19 | |
Lucas De Marchi | ee1d188 | 2012-02-27 18:48:02 -0300 | [diff] [blame] | 20 | #include <assert.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 21 | #include <ctype.h> |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 22 | #include <errno.h> |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 23 | #include <fnmatch.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 24 | #include <limits.h> |
| 25 | #include <stdarg.h> |
| 26 | #include <stddef.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 29 | #include <string.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 30 | #include <unistd.h> |
Lucas De Marchi | c4dc3ca | 2011-12-31 19:28:31 -0200 | [diff] [blame] | 31 | #include <sys/stat.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 32 | #include <sys/utsname.h> |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 33 | |
Lucas De Marchi | 0db718e | 2014-10-03 00:29:18 -0300 | [diff] [blame] | 34 | #include <shared/hash.h> |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 35 | #include <shared/util.h> |
| 36 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 37 | #include "libkmod.h" |
Lucas De Marchi | 83b855a | 2013-07-04 16:13:11 -0300 | [diff] [blame] | 38 | #include "libkmod-internal.h" |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 39 | #include "libkmod-index.h" |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 40 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 41 | #define KMOD_HASH_SIZE (256) |
| 42 | #define KMOD_LRU_MAX (128) |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 43 | #define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_BUILTIN + 1 |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 44 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 45 | /** |
| 46 | * SECTION:libkmod |
| 47 | * @short_description: libkmod context |
| 48 | * |
| 49 | * The context contains the default values for the library user, |
| 50 | * and is passed to all library operations. |
| 51 | */ |
| 52 | |
Lucas De Marchi | 63be91c | 2012-01-16 10:43:34 -0200 | [diff] [blame] | 53 | static struct _index_files { |
| 54 | const char *fn; |
| 55 | const char *prefix; |
| 56 | } index_files[] = { |
Lucas De Marchi | b08314f | 2012-01-16 12:01:48 -0200 | [diff] [blame] | 57 | [KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep", .prefix = "" }, |
| 58 | [KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .prefix = "alias " }, |
| 59 | [KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .prefix = "alias "}, |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 60 | [KMOD_INDEX_MODULES_BUILTIN] = { .fn = "modules.builtin", .prefix = ""}, |
Lucas De Marchi | a4a7502 | 2011-12-08 14:56:48 -0200 | [diff] [blame] | 61 | }; |
| 62 | |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 63 | static const char *default_config_paths[] = { |
Kay Sievers | a308abe | 2011-12-20 17:58:05 +0100 | [diff] [blame] | 64 | SYSCONFDIR "/modprobe.d", |
Lucas De Marchi | 436da1e | 2012-03-15 09:19:34 -0300 | [diff] [blame] | 65 | "/run/modprobe.d", |
Dave Reisner | c5b37db | 2012-09-27 11:00:42 -0400 | [diff] [blame] | 66 | "/lib/modprobe.d", |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 67 | NULL |
| 68 | }; |
| 69 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 70 | /** |
| 71 | * kmod_ctx: |
| 72 | * |
| 73 | * Opaque object representing the library context. |
| 74 | */ |
| 75 | struct kmod_ctx { |
| 76 | int refcount; |
Gustavo Sverzut Barbieri | 8d3f3ef | 2011-12-02 21:10:24 -0200 | [diff] [blame] | 77 | int log_priority; |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 78 | void (*log_fn)(void *data, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 79 | int priority, const char *file, int line, |
| 80 | const char *fn, const char *format, va_list args); |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 81 | void *log_data; |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 82 | const void *userdata; |
| 83 | char *dirname; |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 84 | struct kmod_config *config; |
Lucas De Marchi | 822913d | 2011-12-27 12:13:54 -0200 | [diff] [blame] | 85 | struct hash *modules_by_name; |
Lucas De Marchi | b08314f | 2012-01-16 12:01:48 -0200 | [diff] [blame] | 86 | struct index_mm *indexes[_KMOD_INDEX_MODULES_SIZE]; |
| 87 | unsigned long long indexes_stamp[_KMOD_INDEX_MODULES_SIZE]; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 88 | }; |
| 89 | |
Lucas De Marchi | 7192828 | 2012-05-10 20:58:46 -0300 | [diff] [blame] | 90 | void kmod_log(const struct kmod_ctx *ctx, |
| 91 | int priority, const char *file, int line, const char *fn, |
| 92 | const char *format, ...) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 93 | { |
| 94 | va_list args; |
| 95 | |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 96 | if (ctx->log_fn == NULL) |
| 97 | return; |
| 98 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 99 | va_start(args, format); |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 100 | ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 101 | va_end(args); |
| 102 | } |
| 103 | |
Lucas De Marchi | 1958af8 | 2013-04-21 16:16:18 -0300 | [diff] [blame] | 104 | _printf_format_(6, 0) |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 105 | static void log_filep(void *data, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 106 | int priority, const char *file, int line, |
| 107 | const char *fn, const char *format, va_list args) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 108 | { |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 109 | FILE *fp = data; |
Gustavo Sverzut Barbieri | e3cb090 | 2012-01-07 19:25:03 -0200 | [diff] [blame] | 110 | #ifdef ENABLE_DEBUG |
| 111 | char buf[16]; |
| 112 | const char *priname; |
| 113 | switch (priority) { |
| 114 | case LOG_EMERG: |
| 115 | priname = "EMERGENCY"; |
| 116 | break; |
| 117 | case LOG_ALERT: |
| 118 | priname = "ALERT"; |
| 119 | break; |
| 120 | case LOG_CRIT: |
| 121 | priname = "CRITICAL"; |
| 122 | break; |
| 123 | case LOG_ERR: |
| 124 | priname = "ERROR"; |
| 125 | break; |
| 126 | case LOG_WARNING: |
| 127 | priname = "WARNING"; |
| 128 | break; |
| 129 | case LOG_NOTICE: |
| 130 | priname = "NOTICE"; |
| 131 | break; |
| 132 | case LOG_INFO: |
| 133 | priname = "INFO"; |
| 134 | break; |
| 135 | case LOG_DEBUG: |
| 136 | priname = "DEBUG"; |
| 137 | break; |
| 138 | default: |
| 139 | snprintf(buf, sizeof(buf), "L:%d", priority); |
| 140 | priname = buf; |
| 141 | } |
| 142 | fprintf(fp, "libkmod: %s %s:%d %s: ", priname, file, line, fn); |
| 143 | #else |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 144 | fprintf(fp, "libkmod: %s: ", fn); |
Gustavo Sverzut Barbieri | e3cb090 | 2012-01-07 19:25:03 -0200 | [diff] [blame] | 145 | #endif |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 146 | vfprintf(fp, format, args); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 147 | } |
| 148 | |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 149 | const char *kmod_get_dirname(const struct kmod_ctx *ctx) |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 150 | { |
| 151 | return ctx->dirname; |
| 152 | } |
| 153 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 154 | /** |
| 155 | * kmod_get_userdata: |
| 156 | * @ctx: kmod library context |
| 157 | * |
| 158 | * Retrieve stored data pointer from library context. This might be useful |
Lucas De Marchi | be5a6de | 2011-12-14 03:44:38 -0200 | [diff] [blame] | 159 | * to access from callbacks. |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 160 | * |
| 161 | * Returns: stored userdata |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 162 | */ |
Lucas De Marchi | 6d17755 | 2011-11-23 11:52:30 -0200 | [diff] [blame] | 163 | KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 164 | { |
| 165 | if (ctx == NULL) |
| 166 | return NULL; |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 167 | return (void *)ctx->userdata; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| 171 | * kmod_set_userdata: |
| 172 | * @ctx: kmod library context |
| 173 | * @userdata: data pointer |
| 174 | * |
| 175 | * Store custom @userdata in the library context. |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 176 | */ |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 177 | KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 178 | { |
| 179 | if (ctx == NULL) |
| 180 | return; |
| 181 | ctx->userdata = userdata; |
| 182 | } |
| 183 | |
| 184 | static int log_priority(const char *priority) |
| 185 | { |
| 186 | char *endptr; |
| 187 | int prio; |
| 188 | |
| 189 | prio = strtol(priority, &endptr, 10); |
| 190 | if (endptr[0] == '\0' || isspace(endptr[0])) |
| 191 | return prio; |
| 192 | if (strncmp(priority, "err", 3) == 0) |
| 193 | return LOG_ERR; |
| 194 | if (strncmp(priority, "info", 4) == 0) |
| 195 | return LOG_INFO; |
| 196 | if (strncmp(priority, "debug", 5) == 0) |
| 197 | return LOG_DEBUG; |
| 198 | return 0; |
| 199 | } |
| 200 | |
Dave Reisner | c5b37db | 2012-09-27 11:00:42 -0400 | [diff] [blame] | 201 | static const char *dirname_default_prefix = "/lib/modules"; |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 202 | |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 203 | static char *get_kernel_release(const char *dirname) |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 204 | { |
| 205 | struct utsname u; |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 206 | char *p; |
| 207 | |
| 208 | if (dirname != NULL) |
Lucas De Marchi | 2e092e1 | 2012-01-14 02:31:51 -0200 | [diff] [blame] | 209 | return path_make_absolute_cwd(dirname); |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 210 | |
| 211 | if (uname(&u) < 0) |
| 212 | return NULL; |
| 213 | |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 214 | if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0) |
| 215 | return NULL; |
| 216 | |
| 217 | return p; |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 218 | } |
| 219 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 220 | /** |
| 221 | * kmod_new: |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 222 | * @dirname: what to consider as linux module's directory, if NULL |
Dave Reisner | c5b37db | 2012-09-27 11:00:42 -0400 | [diff] [blame] | 223 | * defaults to /lib/modules/`uname -r`. If it's relative, |
Chengwei Yang | 491c490 | 2013-05-04 17:07:02 +0800 | [diff] [blame] | 224 | * it's treated as relative to the current working directory. |
| 225 | * Otherwise, give an absolute dirname. |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 226 | * @config_paths: ordered array of paths (directories or files) where |
Lucas De Marchi | be5a6de | 2011-12-14 03:44:38 -0200 | [diff] [blame] | 227 | * to load from user-defined configuration parameters such as |
| 228 | * alias, blacklists, commands (install, remove). If |
| 229 | * NULL defaults to /run/modprobe.d, /etc/modprobe.d and |
Dave Reisner | c5b37db | 2012-09-27 11:00:42 -0400 | [diff] [blame] | 230 | * /lib/modprobe.d. Give an empty vector if configuration should |
| 231 | * not be read. This array must be null terminated. |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 232 | * |
Lucas De Marchi | e1daa4f | 2012-01-09 03:09:49 -0200 | [diff] [blame] | 233 | * Create kmod library context. This reads the kmod configuration |
| 234 | * and fills in the default values. |
| 235 | * |
| 236 | * The initial refcount is 1, and needs to be decremented to |
| 237 | * release the resources of the kmod library context. |
| 238 | * |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 239 | * Returns: a new kmod library context |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 240 | */ |
Lucas De Marchi | c35347f | 2011-12-12 10:48:02 -0200 | [diff] [blame] | 241 | KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname, |
| 242 | const char * const *config_paths) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 243 | { |
| 244 | const char *env; |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 245 | struct kmod_ctx *ctx; |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 246 | int err; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 247 | |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 248 | ctx = calloc(1, sizeof(struct kmod_ctx)); |
| 249 | if (!ctx) |
| 250 | return NULL; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 251 | |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 252 | ctx->refcount = 1; |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 253 | ctx->log_fn = log_filep; |
| 254 | ctx->log_data = stderr; |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 255 | ctx->log_priority = LOG_ERR; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 256 | |
Lucas De Marchi | 904c63a | 2011-11-30 20:27:50 -0200 | [diff] [blame] | 257 | ctx->dirname = get_kernel_release(dirname); |
Lucas De Marchi | 221631d | 2011-11-24 16:41:01 -0200 | [diff] [blame] | 258 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 259 | /* environment overwrites config */ |
Cristian RodrÃguez | 41a51c2 | 2013-02-11 15:07:52 -0300 | [diff] [blame] | 260 | env = secure_getenv("KMOD_LOG"); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 261 | if (env != NULL) |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 262 | kmod_set_log_priority(ctx, log_priority(env)); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 263 | |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 264 | if (config_paths == NULL) |
| 265 | config_paths = default_config_paths; |
| 266 | err = kmod_config_new(ctx, &ctx->config, config_paths); |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 267 | if (err < 0) { |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 268 | ERR(ctx, "could not create config\n"); |
| 269 | goto fail; |
| 270 | } |
| 271 | |
Lucas De Marchi | 822913d | 2011-12-27 12:13:54 -0200 | [diff] [blame] | 272 | ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 273 | if (ctx->modules_by_name == NULL) { |
| 274 | ERR(ctx, "could not create by-name hash\n"); |
| 275 | goto fail; |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 276 | } |
Lucas De Marchi | 7c2ab35 | 2011-11-29 18:07:43 -0200 | [diff] [blame] | 277 | |
Lucas De Marchi | ae6df84 | 2011-11-25 01:05:30 -0200 | [diff] [blame] | 278 | INFO(ctx, "ctx %p created\n", ctx); |
| 279 | DBG(ctx, "log_priority=%d\n", ctx->log_priority); |
Lucas De Marchi | 52a7704 | 2011-11-21 15:07:27 -0200 | [diff] [blame] | 280 | |
| 281 | return ctx; |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 282 | |
| 283 | fail: |
| 284 | free(ctx->modules_by_name); |
| 285 | free(ctx->dirname); |
| 286 | free(ctx); |
| 287 | return NULL; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
| 291 | * kmod_ref: |
| 292 | * @ctx: kmod library context |
| 293 | * |
| 294 | * Take a reference of the kmod library context. |
| 295 | * |
| 296 | * Returns: the passed kmod library context |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 297 | */ |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 298 | KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx) |
| 299 | { |
| 300 | if (ctx == NULL) |
| 301 | return NULL; |
| 302 | ctx->refcount++; |
| 303 | return ctx; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * kmod_unref: |
| 308 | * @ctx: kmod library context |
| 309 | * |
| 310 | * Drop a reference of the kmod library context. If the refcount |
| 311 | * reaches zero, the resources of the context will be released. |
Chengwei Yang | 491c490 | 2013-05-04 17:07:02 +0800 | [diff] [blame] | 312 | * |
| 313 | * Returns: the passed kmod library context or NULL if it's freed |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 314 | */ |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 315 | KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx) |
| 316 | { |
| 317 | if (ctx == NULL) |
| 318 | return NULL; |
Lucas De Marchi | 4d1e689 | 2011-11-24 15:41:48 -0200 | [diff] [blame] | 319 | |
| 320 | if (--ctx->refcount > 0) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 321 | return ctx; |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 322 | |
Lucas De Marchi | ae6df84 | 2011-11-25 01:05:30 -0200 | [diff] [blame] | 323 | INFO(ctx, "context %p released\n", ctx); |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 324 | |
| 325 | kmod_unload_resources(ctx); |
Lucas De Marchi | 822913d | 2011-12-27 12:13:54 -0200 | [diff] [blame] | 326 | hash_free(ctx->modules_by_name); |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 327 | free(ctx->dirname); |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 328 | if (ctx->config) |
| 329 | kmod_config_free(ctx->config); |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 330 | |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 331 | free(ctx); |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * kmod_set_log_fn: |
| 337 | * @ctx: kmod library context |
| 338 | * @log_fn: function to be called for logging messages |
Lucas De Marchi | b5b4d8e | 2012-01-04 21:07:59 -0200 | [diff] [blame] | 339 | * @data: data to pass to log function |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 340 | * |
| 341 | * The built-in logging writes to stderr. It can be |
| 342 | * overridden by a custom function, to plug log messages |
| 343 | * into the user's logging functionality. |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 344 | */ |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 345 | KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx, |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 346 | void (*log_fn)(void *data, |
Lucas De Marchi | e4351b0 | 2011-11-21 14:59:23 -0200 | [diff] [blame] | 347 | int priority, const char *file, |
| 348 | int line, const char *fn, |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 349 | const char *format, va_list args), |
| 350 | const void *data) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 351 | { |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 352 | if (ctx == NULL) |
| 353 | return; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 354 | ctx->log_fn = log_fn; |
Gustavo Sverzut Barbieri | 1bdd951 | 2011-12-08 13:47:55 -0200 | [diff] [blame] | 355 | ctx->log_data = (void *)data; |
Lucas De Marchi | ae6df84 | 2011-11-25 01:05:30 -0200 | [diff] [blame] | 356 | INFO(ctx, "custom logging function %p registered\n", log_fn); |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /** |
| 360 | * kmod_get_log_priority: |
| 361 | * @ctx: kmod library context |
| 362 | * |
| 363 | * Returns: the current logging priority |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 364 | */ |
Lucas De Marchi | 6d17755 | 2011-11-23 11:52:30 -0200 | [diff] [blame] | 365 | KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx) |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 366 | { |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 367 | if (ctx == NULL) |
| 368 | return -1; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 369 | return ctx->log_priority; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * kmod_set_log_priority: |
| 374 | * @ctx: kmod library context |
| 375 | * @priority: the new logging priority |
| 376 | * |
| 377 | * Set the current logging priority. The value controls which messages |
| 378 | * are logged. |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 379 | */ |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 380 | KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority) |
| 381 | { |
Gustavo Sverzut Barbieri | e5c60f1 | 2011-12-08 13:58:46 -0200 | [diff] [blame] | 382 | if (ctx == NULL) |
| 383 | return; |
Lucas De Marchi | 586fc30 | 2011-11-21 14:35:35 -0200 | [diff] [blame] | 384 | ctx->log_priority = priority; |
| 385 | } |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 386 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 387 | struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx, |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 388 | const char *key) |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 389 | { |
| 390 | struct kmod_module *mod; |
| 391 | |
Lucas De Marchi | 822913d | 2011-12-27 12:13:54 -0200 | [diff] [blame] | 392 | mod = hash_find(ctx->modules_by_name, key); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 393 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 394 | DBG(ctx, "get module name='%s' found=%p\n", key, mod); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 395 | |
| 396 | return mod; |
| 397 | } |
| 398 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 399 | void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, |
| 400 | const char *key) |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 401 | { |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 402 | DBG(ctx, "add %p key='%s'\n", mod, key); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 403 | |
Lucas De Marchi | 822913d | 2011-12-27 12:13:54 -0200 | [diff] [blame] | 404 | hash_add(ctx->modules_by_name, key, mod); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 405 | } |
| 406 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 407 | void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod, |
| 408 | const char *key) |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 409 | { |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 410 | DBG(ctx, "del %p key='%s'\n", mod, key); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 411 | |
Lucas De Marchi | 822913d | 2011-12-27 12:13:54 -0200 | [diff] [blame] | 412 | hash_del(ctx->modules_by_name, key); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 413 | } |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 414 | |
Lucas De Marchi | a009482 | 2011-12-02 09:53:31 -0200 | [diff] [blame] | 415 | static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx, |
Lucas De Marchi | 810803d | 2011-12-09 16:06:04 -0200 | [diff] [blame] | 416 | enum kmod_index index_number, |
Lucas De Marchi | 7b30f4f | 2011-12-01 16:25:37 -0200 | [diff] [blame] | 417 | const char *name, |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 418 | struct kmod_list **list) |
| 419 | { |
Lucas De Marchi | 6f1bc6e | 2011-12-02 10:02:05 -0200 | [diff] [blame] | 420 | int err, nmatch = 0; |
Lucas De Marchi | 0fbdfef | 2011-12-02 09:56:22 -0200 | [diff] [blame] | 421 | struct index_file *idx; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 422 | struct index_value *realnames, *realname; |
| 423 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 424 | if (ctx->indexes[index_number] != NULL) { |
Gustavo Sverzut Barbieri | 3e245be | 2011-12-10 09:28:42 -0200 | [diff] [blame] | 425 | DBG(ctx, "use mmaped index '%s' for name=%s\n", |
Lucas De Marchi | 63be91c | 2012-01-16 10:43:34 -0200 | [diff] [blame] | 426 | index_files[index_number].fn, name); |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 427 | realnames = index_mm_searchwild(ctx->indexes[index_number], |
| 428 | name); |
Lucas De Marchi | 9fd58f3 | 2011-12-31 18:53:24 -0200 | [diff] [blame] | 429 | } else { |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 430 | char fn[PATH_MAX]; |
| 431 | |
Gustavo Sverzut Barbieri | 3b20995 | 2011-12-10 09:21:03 -0200 | [diff] [blame] | 432 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
Lucas De Marchi | 63be91c | 2012-01-16 10:43:34 -0200 | [diff] [blame] | 433 | index_files[index_number].fn); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 434 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 435 | DBG(ctx, "file=%s name=%s\n", fn, name); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 436 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 437 | idx = index_file_open(fn); |
| 438 | if (idx == NULL) |
| 439 | return -ENOSYS; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 440 | |
Lucas De Marchi | 65a84f5 | 2011-12-09 16:11:42 -0200 | [diff] [blame] | 441 | realnames = index_searchwild(idx, name); |
| 442 | index_file_close(idx); |
| 443 | } |
Lucas De Marchi | 4272d08 | 2011-12-09 15:33:37 -0200 | [diff] [blame] | 444 | |
Ulisses Furquim | a955f71 | 2011-12-15 19:17:49 -0200 | [diff] [blame] | 445 | for (realname = realnames; realname; realname = realname->next) { |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 446 | struct kmod_module *mod; |
| 447 | |
Lucas De Marchi | ee3b3ff | 2011-12-13 14:20:48 -0200 | [diff] [blame] | 448 | err = kmod_module_new_from_alias(ctx, name, realname->value, &mod); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 449 | if (err < 0) { |
Gustavo Sverzut Barbieri | dfa96f1 | 2012-01-07 19:37:37 -0200 | [diff] [blame] | 450 | ERR(ctx, "Could not create module for alias=%s realname=%s: %s\n", |
| 451 | name, realname->value, strerror(-err)); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 452 | goto fail; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | *list = kmod_list_append(*list, mod); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 456 | nmatch++; |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 457 | } |
| 458 | |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 459 | index_values_free(realnames); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 460 | return nmatch; |
| 461 | |
| 462 | fail: |
| 463 | *list = kmod_list_remove_n_latest(*list, nmatch); |
Leandro Pereira | e84d912 | 2014-04-28 21:01:48 -0300 | [diff] [blame] | 464 | index_values_free(realnames); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 465 | return err; |
Lucas De Marchi | 7b30f4f | 2011-12-01 16:25:37 -0200 | [diff] [blame] | 466 | |
| 467 | } |
| 468 | |
Lucas De Marchi | 7b30f4f | 2011-12-01 16:25:37 -0200 | [diff] [blame] | 469 | int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name, |
| 470 | struct kmod_list **list) |
| 471 | { |
Lucas De Marchi | 0c010fa | 2011-12-28 13:33:26 -0200 | [diff] [blame] | 472 | if (!strstartswith(name, "symbol:")) |
Lucas De Marchi | 7b30f4f | 2011-12-01 16:25:37 -0200 | [diff] [blame] | 473 | return 0; |
| 474 | |
Lucas De Marchi | b08314f | 2012-01-16 12:01:48 -0200 | [diff] [blame] | 475 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_SYMBOL, |
| 476 | name, list); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 477 | } |
| 478 | |
Lucas De Marchi | 49e61ca | 2011-12-01 16:27:04 -0200 | [diff] [blame] | 479 | int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name, |
| 480 | struct kmod_list **list) |
| 481 | { |
Lucas De Marchi | b08314f | 2012-01-16 12:01:48 -0200 | [diff] [blame] | 482 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_ALIAS, |
| 483 | name, list); |
Lucas De Marchi | 49e61ca | 2011-12-01 16:27:04 -0200 | [diff] [blame] | 484 | } |
| 485 | |
Harish Jenny K N | fd44a98 | 2015-02-22 15:41:07 -0300 | [diff] [blame^] | 486 | static char *lookup_builtin_file(struct kmod_ctx *ctx, const char *name) |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 487 | { |
Harish Jenny K N | fd44a98 | 2015-02-22 15:41:07 -0300 | [diff] [blame^] | 488 | char *line; |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 489 | |
Lucas De Marchi | ee1d188 | 2012-02-27 18:48:02 -0300 | [diff] [blame] | 490 | if (ctx->indexes[KMOD_INDEX_MODULES_BUILTIN]) { |
| 491 | DBG(ctx, "use mmaped index '%s' modname=%s\n", |
| 492 | index_files[KMOD_INDEX_MODULES_BUILTIN].fn, |
| 493 | name); |
| 494 | line = index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_BUILTIN], |
| 495 | name); |
| 496 | } else { |
| 497 | struct index_file *idx; |
| 498 | char fn[PATH_MAX]; |
| 499 | |
| 500 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
| 501 | index_files[KMOD_INDEX_MODULES_BUILTIN].fn); |
| 502 | DBG(ctx, "file=%s modname=%s\n", fn, name); |
| 503 | |
| 504 | idx = index_file_open(fn); |
| 505 | if (idx == NULL) { |
| 506 | DBG(ctx, "could not open builtin file '%s'\n", fn); |
Harish Jenny K N | fd44a98 | 2015-02-22 15:41:07 -0300 | [diff] [blame^] | 507 | return NULL; |
Lucas De Marchi | ee1d188 | 2012-02-27 18:48:02 -0300 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | line = index_search(idx, name); |
| 511 | index_file_close(idx); |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 512 | } |
| 513 | |
Harish Jenny K N | fd44a98 | 2015-02-22 15:41:07 -0300 | [diff] [blame^] | 514 | return line; |
| 515 | } |
| 516 | |
| 517 | int kmod_lookup_alias_from_builtin_file(struct kmod_ctx *ctx, const char *name, |
| 518 | struct kmod_list **list) |
| 519 | { |
| 520 | char *line; |
| 521 | int err = 0; |
| 522 | |
| 523 | assert(*list == NULL); |
| 524 | |
| 525 | line = lookup_builtin_file(ctx, name); |
Lucas De Marchi | ee1d188 | 2012-02-27 18:48:02 -0300 | [diff] [blame] | 526 | if (line != NULL) { |
| 527 | struct kmod_module *mod; |
| 528 | |
| 529 | err = kmod_module_new_from_name(ctx, name, &mod); |
| 530 | if (err < 0) { |
| 531 | ERR(ctx, "Could not create module from name %s: %s\n", |
| 532 | name, strerror(-err)); |
| 533 | goto finish; |
| 534 | } |
| 535 | |
Harish Jenny K N | fd44a98 | 2015-02-22 15:41:07 -0300 | [diff] [blame^] | 536 | /* already mark it as builtin since it's being created from |
| 537 | * this index */ |
Lucas De Marchi | ee1d188 | 2012-02-27 18:48:02 -0300 | [diff] [blame] | 538 | kmod_module_set_builtin(mod, true); |
| 539 | *list = kmod_list_append(*list, mod); |
| 540 | if (*list == NULL) |
| 541 | err = -ENOMEM; |
| 542 | } |
| 543 | |
| 544 | finish: |
| 545 | free(line); |
Lucas De Marchi | 3805274 | 2012-02-16 20:43:16 -0200 | [diff] [blame] | 546 | return err; |
| 547 | } |
| 548 | |
Harish Jenny K N | fd44a98 | 2015-02-22 15:41:07 -0300 | [diff] [blame^] | 549 | bool kmod_lookup_alias_is_builtin(struct kmod_ctx *ctx, const char *name) |
| 550 | { |
| 551 | _cleanup_free_ char *line; |
| 552 | |
| 553 | line = lookup_builtin_file(ctx, name); |
| 554 | |
| 555 | return line != NULL; |
| 556 | } |
| 557 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 558 | char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name) |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 559 | { |
| 560 | struct index_file *idx; |
| 561 | char fn[PATH_MAX]; |
| 562 | char *line; |
| 563 | |
Lucas De Marchi | b08314f | 2012-01-16 12:01:48 -0200 | [diff] [blame] | 564 | if (ctx->indexes[KMOD_INDEX_MODULES_DEP]) { |
Gustavo Sverzut Barbieri | 8513210 | 2011-12-10 09:26:27 -0200 | [diff] [blame] | 565 | DBG(ctx, "use mmaped index '%s' modname=%s\n", |
Lucas De Marchi | b08314f | 2012-01-16 12:01:48 -0200 | [diff] [blame] | 566 | index_files[KMOD_INDEX_MODULES_DEP].fn, name); |
| 567 | return index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_DEP], |
| 568 | name); |
Gustavo Sverzut Barbieri | 8513210 | 2011-12-10 09:26:27 -0200 | [diff] [blame] | 569 | } |
| 570 | |
Lucas De Marchi | a4a7502 | 2011-12-08 14:56:48 -0200 | [diff] [blame] | 571 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
Lucas De Marchi | b08314f | 2012-01-16 12:01:48 -0200 | [diff] [blame] | 572 | index_files[KMOD_INDEX_MODULES_DEP].fn); |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 573 | |
| 574 | DBG(ctx, "file=%s modname=%s\n", fn, name); |
| 575 | |
| 576 | idx = index_file_open(fn); |
| 577 | if (idx == NULL) { |
Lucas De Marchi | adca3cd | 2012-02-17 05:00:09 -0200 | [diff] [blame] | 578 | DBG(ctx, "could not open moddep file '%s'\n", fn); |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | line = index_search(idx, name); |
| 583 | index_file_close(idx); |
| 584 | |
| 585 | return line; |
| 586 | } |
| 587 | |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 588 | int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name, |
| 589 | struct kmod_list **list) |
| 590 | { |
Lucas De Marchi | 1eb2ef6 | 2011-12-05 19:58:39 -0200 | [diff] [blame] | 591 | char *line; |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 592 | int n = 0; |
| 593 | |
| 594 | /* |
| 595 | * Module names do not contain ':'. Return early if we know it will |
| 596 | * not be found. |
| 597 | */ |
| 598 | if (strchr(name, ':')) |
| 599 | return 0; |
| 600 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 601 | line = kmod_search_moddep(ctx, name); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 602 | if (line != NULL) { |
| 603 | struct kmod_module *mod; |
| 604 | |
| 605 | n = kmod_module_new_from_name(ctx, name, &mod); |
| 606 | if (n < 0) { |
Gustavo Sverzut Barbieri | dfa96f1 | 2012-01-07 19:37:37 -0200 | [diff] [blame] | 607 | ERR(ctx, "Could not create module from name %s: %s\n", |
| 608 | name, strerror(-n)); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 609 | goto finish; |
| 610 | } |
| 611 | |
| 612 | *list = kmod_list_append(*list, mod); |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 613 | kmod_module_parse_depline(mod, line); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | finish: |
| 617 | free(line); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 618 | |
| 619 | return n; |
| 620 | } |
| 621 | |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 622 | int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, |
| 623 | struct kmod_list **list) |
| 624 | { |
Gustavo Sverzut Barbieri | d13e606 | 2011-12-02 21:40:22 -0200 | [diff] [blame] | 625 | struct kmod_config *config = ctx->config; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 626 | struct kmod_list *l; |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 627 | int err, nmatch = 0; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 628 | |
| 629 | kmod_list_foreach(l, config->aliases) { |
| 630 | const char *aliasname = kmod_alias_get_name(l); |
| 631 | const char *modname = kmod_alias_get_modname(l); |
| 632 | |
| 633 | if (fnmatch(aliasname, name, 0) == 0) { |
| 634 | struct kmod_module *mod; |
| 635 | |
Lucas De Marchi | ee3b3ff | 2011-12-13 14:20:48 -0200 | [diff] [blame] | 636 | err = kmod_module_new_from_alias(ctx, aliasname, |
| 637 | modname, &mod); |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 638 | if (err < 0) { |
Gustavo Sverzut Barbieri | dfa96f1 | 2012-01-07 19:37:37 -0200 | [diff] [blame] | 639 | ERR(ctx, "Could not create module for alias=%s modname=%s: %s\n", |
| 640 | name, modname, strerror(-err)); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 641 | goto fail; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | *list = kmod_list_append(*list, mod); |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 645 | nmatch++; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Lucas De Marchi | 23fc91c | 2011-12-01 15:35:31 -0200 | [diff] [blame] | 649 | return nmatch; |
| 650 | |
| 651 | fail: |
| 652 | *list = kmod_list_remove_n_latest(*list, nmatch); |
| 653 | return err; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 654 | } |
Gustavo Sverzut Barbieri | 1487a64 | 2011-12-08 05:17:43 -0200 | [diff] [blame] | 655 | |
Lucas De Marchi | f4fc552 | 2011-12-16 03:57:12 -0200 | [diff] [blame] | 656 | int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name, |
| 657 | struct kmod_list **list) |
| 658 | { |
| 659 | struct kmod_config *config = ctx->config; |
| 660 | struct kmod_list *l, *node; |
| 661 | int err, nmatch = 0; |
| 662 | |
| 663 | kmod_list_foreach(l, config->install_commands) { |
| 664 | const char *modname = kmod_command_get_modname(l); |
| 665 | |
| 666 | if (streq(modname, name)) { |
| 667 | const char *cmd = kmod_command_get_command(l); |
| 668 | struct kmod_module *mod; |
| 669 | |
| 670 | err = kmod_module_new_from_name(ctx, modname, &mod); |
| 671 | if (err < 0) { |
Gustavo Sverzut Barbieri | dfa96f1 | 2012-01-07 19:37:37 -0200 | [diff] [blame] | 672 | ERR(ctx, "Could not create module from name %s: %s\n", |
| 673 | modname, strerror(-err)); |
Lucas De Marchi | f4fc552 | 2011-12-16 03:57:12 -0200 | [diff] [blame] | 674 | return err; |
| 675 | } |
| 676 | |
| 677 | node = kmod_list_append(*list, mod); |
| 678 | if (node == NULL) { |
| 679 | ERR(ctx, "out of memory\n"); |
| 680 | return -ENOMEM; |
| 681 | } |
| 682 | |
| 683 | *list = node; |
| 684 | nmatch = 1; |
| 685 | |
| 686 | kmod_module_set_install_commands(mod, cmd); |
| 687 | |
| 688 | /* |
| 689 | * match only the first one, like modprobe from |
| 690 | * module-init-tools does |
| 691 | */ |
| 692 | break; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if (nmatch) |
| 697 | return nmatch; |
| 698 | |
| 699 | kmod_list_foreach(l, config->remove_commands) { |
| 700 | const char *modname = kmod_command_get_modname(l); |
| 701 | |
| 702 | if (streq(modname, name)) { |
| 703 | const char *cmd = kmod_command_get_command(l); |
| 704 | struct kmod_module *mod; |
| 705 | |
| 706 | err = kmod_module_new_from_name(ctx, modname, &mod); |
| 707 | if (err < 0) { |
Gustavo Sverzut Barbieri | dfa96f1 | 2012-01-07 19:37:37 -0200 | [diff] [blame] | 708 | ERR(ctx, "Could not create module from name %s: %s\n", |
| 709 | modname, strerror(-err)); |
Lucas De Marchi | f4fc552 | 2011-12-16 03:57:12 -0200 | [diff] [blame] | 710 | return err; |
| 711 | } |
| 712 | |
| 713 | node = kmod_list_append(*list, mod); |
| 714 | if (node == NULL) { |
| 715 | ERR(ctx, "out of memory\n"); |
| 716 | return -ENOMEM; |
| 717 | } |
| 718 | |
| 719 | *list = node; |
| 720 | nmatch = 1; |
| 721 | |
| 722 | kmod_module_set_remove_commands(mod, cmd); |
| 723 | |
| 724 | /* |
| 725 | * match only the first one, like modprobe from |
| 726 | * module-init-tools does |
| 727 | */ |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | return nmatch; |
| 733 | } |
| 734 | |
Lucas De Marchi | ece09aa | 2012-01-18 01:26:44 -0200 | [diff] [blame] | 735 | void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited) |
| 736 | { |
| 737 | struct hash_iter iter; |
| 738 | const void *v; |
| 739 | |
| 740 | hash_iter_init(ctx->modules_by_name, &iter); |
| 741 | while (hash_iter_next(&iter, NULL, &v)) |
| 742 | kmod_module_set_visited((struct kmod_module *)v, visited); |
| 743 | } |
| 744 | |
Michal Marek | 450bd1b | 2014-03-31 15:18:50 +0200 | [diff] [blame] | 745 | void kmod_set_modules_required(struct kmod_ctx *ctx, bool required) |
| 746 | { |
| 747 | struct hash_iter iter; |
| 748 | const void *v; |
| 749 | |
| 750 | hash_iter_init(ctx->modules_by_name, &iter); |
| 751 | while (hash_iter_next(&iter, NULL, &v)) |
| 752 | kmod_module_set_required((struct kmod_module *)v, required); |
| 753 | } |
| 754 | |
Lucas De Marchi | c4dc3ca | 2011-12-31 19:28:31 -0200 | [diff] [blame] | 755 | static bool is_cache_invalid(const char *path, unsigned long long stamp) |
| 756 | { |
| 757 | struct stat st; |
| 758 | |
| 759 | if (stat(path, &st) < 0) |
| 760 | return true; |
| 761 | |
Lucas De Marchi | 6068aaa | 2012-01-17 12:10:42 -0200 | [diff] [blame] | 762 | if (stamp != stat_mstamp(&st)) |
Lucas De Marchi | c4dc3ca | 2011-12-31 19:28:31 -0200 | [diff] [blame] | 763 | return true; |
| 764 | |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * kmod_validate_resources: |
| 770 | * @ctx: kmod library context |
| 771 | * |
| 772 | * Check if indexes and configuration files changed on disk and the current |
| 773 | * context is not valid anymore. |
| 774 | * |
Lucas De Marchi | f4cc6ea | 2012-01-09 02:35:41 -0200 | [diff] [blame] | 775 | * Returns: KMOD_RESOURCES_OK if resources are still valid, |
Lucas De Marchi | c4dc3ca | 2011-12-31 19:28:31 -0200 | [diff] [blame] | 776 | * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call |
| 777 | * kmod_unload_resources() and kmod_load_resources() or |
| 778 | * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created. |
| 779 | */ |
| 780 | KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx) |
| 781 | { |
| 782 | struct kmod_list *l; |
| 783 | size_t i; |
| 784 | |
| 785 | if (ctx == NULL || ctx->config == NULL) |
| 786 | return KMOD_RESOURCES_MUST_RECREATE; |
| 787 | |
| 788 | kmod_list_foreach(l, ctx->config->paths) { |
| 789 | struct kmod_config_path *cf = l->data; |
| 790 | |
| 791 | if (is_cache_invalid(cf->path, cf->stamp)) |
| 792 | return KMOD_RESOURCES_MUST_RECREATE; |
| 793 | } |
| 794 | |
Lucas De Marchi | b08314f | 2012-01-16 12:01:48 -0200 | [diff] [blame] | 795 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
Lucas De Marchi | c4dc3ca | 2011-12-31 19:28:31 -0200 | [diff] [blame] | 796 | char path[PATH_MAX]; |
| 797 | |
| 798 | if (ctx->indexes[i] == NULL) |
| 799 | continue; |
| 800 | |
| 801 | snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, |
Lucas De Marchi | 63be91c | 2012-01-16 10:43:34 -0200 | [diff] [blame] | 802 | index_files[i].fn); |
Lucas De Marchi | c4dc3ca | 2011-12-31 19:28:31 -0200 | [diff] [blame] | 803 | |
| 804 | if (is_cache_invalid(path, ctx->indexes_stamp[i])) |
| 805 | return KMOD_RESOURCES_MUST_RELOAD; |
| 806 | } |
| 807 | |
| 808 | return KMOD_RESOURCES_OK; |
| 809 | } |
| 810 | |
Lucas De Marchi | 54ba8b3 | 2011-12-09 16:42:14 -0200 | [diff] [blame] | 811 | /** |
Lucas De Marchi | be5a6de | 2011-12-14 03:44:38 -0200 | [diff] [blame] | 812 | * kmod_load_resources: |
| 813 | * @ctx: kmod library context |
| 814 | * |
| 815 | * Load indexes and keep them open in @ctx. This way it's faster to lookup |
| 816 | * information within the indexes. If this function is not called before a |
| 817 | * search, the necessary index is always opened and closed. |
| 818 | * |
| 819 | * If user will do more than one or two lookups, insertions, deletions, most |
| 820 | * likely it's good to call this function first. Particularly in a daemon like |
| 821 | * udev that on bootup issues hundreds of calls to lookup the index, calling |
| 822 | * this function will speedup the searches. |
| 823 | * |
| 824 | * Returns: 0 on success or < 0 otherwise. |
| 825 | */ |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 826 | KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx) |
| 827 | { |
| 828 | size_t i; |
| 829 | |
| 830 | if (ctx == NULL) |
| 831 | return -ENOENT; |
| 832 | |
Lucas De Marchi | b08314f | 2012-01-16 12:01:48 -0200 | [diff] [blame] | 833 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
Lucas De Marchi | 6de8f6e | 2011-12-14 03:58:31 -0200 | [diff] [blame] | 834 | char path[PATH_MAX]; |
Lucas De Marchi | 3e67676 | 2011-12-14 03:53:43 -0200 | [diff] [blame] | 835 | |
Lucas De Marchi | 16ca366 | 2011-12-20 12:29:13 -0200 | [diff] [blame] | 836 | if (ctx->indexes[i] != NULL) { |
Lucas De Marchi | 63be91c | 2012-01-16 10:43:34 -0200 | [diff] [blame] | 837 | INFO(ctx, "Index %s already loaded\n", |
| 838 | index_files[i].fn); |
Lucas De Marchi | 3e67676 | 2011-12-14 03:53:43 -0200 | [diff] [blame] | 839 | continue; |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 840 | } |
Lucas De Marchi | 3e67676 | 2011-12-14 03:53:43 -0200 | [diff] [blame] | 841 | |
Lucas De Marchi | 6de8f6e | 2011-12-14 03:58:31 -0200 | [diff] [blame] | 842 | snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, |
Lucas De Marchi | 63be91c | 2012-01-16 10:43:34 -0200 | [diff] [blame] | 843 | index_files[i].fn); |
Lucas De Marchi | 2e2e252 | 2012-03-02 20:33:26 -0300 | [diff] [blame] | 844 | ctx->indexes[i] = index_mm_open(ctx, path, |
Lucas De Marchi | 9fd58f3 | 2011-12-31 18:53:24 -0200 | [diff] [blame] | 845 | &ctx->indexes_stamp[i]); |
Lucas De Marchi | 3e67676 | 2011-12-14 03:53:43 -0200 | [diff] [blame] | 846 | if (ctx->indexes[i] == NULL) |
| 847 | goto fail; |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | return 0; |
| 851 | |
| 852 | fail: |
| 853 | kmod_unload_resources(ctx); |
| 854 | return -ENOMEM; |
| 855 | } |
| 856 | |
Lucas De Marchi | be5a6de | 2011-12-14 03:44:38 -0200 | [diff] [blame] | 857 | /** |
| 858 | * kmod_unload_resources: |
| 859 | * @ctx: kmod library context |
| 860 | * |
| 861 | * Unload all the indexes. This will free the resources to maintain the index |
| 862 | * open and all subsequent searches will need to open and close the index. |
| 863 | * |
| 864 | * User is free to call kmod_load_resources() and kmod_unload_resources() as |
| 865 | * many times as wanted during the lifecycle of @ctx. For example, if a daemon |
| 866 | * knows that when starting up it will lookup a lot of modules, it could call |
| 867 | * kmod_load_resources() and after the first burst of searches is gone, it |
| 868 | * could free the resources by calling kmod_unload_resources(). |
| 869 | * |
| 870 | * Returns: 0 on success or < 0 otherwise. |
| 871 | */ |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 872 | KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx) |
| 873 | { |
| 874 | size_t i; |
| 875 | |
| 876 | if (ctx == NULL) |
| 877 | return; |
| 878 | |
Lucas De Marchi | b08314f | 2012-01-16 12:01:48 -0200 | [diff] [blame] | 879 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 880 | if (ctx->indexes[i] != NULL) { |
| 881 | index_mm_close(ctx->indexes[i]); |
| 882 | ctx->indexes[i] = NULL; |
Lucas De Marchi | 9fd58f3 | 2011-12-31 18:53:24 -0200 | [diff] [blame] | 883 | ctx->indexes_stamp[i] = 0; |
Lucas De Marchi | 33bb69b | 2011-12-08 14:59:51 -0200 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | } |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 887 | |
Lucas De Marchi | 0224482 | 2012-01-16 16:43:47 -0200 | [diff] [blame] | 888 | /** |
| 889 | * kmod_dump_index: |
| 890 | * @ctx: kmod library context |
Chengwei Yang | d7152f6 | 2013-05-04 17:07:03 +0800 | [diff] [blame] | 891 | * @type: index to dump, valid indexes are |
| 892 | * KMOD_INDEX_MODULES_DEP: index of module dependencies; |
| 893 | * KMOD_INDEX_MODULES_ALIAS: index of module aliases; |
| 894 | * KMOD_INDEX_MODULES_SYMBOL: index of symbol aliases; |
| 895 | * KMOD_INDEX_MODULES_BUILTIN: index of builtin module. |
Lucas De Marchi | 0224482 | 2012-01-16 16:43:47 -0200 | [diff] [blame] | 896 | * @fd: file descriptor to dump index to |
| 897 | * |
Lucas De Marchi | 09e9ae5 | 2012-01-17 10:05:02 -0200 | [diff] [blame] | 898 | * Dump index to file descriptor. Note that this function doesn't use stdio.h |
| 899 | * so call fflush() before calling this function to be sure data is written in |
| 900 | * order. |
Lucas De Marchi | 0224482 | 2012-01-16 16:43:47 -0200 | [diff] [blame] | 901 | * |
| 902 | * Returns: 0 on success or < 0 otherwise. |
| 903 | */ |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 904 | KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type, |
| 905 | int fd) |
| 906 | { |
| 907 | if (ctx == NULL) |
| 908 | return -ENOSYS; |
| 909 | |
| 910 | if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE) |
| 911 | return -ENOENT; |
| 912 | |
| 913 | if (ctx->indexes[type] != NULL) { |
| 914 | DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn); |
| 915 | index_mm_dump(ctx->indexes[type], fd, |
| 916 | index_files[type].prefix); |
| 917 | } else { |
| 918 | char fn[PATH_MAX]; |
| 919 | struct index_file *idx; |
| 920 | |
| 921 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
| 922 | index_files[type].fn); |
| 923 | |
| 924 | DBG(ctx, "file=%s\n", fn); |
| 925 | |
| 926 | idx = index_file_open(fn); |
| 927 | if (idx == NULL) |
| 928 | return -ENOSYS; |
| 929 | |
| 930 | index_dump(idx, fd, index_files[type].prefix); |
| 931 | index_file_close(idx); |
| 932 | } |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 937 | const struct kmod_config *kmod_get_config(const struct kmod_ctx *ctx) |
Lucas De Marchi | c1c9c44 | 2011-12-24 10:50:47 -0200 | [diff] [blame] | 938 | { |
Lucas De Marchi | e7fc2c8 | 2012-06-12 01:43:46 -0300 | [diff] [blame] | 939 | return ctx->config; |
Lucas De Marchi | 8b5ee61 | 2012-01-13 01:14:46 -0200 | [diff] [blame] | 940 | } |