kern_linker.c revision 40625
1/*-
2 * Copyright (c) 1997 Doug Rabson
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 *	$Id: kern_linker.c,v 1.10 1998/10/15 17:12:12 peter Exp $
27 */
28
29#include "opt_ddb.h"
30
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/systm.h>
34#include <sys/malloc.h>
35#include <sys/sysproto.h>
36#include <sys/sysent.h>
37#include <sys/proc.h>
38#include <sys/lock.h>
39#include <machine/cpu.h>
40#include <machine/bootinfo.h>
41#include <sys/module.h>
42#include <sys/linker.h>
43#include <sys/unistd.h>
44#include <sys/fcntl.h>
45#include <sys/libkern.h>
46#include <sys/namei.h>
47#include <sys/vnode.h>
48#include <sys/sysctl.h>
49
50MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
51linker_file_t linker_current_file;
52
53static struct lock lock;	/* lock for the file list */
54static linker_class_list_t classes;
55static linker_file_list_t files;
56static int next_file_id = 1;
57
58static void
59linker_init(void* arg)
60{
61    lockinit(&lock, PVM, "klink", 0, 0);
62    TAILQ_INIT(&classes);
63    TAILQ_INIT(&files);
64}
65
66SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
67
68int
69linker_add_class(const char* desc, void* priv,
70		 struct linker_class_ops* ops)
71{
72    linker_class_t lc;
73
74    lc = malloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT);
75    if (!lc)
76	return ENOMEM;
77    bzero(lc, sizeof(*lc));
78
79    lc->desc = desc;
80    lc->priv = priv;
81    lc->ops = ops;
82    TAILQ_INSERT_HEAD(&classes, lc, link);
83
84    return 0;
85}
86
87static void
88linker_file_sysinit(linker_file_t lf)
89{
90    struct linker_set* sysinits;
91    struct sysinit** sipp;
92    struct sysinit** xipp;
93    struct sysinit* save;
94    moduledata_t *moddata;
95
96    KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
97		   lf->filename));
98
99    sysinits = (struct linker_set*)
100	linker_file_lookup_symbol(lf, "sysinit_set", 0);
101
102    KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits));
103    if (!sysinits)
104	return;
105
106    /* HACK ALERT! */
107    for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
108	if ((*sipp)->func == module_register_init) {
109	    moddata = (*sipp)->udata;
110	    moddata->_file = lf;
111	}
112    }
113
114    /*
115     * Perform a bubble sort of the system initialization objects by
116     * their subsystem (primary key) and order (secondary key).
117     *
118     * Since some things care about execution order, this is the
119     * operation which ensures continued function.
120     */
121    for( sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
122	for( xipp = sipp + 1; *xipp; xipp++) {
123	    if( (*sipp)->subsystem < (*xipp)->subsystem ||
124		( (*sipp)->subsystem == (*xipp)->subsystem &&
125		  (*sipp)->order < (*xipp)->order))
126		continue;	/* skip*/
127	    save = *sipp;
128	    *sipp = *xipp;
129	    *xipp = save;
130	}
131    }
132
133
134    /*
135     * Traverse the (now) ordered list of system initialization tasks.
136     * Perform each task, and continue on to the next task.
137     *
138     * The last item on the list is expected to be the scheduler,
139     * which will not return.
140     */
141    for( sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
142	if( (*sipp)->subsystem == SI_SUB_DUMMY)
143	    continue;	/* skip dummy task(s)*/
144
145	switch( (*sipp)->type) {
146	case SI_TYPE_DEFAULT:
147	    /* no special processing*/
148	    (*((*sipp)->func))( (*sipp)->udata);
149	    break;
150
151	case SI_TYPE_KTHREAD:
152#if !defined(SMP)
153	    /* kernel thread*/
154	    if (fork1(&proc0, RFFDG|RFPROC|RFMEM))
155		panic("fork kernel thread");
156	    cpu_set_fork_handler(pfind(proc0.p_retval[0]),
157		(*sipp)->func, (*sipp)->udata);
158	    break;
159#endif
160
161	case SI_TYPE_KPROCESS:
162	    /* kernel thread*/
163	    if (fork1(&proc0, RFFDG|RFPROC))
164		panic("fork kernel process");
165	    cpu_set_fork_handler(pfind(proc0.p_retval[0]),
166		(*sipp)->func, (*sipp)->udata);
167	    break;
168
169	default:
170	    panic( "linker_file_sysinit: unrecognized init type");
171	}
172    }
173}
174
175int
176linker_load_file(const char* filename, linker_file_t* result)
177{
178    linker_class_t lc;
179    linker_file_t lf;
180    int error = 0;
181
182    lf = linker_find_file_by_name(filename);
183    if (lf) {
184	KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
185	*result = lf;
186	lf->refs++;
187	goto out;
188    }
189
190    lf = NULL;
191    for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
192	KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
193		       filename, lc->desc));
194	if (error = lc->ops->load_file(filename, &lf))
195	    goto out;
196	if (lf) {
197	    linker_file_sysinit(lf);
198
199	    *result = lf;
200	    goto out;
201	}
202    }
203    error = ENOEXEC;		/* format not recognised */
204
205out:
206    return error;
207}
208
209linker_file_t
210linker_find_file_by_name(const char* filename)
211{
212    linker_file_t lf = 0;
213
214    lockmgr(&lock, LK_SHARED, 0, curproc);
215    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link))
216	if (!strcmp(lf->filename, filename))
217	    break;
218    lockmgr(&lock, LK_RELEASE, 0, curproc);
219
220    return lf;
221}
222
223linker_file_t
224linker_find_file_by_id(int fileid)
225{
226    linker_file_t lf = 0;
227
228    lockmgr(&lock, LK_SHARED, 0, curproc);
229    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link))
230	if (lf->id == fileid)
231	    break;
232    lockmgr(&lock, LK_RELEASE, 0, curproc);
233
234    return lf;
235}
236
237linker_file_t
238linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
239{
240    linker_file_t lf = 0;
241    int namelen;
242    const char *filename;
243
244    filename = rindex(pathname, '/');
245    if (filename && filename[1])
246	filename++;
247    else
248	filename = pathname;
249
250    KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
251    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
252    namelen = strlen(filename) + 1;
253    lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
254    if (!lf)
255	goto out;
256    bzero(lf, sizeof(*lf));
257
258    lf->refs = 1;
259    lf->userrefs = 0;
260    lf->filename = (char*) (lf + 1);
261    strcpy(lf->filename, filename);
262    lf->id = next_file_id++;
263    lf->ndeps = 0;
264    lf->deps = NULL;
265    STAILQ_INIT(&lf->common);
266    TAILQ_INIT(&lf->modules);
267
268    lf->priv = priv;
269    lf->ops = ops;
270    TAILQ_INSERT_TAIL(&files, lf, link);
271
272out:
273    lockmgr(&lock, LK_RELEASE, 0, curproc);
274    return lf;
275}
276
277int
278linker_file_unload(linker_file_t file)
279{
280    module_t mod, next;
281    struct common_symbol* cp;
282    int error = 0;
283    int i;
284
285    KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
286    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
287    if (file->refs == 1) {
288	KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
289	/*
290	 * Inform any modules associated with this file.
291	 */
292	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
293	    next = module_getfnext(mod);
294
295	    /*
296	     * Give the module a chance to veto the unload.
297	     */
298	    if (error = module_unload(mod)) {
299		KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
300			       mod));
301		lockmgr(&lock, LK_RELEASE, 0, curproc);
302		goto out;
303	    }
304
305	    module_release(mod);
306	}
307    }
308
309    file->refs--;
310    if (file->refs > 0) {
311	lockmgr(&lock, LK_RELEASE, 0, curproc);
312	goto out;
313    }
314
315    TAILQ_REMOVE(&files, file, link);
316    lockmgr(&lock, LK_RELEASE, 0, curproc);
317
318    for (i = 0; i < file->ndeps; i++)
319	linker_file_unload(file->deps[i]);
320    free(file->deps, M_LINKER);
321
322    for (cp = STAILQ_FIRST(&file->common); cp;
323	 cp = STAILQ_FIRST(&file->common)) {
324	STAILQ_REMOVE(&file->common, cp, common_symbol, link);
325	free(cp, M_LINKER);
326    }
327
328    file->ops->unload(file);
329    free(file, M_LINKER);
330
331out:
332    return error;
333}
334
335int
336linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
337{
338    linker_file_t* newdeps;
339
340    newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
341		     M_LINKER, M_WAITOK);
342    if (newdeps == NULL)
343	return ENOMEM;
344    bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*));
345
346    if (file->deps) {
347	bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
348	free(file->deps, M_LINKER);
349    }
350    file->deps = newdeps;
351    file->deps[file->ndeps] = dep;
352    file->ndeps++;
353
354    return 0;
355}
356
357caddr_t
358linker_file_lookup_symbol(linker_file_t file, const char* name, int deps)
359{
360    linker_sym_t sym;
361    linker_symval_t symval;
362    caddr_t address;
363    size_t common_size = 0;
364    int i;
365
366    KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
367		  file, name, deps));
368
369    if (file->ops->lookup_symbol(file, name, &sym) == 0) {
370	file->ops->symbol_values(file, sym, &symval);
371	if (symval.value == 0)
372	    /*
373	     * For commons, first look them up in the dependancies and
374	     * only allocate space if not found there.
375	     */
376	    common_size = symval.size;
377	else {
378	    KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
379	    return symval.value;
380	}
381    }
382
383    if (deps)
384	for (i = 0; i < file->ndeps; i++) {
385	    address = linker_file_lookup_symbol(file->deps[i], name, 0);
386	    if (address) {
387		KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address));
388		return address;
389	    }
390	}
391
392    if (common_size > 0) {
393	/*
394	 * This is a common symbol which was not found in the
395	 * dependancies.  We maintain a simple common symbol table in
396	 * the file object.
397	 */
398	struct common_symbol* cp;
399
400	for (cp = STAILQ_FIRST(&file->common); cp;
401	     cp = STAILQ_NEXT(cp, link))
402	    if (!strcmp(cp->name, name)) {
403		KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
404		return cp->address;
405	    }
406
407	/*
408	 * Round the symbol size up to align.
409	 */
410	common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
411	cp = malloc(sizeof(struct common_symbol)
412		    + common_size
413		    + strlen(name) + 1,
414		    M_LINKER, M_WAITOK);
415	if (!cp) {
416	    KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
417	    return 0;
418	}
419	bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1);
420
421	cp->address = (caddr_t) (cp + 1);
422	cp->name = cp->address + common_size;
423	strcpy(cp->name, name);
424	bzero(cp->address, common_size);
425	STAILQ_INSERT_TAIL(&file->common, cp, link);
426
427	KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
428	return cp->address;
429    }
430
431    KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
432    return 0;
433}
434
435#ifdef DDB
436/*
437 * DDB Helpers.  DDB has to look across multiple files with their own
438 * symbol tables and string tables.
439 *
440 * Note that we do not obey list locking protocols here.  We really don't
441 * need DDB to hang because somebody's got the lock held.  We'll take the
442 * chance that the files list is inconsistant instead.
443 */
444
445int
446linker_ddb_lookup(char *symstr, linker_sym_t *sym)
447{
448    linker_file_t lf;
449
450    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
451	if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
452	    return 0;
453    }
454    return ENOENT;
455}
456
457int
458linker_ddb_search_symbol(caddr_t value, linker_sym_t *sym, long *diffp)
459{
460    linker_file_t lf;
461    u_long off = (u_long)value;
462    u_long diff, bestdiff;
463    linker_sym_t best;
464    linker_sym_t es;
465
466    best = 0;
467    bestdiff = off;
468    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
469	if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
470	    continue;
471	if (es != 0 && diff < bestdiff) {
472	    best = es;
473	    bestdiff = diff;
474	}
475	if (bestdiff == 0)
476	    break;
477    }
478    if (best) {
479	*sym = best;
480	*diffp = bestdiff;
481	return 0;
482    } else {
483	*sym = 0;
484	*diffp = off;
485	return ENOENT;
486    }
487}
488
489int
490linker_ddb_symbol_values(linker_sym_t sym, linker_symval_t *symval)
491{
492    linker_file_t lf;
493
494    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
495	if (lf->ops->symbol_values(lf, sym, symval) == 0)
496	    return 0;
497    }
498    return ENOENT;
499}
500
501#endif
502
503/*
504 * Syscalls.
505 */
506
507int
508kldload(struct proc* p, struct kldload_args* uap)
509{
510    char* filename = NULL;
511    linker_file_t lf;
512    int error = 0;
513
514    p->p_retval[0] = -1;
515
516    if (securelevel > 0)
517	return EPERM;
518
519    if (error = suser(p->p_ucred, &p->p_acflag))
520	return error;
521
522    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
523    if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL))
524	goto out;
525
526    if (error = linker_load_file(uap->file, &lf))
527	goto out;
528
529    lf->userrefs++;
530    p->p_retval[0] = lf->id;
531
532out:
533    if (filename)
534	free(filename, M_TEMP);
535    return error;
536}
537
538int
539kldunload(struct proc* p, struct kldunload_args* uap)
540{
541    linker_file_t lf;
542    int error = 0;
543
544    if (securelevel > 0)
545	return EPERM;
546
547    if (error = suser(p->p_ucred, &p->p_acflag))
548	return error;
549
550    lf = linker_find_file_by_id(SCARG(uap, fileid));
551    if (lf) {
552	KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
553	if (lf->userrefs == 0) {
554	    printf("linkerunload: attempt to unload file which was not loaded by user\n");
555	    error = EBUSY;
556	    goto out;
557	}
558	lf->userrefs--;
559	error = linker_file_unload(lf);
560    } else
561	error = ENOENT;
562
563out:
564    return error;
565}
566
567int
568kldfind(struct proc* p, struct kldfind_args* uap)
569{
570    char* filename = NULL;
571    linker_file_t lf;
572    int error = 0;
573
574    p->p_retval[0] = -1;
575
576    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
577    if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL))
578	goto out;
579
580    lf = linker_find_file_by_name(filename);
581    if (lf)
582	p->p_retval[0] = lf->id;
583    else
584	error = ENOENT;
585
586out:
587    if (filename)
588	free(filename, M_TEMP);
589    return error;
590}
591
592int
593kldnext(struct proc* p, struct kldnext_args* uap)
594{
595    linker_file_t lf;
596    int error = 0;
597
598    if (SCARG(uap, fileid) == 0) {
599	if (TAILQ_FIRST(&files))
600	    p->p_retval[0] = TAILQ_FIRST(&files)->id;
601	else
602	    p->p_retval[0] = 0;
603	return 0;
604    }
605
606    lf = linker_find_file_by_id(SCARG(uap, fileid));
607    if (lf) {
608	if (TAILQ_NEXT(lf, link))
609	    p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
610	else
611	    p->p_retval[0] = 0;
612    } else
613	error = ENOENT;
614
615    return error;
616}
617
618int
619kldstat(struct proc* p, struct kldstat_args* uap)
620{
621    linker_file_t lf;
622    int error = 0;
623    int version;
624    struct kld_file_stat* stat;
625    int namelen;
626
627    lf = linker_find_file_by_id(SCARG(uap, fileid));
628    if (!lf) {
629	error = ENOENT;
630	goto out;
631    }
632
633    stat = SCARG(uap, stat);
634
635    /*
636     * Check the version of the user's structure.
637     */
638    if (error = copyin(&stat->version, &version, sizeof(version)))
639	goto out;
640    if (version != sizeof(struct kld_file_stat)) {
641	error = EINVAL;
642	goto out;
643    }
644
645    namelen = strlen(lf->filename) + 1;
646    if (namelen > MAXPATHLEN)
647	namelen = MAXPATHLEN;
648    if (error = copyout(lf->filename, &stat->name[0], namelen))
649	goto out;
650    if (error = copyout(&lf->refs, &stat->refs, sizeof(int)))
651	goto out;
652    if (error = copyout(&lf->id, &stat->id, sizeof(int)))
653	goto out;
654    if (error = copyout(&lf->address, &stat->address, sizeof(caddr_t)))
655	goto out;
656    if (error = copyout(&lf->size, &stat->size, sizeof(size_t)))
657	goto out;
658
659    p->p_retval[0] = 0;
660
661out:
662    return error;
663}
664
665int
666kldfirstmod(struct proc* p, struct kldfirstmod_args* uap)
667{
668    linker_file_t lf;
669    int error = 0;
670
671    lf = linker_find_file_by_id(SCARG(uap, fileid));
672    if (lf) {
673	if (TAILQ_FIRST(&lf->modules))
674	    p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
675	else
676	    p->p_retval[0] = 0;
677    } else
678	error = ENOENT;
679
680    return error;
681}
682
683/*
684 * Preloaded module support
685 */
686
687static void
688linker_preload(void* arg)
689{
690    caddr_t		modptr;
691    char		*modname;
692    char		*modtype;
693    linker_file_t	lf;
694    linker_class_t	lc;
695    int			error;
696    struct linker_set	*sysinits;
697    struct sysinit	**sipp;
698    moduledata_t	*moddata;
699
700    modptr = NULL;
701    while ((modptr = preload_search_next_name(modptr)) != NULL) {
702	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
703	modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
704	if (modname == NULL) {
705	    printf("Preloaded module at %p does not have a name!\n", modptr);
706	    continue;
707	}
708	if (modtype == NULL) {
709	    printf("Preloaded module at %p does not have a type!\n", modptr);
710	    continue;
711	}
712	printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
713	lf = linker_find_file_by_name(modname);
714	if (lf) {
715	    lf->userrefs++;
716	    continue;
717	}
718	lf = NULL;
719	for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
720	    error = lc->ops->load_file(modname, &lf);
721	    if (error) {
722		lf = NULL;
723		break;
724	    }
725	}
726	if (lf) {
727	    lf->userrefs++;
728
729	    sysinits = (struct linker_set*)
730		linker_file_lookup_symbol(lf, "sysinit_set", 0);
731	    if (sysinits) {
732		/* HACK ALERT!
733		 * This is to set the sysinit moduledata so that the module
734		 * can attach itself to the correct containing file.
735		 * The sysinit could be run at *any* time.
736		 */
737		for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
738		    if ((*sipp)->func == module_register_init) {
739			moddata = (*sipp)->udata;
740			moddata->_file = lf;
741		    }
742		}
743		sysinit_add((struct sysinit **)sysinits->ls_items);
744	    }
745	}
746    }
747}
748
749SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
750
751/*
752 * Search for a not-loaded module by name.
753 *
754 * Modules may be found in the following locations:
755 *
756 * - preloaded (result is just the module name)
757 * - on disk (result is full path to module)
758 *
759 * If the module name is qualified in any way (contains path, etc.)
760 * the we simply return a copy of it.
761 *
762 * The search path can be manipulated via sysctl.  Note that we use the ';'
763 * character as a separator to be consistent with the bootloader.
764 */
765
766static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/";
767
768SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
769	      sizeof(linker_path), "module load search path");
770
771static char *
772linker_strdup(const char *str)
773{
774    char	*result;
775
776    if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
777	strcpy(result, str);
778    return(result);
779}
780
781char *
782linker_search_path(const char *name)
783{
784    struct nameidata	nd;
785    struct proc		*p = curproc;	/* XXX */
786    char		*cp, *ep, *result;
787    int			error;
788    enum vtype		type;
789
790    /* qualified at all? */
791    if (index(name, '/'))
792	return(linker_strdup(name));
793
794    /* traverse the linker path */
795    cp = linker_path;
796    for (;;) {
797
798	/* find the end of this component */
799	for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
800	    ;
801	result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
802	if (result == NULL)	/* actually ENOMEM */
803	    return(NULL);
804
805	strncpy(result, cp, ep - cp);
806	strcpy(result + (ep - cp), name);
807
808	/*
809	 * Attempt to open the file, and return the path if we succeed and it's
810	 * a regular file.
811	 */
812	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p);
813	error = vn_open(&nd, FREAD, 0);
814	if (error == 0) {
815	    type = nd.ni_vp->v_type;
816	    VOP_UNLOCK(nd.ni_vp, 0, p);
817	    vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
818	    if (type == VREG)
819		return(result);
820	}
821	free(result, M_LINKER);
822
823	if (*ep == 0)
824	    break;
825	cp = ep + 1;
826    }
827    return(NULL);
828}
829