Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/modules/examples New kernel example module written by Ay...



details:   https://anonhg.NetBSD.org/src/rev/67f859aafd99
branches:  trunk
changeset: 931738:67f859aafd99
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Apr 30 00:48:10 2020 +0000

description:
New kernel example module written by Ayushi Sharma

diffstat:

 sys/modules/examples/Makefile              |    3 +-
 sys/modules/examples/README                |    6 +-
 sys/modules/examples/ping/ping.c           |    7 +-
 sys/modules/examples/pollpal/Makefile      |   18 +
 sys/modules/examples/pollpal/cmd_pollpal.c |  130 +++++++++++
 sys/modules/examples/pollpal/pollpal.c     |  317 +++++++++++++++++++++++++++++
 6 files changed, 477 insertions(+), 4 deletions(-)

diffs (truncated from 554 to 300 lines):

diff -r 6ed486e788ff -r 67f859aafd99 sys/modules/examples/Makefile
--- a/sys/modules/examples/Makefile     Thu Apr 30 00:32:16 2020 +0000
+++ b/sys/modules/examples/Makefile     Thu Apr 30 00:48:10 2020 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.10 2020/04/04 21:15:04 kamil Exp $
+#      $NetBSD: Makefile,v 1.11 2020/04/30 00:48:10 christos Exp $
 
 .include <bsd.own.mk>
 
@@ -12,6 +12,7 @@
 SUBDIR+=       panic_string            # Crashes the system
 SUBDIR+=       ping                    # Needs an additional helper program
 SUBDIR+=       ping_block              # Needs an additional helper program
+SUBDIR+=       pollpal                 # Needs an additional helper program
 SUBDIR+=       properties
 SUBDIR+=       readhappy
 SUBDIR+=       readhappy_mpsafe        # Contains an additional helper program
diff -r 6ed486e788ff -r 67f859aafd99 sys/modules/examples/README
--- a/sys/modules/examples/README       Thu Apr 30 00:32:16 2020 +0000
+++ b/sys/modules/examples/README       Thu Apr 30 00:48:10 2020 +0000
@@ -1,4 +1,4 @@
-       $NetBSD: README,v 1.14 2020/04/04 21:15:04 kamil Exp $
+       $NetBSD: README,v 1.15 2020/04/30 00:48:10 christos Exp $
 
                            Kernel Developer's Manual
 
@@ -17,6 +17,7 @@
      * ping            - basic ioctl(9)
      * ping_block      - basic ioctl(9) with a block device
      * properties      - handle incoming properties during the module load
+     * pollpal         - implementation of basic poll(9) using palindrome
      * readhappy       - basic implementation of read(9) with happy numbers
      * readhappy_mpsafe- demonstrates how to make a module MPSAFE
      * sysctl          - demonstrates adding a sysctl handle dynamically
@@ -82,5 +83,8 @@
      The current_time module first appeared in NetBSD 10.0 and was authored by
      Apurva Nandan.
 
+     The pollpall module first appeared in NetBSD 10.0 and was authored by
+     Ayushi Sharma.
+
 AUTHORS
      This document was written by Kamil Rytarowski.
diff -r 6ed486e788ff -r 67f859aafd99 sys/modules/examples/ping/ping.c
--- a/sys/modules/examples/ping/ping.c  Thu Apr 30 00:32:16 2020 +0000
+++ b/sys/modules/examples/ping/ping.c  Thu Apr 30 00:48:10 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ping.c,v 1.3 2020/02/05 14:10:46 pgoyette Exp $        */
+/*     $NetBSD: ping.c,v 1.4 2020/04/30 00:48:10 christos Exp $        */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -27,13 +27,15 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ping.c,v 1.3 2020/02/05 14:10:46 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ping.c,v 1.4 2020/04/30 00:48:10 christos Exp $");
 
+#define DDB
 #include <sys/param.h>
 #include <sys/conf.h>
 #include <sys/device.h>
 #include <sys/kernel.h>
 #include <sys/module.h>
+#include <sys/systm.h>
 
 #include "ping.h"
 
@@ -99,6 +101,7 @@
        switch(cmd) {
        case CMD_PING:
                printf("ping: pong!\n");
+               Debugger();
                return 0;
        default:
                return ENOTTY;
diff -r 6ed486e788ff -r 67f859aafd99 sys/modules/examples/pollpal/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/pollpal/Makefile     Thu Apr 30 00:48:10 2020 +0000
@@ -0,0 +1,18 @@
+#   $NetBSD: Makefile,v 1.1 2020/04/30 00:48:10 christos Exp $
+
+.include "../Makefile.inc"
+
+#S?=   /usr/src/sys
+
+KMOD=   pollpal
+SRCS=   pollpal.c
+
+.include <bsd.kmodule.mk>
+
+# To make use of this module, you'll need to separately build the
+# cmd_pollpall program, with a Makefile similar to
+#
+#   MKMAN=  NO
+#   PROG=   cmd_pollpal
+#   .include <bsd.prog.mk>
+
diff -r 6ed486e788ff -r 67f859aafd99 sys/modules/examples/pollpal/cmd_pollpal.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/pollpal/cmd_pollpal.c        Thu Apr 30 00:48:10 2020 +0000
@@ -0,0 +1,130 @@
+/*     $NetBSD: cmd_pollpal.c,v 1.1 2020/04/30 00:48:10 christos Exp $ */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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/cdefs.h>
+__RCSID("$NetBSD: cmd_pollpal.c,v 1.1 2020/04/30 00:48:10 christos Exp $");
+
+#include <sys/mman.h>
+
+#include <errno.h>
+#include <err.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+
+#define BUFFLEN 100
+#define TIMEOUT 10000
+
+static int fd; 
+
+static __dead __printflike(2, 3) void 
+done(int e, const char *msg, ...)
+{
+       if (msg) {
+               va_list ap;
+
+               va_start(ap, msg);
+               verr(e, msg, ap);
+               /*NOTREACHED*/
+               va_end(ap);
+       }
+       exit(e);
+}
+       
+static __dead void 
+handler(int sig)
+{
+       done(EXIT_SUCCESS, NULL);
+}
+
+static const char dev[] = "/dev/pollpal";
+static const char wbuf[] = "abcdefghijklmnopqrstuvwxyz";
+
+int 
+main(int argc, char **argv)
+{
+       struct pollfd fds;
+       ssize_t ret;
+       size_t i;
+       char rbuf[BUFFLEN];
+
+       setprogname(argv[0]);
+
+       fd = open(dev, O_RDWR, 0);
+       if (fd == -1)
+               err(EXIT_FAILURE, "open `%s'", dev);
+
+       fds.fd = fd;
+       fds.events = POLLOUT|POLLIN;
+
+       signal(SIGINT, handler);
+       for (i = 1; i <= 100; i++) {
+               ret = poll(&fds, 1, TIMEOUT);
+               if (ret == -1) {
+                       if (errno == EINTR)
+                               continue;
+                       done(EXIT_FAILURE, "poll");
+               }
+               printf("Poll_executing %zu times\n", i);
+               if (ret == 0) {
+                       printf("%d seconds elapsed \n.TIMEOUT.\n",
+                           TIMEOUT / 1000);
+                       done(EXIT_SUCCESS, NULL);
+               }
+               
+               if (fds.revents & (POLLERR | POLLHUP | POLLNVAL)) 
+                       done(EXIT_FAILURE, "ERR|HUP|NVAL");
+               
+               if (fds.revents & POLLOUT) {
+                       do {
+                               ret = write(fd, wbuf, sizeof(wbuf) - 1);
+                       } while (ret == -1 && errno == EINTR);
+                       
+                       if (ret == -1) {
+                               done(EXIT_FAILURE, "write");
+                       }
+                       printf("Wrote %zd byte\n", ret);
+               }
+               if (fds.revents & POLLIN) {
+                       memset(rbuf, 0, BUFFLEN);
+                       do {
+                               ret = read(fd, rbuf, BUFFLEN);
+                       } while (ret == -1 && errno == EINTR);
+                       if (ret == -1) {
+                               done(EXIT_FAILURE, "read");
+                       }
+                       printf("Read %zd bytes\n", ret);
+               }
+       }
+       done(EXIT_SUCCESS, NULL);
+}
diff -r 6ed486e788ff -r 67f859aafd99 sys/modules/examples/pollpal/pollpal.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/pollpal/pollpal.c    Thu Apr 30 00:48:10 2020 +0000
@@ -0,0 +1,317 @@
+/*     $NetBSD: pollpal.c,v 1.1 2020/04/30 00:48:10 christos Exp $     */ 
+
+/*-
+* Copyright (c) 2020 The NetBSD Foundation, Inc.
+* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: pollpal.c,v 1.1 2020/04/30 00:48:10 christos Exp $");
+
+#include <sys/module.h>
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/systm.h>
+
+#include <sys/condvar.h>
+#include <sys/conf.h>
+#include <sys/device.h>
+#include <sys/file.h>
+#include <sys/filedesc.h>
+#include <sys/mutex.h>
+#include <sys/kmem.h>
+#include <sys/poll.h>
+#include <sys/select.h>
+
+/* 
+ * Create a device /dev/pollpal
+ *
+ * To use this device you need to do:
+ *     mknod /dev/pollpal c 351 0
+ *
+ */ 
+
+dev_type_open(pollpal_open);
+
+static struct cdevsw pollpal_cdevsw = {
+       .d_open = pollpal_open,
+       .d_close = noclose,
+       .d_read = noread,
+       .d_write = nowrite,
+       .d_ioctl = noioctl,
+       .d_stop = nostop,



Home | Main Index | Thread Index | Old Index