Source-Changes-HG archive

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

[src/trunk]: src/sys/modules/examples Add new example kernel module: current_...



details:   https://anonhg.NetBSD.org/src/rev/52054faba552
branches:  trunk
changeset: 970843:52054faba552
user:      kamil <kamil%NetBSD.org@localhost>
date:      Sat Apr 04 21:15:04 2020 +0000

description:
Add new example kernel module: current_time

Submitted by Apurva Nandan.

diffstat:

 sys/modules/examples/Makefile                    |   3 +-
 sys/modules/examples/README                      |   6 +-
 sys/modules/examples/current_time/Makefile       |   9 ++
 sys/modules/examples/current_time/current_time.c |  81 ++++++++++++++++++++++++
 4 files changed, 97 insertions(+), 2 deletions(-)

diffs (137 lines):

diff -r bf28b3399294 -r 52054faba552 sys/modules/examples/Makefile
--- a/sys/modules/examples/Makefile     Sat Apr 04 21:13:20 2020 +0000
+++ b/sys/modules/examples/Makefile     Sat Apr 04 21:15:04 2020 +0000
@@ -1,7 +1,8 @@
-#      $NetBSD: Makefile,v 1.9 2020/04/01 01:57:20 kamil Exp $
+#      $NetBSD: Makefile,v 1.10 2020/04/04 21:15:04 kamil Exp $
 
 .include <bsd.own.mk>
 
+SUBDIR+=       current_time
 SUBDIR+=       executor
 SUBDIR+=       fopsmapper              # Needs an additional helper program
 SUBDIR+=       hello
diff -r bf28b3399294 -r 52054faba552 sys/modules/examples/README
--- a/sys/modules/examples/README       Sat Apr 04 21:13:20 2020 +0000
+++ b/sys/modules/examples/README       Sat Apr 04 21:15:04 2020 +0000
@@ -1,4 +1,4 @@
-       $NetBSD: README,v 1.13 2020/04/01 01:57:20 kamil Exp $
+       $NetBSD: README,v 1.14 2020/04/04 21:15:04 kamil Exp $
 
                            Kernel Developer's Manual
 
@@ -6,6 +6,7 @@
      The kernel example dynamic modules.
 
      This directory contains the following example modules:
+     * current_time    - prints current date and time in GMT/UTC
      * executor        - basic implementation of callout and RUN_ONCE
      * fopsmapper      - basic implementation of mmap with fileops fo_mmap
      * hello           - the simplest `hello world' module
@@ -78,5 +79,8 @@
      The fopsmapper module first appeared in NetBSD 10.0 and was authored by
      Aditya Vardhan Padala.
 
+     The current_time module first appeared in NetBSD 10.0 and was authored by
+     Apurva Nandan.
+
 AUTHORS
      This document was written by Kamil Rytarowski.
diff -r bf28b3399294 -r 52054faba552 sys/modules/examples/current_time/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/current_time/Makefile        Sat Apr 04 21:15:04 2020 +0000
@@ -0,0 +1,9 @@
+#      $NetBSD: Makefile,v 1.1 2020/04/04 21:15:04 kamil Exp $
+
+.include "../Makefile.inc"
+
+#S?=   /usr/src/sys
+KMOD=  current_time
+SRCS=  current_time.c
+
+.include <bsd.kmodule.mk>
diff -r bf28b3399294 -r 52054faba552 sys/modules/examples/current_time/current_time.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/current_time/current_time.c  Sat Apr 04 21:15:04 2020 +0000
@@ -0,0 +1,81 @@
+/*     $NetBSD: current_time.c,v 1.1 2020/04/04 21:15:04 kamil 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: current_time.c,v 1.1 2020/04/04 21:15:04 kamil Exp $");
+
+#include <sys/param.h>
+#include <sys/module.h>
+#include <dev/clock_subr.h>
+
+/*
+ * Function print_time() fetches the epoch seconds/unix time from the
+ * getmicrotime() function and sends its to clock_secs_to_ymdhms(..) in
+ * dev/clock_subr to parse it into readable date and time format and print it.
+ *
+ * Please note that the current time is printed in GMT/UTC because the kernel
+ * doesn't have the notion of local timezones.
+ */
+
+static void
+print_current_time(void)
+{
+       struct timeval tv;
+       struct clock_ymdhms dt;
+       const char *w_day[] = {
+               "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
+               "Friday", "Saturday"
+       };
+       
+       getmicrotime(&tv);
+       clock_secs_to_ymdhms(tv.tv_sec, &dt);
+    
+       printf("Current Time: %s, %04lu/%02u/%02u %02u:%02u:%02u UTC\n",
+           w_day[dt.dt_wday], dt.dt_year, dt.dt_mon, dt.dt_day, dt.dt_hour,
+           dt.dt_min, dt.dt_sec);
+}
+
+MODULE(MODULE_CLASS_MISC, current_time, NULL);
+
+static int
+current_time_modcmd(modcmd_t cmd, void *arg __unused)
+{
+       switch (cmd) {
+       case MODULE_CMD_INIT:
+               printf("current_time example module loaded.\n");
+               print_current_time();
+               break;
+       case MODULE_CMD_FINI:
+               printf("current_time example module unloaded.\n");
+               break;
+       default:
+               return ENOTTY;
+       }
+
+       return 0;
+}



Home | Main Index | Thread Index | Old Index