kld.c revision 155804
1155804Sdes/*- 2155804Sdes * Copyright (c) 2006 Dag-Erling Co�dan Sm�rgrav 3155804Sdes * All rights reserved. 4155804Sdes * 5155804Sdes * Redistribution and use in source and binary forms, with or without 6155804Sdes * modification, are permitted provided that the following conditions 7155804Sdes * are met: 8155804Sdes * 1. Redistributions of source code must retain the above copyright 9155804Sdes * notice, this list of conditions and the following disclaimer 10155804Sdes * in this position and unchanged. 11155804Sdes * 2. Redistributions in binary form must reproduce the above copyright 12155804Sdes * notice, this list of conditions and the following disclaimer in the 13155804Sdes * documentation and/or other materials provided with the distribution. 14155804Sdes * 15155804Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16155804Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17155804Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18155804Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19155804Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20155804Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21155804Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22155804Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23155804Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24155804Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25155804Sdes * SUCH DAMAGE. 26155804Sdes * 27155804Sdes * $FreeBSD: head/lib/libutil/kld.c 155804 2006-02-18 11:25:28Z des $ 28155804Sdes */ 29155804Sdes 30155804Sdes#include <sys/param.h> 31155804Sdes#include <sys/linker.h> 32155804Sdes#include <sys/module.h> 33155804Sdes 34155804Sdes#include <errno.h> 35155804Sdes 36155804Sdesint 37155804Sdeskld_isloaded(const char *name) 38155804Sdes{ 39155804Sdes struct kld_file_stat fstat; 40155804Sdes struct module_stat mstat; 41155804Sdes const char *ko; 42155804Sdes int fid, mid; 43155804Sdes 44155804Sdes for (fid = kldnext(0); fid > 0; fid = kldnext(fid)) { 45155804Sdes fstat.version = sizeof(fstat); 46155804Sdes if (kldstat(fid, &fstat) != 0) 47155804Sdes continue; 48155804Sdes /* check if the file name matches the supplied name */ 49155804Sdes if (strcmp(fstat.name, name) == 0) 50155804Sdes return (1); 51155804Sdes /* strip .ko and try again */ 52155804Sdes if ((ko = strstr(fstat.name, ".ko")) != NULL && 53155804Sdes strlen(name) == ko - fstat.name && 54155804Sdes strncmp(fstat.name, name, ko - fstat.name) == 0) 55155804Sdes return (1); 56155804Sdes /* look for a matching module within the file */ 57155804Sdes for (mid = kldfirstmod(fid); mid > 0; mid = modfnext(mid)) { 58155804Sdes mstat.version = sizeof(mstat); 59155804Sdes if (modstat(mid, &mstat) != 0) 60155804Sdes continue; 61155804Sdes if (strcmp(mstat.name, name) == 0) 62155804Sdes return (1); 63155804Sdes } 64155804Sdes } 65155804Sdes return (0); 66155804Sdes} 67155804Sdes 68155804Sdesint 69155804Sdeskld_load(const char *name) 70155804Sdes{ 71155804Sdes if (kldload(name) == -1 && errno != EEXIST) 72155804Sdes return (-1); 73155804Sdes return (0); 74155804Sdes} 75