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> |
| 37 | |
| 38 | #include "libkmod.h" |
| 39 | #include "libkmod-private.h" |
| 40 | |
| 41 | /** |
| 42 | * kmod_module: |
| 43 | * |
| 44 | * Opaque object representing a module. |
| 45 | */ |
| 46 | struct kmod_module { |
| 47 | struct kmod_ctx *ctx; |
Lucas De Marchi | 219f9c3 | 2011-12-13 13:07:40 -0200 | [diff] [blame] | 48 | char *name; |
Gustavo Sverzut Barbieri | f1fb6f8 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 49 | char *path; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 50 | struct kmod_list *dep; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 51 | char *options; |
| 52 | char *install_commands; |
| 53 | char *remove_commands; |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 54 | 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] | 55 | int n_dep; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 56 | int refcount; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 57 | struct { |
| 58 | bool dep : 1; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 59 | bool options : 1; |
| 60 | bool install_commands : 1; |
| 61 | bool remove_commands : 1; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 62 | } init; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 63 | }; |
| 64 | |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 65 | inline char *modname_normalize(const char *modname, char buf[NAME_MAX], |
Lucas De Marchi | 6c343b1 | 2011-12-06 09:01:01 -0200 | [diff] [blame] | 66 | size_t *len) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 67 | { |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 68 | size_t s; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 69 | |
Gustavo Sverzut Barbieri | e1a6b30 | 2011-12-08 04:10:49 -0200 | [diff] [blame] | 70 | for (s = 0; s < NAME_MAX - 1; s++) { |
| 71 | const char c = modname[s]; |
| 72 | if (c == '-') |
| 73 | buf[s] = '_'; |
| 74 | else if (c == '\0' || c == '.') |
| 75 | break; |
| 76 | else |
| 77 | buf[s] = c; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 78 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 79 | |
Gustavo Sverzut Barbieri | e1a6b30 | 2011-12-08 04:10:49 -0200 | [diff] [blame] | 80 | buf[s] = '\0'; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 81 | |
| 82 | if (len) |
| 83 | *len = s; |
| 84 | |
Gustavo Sverzut Barbieri | e1a6b30 | 2011-12-08 04:10:49 -0200 | [diff] [blame] | 85 | return buf; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 86 | } |
| 87 | |
Lucas De Marchi | 6c343b1 | 2011-12-06 09:01:01 -0200 | [diff] [blame] | 88 | static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len) |
| 89 | { |
| 90 | char *modname; |
| 91 | |
| 92 | modname = basename(path); |
| 93 | if (modname == NULL || modname[0] == '\0') |
| 94 | return NULL; |
| 95 | |
| 96 | return modname_normalize(modname, buf, len); |
| 97 | } |
| 98 | |
Lucas De Marchi | c35347f | 2011-12-12 10:48:02 -0200 | [diff] [blame] | 99 | static inline const char *path_join(const char *path, size_t prefixlen, |
| 100 | char buf[PATH_MAX]) |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 101 | { |
| 102 | size_t pathlen; |
| 103 | |
| 104 | if (path[0] == '/') |
| 105 | return path; |
| 106 | |
| 107 | pathlen = strlen(path); |
| 108 | if (prefixlen + pathlen + 1 >= PATH_MAX) |
| 109 | return NULL; |
| 110 | |
| 111 | memcpy(buf + prefixlen, path, pathlen + 1); |
| 112 | return buf; |
| 113 | } |
| 114 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 115 | int kmod_module_parse_depline(struct kmod_module *mod, char *line) |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 116 | { |
| 117 | struct kmod_ctx *ctx = mod->ctx; |
| 118 | struct kmod_list *list = NULL; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 119 | const char *dirname; |
| 120 | char buf[PATH_MAX]; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 121 | char *p, *saveptr; |
Lucas De Marchi | 45f2778 | 2011-12-12 17:23:04 -0200 | [diff] [blame] | 122 | int err = 0, n = 0; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 123 | size_t dirnamelen; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 124 | |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 125 | if (mod->init.dep) |
| 126 | return mod->n_dep; |
| 127 | assert(mod->dep == NULL); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 128 | mod->init.dep = true; |
| 129 | |
| 130 | p = strchr(line, ':'); |
| 131 | if (p == NULL) |
| 132 | return 0; |
| 133 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 134 | *p = '\0'; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 135 | dirname = kmod_get_dirname(mod->ctx); |
| 136 | dirnamelen = strlen(dirname); |
| 137 | if (dirnamelen + 2 >= PATH_MAX) |
| 138 | return 0; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 139 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 140 | memcpy(buf, dirname, dirnamelen); |
| 141 | buf[dirnamelen] = '/'; |
| 142 | dirnamelen++; |
| 143 | buf[dirnamelen] = '\0'; |
| 144 | |
| 145 | if (mod->path == NULL) { |
| 146 | const char *str = path_join(line, dirnamelen, buf); |
| 147 | if (str == NULL) |
| 148 | return 0; |
| 149 | mod->path = strdup(str); |
| 150 | if (mod->path == NULL) |
| 151 | return 0; |
| 152 | } |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 153 | |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 154 | p++; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 155 | for (p = strtok_r(p, " \t", &saveptr); p != NULL; |
| 156 | p = strtok_r(NULL, " \t", &saveptr)) { |
Lucas De Marchi | 1fc1c9a | 2011-12-02 10:00:03 -0200 | [diff] [blame] | 157 | struct kmod_module *depmod; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 158 | const char *path; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 159 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 160 | path = path_join(p, dirnamelen, buf); |
| 161 | if (path == NULL) { |
| 162 | ERR(ctx, "could not join path '%s' and '%s'.\n", |
| 163 | dirname, p); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 164 | goto fail; |
| 165 | } |
| 166 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 167 | err = kmod_module_new_from_path(ctx, path, &depmod); |
| 168 | if (err < 0) { |
| 169 | ERR(ctx, "ctx=%p path=%s error=%s\n", |
| 170 | ctx, path, strerror(-err)); |
| 171 | goto fail; |
| 172 | } |
| 173 | |
| 174 | DBG(ctx, "add dep: %s\n", path); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 175 | |
Lucas De Marchi | 1fc1c9a | 2011-12-02 10:00:03 -0200 | [diff] [blame] | 176 | list = kmod_list_append(list, depmod); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 177 | n++; |
| 178 | } |
| 179 | |
| 180 | DBG(ctx, "%d dependencies for %s\n", n, mod->name); |
| 181 | |
| 182 | mod->dep = list; |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 183 | mod->n_dep = n; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 184 | return n; |
| 185 | |
| 186 | fail: |
| 187 | kmod_module_unref_list(list); |
| 188 | mod->init.dep = false; |
| 189 | return err; |
| 190 | } |
| 191 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 192 | /** |
| 193 | * kmod_module_new_from_name: |
| 194 | * @ctx: kmod library context |
| 195 | * @name: name of the module |
| 196 | * @mod: where to save the created struct kmod_module |
| 197 | * |
| 198 | * Create a new struct kmod_module using the module name. @name can not be an |
| 199 | * alias, file name or anything else; it must be a module name. There's no |
| 200 | * check if the module does exists in the system. |
| 201 | * |
| 202 | * This function is also used internally by many others that return a new |
| 203 | * struct kmod_module or a new list of modules. |
| 204 | * |
| 205 | * The initial refcount is 1, and needs to be decremented to release the |
| 206 | * resources of the kmod_module. Since libkmod keeps track of all |
| 207 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 208 | * not unref @ctx before all the desired operations with the returned |
| 209 | * kmod_module are done. |
| 210 | * |
| 211 | * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid |
| 212 | * module name or if memory allocation failed. |
| 213 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 214 | KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx, |
| 215 | const char *name, |
| 216 | struct kmod_module **mod) |
| 217 | { |
| 218 | struct kmod_module *m; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 219 | size_t namelen; |
Lucas De Marchi | 4f2bb7c | 2011-12-06 02:46:22 -0200 | [diff] [blame] | 220 | char name_norm[NAME_MAX]; |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 221 | char *namesep; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 222 | |
| 223 | if (ctx == NULL || name == NULL) |
| 224 | return -ENOENT; |
| 225 | |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 226 | alias_normalize(name, name_norm, &namelen); |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 227 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 228 | m = kmod_pool_get_module(ctx, name_norm); |
| 229 | if (m != NULL) { |
| 230 | *mod = kmod_module_ref(m); |
| 231 | return 0; |
| 232 | } |
| 233 | |
Lucas De Marchi | 788ef0f | 2011-12-14 15:05:03 -0200 | [diff] [blame] | 234 | m = malloc(sizeof(*m) + namelen + 1); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 235 | if (m == NULL) { |
| 236 | free(m); |
| 237 | return -ENOMEM; |
| 238 | } |
| 239 | |
Lucas De Marchi | 788ef0f | 2011-12-14 15:05:03 -0200 | [diff] [blame] | 240 | memset(m, 0, sizeof(*m)); |
| 241 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 242 | m->ctx = kmod_ref(ctx); |
Lucas De Marchi | 219f9c3 | 2011-12-13 13:07:40 -0200 | [diff] [blame] | 243 | m->name = (char *)m + sizeof(*m); |
Lucas De Marchi | 4f2bb7c | 2011-12-06 02:46:22 -0200 | [diff] [blame] | 244 | memcpy(m->name, name_norm, namelen + 1); |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 245 | |
Gustavo Sverzut Barbieri | 87ca03b | 2011-12-04 12:34:02 -0200 | [diff] [blame] | 246 | m->refcount = 1; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 247 | |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 248 | /* set alias later, so m->name is still modname/modalias */ |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 249 | kmod_pool_add_module(ctx, m); |
| 250 | |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 251 | namesep = strchr(m->name, '/'); |
| 252 | if (namesep != NULL) { |
| 253 | *namesep = '\0'; |
| 254 | m->alias = namesep + 1; |
| 255 | } |
| 256 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 257 | *mod = m; |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 262 | int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias, |
| 263 | const char *name, struct kmod_module **mod) |
| 264 | { |
| 265 | int err; |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 266 | char key[NAME_MAX]; |
| 267 | size_t namelen = strlen(name); |
| 268 | size_t aliaslen = strlen(alias); |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 269 | |
Lucas De Marchi | 113c66a | 2011-12-14 15:21:10 -0200 | [diff] [blame] | 270 | if (namelen + aliaslen + 2 > NAME_MAX) |
| 271 | return -ENAMETOOLONG; |
| 272 | |
| 273 | memcpy(key, name, namelen); |
| 274 | memcpy(key + namelen + 1, alias, aliaslen + 1); |
| 275 | key[namelen] = '/'; |
| 276 | |
| 277 | err = kmod_module_new_from_name(ctx, key, mod); |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 278 | if (err < 0) |
| 279 | return err; |
| 280 | |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 281 | return 0; |
Lucas De Marchi | 6ad5f26 | 2011-12-13 14:12:50 -0200 | [diff] [blame] | 282 | } |
| 283 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 284 | /** |
| 285 | * kmod_module_new_from_path: |
| 286 | * @ctx: kmod library context |
| 287 | * @path: path where to find the given module |
| 288 | * @mod: where to save the created struct kmod_module |
| 289 | * |
| 290 | * Create a new struct kmod_module using the module path. @path must be an |
| 291 | * existent file with in the filesystem and must be accessible to libkmod. |
| 292 | * |
| 293 | * The initial refcount is 1, and needs to be decremented to release the |
| 294 | * resources of the kmod_module. Since libkmod keeps track of all |
| 295 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 296 | * not unref @ctx before all the desired operations with the returned |
| 297 | * kmod_module are done. |
| 298 | * |
| 299 | * If @path is relative, it's treated as relative to the current working |
| 300 | * directory. Otherwise, give an absolute path. |
| 301 | * |
| 302 | * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if |
| 303 | * it's not a valid file for a kmod_module or if memory allocation failed. |
| 304 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 305 | KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx, |
| 306 | const char *path, |
| 307 | struct kmod_module **mod) |
| 308 | { |
| 309 | struct kmod_module *m; |
| 310 | int err; |
| 311 | struct stat st; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 312 | char name[NAME_MAX]; |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 313 | char *abspath; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 314 | size_t namelen; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 315 | |
| 316 | if (ctx == NULL || path == NULL) |
| 317 | return -ENOENT; |
| 318 | |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 319 | abspath = path_make_absolute_cwd(path); |
| 320 | if (abspath == NULL) |
| 321 | return -ENOMEM; |
| 322 | |
| 323 | err = stat(abspath, &st); |
| 324 | if (err < 0) { |
| 325 | free(abspath); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 326 | return -errno; |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 327 | } |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 328 | |
Gustavo Sverzut Barbieri | 973c80b | 2011-12-12 18:28:52 -0200 | [diff] [blame] | 329 | if (path_to_modname(path, name, &namelen) == NULL) { |
| 330 | free(abspath); |
| 331 | return -ENOENT; |
| 332 | } |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 333 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 334 | m = kmod_pool_get_module(ctx, name); |
| 335 | if (m != NULL) { |
Lucas De Marchi | 6bd0b8d | 2011-12-07 14:08:01 -0200 | [diff] [blame] | 336 | if (m->path == NULL) |
| 337 | m->path = abspath; |
| 338 | else if (streq(m->path, abspath)) |
| 339 | free(abspath); |
| 340 | else { |
| 341 | ERR(ctx, "kmod_module '%s' already exists with different path\n", |
| 342 | name); |
| 343 | free(abspath); |
| 344 | return -EEXIST; |
| 345 | } |
| 346 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 347 | *mod = kmod_module_ref(m); |
| 348 | return 0; |
| 349 | } |
| 350 | |
Lucas De Marchi | 788ef0f | 2011-12-14 15:05:03 -0200 | [diff] [blame] | 351 | m = malloc(sizeof(*m) + namelen + 1); |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 352 | if (m == NULL) |
| 353 | return -errno; |
| 354 | |
Lucas De Marchi | 788ef0f | 2011-12-14 15:05:03 -0200 | [diff] [blame] | 355 | memset(m, 0, sizeof(*m)); |
| 356 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 357 | m->ctx = kmod_ref(ctx); |
Lucas De Marchi | 219f9c3 | 2011-12-13 13:07:40 -0200 | [diff] [blame] | 358 | m->name = (char *)m + sizeof(*m); |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 359 | memcpy(m->name, name, namelen); |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 360 | m->path = abspath; |
Gustavo Sverzut Barbieri | 87ca03b | 2011-12-04 12:34:02 -0200 | [diff] [blame] | 361 | m->refcount = 1; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 362 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 363 | kmod_pool_add_module(ctx, m); |
| 364 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 365 | *mod = m; |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 370 | /** |
| 371 | * kmod_module_unref: |
| 372 | * @mod: kmod module |
| 373 | * |
| 374 | * Drop a reference of the kmod module. If the refcount reaches zero, its |
| 375 | * resources are released. |
| 376 | * |
| 377 | * Returns: NULL if @mod is NULL or if the module was released. Otherwise it |
| 378 | * returns the passed @mod with its refcount decremented. |
| 379 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 380 | KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod) |
| 381 | { |
| 382 | if (mod == NULL) |
| 383 | return NULL; |
| 384 | |
| 385 | if (--mod->refcount > 0) |
| 386 | return mod; |
| 387 | |
| 388 | DBG(mod->ctx, "kmod_module %p released\n", mod); |
| 389 | |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 390 | kmod_module_unref_list(mod->dep); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 391 | kmod_unref(mod->ctx); |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 392 | free(mod->options); |
| 393 | free(mod->install_commands); |
| 394 | free(mod->remove_commands); |
Gustavo Sverzut Barbieri | f1fb6f8 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 395 | free(mod->path); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 396 | free(mod); |
| 397 | return NULL; |
| 398 | } |
| 399 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 400 | /** |
| 401 | * kmod_module_ref: |
| 402 | * @mod: kmod module |
| 403 | * |
| 404 | * Take a reference of the kmod module, incrementing its refcount. |
| 405 | * |
| 406 | * Returns: the passed @module with its refcount incremented. |
| 407 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 408 | KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod) |
| 409 | { |
| 410 | if (mod == NULL) |
| 411 | return NULL; |
| 412 | |
| 413 | mod->refcount++; |
| 414 | |
| 415 | return mod; |
| 416 | } |
| 417 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 418 | #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \ |
| 419 | do { \ |
| 420 | if ((_err) < 0) \ |
| 421 | goto _label_err; \ |
| 422 | if (*(_list) != NULL) \ |
| 423 | goto finish; \ |
| 424 | } while (0) |
| 425 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 426 | /** |
| 427 | * kmod_module_new_from_lookup: |
| 428 | * @ctx: kmod library context |
| 429 | * @given_alias: alias to look for |
| 430 | * @list: an empty list where to save the list of modules matching |
| 431 | * @given_alias |
| 432 | * |
| 433 | * Create a new list of kmod modules using an alias or module name and lookup |
| 434 | * libkmod's configuration files and indexes in order to find the module. |
| 435 | * Once it's found in one of the places, it stops searching and create the |
| 436 | * list of modules that is saved in @list. |
| 437 | * |
| 438 | * The search order is: 1. aliases in configuration file; 2. module names in |
| 439 | * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases |
| 440 | * in modules.alias index. |
| 441 | * |
| 442 | * The initial refcount is 1, and needs to be decremented to release the |
| 443 | * resources of the kmod_module. The returned @list must be released by |
| 444 | * calling kmod_module_unref_list(). Since libkmod keeps track of all |
| 445 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 446 | * not unref @ctx before all the desired operations with the returned list are |
| 447 | * completed. |
| 448 | * |
| 449 | * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup |
| 450 | * methods failed, which is basically due to memory allocation fail. If module |
| 451 | * is not found, it still returns 0, but @list is an empty list. |
| 452 | */ |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 453 | 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] | 454 | const char *given_alias, |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 455 | struct kmod_list **list) |
| 456 | { |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 457 | int err; |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 458 | char alias[NAME_MAX]; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 459 | |
Lucas De Marchi | 4308b17 | 2011-12-13 10:26:04 -0200 | [diff] [blame] | 460 | if (ctx == NULL || given_alias == NULL) |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 461 | return -ENOENT; |
| 462 | |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 463 | if (list == NULL || *list != NULL) { |
| 464 | ERR(ctx, "An empty list is needed to create lookup\n"); |
| 465 | return -ENOSYS; |
| 466 | } |
| 467 | |
Lucas De Marchi | d470db1 | 2011-12-13 10:28:00 -0200 | [diff] [blame] | 468 | if (alias_normalize(given_alias, alias, NULL) < 0) |
| 469 | return -EINVAL; |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 470 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 471 | /* Aliases from config file override all the others */ |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 472 | err = kmod_lookup_alias_from_config(ctx, alias, list); |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 473 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 474 | |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 475 | err = kmod_lookup_alias_from_moddep_file(ctx, alias, list); |
| 476 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
| 477 | |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 478 | err = kmod_lookup_alias_from_symbols_file(ctx, alias, list); |
| 479 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 480 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 481 | // TODO: add lookup for install commands here. |
| 482 | |
Lucas De Marchi | 49e61ca | 2011-12-01 16:27:04 -0200 | [diff] [blame] | 483 | err = kmod_lookup_alias_from_aliases_file(ctx, alias, list); |
| 484 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
| 485 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 486 | finish: |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 487 | |
| 488 | return err; |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 489 | fail: |
| 490 | kmod_module_unref_list(*list); |
| 491 | *list = NULL; |
Lucas De Marchi | 84f4220 | 2011-12-02 10:03:34 -0200 | [diff] [blame] | 492 | return err; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 493 | } |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 494 | #undef CHECK_ERR_AND_FINISH |
| 495 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 496 | /** |
Lucas De Marchi | 91428ae | 2011-12-15 13:09:46 -0200 | [diff] [blame^] | 497 | * kmod_module_unref_list: |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 498 | * @list: list of kmod modules |
| 499 | * |
| 500 | * Drop a reference of each kmod module in @list and releases the resources |
| 501 | * taken by the list itself. |
| 502 | * |
| 503 | * Returns: NULL if @mod is NULL or if the module was released. Otherwise it |
| 504 | * returns the passed @mod with its refcount decremented. |
| 505 | */ |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 506 | KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list) |
| 507 | { |
| 508 | for (; list != NULL; list = kmod_list_remove(list)) |
| 509 | kmod_module_unref(list->data); |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 514 | /** |
Lucas De Marchi | 91428ae | 2011-12-15 13:09:46 -0200 | [diff] [blame^] | 515 | * kmod_module_get_dependencies: |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 516 | * @mod: kmod module |
| 517 | * |
| 518 | * Search the modules.dep index to find the dependencies of the given @mod. |
| 519 | * The result is cached in @mod, so subsequent calls to this function will |
| 520 | * return the already searched list of modules. |
| 521 | * |
| 522 | * Returns: NULL on failure or if there are any dependencies. Otherwise it |
| 523 | * returns a list of kmod modules that can be released by calling |
| 524 | * kmod_module_unref_list(). |
| 525 | */ |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 526 | 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] | 527 | { |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 528 | struct kmod_list *l, *l_new, *list_new = NULL; |
| 529 | |
| 530 | if (mod == NULL) |
| 531 | return NULL; |
| 532 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 533 | if (!mod->init.dep) { |
| 534 | /* lazy init */ |
| 535 | char *line = kmod_search_moddep(mod->ctx, mod->name); |
| 536 | |
| 537 | if (line == NULL) |
| 538 | return NULL; |
| 539 | |
| 540 | kmod_module_parse_depline((struct kmod_module *)mod, line); |
| 541 | free(line); |
| 542 | |
| 543 | if (!mod->init.dep) |
| 544 | return NULL; |
| 545 | } |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 546 | |
| 547 | kmod_list_foreach(l, mod->dep) { |
| 548 | l_new = kmod_list_append(list_new, kmod_module_ref(l->data)); |
| 549 | if (l_new == NULL) { |
| 550 | kmod_module_unref(l->data); |
| 551 | goto fail; |
| 552 | } |
| 553 | |
| 554 | list_new = l_new; |
| 555 | } |
| 556 | |
| 557 | return list_new; |
| 558 | |
| 559 | fail: |
| 560 | ERR(mod->ctx, "out of memory\n"); |
| 561 | kmod_module_unref_list(list_new); |
| 562 | return NULL; |
Lucas De Marchi | 0835fc3 | 2011-12-01 20:06:08 -0200 | [diff] [blame] | 563 | } |
| 564 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 565 | /** |
| 566 | * kmod_module_get_module: |
| 567 | * @entry: an entry in a list of kmod modules. |
| 568 | * |
| 569 | * Get the kmod module of this @entry in the list, increasing its refcount. |
| 570 | * After it's used, unref it. Since the refcount is incremented upon return, |
| 571 | * you still have to call kmod_module_unref_list() to release the list of kmod |
| 572 | * modules. |
| 573 | * |
| 574 | * Returns: NULL on failure or the kmod_module contained in this list entry |
| 575 | * with its refcount incremented. |
| 576 | */ |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 577 | 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] | 578 | { |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 579 | if (entry == NULL) |
| 580 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 581 | |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 582 | return kmod_module_ref(entry->data); |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 583 | } |
| 584 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 585 | /** |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 586 | * kmod_module_get_name: |
| 587 | * @mod: kmod module |
| 588 | * |
| 589 | * Get the name of this kmod module. Name is always available, independently |
| 590 | * if it was created by kmod_module_new_from_name() or another function and |
| 591 | * it's always normalized (dashes are replaced with underscores). |
| 592 | * |
| 593 | * Returns: the name of this kmod module. |
| 594 | */ |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 595 | 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] | 596 | { |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 597 | return mod->name; |
| 598 | } |
| 599 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 600 | /** |
| 601 | * kmod_module_get_path: |
| 602 | * @mod: kmod module |
| 603 | * |
| 604 | * Get the path of this kmod module. If this kmod module was not created by |
| 605 | * path, it can search the modules.dep index in order to find out the module |
| 606 | * under context's dirname (see kmod_get_dirname()). |
| 607 | * |
| 608 | * Returns: the path of this kmod module or NULL if such information is not |
| 609 | * available. |
| 610 | */ |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 611 | 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] | 612 | { |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 613 | char *line; |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 614 | |
Gustavo Sverzut Barbieri | d01c67e | 2011-12-11 19:42:02 -0200 | [diff] [blame] | 615 | 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] | 616 | |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 617 | if (mod->path != NULL) |
| 618 | return mod->path; |
| 619 | if (mod->init.dep) |
| 620 | return NULL; |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 621 | |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 622 | /* lazy init */ |
| 623 | line = kmod_search_moddep(mod->ctx, mod->name); |
| 624 | if (line == NULL) |
| 625 | return NULL; |
| 626 | |
| 627 | kmod_module_parse_depline((struct kmod_module *) mod, line); |
| 628 | free(line); |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 629 | |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 630 | return mod->path; |
| 631 | } |
| 632 | |
| 633 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 634 | extern long delete_module(const char *name, unsigned int flags); |
| 635 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 636 | /** |
| 637 | * kmod_module_remove_module: |
| 638 | * @mod: kmod module |
| 639 | * @flags: flags to pass to Linux kernel when removing the module |
| 640 | * |
| 641 | * Remove a module from Linux kernel. |
| 642 | * |
| 643 | * Returns: 0 on success or < 0 on failure. |
| 644 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 645 | KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, |
| 646 | unsigned int flags) |
| 647 | { |
| 648 | int err; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 649 | |
| 650 | if (mod == NULL) |
| 651 | return -ENOENT; |
| 652 | |
| 653 | /* Filter out other flags */ |
| 654 | flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT); |
| 655 | |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 656 | err = delete_module(mod->name, flags); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 657 | if (err != 0) { |
Lucas De Marchi | 63af061 | 2011-12-14 12:07:37 -0200 | [diff] [blame] | 658 | ERR(mod->ctx, "Could not remove '%s': %s\n", mod->name, |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 659 | strerror(-err)); |
| 660 | return err; |
| 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | extern long init_module(void *mem, unsigned long len, const char *args); |
| 667 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 668 | /** |
| 669 | * kmod_module_insert_module: |
| 670 | * @mod: kmod module |
| 671 | * @flags: flags are not passed to Linux Kernel, but instead it dictates the |
| 672 | * behavior of this function. They are not implemented yet. |
| 673 | * @options: module's options to pass to Linux Kernel. |
| 674 | * |
| 675 | * Insert a module in Linux kernel. It opens the file pointed by @mod, |
| 676 | * mmap'ing it and passing to kernel. |
| 677 | * |
| 678 | * Returns: 0 on success or < 0 on failure. |
| 679 | */ |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 680 | KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod, |
Gustavo Sverzut Barbieri | 3a721bb | 2011-12-10 21:02:39 -0200 | [diff] [blame] | 681 | unsigned int flags, |
| 682 | const char *options) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 683 | { |
| 684 | int err; |
| 685 | void *mmaped_file; |
| 686 | struct stat st; |
| 687 | int fd; |
Gustavo Sverzut Barbieri | 3a721bb | 2011-12-10 21:02:39 -0200 | [diff] [blame] | 688 | const char *args = options ? options : ""; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 689 | |
| 690 | if (mod == NULL) |
| 691 | return -ENOENT; |
| 692 | |
| 693 | if (mod->path == NULL) { |
Lucas De Marchi | 1b2e26a | 2011-11-25 01:28:39 -0200 | [diff] [blame] | 694 | 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] | 695 | return -ENOSYS; |
| 696 | } |
| 697 | |
| 698 | if (flags != 0) |
Lucas De Marchi | 1b2e26a | 2011-11-25 01:28:39 -0200 | [diff] [blame] | 699 | INFO(mod->ctx, "Flags are not implemented yet\n"); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 700 | |
| 701 | if ((fd = open(mod->path, O_RDONLY)) < 0) { |
| 702 | err = -errno; |
| 703 | return err; |
| 704 | } |
| 705 | |
Lucas De Marchi | b418a82 | 2011-12-01 23:13:27 -0200 | [diff] [blame] | 706 | fstat(fd, &st); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 707 | |
| 708 | if ((mmaped_file = mmap(0, st.st_size, PROT_READ, |
| 709 | MAP_PRIVATE, fd, 0)) == MAP_FAILED) { |
| 710 | close(fd); |
| 711 | return -errno; |
| 712 | } |
| 713 | |
| 714 | err = init_module(mmaped_file, st.st_size, args); |
| 715 | if (err < 0) |
Lucas De Marchi | 1b2e26a | 2011-11-25 01:28:39 -0200 | [diff] [blame] | 716 | ERR(mod->ctx, "Failed to insert module '%s'\n", mod->path); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 717 | |
| 718 | munmap(mmaped_file, st.st_size); |
| 719 | close(fd); |
| 720 | |
| 721 | return err; |
| 722 | } |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 723 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 724 | /** |
| 725 | * kmod_module_get_options: |
| 726 | * @mod: kmod module |
| 727 | * |
| 728 | * Get options of this kmod module. Options come from the configuration file |
| 729 | * and are cached in @mod. The first call to this function will search for |
| 730 | * this module in configuration and subsequent calls return the cached string. |
| 731 | * |
| 732 | * Returns: a string with all the options separated by spaces. This string is |
| 733 | * owned by @mod, do not free it. |
| 734 | */ |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 735 | KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod) |
| 736 | { |
| 737 | if (mod == NULL) |
| 738 | return NULL; |
| 739 | |
| 740 | if (!mod->init.options) { |
| 741 | /* lazy init */ |
| 742 | struct kmod_module *m = (struct kmod_module *)mod; |
| 743 | const struct kmod_list *l, *ctx_options; |
| 744 | char *opts = NULL; |
| 745 | size_t optslen = 0; |
| 746 | |
| 747 | ctx_options = kmod_get_options(mod->ctx); |
| 748 | |
| 749 | kmod_list_foreach(l, ctx_options) { |
| 750 | const char *modname = kmod_option_get_modname(l); |
| 751 | const char *str; |
| 752 | size_t len; |
| 753 | void *tmp; |
| 754 | |
Lucas De Marchi | 07b8c82 | 2011-12-13 14:21:24 -0200 | [diff] [blame] | 755 | DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias); |
| 756 | if (!(streq(modname, mod->name) || (mod->alias != NULL && |
| 757 | streq(modname, mod->alias)))) |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 758 | continue; |
| 759 | |
Lucas De Marchi | 07b8c82 | 2011-12-13 14:21:24 -0200 | [diff] [blame] | 760 | 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] | 761 | str = kmod_option_get_options(l); |
| 762 | len = strlen(str); |
| 763 | if (len < 1) |
| 764 | continue; |
| 765 | |
| 766 | tmp = realloc(opts, optslen + len + 2); |
| 767 | if (tmp == NULL) { |
| 768 | free(opts); |
| 769 | goto failed; |
| 770 | } |
| 771 | |
| 772 | opts = tmp; |
| 773 | |
| 774 | if (optslen > 0) { |
| 775 | opts[optslen] = ' '; |
| 776 | optslen++; |
| 777 | } |
| 778 | |
| 779 | memcpy(opts + optslen, str, len); |
| 780 | optslen += len; |
| 781 | opts[optslen] = '\0'; |
| 782 | } |
| 783 | |
| 784 | m->init.options = true; |
| 785 | m->options = opts; |
| 786 | } |
| 787 | |
| 788 | return mod->options; |
| 789 | |
| 790 | failed: |
| 791 | ERR(mod->ctx, "out of memory\n"); |
| 792 | return NULL; |
| 793 | } |
| 794 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 795 | /** |
| 796 | * kmod_module_get_install_commands: |
| 797 | * @mod: kmod module |
| 798 | * |
| 799 | * Get install commands for this kmod module. Install commands come from the |
| 800 | * configuration file and are cached in @mod. The first call to this function |
| 801 | * will search for this module in configuration and subsequent calls return |
| 802 | * the cached string. The install commands are returned as they were in the |
| 803 | * configuration, concatenated by ';'. No other processing is made in this |
| 804 | * string. |
| 805 | * |
| 806 | * Returns: a string with all install commands separated by semicolons. This |
| 807 | * string is owned by @mod, do not free it. |
| 808 | */ |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 809 | KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod) |
| 810 | { |
| 811 | if (mod == NULL) |
| 812 | return NULL; |
| 813 | |
| 814 | if (!mod->init.install_commands) { |
| 815 | /* lazy init */ |
| 816 | struct kmod_module *m = (struct kmod_module *)mod; |
| 817 | const struct kmod_list *l, *ctx_install_commands; |
| 818 | char *cmds = NULL; |
| 819 | size_t cmdslen = 0; |
| 820 | |
| 821 | ctx_install_commands = kmod_get_install_commands(mod->ctx); |
| 822 | |
| 823 | kmod_list_foreach(l, ctx_install_commands) { |
| 824 | const char *modname = kmod_command_get_modname(l); |
| 825 | const char *str; |
| 826 | size_t len; |
| 827 | void *tmp; |
| 828 | |
| 829 | if (strcmp(modname, mod->name) != 0) |
| 830 | continue; |
| 831 | |
| 832 | str = kmod_command_get_command(l); |
| 833 | len = strlen(str); |
| 834 | if (len < 1) |
| 835 | continue; |
| 836 | |
| 837 | tmp = realloc(cmds, cmdslen + len + 2); |
| 838 | if (tmp == NULL) { |
| 839 | free(cmds); |
| 840 | goto failed; |
| 841 | } |
| 842 | |
| 843 | cmds = tmp; |
| 844 | |
| 845 | if (cmdslen > 0) { |
| 846 | cmds[cmdslen] = ';'; |
| 847 | cmdslen++; |
| 848 | } |
| 849 | |
| 850 | memcpy(cmds + cmdslen, str, len); |
| 851 | cmdslen += len; |
| 852 | cmds[cmdslen] = '\0'; |
| 853 | } |
| 854 | |
| 855 | m->init.install_commands = true; |
| 856 | m->install_commands = cmds; |
| 857 | } |
| 858 | |
| 859 | return mod->install_commands; |
| 860 | |
| 861 | failed: |
| 862 | ERR(mod->ctx, "out of memory\n"); |
| 863 | return NULL; |
| 864 | } |
| 865 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 866 | /** |
| 867 | * kmod_module_get_remove_commands: |
| 868 | * @mod: kmod module |
| 869 | * |
| 870 | * Get remove commands for this kmod module. Remove commands come from the |
| 871 | * configuration file and are cached in @mod. The first call to this function |
| 872 | * will search for this module in configuration and subsequent calls return |
| 873 | * the cached string. The remove commands are returned as they were in the |
| 874 | * configuration, concatenated by ';'. No other processing is made in this |
| 875 | * string. |
| 876 | * |
| 877 | * Returns: a string with all remove commands separated by semicolons. This |
| 878 | * string is owned by @mod, do not free it. |
| 879 | */ |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 880 | KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod) |
| 881 | { |
| 882 | if (mod == NULL) |
| 883 | return NULL; |
| 884 | |
| 885 | if (!mod->init.remove_commands) { |
| 886 | /* lazy init */ |
| 887 | struct kmod_module *m = (struct kmod_module *)mod; |
| 888 | const struct kmod_list *l, *ctx_remove_commands; |
| 889 | char *cmds = NULL; |
| 890 | size_t cmdslen = 0; |
| 891 | |
| 892 | ctx_remove_commands = kmod_get_remove_commands(mod->ctx); |
| 893 | |
| 894 | kmod_list_foreach(l, ctx_remove_commands) { |
| 895 | const char *modname = kmod_command_get_modname(l); |
| 896 | const char *str; |
| 897 | size_t len; |
| 898 | void *tmp; |
| 899 | |
| 900 | if (strcmp(modname, mod->name) != 0) |
| 901 | continue; |
| 902 | |
| 903 | str = kmod_command_get_command(l); |
| 904 | len = strlen(str); |
| 905 | if (len < 1) |
| 906 | continue; |
| 907 | |
| 908 | tmp = realloc(cmds, cmdslen + len + 2); |
| 909 | if (tmp == NULL) { |
| 910 | free(cmds); |
| 911 | goto failed; |
| 912 | } |
| 913 | |
| 914 | cmds = tmp; |
| 915 | |
| 916 | if (cmdslen > 0) { |
| 917 | cmds[cmdslen] = ';'; |
| 918 | cmdslen++; |
| 919 | } |
| 920 | |
| 921 | memcpy(cmds + cmdslen, str, len); |
| 922 | cmdslen += len; |
| 923 | cmds[cmdslen] = '\0'; |
| 924 | } |
| 925 | |
| 926 | m->init.remove_commands = true; |
| 927 | m->remove_commands = cmds; |
| 928 | } |
| 929 | |
| 930 | return mod->remove_commands; |
| 931 | |
| 932 | failed: |
| 933 | ERR(mod->ctx, "out of memory\n"); |
| 934 | return NULL; |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * SECTION:libkmod-loaded |
| 939 | * @short_description: currently loaded modules |
| 940 | * |
| 941 | * Information about currently loaded modules, as reported by Linux kernel. |
| 942 | * These information are not cached by libkmod and are always read from /sys |
| 943 | * and /proc/modules. |
| 944 | */ |
| 945 | |
| 946 | /** |
| 947 | * kmod_module_new_from_loaded: |
| 948 | * @ctx: kmod library context |
| 949 | * @list: where to save the list of loaded modules |
| 950 | * |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 951 | * Create a new list of kmod modules with all modules currently loaded in |
| 952 | * kernel. It uses /proc/modules to get the names of loaded modules and to |
| 953 | * create kmod modules by calling kmod_module_new_from_name() in each of them. |
| 954 | * They are put are put in @list in no particular order. |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 955 | * |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 956 | * The initial refcount is 1, and needs to be decremented to release the |
| 957 | * resources of the kmod_module. The returned @list must be released by |
| 958 | * calling kmod_module_unref_list(). Since libkmod keeps track of all |
| 959 | * kmod_modules created, they are all released upon @ctx destruction too. Do |
| 960 | * not unref @ctx before all the desired operations with the returned list are |
| 961 | * completed. |
Lucas De Marchi | 49ce6d0 | 2011-12-12 13:56:47 -0200 | [diff] [blame] | 962 | * |
| 963 | * Returns: 0 on success or < 0 on error. |
| 964 | */ |
| 965 | KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx, |
| 966 | struct kmod_list **list) |
| 967 | { |
| 968 | struct kmod_list *l = NULL; |
| 969 | FILE *fp; |
| 970 | char line[4096]; |
| 971 | |
| 972 | if (ctx == NULL || list == NULL) |
| 973 | return -ENOENT; |
| 974 | |
| 975 | fp = fopen("/proc/modules", "r"); |
| 976 | if (fp == NULL) { |
| 977 | int err = -errno; |
| 978 | ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno)); |
| 979 | return err; |
| 980 | } |
| 981 | |
| 982 | while (fgets(line, sizeof(line), fp)) { |
| 983 | struct kmod_module *m; |
| 984 | struct kmod_list *node; |
| 985 | int err; |
| 986 | char *saveptr, *name = strtok_r(line, " \t", &saveptr); |
| 987 | |
| 988 | err = kmod_module_new_from_name(ctx, name, &m); |
| 989 | if (err < 0) { |
| 990 | ERR(ctx, "could not get module from name '%s': %s\n", |
| 991 | name, strerror(-err)); |
| 992 | continue; |
| 993 | } |
| 994 | |
| 995 | node = kmod_list_append(l, m); |
| 996 | if (node) |
| 997 | l = node; |
| 998 | else { |
| 999 | ERR(ctx, "out of memory\n"); |
| 1000 | kmod_module_unref(m); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | fclose(fp); |
| 1005 | *list = l; |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1010 | /** |
| 1011 | * kmod_module_initstate_str: |
| 1012 | * @state: the state as returned by kmod_module_get_initstate() |
| 1013 | * |
| 1014 | * Translate a initstate to a string. |
| 1015 | * |
| 1016 | * Returns: the string associated to the @state. This string is statically |
| 1017 | * allocated, do not free it. |
| 1018 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1019 | KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state) |
| 1020 | { |
| 1021 | switch (state) { |
| 1022 | case KMOD_MODULE_BUILTIN: |
| 1023 | return "builtin"; |
| 1024 | case KMOD_MODULE_LIVE: |
| 1025 | return "live"; |
| 1026 | case KMOD_MODULE_COMING: |
| 1027 | return "coming"; |
| 1028 | case KMOD_MODULE_GOING: |
| 1029 | return "going"; |
| 1030 | default: |
| 1031 | return NULL; |
| 1032 | } |
| 1033 | } |
| 1034 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1035 | /** |
| 1036 | * kmod_module_get_initstate: |
| 1037 | * @mod: kmod module |
| 1038 | * |
| 1039 | * Get the initstate of this @mod, as returned by Linux Kernel, by reading |
| 1040 | * /sys filesystem. |
| 1041 | * |
| 1042 | * Returns: < 0 on error or enum kmod_initstate if module is found in kernel. |
| 1043 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1044 | KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod) |
| 1045 | { |
| 1046 | char path[PATH_MAX], buf[32]; |
| 1047 | int fd, err, pathlen; |
| 1048 | |
| 1049 | pathlen = snprintf(path, sizeof(path), |
| 1050 | "/sys/module/%s/initstate", mod->name); |
| 1051 | fd = open(path, O_RDONLY); |
| 1052 | if (fd < 0) { |
| 1053 | err = -errno; |
| 1054 | |
| 1055 | if (pathlen > (int)sizeof("/initstate") - 1) { |
| 1056 | struct stat st; |
| 1057 | path[pathlen - (sizeof("/initstate") - 1)] = '\0'; |
| 1058 | if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) |
| 1059 | return KMOD_MODULE_BUILTIN; |
| 1060 | } |
| 1061 | |
Gustavo Sverzut Barbieri | 926f67a | 2011-12-11 19:33:03 -0200 | [diff] [blame] | 1062 | DBG(mod->ctx, "could not open '%s': %s\n", |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1063 | path, strerror(-err)); |
| 1064 | return err; |
| 1065 | } |
| 1066 | |
| 1067 | err = read_str_safe(fd, buf, sizeof(buf)); |
| 1068 | close(fd); |
| 1069 | if (err < 0) { |
| 1070 | ERR(mod->ctx, "could not read from '%s': %s\n", |
| 1071 | path, strerror(-err)); |
| 1072 | return err; |
| 1073 | } |
| 1074 | |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 1075 | if (streq(buf, "live\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1076 | return KMOD_MODULE_LIVE; |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 1077 | else if (streq(buf, "coming\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1078 | return KMOD_MODULE_COMING; |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 1079 | else if (streq(buf, "going\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1080 | return KMOD_MODULE_GOING; |
| 1081 | |
| 1082 | ERR(mod->ctx, "unknown %s: '%s'\n", path, buf); |
| 1083 | return -EINVAL; |
| 1084 | } |
| 1085 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1086 | /** |
Lucas De Marchi | 2d7bab5 | 2011-12-14 12:10:02 -0200 | [diff] [blame] | 1087 | * kmod_module_get_size: |
| 1088 | * @mod: kmod module |
| 1089 | * |
| 1090 | * Get the size of this kmod module as returned by Linux kernel. It reads the |
| 1091 | * file /proc/modules to search for this module and get its size. |
| 1092 | * |
| 1093 | * Returns: the size of this kmod module. |
| 1094 | */ |
| 1095 | KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod) |
| 1096 | { |
| 1097 | // FIXME TODO: this should be available from /sys/module/foo |
| 1098 | FILE *fp; |
| 1099 | char line[4096]; |
| 1100 | int lineno = 0; |
| 1101 | long size = -ENOENT; |
| 1102 | |
| 1103 | if (mod == NULL) |
| 1104 | return -ENOENT; |
| 1105 | |
| 1106 | fp = fopen("/proc/modules", "r"); |
| 1107 | if (fp == NULL) { |
| 1108 | int err = -errno; |
| 1109 | ERR(mod->ctx, |
| 1110 | "could not open /proc/modules: %s\n", strerror(errno)); |
| 1111 | return err; |
| 1112 | } |
| 1113 | |
| 1114 | while (fgets(line, sizeof(line), fp)) { |
| 1115 | char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr); |
| 1116 | long value; |
| 1117 | |
| 1118 | lineno++; |
| 1119 | if (tok == NULL || !streq(tok, mod->name)) |
| 1120 | continue; |
| 1121 | |
| 1122 | tok = strtok_r(NULL, " \t", &saveptr); |
| 1123 | if (tok == NULL) { |
| 1124 | ERR(mod->ctx, |
| 1125 | "invalid line format at /proc/modules:%d\n", lineno); |
| 1126 | break; |
| 1127 | } |
| 1128 | |
| 1129 | value = strtol(tok, &endptr, 10); |
| 1130 | if (endptr == tok || *endptr != '\0') { |
| 1131 | ERR(mod->ctx, |
| 1132 | "invalid line format at /proc/modules:%d\n", lineno); |
| 1133 | break; |
| 1134 | } |
| 1135 | |
| 1136 | size = value; |
| 1137 | break; |
| 1138 | } |
| 1139 | fclose(fp); |
| 1140 | return size; |
| 1141 | } |
| 1142 | |
| 1143 | /** |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1144 | * kmod_module_get_refcnt: |
| 1145 | * @mod: kmod module |
| 1146 | * |
| 1147 | * Get the ref count of this @mod, as returned by Linux Kernel, by reading |
| 1148 | * /sys filesystem. |
| 1149 | * |
| 1150 | * Returns: 0 on success or < 0 on failure. |
| 1151 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1152 | KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) |
| 1153 | { |
| 1154 | char path[PATH_MAX]; |
| 1155 | long refcnt; |
| 1156 | int fd, err; |
| 1157 | |
| 1158 | snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name); |
| 1159 | fd = open(path, O_RDONLY); |
| 1160 | if (fd < 0) { |
| 1161 | err = -errno; |
| 1162 | ERR(mod->ctx, "could not open '%s': %s\n", |
| 1163 | path, strerror(errno)); |
| 1164 | return err; |
| 1165 | } |
| 1166 | |
| 1167 | err = read_str_long(fd, &refcnt, 10); |
| 1168 | close(fd); |
| 1169 | if (err < 0) { |
| 1170 | ERR(mod->ctx, "could not read integer from '%s': '%s'\n", |
| 1171 | path, strerror(-err)); |
| 1172 | return err; |
| 1173 | } |
| 1174 | |
| 1175 | return (int)refcnt; |
| 1176 | } |
| 1177 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1178 | /** |
| 1179 | * kmod_module_get_holders: |
| 1180 | * @mod: kmod module |
| 1181 | * |
| 1182 | * Get a list of kmod modules that are holding this @mod, as returned by Linux |
| 1183 | * Kernel. After use, free the @list by calling kmod_module_unref_list(). |
| 1184 | * |
| 1185 | * Returns: a new list of kmod modules on success or NULL on failure. |
| 1186 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1187 | KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod) |
| 1188 | { |
| 1189 | char dname[PATH_MAX]; |
| 1190 | struct kmod_list *list = NULL; |
| 1191 | DIR *d; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1192 | |
| 1193 | if (mod == NULL) |
| 1194 | return NULL; |
| 1195 | snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name); |
| 1196 | |
| 1197 | d = opendir(dname); |
| 1198 | if (d == NULL) { |
| 1199 | ERR(mod->ctx, "could not open '%s': %s\n", |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1200 | dname, strerror(errno)); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1201 | return NULL; |
| 1202 | } |
| 1203 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1204 | for (;;) { |
| 1205 | struct dirent de, *entp; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1206 | struct kmod_module *holder; |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1207 | struct kmod_list *l; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1208 | int err; |
| 1209 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1210 | err = readdir_r(d, &de, &entp); |
| 1211 | if (err != 0) { |
| 1212 | ERR(mod->ctx, "could not iterate for module '%s': %s\n", |
| 1213 | mod->name, strerror(-err)); |
| 1214 | goto fail; |
| 1215 | } |
| 1216 | |
| 1217 | if (entp == NULL) |
| 1218 | break; |
| 1219 | |
| 1220 | if (de.d_name[0] == '.') { |
| 1221 | if (de.d_name[1] == '\0' || |
| 1222 | (de.d_name[1] == '.' && de.d_name[2] == '\0')) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1223 | continue; |
| 1224 | } |
| 1225 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1226 | 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] | 1227 | if (err < 0) { |
| 1228 | ERR(mod->ctx, "could not create module for '%s': %s\n", |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1229 | de.d_name, strerror(-err)); |
| 1230 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1231 | } |
| 1232 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1233 | l = kmod_list_append(list, holder); |
| 1234 | if (l != NULL) { |
| 1235 | list = l; |
| 1236 | } else { |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1237 | ERR(mod->ctx, "out of memory\n"); |
| 1238 | kmod_module_unref(holder); |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1239 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | closedir(d); |
| 1244 | return list; |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 1245 | |
| 1246 | fail: |
| 1247 | closedir(d); |
| 1248 | kmod_module_unref_list(list); |
| 1249 | return NULL; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | struct kmod_module_section { |
| 1253 | unsigned long address; |
| 1254 | char name[]; |
| 1255 | }; |
| 1256 | |
| 1257 | static void kmod_module_section_free(struct kmod_module_section *section) |
| 1258 | { |
| 1259 | free(section); |
| 1260 | } |
| 1261 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1262 | /** |
| 1263 | * kmod_module_get_sections: |
| 1264 | * @mod: kmod module |
| 1265 | * |
| 1266 | * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The |
| 1267 | * structure contained in this list is internal to libkmod and their fields |
| 1268 | * can be obtained by calling kmod_module_section_get_name() and |
| 1269 | * kmod_module_section_get_address(). |
| 1270 | * |
| 1271 | * After use, free the @list by calling kmod_module_section_free_list(). |
| 1272 | * |
| 1273 | * Returns: a new list of kmod module sections on success or NULL on failure. |
| 1274 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1275 | KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod) |
| 1276 | { |
| 1277 | char dname[PATH_MAX]; |
| 1278 | struct kmod_list *list = NULL; |
| 1279 | DIR *d; |
| 1280 | int dfd; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1281 | |
| 1282 | if (mod == NULL) |
| 1283 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1284 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1285 | snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name); |
| 1286 | |
| 1287 | d = opendir(dname); |
| 1288 | if (d == NULL) { |
| 1289 | ERR(mod->ctx, "could not open '%s': %s\n", |
| 1290 | dname, strerror(errno)); |
| 1291 | return NULL; |
| 1292 | } |
| 1293 | |
| 1294 | dfd = dirfd(d); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1295 | |
| 1296 | for (;;) { |
| 1297 | struct dirent de, *entp; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1298 | struct kmod_module_section *section; |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1299 | struct kmod_list *l; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1300 | unsigned long address; |
| 1301 | size_t namesz; |
| 1302 | int fd, err; |
| 1303 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1304 | err = readdir_r(d, &de, &entp); |
| 1305 | if (err != 0) { |
| 1306 | ERR(mod->ctx, "could not iterate for module '%s': %s\n", |
| 1307 | mod->name, strerror(-err)); |
| 1308 | goto fail; |
| 1309 | } |
| 1310 | |
| 1311 | if (de.d_name[0] == '.') { |
| 1312 | if (de.d_name[1] == '\0' || |
| 1313 | (de.d_name[1] == '.' && de.d_name[2] == '\0')) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1314 | continue; |
| 1315 | } |
| 1316 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1317 | fd = openat(dfd, de.d_name, O_RDONLY); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1318 | if (fd < 0) { |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1319 | ERR(mod->ctx, "could not open '%s/%s': %m\n", |
| 1320 | dname, de.d_name); |
| 1321 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | err = read_str_ulong(fd, &address, 16); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1325 | close(fd); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1326 | if (err < 0) { |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1327 | ERR(mod->ctx, "could not read long from '%s/%s': %m\n", |
| 1328 | dname, de.d_name); |
| 1329 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1330 | } |
| 1331 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1332 | namesz = strlen(de.d_name) + 1; |
| 1333 | section = malloc(sizeof(*section) + namesz); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1334 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1335 | if (section == NULL) { |
| 1336 | ERR(mod->ctx, "out of memory\n"); |
| 1337 | goto fail; |
| 1338 | } |
| 1339 | |
| 1340 | section->address = address; |
| 1341 | memcpy(section->name, de.d_name, namesz); |
| 1342 | |
| 1343 | l = kmod_list_append(list, section); |
| 1344 | if (l != NULL) { |
| 1345 | list = l; |
| 1346 | } else { |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1347 | ERR(mod->ctx, "out of memory\n"); |
| 1348 | free(section); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1349 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1350 | } |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | closedir(d); |
| 1354 | return list; |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 1355 | |
| 1356 | fail: |
| 1357 | closedir(d); |
| 1358 | kmod_module_unref_list(list); |
| 1359 | return NULL; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1362 | /** |
| 1363 | * kmod_module_section_get_module_name: |
| 1364 | * @entry: a list entry representing a kmod module section |
| 1365 | * |
| 1366 | * Get the name of a kmod module section. |
| 1367 | * |
| 1368 | * After use, free the @list by calling kmod_module_section_free_list(). |
| 1369 | * |
| 1370 | * Returns: the name of this kmod module section on success or NULL on |
| 1371 | * failure. The string is owned by the section, do not free it. |
| 1372 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1373 | KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry) |
| 1374 | { |
| 1375 | struct kmod_module_section *section; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1376 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1377 | if (entry == NULL) |
| 1378 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1379 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1380 | section = entry->data; |
| 1381 | return section->name; |
| 1382 | } |
| 1383 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1384 | /** |
| 1385 | * kmod_module_section_get_address: |
| 1386 | * @entry: a list entry representing a kmod module section |
| 1387 | * |
| 1388 | * Get the address of a kmod module section. |
| 1389 | * |
| 1390 | * After use, free the @list by calling kmod_module_section_free_list(). |
| 1391 | * |
| 1392 | * Returns: the address of this kmod module section on success or ULONG_MAX |
| 1393 | * on failure. |
| 1394 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1395 | KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry) |
| 1396 | { |
| 1397 | struct kmod_module_section *section; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1398 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1399 | if (entry == NULL) |
| 1400 | return (unsigned long)-1; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame] | 1401 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1402 | section = entry->data; |
| 1403 | return section->address; |
| 1404 | } |
| 1405 | |
Lucas De Marchi | 7afc98a | 2011-12-14 12:06:18 -0200 | [diff] [blame] | 1406 | /** |
| 1407 | * kmod_module_section_free_list: |
| 1408 | * @list: kmod module section list |
| 1409 | * |
| 1410 | * Release the resources taken by @list |
| 1411 | */ |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 1412 | KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list) |
| 1413 | { |
| 1414 | while (list) { |
| 1415 | kmod_module_section_free(list->data); |
| 1416 | list = kmod_list_remove(list); |
| 1417 | } |
| 1418 | } |