Skip to content

Instantly share code, notes, and snippets.

Created December 31, 2010 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/761004 to your computer and use it in GitHub Desktop.
Save anonymous/761004 to your computer and use it in GitHub Desktop.
C++でLinuxカーネルモジュール、モジュール本体
#include "stdafx.h"
#define HELLOCXX_MAJOR 204
static const char driver_name[] = "hellocxx";
static int hellocxx_open(struct inode* inode, struct file* f)
{
return 0;
}
static int hellocxx_release(struct inode* inode, struct file* f)
{
return 0;
}
static ssize_t hellocxx_read(struct file* f, char* buf, size_t count, loff_t* pos)
{
if (*pos != 0) {
return 0;
}
char message[] = "Hello, C++\n";
ssize_t copy_count =
count < sizeof(message) ? count : sizeof(message);
copy_to_user(buf, message, copy_count);
*pos += copy_count;
return copy_count;
}
static struct file_operations fops = {0};
static int __init hellocxx_init(void)
{
fops.owner = THIS_MODULE;
fops.read = hellocxx_read;
fops.open = hellocxx_open;
fops.release = hellocxx_release;
printk("hellocxx init 1\n");
int r = register_chrdev(HELLOCXX_MAJOR, driver_name, &fops);
if (r < 0) {
printk("hellocxx register_chrdev failed %d\n", r);
}
printk("hellocxx init 2\n");
return 0;
}
static void __exit hellocxx_exit(void)
{
printk("hellocxx exit 1\n");
unregister_chrdev(HELLOCXX_MAJOR, driver_name);
printk("hellocxx exit 2\n");
}
MODULE_AUTHOR("saturday06 - Jinsei Owata - <dyob@lunaport.net>");
MODULE_DESCRIPTION("C++ kernel module");
MODULE_LICENSE("Dual BSD/GPL");
module_init(hellocxx_init);
module_exit(hellocxx_exit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment