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