Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

connector: get test code working by default

The connector test code currently does not work out of the box. This is
because it uses a connector id that is above the registered limit. So
rather than force people to stumble through undocumented code wondering
why it isn't working, have the test code use one of the "private" ids by
default. While I'm in here, clean up the code (kernel and user app) so
that it's a bit more user friendly and verbose in significant things that
it does. Terse test code wastes people time as they simply enumerate it
with all the same kind of debug messages to get a better feel of what code
is running at any time.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Mike Frysinger and committed by
David S. Miller
37cf2b8d 41144ca3

+76 -22
+5
Documentation/connector/Makefile
··· 9 9 always := $(hostprogs-y) 10 10 11 11 HOSTCFLAGS_ucon.o += -I$(objtree)/usr/include 12 + 13 + all: modules 14 + 15 + modules clean: 16 + $(MAKE) -C ../.. SUBDIRS=$(PWD) $@
+18 -13
Documentation/connector/cn_test.c
··· 19 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 20 */ 21 21 22 + #define pr_fmt(fmt) "cn_test: " fmt 23 + 22 24 #include <linux/kernel.h> 23 25 #include <linux/module.h> 24 26 #include <linux/moduleparam.h> ··· 29 27 30 28 #include <linux/connector.h> 31 29 32 - static struct cb_id cn_test_id = { 0x123, 0x456 }; 30 + static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 }; 33 31 static char cn_test_name[] = "cn_test"; 34 32 static struct sock *nls; 35 33 static struct timer_list cn_test_timer; 36 34 37 - void cn_test_callback(struct cn_msg *msg) 35 + static void cn_test_callback(struct cn_msg *msg) 38 36 { 39 - printk("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n", 40 - __func__, jiffies, msg->id.idx, msg->id.val, 41 - msg->seq, msg->ack, msg->len, (char *)msg->data); 37 + pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n", 38 + __func__, jiffies, msg->id.idx, msg->id.val, 39 + msg->seq, msg->ack, msg->len, 40 + msg->len ? (char *)msg->data : ""); 42 41 } 43 42 44 43 /* ··· 64 61 65 62 skb = alloc_skb(size, GFP_ATOMIC); 66 63 if (!skb) { 67 - printk(KERN_ERR "Failed to allocate new skb with size=%u.\n", 68 - size); 69 - 64 + pr_err("failed to allocate new skb with size=%u\n", size); 70 65 return -ENOMEM; 71 66 } 72 67 ··· 113 112 //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC); 114 113 netlink_unicast(nls, skb, 0, 0); 115 114 116 - printk(KERN_INFO "Request was sent. Group=0x%x.\n", ctl->group); 115 + pr_info("request was sent: group=0x%x\n", ctl->group); 117 116 118 117 return 0; 119 118 120 119 nlmsg_failure: 121 - printk(KERN_ERR "Failed to send %u.%u\n", msg->seq, msg->ack); 120 + pr_err("failed to send %u.%u\n", msg->seq, msg->ack); 122 121 kfree_skb(skb); 123 122 return -EINVAL; 124 123 } ··· 129 128 { 130 129 struct cn_msg *m; 131 130 char data[32]; 131 + 132 + pr_debug("%s: timer fired with data %lu\n", __func__, __data); 132 133 133 134 m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC); 134 135 if (m) { ··· 151 148 152 149 cn_test_timer_counter++; 153 150 154 - mod_timer(&cn_test_timer, jiffies + HZ); 151 + mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000)); 155 152 } 156 153 157 154 static int cn_test_init(void) ··· 169 166 } 170 167 171 168 setup_timer(&cn_test_timer, cn_test_timer_func, 0); 172 - cn_test_timer.expires = jiffies + HZ; 173 - add_timer(&cn_test_timer); 169 + mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000)); 170 + 171 + pr_info("initialized with id={%u.%u}\n", 172 + cn_test_id.idx, cn_test_id.val); 174 173 175 174 return 0; 176 175
+53 -9
Documentation/connector/ucon.c
··· 30 30 31 31 #include <arpa/inet.h> 32 32 33 + #include <stdbool.h> 33 34 #include <stdio.h> 34 35 #include <stdlib.h> 35 36 #include <unistd.h> 36 37 #include <string.h> 37 38 #include <errno.h> 38 39 #include <time.h> 40 + #include <getopt.h> 39 41 40 42 #include <linux/connector.h> 41 43 42 44 #define DEBUG 43 45 #define NETLINK_CONNECTOR 11 46 + 47 + /* Hopefully your userspace connector.h matches this kernel */ 48 + #define CN_TEST_IDX CN_NETLINK_USERS + 3 49 + #define CN_TEST_VAL 0x456 44 50 45 51 #ifdef DEBUG 46 52 #define ulog(f, a...) fprintf(stdout, f, ##a) ··· 89 83 return err; 90 84 } 91 85 86 + static void usage(void) 87 + { 88 + printf( 89 + "Usage: ucon [options] [output file]\n" 90 + "\n" 91 + "\t-h\tthis help screen\n" 92 + "\t-s\tsend buffers to the test module\n" 93 + "\n" 94 + "The default behavior of ucon is to subscribe to the test module\n" 95 + "and wait for state messages. Any ones received are dumped to the\n" 96 + "specified output file (or stdout). The test module is assumed to\n" 97 + "have an id of {%u.%u}\n" 98 + "\n" 99 + "If you get no output, then verify the cn_test module id matches\n" 100 + "the expected id above.\n" 101 + , CN_TEST_IDX, CN_TEST_VAL 102 + ); 103 + } 104 + 92 105 int main(int argc, char *argv[]) 93 106 { 94 107 int s; ··· 119 94 FILE *out; 120 95 time_t tm; 121 96 struct pollfd pfd; 97 + bool send_msgs = false; 122 98 123 - if (argc < 2) 124 - out = stdout; 125 - else { 126 - out = fopen(argv[1], "a+"); 99 + while ((s = getopt(argc, argv, "hs")) != -1) { 100 + switch (s) { 101 + case 's': 102 + send_msgs = true; 103 + break; 104 + 105 + case 'h': 106 + usage(); 107 + return 0; 108 + 109 + default: 110 + /* getopt() outputs an error for us */ 111 + usage(); 112 + return 1; 113 + } 114 + } 115 + 116 + if (argc != optind) { 117 + out = fopen(argv[optind], "a+"); 127 118 if (!out) { 128 119 ulog("Unable to open %s for writing: %s\n", 129 120 argv[1], strerror(errno)); 130 121 out = stdout; 131 122 } 132 - } 123 + } else 124 + out = stdout; 133 125 134 126 memset(buf, 0, sizeof(buf)); 135 127 ··· 157 115 } 158 116 159 117 l_local.nl_family = AF_NETLINK; 160 - l_local.nl_groups = 0x123; /* bitmask of requested groups */ 118 + l_local.nl_groups = -1; /* bitmask of requested groups */ 161 119 l_local.nl_pid = 0; 120 + 121 + ulog("subscribing to %u.%u\n", CN_TEST_IDX, CN_TEST_VAL); 162 122 163 123 if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) { 164 124 perror("bind"); ··· 174 130 setsockopt(s, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &on, sizeof(on)); 175 131 } 176 132 #endif 177 - if (0) { 133 + if (send_msgs) { 178 134 int i, j; 179 135 180 136 memset(buf, 0, sizeof(buf)); 181 137 182 138 data = (struct cn_msg *)buf; 183 139 184 - data->id.idx = 0x123; 185 - data->id.val = 0x456; 140 + data->id.idx = CN_TEST_IDX; 141 + data->id.val = CN_TEST_VAL; 186 142 data->seq = seq++; 187 143 data->ack = 0; 188 144 data->len = 0;