11eaf0ac3Slogwang /*-
2*22ce4affSfengbojiang * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang *
41eaf0ac3Slogwang * Copyright (c) 2006 Dag-Erling Coïdan Smørgrav
51eaf0ac3Slogwang * All rights reserved.
61eaf0ac3Slogwang *
71eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
81eaf0ac3Slogwang * modification, are permitted provided that the following conditions
91eaf0ac3Slogwang * are met:
101eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
111eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer
121eaf0ac3Slogwang * in this position and unchanged.
131eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
141eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
151eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
161eaf0ac3Slogwang *
171eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271eaf0ac3Slogwang * SUCH DAMAGE.
281eaf0ac3Slogwang *
291eaf0ac3Slogwang * $FreeBSD$
301eaf0ac3Slogwang */
311eaf0ac3Slogwang
321eaf0ac3Slogwang #include <sys/param.h>
331eaf0ac3Slogwang #include <sys/linker.h>
341eaf0ac3Slogwang #include <sys/module.h>
351eaf0ac3Slogwang
361eaf0ac3Slogwang #include <errno.h>
371eaf0ac3Slogwang #include <libutil.h>
381eaf0ac3Slogwang #include <string.h>
391eaf0ac3Slogwang
401eaf0ac3Slogwang int
kld_isloaded(const char * name)411eaf0ac3Slogwang kld_isloaded(const char *name)
421eaf0ac3Slogwang {
431eaf0ac3Slogwang struct kld_file_stat fstat;
441eaf0ac3Slogwang struct module_stat mstat;
451eaf0ac3Slogwang const char *ko;
461eaf0ac3Slogwang int fid, mid;
471eaf0ac3Slogwang
481eaf0ac3Slogwang for (fid = kldnext(0); fid > 0; fid = kldnext(fid)) {
491eaf0ac3Slogwang fstat.version = sizeof(fstat);
501eaf0ac3Slogwang if (kldstat(fid, &fstat) != 0)
511eaf0ac3Slogwang continue;
521eaf0ac3Slogwang /* check if the file name matches the supplied name */
531eaf0ac3Slogwang if (strcmp(fstat.name, name) == 0)
541eaf0ac3Slogwang return (1);
551eaf0ac3Slogwang /* strip .ko and try again */
561eaf0ac3Slogwang if ((ko = strstr(fstat.name, ".ko")) != NULL &&
571eaf0ac3Slogwang strlen(name) == (size_t)(ko - fstat.name) &&
581eaf0ac3Slogwang strncmp(fstat.name, name, ko - fstat.name) == 0)
591eaf0ac3Slogwang return (1);
601eaf0ac3Slogwang /* look for a matching module within the file */
611eaf0ac3Slogwang for (mid = kldfirstmod(fid); mid > 0; mid = modfnext(mid)) {
621eaf0ac3Slogwang mstat.version = sizeof(mstat);
631eaf0ac3Slogwang if (modstat(mid, &mstat) != 0)
641eaf0ac3Slogwang continue;
651eaf0ac3Slogwang if (strcmp(mstat.name, name) == 0)
661eaf0ac3Slogwang return (1);
671eaf0ac3Slogwang }
681eaf0ac3Slogwang }
691eaf0ac3Slogwang return (0);
701eaf0ac3Slogwang }
711eaf0ac3Slogwang
721eaf0ac3Slogwang int
kld_load(const char * name)731eaf0ac3Slogwang kld_load(const char *name)
741eaf0ac3Slogwang {
751eaf0ac3Slogwang if (kldload(name) == -1 && errno != EEXIST)
761eaf0ac3Slogwang return (-1);
771eaf0ac3Slogwang return (0);
781eaf0ac3Slogwang }
79