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 |
| 8 | * License as published by the Free Software Foundation version 2.1. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 20 | #include <assert.h> |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stddef.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <unistd.h> |
| 26 | #include <errno.h> |
| 27 | #include <string.h> |
| 28 | #include <ctype.h> |
| 29 | #include <inttypes.h> |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 30 | #include <limits.h> |
| 31 | #include <dirent.h> |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/mman.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | #include "libkmod.h" |
| 38 | #include "libkmod-private.h" |
| 39 | |
| 40 | /** |
| 41 | * kmod_module: |
| 42 | * |
| 43 | * Opaque object representing a module. |
| 44 | */ |
| 45 | struct kmod_module { |
| 46 | struct kmod_ctx *ctx; |
Gustavo Sverzut Barbieri | f1fb6f8 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 47 | char *path; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 48 | struct kmod_list *dep; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 49 | char *options; |
| 50 | char *install_commands; |
| 51 | char *remove_commands; |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 52 | int n_dep; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 53 | int refcount; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 54 | struct { |
| 55 | bool dep : 1; |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 56 | bool options : 1; |
| 57 | bool install_commands : 1; |
| 58 | bool remove_commands : 1; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 59 | } init; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 60 | char name[]; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 61 | }; |
| 62 | |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 63 | inline char *modname_normalize(const char *modname, char buf[NAME_MAX], |
Lucas De Marchi | 6c343b1 | 2011-12-06 09:01:01 -0200 | [diff] [blame] | 64 | size_t *len) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 65 | { |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 66 | size_t s; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 67 | |
Gustavo Sverzut Barbieri | e1a6b30 | 2011-12-08 04:10:49 -0200 | [diff] [blame] | 68 | for (s = 0; s < NAME_MAX - 1; s++) { |
| 69 | const char c = modname[s]; |
| 70 | if (c == '-') |
| 71 | buf[s] = '_'; |
| 72 | else if (c == '\0' || c == '.') |
| 73 | break; |
| 74 | else |
| 75 | buf[s] = c; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 76 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 77 | |
Gustavo Sverzut Barbieri | e1a6b30 | 2011-12-08 04:10:49 -0200 | [diff] [blame] | 78 | buf[s] = '\0'; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 79 | |
| 80 | if (len) |
| 81 | *len = s; |
| 82 | |
Gustavo Sverzut Barbieri | e1a6b30 | 2011-12-08 04:10:49 -0200 | [diff] [blame] | 83 | return buf; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 84 | } |
| 85 | |
Lucas De Marchi | 6c343b1 | 2011-12-06 09:01:01 -0200 | [diff] [blame] | 86 | static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len) |
| 87 | { |
| 88 | char *modname; |
| 89 | |
| 90 | modname = basename(path); |
| 91 | if (modname == NULL || modname[0] == '\0') |
| 92 | return NULL; |
| 93 | |
| 94 | return modname_normalize(modname, buf, len); |
| 95 | } |
| 96 | |
Lucas De Marchi | c35347f | 2011-12-12 10:48:02 -0200 | [diff] [blame] | 97 | static inline const char *path_join(const char *path, size_t prefixlen, |
| 98 | char buf[PATH_MAX]) |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 99 | { |
| 100 | size_t pathlen; |
| 101 | |
| 102 | if (path[0] == '/') |
| 103 | return path; |
| 104 | |
| 105 | pathlen = strlen(path); |
| 106 | if (prefixlen + pathlen + 1 >= PATH_MAX) |
| 107 | return NULL; |
| 108 | |
| 109 | memcpy(buf + prefixlen, path, pathlen + 1); |
| 110 | return buf; |
| 111 | } |
| 112 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 113 | int kmod_module_parse_depline(struct kmod_module *mod, char *line) |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 114 | { |
| 115 | struct kmod_ctx *ctx = mod->ctx; |
| 116 | struct kmod_list *list = NULL; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 117 | const char *dirname; |
| 118 | char buf[PATH_MAX]; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 119 | char *p, *saveptr; |
| 120 | int err, n = 0; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 121 | size_t dirnamelen; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 122 | |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 123 | if (mod->init.dep) |
| 124 | return mod->n_dep; |
| 125 | assert(mod->dep == NULL); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 126 | mod->init.dep = true; |
| 127 | |
| 128 | p = strchr(line, ':'); |
| 129 | if (p == NULL) |
| 130 | return 0; |
| 131 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 132 | *p = '\0'; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 133 | dirname = kmod_get_dirname(mod->ctx); |
| 134 | dirnamelen = strlen(dirname); |
| 135 | if (dirnamelen + 2 >= PATH_MAX) |
| 136 | return 0; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 137 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 138 | memcpy(buf, dirname, dirnamelen); |
| 139 | buf[dirnamelen] = '/'; |
| 140 | dirnamelen++; |
| 141 | buf[dirnamelen] = '\0'; |
| 142 | |
| 143 | if (mod->path == NULL) { |
| 144 | const char *str = path_join(line, dirnamelen, buf); |
| 145 | if (str == NULL) |
| 146 | return 0; |
| 147 | mod->path = strdup(str); |
| 148 | if (mod->path == NULL) |
| 149 | return 0; |
| 150 | } |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 151 | |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 152 | p++; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 153 | for (p = strtok_r(p, " \t", &saveptr); p != NULL; |
| 154 | p = strtok_r(NULL, " \t", &saveptr)) { |
Lucas De Marchi | 1fc1c9a | 2011-12-02 10:00:03 -0200 | [diff] [blame] | 155 | struct kmod_module *depmod; |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 156 | const char *path; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 157 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 158 | path = path_join(p, dirnamelen, buf); |
| 159 | if (path == NULL) { |
| 160 | ERR(ctx, "could not join path '%s' and '%s'.\n", |
| 161 | dirname, p); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 162 | goto fail; |
| 163 | } |
| 164 | |
Gustavo Sverzut Barbieri | e18ad35 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 165 | err = kmod_module_new_from_path(ctx, path, &depmod); |
| 166 | if (err < 0) { |
| 167 | ERR(ctx, "ctx=%p path=%s error=%s\n", |
| 168 | ctx, path, strerror(-err)); |
| 169 | goto fail; |
| 170 | } |
| 171 | |
| 172 | DBG(ctx, "add dep: %s\n", path); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 173 | |
Lucas De Marchi | 1fc1c9a | 2011-12-02 10:00:03 -0200 | [diff] [blame] | 174 | list = kmod_list_append(list, depmod); |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 175 | n++; |
| 176 | } |
| 177 | |
| 178 | DBG(ctx, "%d dependencies for %s\n", n, mod->name); |
| 179 | |
| 180 | mod->dep = list; |
Gustavo Sverzut Barbieri | b6a534f | 2011-12-10 20:36:22 -0200 | [diff] [blame] | 181 | mod->n_dep = n; |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 182 | return n; |
| 183 | |
| 184 | fail: |
| 185 | kmod_module_unref_list(list); |
| 186 | mod->init.dep = false; |
| 187 | return err; |
| 188 | } |
| 189 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 190 | KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx, |
| 191 | const char *name, |
| 192 | struct kmod_module **mod) |
| 193 | { |
| 194 | struct kmod_module *m; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 195 | size_t namelen; |
Lucas De Marchi | 4f2bb7c | 2011-12-06 02:46:22 -0200 | [diff] [blame] | 196 | char name_norm[NAME_MAX]; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 197 | |
| 198 | if (ctx == NULL || name == NULL) |
| 199 | return -ENOENT; |
| 200 | |
Gustavo Sverzut Barbieri | e1a6b30 | 2011-12-08 04:10:49 -0200 | [diff] [blame] | 201 | modname_normalize(name, name_norm, &namelen); |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 202 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 203 | m = kmod_pool_get_module(ctx, name_norm); |
| 204 | if (m != NULL) { |
| 205 | *mod = kmod_module_ref(m); |
| 206 | return 0; |
| 207 | } |
| 208 | |
Lucas De Marchi | 4f2bb7c | 2011-12-06 02:46:22 -0200 | [diff] [blame] | 209 | m = calloc(1, sizeof(*m) + namelen + 1); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 210 | if (m == NULL) { |
| 211 | free(m); |
| 212 | return -ENOMEM; |
| 213 | } |
| 214 | |
| 215 | m->ctx = kmod_ref(ctx); |
Lucas De Marchi | 4f2bb7c | 2011-12-06 02:46:22 -0200 | [diff] [blame] | 216 | memcpy(m->name, name_norm, namelen + 1); |
Gustavo Sverzut Barbieri | 87ca03b | 2011-12-04 12:34:02 -0200 | [diff] [blame] | 217 | m->refcount = 1; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 218 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 219 | kmod_pool_add_module(ctx, m); |
| 220 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 221 | *mod = m; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx, |
| 227 | const char *path, |
| 228 | struct kmod_module **mod) |
| 229 | { |
| 230 | struct kmod_module *m; |
| 231 | int err; |
| 232 | struct stat st; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 233 | char name[NAME_MAX]; |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 234 | char *abspath; |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 235 | size_t namelen; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 236 | |
| 237 | if (ctx == NULL || path == NULL) |
| 238 | return -ENOENT; |
| 239 | |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 240 | abspath = path_make_absolute_cwd(path); |
| 241 | if (abspath == NULL) |
| 242 | return -ENOMEM; |
| 243 | |
| 244 | err = stat(abspath, &st); |
| 245 | if (err < 0) { |
| 246 | free(abspath); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 247 | return -errno; |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 248 | } |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 249 | |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 250 | path_to_modname(path, name, &namelen); |
| 251 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 252 | m = kmod_pool_get_module(ctx, name); |
| 253 | if (m != NULL) { |
Lucas De Marchi | 6bd0b8d | 2011-12-07 14:08:01 -0200 | [diff] [blame] | 254 | if (m->path == NULL) |
| 255 | m->path = abspath; |
| 256 | else if (streq(m->path, abspath)) |
| 257 | free(abspath); |
| 258 | else { |
| 259 | ERR(ctx, "kmod_module '%s' already exists with different path\n", |
| 260 | name); |
| 261 | free(abspath); |
| 262 | return -EEXIST; |
| 263 | } |
| 264 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 265 | *mod = kmod_module_ref(m); |
| 266 | return 0; |
| 267 | } |
| 268 | |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 269 | m = calloc(1, sizeof(*m) + namelen + 1); |
| 270 | if (m == NULL) |
| 271 | return -errno; |
| 272 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 273 | m->ctx = kmod_ref(ctx); |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 274 | memcpy(m->name, name, namelen); |
Lucas De Marchi | 71e975c | 2011-12-07 13:53:53 -0200 | [diff] [blame] | 275 | m->path = abspath; |
Gustavo Sverzut Barbieri | 87ca03b | 2011-12-04 12:34:02 -0200 | [diff] [blame] | 276 | m->refcount = 1; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 277 | |
Lucas De Marchi | fd186ae | 2011-12-06 03:38:37 -0200 | [diff] [blame] | 278 | kmod_pool_add_module(ctx, m); |
| 279 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 280 | *mod = m; |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod) |
| 286 | { |
| 287 | if (mod == NULL) |
| 288 | return NULL; |
| 289 | |
| 290 | if (--mod->refcount > 0) |
| 291 | return mod; |
| 292 | |
| 293 | DBG(mod->ctx, "kmod_module %p released\n", mod); |
| 294 | |
Lucas De Marchi | 7636e72 | 2011-12-01 17:56:03 -0200 | [diff] [blame] | 295 | kmod_module_unref_list(mod->dep); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 296 | kmod_unref(mod->ctx); |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 297 | free(mod->options); |
| 298 | free(mod->install_commands); |
| 299 | free(mod->remove_commands); |
Gustavo Sverzut Barbieri | f1fb6f8 | 2011-12-08 04:44:03 -0200 | [diff] [blame] | 300 | free(mod->path); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 301 | free(mod); |
| 302 | return NULL; |
| 303 | } |
| 304 | |
| 305 | KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod) |
| 306 | { |
| 307 | if (mod == NULL) |
| 308 | return NULL; |
| 309 | |
| 310 | mod->refcount++; |
| 311 | |
| 312 | return mod; |
| 313 | } |
| 314 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 315 | #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \ |
| 316 | do { \ |
| 317 | if ((_err) < 0) \ |
| 318 | goto _label_err; \ |
| 319 | if (*(_list) != NULL) \ |
| 320 | goto finish; \ |
| 321 | } while (0) |
| 322 | |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 323 | 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] | 324 | const char *given_alias, |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 325 | struct kmod_list **list) |
| 326 | { |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 327 | int err; |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 328 | char alias[NAME_MAX]; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 329 | |
| 330 | if (ctx == NULL || alias == NULL) |
| 331 | return -ENOENT; |
| 332 | |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 333 | if (list == NULL || *list != NULL) { |
| 334 | ERR(ctx, "An empty list is needed to create lookup\n"); |
| 335 | return -ENOSYS; |
| 336 | } |
| 337 | |
Gustavo Sverzut Barbieri | d917f27 | 2011-12-10 21:00:19 -0200 | [diff] [blame] | 338 | modname_normalize(given_alias, alias, NULL); |
| 339 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 340 | /* Aliases from config file override all the others */ |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 341 | err = kmod_lookup_alias_from_config(ctx, alias, list); |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 342 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 343 | |
Lucas De Marchi | 64700e4 | 2011-12-01 15:57:53 -0200 | [diff] [blame] | 344 | err = kmod_lookup_alias_from_moddep_file(ctx, alias, list); |
| 345 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
| 346 | |
Lucas De Marchi | 9ba6f57 | 2011-11-30 20:31:45 -0200 | [diff] [blame] | 347 | err = kmod_lookup_alias_from_symbols_file(ctx, alias, list); |
| 348 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 349 | |
Lucas De Marchi | 49e61ca | 2011-12-01 16:27:04 -0200 | [diff] [blame] | 350 | err = kmod_lookup_alias_from_aliases_file(ctx, alias, list); |
| 351 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
| 352 | |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 353 | finish: |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 354 | |
| 355 | return err; |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 356 | fail: |
| 357 | kmod_module_unref_list(*list); |
| 358 | *list = NULL; |
Lucas De Marchi | 84f4220 | 2011-12-02 10:03:34 -0200 | [diff] [blame] | 359 | return err; |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 360 | } |
Lucas De Marchi | b14dcfd | 2011-11-30 20:29:51 -0200 | [diff] [blame] | 361 | #undef CHECK_ERR_AND_FINISH |
| 362 | |
Lucas De Marchi | 7f3eb0c | 2011-11-30 19:03:41 -0200 | [diff] [blame] | 363 | |
| 364 | KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list) |
| 365 | { |
| 366 | for (; list != NULL; list = kmod_list_remove(list)) |
| 367 | kmod_module_unref(list->data); |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 372 | 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] | 373 | { |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 374 | struct kmod_list *l, *l_new, *list_new = NULL; |
| 375 | |
| 376 | if (mod == NULL) |
| 377 | return NULL; |
| 378 | |
Lucas De Marchi | 671d489 | 2011-12-05 20:23:05 -0200 | [diff] [blame] | 379 | if (!mod->init.dep) { |
| 380 | /* lazy init */ |
| 381 | char *line = kmod_search_moddep(mod->ctx, mod->name); |
| 382 | |
| 383 | if (line == NULL) |
| 384 | return NULL; |
| 385 | |
| 386 | kmod_module_parse_depline((struct kmod_module *)mod, line); |
| 387 | free(line); |
| 388 | |
| 389 | if (!mod->init.dep) |
| 390 | return NULL; |
| 391 | } |
Lucas De Marchi | f1cd799 | 2011-12-05 19:40:45 -0200 | [diff] [blame] | 392 | |
| 393 | kmod_list_foreach(l, mod->dep) { |
| 394 | l_new = kmod_list_append(list_new, kmod_module_ref(l->data)); |
| 395 | if (l_new == NULL) { |
| 396 | kmod_module_unref(l->data); |
| 397 | goto fail; |
| 398 | } |
| 399 | |
| 400 | list_new = l_new; |
| 401 | } |
| 402 | |
| 403 | return list_new; |
| 404 | |
| 405 | fail: |
| 406 | ERR(mod->ctx, "out of memory\n"); |
| 407 | kmod_module_unref_list(list_new); |
| 408 | return NULL; |
Lucas De Marchi | 0835fc3 | 2011-12-01 20:06:08 -0200 | [diff] [blame] | 409 | } |
| 410 | |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 411 | 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] | 412 | { |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 413 | if (entry == NULL) |
| 414 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 415 | |
Gustavo Sverzut Barbieri | ad4d1ae | 2011-12-04 13:14:11 -0200 | [diff] [blame] | 416 | return kmod_module_ref(entry->data); |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 417 | } |
| 418 | |
Gustavo Sverzut Barbieri | 69f9dd4 | 2011-12-04 14:02:30 -0200 | [diff] [blame] | 419 | KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod) |
| 420 | { |
| 421 | // FIXME TODO: this should be available from /sys/module/foo |
| 422 | FILE *fp; |
| 423 | char line[4096]; |
| 424 | int lineno = 0; |
| 425 | long size = -ENOENT; |
| 426 | |
| 427 | if (mod == NULL) |
| 428 | return -ENOENT; |
| 429 | |
| 430 | fp = fopen("/proc/modules", "r"); |
| 431 | if (fp == NULL) { |
| 432 | int err = -errno; |
| 433 | ERR(mod->ctx, |
| 434 | "could not open /proc/modules: %s\n", strerror(errno)); |
| 435 | return err; |
| 436 | } |
| 437 | |
| 438 | while (fgets(line, sizeof(line), fp)) { |
| 439 | char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr); |
| 440 | long value; |
| 441 | |
| 442 | lineno++; |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 443 | if (tok == NULL || !streq(tok, mod->name)) |
Gustavo Sverzut Barbieri | 69f9dd4 | 2011-12-04 14:02:30 -0200 | [diff] [blame] | 444 | continue; |
| 445 | |
| 446 | tok = strtok_r(NULL, " \t", &saveptr); |
| 447 | if (tok == NULL) { |
| 448 | ERR(mod->ctx, |
| 449 | "invalid line format at /proc/modules:%d\n", lineno); |
| 450 | break; |
| 451 | } |
| 452 | |
| 453 | value = strtol(tok, &endptr, 10); |
| 454 | if (endptr == tok || *endptr != '\0') { |
| 455 | ERR(mod->ctx, |
| 456 | "invalid line format at /proc/modules:%d\n", lineno); |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | size = value; |
| 461 | break; |
| 462 | } |
| 463 | fclose(fp); |
| 464 | return size; |
| 465 | } |
| 466 | |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 467 | 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] | 468 | { |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 469 | return mod->name; |
| 470 | } |
| 471 | |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 472 | /* |
| 473 | * Relative paths are relative to dirname. Absolute paths are only used when |
| 474 | * user created kmod_module by giving a path |
| 475 | */ |
Gustavo Sverzut Barbieri | 1ce08a5 | 2011-12-02 20:24:07 -0200 | [diff] [blame] | 476 | 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] | 477 | { |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 478 | char *line; |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 479 | |
Gustavo Sverzut Barbieri | d01c67e | 2011-12-11 19:42:02 -0200 | [diff] [blame] | 480 | 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] | 481 | |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 482 | if (mod->path != NULL) |
| 483 | return mod->path; |
| 484 | if (mod->init.dep) |
| 485 | return NULL; |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 486 | |
Lucas De Marchi | e005fac | 2011-12-08 10:42:34 -0200 | [diff] [blame] | 487 | /* lazy init */ |
| 488 | line = kmod_search_moddep(mod->ctx, mod->name); |
| 489 | if (line == NULL) |
| 490 | return NULL; |
| 491 | |
| 492 | kmod_module_parse_depline((struct kmod_module *) mod, line); |
| 493 | free(line); |
Lucas De Marchi | c5e7b1f | 2011-12-05 20:28:13 -0200 | [diff] [blame] | 494 | |
Lucas De Marchi | 6e869df | 2011-11-30 19:01:01 -0200 | [diff] [blame] | 495 | return mod->path; |
| 496 | } |
| 497 | |
| 498 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 499 | extern long delete_module(const char *name, unsigned int flags); |
| 500 | |
| 501 | KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, |
| 502 | unsigned int flags) |
| 503 | { |
| 504 | int err; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 505 | |
| 506 | if (mod == NULL) |
| 507 | return -ENOENT; |
| 508 | |
| 509 | /* Filter out other flags */ |
| 510 | flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT); |
| 511 | |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 512 | err = delete_module(mod->name, flags); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 513 | if (err != 0) { |
Lucas De Marchi | d753b8c | 2011-12-05 18:14:51 -0200 | [diff] [blame] | 514 | ERR(mod->ctx, "Removing '%s': %s\n", mod->name, |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 515 | strerror(-err)); |
| 516 | return err; |
| 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | extern long init_module(void *mem, unsigned long len, const char *args); |
| 523 | |
| 524 | KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod, |
Gustavo Sverzut Barbieri | 3a721bb | 2011-12-10 21:02:39 -0200 | [diff] [blame] | 525 | unsigned int flags, |
| 526 | const char *options) |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 527 | { |
| 528 | int err; |
| 529 | void *mmaped_file; |
| 530 | struct stat st; |
| 531 | int fd; |
Gustavo Sverzut Barbieri | 3a721bb | 2011-12-10 21:02:39 -0200 | [diff] [blame] | 532 | const char *args = options ? options : ""; |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 533 | |
| 534 | if (mod == NULL) |
| 535 | return -ENOENT; |
| 536 | |
| 537 | if (mod->path == NULL) { |
Lucas De Marchi | 1b2e26a | 2011-11-25 01:28:39 -0200 | [diff] [blame] | 538 | 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] | 539 | return -ENOSYS; |
| 540 | } |
| 541 | |
| 542 | if (flags != 0) |
Lucas De Marchi | 1b2e26a | 2011-11-25 01:28:39 -0200 | [diff] [blame] | 543 | INFO(mod->ctx, "Flags are not implemented yet\n"); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 544 | |
| 545 | if ((fd = open(mod->path, O_RDONLY)) < 0) { |
| 546 | err = -errno; |
| 547 | return err; |
| 548 | } |
| 549 | |
Lucas De Marchi | b418a82 | 2011-12-01 23:13:27 -0200 | [diff] [blame] | 550 | fstat(fd, &st); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 551 | |
| 552 | if ((mmaped_file = mmap(0, st.st_size, PROT_READ, |
| 553 | MAP_PRIVATE, fd, 0)) == MAP_FAILED) { |
| 554 | close(fd); |
| 555 | return -errno; |
| 556 | } |
| 557 | |
| 558 | err = init_module(mmaped_file, st.st_size, args); |
| 559 | if (err < 0) |
Lucas De Marchi | 1b2e26a | 2011-11-25 01:28:39 -0200 | [diff] [blame] | 560 | ERR(mod->ctx, "Failed to insert module '%s'\n", mod->path); |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 561 | |
| 562 | munmap(mmaped_file, st.st_size); |
| 563 | close(fd); |
| 564 | |
| 565 | return err; |
| 566 | } |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 567 | |
| 568 | KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state) |
| 569 | { |
| 570 | switch (state) { |
| 571 | case KMOD_MODULE_BUILTIN: |
| 572 | return "builtin"; |
| 573 | case KMOD_MODULE_LIVE: |
| 574 | return "live"; |
| 575 | case KMOD_MODULE_COMING: |
| 576 | return "coming"; |
| 577 | case KMOD_MODULE_GOING: |
| 578 | return "going"; |
| 579 | default: |
| 580 | return NULL; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod) |
| 585 | { |
| 586 | char path[PATH_MAX], buf[32]; |
| 587 | int fd, err, pathlen; |
| 588 | |
| 589 | pathlen = snprintf(path, sizeof(path), |
| 590 | "/sys/module/%s/initstate", mod->name); |
| 591 | fd = open(path, O_RDONLY); |
| 592 | if (fd < 0) { |
| 593 | err = -errno; |
| 594 | |
| 595 | if (pathlen > (int)sizeof("/initstate") - 1) { |
| 596 | struct stat st; |
| 597 | path[pathlen - (sizeof("/initstate") - 1)] = '\0'; |
| 598 | if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) |
| 599 | return KMOD_MODULE_BUILTIN; |
| 600 | } |
| 601 | |
Gustavo Sverzut Barbieri | 926f67a | 2011-12-11 19:33:03 -0200 | [diff] [blame] | 602 | DBG(mod->ctx, "could not open '%s': %s\n", |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 603 | path, strerror(-err)); |
| 604 | return err; |
| 605 | } |
| 606 | |
| 607 | err = read_str_safe(fd, buf, sizeof(buf)); |
| 608 | close(fd); |
| 609 | if (err < 0) { |
| 610 | ERR(mod->ctx, "could not read from '%s': %s\n", |
| 611 | path, strerror(-err)); |
| 612 | return err; |
| 613 | } |
| 614 | |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 615 | if (streq(buf, "live\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 616 | return KMOD_MODULE_LIVE; |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 617 | else if (streq(buf, "coming\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 618 | return KMOD_MODULE_COMING; |
Lucas De Marchi | 877e80c | 2011-12-07 02:26:31 -0200 | [diff] [blame] | 619 | else if (streq(buf, "going\n")) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 620 | return KMOD_MODULE_GOING; |
| 621 | |
| 622 | ERR(mod->ctx, "unknown %s: '%s'\n", path, buf); |
| 623 | return -EINVAL; |
| 624 | } |
| 625 | |
| 626 | KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) |
| 627 | { |
| 628 | char path[PATH_MAX]; |
| 629 | long refcnt; |
| 630 | int fd, err; |
| 631 | |
| 632 | snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name); |
| 633 | fd = open(path, O_RDONLY); |
| 634 | if (fd < 0) { |
| 635 | err = -errno; |
| 636 | ERR(mod->ctx, "could not open '%s': %s\n", |
| 637 | path, strerror(errno)); |
| 638 | return err; |
| 639 | } |
| 640 | |
| 641 | err = read_str_long(fd, &refcnt, 10); |
| 642 | close(fd); |
| 643 | if (err < 0) { |
| 644 | ERR(mod->ctx, "could not read integer from '%s': '%s'\n", |
| 645 | path, strerror(-err)); |
| 646 | return err; |
| 647 | } |
| 648 | |
| 649 | return (int)refcnt; |
| 650 | } |
| 651 | |
| 652 | KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod) |
| 653 | { |
| 654 | char dname[PATH_MAX]; |
| 655 | struct kmod_list *list = NULL; |
| 656 | DIR *d; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 657 | |
| 658 | if (mod == NULL) |
| 659 | return NULL; |
| 660 | snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name); |
| 661 | |
| 662 | d = opendir(dname); |
| 663 | if (d == NULL) { |
| 664 | ERR(mod->ctx, "could not open '%s': %s\n", |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 665 | dname, strerror(errno)); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 666 | return NULL; |
| 667 | } |
| 668 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 669 | for (;;) { |
| 670 | struct dirent de, *entp; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 671 | struct kmod_module *holder; |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 672 | struct kmod_list *l; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 673 | int err; |
| 674 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 675 | err = readdir_r(d, &de, &entp); |
| 676 | if (err != 0) { |
| 677 | ERR(mod->ctx, "could not iterate for module '%s': %s\n", |
| 678 | mod->name, strerror(-err)); |
| 679 | goto fail; |
| 680 | } |
| 681 | |
| 682 | if (entp == NULL) |
| 683 | break; |
| 684 | |
| 685 | if (de.d_name[0] == '.') { |
| 686 | if (de.d_name[1] == '\0' || |
| 687 | (de.d_name[1] == '.' && de.d_name[2] == '\0')) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 688 | continue; |
| 689 | } |
| 690 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 691 | 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] | 692 | if (err < 0) { |
| 693 | ERR(mod->ctx, "could not create module for '%s': %s\n", |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 694 | de.d_name, strerror(-err)); |
| 695 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 696 | } |
| 697 | |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 698 | l = kmod_list_append(list, holder); |
| 699 | if (l != NULL) { |
| 700 | list = l; |
| 701 | } else { |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 702 | ERR(mod->ctx, "out of memory\n"); |
| 703 | kmod_module_unref(holder); |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 704 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | closedir(d); |
| 709 | return list; |
Lucas De Marchi | 53886dd | 2011-12-05 13:24:23 -0200 | [diff] [blame] | 710 | |
| 711 | fail: |
| 712 | closedir(d); |
| 713 | kmod_module_unref_list(list); |
| 714 | return NULL; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | struct kmod_module_section { |
| 718 | unsigned long address; |
| 719 | char name[]; |
| 720 | }; |
| 721 | |
| 722 | static void kmod_module_section_free(struct kmod_module_section *section) |
| 723 | { |
| 724 | free(section); |
| 725 | } |
| 726 | |
| 727 | KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod) |
| 728 | { |
| 729 | char dname[PATH_MAX]; |
| 730 | struct kmod_list *list = NULL; |
| 731 | DIR *d; |
| 732 | int dfd; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 733 | |
| 734 | if (mod == NULL) |
| 735 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 736 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 737 | snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name); |
| 738 | |
| 739 | d = opendir(dname); |
| 740 | if (d == NULL) { |
| 741 | ERR(mod->ctx, "could not open '%s': %s\n", |
| 742 | dname, strerror(errno)); |
| 743 | return NULL; |
| 744 | } |
| 745 | |
| 746 | dfd = dirfd(d); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 747 | |
| 748 | for (;;) { |
| 749 | struct dirent de, *entp; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 750 | struct kmod_module_section *section; |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 751 | struct kmod_list *l; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 752 | unsigned long address; |
| 753 | size_t namesz; |
| 754 | int fd, err; |
| 755 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 756 | err = readdir_r(d, &de, &entp); |
| 757 | if (err != 0) { |
| 758 | ERR(mod->ctx, "could not iterate for module '%s': %s\n", |
| 759 | mod->name, strerror(-err)); |
| 760 | goto fail; |
| 761 | } |
| 762 | |
| 763 | if (de.d_name[0] == '.') { |
| 764 | if (de.d_name[1] == '\0' || |
| 765 | (de.d_name[1] == '.' && de.d_name[2] == '\0')) |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 766 | continue; |
| 767 | } |
| 768 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 769 | fd = openat(dfd, de.d_name, O_RDONLY); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 770 | if (fd < 0) { |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 771 | ERR(mod->ctx, "could not open '%s/%s': %m\n", |
| 772 | dname, de.d_name); |
| 773 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | err = read_str_ulong(fd, &address, 16); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 777 | close(fd); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 778 | if (err < 0) { |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 779 | ERR(mod->ctx, "could not read long from '%s/%s': %m\n", |
| 780 | dname, de.d_name); |
| 781 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 782 | } |
| 783 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 784 | namesz = strlen(de.d_name) + 1; |
| 785 | section = malloc(sizeof(*section) + namesz); |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 786 | |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 787 | if (section == NULL) { |
| 788 | ERR(mod->ctx, "out of memory\n"); |
| 789 | goto fail; |
| 790 | } |
| 791 | |
| 792 | section->address = address; |
| 793 | memcpy(section->name, de.d_name, namesz); |
| 794 | |
| 795 | l = kmod_list_append(list, section); |
| 796 | if (l != NULL) { |
| 797 | list = l; |
| 798 | } else { |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 799 | ERR(mod->ctx, "out of memory\n"); |
| 800 | free(section); |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 801 | goto fail; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 802 | } |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | closedir(d); |
| 806 | return list; |
Lucas De Marchi | 40923bd | 2011-12-05 13:40:16 -0200 | [diff] [blame] | 807 | |
| 808 | fail: |
| 809 | closedir(d); |
| 810 | kmod_module_unref_list(list); |
| 811 | return NULL; |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry) |
| 815 | { |
| 816 | struct kmod_module_section *section; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 817 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 818 | if (entry == NULL) |
| 819 | return NULL; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 820 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 821 | section = entry->data; |
| 822 | return section->name; |
| 823 | } |
| 824 | |
| 825 | KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry) |
| 826 | { |
| 827 | struct kmod_module_section *section; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 828 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 829 | if (entry == NULL) |
| 830 | return (unsigned long)-1; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 831 | |
Gustavo Sverzut Barbieri | f12ae3c | 2011-12-04 12:40:00 -0200 | [diff] [blame] | 832 | section = entry->data; |
| 833 | return section->address; |
| 834 | } |
| 835 | |
| 836 | KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list) |
| 837 | { |
| 838 | while (list) { |
| 839 | kmod_module_section_free(list->data); |
| 840 | list = kmod_list_remove(list); |
| 841 | } |
| 842 | } |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 843 | |
| 844 | KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod) |
| 845 | { |
| 846 | if (mod == NULL) |
| 847 | return NULL; |
| 848 | |
| 849 | if (!mod->init.options) { |
| 850 | /* lazy init */ |
| 851 | struct kmod_module *m = (struct kmod_module *)mod; |
| 852 | const struct kmod_list *l, *ctx_options; |
| 853 | char *opts = NULL; |
| 854 | size_t optslen = 0; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 855 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 856 | ctx_options = kmod_get_options(mod->ctx); |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 857 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 858 | kmod_list_foreach(l, ctx_options) { |
| 859 | const char *modname = kmod_option_get_modname(l); |
| 860 | const char *str; |
| 861 | size_t len; |
| 862 | void *tmp; |
| 863 | |
| 864 | if (strcmp(modname, mod->name) != 0) |
| 865 | continue; |
| 866 | |
| 867 | str = kmod_option_get_options(l); |
| 868 | len = strlen(str); |
| 869 | if (len < 1) |
| 870 | continue; |
| 871 | |
| 872 | tmp = realloc(opts, optslen + len + 2); |
| 873 | if (tmp == NULL) { |
| 874 | free(opts); |
| 875 | goto failed; |
| 876 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 877 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 878 | opts = tmp; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 879 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 880 | if (optslen > 0) { |
| 881 | opts[optslen] = ' '; |
| 882 | optslen++; |
| 883 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 884 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 885 | memcpy(opts + optslen, str, len); |
| 886 | optslen += len; |
| 887 | opts[optslen] = '\0'; |
| 888 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 889 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 890 | m->init.options = true; |
| 891 | m->options = opts; |
| 892 | } |
| 893 | |
| 894 | return mod->options; |
| 895 | |
| 896 | failed: |
| 897 | ERR(mod->ctx, "out of memory\n"); |
| 898 | return NULL; |
| 899 | } |
| 900 | |
| 901 | KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod) |
| 902 | { |
| 903 | if (mod == NULL) |
| 904 | return NULL; |
| 905 | |
| 906 | if (!mod->init.install_commands) { |
| 907 | /* lazy init */ |
| 908 | struct kmod_module *m = (struct kmod_module *)mod; |
| 909 | const struct kmod_list *l, *ctx_install_commands; |
| 910 | char *cmds = NULL; |
| 911 | size_t cmdslen = 0; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 912 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 913 | ctx_install_commands = kmod_get_install_commands(mod->ctx); |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 914 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 915 | kmod_list_foreach(l, ctx_install_commands) { |
| 916 | const char *modname = kmod_command_get_modname(l); |
| 917 | const char *str; |
| 918 | size_t len; |
| 919 | void *tmp; |
| 920 | |
| 921 | if (strcmp(modname, mod->name) != 0) |
| 922 | continue; |
| 923 | |
| 924 | str = kmod_command_get_command(l); |
| 925 | len = strlen(str); |
| 926 | if (len < 1) |
| 927 | continue; |
| 928 | |
| 929 | tmp = realloc(cmds, cmdslen + len + 2); |
| 930 | if (tmp == NULL) { |
| 931 | free(cmds); |
| 932 | goto failed; |
| 933 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 934 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 935 | cmds = tmp; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 936 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 937 | if (cmdslen > 0) { |
| 938 | cmds[cmdslen] = ';'; |
| 939 | cmdslen++; |
| 940 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 941 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 942 | memcpy(cmds + cmdslen, str, len); |
| 943 | cmdslen += len; |
| 944 | cmds[cmdslen] = '\0'; |
| 945 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 946 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 947 | m->init.install_commands = true; |
| 948 | m->install_commands = cmds; |
| 949 | } |
| 950 | |
| 951 | return mod->install_commands; |
| 952 | |
| 953 | failed: |
| 954 | ERR(mod->ctx, "out of memory\n"); |
| 955 | return NULL; |
| 956 | } |
| 957 | |
| 958 | KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod) |
| 959 | { |
| 960 | if (mod == NULL) |
| 961 | return NULL; |
| 962 | |
| 963 | if (!mod->init.remove_commands) { |
| 964 | /* lazy init */ |
| 965 | struct kmod_module *m = (struct kmod_module *)mod; |
| 966 | const struct kmod_list *l, *ctx_remove_commands; |
| 967 | char *cmds = NULL; |
| 968 | size_t cmdslen = 0; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 969 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 970 | ctx_remove_commands = kmod_get_remove_commands(mod->ctx); |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 971 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 972 | kmod_list_foreach(l, ctx_remove_commands) { |
| 973 | const char *modname = kmod_command_get_modname(l); |
| 974 | const char *str; |
| 975 | size_t len; |
| 976 | void *tmp; |
| 977 | |
| 978 | if (strcmp(modname, mod->name) != 0) |
| 979 | continue; |
| 980 | |
| 981 | str = kmod_command_get_command(l); |
| 982 | len = strlen(str); |
| 983 | if (len < 1) |
| 984 | continue; |
| 985 | |
| 986 | tmp = realloc(cmds, cmdslen + len + 2); |
| 987 | if (tmp == NULL) { |
| 988 | free(cmds); |
| 989 | goto failed; |
| 990 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 991 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 992 | cmds = tmp; |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 993 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 994 | if (cmdslen > 0) { |
| 995 | cmds[cmdslen] = ';'; |
| 996 | cmdslen++; |
| 997 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 998 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 999 | memcpy(cmds + cmdslen, str, len); |
| 1000 | cmdslen += len; |
| 1001 | cmds[cmdslen] = '\0'; |
| 1002 | } |
Lucas De Marchi | 28c175e | 2011-12-12 11:52:59 -0200 | [diff] [blame^] | 1003 | |
Gustavo Sverzut Barbieri | bd3f553 | 2011-12-10 20:47:01 -0200 | [diff] [blame] | 1004 | m->init.remove_commands = true; |
| 1005 | m->remove_commands = cmds; |
| 1006 | } |
| 1007 | |
| 1008 | return mod->remove_commands; |
| 1009 | |
| 1010 | failed: |
| 1011 | ERR(mod->ctx, "out of memory\n"); |
| 1012 | return NULL; |
| 1013 | } |