[Midnightbsd-cvs] src: lib/libutil: Add kld_load and kld_isloaded which are wrappers

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sat May 17 02:39:49 EDT 2008


Log Message:
-----------
Add kld_load and kld_isloaded which are wrappers around some of the kld kernel functions.

See kld(3)

Modified Files:
--------------
    src/lib/libutil:
        Makefile (r1.2 -> r1.3)
        libutil.h (r1.2 -> r1.3)

Added Files:
-----------
    src/lib/libutil:
        kld.3 (r1.1)
        kld.c (r1.1)

-------------- next part --------------
--- /dev/null
+++ lib/libutil/kld.c
@@ -0,0 +1,77 @@
+/*-
+ * Copyright (c) 2006 Dag-Erling Coïdan Smørgrav
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $MidnightBSD: src/lib/libutil/kld.c,v 1.1 2008/05/17 06:39:48 laffer1 Exp $
+ * $FreeBSD: src/lib/libutil/kld.c,v 1.2 2006/05/25 04:01:04 delphij Exp $
+ */
+
+#include <sys/param.h>
+#include <sys/linker.h>
+#include <sys/module.h>
+
+#include <errno.h>
+#include <libutil.h>
+#include <string.h>
+
+int
+kld_isloaded(const char *name)
+{
+	struct kld_file_stat fstat;
+	struct module_stat mstat;
+	const char *ko;
+	int fid, mid;
+
+	for (fid = kldnext(0); fid > 0; fid = kldnext(fid)) {
+		fstat.version = sizeof(fstat);
+		if (kldstat(fid, &fstat) != 0)
+			continue;
+		/* check if the file name matches the supplied name */
+		if (strcmp(fstat.name, name) == 0)
+			return (1);
+		/* strip .ko and try again */
+		if ((ko = strstr(fstat.name, ".ko")) != NULL &&
+		    strlen(name) == (size_t)(ko - fstat.name) &&
+		    strncmp(fstat.name, name, ko - fstat.name) == 0)
+			return (1);
+		/* look for a matching module within the file */
+		for (mid = kldfirstmod(fid); mid > 0; mid = modfnext(mid)) {
+			mstat.version = sizeof(mstat);
+			if (modstat(mid, &mstat) != 0)
+				continue;
+			if (strcmp(mstat.name, name) == 0)
+				return (1);
+		}
+	}
+	return (0);
+}
+
+int
+kld_load(const char *name)
+{
+	if (kldload(name) == -1 && errno != EEXIST)
+		return (-1);
+	return (0);
+}
Index: libutil.h
===================================================================
RCS file: /home/cvs/src/lib/libutil/libutil.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -L lib/libutil/libutil.h -L lib/libutil/libutil.h -u -r1.2 -r1.3
--- lib/libutil/libutil.h
+++ lib/libutil/libutil.h
@@ -102,6 +102,10 @@
 struct sockaddr;
 int	realhostname_sa(char *host, size_t hsize, struct sockaddr *addr,
 			     int addrlen);
+
+int	kld_isloaded(const char *name);
+int	kld_load(const char *name);
+
 void		(*esetfunc(void (*)(int, const char *, ...)))
     (int, const char *, ...);
 size_t 		estrlcpy(char *, const char *, size_t);
--- /dev/null
+++ lib/libutil/kld.3
@@ -0,0 +1,94 @@
+.\"-
+.\" Copyright (c) 2006 Dag-Erling Coïdan Smørgrav
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD: src/lib/libutil/kld.3,v 1.4 2007/09/28 15:31:44 obrien Exp $
+.\" $MidnightBSD: src/lib/libutil/kld.3,v 1.1 2008/05/17 06:39:48 laffer1 Exp $
+.\"
+.Dd May 16, 2008
+.Os
+.Dt KLD 3
+.Sh NAME
+.Nm kld_isloaded ,
+.Nm kld_load
+.Nd kld utility functions
+.Sh LIBRARY
+.Lb libutil
+.Sh SYNOPSIS
+.In libutil.h
+.Ft int
+.Fn kld_isloaded "const char *name"
+.Ft int
+.Fn kld_load "const char *name"
+.Sh DESCRIPTION
+These functions facilitate loading kernel modules from userland
+applications.
+.Pp
+The
+.Fn kld_isloaded
+function takes a name and returns a non-zero value if a module of that
+name is currently loaded.
+The name can be either the name of a module file
+.Pq Pa cpufreq.ko ,
+the same name without the
+.Pa .ko
+extension
+.Pq Pa cpufreq ,
+or the name of a module contained within that file
+.Pq Pa cpu/ichss .
+Only the latter will return correct results if the module is compiled
+into the kernel.
+.Pp
+The
+.Fn kld_load
+function is a simple wrapper around the
+.Xr kldload 2
+function.
+It returns zero if and only if the corresponding
+.Fn kldload
+call succeeded or returned
+.Er EEXIST
+(signifying that the requested module was already loaded).
+.Sh SEE ALSO
+.Xr kldfirstmod 2 ,
+.Xr kldload 2 ,
+.Xr kldnext 2 ,
+.Xr kldstat 2 ,
+.Xr modfnext 2 ,
+.Xr modstat 2 ,
+.Xr kld 4
+.Sh HISTORY
+The
+.Fn kld_isloaded
+and
+.Fn kld_load
+functions first appeared in
+.Fx 6.3 .
+.Sh AUTHORS
+The
+.Fn kld_isloaded
+and
+.Fn kld_load
+functions and this manual page were written by
+.An Dag-Erling Sm\(/orgrav Aq des at FreeBSD.org .
Index: Makefile
===================================================================
RCS file: /home/cvs/src/lib/libutil/Makefile,v
retrieving revision 1.2
retrieving revision 1.3
diff -L lib/libutil/Makefile -L lib/libutil/Makefile -u -r1.2 -r1.3
--- lib/libutil/Makefile
+++ lib/libutil/Makefile
@@ -1,19 +1,20 @@
 #	@(#)Makefile	8.1 (Berkeley) 6/4/93
 #	$FreeBSD: src/lib/libutil/Makefile,v 1.56.8.2 2006/01/15 17:50:35 delphij Exp $
+# 	$MidnightBSD$
 
 LIB=	util
 SHLIB_MAJOR= 6
 SHLIBDIR?= /lib
 CFLAGS+=-DLIBC_SCCS -I${.CURDIR} -I${.CURDIR}/../libc/gen/
 CFLAGS+=-DINET6
-SRCS=	_secure_path.c auth.c efun.c fparseln.c humanize_number.c login.c \
+SRCS=	_secure_path.c auth.c efun.c fparseln.c humanize_number.c kld.c login.c \
 	login_auth.c login_cap.c login_class.c login_crypt.c login_ok.c \
 	login_times.c login_tty.c logout.c logwtmp.c \
 	pidfile.c property.c pty.c pw_util.c realhostname.c stub.c \
 	trimdomain.c uucplock.c
 INCS=	libutil.h login_cap.h
 
-MAN+=	login.3 login_auth.3 login_tty.3 logout.3 logwtmp.3 pty.3 \
+MAN+=	kld.3 login.3 login_auth.3 login_tty.3 logout.3 logwtmp.3 pty.3 \
 	login_cap.3 login_class.3 login_times.3 login_ok.3 \
 	_secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 \
 	realhostname_sa.3 trimdomain.3 fparseln.3 humanize_number.3 \
@@ -23,6 +24,7 @@
 MLINKS+= property.3 property_find.3
 MLINKS+= auth.3 auth_getval.3
 MLINKS+= pty.3 openpty.3  pty.3 forkpty.3
+MLINKS+= kld.3 kld_isloaded.3 kld.3 kld_load.3
 MLINKS+=login_cap.3 login_getclassbyname.3 login_cap.3 login_close.3 \
 	login_cap.3 login_getclass.3 login_cap.3 login_getuserclass.3 \
 	login_cap.3 login_getcapstr.3 login_cap.3 login_getcaplist.3 \


More information about the Midnightbsd-cvs mailing list