diff options
Diffstat (limited to 'share/examples/kld')
28 files changed, 1614 insertions, 0 deletions
diff --git a/share/examples/kld/Makefile b/share/examples/kld/Makefile new file mode 100644 index 000000000000..3116f547e057 --- /dev/null +++ b/share/examples/kld/Makefile @@ -0,0 +1,73 @@ +# 08 Nov 1998 +# +# Makefile for sample programs for kld modules package +# +# 08 Nov 1998 Rajesh Vaidheeswarran - adapted from lkm Makefile +# +# Copyright (c) 1998 Rajesh Vaidheeswarran +# 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. +# 3. All advertising materials mentioning features or use of this software +# must display the following acknowledgement: +# This product includes software developed by Rajesh Vaidheeswarran. +# 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote +# products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``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 RAJESH VAIDHEESWARRAN 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. +# +# Copyright (c) 1993 Terrence R. Lambert. +# 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. +# 3. All advertising materials mentioning features or use of this software +# must display the following acknowledgement: +# This product includes software developed by Terrence R. Lambert. +# 4. The name Terrence R. Lambert may not be used to endorse or promote +# products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``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 TERRENCE R. LAMBERT 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. +# +# + +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld +SUBDIR= cdev dyn_sysctl firmware khelp random_adaptor syscall + +.include <bsd.subdir.mk> diff --git a/share/examples/kld/cdev/Makefile b/share/examples/kld/cdev/Makefile new file mode 100644 index 000000000000..8c7662243b3c --- /dev/null +++ b/share/examples/kld/cdev/Makefile @@ -0,0 +1,7 @@ +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/cdev +SUBDIR= module test + +load unload: _SUBDIR + +.include <bsd.subdir.mk> diff --git a/share/examples/kld/cdev/README b/share/examples/kld/cdev/README new file mode 100644 index 000000000000..617a3460f86d --- /dev/null +++ b/share/examples/kld/cdev/README @@ -0,0 +1,130 @@ +# Copyright (c) 1998 Rajesh Vaidheeswarran +# 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. +# 3. All advertising materials mentioning features or use of this software +# must display the following acknowledgement: +# This product includes software developed by Rajesh Vaidheeswarran +# 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote +# products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``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 RAJESH VAIDHEESWARRAN 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. +# +# Copyright (c) 1993 Terrence R. Lambert. +# 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. +# 3. All advertising materials mentioning features or use of this software +# must display the following acknowledgement: +# This product includes software developed by Terrence R. Lambert. +# 4. The name Terrence R. Lambert may not be used to endorse or promote +# products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``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 TERRENCE R. LAMBERT 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. +# +# + +1.0 Overview + + This is the README file for the sample kld module + that mimics a character device driver. + + A kld module may be used to load any data or + program into the kernel that can be made available by + modifying a table, pointer, or other kernel data to inform + the kernel that the module should be used instead of the + previous code/data path. + + Generally, it is assumed that a loadable module is one of + a set of similar modules (such as a file system or console + terminal emulation), and that the reference is through a + table (such as vfssw[]), and that a "special" value is + assigned to the slots which are allowed to be replaced. + This is not enforced, so you may use the kld module + any way you see fit. + + As with the loadable system calls, it may be desirable to + allow the module loader to replace an *existing* entry to + try out changes to kernel code without rebuilding and + booting from the new kernel. + + The idea behind this example is to show some interaction + with the device driver. Therefore the flow of the code that + this driver is aimed at is as follows: + + open(2) -> ioctl(2) -> write(2) -> read(2) -> close(2). + + We will first open the device in the /dev/ directory; then + we will send an ioctl message to it using ioctl(2) call; + then write a small string via the write(2) call. This string + we write to the device will be stored in a static buffer, + and later will be accessible via the read(2) call. Finally, + we will close(2) our open()'d device so that we may no + longer make read or write calls on it. + +2.0 Directions + + To test the module, do the following: + + cd module + make load + + A load message (the copyright) will be printed on the console. + + cd ../test + make load + + The system call prints a message on the console when called. + This message will be printed when running "make load" in + the "test" subdirectory. + + +3.0 Recovering resources + + The module consumes memory when loaded; it can be freed up by + unloading it. To unload it, type the following from the directory + this file is in: + + cd module + make unload + + The miscellaneous module will be unloaded by name. + + +4.0 END OF DOCUMENT diff --git a/share/examples/kld/cdev/module/Makefile b/share/examples/kld/cdev/module/Makefile new file mode 100644 index 000000000000..7db8ca7442c6 --- /dev/null +++ b/share/examples/kld/cdev/module/Makefile @@ -0,0 +1,8 @@ +# Makefile for kld char device driver. + +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/${KMOD}/module +KMOD= cdev +SRCS= cdev.c cdevmod.c + +.include <bsd.kmod.mk> diff --git a/share/examples/kld/cdev/module/cdev.c b/share/examples/kld/cdev/module/cdev.c new file mode 100644 index 000000000000..9d10e8d03f98 --- /dev/null +++ b/share/examples/kld/cdev/module/cdev.c @@ -0,0 +1,182 @@ +/* 08 Nov 1998*/ +/*- + * cdev.c + * + * 08 Nov 1998 Rajesh Vaidheeswarran + * + * SPDX-License-Identifier: BSD-4-Clause + * + * Copyright (c) 1998 Rajesh Vaidheeswarran + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Rajesh Vaidheeswarran. + * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``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 RAJESH VAIDHEESWARRAN 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. + * + * Copyright (c) 1993 Terrence R. Lambert. + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Terrence R. Lambert. + * 4. The name Terrence R. Lambert may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``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 TERRENCE R. LAMBERT 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. + * + */ +#include <sys/param.h> +#include <sys/uio.h> +#include <sys/proc.h> +#include <sys/systm.h> +#include <sys/ioccom.h> +#include <sys/conf.h> + +#include "cdev.h" + +/* + * This is the actual code for the system call... it can't be static because + * it is exported to another part of the module... the only place it needs + * to be referenced is the sysent we are interested in. + * + * To write your own system call using this as a template, you could strip + * out this code and use the rest as a prototype module, changing only the + * function names and the number of arguments to the call in the module + * specific "sysent". + * + * You would have to use the "-R" option of "ld" to ensure a linkable file + * if you were to do this, since you would need to combine multiple ".o" + * files into a single ".o" file for use by "modload". + */ + +#define CDEV_IOCTL1 _IOR('C', 1, u_int) + +/* Stores string recv'd by _write() */ +static char buf[512+1]; +static size_t len; + +int +mydev_open(struct cdev *dev, int flag, int otyp, struct thread *td) +{ + struct proc *procp = td->td_proc; + + printf("mydev_open: dev_t=%lu, flag=%x, otyp=%x, procp=%p\n", + dev2udev(dev), flag, otyp, procp); + memset(&buf, '\0', 513); + len = 0; + return (0); +} + +int +mydev_close(struct cdev *dev, int flag, int otyp, struct thread *td) +{ + struct proc *procp = td->td_proc; + + printf("mydev_close: dev_t=%lu, flag=%x, otyp=%x, procp=%p\n", + dev2udev(dev), flag, otyp, procp); + return (0); +} + +int +mydev_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, + struct thread *td) +{ + int error = 0; + struct proc *procp = td->td_proc; + + printf("mydev_ioctl: dev_t=%lu, cmd=%lx, arg=%p, mode=%x procp=%p\n", + dev2udev(dev), cmd, arg, mode, procp); + + switch(cmd) { + case CDEV_IOCTL1: + printf("you called mydev_ioctl CDEV_IOCTL1\n"); + break; + default: + printf("No such ioctl for me!\n"); + error = EINVAL; + break; + } + return (error); +} + +/* + * mydev_write takes in a character string and saves it + * to buf for later accessing. + */ +int +mydev_write(struct cdev *dev, struct uio *uio, int ioflag) +{ + int err = 0; + + printf("mydev_write: dev_t=%lu, uio=%p, ioflag=%d\n", + dev2udev(dev), uio, ioflag); + + err = copyinstr(uio->uio_iov->iov_base, &buf, 512, &len); + if (err != 0) { + printf("Write to \"cdev\" failed.\n"); + } + return(err); +} + +/* + * The mydev_read function just takes the buf that was saved + * via mydev_write() and returns it to userland for + * accessing. + */ +int +mydev_read(struct cdev *dev, struct uio *uio, int ioflag) +{ + int err = 0; + + printf("mydev_read: dev_t=%lu, uio=%p, ioflag=%d\n", + dev2udev(dev), uio, ioflag); + + if (len <= 0) { + err = -1; + } else { /* copy buf to userland */ + copystr(&buf, uio->uio_iov->iov_base, 513, &len); + } + return(err); +} diff --git a/share/examples/kld/cdev/module/cdev.h b/share/examples/kld/cdev/module/cdev.h new file mode 100644 index 000000000000..0ce0dd14d359 --- /dev/null +++ b/share/examples/kld/cdev/module/cdev.h @@ -0,0 +1,81 @@ +/* 08 Nov 1998*/ +/*- + * cdev.h - header for sample kld module implementing a character device + * driver. + * + * 08 Nov 1998 Rajesh Vaidheeswarran + * + * SPDX-License-Identifier: BSD-4-Clause + * + * Copyright (c) 1998 Rajesh Vaidheeswarran + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Rajesh Vaidheeswarran. + * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``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 RAJESH VAIDHEESWARRAN 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. + * + * Copyright (c) 1993 Terrence R. Lambert. + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Terrence R. Lambert. + * 4. The name Terrence R. Lambert may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``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 TERRENCE R. LAMBERT 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. + * + */ + +#ifndef __CDEV_H_ +#define __CDEV_H_ + +d_open_t mydev_open; +d_close_t mydev_close; +d_ioctl_t mydev_ioctl; +d_read_t mydev_read; +d_write_t mydev_write; + +#endif diff --git a/share/examples/kld/cdev/module/cdevmod.c b/share/examples/kld/cdev/module/cdevmod.c new file mode 100644 index 000000000000..1df008ac017b --- /dev/null +++ b/share/examples/kld/cdev/module/cdevmod.c @@ -0,0 +1,146 @@ +/* 08 Nov 1998*/ +/*- + * cdevmod.c - a sample kld module implementing a character device driver. + * + * 08 Nov 1998 Rajesh Vaidheeswarran + * + * SPDX-License-Identifier: BSD-4-Clause + * + * Copyright (c) 1998 Rajesh Vaidheeswarran + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Rajesh Vaidheeswarran. + * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``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 RAJESH VAIDHEESWARRAN 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. + * + * Copyright (c) 1993 Terrence R. Lambert. + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Terrence R. Lambert. + * 4. The name Terrence R. Lambert may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``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 TERRENCE R. LAMBERT 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. + * + */ +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/module.h> +#include <sys/conf.h> + +#include "cdev.h" + +static struct cdevsw my_devsw = { + /* version */ .d_version = D_VERSION, + /* open */ .d_open = mydev_open, + /* close */ .d_close = mydev_close, + /* read */ .d_read = mydev_read, + /* write */ .d_write = mydev_write, + /* ioctl */ .d_ioctl = mydev_ioctl, + /* name */ .d_name = "cdev" +}; + +/* + * Used as the variable that is the reference to our device + * in devfs... we must keep this variable sane until we + * call kldunload. + */ +static struct cdev *sdev; + +/* + * This function is called each time the module is loaded or unloaded. + * Since we are a miscellaneous module, we have to provide whatever + * code is necessary to patch ourselves into the area we are being + * loaded to change. + * + * The stat information is basically common to all modules, so there + * is no real issue involved with stat; we will leave it lkm_nullcmd(), + * since we don't have to do anything about it. + */ + +static int +cdev_load(module_t mod, int cmd, void *arg) +{ + int err = 0; + struct make_dev_args mda; + + switch (cmd) { + case MOD_LOAD: + + /* Do any initialization that you should do with the kernel */ + + /* if we make it to here, print copyright on console*/ + printf("\nSample Loaded kld character device driver\n"); + printf("Copyright (c) 1998\n"); + printf("Rajesh Vaidheeswarran\n"); + printf("All rights reserved\n"); + + make_dev_args_init(&mda); + mda.mda_devsw = &my_devsw; + mda.mda_uid = UID_ROOT; + mda.mda_gid = GID_WHEEL; + mda.mda_mode = 0600; + err = make_dev_s(&mda, &sdev, "cdev"); + break; + + case MOD_UNLOAD: + printf("Unloaded kld character device driver\n"); + destroy_dev(sdev); + break; /* Success*/ + + default: /* we only understand load/unload*/ + err = EOPNOTSUPP; + break; + } + + return(err); +} + +/* Now declare the module to the system */ + +DEV_MODULE(cdev, cdev_load, NULL); diff --git a/share/examples/kld/cdev/test/Makefile b/share/examples/kld/cdev/test/Makefile new file mode 100644 index 000000000000..3ac6e07d339f --- /dev/null +++ b/share/examples/kld/cdev/test/Makefile @@ -0,0 +1,96 @@ +# 05 Jun 93 +# +# Makefile for testmisc +# +# 05 Jun 93 Rajesh Vaidheeswarran Original +# +# Copyright (c) 1993 Rajesh Vaidheeswarran. +# 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. +# 3. All advertising materials mentioning features or use of this software +# must display the following acknowledgement: +# This product includes software developed by Rajesh Vaidheeswarran. +# 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote +# products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``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 RAJESH VAIDHEESWARRAN 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. +# +# Copyright (c) 1993 Terrence R. Lambert. +# 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. +# 3. All advertising materials mentioning features or use of this software +# must display the following acknowledgement: +# This product includes software developed by Terrence R. Lambert. +# 4. The name Terrence R. Lambert may not be used to endorse or promote +# products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``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 TERRENCE R. LAMBERT 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. +# +# +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/cdev/test +PROG= testcdev +MAN= +WARNS?= 5 + +MODSTAT= /sbin/kldstat + +load: + @echo "This test program will call the sample kld character device "; + @echo "driver." + @echo + @echo "The sample driver will display a message on the" + @echo "system console each time an ioctl is sent to it." + @echo + @echo + @echo + @./testcdev + +unload: + @echo "This test program will cause an error if the driver" + @echo "has been successfully unloaded by building 'unload' in" + @echo "the 'module' subdirectory." + @echo + ${MODSTAT} -n cdev + +install: + +.include <bsd.prog.mk> diff --git a/share/examples/kld/cdev/test/testcdev.c b/share/examples/kld/cdev/test/testcdev.c new file mode 100644 index 000000000000..55205ac9b73e --- /dev/null +++ b/share/examples/kld/cdev/test/testcdev.c @@ -0,0 +1,125 @@ +/* 08 Nov 1998*/ +/*- + * testmisc.c + * + * Test program to call the sample loaded kld device driver. + * + * 05 Jun 93 Rajesh Vaidheeswarran Original + * + * + * SPDX-License-Identifier: BSD-4-Clause + * + * Copyright (c) 1993 Rajesh Vaidheeswarran. + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Rajesh Vaidheeswarran. + * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``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 RAJESH VAIDHEESWARRAN 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. + * + * Copyright (c) 1993 Terrence R. Lambert. + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Terrence R. Lambert. + * 4. The name Terrence R. Lambert may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``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 TERRENCE R. LAMBERT 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. + * + */ +#include <sys/types.h> +#include <sys/ioccom.h> + +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <paths.h> +#include <string.h> +#include <unistd.h> + +#define CDEV_IOCTL1 _IOR('C', 1, u_int) +#define CDEV_DEVICE "cdev" + +static char writestr[] = "Hello kernel!"; +static char buf[512+1]; + +int +main(int argc __unused, char *argv[] __unused) +{ + int kernel_fd; + int one; + int len; + + if ((kernel_fd = open("/dev/" CDEV_DEVICE, O_RDWR)) == -1) { + perror("/dev/" CDEV_DEVICE); + exit(1); + } + + /* Send ioctl */ + if (ioctl(kernel_fd, CDEV_IOCTL1, &one) == -1) { + perror("CDEV_IOCTL1"); + } else { + printf( "Sent ioctl CDEV_IOCTL1 to device %s%s\n", _PATH_DEV, CDEV_DEVICE); + } + + len = strlen(writestr) + 1; + + /* Write operation */ + if (write(kernel_fd, writestr, len) == -1) { + perror("write()"); + } else { + printf("Written \"%s\" string to device /dev/" CDEV_DEVICE "\n", writestr); + } + + /* Read operation */ + if (read(kernel_fd, buf, len) == -1) { + perror("read()"); + } else { + printf("Read \"%s\" string from device /dev/" CDEV_DEVICE "\n", buf); + } + + exit(0); +} diff --git a/share/examples/kld/dyn_sysctl/Makefile b/share/examples/kld/dyn_sysctl/Makefile new file mode 100644 index 000000000000..e47eb3f339b3 --- /dev/null +++ b/share/examples/kld/dyn_sysctl/Makefile @@ -0,0 +1,6 @@ +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/${KMOD} +SRCS = dyn_sysctl.c +KMOD = dyn_sysctl + +.include <bsd.kmod.mk> diff --git a/share/examples/kld/dyn_sysctl/README b/share/examples/kld/dyn_sysctl/README new file mode 100644 index 000000000000..054a6e2328f7 --- /dev/null +++ b/share/examples/kld/dyn_sysctl/README @@ -0,0 +1,6 @@ +This example module creates partially overlapping subtrees to demonstrate +reference counting. It also contains example of attaching a subtree to the +wrong place, i.e. to a dynamic oid that could belong to someone else. +The framework should deal with this case gracefully. + +Andrzej Bialecki <abial@freebsd.org> diff --git a/share/examples/kld/dyn_sysctl/dyn_sysctl.c b/share/examples/kld/dyn_sysctl/dyn_sysctl.c new file mode 100644 index 000000000000..b6d390bb9583 --- /dev/null +++ b/share/examples/kld/dyn_sysctl/dyn_sysctl.c @@ -0,0 +1,167 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2000 Andrzej Bialecki <abial@freebsd.org> + * 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. + */ + +#include <sys/types.h> +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/module.h> +#include <sys/sysctl.h> +#include <sys/kernel.h> + + +/* Some example data */ +static long a = 100; +static int b = 200; +static char *c = "hi there from dyn_sysctl"; +static struct sysctl_oid *a_root, *a_root1, *b_root; +static struct sysctl_ctx_list clist, clist1, clist2; + +static int +sysctl_dyn_sysctl_test(SYSCTL_HANDLER_ARGS) +{ + char *buf = "let's produce some text..."; + + return (sysctl_handle_string(oidp, buf, strlen(buf), req)); +} + +/* + * The function called at load/unload. + */ +static int +load(module_t mod, int cmd, void *arg) +{ + int error; + + error = 0; + switch (cmd) { + case MOD_LOAD: + /* Initialize the contexts */ + printf("Initializing contexts and creating subtrees.\n\n"); + sysctl_ctx_init(&clist); + sysctl_ctx_init(&clist1); + sysctl_ctx_init(&clist2); + /* + * Create two partially overlapping subtrees, belonging + * to different contexts. + */ + printf("TREE ROOT NAME\n"); + a_root = SYSCTL_ADD_ROOT_NODE(&clist, + OID_AUTO, "dyn_sysctl", CTLFLAG_RW, 0, + "dyn_sysctl root node"); + a_root = SYSCTL_ADD_ROOT_NODE(&clist1, + OID_AUTO, "dyn_sysctl", CTLFLAG_RW, 0, + "dyn_sysctl root node"); + if (a_root == NULL) { + printf("SYSCTL_ADD_NODE failed!\n"); + return (EINVAL); + } + SYSCTL_ADD_LONG(&clist, SYSCTL_CHILDREN(a_root), + OID_AUTO, "long_a", CTLFLAG_RW, &a, "just to try"); + SYSCTL_ADD_INT(&clist, SYSCTL_CHILDREN(a_root), + OID_AUTO, "int_b", CTLFLAG_RW, &b, 0, "just to try 1"); + a_root1 = SYSCTL_ADD_NODE(&clist, SYSCTL_CHILDREN(a_root), + OID_AUTO, "nextlevel", CTLFLAG_RD, 0, "one level down"); + SYSCTL_ADD_STRING(&clist, SYSCTL_CHILDREN(a_root1), + OID_AUTO, "string_c", CTLFLAG_RD, c, 0, "just to try 2"); + printf("1. (%p) / dyn_sysctl\n", &clist); + + /* Add a subtree under already existing category */ + a_root1 = SYSCTL_ADD_NODE(&clist, SYSCTL_STATIC_CHILDREN(_kern), + OID_AUTO, "dyn_sysctl", CTLFLAG_RW, 0, "dyn_sysctl root node"); + if (a_root1 == NULL) { + printf("SYSCTL_ADD_NODE failed!\n"); + return (EINVAL); + } + SYSCTL_ADD_PROC(&clist, SYSCTL_CHILDREN(a_root1), + OID_AUTO, "procedure", CTLTYPE_STRING | CTLFLAG_RD, + NULL, 0, sysctl_dyn_sysctl_test, "A", + "I can be here, too"); + printf(" (%p) /kern dyn_sysctl\n", &clist); + + /* Overlap second tree with the first. */ + b_root = SYSCTL_ADD_NODE(&clist1, SYSCTL_CHILDREN(a_root), + OID_AUTO, "nextlevel", CTLFLAG_RD, 0, "one level down"); + SYSCTL_ADD_STRING(&clist1, SYSCTL_CHILDREN(b_root), + OID_AUTO, "string_c1", CTLFLAG_RD, c, 0, "just to try 2"); + printf("2. (%p) / dyn_sysctl (overlapping #1)\n", &clist1); + + /* + * And now do something stupid. Connect another subtree to + * dynamic oid. + * WARNING: this is an example of WRONG use of dynamic sysctls. + */ + b_root=SYSCTL_ADD_NODE(&clist2, SYSCTL_CHILDREN(a_root1), + OID_AUTO, "bad", CTLFLAG_RW, 0, "dependent node"); + SYSCTL_ADD_STRING(&clist2, SYSCTL_CHILDREN(b_root), + OID_AUTO, "string_c", CTLFLAG_RD, c, 0, "shouldn't panic"); + printf("3. (%p) /kern/dyn_sysctl bad (WRONG!)\n", &clist2); + break; + case MOD_UNLOAD: + printf("1. Try to free ctx1 (%p): ", &clist); + if (sysctl_ctx_free(&clist) != 0) + printf("failed: expected. Need to remove ctx3 first.\n"); + else + printf("HELP! sysctl_ctx_free(%p) succeeded. EXPECT PANIC!!!\n", &clist); + printf("2. Try to free ctx3 (%p): ", &clist2); + if (sysctl_ctx_free(&clist2) != 0) { + printf("sysctl_ctx_free(%p) failed!\n", &clist2); + /* Remove subtree forcefully... */ + sysctl_remove_oid(b_root, 1, 1); + printf("sysctl_remove_oid(%p) succeeded\n", b_root); + } else + printf("Ok\n"); + printf("3. Try to free ctx1 (%p) again: ", &clist); + if (sysctl_ctx_free(&clist) != 0) { + printf("sysctl_ctx_free(%p) failed!\n", &clist); + /* Remove subtree forcefully... */ + sysctl_remove_oid(a_root1, 1, 1); + printf("sysctl_remove_oid(%p) succeeded\n", a_root1); + } else + printf("Ok\n"); + printf("4. Try to free ctx2 (%p): ", &clist1); + if (sysctl_ctx_free(&clist1) != 0) { + printf("sysctl_ctx_free(%p) failed!\n", &clist1); + /* Remove subtree forcefully... */ + sysctl_remove_oid(a_root, 1, 1); + } else + printf("Ok\n"); + break; + default: + error = EOPNOTSUPP; + break; + } + return (error); +} + +static moduledata_t mod_data = { + "dyn_sysctl", + load, + 0 +}; + +DECLARE_MODULE(dyn_sysctl, mod_data, SI_SUB_EXEC, SI_ORDER_ANY); diff --git a/share/examples/kld/firmware/Makefile b/share/examples/kld/firmware/Makefile new file mode 100644 index 000000000000..5e31deaa17eb --- /dev/null +++ b/share/examples/kld/firmware/Makefile @@ -0,0 +1,5 @@ +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/firmware +SUBDIR= fwimage fwconsumer + +.include <bsd.subdir.mk> diff --git a/share/examples/kld/firmware/README b/share/examples/kld/firmware/README new file mode 100644 index 000000000000..473245a8e2ae --- /dev/null +++ b/share/examples/kld/firmware/README @@ -0,0 +1,17 @@ + +This is a simple example of the firmware(9) system. It consists of two +parts: + +1) fwimage + This is the firmware image (the ascii art of beastie from the boot + menu). The Makefile lists the firmware file "firmware.img" and the + short handle for this firmware image "beastie". + Note that the module is called "beastie" as well so that it can be + loaded automatically if requested. + +2) fwconsumer + This module tries to get the a firmware image called "beastie", + checks if the data is '\0'-terminated and prints it to the console. + It keeps a reference to the firmware until it is unloaded. + +This is mainly to demonstrate how to construct firmware image modules. diff --git a/share/examples/kld/firmware/fwconsumer/Makefile b/share/examples/kld/firmware/fwconsumer/Makefile new file mode 100644 index 000000000000..ce4f77d41d89 --- /dev/null +++ b/share/examples/kld/firmware/fwconsumer/Makefile @@ -0,0 +1,6 @@ +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/fwconsumer +KMOD= fw_consumer +SRCS= fw_consumer.c + +.include <bsd.kmod.mk> diff --git a/share/examples/kld/firmware/fwconsumer/fw_consumer.c b/share/examples/kld/firmware/fwconsumer/fw_consumer.c new file mode 100644 index 000000000000..8d749fed65df --- /dev/null +++ b/share/examples/kld/firmware/fwconsumer/fw_consumer.c @@ -0,0 +1,77 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2006, Max Laier <mlaier@FreeBSD.org> + * 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 unmodified, 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 ``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 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. + */ + +#include <sys/param.h> +#include <sys/kernel.h> +#include <sys/errno.h> +#include <sys/systm.h> +#include <sys/linker.h> +#include <sys/firmware.h> +#include <sys/proc.h> +#include <sys/module.h> + +static const struct firmware *fp; + +static int +fw_consumer_modevent(module_t mod, int type, void *unused) +{ + switch (type) { + case MOD_LOAD: + fp = firmware_get("beastie"); + + if (fp == NULL) + return (ENOENT); + + if (((const char *)fp->data)[fp->datasize - 1] != '\0') { + firmware_put(fp, FIRMWARE_UNLOAD); + return (EINVAL); + } + printf("%s", (const char *)fp->data); + + return (0); + case MOD_UNLOAD: + printf("Bye!\n"); + + if (fp != NULL) { + printf("%s", (const char *)fp->data); + firmware_put(fp, FIRMWARE_UNLOAD); + } + + return (0); + } + return (EINVAL); +} + +static moduledata_t fw_consumer_mod = { + "fw_consumer", + fw_consumer_modevent, + 0 +}; +DECLARE_MODULE(fw_consumer, fw_consumer_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); +MODULE_VERSION(fw_consumer, 1); +MODULE_DEPEND(fw_consumer, firmware, 1, 1, 1); diff --git a/share/examples/kld/firmware/fwimage/Makefile b/share/examples/kld/firmware/fwimage/Makefile new file mode 100644 index 000000000000..b57d2bf326b4 --- /dev/null +++ b/share/examples/kld/firmware/fwimage/Makefile @@ -0,0 +1,11 @@ +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/fwimage +KMOD= beastie +FIRMWS= firmware.img:beastie + +CLEANFILES= firmware.img + +firmware.img: firmware.img.uu + uudecode -p ${.ALLSRC} > ${.TARGET} + +.include <bsd.kmod.mk> diff --git a/share/examples/kld/firmware/fwimage/firmware.img.uu b/share/examples/kld/firmware/fwimage/firmware.img.uu new file mode 100644 index 000000000000..075877e0bcb3 --- /dev/null +++ b/share/examples/kld/firmware/fwimage/firmware.img.uu @@ -0,0 +1,15 @@ +begin 644 firmware.img +M("`@("`@("`@("`@("`L("`@("`@("`L"B`@("`@("`@("`@("`O*"`@("`@ +M("`@*0H@("`@("`@("`@("`@7"!<7U]?("`@+R!\"B`@("`@("`@("`@("`O +M+2!?("`M+R`@)PH@("`@("`@("`@("`H+UPO(%P@7"`@("]<"B`@("`@("`@ +M("`@("\@+R`@('P@("`@(%P*("`@("`@("`@("`@3R!/("`@*2`O("`@('P* +M("`@("`@("`@("`@+5XM+2<\("`@("`G"B`@("`@("`@("`@*%\N*2`@7R`@ +M*2`@("\*("`@("`@("`@("`@+E]?7R\@("`@+PH@("`@("`@("`@("`@("TM +M+2TM)R`O"B`\+2TM+2X@("`@(%]?("\@7U\@("!<"B`\+2TM+7P]/3T]3RDI +M*3T]*2!<*2`O/3T]/0H@/"TM+2TG("`@("TM)R`N7U\L)R!<"B`@("`@("`@ +M("`@("`@?"`@("`@("`@?`H@("`@("`@("`@("`@("!<("`@("`@("\@("`@ +M("`@+UP*("`@("`@("`@(%]?7U]?7R@@*%\@("\@7%]?7U]?7R\*("`@("`@ +M("`L)R`@+"TM+2TM)R`@('P*("`@("`@("`M+7M?7U]?7U]?7U]?*2`@0V]P +@>7)I9VAT("AC*2`R,#`S(%-C;W1T($QO;F<*```````` +` +end diff --git a/share/examples/kld/khelp/Makefile b/share/examples/kld/khelp/Makefile new file mode 100644 index 000000000000..dfdf602a4689 --- /dev/null +++ b/share/examples/kld/khelp/Makefile @@ -0,0 +1,11 @@ +.include <bsd.own.mk> + +# Change if the src tree you are compiling for is not in /usr/src +#SYSDIR=/usr/src/sys + +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/khelp +KMOD= h_example +SRCS= h_example.c + +.include <bsd.kmod.mk> diff --git a/share/examples/kld/khelp/README b/share/examples/kld/khelp/README new file mode 100644 index 000000000000..af201809864c --- /dev/null +++ b/share/examples/kld/khelp/README @@ -0,0 +1,5 @@ + +An example Khelp module which uses the helper hook points available in the TCP +stack to calculate a per-connection count of inbound and outbound packets when +the connection is in the established state. The code is verbosely documented in +an attempt to explain how everything fits together. diff --git a/share/examples/kld/khelp/h_example.c b/share/examples/kld/khelp/h_example.c new file mode 100644 index 000000000000..92f0c885bbce --- /dev/null +++ b/share/examples/kld/khelp/h_example.c @@ -0,0 +1,152 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2010-2011 The FreeBSD Foundation + * + * This software was developed at the Centre for Advanced Internet + * Architectures, Swinburne University of Technology, Melbourne, Australia by + * Lawrence Stewart under sponsorship from the FreeBSD Foundation. + * + * 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. + */ + +/* + * This example Khelp module uses the helper hook points available in the TCP + * stack to calculate a per-connection count of inbound and outbound packets + * when the connection is in the established state. The code is verbosely + * documented in an attempt to explain how everything fits together. + */ + +#include <sys/param.h> +#include <sys/kernel.h> +#include <sys/hhook.h> +#include <sys/khelp.h> +#include <sys/module.h> +#include <sys/module_khelp.h> +#include <sys/socket.h> +#include <sys/socketvar.h> + +#include <netinet/tcp_var.h> + +#include <vm/uma.h> + +/* + * Function prototype for our helper hook (man 9 hhook) compatible hook + * function. + */ +static int example_hook(int hhook_type, int hhook_id, void *udata, + void *ctx_data, void *hdata, struct osd *hosd); + +/* + * Our per-connection persistent data storage struct. + */ +struct example { + uint32_t est_in_count; + uint32_t est_out_count; +}; + +/* + * Fill in the required bits of our module's struct helper (defined in + * <sys/module_khelp.h>). + * + * - Our helper will be storing persistent state for each TCP connection, so we + * request the use the Object Specific Data (OSD) feature from the framework by + * setting the HELPER_NEEDS_OSD flag. + * + * - Our helper is related to the TCP subsystem, so tell the Khelp framework + * this by setting an appropriate class for the module. When a new TCP + * connection is created, the Khelp framework takes care of associating helper + * modules of the appropriate class with the new connection. + */ +struct helper example_helper = { + .h_flags = HELPER_NEEDS_OSD, + .h_classes = HELPER_CLASS_TCP +}; + +/* + * Set which helper hook points our module wants to hook by creating an array of + * hookinfo structs (defined in <sys/hhook.h>). We hook the TCP established + * inbound/outbound hook points (TCP hhook points are defined in + * <netinet/tcp_var.h>) with our example_hook() function. We don't require a user + * data pointer to be passed to our hook function when called, so we set it to + * NULL. + */ +struct hookinfo example_hooks[] = { + { + .hook_type = HHOOK_TYPE_TCP, + .hook_id = HHOOK_TCP_EST_IN, + .hook_udata = NULL, + .hook_func = &example_hook + }, + { + .hook_type = HHOOK_TYPE_TCP, + .hook_id = HHOOK_TCP_EST_OUT, + .hook_udata = NULL, + .hook_func = &example_hook + } +}; + +/* + * Very simple helper hook function. Here's a quick run through the arguments: + * + * - hhook_type and hhook_id are useful if you use a single function with many + * hook points and want to know which hook point called the function. + * + * - udata will be NULL, because we didn't elect to pass a pointer in either of + * the hookinfo structs we instantiated above in the example_hooks array. + * + * - ctx_data contains context specific data from the hook point call site. The + * data type passed is subsystem dependent. In the case of TCP, the hook points + * pass a pointer to a "struct tcp_hhook_data" (defined in <netinet/tcp_var.h>). + * + * - hdata is a pointer to the persistent per-object storage for our module. The + * pointer is allocated automagically by the Khelp framework when the connection + * is created, and comes from a dedicated UMA zone. It will never be NULL. + * + * - hosd can be used with the Khelp framework's khelp_get_osd() function to + * access data belonging to a different Khelp module. + */ +static int +example_hook(int hhook_type, int hhook_id, void *udata, void *ctx_data, + void *hdata, struct osd *hosd) +{ + struct example *data; + + data = hdata; + + if (hhook_id == HHOOK_TCP_EST_IN) + data->est_in_count++; + else if (hhook_id == HHOOK_TCP_EST_OUT) + data->est_out_count++; + + return (0); +} + +/* + * We use a convenient macro which handles registering our module with the Khelp + * framework. Note that Khelp modules which set the HELPER_NEEDS_OSD flag (i.e. + * require persistent per-object storage) must use the KHELP_DECLARE_MOD_UMA() + * macro. If you don't require per-object storage, use the KHELP_DECLARE_MOD() + * macro instead. + */ +KHELP_DECLARE_MOD_UMA(example, &example_helper, example_hooks, 1, + sizeof(struct example), NULL, NULL); diff --git a/share/examples/kld/random_adaptor/Makefile b/share/examples/kld/random_adaptor/Makefile new file mode 100644 index 000000000000..c9011bb2dba3 --- /dev/null +++ b/share/examples/kld/random_adaptor/Makefile @@ -0,0 +1,6 @@ +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/${KMOD} +KMOD= random_adaptor_example +SRCS= ${KMOD}.c + +.include <bsd.kmod.mk> diff --git a/share/examples/kld/random_adaptor/random_adaptor_example.c b/share/examples/kld/random_adaptor/random_adaptor_example.c new file mode 100644 index 000000000000..6dd3321d1286 --- /dev/null +++ b/share/examples/kld/random_adaptor/random_adaptor_example.c @@ -0,0 +1,124 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com> + * 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 ``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 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. + * + */ + +#include <sys/param.h> +#include <sys/kernel.h> +#include <sys/lock.h> +#include <sys/module.h> +#include <sys/random.h> +#include <sys/systm.h> + +#include <dev/random/randomdev.h> +#include <dev/random/randomdev_soft.h> +#include <dev/random/random_adaptors.h> +#include <dev/random/live_entropy_sources.h> + +static void live_random_example_init(void); +static void live_random_example_deinit(void); +static u_int live_random_example_read(void *, u_int); + +struct random_adaptor live_random_example = { + .les_ident = "Example RNG", + .les_source = RANDOM_PURE_BOGUS, /* Make sure this is in + * sys/random.h and is unique */ + .les_read = live_random_example_read, +}; + +/* + * Used under the license provided @ http://xkcd.com/221/ + * http://creativecommons.org/licenses/by-nc/2.5/ + */ +static uint8_t +getRandomNumber(void) +{ + return 4; /* chosen by fair dice roll, guaranteed to be random */ +} + +static void +live_random_example_init(void) +{ + + /* Do initialisation stuff here */ +} + +static void +live_random_example_deinit(void) +{ + + /* Do de-initialisation stuff here */ +} + +/* get <c> bytes of random stuff into <buf>. You may presume + * that <c> is a multiple of 2^n, with n>=3. A typical value + * is c=16. + */ +static u_int +live_random_example_read(void *buf, u_int c) +{ + uint8_t *b; + int count; + + b = buf; + + for (count = 0; count < c; count++) + b[count] = getRandomNumber(); + + /* printf("returning %d bytes of pure randomness\n", c); */ + return (c); +} + +/* ARGSUSED */ +static int +live_random_example_modevent(module_t mod __unused, int type, void *unused __unused) +{ + int error = 0; + + switch (type) { + case MOD_LOAD: + live_entropy_source_register(&live_random_example); + break; + + case MOD_UNLOAD: + live_entropy_source_deregister(&live_random_example); + break; + + case MOD_SHUTDOWN: + break; + + default: + error = EOPNOTSUPP; + break; + } + + return (error); +} + +DEV_MODULE(live_random_example, live_random_example_modevent, NULL); +MODULE_VERSION(live_random_example, 1); +MODULE_DEPEND(live_random_example, randomdev, 1, 1, 1); diff --git a/share/examples/kld/syscall/Makefile b/share/examples/kld/syscall/Makefile new file mode 100644 index 000000000000..4270f388bd1b --- /dev/null +++ b/share/examples/kld/syscall/Makefile @@ -0,0 +1,7 @@ +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/syscall +SUBDIR= module test + +load unload: _SUBDIR + +.include <bsd.subdir.mk> diff --git a/share/examples/kld/syscall/module/Makefile b/share/examples/kld/syscall/module/Makefile new file mode 100644 index 000000000000..760825e4d52a --- /dev/null +++ b/share/examples/kld/syscall/module/Makefile @@ -0,0 +1,8 @@ +# Makefile for building the sample syscall module + +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/${KMOD} +KMOD= syscall +SRCS= syscall.c + +.include <bsd.kmod.mk> diff --git a/share/examples/kld/syscall/module/syscall.c b/share/examples/kld/syscall/module/syscall.c new file mode 100644 index 000000000000..3f6051d03777 --- /dev/null +++ b/share/examples/kld/syscall/module/syscall.c @@ -0,0 +1,83 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 1999 Assar Westerlund + * 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. + */ + +#include <sys/param.h> +#include <sys/proc.h> +#include <sys/module.h> +#include <sys/sysproto.h> +#include <sys/sysent.h> +#include <sys/kernel.h> +#include <sys/systm.h> + +/* + * The function for implementing the syscall. + */ +static int +hello(struct thread *td, void *arg) +{ + + printf("hello kernel\n"); + return (0); +} + +/* + * The `sysent' for the new syscall + */ +static struct sysent hello_sysent = { + .sy_narg = 0, + .sy_call = hello +}; + +/* + * The offset in sysent where the syscall is allocated. + */ +static int offset = NO_SYSCALL; + +/* + * The function called at load/unload. + */ +static int +load(struct module *module, int cmd, void *arg) +{ + int error = 0; + + switch (cmd) { + case MOD_LOAD : + printf("syscall loaded at %d\n", offset); + break; + case MOD_UNLOAD : + printf("syscall unloaded from %d\n", offset); + break; + default : + error = EOPNOTSUPP; + break; + } + return (error); +} + +SYSCALL_MODULE(syscall, &offset, &hello_sysent, load, NULL); diff --git a/share/examples/kld/syscall/test/Makefile b/share/examples/kld/syscall/test/Makefile new file mode 100644 index 000000000000..d14cd895c15d --- /dev/null +++ b/share/examples/kld/syscall/test/Makefile @@ -0,0 +1,9 @@ +# Makefile for simple caller of syscall + +PACKAGE=examples +FILESDIR=${SHAREDIR}/examples/kld/test +PROG= call +MAN= +WARNS?= 5 + +.include <bsd.prog.mk> diff --git a/share/examples/kld/syscall/test/call.c b/share/examples/kld/syscall/test/call.c new file mode 100644 index 000000000000..e62f8f27f1dd --- /dev/null +++ b/share/examples/kld/syscall/test/call.c @@ -0,0 +1,51 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 1999 Assar Westerlund + * 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. + */ + +#include <sys/param.h> +#include <sys/module.h> +#include <sys/syscall.h> + +#include <err.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +int +main(int argc __unused, char **argv __unused) +{ + int modid, syscall_num; + struct module_stat stat; + + stat.version = sizeof(stat); + if ((modid = modfind("sys/syscall")) == -1) + err(1, "modfind"); + if (modstat(modid, &stat) != 0) + err(1, "modstat"); + syscall_num = stat.data.intval; + return syscall (syscall_num); +} |