Skip to content

Commit

Permalink
kernel module: format, use tab to indent.
Browse files Browse the repository at this point in the history
  • Loading branch information
reveriel authored and cirosantilli committed Dec 12, 2018
1 parent 9f6ddbc commit 3b0a343
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 117 deletions.
52 changes: 26 additions & 26 deletions kernel_modules/character_device_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,42 @@ static const struct file_operations fops = {

static void cleanup(int device_created)
{
if (device_created) {
device_destroy(myclass, major);
cdev_del(&mycdev);
}
if (myclass)
class_destroy(myclass);
if (major != -1)
unregister_chrdev_region(major, 1);
if (device_created) {
device_destroy(myclass, major);
cdev_del(&mycdev);
}
if (myclass)
class_destroy(myclass);
if (major != -1)
unregister_chrdev_region(major, 1);
}

static int myinit(void)
{
int device_created = 0;
int device_created = 0;

/* cat /proc/devices */
if (alloc_chrdev_region(&major, 0, 1, NAME "_proc") < 0)
goto error;
/* ls /sys/class */
if ((myclass = class_create(THIS_MODULE, NAME "_sys")) == NULL)
goto error;
/* ls /dev/ */
if (device_create(myclass, NULL, major, NULL, NAME "_dev") == NULL)
goto error;
device_created = 1;
cdev_init(&mycdev, &fops);
if (cdev_add(&mycdev, major, 1) == -1)
goto error;
return 0;
/* cat /proc/devices */
if (alloc_chrdev_region(&major, 0, 1, NAME "_proc") < 0)
goto error;
/* ls /sys/class */
if ((myclass = class_create(THIS_MODULE, NAME "_sys")) == NULL)
goto error;
/* ls /dev/ */
if (device_create(myclass, NULL, major, NULL, NAME "_dev") == NULL)
goto error;
device_created = 1;
cdev_init(&mycdev, &fops);
if (cdev_add(&mycdev, major, 1) == -1)
goto error;
return 0;
error:
cleanup(device_created);
return -1;
cleanup(device_created);
return -1;
}

static void myexit(void)
{
cleanup(1);
cleanup(1);
}

module_init(myinit)
Expand Down
4 changes: 2 additions & 2 deletions kernel_modules/memcpy_overflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
static int myinit(void)
{
void *dst, *src;
dst = kmalloc(0x10, GFP_KERNEL);
src = kmalloc(0x1000000, GFP_KERNEL);
dst = kmalloc(0x10, GFP_KERNEL);
src = kmalloc(0x1000000, GFP_KERNEL);
memcpy(dst, src, 0x1000000);
return 0;
}
Expand Down
20 changes: 10 additions & 10 deletions kernel_modules/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static void vm_close(struct vm_area_struct *vma)
}

/* First page access. */
int (*fault)(struct vm_fault *vmf);
int (*fault)(struct vm_fault *vmf);
static int vm_fault(struct vm_fault *vmf)
{
struct page *page;
Expand Down Expand Up @@ -79,13 +79,13 @@ static int open(struct inode *inode, struct file *filp)
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
struct mmap_info *info;
int ret;
int ret;

pr_info("read\n");
info = filp->private_data;
ret = min(len, (size_t)BUFFER_SIZE);
if (copy_to_user(buf, info->data, ret)) {
ret = -EFAULT;
ret = min(len, (size_t)BUFFER_SIZE);
if (copy_to_user(buf, info->data, ret)) {
ret = -EFAULT;
}
return ret;
}
Expand All @@ -96,11 +96,11 @@ static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff

pr_info("write\n");
info = filp->private_data;
if (copy_from_user(info->data, buf, min(len, (size_t)BUFFER_SIZE))) {
return -EFAULT;
} else {
return len;
}
if (copy_from_user(info->data, buf, min(len, (size_t)BUFFER_SIZE))) {
return -EFAULT;
} else {
return len;
}
}

static int release(struct inode *inode, struct file *filp)
Expand Down
58 changes: 29 additions & 29 deletions kernel_modules/netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,47 @@ static void callback(struct sk_buff *skb)
{
char readbuf[9];
size_t readbuflen;
int pid;
int res;
struct nlmsghdr *nlh;
struct sk_buff *skb_out;
int pid;
int res;
struct nlmsghdr *nlh;
struct sk_buff *skb_out;

nlh = (struct nlmsghdr *)skb->data;
pr_info("kernel received: %s\n", (char *)nlmsg_data(nlh));
nlh = (struct nlmsghdr *)skb->data;
pr_info("kernel received: %s\n", (char *)nlmsg_data(nlh));
if (sleep)
usleep_range(1000000, 1000001);
readbuflen = snprintf(readbuf, sizeof(readbuf), "%x", count);
count++;
pid = nlh->nlmsg_pid;
skb_out = nlmsg_new(readbuflen, 0);
if (!skb_out) {
pr_err("nlmsg_new\n");
return;
}
nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, readbuflen, 0);
NETLINK_CB(skb_out).dst_group = 0;
strncpy(nlmsg_data(nlh), readbuf, readbuflen);
res = nlmsg_unicast(nl_sk, skb_out, pid);
if (res < 0)
pr_info("nlmsg_unicast\n");
count++;
pid = nlh->nlmsg_pid;
skb_out = nlmsg_new(readbuflen, 0);
if (!skb_out) {
pr_err("nlmsg_new\n");
return;
}
nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, readbuflen, 0);
NETLINK_CB(skb_out).dst_group = 0;
strncpy(nlmsg_data(nlh), readbuf, readbuflen);
res = nlmsg_unicast(nl_sk, skb_out, pid);
if (res < 0)
pr_info("nlmsg_unicast\n");
}

static int myinit(void)
{
struct netlink_kernel_cfg cfg = {
.input = callback,
};
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
if (!nl_sk) {
pr_err("netlink_kernel_create\n");
return -10;
}
return 0;
struct netlink_kernel_cfg cfg = {
.input = callback,
};
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
if (!nl_sk) {
pr_err("netlink_kernel_create\n");
return -10;
}
return 0;
}

static void myexit(void)
{
netlink_kernel_release(nl_sk);
netlink_kernel_release(nl_sk);
}

module_init(myinit);
Expand Down
78 changes: 39 additions & 39 deletions kernel_modules/pci_min.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,75 +15,75 @@
#define QEMU_VENDOR_ID 0x1234

static struct pci_device_id id_table[] = {
{ PCI_DEVICE(QEMU_VENDOR_ID, EDU_DEVICE_ID), },
{ 0, }
{ PCI_DEVICE(QEMU_VENDOR_ID, EDU_DEVICE_ID), },
{ 0, }
};
MODULE_DEVICE_TABLE(pci, id_table);
static int major;
static struct pci_dev *pdev;
static void __iomem *mmio;
static struct file_operations fops = {
.owner = THIS_MODULE,
.owner = THIS_MODULE,
};

static irqreturn_t irq_handler(int irq, void *dev)
{
pr_info("irq_handler irq = %d dev = %d\n", irq, *(int *)dev);
iowrite32(0, mmio + 4);
return IRQ_HANDLED;
pr_info("irq_handler irq = %d dev = %d\n", irq, *(int *)dev);
iowrite32(0, mmio + 4);
return IRQ_HANDLED;
}

static int probe(struct pci_dev *dev, const struct pci_device_id *id)
{
pr_info("probe\n");
major = register_chrdev(0, CDEV_NAME, &fops);
pdev = dev;
if (pci_enable_device(dev) < 0) {
dev_err(&(pdev->dev), "pci_enable_device\n");
goto error;
}
if (pci_request_region(dev, BAR, "myregion0")) {
dev_err(&(pdev->dev), "pci_request_region\n");
goto error;
}
mmio = pci_iomap(pdev, BAR, pci_resource_len(pdev, BAR));
pr_info("dev->irq = %u\n", dev->irq);
if (request_irq(dev->irq, irq_handler, IRQF_SHARED, "pci_irq_handler0", &major) < 0) {
dev_err(&(dev->dev), "request_irq\n");
goto error;
}
iowrite32(0x12345678, mmio);
return 0;
pr_info("probe\n");
major = register_chrdev(0, CDEV_NAME, &fops);
pdev = dev;
if (pci_enable_device(dev) < 0) {
dev_err(&(pdev->dev), "pci_enable_device\n");
goto error;
}
if (pci_request_region(dev, BAR, "myregion0")) {
dev_err(&(pdev->dev), "pci_request_region\n");
goto error;
}
mmio = pci_iomap(pdev, BAR, pci_resource_len(pdev, BAR));
pr_info("dev->irq = %u\n", dev->irq);
if (request_irq(dev->irq, irq_handler, IRQF_SHARED, "pci_irq_handler0", &major) < 0) {
dev_err(&(dev->dev), "request_irq\n");
goto error;
}
iowrite32(0x12345678, mmio);
return 0;
error:
return 1;
return 1;
}

static void remove(struct pci_dev *dev)
{
pr_info("remove\n");
free_irq(dev->irq, &major);
pci_release_region(dev, BAR);
unregister_chrdev(major, CDEV_NAME);
pr_info("remove\n");
free_irq(dev->irq, &major);
pci_release_region(dev, BAR);
unregister_chrdev(major, CDEV_NAME);
}

static struct pci_driver pci_driver = {
.name = CDEV_NAME,
.id_table = id_table,
.probe = probe,
.remove = remove,
.name = CDEV_NAME,
.id_table = id_table,
.probe = probe,
.remove = remove,
};

static int myinit(void)
{
if (pci_register_driver(&pci_driver) < 0) {
return 1;
}
return 0;
if (pci_register_driver(&pci_driver) < 0) {
return 1;
}
return 0;
}

static void myexit(void)
{
pci_unregister_driver(&pci_driver);
pci_unregister_driver(&pci_driver);
}

module_init(myinit);
Expand Down
2 changes: 1 addition & 1 deletion kernel_modules/ring0.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static int myinit(void)
pr_info("cr2 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr2);
pr_info("cr3 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr3);
#endif
return 0;
return 0;
}

static void myexit(void) {}
Expand Down
20 changes: 10 additions & 10 deletions kernel_modules/seq_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ static int show(struct seq_file *s, void *v)
}

static struct seq_operations my_seq_ops = {
.next = next,
.show = show,
.start = start,
.stop = stop,
.next = next,
.show = show,
.start = start,
.stop = stop,
};

static int open(struct inode *inode, struct file *file)
{
pr_info("open\n");
return seq_open(file, &my_seq_ops);
return seq_open(file, &my_seq_ops);
}

static struct file_operations fops = {
.owner = THIS_MODULE,
.llseek = seq_lseek,
.open = open,
.read = seq_read,
.release = seq_release
.owner = THIS_MODULE,
.llseek = seq_lseek,
.open = open,
.read = seq_read,
.release = seq_release
};

static int myinit(void)
Expand Down

0 comments on commit 3b0a343

Please sign in to comment.