The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/boot/common/module.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/8.2/sys/boot/common/module.c 202343 2010-01-15 11:26:20Z nyan $");
   29 
   30 /*
   31  * file/module function dispatcher, support, etc.
   32  */
   33 
   34 #include <stand.h>
   35 #include <string.h>
   36 #include <sys/param.h>
   37 #include <sys/linker.h>
   38 #include <sys/module.h>
   39 #include <sys/queue.h>
   40 
   41 #include "bootstrap.h"
   42 
   43 #define MDIR_REMOVED    0x0001
   44 #define MDIR_NOHINTS    0x0002
   45 
   46 struct moduledir {
   47         char    *d_path;        /* path of modules directory */
   48         u_char  *d_hints;       /* content of linker.hints file */
   49         int     d_hintsz;       /* size of hints data */
   50         int     d_flags;
   51         STAILQ_ENTRY(moduledir) d_link;
   52 };
   53 
   54 static int                      file_load(char *filename, vm_offset_t dest, struct preloaded_file **result);
   55 static int                      file_loadraw(char *type, char *name);
   56 static int                      file_load_dependencies(struct preloaded_file *base_mod);
   57 static char *                   file_search(const char *name, char **extlist);
   58 static struct kernel_module *   file_findmodule(struct preloaded_file *fp, char *modname, struct mod_depend *verinfo);
   59 static int                      file_havepath(const char *name);
   60 static char                     *mod_searchmodule(char *name, struct mod_depend *verinfo);
   61 static void                     file_insert_tail(struct preloaded_file *mp);
   62 struct file_metadata*           metadata_next(struct file_metadata *base_mp, int type);
   63 static void                     moduledir_readhints(struct moduledir *mdp);
   64 static void                     moduledir_rebuild(void);
   65 
   66 /* load address should be tweaked by first module loaded (kernel) */
   67 static vm_offset_t      loadaddr = 0;
   68 
   69 static const char       *default_searchpath ="/boot/kernel;/boot/modules";
   70 
   71 static STAILQ_HEAD(, moduledir) moduledir_list = STAILQ_HEAD_INITIALIZER(moduledir_list);
   72 
   73 struct preloaded_file *preloaded_files = NULL;
   74 
   75 static char *kld_ext_list[] = {
   76     ".ko",
   77     "",
   78     ".debug",
   79     NULL
   80 };
   81 
   82 
   83 /*
   84  * load an object, either a disk file or code module.
   85  *
   86  * To load a file, the syntax is:
   87  *
   88  * load -t <type> <path>
   89  *
   90  * code modules are loaded as:
   91  *
   92  * load <path> <options>
   93  */
   94 
   95 COMMAND_SET(load, "load", "load a kernel or module", command_load);
   96 
   97 static int
   98 command_load(int argc, char *argv[])
   99 {
  100     char        *typestr;
  101     int         dofile, dokld, ch, error;
  102     
  103     dokld = dofile = 0;
  104     optind = 1;
  105     optreset = 1;
  106     typestr = NULL;
  107     if (argc == 1) {
  108         command_errmsg = "no filename specified";
  109         return(CMD_ERROR);
  110     }
  111     while ((ch = getopt(argc, argv, "kt:")) != -1) {
  112         switch(ch) {
  113         case 'k':
  114             dokld = 1;
  115             break;
  116         case 't':
  117             typestr = optarg;
  118             dofile = 1;
  119             break;
  120         case '?':
  121         default:
  122             /* getopt has already reported an error */
  123             return(CMD_OK);
  124         }
  125     }
  126     argv += (optind - 1);
  127     argc -= (optind - 1);
  128 
  129     /*
  130      * Request to load a raw file?
  131      */
  132     if (dofile) {
  133         if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
  134             command_errmsg = "invalid load type";
  135             return(CMD_ERROR);
  136         }
  137         return(file_loadraw(typestr, argv[1]));
  138     }
  139     /*
  140      * Do we have explicit KLD load ?
  141      */
  142     if (dokld || file_havepath(argv[1])) {
  143         error = mod_loadkld(argv[1], argc - 2, argv + 2);
  144         if (error == EEXIST)
  145             sprintf(command_errbuf, "warning: KLD '%s' already loaded", argv[1]);
  146         return (error == 0 ? CMD_OK : CMD_ERROR);
  147     }
  148     /*
  149      * Looks like a request for a module.
  150      */
  151     error = mod_load(argv[1], NULL, argc - 2, argv + 2);
  152     if (error == EEXIST)
  153         sprintf(command_errbuf, "warning: module '%s' already loaded", argv[1]);
  154     return (error == 0 ? CMD_OK : CMD_ERROR);
  155 }
  156 
  157 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
  158 
  159 static int
  160 command_load_geli(int argc, char *argv[])
  161 {
  162     char        typestr[80];
  163     char        *cp;
  164     int         ch, num;
  165 
  166     if (argc < 3) {
  167             command_errmsg = "usage is [-n key#] <prov> <file>";
  168             return(CMD_ERROR);
  169     }
  170 
  171     num = 0;
  172     optind = 1;
  173     optreset = 1;
  174     while ((ch = getopt(argc, argv, "n:")) != -1) {
  175         switch(ch) {
  176         case 'n':
  177             num = strtol(optarg, &cp, 0);
  178             if (cp == optarg) {
  179                     sprintf(command_errbuf, "bad key index '%s'", optarg);
  180                     return(CMD_ERROR);
  181             }
  182             break;
  183         case '?':
  184         default:
  185             /* getopt has already reported an error */
  186             return(CMD_OK);
  187         }
  188     }
  189     argv += (optind - 1);
  190     argc -= (optind - 1);
  191     sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
  192     return(file_loadraw(typestr, argv[2]));
  193 }
  194 
  195 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
  196 
  197 static int
  198 command_unload(int argc, char *argv[])
  199 {
  200     struct preloaded_file       *fp;
  201     
  202     while (preloaded_files != NULL) {
  203         fp = preloaded_files;
  204         preloaded_files = preloaded_files->f_next;
  205         file_discard(fp);
  206     }
  207     loadaddr = 0;
  208     unsetenv("kernelname");
  209     return(CMD_OK);
  210 }
  211 
  212 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
  213 
  214 static int
  215 command_lsmod(int argc, char *argv[])
  216 {
  217     struct preloaded_file       *fp;
  218     struct kernel_module        *mp;
  219     struct file_metadata        *md;
  220     char                        lbuf[80];
  221     int                         ch, verbose;
  222 
  223     verbose = 0;
  224     optind = 1;
  225     optreset = 1;
  226     while ((ch = getopt(argc, argv, "v")) != -1) {
  227         switch(ch) {
  228         case 'v':
  229             verbose = 1;
  230             break;
  231         case '?':
  232         default:
  233             /* getopt has already reported an error */
  234             return(CMD_OK);
  235         }
  236     }
  237 
  238     pager_open();
  239     for (fp = preloaded_files; fp; fp = fp->f_next) {
  240         sprintf(lbuf, " %p: %s (%s, 0x%lx)\n", 
  241                 (void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
  242         pager_output(lbuf);
  243         if (fp->f_args != NULL) {
  244             pager_output("    args: ");
  245             pager_output(fp->f_args);
  246             pager_output("\n");
  247         }
  248         if (fp->f_modules) {
  249             pager_output("  modules: ");
  250             for (mp = fp->f_modules; mp; mp = mp->m_next) {
  251                 sprintf(lbuf, "%s.%d ", mp->m_name, mp->m_version);
  252                 pager_output(lbuf);
  253             }
  254             pager_output("\n");
  255         }
  256         if (verbose) {
  257             /* XXX could add some formatting smarts here to display some better */
  258             for (md = fp->f_metadata; md != NULL; md = md->md_next) {
  259                 sprintf(lbuf, "      0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
  260                 pager_output(lbuf);
  261             }
  262         }
  263     }
  264     pager_close();
  265     return(CMD_OK);
  266 }
  267 
  268 /*
  269  * File level interface, functions file_*
  270  */
  271 int
  272 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
  273 {
  274     struct preloaded_file *fp;
  275     int error;
  276     int i;
  277 
  278     error = EFTYPE;
  279     for (i = 0, fp = NULL; file_formats[i] && fp == NULL; i++) {
  280         error = (file_formats[i]->l_load)(filename, loadaddr, &fp);
  281         if (error == 0) {
  282             fp->f_loader = i;           /* remember the loader */
  283             *result = fp;
  284             break;
  285         }
  286         if (error == EFTYPE)
  287             continue;           /* Unknown to this handler? */
  288         if (error) {
  289             sprintf(command_errbuf, "can't load file '%s': %s",
  290                 filename, strerror(error));
  291             break;
  292         }
  293     }
  294     return (error);
  295 }
  296 
  297 static int
  298 file_load_dependencies(struct preloaded_file *base_file) {
  299     struct file_metadata *md;
  300     struct preloaded_file *fp;
  301     struct mod_depend *verinfo;
  302     struct kernel_module *mp;
  303     char *dmodname;
  304     int error;
  305 
  306     md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
  307     if (md == NULL)
  308         return (0);
  309     error = 0;
  310     do {
  311         verinfo = (struct mod_depend*)md->md_data;
  312         dmodname = (char *)(verinfo + 1);
  313         if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
  314             printf("loading required module '%s'\n", dmodname);
  315             error = mod_load(dmodname, verinfo, 0, NULL);
  316             if (error)
  317                 break;
  318             /*
  319              * If module loaded via kld name which isn't listed
  320              * in the linker.hints file, we should check if it have
  321              * required version.
  322              */
  323             mp = file_findmodule(NULL, dmodname, verinfo);
  324             if (mp == NULL) {
  325                 sprintf(command_errbuf, "module '%s' exists but with wrong version",
  326                     dmodname);
  327                 error = ENOENT;
  328                 break;
  329             }
  330         }
  331         md = metadata_next(md, MODINFOMD_DEPLIST);
  332     } while (md);
  333     if (!error)
  334         return (0);
  335     /* Load failed; discard everything */
  336     while (base_file != NULL) {
  337         fp = base_file;
  338         base_file = base_file->f_next;
  339         file_discard(fp);
  340     }
  341     return (error);
  342 }
  343 /*
  344  * We've been asked to load (name) as (type), so just suck it in,
  345  * no arguments or anything.
  346  */
  347 int
  348 file_loadraw(char *type, char *name)
  349 {
  350     struct preloaded_file       *fp;
  351     char                        *cp;
  352     int                         fd, got;
  353     vm_offset_t                 laddr;
  354 #ifdef PC98
  355     struct stat                 st;
  356 #endif
  357 
  358     /* We can't load first */
  359     if ((file_findfile(NULL, NULL)) == NULL) {
  360         command_errmsg = "can't load file before kernel";
  361         return(CMD_ERROR);
  362     }
  363 
  364     /* locate the file on the load path */
  365     cp = file_search(name, NULL);
  366     if (cp == NULL) {
  367         sprintf(command_errbuf, "can't find '%s'", name);
  368         return(CMD_ERROR);
  369     }
  370     name = cp;
  371     
  372     if ((fd = open(name, O_RDONLY)) < 0) {
  373         sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
  374         free(name);
  375         return(CMD_ERROR);
  376     }
  377 
  378 #ifdef PC98
  379     /* We cannot use 15M-16M area on pc98. */
  380     if (loadaddr < 0x1000000 &&
  381         fstat(fd, &st) == 0 &&
  382         (st.st_size == -1 || loadaddr + st.st_size > 0xf00000))
  383         loadaddr = 0x1000000;
  384 #endif
  385 
  386     laddr = loadaddr;
  387     for (;;) {
  388         /* read in 4k chunks; size is not really important */
  389         got = archsw.arch_readin(fd, laddr, 4096);
  390         if (got == 0)                           /* end of file */
  391             break;
  392         if (got < 0) {                          /* error */
  393             sprintf(command_errbuf, "error reading '%s': %s", name, strerror(errno));
  394             free(name);
  395             close(fd);
  396             return(CMD_ERROR);
  397         }
  398         laddr += got;
  399     }
  400     
  401     /* Looks OK so far; create & populate control structure */
  402     fp = file_alloc();
  403     fp->f_name = name;
  404     fp->f_type = strdup(type);
  405     fp->f_args = NULL;
  406     fp->f_metadata = NULL;
  407     fp->f_loader = -1;
  408     fp->f_addr = loadaddr;
  409     fp->f_size = laddr - loadaddr;
  410 
  411     /* recognise space consumption */
  412     loadaddr = laddr;
  413 
  414     /* Add to the list of loaded files */
  415     file_insert_tail(fp);
  416     close(fd);
  417     return(CMD_OK);
  418 }
  419 
  420 /*
  421  * Load the module (name), pass it (argc),(argv), add container file
  422  * to the list of loaded files.
  423  * If module is already loaded just assign new argc/argv.
  424  */
  425 int
  426 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
  427 {
  428     struct kernel_module        *mp;
  429     int                         err;
  430     char                        *filename;
  431 
  432     if (file_havepath(modname)) {
  433         printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
  434         return (mod_loadkld(modname, argc, argv));
  435     }
  436     /* see if module is already loaded */
  437     mp = file_findmodule(NULL, modname, verinfo);
  438     if (mp) {
  439 #ifdef moduleargs
  440         if (mp->m_args)
  441             free(mp->m_args);
  442         mp->m_args = unargv(argc, argv);
  443 #endif
  444         sprintf(command_errbuf, "warning: module '%s' already loaded", mp->m_name);
  445         return (0);
  446     }
  447     /* locate file with the module on the search path */
  448     filename = mod_searchmodule(modname, verinfo);
  449     if (filename == NULL) {
  450         sprintf(command_errbuf, "can't find '%s'", modname);
  451         return (ENOENT);
  452     }
  453     err = mod_loadkld(filename, argc, argv);
  454     return (err);
  455 }
  456 
  457 /*
  458  * Load specified KLD. If path is omitted, then try to locate it via
  459  * search path.
  460  */
  461 int
  462 mod_loadkld(const char *kldname, int argc, char *argv[])
  463 {
  464     struct preloaded_file       *fp, *last_file;
  465     int                         err;
  466     char                        *filename;
  467 
  468     /*
  469      * Get fully qualified KLD name
  470      */
  471     filename = file_search(kldname, kld_ext_list);
  472     if (filename == NULL) {
  473         sprintf(command_errbuf, "can't find '%s'", kldname);
  474         return (ENOENT);
  475     }
  476     /* 
  477      * Check if KLD already loaded
  478      */
  479     fp = file_findfile(filename, NULL);
  480     if (fp) {
  481         sprintf(command_errbuf, "warning: KLD '%s' already loaded", filename);
  482         free(filename);
  483         return (0);
  484     }
  485     for (last_file = preloaded_files; 
  486          last_file != NULL && last_file->f_next != NULL;
  487          last_file = last_file->f_next)
  488         ;
  489 
  490     do {
  491 #ifdef PC98
  492         /* We cannot use 15M-16M area on pc98. */
  493         struct stat st;
  494         if (loadaddr < 0x1000000 &&
  495             stat(filename, &st) == 0 &&
  496             (st.st_size == -1 || loadaddr + st.st_size > 0xf00000))
  497             loadaddr = 0x1000000;
  498 #endif
  499         err = file_load(filename, loadaddr, &fp);
  500         if (err)
  501             break;
  502         fp->f_args = unargv(argc, argv);
  503         loadaddr = fp->f_addr + fp->f_size;
  504         file_insert_tail(fp);           /* Add to the list of loaded files */
  505         if (file_load_dependencies(fp) != 0) {
  506             err = ENOENT;
  507             last_file->f_next = NULL;
  508             loadaddr = last_file->f_addr + last_file->f_size;
  509             fp = NULL;
  510             break;
  511         }
  512     } while(0);
  513     if (err == EFTYPE)
  514         sprintf(command_errbuf, "don't know how to load module '%s'", filename);
  515     if (err && fp)
  516         file_discard(fp);
  517     free(filename);
  518     return (err);
  519 }
  520 
  521 /*
  522  * Find a file matching (name) and (type).
  523  * NULL may be passed as a wildcard to either.
  524  */
  525 struct preloaded_file *
  526 file_findfile(char *name, char *type)
  527 {
  528     struct preloaded_file *fp;
  529 
  530     for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
  531         if (((name == NULL) || !strcmp(name, fp->f_name)) &&
  532             ((type == NULL) || !strcmp(type, fp->f_type)))
  533             break;
  534     }
  535     return (fp);
  536 }
  537 
  538 /*
  539  * Find a module matching (name) inside of given file.
  540  * NULL may be passed as a wildcard.
  541  */
  542 struct kernel_module *
  543 file_findmodule(struct preloaded_file *fp, char *modname,
  544         struct mod_depend *verinfo)
  545 {
  546     struct kernel_module *mp, *best;
  547     int bestver, mver;
  548 
  549     if (fp == NULL) {
  550         for (fp = preloaded_files; fp; fp = fp->f_next) {
  551             mp = file_findmodule(fp, modname, verinfo);
  552             if (mp)
  553                 return (mp);
  554         }
  555         return (NULL);
  556     }
  557     best = NULL;
  558     bestver = 0;
  559     for (mp = fp->f_modules; mp; mp = mp->m_next) {
  560         if (strcmp(modname, mp->m_name) == 0) {
  561             if (verinfo == NULL)
  562                 return (mp);
  563             mver = mp->m_version;
  564             if (mver == verinfo->md_ver_preferred)
  565                 return (mp);
  566             if (mver >= verinfo->md_ver_minimum && 
  567                 mver <= verinfo->md_ver_maximum &&
  568                 mver > bestver) {
  569                 best = mp;
  570                 bestver = mver;
  571             }
  572         }
  573     }
  574     return (best);
  575 }
  576 /*
  577  * Make a copy of (size) bytes of data from (p), and associate them as
  578  * metadata of (type) to the module (mp).
  579  */
  580 void
  581 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
  582 {
  583     struct file_metadata        *md;
  584 
  585     md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
  586     md->md_size = size;
  587     md->md_type = type;
  588     bcopy(p, md->md_data, size);
  589     md->md_next = fp->f_metadata;
  590     fp->f_metadata = md;
  591 }
  592 
  593 /*
  594  * Find a metadata object of (type) associated with the file (fp)
  595  */
  596 struct file_metadata *
  597 file_findmetadata(struct preloaded_file *fp, int type)
  598 {
  599     struct file_metadata *md;
  600 
  601     for (md = fp->f_metadata; md != NULL; md = md->md_next)
  602         if (md->md_type == type)
  603             break;
  604     return(md);
  605 }
  606 
  607 struct file_metadata *
  608 metadata_next(struct file_metadata *md, int type)
  609 {
  610     if (md == NULL)
  611         return (NULL);
  612     while((md = md->md_next) != NULL)
  613         if (md->md_type == type)
  614             break;
  615     return (md);
  616 }
  617 
  618 static char *emptyextlist[] = { "", NULL };
  619 
  620 /*
  621  * Check if the given file is in place and return full path to it.
  622  */
  623 static char *
  624 file_lookup(const char *path, const char *name, int namelen, char **extlist)
  625 {
  626     struct stat st;
  627     char        *result, *cp, **cpp;
  628     int         pathlen, extlen, len;
  629 
  630     pathlen = strlen(path);
  631     extlen = 0;
  632     if (extlist == NULL)
  633         extlist = emptyextlist;
  634     for (cpp = extlist; *cpp; cpp++) {
  635         len = strlen(*cpp);
  636         if (len > extlen)
  637             extlen = len;
  638     }
  639     result = malloc(pathlen + namelen + extlen + 2);
  640     if (result == NULL)
  641         return (NULL);
  642     bcopy(path, result, pathlen);
  643     if (pathlen > 0 && result[pathlen - 1] != '/')
  644         result[pathlen++] = '/';
  645     cp = result + pathlen;
  646     bcopy(name, cp, namelen);
  647     cp += namelen;
  648     for (cpp = extlist; *cpp; cpp++) {
  649         strcpy(cp, *cpp);
  650         if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
  651             return result;
  652     }
  653     free(result);
  654     return NULL;
  655 }
  656 
  657 /*
  658  * Check if file name have any qualifiers
  659  */
  660 static int
  661 file_havepath(const char *name)
  662 {
  663     const char          *cp;
  664 
  665     archsw.arch_getdev(NULL, name, &cp);
  666     return (cp != name || strchr(name, '/') != NULL);
  667 }
  668 
  669 /*
  670  * Attempt to find the file (name) on the module searchpath.
  671  * If (name) is qualified in any way, we simply check it and
  672  * return it or NULL.  If it is not qualified, then we attempt
  673  * to construct a path using entries in the environment variable
  674  * module_path.
  675  *
  676  * The path we return a pointer to need never be freed, as we manage
  677  * it internally.
  678  */
  679 static char *
  680 file_search(const char *name, char **extlist)
  681 {
  682     struct moduledir    *mdp;
  683     struct stat         sb;
  684     char                *result;
  685     int                 namelen;
  686 
  687     /* Don't look for nothing */
  688     if (name == NULL)
  689         return(NULL);
  690 
  691     if (*name == 0)
  692         return(strdup(name));
  693 
  694     if (file_havepath(name)) {
  695         /* Qualified, so just see if it exists */
  696         if (stat(name, &sb) == 0)
  697             return(strdup(name));
  698         return(NULL);
  699     }
  700     moduledir_rebuild();
  701     result = NULL;
  702     namelen = strlen(name);
  703     STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
  704         result = file_lookup(mdp->d_path, name, namelen, extlist);
  705         if (result)
  706             break;
  707     }
  708     return(result);
  709 }
  710 
  711 #define INT_ALIGN(base, ptr)    ptr = \
  712         (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
  713 
  714 static char *
  715 mod_search_hints(struct moduledir *mdp, const char *modname,
  716         struct mod_depend *verinfo)
  717 {
  718     u_char      *cp, *recptr, *bufend, *best;
  719     char        *result;
  720     int         *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
  721 
  722     moduledir_readhints(mdp);
  723     modnamelen = strlen(modname);
  724     found = 0;
  725     result = NULL;
  726     bestver = 0;
  727     if (mdp->d_hints == NULL)
  728         goto bad;
  729     recptr = mdp->d_hints;
  730     bufend = recptr + mdp->d_hintsz;
  731     clen = blen = 0;
  732     best = cp = NULL;
  733     while (recptr < bufend && !found) {
  734         intp = (int*)recptr;
  735         reclen = *intp++;
  736         ival = *intp++;
  737         cp = (char*)intp;
  738         switch (ival) {
  739         case MDT_VERSION:
  740             clen = *cp++;
  741             if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
  742                 break;
  743             cp += clen;
  744             INT_ALIGN(mdp->d_hints, cp);
  745             ival = *(int*)cp;
  746             cp += sizeof(int);
  747             clen = *cp++;
  748             if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
  749                 found = 1;
  750                 break;
  751             }
  752             if (ival >= verinfo->md_ver_minimum && 
  753                 ival <= verinfo->md_ver_maximum &&
  754                 ival > bestver) {
  755                 bestver = ival;
  756                 best = cp;
  757                 blen = clen;
  758             }
  759             break;
  760         default:
  761             break;
  762         }
  763         recptr += reclen + sizeof(int);
  764     }
  765     /*
  766      * Finally check if KLD is in the place
  767      */
  768     if (found)
  769         result = file_lookup(mdp->d_path, cp, clen, NULL);
  770     else if (best)
  771         result = file_lookup(mdp->d_path, best, blen, NULL);
  772 bad:
  773     /*
  774      * If nothing found or hints is absent - fallback to the old way
  775      * by using "kldname[.ko]" as module name.
  776      */
  777     if (!found && !bestver && result == NULL)
  778         result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
  779     return result;
  780 }
  781 
  782 /*
  783  * Attempt to locate the file containing the module (name)
  784  */
  785 static char *
  786 mod_searchmodule(char *name, struct mod_depend *verinfo)
  787 {
  788     struct      moduledir *mdp;
  789     char        *result;
  790 
  791     moduledir_rebuild();
  792     /*
  793      * Now we ready to lookup module in the given directories
  794      */
  795     result = NULL;
  796     STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
  797         result = mod_search_hints(mdp, name, verinfo);
  798         if (result)
  799             break;
  800     }
  801 
  802     return(result);
  803 }
  804 
  805 int
  806 file_addmodule(struct preloaded_file *fp, char *modname, int version,
  807         struct kernel_module **newmp)
  808 {
  809     struct kernel_module *mp;
  810     struct mod_depend mdepend;
  811 
  812     bzero(&mdepend, sizeof(mdepend));
  813     mdepend.md_ver_preferred = version;
  814     mp = file_findmodule(fp, modname, &mdepend);
  815     if (mp)
  816         return (EEXIST);
  817     mp = malloc(sizeof(struct kernel_module));
  818     if (mp == NULL)
  819         return (ENOMEM);
  820     bzero(mp, sizeof(struct kernel_module));
  821     mp->m_name = strdup(modname);
  822     mp->m_version = version;
  823     mp->m_fp = fp;
  824     mp->m_next = fp->f_modules;
  825     fp->f_modules = mp;
  826     if (newmp)
  827         *newmp = mp;
  828     return (0);
  829 }
  830 
  831 /*
  832  * Throw a file away
  833  */
  834 void
  835 file_discard(struct preloaded_file *fp)
  836 {
  837     struct file_metadata        *md, *md1;
  838     struct kernel_module        *mp, *mp1;
  839     if (fp == NULL)
  840         return;
  841     md = fp->f_metadata;
  842     while (md) {
  843         md1 = md;
  844         md = md->md_next;
  845         free(md1);
  846     }
  847     mp = fp->f_modules;
  848     while (mp) {
  849         if (mp->m_name)
  850             free(mp->m_name);
  851         mp1 = mp;
  852         mp = mp->m_next;
  853         free(mp1);
  854     }   
  855     if (fp->f_name != NULL)
  856         free(fp->f_name);
  857     if (fp->f_type != NULL)
  858         free(fp->f_type);
  859     if (fp->f_args != NULL)
  860         free(fp->f_args);
  861     free(fp);
  862 }
  863 
  864 /*
  865  * Allocate a new file; must be used instead of malloc()
  866  * to ensure safe initialisation.
  867  */
  868 struct preloaded_file *
  869 file_alloc(void)
  870 {
  871     struct preloaded_file       *fp;
  872     
  873     if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
  874         bzero(fp, sizeof(struct preloaded_file));
  875     }
  876     return (fp);
  877 }
  878 
  879 /*
  880  * Add a module to the chain
  881  */
  882 static void
  883 file_insert_tail(struct preloaded_file *fp)
  884 {
  885     struct preloaded_file       *cm;
  886     
  887     /* Append to list of loaded file */
  888     fp->f_next = NULL;
  889     if (preloaded_files == NULL) {
  890         preloaded_files = fp;
  891     } else {
  892         for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
  893             ;
  894         cm->f_next = fp;
  895     }
  896 }
  897 
  898 static char *
  899 moduledir_fullpath(struct moduledir *mdp, const char *fname)
  900 {
  901     char *cp;
  902 
  903     cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
  904     if (cp == NULL)
  905         return NULL;
  906     strcpy(cp, mdp->d_path);
  907     strcat(cp, "/");
  908     strcat(cp, fname);
  909     return (cp);
  910 }
  911 
  912 /*
  913  * Read linker.hints file into memory performing some sanity checks.
  914  */
  915 static void
  916 moduledir_readhints(struct moduledir *mdp)
  917 {
  918     struct stat st;
  919     char        *path;
  920     int         fd, size, version;
  921 
  922     if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
  923         return;
  924     path = moduledir_fullpath(mdp, "linker.hints");
  925     if (stat(path, &st) != 0 ||
  926         st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
  927         st.st_size > 100 * 1024 || (fd = open(path, O_RDONLY)) < 0) {
  928         free(path);
  929         mdp->d_flags |= MDIR_NOHINTS;
  930         return;
  931     }
  932     free(path);
  933     size = read(fd, &version, sizeof(version));
  934     if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
  935         goto bad;
  936     size = st.st_size - size;
  937     mdp->d_hints = malloc(size);
  938     if (mdp->d_hints == NULL)
  939         goto bad;
  940     if (read(fd, mdp->d_hints, size) != size)
  941         goto bad;
  942     mdp->d_hintsz = size;
  943     close(fd);
  944     return;
  945 bad:
  946     close(fd);
  947     if (mdp->d_hints) {
  948         free(mdp->d_hints);
  949         mdp->d_hints = NULL;
  950     }
  951     mdp->d_flags |= MDIR_NOHINTS;
  952     return;
  953 }
  954 
  955 /*
  956  * Extract directories from the ';' separated list, remove duplicates.
  957  */
  958 static void
  959 moduledir_rebuild(void)
  960 {
  961     struct      moduledir *mdp, *mtmp;
  962     const char  *path, *cp, *ep;
  963     int         cplen;
  964 
  965     path = getenv("module_path");
  966     if (path == NULL)
  967         path = default_searchpath;
  968     /*
  969      * Rebuild list of module directories if it changed
  970      */
  971     STAILQ_FOREACH(mdp, &moduledir_list, d_link)
  972         mdp->d_flags |= MDIR_REMOVED;
  973 
  974     for (ep = path; *ep != 0;  ep++) {
  975         cp = ep;
  976         for (; *ep != 0 && *ep != ';'; ep++)
  977             ;
  978         /*
  979          * Ignore trailing slashes
  980          */
  981         for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
  982             ;
  983         STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
  984             if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
  985                 continue;
  986             mdp->d_flags &= ~MDIR_REMOVED;
  987             break;
  988         }
  989         if (mdp == NULL) {
  990             mdp = malloc(sizeof(*mdp) + cplen + 1);
  991             if (mdp == NULL)
  992                 return;
  993             mdp->d_path = (char*)(mdp + 1);
  994             bcopy(cp, mdp->d_path, cplen);
  995             mdp->d_path[cplen] = 0;
  996             mdp->d_hints = NULL;
  997             mdp->d_flags = 0;
  998             STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
  999         }
 1000         if (*ep == 0)
 1001             break;
 1002     }
 1003     /*
 1004      * Delete unused directories if any
 1005      */
 1006     mdp = STAILQ_FIRST(&moduledir_list);
 1007     while (mdp) {
 1008         if ((mdp->d_flags & MDIR_REMOVED) == 0) {
 1009             mdp = STAILQ_NEXT(mdp, d_link);
 1010         } else {
 1011             if (mdp->d_hints)
 1012                 free(mdp->d_hints);
 1013             mtmp = mdp;
 1014             mdp = STAILQ_NEXT(mdp, d_link);
 1015             STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);
 1016             free(mtmp);
 1017         }
 1018     }
 1019     return;
 1020 }

Cache object: 7b47fbfb5523d493daf61adb6e6f3119


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.