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