Introduce old redir program
[lcapit-junk-code.git] / linux-kernel / netlink / tiny-to-kernel / netlink-tiny-tk.c
blob474c5bd92ffc3e3ba433927d1e8cea7d94785db7
1 /*
2 * netlink-tiny-tk: Wait for a netlink message from user-space, when it
3 * arraives print the process ID of the process which sent the message.
4 *
5 * Luiz Fernando N. Capitulino
6 * <lcapitulino@gmail.com>
7 */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/mutex.h>
12 #include <linux/net.h>
13 #include <linux/skbuff.h>
14 #include <linux/netlink.h>
15 #include <net/sock.h>
17 #define NETLINK_TINY_TK 20 // always check whether this is safe
18 #define LOG_PREFIX "KNETLINK_TINY_TK: "
20 static struct sock *nl;
21 static DEFINE_MUTEX(netlink_tiny_tk_mutex);
23 static void netlink_tiny_tk_input(struct sk_buff *skb)
25 struct nlmsghdr *nlh;
27 mutex_lock(&netlink_tiny_tk_mutex);
29 if (skb->len < sizeof(*nlh))
30 return;
32 nlh = nlmsg_hdr(skb);
33 printk(LOG_PREFIX "message from: %d\n", nlh->nlmsg_pid);
35 mutex_unlock(&netlink_tiny_tk_mutex);
38 static int __init netlink_tiny_tk_init(void)
40 nl = netlink_kernel_create(&init_net, NETLINK_TINY_TK, 10,
41 netlink_tiny_tk_input, NULL, THIS_MODULE);
42 if (!nl) {
43 printk(KERN_ERR LOG_PREFIX "cannot create socket\n");
44 return -EINVAL;
47 return 0;
50 static void __exit netlink_tiny_tk_exit(void)
52 sock_release(nl->sk_socket);
55 module_init(netlink_tiny_tk_init);
56 module_exit(netlink_tiny_tk_exit);
58 MODULE_LICENSE("GPL");
59 MODULE_AUTHOR("Luiz Capitulino <lcapitulino@gmail.com>");
60 MODULE_DESCRIPTION("Netlink (to kernel) example module");