Path: blob/master/Documentation/connector/cn_test.c
10821 views
/*1* cn_test.c2*3* 2004+ Copyright (c) Evgeniy Polyakov <[email protected]>4* All rights reserved.5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (at your option) any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA19*/2021#define pr_fmt(fmt) "cn_test: " fmt2223#include <linux/kernel.h>24#include <linux/module.h>25#include <linux/moduleparam.h>26#include <linux/skbuff.h>27#include <linux/slab.h>28#include <linux/timer.h>2930#include <linux/connector.h>3132static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };33static char cn_test_name[] = "cn_test";34static struct sock *nls;35static struct timer_list cn_test_timer;3637static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)38{39pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",40__func__, jiffies, msg->id.idx, msg->id.val,41msg->seq, msg->ack, msg->len,42msg->len ? (char *)msg->data : "");43}4445/*46* Do not remove this function even if no one is using it as47* this is an example of how to get notifications about new48* connector user registration49*/50#if 051static int cn_test_want_notify(void)52{53struct cn_ctl_msg *ctl;54struct cn_notify_req *req;55struct cn_msg *msg = NULL;56int size, size0;57struct sk_buff *skb;58struct nlmsghdr *nlh;59u32 group = 1;6061size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);6263size = NLMSG_SPACE(size0);6465skb = alloc_skb(size, GFP_ATOMIC);66if (!skb) {67pr_err("failed to allocate new skb with size=%u\n", size);68return -ENOMEM;69}7071nlh = NLMSG_PUT(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh));7273msg = (struct cn_msg *)NLMSG_DATA(nlh);7475memset(msg, 0, size0);7677msg->id.idx = -1;78msg->id.val = -1;79msg->seq = 0x123;80msg->ack = 0x345;81msg->len = size0 - sizeof(*msg);8283ctl = (struct cn_ctl_msg *)(msg + 1);8485ctl->idx_notify_num = 1;86ctl->val_notify_num = 2;87ctl->group = group;88ctl->len = msg->len - sizeof(*ctl);8990req = (struct cn_notify_req *)(ctl + 1);9192/*93* Idx.94*/95req->first = cn_test_id.idx;96req->range = 10;9798/*99* Val 0.100*/101req++;102req->first = cn_test_id.val;103req->range = 10;104105/*106* Val 1.107*/108req++;109req->first = cn_test_id.val + 20;110req->range = 10;111112NETLINK_CB(skb).dst_group = ctl->group;113//netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);114netlink_unicast(nls, skb, 0, 0);115116pr_info("request was sent: group=0x%x\n", ctl->group);117118return 0;119120nlmsg_failure:121pr_err("failed to send %u.%u\n", msg->seq, msg->ack);122kfree_skb(skb);123return -EINVAL;124}125#endif126127static u32 cn_test_timer_counter;128static void cn_test_timer_func(unsigned long __data)129{130struct cn_msg *m;131char data[32];132133pr_debug("%s: timer fired with data %lu\n", __func__, __data);134135m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);136if (m) {137138memcpy(&m->id, &cn_test_id, sizeof(m->id));139m->seq = cn_test_timer_counter;140m->len = sizeof(data);141142m->len =143scnprintf(data, sizeof(data), "counter = %u",144cn_test_timer_counter) + 1;145146memcpy(m + 1, data, m->len);147148cn_netlink_send(m, 0, GFP_ATOMIC);149kfree(m);150}151152cn_test_timer_counter++;153154mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));155}156157static int cn_test_init(void)158{159int err;160161err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);162if (err)163goto err_out;164cn_test_id.val++;165err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);166if (err) {167cn_del_callback(&cn_test_id);168goto err_out;169}170171setup_timer(&cn_test_timer, cn_test_timer_func, 0);172mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));173174pr_info("initialized with id={%u.%u}\n",175cn_test_id.idx, cn_test_id.val);176177return 0;178179err_out:180if (nls && nls->sk_socket)181sock_release(nls->sk_socket);182183return err;184}185186static void cn_test_fini(void)187{188del_timer_sync(&cn_test_timer);189cn_del_callback(&cn_test_id);190cn_test_id.val--;191cn_del_callback(&cn_test_id);192if (nls && nls->sk_socket)193sock_release(nls->sk_socket);194}195196module_init(cn_test_init);197module_exit(cn_test_fini);198199MODULE_LICENSE("GPL");200MODULE_AUTHOR("Evgeniy Polyakov <[email protected]>");201MODULE_DESCRIPTION("Connector's test module");202203204