From: "Américo Wang" <xiyou.wangcong@gmail.com>
To: Christopher Brannon <cmbrannon@cox.net>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] /dev/time for Linux, inspired by Plan 9
Date: Wed, 11 Mar 2009 15:28:55 +0800 [thread overview]
Message-ID: <20090311072855.GC3262@hack> (raw)
In-Reply-To: <20090304031238.WCEC18213.eastrmmtao106.cox.net@eastrmimpo03.cox.net>
On Tue, Mar 03, 2009 at 09:06:46PM -0600, Christopher Brannon wrote:
<snip>
>@@ -0,0 +1,112 @@
>+#include <linux/init.h>
>+#include <linux/module.h>
>+#include <linux/fs.h>
>+#include <linux/miscdevice.h>
>+#include <linux/time.h>
>+#include <linux/jiffies.h>
>+#include <linux/uaccess.h>
>+
>+MODULE_AUTHOR("Christopher Brannon <cmbrannon79@gmail.com>");
>+MODULE_LICENSE("GPL");
>+
>+const long long NS_PER_SEC = 1000000000;
>+const long long NS_PER_USEC = 1000;
>+const size_t time_bufsize = 256;
I notice that you use this "const" as an array size below, so
you are using VLA, yes, C99 supports it but this is in the kernel,
try to avoid this by using marco:
#define BUFSIZE 256
And yes, C's const sucks. :)
>+
>+static ssize_t time_read(struct file *, char __user *, size_t, loff_t *);
>+static ssize_t time_write(struct file *, const char __user *, size_t, loff_t *);
>+
>+static const struct file_operations time_fops = {
>+ .owner = THIS_MODULE,
>+ .read = time_read,
>+ .write = time_write,
>+};
>+
>+static struct miscdevice timedev = {
>+ .minor = MISC_DYNAMIC_MINOR,
>+ .name = "time",
>+ .fops = &time_fops
>+};
>+
>+static int time2text(char *buffer, size_t bufsize)
>+{
>+ int count = 0;
>+ struct timeval tv;
>+ long long nanos;
>+
>+/* jiffies isn't 0 at boot time; its value is INITIAL_JIFFIES. */
>+ u64 ticks = get_jiffies_64() - INITIAL_JIFFIES;
>+
>+ do_gettimeofday(&tv);
>+ nanos = tv.tv_sec * NS_PER_SEC + tv.tv_usec * NS_PER_USEC;
>+ count =
>+ scnprintf(buffer, bufsize, "%ld %lld %llu %d\n", tv.tv_sec, nanos,
>+ ticks, HZ);
>+ return count;
>+}
>+
>+static int text2time(char *buffer)
>+{
>+ struct timespec tv;
>+ int result = strict_strtol(buffer, 10, &tv.tv_sec);
>+ if ((result == 0) && (tv.tv_sec > 0)) {
>+ tv.tv_nsec = 0;
>+ do_settimeofday(&tv);
>+ } else
>+ result = -EINVAL; /* only positive longs are valid. */
>+ return result;
>+}
>+
>+static ssize_t
>+time_read(struct file *f, char __user *buffer, size_t count, loff_t * offset)
>+{
>+ int result = 0;
>+ if (*offset != 0)
>+ result = 0;
>+ else {
>+ char tmpbuf[time_bufsize];
>+ int timetextlen = time2text(tmpbuf, time_bufsize);
>+ unsigned long readcount = min(count, (size_t) timetextlen);
>+ if (timetextlen <= 0)
>+ return -EAGAIN;
>+ if (!copy_to_user(buffer, tmpbuf, readcount)) {
>+ *offset += readcount;
>+ result = readcount;
>+ } else
>+ result = -EFAULT;
>+ }
>+ return result;
>+}
>+
>+static ssize_t
>+time_write(struct file *f, const char __user * buffer, size_t count,
>+ loff_t *offset)
>+{
>+ unsigned int result = 0;
>+ char tmpbuf[time_bufsize];
>+
>+ if (*offset != 0)
>+ return -EINVAL;
>+ if (count > ((size_t) time_bufsize - 1))
>+ return -EINVAL; /* Likely trying to feed bogus data anyway. */
>+ result = copy_from_user(tmpbuf, buffer, count);
>+ if (result)
>+ return -EFAULT;
>+ tmpbuf[count] = '\0';
>+ if (text2time(tmpbuf))
>+ return -EINVAL;
>+ return count;
>+}
>+
>+static int __init time_init(void)
>+{
>+ return misc_register(&timedev);
>+}
>+
>+static void __exit time_exit(void)
>+{
>+ misc_deregister(&timedev);
>+}
>+
>+module_init(time_init);
>+module_exit(time_exit);
>--
>1.6.1.3
>
>--
>To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at http://vger.kernel.org/majordomo-info.html
>Please read the FAQ at http://www.tux.org/lkml/
--
Do what you love, f**k the rest! F**k the regulations!
next prev parent reply other threads:[~2009-03-11 7:28 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-03-04 3:06 [PATCH] /dev/time for Linux, inspired by Plan 9 Christopher Brannon
2009-03-06 17:57 ` Greg KH
2009-03-07 5:43 ` J.R. Mauro
2009-03-07 5:54 ` Greg KH
2009-03-07 16:08 ` Chris Brannon
2009-03-08 16:03 ` J.R. Mauro
2009-03-06 22:56 ` john stultz
2009-03-07 16:23 ` Chris Brannon
2009-03-12 18:12 ` J.R. Mauro
2009-03-14 23:27 ` Chris Brannon
2009-03-10 11:22 ` Pavel Machek
2009-03-11 7:28 ` Américo Wang [this message]
-- strict thread matches above, loose matches on Subject: below --
2009-03-11 15:10 Christopher Brannon
2009-03-11 21:49 ` Jochen Voß
2009-03-11 22:07 ` Bill Nottingham
2009-03-12 8:00 ` David Newall
2009-03-12 18:15 ` J.R. Mauro
2009-03-12 9:30 ` Giacomo A. Catenazzi
2009-03-14 16:13 ` Pavel Machek
2009-03-17 15:15 ` J.R. Mauro
2009-03-21 22:05 ` Pavel Machek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090311072855.GC3262@hack \
--to=xiyou.wangcong@gmail.com \
--cc=cmbrannon@cox.net \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox