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