diff options
author | Erik Troan <ewt@redhat.com> | 1999-07-19 19:00:32 +0000 |
---|---|---|
committer | Erik Troan <ewt@redhat.com> | 1999-07-19 19:00:32 +0000 |
commit | 21e09c6da1652e378ed26de8fd367f521bd59e67 (patch) | |
tree | 6e845029134b99735a5dd5ac664ad7d1b40fff0a /loader/modules.c | |
parent | 01c3adadca9f1035f03759ec03d934f6aa5c5fdc (diff) | |
download | anaconda-21e09c6da1652e378ed26de8fd367f521bd59e67.tar.gz anaconda-21e09c6da1652e378ed26de8fd367f521bd59e67.tar.xz anaconda-21e09c6da1652e378ed26de8fd367f521bd59e67.zip |
*** empty log message ***
Diffstat (limited to 'loader/modules.c')
-rw-r--r-- | loader/modules.c | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/loader/modules.c b/loader/modules.c new file mode 100644 index 000000000..bfeda0909 --- /dev/null +++ b/loader/modules.c @@ -0,0 +1,187 @@ +#include <alloca.h> +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> + +#include "isys/isys.h" + +#include "modules.h" + +struct moduleDependency_s { + char * name; + char ** deps; +}; + +struct moduleList_s { + char * modules[50]; + int numModules; +}; + +static int moduleLoaded(moduleList modList, const char * name); + +int mlReadLoadedList(moduleList * mlp) { + int fd; + char * start; + char * end; + char buf[4096]; + struct stat sb; + int i; + moduleList ml; + + if ((fd = open("/proc/modules", O_RDONLY)) < 0) + return -1; + + fstat(fd, &sb); + i = read(fd, buf, sizeof(buf)); + buf[i] = '\0'; + close(fd); + + ml = malloc(sizeof(*ml)); + ml->numModules = 0; + + start = buf; + while (start && *start) { + end = start; + while (!isspace(*end) && *end != '\n') end++; + *end = '\0'; + ml->modules[ml->numModules] = strdup(start); + *end = ' '; + ml->numModules++; + start = strchr(end, '\n'); + if (start) start++; + } + + *mlp = ml; + + return 0; +} + +void mlFreeList(moduleList ml) { + int i; + + for (i = 0; i < ml->numModules; i++) + free(ml->modules[i]); + free(ml); +} + +moduleDeps mlNewDeps(void) { + moduleDeps md; + + md = malloc(sizeof(*md)); + md->name = NULL; + md->deps = NULL; + + return md; +} + +int mlLoadDeps(moduleDeps moduleDepList, const char * path) { + int fd; + char * buf; + struct stat sb; + char * start, * end, * chptr; + int i, numItems; + moduleDeps nextDep; + + fd = open(path, O_RDONLY); + if (fd < 0) { + return -1; + } + + fstat(fd, &sb); + buf = alloca(sb.st_size + 1); + read(fd, buf, sb.st_size); + buf[sb.st_size] = '\0'; + close(fd); + + start = buf; + numItems = 0; + while (start) { + numItems++; + start = strchr(start + 1, '\n'); + } + + for (nextDep = moduleDepList; nextDep->name; nextDep++) numItems++; + + moduleDepList = realloc(moduleDepList, sizeof(*moduleDepList) * numItems); + for (nextDep = moduleDepList; nextDep->name; nextDep++) ; + + start = buf; + while (start < (buf + sb.st_size) && *start) { + end = strchr(start, '\n'); + *end = '\0'; + + chptr = strchr(start, ':'); + if (!chptr) { + start = end + 1; + continue; + } + + *chptr++ = '\0'; + while (*chptr && isspace(*chptr)) chptr++; + if (!*chptr) { + start = end + 1; + continue; + } + + /* found something */ + nextDep->name = strdup(start); + i = strlen(chptr) / 3; + nextDep->deps = malloc(sizeof(char **) * i); + start = chptr, i = 0; + while (start && *start) { + chptr = strchr(start, ' '); + if (chptr) *chptr = '\0'; + nextDep->deps[i++] = strdup(start); + if (chptr) + start = chptr + 1; + else + start = NULL; + while (start && *start && isspace(*start)) start++; + } + nextDep++; + + start = end + 1; + } + + nextDep->name = NULL; + nextDep->deps = NULL; + moduleDepList = realloc(moduleDepList, sizeof(*moduleDepList) * + (nextDep - moduleDepList + 1)); + + return 0; +} + +static int moduleLoaded(moduleList modList, const char * name) { + int i; + + for (i = 0; i < modList->numModules; i++) + if (!strcmp(modList->modules[i], name)) return 1; + + return 0; +} + +int mlLoadModule(struct moduleInfo * modInfo, moduleList modLoaded, + moduleDeps modDeps) { + moduleDeps dep; + char ** nextDep; + + for (dep = modDeps; dep->name && strcmp(dep->name, modInfo->moduleName); + dep++); + + if (dep) { + nextDep = dep->deps; + while (*nextDep) { + if (!moduleLoaded(modLoaded, *nextDep)) { + insmod(*nextDep, NULL); + } + + dep++; + } + } + + return insmod(modInfo->moduleName, NULL); +} |