Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 1 | /* |
| 2 | * libkmod - interface to kernel module operations |
| 3 | * |
| 4 | * Copyright (C) 2011 ProFUSION embedded systems |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 21 | #include <assert.h> |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stddef.h> |
| 25 | #include <stdarg.h> |
| 26 | #include <unistd.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
| 29 | #include <ctype.h> |
| 30 | #include <inttypes.h> |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 31 | #include <limits.h> |
| 32 | #include <dirent.h> |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/mman.h> |
| 36 | #include <string.h> |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 37 | #include <fnmatch.h> |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 38 | |
| 39 | #include "libkmod.h" |
| 40 | #include "libkmod-private.h" |
| 41 | |
| 42 | /** |
| 43 | * kmod_module: |
| 44 | * |
| 45 | * Opaque object representing a module. |
| 46 | */ |
| 47 | struct kmod_module { |
| 48 | struct kmod_ctx *ctx; |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 49 | char *hashkey; |
Lucas De Marchi | 219f9c3 | 2011-12-13 13:07:40 -0200 | [diff] [blame] | 50 | char *name; |
Gustavo Sverzut Barbieri | f1fb6f8 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 51 | char *path; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 52 | struct kmod_list *dep; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 53 | char *options; |
Lucas De Marchi | 60f6760 | 2011-12-16 03:33:26 -0200 | [diff] [blame] | 54 | const char *install_commands; /* owned by kmod_config */ |
| 55 | const char *remove_commands; /* owned by kmod_config */ |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 56 | struct { |
| 57 | struct kmod_list *pre; |
| 58 | struct kmod_list *post; |
| 59 | } softdeps; |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 60 | char *alias; /* only set if this module was created from an alias */ |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 61 | int n_dep; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 62 | int refcount; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 63 | struct { |
| 64 | bool dep : 1; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 65 | bool options : 1; |
| 66 | bool install_commands : 1; |
| 67 | bool remove_commands : 1; |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 68 | bool softdeps : 1; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 69 | } init; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 70 | }; |
| 71 | |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 72 | inline char *modname_normalize(const char *modname, char buf[NAME_MAX], |
Lucas De Marchi | 6c343b1 | 2011-12-06 09:01:01 -0200 | [diff] [blame] | 73 | size_t *len) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 74 | { |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 75 | size_t s; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 76 | |
Gustavo Sverzut Barbieri | e1a6b30 | 2011-12-08 04:10:49 -0200 | [diff] [blame] | 77 | for (s = 0; s < NAME_MAX - 1; s++) { |
| 78 | const char c = modname[s]; |
| 79 | if (c == '-') |
| 80 | buf[s] = '_'; |
| 81 | else if (c == '\0' || c == '.') |
| 82 | break; |
| 83 | else |
| 84 | buf[s] = c; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 85 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 86 | |
Gustavo Sverzut Barbieri | e1a6b30 | 2011-12-08 04:10:49 -0200 | [diff] [blame] | 87 | buf[s] = '\0'; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 88 | |
| 89 | if (len) |
| 90 | *len = s; |
| 91 | |
Gustavo Sverzut Barbieri | e1a6b30 | 2011-12-08 04:10:49 -0200 | [diff] [blame] | 92 | return buf; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 93 | } |
| 94 | |
Lucas De Marchi | 6c343b1 | 2011-12-06 09:01:01 -0200 | [diff] [blame] | 95 | static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len) |
| 96 | { |
| 97 | char *modname; |
| 98 | |
| 99 | modname = basename(path); |
| 100 | if (modname == NULL || modname[0] == '\0') |
| 101 | return NULL; |
| 102 | |
| 103 | return modname_normalize(modname, buf, len); |
| 104 | } |
| 105 | |
Lucas De Marchi | c35347f | 2011-12-12 10:48:02 -0200 | [diff] [blame] | 106 | static inline const char *path_join(const char *path, size_t prefixlen, |
| 107 | char buf[PATH_MAX]) |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 108 | { |
| 109 | size_t pathlen; |
| 110 | |
| 111 | if (path[0] == '/') |
| 112 | return path; |
| 113 | |
| 114 | pathlen = strlen(path); |
| 115 | if (prefixlen + pathlen + 1 >= PATH_MAX) |
| 116 | return NULL; |
| 117 | |
| 118 | memcpy(buf + prefixlen, path, pathlen + 1); |
| 119 | return buf; |
| 120 | } |
| 121 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 122 | int kmod_module_parse_depline(struct kmod_module *mod, char *line) |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 123 | { |
| 124 | struct kmod_ctx *ctx = mod->ctx; |
| 125 | struct kmod_list *list = NULL; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 126 | const char *dirname; |
| 127 | char buf[PATH_MAX]; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 128 | char *p, *saveptr; |
Lucas De Marchi | 45f2778 | 2011-12-12 17:23:04 -0200 | [diff] [blame] | 129 | int err = 0, n = 0; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 130 | size_t dirnamelen; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 131 | |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 132 | if (mod->init.dep) |
| 133 | return mod->n_dep; |
| 134 | assert(mod->dep == NULL); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 135 | mod->init.dep = true; |
| 136 | |
| 137 | p = strchr(line, ':'); |
| 138 | if (p == NULL) |
| 139 | return 0; |
| 140 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 141 | *p = '\0'; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 142 | dirname = kmod_get_dirname(mod->ctx); |
| 143 | dirnamelen = strlen(dirname); |
| 144 | if (dirnamelen + 2 >= PATH_MAX) |
| 145 | return 0; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 146 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 147 | memcpy(buf, dirname, dirnamelen); |
| 148 | buf[dirnamelen] = '/'; |
| 149 | dirnamelen++; |
| 150 | buf[dirnamelen] = '\0'; |
| 151 | |
| 152 | if (mod->path == NULL) { |
| 153 | const char *str = path_join(line, dirnamelen, buf); |
| 154 | if (str == NULL) |
| 155 | return 0; |
| 156 | mod->path = strdup(str); |
| 157 | if (mod->path == NULL) |
| 158 | return 0; |
| 159 | } |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 160 | |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 161 | p++; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 162 | for (p = strtok_r(p, " \t", &saveptr); p != NULL; |
| 163 | p = strtok_r(NULL, " \t", &saveptr)) { |
Lucas De Marchi | 1fc1c9a | 2011-12-02 10:00:03 -0200 | [diff] [blame] | 164 | struct kmod_module *depmod; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 165 | const char *path; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 166 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 167 | path = path_join(p, dirnamelen, buf); |
| 168 | if (path == NULL) { |
| 169 | ERR(ctx, "could not join path '%s' and '%s'.\n", |
| 170 | dirname, p); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 171 | goto fail; |
| 172 | } |
| 173 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 174 | err = kmod_module_new_from_path(ctx, path, &depmod); |
| 175 | if (err < 0) { |
| 176 | ERR(ctx, "ctx=%p path=%s error=%s\n", |
| 177 | ctx, path, strerror(-err)); |
| 178 | goto fail; |
| 179 | } |
| 180 | |
| 181 | DBG(ctx, "add dep: %s\n", path); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 182 | |
Lucas De Marchi | 1fc1c9a | 2011-12-02 10:00:03 -0200 | [diff] [blame] | 183 | list = kmod_list_append(list, depmod); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 184 | n++; |
| 185 | } |
| 186 | |
| 187 | DBG(ctx, "%d dependencies for %s\n", n, mod->name); |
| 188 | |
| 189 | mod->dep = list; |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 190 | mod->n_dep = n; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 191 | return n; |
| 192 | |
| 193 | fail: |
| 194 | kmod_module_unref_list(list); |
| 195 | mod->init.dep = false; |
| 196 | return err; |
| 197 | } |
| 198 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 199 | /** |
| 200 | * kmod_module_new_from_name: |
| 201 | * @ctx: kmod library context |
| 202 | * @name: name of the module |
| 203 | * @mod: where to save the created struct kmod_module |
| 204 | * |
| 205 | * Create a new struct kmod_module using the module name. @name can not be an |
| 206 | * alias, file name or anything else; it must be a module name. There's no |
| 207 | * check if the module does exists in the system. |
| 208 | * |
| 209 | * This function is also used internally by many others that return a new |
| 210 | * struct kmod_module or a new list of modules. |
| 211 | * |
| 212 | * The initial refcount is 1, and needs to be decremented to release the |
| 213 | * resources of the kmod_module. Since libkmod keeps track of all |
| 214 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 215 | * not unref @ctx before all the desired operations with the returned |
| 216 | * kmod_module are done. |
| 217 | * |
| 218 | * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid |
| 219 | * module name or if memory allocation failed. |
| 220 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 221 | KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx, |
| 222 | const char *name, |
| 223 | struct kmod_module **mod) |
| 224 | { |
| 225 | struct kmod_module *m; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 226 | size_t namelen; |
Lucas De Marchi | 4f2bb7c | 2011-12-06 02:46:22 -0200 | [diff] [blame] | 227 | char name_norm[NAME_MAX]; |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 228 | char *namesep; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 229 | |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 230 | if (ctx == NULL || name == NULL || mod == NULL) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 231 | return -ENOENT; |
| 232 | |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 233 | alias_normalize(name, name_norm, &namelen); |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 234 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 235 | m = kmod_pool_get_module(ctx, name_norm); |
| 236 | if (m != NULL) { |
| 237 | *mod = kmod_module_ref(m); |
| 238 | return 0; |
| 239 | } |
| 240 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 241 | namesep = strchr(name_norm, '/'); |
| 242 | m = malloc(sizeof(*m) + (namesep == NULL ? 1 : 2) * namelen + 2); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 243 | if (m == NULL) { |
| 244 | free(m); |
| 245 | return -ENOMEM; |
| 246 | } |
| 247 | |
Lucas De Marchi | 788ef0f | 2011-12-14 15:05:03 -0200 | [diff] [blame] | 248 | memset(m, 0, sizeof(*m)); |
| 249 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 250 | m->ctx = kmod_ref(ctx); |
Lucas De Marchi | 219f9c3 | 2011-12-13 13:07:40 -0200 | [diff] [blame] | 251 | m->name = (char *)m + sizeof(*m); |
Lucas De Marchi | 4f2bb7c | 2011-12-06 02:46:22 -0200 | [diff] [blame] | 252 | memcpy(m->name, name_norm, namelen + 1); |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 253 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 254 | if (namesep) { |
| 255 | size_t len = namesep - name_norm; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 256 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 257 | m->name[len] = '\0'; |
| 258 | m->alias = m->name + len + 1; |
| 259 | m->hashkey = m->name + namelen + 1; |
| 260 | memcpy(m->hashkey, name_norm, namelen + 1); |
| 261 | } else { |
| 262 | m->hashkey = m->name; |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 263 | } |
| 264 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 265 | m->refcount = 1; |
| 266 | kmod_pool_add_module(ctx, m, m->hashkey); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 267 | *mod = m; |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 272 | int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias, |
| 273 | const char *name, struct kmod_module **mod) |
| 274 | { |
| 275 | int err; |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 276 | char key[NAME_MAX]; |
| 277 | size_t namelen = strlen(name); |
| 278 | size_t aliaslen = strlen(alias); |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 279 | |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 280 | if (namelen + aliaslen + 2 > NAME_MAX) |
| 281 | return -ENAMETOOLONG; |
| 282 | |
| 283 | memcpy(key, name, namelen); |
| 284 | memcpy(key + namelen + 1, alias, aliaslen + 1); |
| 285 | key[namelen] = '/'; |
| 286 | |
| 287 | err = kmod_module_new_from_name(ctx, key, mod); |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 288 | if (err < 0) |
| 289 | return err; |
| 290 | |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 291 | return 0; |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 292 | } |
| 293 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 294 | /** |
| 295 | * kmod_module_new_from_path: |
| 296 | * @ctx: kmod library context |
| 297 | * @path: path where to find the given module |
| 298 | * @mod: where to save the created struct kmod_module |
| 299 | * |
| 300 | * Create a new struct kmod_module using the module path. @path must be an |
| 301 | * existent file with in the filesystem and must be accessible to libkmod. |
| 302 | * |
| 303 | * The initial refcount is 1, and needs to be decremented to release the |
| 304 | * resources of the kmod_module. Since libkmod keeps track of all |
| 305 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 306 | * not unref @ctx before all the desired operations with the returned |
| 307 | * kmod_module are done. |
| 308 | * |
| 309 | * If @path is relative, it's treated as relative to the current working |
| 310 | * directory. Otherwise, give an absolute path. |
| 311 | * |
| 312 | * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if |
| 313 | * it's not a valid file for a kmod_module or if memory allocation failed. |
| 314 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 315 | KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx, |
| 316 | const char *path, |
| 317 | struct kmod_module **mod) |
| 318 | { |
| 319 | struct kmod_module *m; |
| 320 | int err; |
| 321 | struct stat st; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 322 | char name[NAME_MAX]; |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 323 | char *abspath; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 324 | size_t namelen; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 325 | |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 326 | if (ctx == NULL || path == NULL || mod == NULL) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 327 | return -ENOENT; |
| 328 | |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 329 | abspath = path_make_absolute_cwd(path); |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 330 | if (abspath == NULL) { |
| 331 | DBG(ctx, "no absolute path for %s\n", path); |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 332 | return -ENOMEM; |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 333 | } |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 334 | |
| 335 | err = stat(abspath, &st); |
| 336 | if (err < 0) { |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 337 | err = -errno; |
| 338 | DBG(ctx, "stat %s: %s\n", path, strerror(errno)); |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 339 | free(abspath); |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 340 | return err; |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 341 | } |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 342 | |
Gustavo Sverzut Barbieri | 973c80b | 2011-12-12 18:28:52 -0200 | [diff] [blame] | 343 | if (path_to_modname(path, name, &namelen) == NULL) { |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 344 | DBG(ctx, "could not get modname from path %s\n", path); |
Gustavo Sverzut Barbieri | 973c80b | 2011-12-12 18:28:52 -0200 | [diff] [blame] | 345 | free(abspath); |
| 346 | return -ENOENT; |
| 347 | } |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 348 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 349 | m = kmod_pool_get_module(ctx, name); |
| 350 | if (m != NULL) { |
Lucas De Marchi | 6bd0b8d | 2011-12-07 14:08:01 -0200 | [diff] [blame] | 351 | if (m->path == NULL) |
| 352 | m->path = abspath; |
| 353 | else if (streq(m->path, abspath)) |
| 354 | free(abspath); |
| 355 | else { |
| 356 | ERR(ctx, "kmod_module '%s' already exists with different path\n", |
| 357 | name); |
| 358 | free(abspath); |
| 359 | return -EEXIST; |
| 360 | } |
| 361 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 362 | *mod = kmod_module_ref(m); |
| 363 | return 0; |
| 364 | } |
| 365 | |
Lucas De Marchi | 788ef0f | 2011-12-14 15:05:03 -0200 | [diff] [blame] | 366 | m = malloc(sizeof(*m) + namelen + 1); |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 367 | if (m == NULL) |
| 368 | return -errno; |
| 369 | |
Lucas De Marchi | 788ef0f | 2011-12-14 15:05:03 -0200 | [diff] [blame] | 370 | memset(m, 0, sizeof(*m)); |
| 371 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 372 | m->ctx = kmod_ref(ctx); |
Lucas De Marchi | 219f9c3 | 2011-12-13 13:07:40 -0200 | [diff] [blame] | 373 | m->name = (char *)m + sizeof(*m); |
Lucas De Marchi | 9dec244 | 2011-12-18 15:12:19 -0200 | [diff] [blame] | 374 | memcpy(m->name, name, namelen + 1); |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 375 | m->path = abspath; |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 376 | m->hashkey = m->name; |
Gustavo Sverzut Barbieri | 87ca03b | 2011-12-04 12:34:02 -0200 | [diff] [blame] | 377 | m->refcount = 1; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 378 | |
Lucas De Marchi | 8bdeca1 | 2011-12-15 13:11:51 -0200 | [diff] [blame] | 379 | kmod_pool_add_module(ctx, m, m->hashkey); |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 380 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 381 | *mod = m; |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 386 | /** |
| 387 | * kmod_module_unref: |
| 388 | * @mod: kmod module |
| 389 | * |
| 390 | * Drop a reference of the kmod module. If the refcount reaches zero, its |
| 391 | * resources are released. |
| 392 | * |
| 393 | * Returns: NULL if @mod is NULL or if the module was released. Otherwise it |
| 394 | * returns the passed @mod with its refcount decremented. |
| 395 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 396 | KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod) |
| 397 | { |
| 398 | if (mod == NULL) |
| 399 | return NULL; |
| 400 | |
| 401 | if (--mod->refcount > 0) |
| 402 | return mod; |
| 403 | |
| 404 | DBG(mod->ctx, "kmod_module %p released\n", mod); |
| 405 | |
Lucas De Marchi | 4084c17 | 2011-12-15 13:43:22 -0200 | [diff] [blame] | 406 | kmod_pool_del_module(mod->ctx, mod, mod->hashkey); |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 407 | kmod_module_unref_list(mod->softdeps.pre); |
| 408 | kmod_module_unref_list(mod->softdeps.post); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 409 | kmod_module_unref_list(mod->dep); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 410 | kmod_unref(mod->ctx); |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 411 | free(mod->options); |
Gustavo Sverzut Barbieri | f1fb6f8 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 412 | free(mod->path); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 413 | free(mod); |
| 414 | return NULL; |
| 415 | } |
| 416 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 417 | /** |
| 418 | * kmod_module_ref: |
| 419 | * @mod: kmod module |
| 420 | * |
| 421 | * Take a reference of the kmod module, incrementing its refcount. |
| 422 | * |
| 423 | * Returns: the passed @module with its refcount incremented. |
| 424 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 425 | KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod) |
| 426 | { |
| 427 | if (mod == NULL) |
| 428 | return NULL; |
| 429 | |
| 430 | mod->refcount++; |
| 431 | |
| 432 | return mod; |
| 433 | } |
| 434 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 435 | #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \ |
| 436 | do { \ |
| 437 | if ((_err) < 0) \ |
| 438 | goto _label_err; \ |
| 439 | if (*(_list) != NULL) \ |
| 440 | goto finish; \ |
| 441 | } while (0) |
| 442 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 443 | /** |
| 444 | * kmod_module_new_from_lookup: |
| 445 | * @ctx: kmod library context |
| 446 | * @given_alias: alias to look for |
| 447 | * @list: an empty list where to save the list of modules matching |
| 448 | * @given_alias |
| 449 | * |
| 450 | * Create a new list of kmod modules using an alias or module name and lookup |
| 451 | * libkmod's configuration files and indexes in order to find the module. |
| 452 | * Once it's found in one of the places, it stops searching and create the |
| 453 | * list of modules that is saved in @list. |
| 454 | * |
| 455 | * The search order is: 1. aliases in configuration file; 2. module names in |
| 456 | * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases |
| 457 | * in modules.alias index. |
| 458 | * |
| 459 | * The initial refcount is 1, and needs to be decremented to release the |
| 460 | * resources of the kmod_module. The returned @list must be released by |
| 461 | * calling kmod_module_unref_list(). Since libkmod keeps track of all |
| 462 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 463 | * not unref @ctx before all the desired operations with the returned list are |
| 464 | * completed. |
| 465 | * |
| 466 | * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup |
| 467 | * methods failed, which is basically due to memory allocation fail. If module |
| 468 | * is not found, it still returns 0, but @list is an empty list. |
| 469 | */ |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 470 | KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx, |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 471 | const char *given_alias, |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 472 | struct kmod_list **list) |
| 473 | { |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 474 | int err; |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 475 | char alias[NAME_MAX]; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 476 | |
Lucas De Marchi | 4308b17 | 2011-12-13 10:26:04 -0200 | [diff] [blame] | 477 | if (ctx == NULL || given_alias == NULL) |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 478 | return -ENOENT; |
| 479 | |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 480 | if (list == NULL || *list != NULL) { |
| 481 | ERR(ctx, "An empty list is needed to create lookup\n"); |
| 482 | return -ENOSYS; |
| 483 | } |
| 484 | |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 485 | if (alias_normalize(given_alias, alias, NULL) < 0) { |
| 486 | DBG(ctx, "invalid alias: %s\n", given_alias); |
Lucas De Marchi | d470db1 | 2011-12-13 10:28:00 -0200 | [diff] [blame] | 487 | return -EINVAL; |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 488 | } |
| 489 | |
| 490 | DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias); |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 491 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 492 | /* Aliases from config file override all the others */ |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 493 | err = kmod_lookup_alias_from_config(ctx, alias, list); |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 494 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 495 | |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 496 | DBG(ctx, "lookup modules.dep %s\n", alias); |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 497 | err = kmod_lookup_alias_from_moddep_file(ctx, alias, list); |
| 498 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
| 499 | |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 500 | DBG(ctx, "lookup modules.symbols %s\n", alias); |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 501 | err = kmod_lookup_alias_from_symbols_file(ctx, alias, list); |
| 502 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 503 | |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 504 | DBG(ctx, "lookup install and remove commands %s\n", alias); |
Lucas De Marchi | f4fc552 | 2011-12-16 03:57:12 -0200 | [diff] [blame] | 505 | err = kmod_lookup_alias_from_commands(ctx, alias, list); |
| 506 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 507 | |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 508 | DBG(ctx, "lookup modules.aliases %s\n", alias); |
Lucas De Marchi | 49e61ca | 2011-12-01 16:27:04 -0200 | [diff] [blame] | 509 | err = kmod_lookup_alias_from_aliases_file(ctx, alias, list); |
| 510 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
| 511 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 512 | finish: |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 513 | DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list); |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 514 | return err; |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 515 | fail: |
Gustavo Sverzut Barbieri | b55df2e | 2011-12-20 13:04:10 -0200 | [diff] [blame^] | 516 | DBG(ctx, "Failed to lookup %s\n", alias); |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 517 | kmod_module_unref_list(*list); |
| 518 | *list = NULL; |
Lucas De Marchi | 84f4220 | 2011-12-02 10:03:34 -0200 | [diff] [blame] | 519 | return err; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 520 | } |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 521 | #undef CHECK_ERR_AND_FINISH |
| 522 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 523 | /** |
Lucas De Marchi | 91428ae | 2011-12-15 13:09:46 -0200 | [diff] [blame] | 524 | * kmod_module_unref_list: |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 525 | * @list: list of kmod modules |
| 526 | * |
| 527 | * Drop a reference of each kmod module in @list and releases the resources |
| 528 | * taken by the list itself. |
| 529 | * |
| 530 | * Returns: NULL if @mod is NULL or if the module was released. Otherwise it |
| 531 | * returns the passed @mod with its refcount decremented. |
| 532 | */ |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 533 | KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list) |
| 534 | { |
| 535 | for (; list != NULL; list = kmod_list_remove(list)) |
| 536 | kmod_module_unref(list->data); |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 541 | /** |
Lucas De Marchi | 91428ae | 2011-12-15 13:09:46 -0200 | [diff] [blame] | 542 | * kmod_module_get_dependencies: |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 543 | * @mod: kmod module |
| 544 | * |
| 545 | * Search the modules.dep index to find the dependencies of the given @mod. |
| 546 | * The result is cached in @mod, so subsequent calls to this function will |
| 547 | * return the already searched list of modules. |
| 548 | * |
| 549 | * Returns: NULL on failure or if there are any dependencies. Otherwise it |
| 550 | * returns a list of kmod modules that can be released by calling |
| 551 | * kmod_module_unref_list(). |
| 552 | */ |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 553 | KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod) |
Lucas De Marchi | 0835fc3 | 2011-12-01 20:06:08 -0200 | [diff] [blame] | 554 | { |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 555 | struct kmod_list *l, *l_new, *list_new = NULL; |
| 556 | |
| 557 | if (mod == NULL) |
| 558 | return NULL; |
| 559 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 560 | if (!mod->init.dep) { |
| 561 | /* lazy init */ |
| 562 | char *line = kmod_search_moddep(mod->ctx, mod->name); |
| 563 | |
| 564 | if (line == NULL) |
| 565 | return NULL; |
| 566 | |
| 567 | kmod_module_parse_depline((struct kmod_module *)mod, line); |
| 568 | free(line); |
| 569 | |
| 570 | if (!mod->init.dep) |
| 571 | return NULL; |
| 572 | } |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 573 | |
| 574 | kmod_list_foreach(l, mod->dep) { |
| 575 | l_new = kmod_list_append(list_new, kmod_module_ref(l->data)); |
| 576 | if (l_new == NULL) { |
| 577 | kmod_module_unref(l->data); |
| 578 | goto fail; |
| 579 | } |
| 580 | |
| 581 | list_new = l_new; |
| 582 | } |
| 583 | |
| 584 | return list_new; |
| 585 | |
| 586 | fail: |
| 587 | ERR(mod->ctx, "out of memory\n"); |
| 588 | kmod_module_unref_list(list_new); |
| 589 | return NULL; |
Lucas De Marchi | 0835fc3 | 2011-12-01 20:06:08 -0200 | [diff] [blame] | 590 | } |
| 591 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 592 | /** |
| 593 | * kmod_module_get_module: |
| 594 | * @entry: an entry in a list of kmod modules. |
| 595 | * |
| 596 | * Get the kmod module of this @entry in the list, increasing its refcount. |
| 597 | * After it's used, unref it. Since the refcount is incremented upon return, |
| 598 | * you still have to call kmod_module_unref_list() to release the list of kmod |
| 599 | * modules. |
| 600 | * |
| 601 | * Returns: NULL on failure or the kmod_module contained in this list entry |
| 602 | * with its refcount incremented. |
| 603 | */ |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 604 | KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry) |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 605 | { |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 606 | if (entry == NULL) |
| 607 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 608 | |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 609 | return kmod_module_ref(entry->data); |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 610 | } |
| 611 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 612 | /** |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 613 | * kmod_module_get_name: |
| 614 | * @mod: kmod module |
| 615 | * |
| 616 | * Get the name of this kmod module. Name is always available, independently |
| 617 | * if it was created by kmod_module_new_from_name() or another function and |
| 618 | * it's always normalized (dashes are replaced with underscores). |
| 619 | * |
| 620 | * Returns: the name of this kmod module. |
| 621 | */ |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 622 | KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod) |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 623 | { |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 624 | if (mod == NULL) |
| 625 | return NULL; |
| 626 | |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 627 | return mod->name; |
| 628 | } |
| 629 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 630 | /** |
| 631 | * kmod_module_get_path: |
| 632 | * @mod: kmod module |
| 633 | * |
| 634 | * Get the path of this kmod module. If this kmod module was not created by |
| 635 | * path, it can search the modules.dep index in order to find out the module |
| 636 | * under context's dirname (see kmod_get_dirname()). |
| 637 | * |
| 638 | * Returns: the path of this kmod module or NULL if such information is not |
| 639 | * available. |
| 640 | */ |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 641 | KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod) |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 642 | { |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 643 | char *line; |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 644 | |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 645 | if (mod == NULL) |
| 646 | return NULL; |
| 647 | |
Gustavo Sverzut Barbieri | d01c67e | 2011-12-11 19:42:02 -0200 | [diff] [blame] | 648 | DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path); |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 649 | |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 650 | if (mod->path != NULL) |
| 651 | return mod->path; |
| 652 | if (mod->init.dep) |
| 653 | return NULL; |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 654 | |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 655 | /* lazy init */ |
| 656 | line = kmod_search_moddep(mod->ctx, mod->name); |
| 657 | if (line == NULL) |
| 658 | return NULL; |
| 659 | |
| 660 | kmod_module_parse_depline((struct kmod_module *) mod, line); |
| 661 | free(line); |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 662 | |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 663 | return mod->path; |
| 664 | } |
| 665 | |
| 666 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 667 | extern long delete_module(const char *name, unsigned int flags); |
| 668 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 669 | /** |
| 670 | * kmod_module_remove_module: |
| 671 | * @mod: kmod module |
| 672 | * @flags: flags to pass to Linux kernel when removing the module |
| 673 | * |
| 674 | * Remove a module from Linux kernel. |
| 675 | * |
| 676 | * Returns: 0 on success or < 0 on failure. |
| 677 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 678 | KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, |
| 679 | unsigned int flags) |
| 680 | { |
| 681 | int err; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 682 | |
| 683 | if (mod == NULL) |
| 684 | return -ENOENT; |
| 685 | |
| 686 | /* Filter out other flags */ |
| 687 | flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT); |
| 688 | |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 689 | err = delete_module(mod->name, flags); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 690 | if (err != 0) { |
Lucas De Marchi | 63af061 | 2011-12-14 12:07:37 -0200 | [diff] [blame] | 691 | ERR(mod->ctx, "Could not remove '%s': %s\n", mod->name, |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 692 | strerror(-err)); |
| 693 | return err; |
| 694 | } |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 699 | extern long init_module(const void *mem, unsigned long len, const char *args); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 700 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 701 | /** |
| 702 | * kmod_module_insert_module: |
| 703 | * @mod: kmod module |
| 704 | * @flags: flags are not passed to Linux Kernel, but instead it dictates the |
| 705 | * behavior of this function. They are not implemented yet. |
| 706 | * @options: module's options to pass to Linux Kernel. |
| 707 | * |
| 708 | * Insert a module in Linux kernel. It opens the file pointed by @mod, |
| 709 | * mmap'ing it and passing to kernel. |
| 710 | * |
| 711 | * Returns: 0 on success or < 0 on failure. |
| 712 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 713 | KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod, |
Gustavo Sverzut Barbieri | 3a721bb | 2011-12-10 21:02:39 -0200 | [diff] [blame] | 714 | unsigned int flags, |
| 715 | const char *options) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 716 | { |
| 717 | int err; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 718 | const void *mem; |
Gustavo Sverzut Barbieri | 3d8226e | 2011-12-16 16:08:53 -0200 | [diff] [blame] | 719 | off_t size; |
| 720 | struct kmod_file *file; |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 721 | struct kmod_elf *elf = NULL; |
| 722 | const char *path; |
Gustavo Sverzut Barbieri | 3a721bb | 2011-12-10 21:02:39 -0200 | [diff] [blame] | 723 | const char *args = options ? options : ""; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 724 | |
| 725 | if (mod == NULL) |
| 726 | return -ENOENT; |
| 727 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 728 | path = kmod_module_get_path(mod); |
| 729 | if (path == NULL) { |
Lucas De Marchi | 1b2e26a | 2011-11-25 01:28:39 -0200 | [diff] [blame] | 730 | ERR(mod->ctx, "Not supported to load a module by name yet\n"); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 731 | return -ENOSYS; |
| 732 | } |
| 733 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 734 | file = kmod_file_open(path); |
Gustavo Sverzut Barbieri | 3d8226e | 2011-12-16 16:08:53 -0200 | [diff] [blame] | 735 | if (file == NULL) { |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 736 | err = -errno; |
| 737 | return err; |
| 738 | } |
| 739 | |
Gustavo Sverzut Barbieri | 3d8226e | 2011-12-16 16:08:53 -0200 | [diff] [blame] | 740 | size = kmod_file_get_size(file); |
| 741 | mem = kmod_file_get_contents(file); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 742 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 743 | if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) { |
| 744 | elf = kmod_elf_new(mem, size); |
| 745 | if (elf == NULL) { |
| 746 | err = -errno; |
| 747 | goto elf_failed; |
| 748 | } |
| 749 | |
| 750 | if (flags & KMOD_INSERT_FORCE_MODVERSION) { |
| 751 | err = kmod_elf_strip_section(elf, "__versions"); |
| 752 | if (err < 0) |
| 753 | INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err)); |
| 754 | } |
| 755 | |
| 756 | if (flags & KMOD_INSERT_FORCE_VERMAGIC) { |
| 757 | err = kmod_elf_strip_vermagic(elf); |
| 758 | if (err < 0) |
| 759 | INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err)); |
| 760 | } |
| 761 | |
| 762 | mem = kmod_elf_get_memory(elf); |
| 763 | } |
| 764 | |
Gustavo Sverzut Barbieri | 3d8226e | 2011-12-16 16:08:53 -0200 | [diff] [blame] | 765 | err = init_module(mem, size, args); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 766 | if (err < 0) |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 767 | ERR(mod->ctx, "Failed to insert module '%s'\n", path); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 768 | |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 769 | if (elf != NULL) |
| 770 | kmod_elf_unref(elf); |
| 771 | elf_failed: |
Gustavo Sverzut Barbieri | 3d8226e | 2011-12-16 16:08:53 -0200 | [diff] [blame] | 772 | kmod_file_unref(file); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 773 | |
| 774 | return err; |
| 775 | } |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 776 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 777 | /** |
| 778 | * kmod_module_get_options: |
| 779 | * @mod: kmod module |
| 780 | * |
| 781 | * Get options of this kmod module. Options come from the configuration file |
| 782 | * and are cached in @mod. The first call to this function will search for |
| 783 | * this module in configuration and subsequent calls return the cached string. |
| 784 | * |
| 785 | * Returns: a string with all the options separated by spaces. This string is |
| 786 | * owned by @mod, do not free it. |
| 787 | */ |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 788 | KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod) |
| 789 | { |
| 790 | if (mod == NULL) |
| 791 | return NULL; |
| 792 | |
| 793 | if (!mod->init.options) { |
| 794 | /* lazy init */ |
| 795 | struct kmod_module *m = (struct kmod_module *)mod; |
| 796 | const struct kmod_list *l, *ctx_options; |
| 797 | char *opts = NULL; |
| 798 | size_t optslen = 0; |
| 799 | |
| 800 | ctx_options = kmod_get_options(mod->ctx); |
| 801 | |
| 802 | kmod_list_foreach(l, ctx_options) { |
| 803 | const char *modname = kmod_option_get_modname(l); |
| 804 | const char *str; |
| 805 | size_t len; |
| 806 | void *tmp; |
| 807 | |
Lucas De Marchi | 07b8c82 | 2011-12-13 14:21:24 -0200 | [diff] [blame] | 808 | DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias); |
| 809 | if (!(streq(modname, mod->name) || (mod->alias != NULL && |
| 810 | streq(modname, mod->alias)))) |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 811 | continue; |
| 812 | |
Lucas De Marchi | 07b8c82 | 2011-12-13 14:21:24 -0200 | [diff] [blame] | 813 | DBG(mod->ctx, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 814 | str = kmod_option_get_options(l); |
| 815 | len = strlen(str); |
| 816 | if (len < 1) |
| 817 | continue; |
| 818 | |
| 819 | tmp = realloc(opts, optslen + len + 2); |
| 820 | if (tmp == NULL) { |
| 821 | free(opts); |
| 822 | goto failed; |
| 823 | } |
| 824 | |
| 825 | opts = tmp; |
| 826 | |
| 827 | if (optslen > 0) { |
| 828 | opts[optslen] = ' '; |
| 829 | optslen++; |
| 830 | } |
| 831 | |
| 832 | memcpy(opts + optslen, str, len); |
| 833 | optslen += len; |
| 834 | opts[optslen] = '\0'; |
| 835 | } |
| 836 | |
| 837 | m->init.options = true; |
| 838 | m->options = opts; |
| 839 | } |
| 840 | |
| 841 | return mod->options; |
| 842 | |
| 843 | failed: |
| 844 | ERR(mod->ctx, "out of memory\n"); |
| 845 | return NULL; |
| 846 | } |
| 847 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 848 | /** |
| 849 | * kmod_module_get_install_commands: |
| 850 | * @mod: kmod module |
| 851 | * |
| 852 | * Get install commands for this kmod module. Install commands come from the |
| 853 | * configuration file and are cached in @mod. The first call to this function |
| 854 | * will search for this module in configuration and subsequent calls return |
| 855 | * the cached string. The install commands are returned as they were in the |
| 856 | * configuration, concatenated by ';'. No other processing is made in this |
| 857 | * string. |
| 858 | * |
| 859 | * Returns: a string with all install commands separated by semicolons. This |
| 860 | * string is owned by @mod, do not free it. |
| 861 | */ |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 862 | KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod) |
| 863 | { |
| 864 | if (mod == NULL) |
| 865 | return NULL; |
| 866 | |
| 867 | if (!mod->init.install_commands) { |
| 868 | /* lazy init */ |
| 869 | struct kmod_module *m = (struct kmod_module *)mod; |
| 870 | const struct kmod_list *l, *ctx_install_commands; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 871 | |
| 872 | ctx_install_commands = kmod_get_install_commands(mod->ctx); |
| 873 | |
| 874 | kmod_list_foreach(l, ctx_install_commands) { |
| 875 | const char *modname = kmod_command_get_modname(l); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 876 | |
Gustavo Sverzut Barbieri | a6bf249 | 2011-12-16 22:43:04 -0200 | [diff] [blame] | 877 | if (fnmatch(modname, mod->name, 0) != 0) |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 878 | continue; |
| 879 | |
Lucas De Marchi | 60f6760 | 2011-12-16 03:33:26 -0200 | [diff] [blame] | 880 | m->install_commands = kmod_command_get_command(l); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 881 | |
Lucas De Marchi | 60f6760 | 2011-12-16 03:33:26 -0200 | [diff] [blame] | 882 | /* |
| 883 | * find only the first command, as modprobe from |
| 884 | * module-init-tools does |
| 885 | */ |
| 886 | break; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | m->init.install_commands = true; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | return mod->install_commands; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 893 | } |
| 894 | |
Lucas De Marchi | f4fc552 | 2011-12-16 03:57:12 -0200 | [diff] [blame] | 895 | void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd) |
| 896 | { |
| 897 | mod->init.install_commands = true; |
| 898 | mod->install_commands = cmd; |
| 899 | } |
| 900 | |
Gustavo Sverzut Barbieri | 1c52260 | 2011-12-16 21:18:10 -0200 | [diff] [blame] | 901 | static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count) |
| 902 | { |
| 903 | struct kmod_list *ret = NULL; |
| 904 | unsigned i; |
| 905 | |
| 906 | for (i = 0; i < count; i++) { |
| 907 | const char *depname = array[i]; |
| 908 | struct kmod_list *lst = NULL; |
| 909 | int err; |
| 910 | |
| 911 | err = kmod_module_new_from_lookup(ctx, depname, &lst); |
| 912 | if (err < 0) { |
| 913 | ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname); |
| 914 | continue; |
| 915 | } else if (lst != NULL) |
| 916 | ret = kmod_list_append_list(ret, lst); |
| 917 | } |
| 918 | return ret; |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * kmod_module_get_softdeps: |
| 923 | * @mod: kmod module |
| 924 | * @pre: where to save the list of preceding soft dependencies. |
| 925 | * @post: where to save the list of post soft dependencies. |
| 926 | * |
| 927 | * Get soft dependencies for this kmod module. Soft dependencies come |
| 928 | * from configuration file and are cached in @mod. The first call |
| 929 | * to this function will search for this module in configuration and |
| 930 | * subsequent calls return the known results. |
| 931 | * |
| 932 | * Both @pre and @post are newly created list of kmod_module and |
| 933 | * should be unreferenced with kmod_module_unref_list(). |
| 934 | * |
| 935 | * Returns: 0 on success or < 0 otherwise. |
| 936 | */ |
| 937 | KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod, struct kmod_list **pre, struct kmod_list **post) |
| 938 | { |
| 939 | const struct kmod_list *l; |
| 940 | struct kmod_list *l_new; |
| 941 | |
| 942 | if (mod == NULL || pre == NULL || post == NULL) |
| 943 | return -ENOENT; |
| 944 | |
| 945 | assert(*pre == NULL); |
| 946 | assert(*post == NULL); |
| 947 | |
| 948 | if (!mod->init.softdeps) { |
| 949 | /* lazy init */ |
| 950 | struct kmod_module *m = (struct kmod_module *)mod; |
| 951 | const struct kmod_list *ctx_softdeps; |
| 952 | |
| 953 | ctx_softdeps = kmod_get_softdeps(mod->ctx); |
| 954 | |
| 955 | kmod_list_foreach(l, ctx_softdeps) { |
| 956 | const char *modname = kmod_softdep_get_name(l); |
| 957 | const char * const *array; |
| 958 | unsigned count; |
| 959 | |
| 960 | if (fnmatch(modname, mod->name, 0) != 0) |
| 961 | continue; |
| 962 | |
| 963 | array = kmod_softdep_get_pre(l, &count); |
| 964 | m->softdeps.pre = lookup_softdep(mod->ctx, array, count); |
| 965 | array = kmod_softdep_get_post(l, &count); |
| 966 | m->softdeps.post = lookup_softdep(mod->ctx, array, count); |
| 967 | /* |
| 968 | * find only the first command, as modprobe from |
| 969 | * module-init-tools does |
| 970 | */ |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | m->init.softdeps = true; |
| 975 | } |
| 976 | |
| 977 | kmod_list_foreach(l, mod->softdeps.pre) { |
| 978 | l_new = kmod_list_append(*pre, kmod_module_ref(l->data)); |
| 979 | if (l_new == NULL) { |
| 980 | kmod_module_unref(l->data); |
| 981 | goto fail; |
| 982 | } |
| 983 | *pre = l_new; |
| 984 | } |
| 985 | |
| 986 | kmod_list_foreach(l, mod->softdeps.post) { |
| 987 | l_new = kmod_list_append(*post, kmod_module_ref(l->data)); |
| 988 | if (l_new == NULL) { |
| 989 | kmod_module_unref(l->data); |
| 990 | goto fail; |
| 991 | } |
| 992 | *post = l_new; |
| 993 | } |
| 994 | |
| 995 | return 0; |
| 996 | |
| 997 | fail: |
| 998 | kmod_module_unref_list(*pre); |
| 999 | *pre = NULL; |
| 1000 | kmod_module_unref_list(*post); |
| 1001 | *post = NULL; |
| 1002 | return -ENOMEM; |
| 1003 | } |
| 1004 | |
| 1005 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1006 | /** |
| 1007 | * kmod_module_get_remove_commands: |
| 1008 | * @mod: kmod module |
| 1009 | * |
| 1010 | * Get remove commands for this kmod module. Remove commands come from the |
| 1011 | * configuration file and are cached in @mod. The first call to this function |
| 1012 | * will search for this module in configuration and subsequent calls return |
| 1013 | * the cached string. The remove commands are returned as they were in the |
| 1014 | * configuration, concatenated by ';'. No other processing is made in this |
| 1015 | * string. |
| 1016 | * |
| 1017 | * Returns: a string with all remove commands separated by semicolons. This |
| 1018 | * string is owned by @mod, do not free it. |
| 1019 | */ |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1020 | KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod) |
| 1021 | { |
| 1022 | if (mod == NULL) |
| 1023 | return NULL; |
| 1024 | |
| 1025 | if (!mod->init.remove_commands) { |
| 1026 | /* lazy init */ |
| 1027 | struct kmod_module *m = (struct kmod_module *)mod; |
| 1028 | const struct kmod_list *l, *ctx_remove_commands; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1029 | |
| 1030 | ctx_remove_commands = kmod_get_remove_commands(mod->ctx); |
| 1031 | |
| 1032 | kmod_list_foreach(l, ctx_remove_commands) { |
| 1033 | const char *modname = kmod_command_get_modname(l); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1034 | |
Gustavo Sverzut Barbieri | a6bf249 | 2011-12-16 22:43:04 -0200 | [diff] [blame] | 1035 | if (fnmatch(modname, mod->name, 0) != 0) |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1036 | continue; |
| 1037 | |
Lucas De Marchi | 60f6760 | 2011-12-16 03:33:26 -0200 | [diff] [blame] | 1038 | m->remove_commands = kmod_command_get_command(l); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1039 | |
Lucas De Marchi | 60f6760 | 2011-12-16 03:33:26 -0200 | [diff] [blame] | 1040 | /* |
| 1041 | * find only the first command, as modprobe from |
| 1042 | * module-init-tools does |
| 1043 | */ |
| 1044 | break; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | m->init.remove_commands = true; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | return mod->remove_commands; |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1051 | } |
| 1052 | |
Lucas De Marchi | f4fc552 | 2011-12-16 03:57:12 -0200 | [diff] [blame] | 1053 | void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd) |
| 1054 | { |
| 1055 | mod->init.remove_commands = true; |
| 1056 | mod->remove_commands = cmd; |
| 1057 | } |
| 1058 | |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1059 | /** |
| 1060 | * SECTION:libkmod-loaded |
| 1061 | * @short_description: currently loaded modules |
| 1062 | * |
| 1063 | * Information about currently loaded modules, as reported by Linux kernel. |
| 1064 | * These information are not cached by libkmod and are always read from /sys |
| 1065 | * and /proc/modules. |
| 1066 | */ |
| 1067 | |
| 1068 | /** |
| 1069 | * kmod_module_new_from_loaded: |
| 1070 | * @ctx: kmod library context |
| 1071 | * @list: where to save the list of loaded modules |
| 1072 | * |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1073 | * Create a new list of kmod modules with all modules currently loaded in |
| 1074 | * kernel. It uses /proc/modules to get the names of loaded modules and to |
| 1075 | * create kmod modules by calling kmod_module_new_from_name() in each of them. |
| 1076 | * They are put are put in @list in no particular order. |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1077 | * |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1078 | * The initial refcount is 1, and needs to be decremented to release the |
| 1079 | * resources of the kmod_module. The returned @list must be released by |
| 1080 | * calling kmod_module_unref_list(). Since libkmod keeps track of all |
| 1081 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 1082 | * not unref @ctx before all the desired operations with the returned list are |
| 1083 | * completed. |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1084 | * |
| 1085 | * Returns: 0 on success or < 0 on error. |
| 1086 | */ |
| 1087 | KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx, |
| 1088 | struct kmod_list **list) |
| 1089 | { |
| 1090 | struct kmod_list *l = NULL; |
| 1091 | FILE *fp; |
| 1092 | char line[4096]; |
| 1093 | |
| 1094 | if (ctx == NULL || list == NULL) |
| 1095 | return -ENOENT; |
| 1096 | |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 1097 | fp = fopen("/proc/modules", "re"); |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 1098 | if (fp == NULL) { |
| 1099 | int err = -errno; |
| 1100 | ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno)); |
| 1101 | return err; |
| 1102 | } |
| 1103 | |
| 1104 | while (fgets(line, sizeof(line), fp)) { |
| 1105 | struct kmod_module *m; |
| 1106 | struct kmod_list *node; |
| 1107 | int err; |
| 1108 | char *saveptr, *name = strtok_r(line, " \t", &saveptr); |
| 1109 | |
| 1110 | err = kmod_module_new_from_name(ctx, name, &m); |
| 1111 | if (err < 0) { |
| 1112 | ERR(ctx, "could not get module from name '%s': %s\n", |
| 1113 | name, strerror(-err)); |
| 1114 | continue; |
| 1115 | } |
| 1116 | |
| 1117 | node = kmod_list_append(l, m); |
| 1118 | if (node) |
| 1119 | l = node; |
| 1120 | else { |
| 1121 | ERR(ctx, "out of memory\n"); |
| 1122 | kmod_module_unref(m); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | fclose(fp); |
| 1127 | *list = l; |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1132 | /** |
| 1133 | * kmod_module_initstate_str: |
| 1134 | * @state: the state as returned by kmod_module_get_initstate() |
| 1135 | * |
| 1136 | * Translate a initstate to a string. |
| 1137 | * |
| 1138 | * Returns: the string associated to the @state. This string is statically |
| 1139 | * allocated, do not free it. |
| 1140 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1141 | KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state) |
| 1142 | { |
| 1143 | switch (state) { |
| 1144 | case KMOD_MODULE_BUILTIN: |
| 1145 | return "builtin"; |
| 1146 | case KMOD_MODULE_LIVE: |
| 1147 | return "live"; |
| 1148 | case KMOD_MODULE_COMING: |
| 1149 | return "coming"; |
| 1150 | case KMOD_MODULE_GOING: |
| 1151 | return "going"; |
| 1152 | default: |
| 1153 | return NULL; |
| 1154 | } |
| 1155 | } |
| 1156 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1157 | /** |
| 1158 | * kmod_module_get_initstate: |
| 1159 | * @mod: kmod module |
| 1160 | * |
| 1161 | * Get the initstate of this @mod, as returned by Linux Kernel, by reading |
| 1162 | * /sys filesystem. |
| 1163 | * |
| 1164 | * Returns: < 0 on error or enum kmod_initstate if module is found in kernel. |
| 1165 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1166 | KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod) |
| 1167 | { |
| 1168 | char path[PATH_MAX], buf[32]; |
| 1169 | int fd, err, pathlen; |
| 1170 | |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 1171 | if (mod == NULL) |
| 1172 | return -ENOENT; |
| 1173 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1174 | pathlen = snprintf(path, sizeof(path), |
| 1175 | "/sys/module/%s/initstate", mod->name); |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 1176 | fd = open(path, O_RDONLY|O_CLOEXEC); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1177 | if (fd < 0) { |
| 1178 | err = -errno; |
| 1179 | |
| 1180 | if (pathlen > (int)sizeof("/initstate") - 1) { |
| 1181 | struct stat st; |
| 1182 | path[pathlen - (sizeof("/initstate") - 1)] = '\0'; |
| 1183 | if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) |
| 1184 | return KMOD_MODULE_BUILTIN; |
| 1185 | } |
| 1186 | |
Gustavo Sverzut Barbieri | 926f67a | 2011-12-11 19:33:03 -0200 | [diff] [blame] | 1187 | DBG(mod->ctx, "could not open '%s': %s\n", |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1188 | path, strerror(-err)); |
| 1189 | return err; |
| 1190 | } |
| 1191 | |
| 1192 | err = read_str_safe(fd, buf, sizeof(buf)); |
| 1193 | close(fd); |
| 1194 | if (err < 0) { |
| 1195 | ERR(mod->ctx, "could not read from '%s': %s\n", |
| 1196 | path, strerror(-err)); |
| 1197 | return err; |
| 1198 | } |
| 1199 | |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 1200 | if (streq(buf, "live\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1201 | return KMOD_MODULE_LIVE; |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 1202 | else if (streq(buf, "coming\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1203 | return KMOD_MODULE_COMING; |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 1204 | else if (streq(buf, "going\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1205 | return KMOD_MODULE_GOING; |
| 1206 | |
| 1207 | ERR(mod->ctx, "unknown %s: '%s'\n", path, buf); |
| 1208 | return -EINVAL; |
| 1209 | } |
| 1210 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1211 | /** |
Lucas De Marchi | 2d7bab5 | 2011-12-14 12:10:02 -0200 | [diff] [blame] | 1212 | * kmod_module_get_size: |
| 1213 | * @mod: kmod module |
| 1214 | * |
| 1215 | * Get the size of this kmod module as returned by Linux kernel. It reads the |
| 1216 | * file /proc/modules to search for this module and get its size. |
| 1217 | * |
| 1218 | * Returns: the size of this kmod module. |
| 1219 | */ |
| 1220 | KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod) |
| 1221 | { |
| 1222 | // FIXME TODO: this should be available from /sys/module/foo |
| 1223 | FILE *fp; |
| 1224 | char line[4096]; |
| 1225 | int lineno = 0; |
| 1226 | long size = -ENOENT; |
| 1227 | |
| 1228 | if (mod == NULL) |
| 1229 | return -ENOENT; |
| 1230 | |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 1231 | fp = fopen("/proc/modules", "re"); |
Lucas De Marchi | 2d7bab5 | 2011-12-14 12:10:02 -0200 | [diff] [blame] | 1232 | if (fp == NULL) { |
| 1233 | int err = -errno; |
| 1234 | ERR(mod->ctx, |
| 1235 | "could not open /proc/modules: %s\n", strerror(errno)); |
| 1236 | return err; |
| 1237 | } |
| 1238 | |
| 1239 | while (fgets(line, sizeof(line), fp)) { |
| 1240 | char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr); |
| 1241 | long value; |
| 1242 | |
| 1243 | lineno++; |
| 1244 | if (tok == NULL || !streq(tok, mod->name)) |
| 1245 | continue; |
| 1246 | |
| 1247 | tok = strtok_r(NULL, " \t", &saveptr); |
| 1248 | if (tok == NULL) { |
| 1249 | ERR(mod->ctx, |
| 1250 | "invalid line format at /proc/modules:%d\n", lineno); |
| 1251 | break; |
| 1252 | } |
| 1253 | |
| 1254 | value = strtol(tok, &endptr, 10); |
| 1255 | if (endptr == tok || *endptr != '\0') { |
| 1256 | ERR(mod->ctx, |
| 1257 | "invalid line format at /proc/modules:%d\n", lineno); |
| 1258 | break; |
| 1259 | } |
| 1260 | |
| 1261 | size = value; |
| 1262 | break; |
| 1263 | } |
| 1264 | fclose(fp); |
| 1265 | return size; |
| 1266 | } |
| 1267 | |
| 1268 | /** |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1269 | * kmod_module_get_refcnt: |
| 1270 | * @mod: kmod module |
| 1271 | * |
| 1272 | * Get the ref count of this @mod, as returned by Linux Kernel, by reading |
| 1273 | * /sys filesystem. |
| 1274 | * |
| 1275 | * Returns: 0 on success or < 0 on failure. |
| 1276 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1277 | KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) |
| 1278 | { |
| 1279 | char path[PATH_MAX]; |
| 1280 | long refcnt; |
| 1281 | int fd, err; |
| 1282 | |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 1283 | if (mod == NULL) |
| 1284 | return -ENOENT; |
| 1285 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1286 | snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name); |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 1287 | fd = open(path, O_RDONLY|O_CLOEXEC); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1288 | if (fd < 0) { |
| 1289 | err = -errno; |
| 1290 | ERR(mod->ctx, "could not open '%s': %s\n", |
| 1291 | path, strerror(errno)); |
| 1292 | return err; |
| 1293 | } |
| 1294 | |
| 1295 | err = read_str_long(fd, &refcnt, 10); |
| 1296 | close(fd); |
| 1297 | if (err < 0) { |
| 1298 | ERR(mod->ctx, "could not read integer from '%s': '%s'\n", |
| 1299 | path, strerror(-err)); |
| 1300 | return err; |
| 1301 | } |
| 1302 | |
| 1303 | return (int)refcnt; |
| 1304 | } |
| 1305 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1306 | /** |
| 1307 | * kmod_module_get_holders: |
| 1308 | * @mod: kmod module |
| 1309 | * |
| 1310 | * Get a list of kmod modules that are holding this @mod, as returned by Linux |
| 1311 | * Kernel. After use, free the @list by calling kmod_module_unref_list(). |
| 1312 | * |
| 1313 | * Returns: a new list of kmod modules on success or NULL on failure. |
| 1314 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1315 | KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod) |
| 1316 | { |
| 1317 | char dname[PATH_MAX]; |
| 1318 | struct kmod_list *list = NULL; |
| 1319 | DIR *d; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1320 | |
| 1321 | if (mod == NULL) |
| 1322 | return NULL; |
Lucas De Marchi | 818f8e8 | 2011-12-15 13:35:40 -0200 | [diff] [blame] | 1323 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1324 | snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name); |
| 1325 | |
| 1326 | d = opendir(dname); |
| 1327 | if (d == NULL) { |
| 1328 | ERR(mod->ctx, "could not open '%s': %s\n", |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1329 | dname, strerror(errno)); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1330 | return NULL; |
| 1331 | } |
| 1332 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1333 | for (;;) { |
| 1334 | struct dirent de, *entp; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1335 | struct kmod_module *holder; |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1336 | struct kmod_list *l; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1337 | int err; |
| 1338 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1339 | err = readdir_r(d, &de, &entp); |
| 1340 | if (err != 0) { |
| 1341 | ERR(mod->ctx, "could not iterate for module '%s': %s\n", |
| 1342 | mod->name, strerror(-err)); |
| 1343 | goto fail; |
| 1344 | } |
| 1345 | |
| 1346 | if (entp == NULL) |
| 1347 | break; |
| 1348 | |
| 1349 | if (de.d_name[0] == '.') { |
| 1350 | if (de.d_name[1] == '\0' || |
| 1351 | (de.d_name[1] == '.' && de.d_name[2] == '\0')) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1352 | continue; |
| 1353 | } |
| 1354 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1355 | err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1356 | if (err < 0) { |
| 1357 | ERR(mod->ctx, "could not create module for '%s': %s\n", |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1358 | de.d_name, strerror(-err)); |
| 1359 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1362 | l = kmod_list_append(list, holder); |
| 1363 | if (l != NULL) { |
| 1364 | list = l; |
| 1365 | } else { |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1366 | ERR(mod->ctx, "out of memory\n"); |
| 1367 | kmod_module_unref(holder); |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1368 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | closedir(d); |
| 1373 | return list; |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1374 | |
| 1375 | fail: |
| 1376 | closedir(d); |
| 1377 | kmod_module_unref_list(list); |
| 1378 | return NULL; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | struct kmod_module_section { |
| 1382 | unsigned long address; |
| 1383 | char name[]; |
| 1384 | }; |
| 1385 | |
| 1386 | static void kmod_module_section_free(struct kmod_module_section *section) |
| 1387 | { |
| 1388 | free(section); |
| 1389 | } |
| 1390 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1391 | /** |
| 1392 | * kmod_module_get_sections: |
| 1393 | * @mod: kmod module |
| 1394 | * |
| 1395 | * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The |
| 1396 | * structure contained in this list is internal to libkmod and their fields |
| 1397 | * can be obtained by calling kmod_module_section_get_name() and |
| 1398 | * kmod_module_section_get_address(). |
| 1399 | * |
| 1400 | * After use, free the @list by calling kmod_module_section_free_list(). |
| 1401 | * |
| 1402 | * Returns: a new list of kmod module sections on success or NULL on failure. |
| 1403 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1404 | KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod) |
| 1405 | { |
| 1406 | char dname[PATH_MAX]; |
| 1407 | struct kmod_list *list = NULL; |
| 1408 | DIR *d; |
| 1409 | int dfd; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1410 | |
| 1411 | if (mod == NULL) |
| 1412 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1413 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1414 | snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name); |
| 1415 | |
| 1416 | d = opendir(dname); |
| 1417 | if (d == NULL) { |
| 1418 | ERR(mod->ctx, "could not open '%s': %s\n", |
| 1419 | dname, strerror(errno)); |
| 1420 | return NULL; |
| 1421 | } |
| 1422 | |
| 1423 | dfd = dirfd(d); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1424 | |
| 1425 | for (;;) { |
| 1426 | struct dirent de, *entp; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1427 | struct kmod_module_section *section; |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1428 | struct kmod_list *l; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1429 | unsigned long address; |
| 1430 | size_t namesz; |
| 1431 | int fd, err; |
| 1432 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1433 | err = readdir_r(d, &de, &entp); |
| 1434 | if (err != 0) { |
| 1435 | ERR(mod->ctx, "could not iterate for module '%s': %s\n", |
| 1436 | mod->name, strerror(-err)); |
| 1437 | goto fail; |
| 1438 | } |
| 1439 | |
| 1440 | if (de.d_name[0] == '.') { |
| 1441 | if (de.d_name[1] == '\0' || |
| 1442 | (de.d_name[1] == '.' && de.d_name[2] == '\0')) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1443 | continue; |
| 1444 | } |
| 1445 | |
Cristian RodrÃguez | 8e3e583 | 2011-12-16 14:46:52 -0300 | [diff] [blame] | 1446 | fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1447 | if (fd < 0) { |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1448 | ERR(mod->ctx, "could not open '%s/%s': %m\n", |
| 1449 | dname, de.d_name); |
| 1450 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | err = read_str_ulong(fd, &address, 16); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1454 | close(fd); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1455 | if (err < 0) { |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1456 | ERR(mod->ctx, "could not read long from '%s/%s': %m\n", |
| 1457 | dname, de.d_name); |
| 1458 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1459 | } |
| 1460 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1461 | namesz = strlen(de.d_name) + 1; |
| 1462 | section = malloc(sizeof(*section) + namesz); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1463 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1464 | if (section == NULL) { |
| 1465 | ERR(mod->ctx, "out of memory\n"); |
| 1466 | goto fail; |
| 1467 | } |
| 1468 | |
| 1469 | section->address = address; |
| 1470 | memcpy(section->name, de.d_name, namesz); |
| 1471 | |
| 1472 | l = kmod_list_append(list, section); |
| 1473 | if (l != NULL) { |
| 1474 | list = l; |
| 1475 | } else { |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1476 | ERR(mod->ctx, "out of memory\n"); |
| 1477 | free(section); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1478 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1479 | } |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | closedir(d); |
| 1483 | return list; |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1484 | |
| 1485 | fail: |
| 1486 | closedir(d); |
| 1487 | kmod_module_unref_list(list); |
| 1488 | return NULL; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1489 | } |
| 1490 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1491 | /** |
| 1492 | * kmod_module_section_get_module_name: |
| 1493 | * @entry: a list entry representing a kmod module section |
| 1494 | * |
| 1495 | * Get the name of a kmod module section. |
| 1496 | * |
| 1497 | * After use, free the @list by calling kmod_module_section_free_list(). |
| 1498 | * |
| 1499 | * Returns: the name of this kmod module section on success or NULL on |
| 1500 | * failure. The string is owned by the section, do not free it. |
| 1501 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1502 | KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry) |
| 1503 | { |
| 1504 | struct kmod_module_section *section; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1505 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1506 | if (entry == NULL) |
| 1507 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1508 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1509 | section = entry->data; |
| 1510 | return section->name; |
| 1511 | } |
| 1512 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1513 | /** |
| 1514 | * kmod_module_section_get_address: |
| 1515 | * @entry: a list entry representing a kmod module section |
| 1516 | * |
| 1517 | * Get the address of a kmod module section. |
| 1518 | * |
| 1519 | * After use, free the @list by calling kmod_module_section_free_list(). |
| 1520 | * |
| 1521 | * Returns: the address of this kmod module section on success or ULONG_MAX |
| 1522 | * on failure. |
| 1523 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1524 | KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry) |
| 1525 | { |
| 1526 | struct kmod_module_section *section; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1527 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1528 | if (entry == NULL) |
| 1529 | return (unsigned long)-1; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1530 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1531 | section = entry->data; |
| 1532 | return section->address; |
| 1533 | } |
| 1534 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1535 | /** |
| 1536 | * kmod_module_section_free_list: |
| 1537 | * @list: kmod module section list |
| 1538 | * |
| 1539 | * Release the resources taken by @list |
| 1540 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1541 | KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list) |
| 1542 | { |
| 1543 | while (list) { |
| 1544 | kmod_module_section_free(list->data); |
| 1545 | list = kmod_list_remove(list); |
| 1546 | } |
| 1547 | } |
Gustavo Sverzut Barbieri | 708624a | 2011-12-18 01:25:06 -0200 | [diff] [blame] | 1548 | |
| 1549 | struct kmod_module_info { |
| 1550 | char *key; |
| 1551 | char value[]; |
| 1552 | }; |
| 1553 | |
| 1554 | static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen) |
| 1555 | { |
| 1556 | struct kmod_module_info *info; |
| 1557 | |
| 1558 | info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2); |
| 1559 | if (info == NULL) |
| 1560 | return NULL; |
| 1561 | |
| 1562 | info->key = (char *)info + sizeof(struct kmod_module_info) |
| 1563 | + valuelen + 1; |
| 1564 | memcpy(info->key, key, keylen); |
| 1565 | info->key[keylen] = '\0'; |
| 1566 | memcpy(info->value, value, valuelen); |
| 1567 | info->value[valuelen] = '\0'; |
| 1568 | return info; |
| 1569 | } |
| 1570 | |
| 1571 | static void kmod_module_info_free(struct kmod_module_info *info) |
| 1572 | { |
| 1573 | free(info); |
| 1574 | } |
| 1575 | |
| 1576 | /** |
| 1577 | * kmod_module_get_info: |
| 1578 | * @mod: kmod module |
| 1579 | * @list: where to return list of module information. Use |
| 1580 | * kmod_module_info_get_key() and |
| 1581 | * kmod_module_info_get_value(). Release this list with |
| 1582 | * kmod_module_info_unref_list() |
| 1583 | * |
| 1584 | * Get a list of entries in ELF section ".modinfo", these contain |
| 1585 | * alias, license, depends, vermagic and other keys with respective |
| 1586 | * values. |
| 1587 | * |
| 1588 | * After use, free the @list by calling kmod_module_info_free_list(). |
| 1589 | * |
| 1590 | * Returns: 0 on success or < 0 otherwise. |
| 1591 | */ |
| 1592 | KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list) |
| 1593 | { |
| 1594 | struct kmod_file *file; |
| 1595 | struct kmod_elf *elf; |
| 1596 | const char *path; |
| 1597 | const void *mem; |
| 1598 | char **strings; |
| 1599 | size_t size; |
| 1600 | int i, count, ret = 0; |
| 1601 | |
| 1602 | if (mod == NULL || list == NULL) |
| 1603 | return -ENOENT; |
| 1604 | |
| 1605 | assert(*list == NULL); |
| 1606 | |
| 1607 | path = kmod_module_get_path(mod); |
| 1608 | if (path == NULL) |
| 1609 | return -ENOENT; |
| 1610 | |
| 1611 | file = kmod_file_open(path); |
| 1612 | if (file == NULL) |
| 1613 | return -errno; |
| 1614 | |
| 1615 | size = kmod_file_get_size(file); |
| 1616 | mem = kmod_file_get_contents(file); |
| 1617 | |
| 1618 | elf = kmod_elf_new(mem, size); |
| 1619 | if (elf == NULL) { |
| 1620 | ret = -errno; |
| 1621 | goto elf_open_error; |
| 1622 | } |
| 1623 | |
| 1624 | count = kmod_elf_get_strings(elf, ".modinfo", &strings); |
| 1625 | if (count < 0) { |
| 1626 | ret = count; |
| 1627 | goto get_strings_error; |
| 1628 | } |
| 1629 | |
| 1630 | for (i = 0; i < count; i++) { |
| 1631 | struct kmod_module_info *info; |
| 1632 | struct kmod_list *n; |
| 1633 | const char *key, *value; |
| 1634 | size_t keylen, valuelen; |
| 1635 | |
| 1636 | key = strings[i]; |
| 1637 | value = strchr(key, '='); |
| 1638 | if (value == NULL) { |
| 1639 | keylen = strlen(key); |
| 1640 | valuelen = 0; |
| 1641 | } else { |
| 1642 | keylen = value - key; |
| 1643 | value++; |
| 1644 | valuelen = strlen(value); |
| 1645 | } |
| 1646 | |
| 1647 | info = kmod_module_info_new(key, keylen, value, valuelen); |
| 1648 | if (info == NULL) { |
| 1649 | ret = -errno; |
| 1650 | kmod_module_info_free_list(*list); |
| 1651 | *list = NULL; |
| 1652 | goto list_error; |
| 1653 | } |
| 1654 | |
| 1655 | n = kmod_list_append(*list, info); |
| 1656 | if (n != NULL) |
| 1657 | *list = n; |
| 1658 | else { |
| 1659 | kmod_module_info_free(info); |
| 1660 | kmod_module_info_free_list(*list); |
| 1661 | *list = NULL; |
| 1662 | ret = -ENOMEM; |
| 1663 | goto list_error; |
| 1664 | } |
| 1665 | } |
| 1666 | ret = count; |
| 1667 | |
| 1668 | list_error: |
| 1669 | free(strings); |
| 1670 | get_strings_error: |
| 1671 | kmod_elf_unref(elf); |
| 1672 | elf_open_error: |
| 1673 | kmod_file_unref(file); |
| 1674 | |
| 1675 | return ret; |
| 1676 | } |
| 1677 | |
| 1678 | /** |
| 1679 | * kmod_module_info_get_key: |
| 1680 | * @entry: a list entry representing a kmod module info |
| 1681 | * |
| 1682 | * Get the key of a kmod module info. |
| 1683 | * |
| 1684 | * Returns: the key of this kmod module info on success or NULL on |
| 1685 | * failure. The string is owned by the info, do not free it. |
| 1686 | */ |
| 1687 | KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry) |
| 1688 | { |
| 1689 | struct kmod_module_info *info; |
| 1690 | |
| 1691 | if (entry == NULL) |
| 1692 | return NULL; |
| 1693 | |
| 1694 | info = entry->data; |
| 1695 | return info->key; |
| 1696 | } |
| 1697 | |
| 1698 | /** |
| 1699 | * kmod_module_info_get_value: |
| 1700 | * @entry: a list entry representing a kmod module info |
| 1701 | * |
| 1702 | * Get the value of a kmod module info. |
| 1703 | * |
| 1704 | * Returns: the value of this kmod module info on success or NULL on |
| 1705 | * failure. The string is owned by the info, do not free it. |
| 1706 | */ |
| 1707 | KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry) |
| 1708 | { |
| 1709 | struct kmod_module_info *info; |
| 1710 | |
| 1711 | if (entry == NULL) |
| 1712 | return NULL; |
| 1713 | |
| 1714 | info = entry->data; |
| 1715 | return info->value; |
| 1716 | } |
| 1717 | |
| 1718 | /** |
| 1719 | * kmod_module_info_free_list: |
| 1720 | * @list: kmod module info list |
| 1721 | * |
| 1722 | * Release the resources taken by @list |
| 1723 | */ |
| 1724 | KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list) |
| 1725 | { |
| 1726 | while (list) { |
| 1727 | kmod_module_info_free(list->data); |
| 1728 | list = kmod_list_remove(list); |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | struct kmod_module_version { |
| 1733 | uint64_t crc; |
| 1734 | char symbol[]; |
| 1735 | }; |
| 1736 | |
| 1737 | static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol) |
| 1738 | { |
| 1739 | struct kmod_module_version *mv; |
| 1740 | size_t symbollen = strlen(symbol) + 1; |
| 1741 | |
| 1742 | mv = malloc(sizeof(struct kmod_module_version) + symbollen); |
| 1743 | if (mv == NULL) |
| 1744 | return NULL; |
| 1745 | |
| 1746 | mv->crc = crc; |
| 1747 | memcpy(mv->symbol, symbol, symbollen); |
| 1748 | return mv; |
| 1749 | } |
| 1750 | |
| 1751 | static void kmod_module_version_free(struct kmod_module_version *version) |
| 1752 | { |
| 1753 | free(version); |
| 1754 | } |
| 1755 | |
| 1756 | /** |
| 1757 | * kmod_module_get_versions: |
| 1758 | * @mod: kmod module |
| 1759 | * @list: where to return list of module versions. Use |
| 1760 | * kmod_module_versions_get_symbol() and |
| 1761 | * kmod_module_versions_get_crc(). Release this list with |
| 1762 | * kmod_module_versions_unref_list() |
| 1763 | * |
| 1764 | * Get a list of entries in ELF section "__versions". |
| 1765 | * |
| 1766 | * After use, free the @list by calling kmod_module_versions_free_list(). |
| 1767 | * |
| 1768 | * Returns: 0 on success or < 0 otherwise. |
| 1769 | */ |
| 1770 | KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list) |
| 1771 | { |
| 1772 | struct kmod_file *file; |
| 1773 | struct kmod_elf *elf; |
| 1774 | const char *path; |
| 1775 | const void *mem; |
| 1776 | struct kmod_modversion *versions; |
| 1777 | size_t size; |
| 1778 | int i, count, ret = 0; |
| 1779 | |
| 1780 | if (mod == NULL || list == NULL) |
| 1781 | return -ENOENT; |
| 1782 | |
| 1783 | assert(*list == NULL); |
| 1784 | |
| 1785 | path = kmod_module_get_path(mod); |
| 1786 | if (path == NULL) |
| 1787 | return -ENOENT; |
| 1788 | |
| 1789 | file = kmod_file_open(path); |
| 1790 | if (file == NULL) |
| 1791 | return -errno; |
| 1792 | |
| 1793 | size = kmod_file_get_size(file); |
| 1794 | mem = kmod_file_get_contents(file); |
| 1795 | |
| 1796 | elf = kmod_elf_new(mem, size); |
| 1797 | if (elf == NULL) { |
| 1798 | ret = -errno; |
| 1799 | goto elf_open_error; |
| 1800 | } |
| 1801 | |
| 1802 | count = kmod_elf_get_modversions(elf, &versions); |
| 1803 | if (count < 0) { |
| 1804 | ret = count; |
| 1805 | goto get_strings_error; |
| 1806 | } |
| 1807 | |
| 1808 | for (i = 0; i < count; i++) { |
| 1809 | struct kmod_module_version *mv; |
| 1810 | struct kmod_list *n; |
| 1811 | |
| 1812 | mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol); |
| 1813 | if (mv == NULL) { |
| 1814 | ret = -errno; |
| 1815 | kmod_module_versions_free_list(*list); |
| 1816 | *list = NULL; |
| 1817 | goto list_error; |
| 1818 | } |
| 1819 | |
| 1820 | n = kmod_list_append(*list, mv); |
| 1821 | if (n != NULL) |
| 1822 | *list = n; |
| 1823 | else { |
| 1824 | kmod_module_version_free(mv); |
| 1825 | kmod_module_versions_free_list(*list); |
| 1826 | *list = NULL; |
| 1827 | ret = -ENOMEM; |
| 1828 | goto list_error; |
| 1829 | } |
| 1830 | } |
| 1831 | ret = count; |
| 1832 | |
| 1833 | list_error: |
| 1834 | free(versions); |
| 1835 | get_strings_error: |
| 1836 | kmod_elf_unref(elf); |
| 1837 | elf_open_error: |
| 1838 | kmod_file_unref(file); |
| 1839 | |
| 1840 | return ret; |
| 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * kmod_module_versions_get_symbol: |
| 1845 | * @entry: a list entry representing a kmod module versions |
| 1846 | * |
| 1847 | * Get the symbol of a kmod module versions. |
| 1848 | * |
| 1849 | * Returns: the symbol of this kmod module versions on success or NULL |
| 1850 | * on failure. The string is owned by the versions, do not free it. |
| 1851 | */ |
| 1852 | KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry) |
| 1853 | { |
| 1854 | struct kmod_module_version *version; |
| 1855 | |
| 1856 | if (entry == NULL) |
| 1857 | return NULL; |
| 1858 | |
| 1859 | version = entry->data; |
| 1860 | return version->symbol; |
| 1861 | } |
| 1862 | |
| 1863 | /** |
| 1864 | * kmod_module_version_get_crc: |
| 1865 | * @entry: a list entry representing a kmod module version |
| 1866 | * |
| 1867 | * Get the crc of a kmod module version. |
| 1868 | * |
| 1869 | * Returns: the crc of this kmod module version on success or NULL on |
| 1870 | * failure. The string is owned by the version, do not free it. |
| 1871 | */ |
| 1872 | KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry) |
| 1873 | { |
| 1874 | struct kmod_module_version *version; |
| 1875 | |
| 1876 | if (entry == NULL) |
| 1877 | return 0; |
| 1878 | |
| 1879 | version = entry->data; |
| 1880 | return version->crc; |
| 1881 | } |
| 1882 | |
| 1883 | /** |
| 1884 | * kmod_module_versions_free_list: |
| 1885 | * @list: kmod module versions list |
| 1886 | * |
| 1887 | * Release the resources taken by @list |
| 1888 | */ |
| 1889 | KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list) |
| 1890 | { |
| 1891 | while (list) { |
| 1892 | kmod_module_version_free(list->data); |
| 1893 | list = kmod_list_remove(list); |
| 1894 | } |
| 1895 | } |