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