Module Name: src
Committed By: kamil
Date: Sat Apr 4 21:15:04 UTC 2020
Modified Files:
src/sys/modules/examples: Makefile README
Added Files:
src/sys/modules/examples/current_time: Makefile current_time.c
Log Message:
Add new example kernel module: current_time
Submitted by Apurva Nandan.
To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/sys/modules/examples/Makefile
cvs rdiff -u -r1.13 -r1.14 src/sys/modules/examples/README
cvs rdiff -u -r0 -r1.1 src/sys/modules/examples/current_time/Makefile \
src/sys/modules/examples/current_time/current_time.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/modules/examples/Makefile
diff -u src/sys/modules/examples/Makefile:1.9 src/sys/modules/examples/Makefile:1.10
--- src/sys/modules/examples/Makefile:1.9 Wed Apr 1 01:57:20 2020
+++ src/sys/modules/examples/Makefile Sat Apr 4 21:15:04 2020
@@ -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
Index: src/sys/modules/examples/README
diff -u src/sys/modules/examples/README:1.13 src/sys/modules/examples/README:1.14
--- src/sys/modules/examples/README:1.13 Wed Apr 1 01:57:20 2020
+++ src/sys/modules/examples/README Sat Apr 4 21:15:04 2020
@@ -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 @@ DESCRIPTION
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 @@ HISTORY
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.
Added files:
Index: src/sys/modules/examples/current_time/Makefile
diff -u /dev/null src/sys/modules/examples/current_time/Makefile:1.1
--- /dev/null Sat Apr 4 21:15:04 2020
+++ src/sys/modules/examples/current_time/Makefile Sat Apr 4 21:15:04 2020
@@ -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>
Index: src/sys/modules/examples/current_time/current_time.c
diff -u /dev/null src/sys/modules/examples/current_time/current_time.c:1.1
--- /dev/null Sat Apr 4 21:15:04 2020
+++ src/sys/modules/examples/current_time/current_time.c Sat Apr 4 21:15:04 2020
@@ -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;
+}