4 * Copyright (c) 2004+ Evgeniy Polyakov <zbr@ioremap.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <asm/types.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <linux/netlink.h>
29 #include <linux/rtnetlink.h>
31 #include <arpa/inet.h>
42 #include <linux/connector.h>
45 #define NETLINK_CONNECTOR 11
47 /* Hopefully your userspace connector.h matches this kernel */
48 #define CN_TEST_IDX CN_NETLINK_USERS + 3
49 #define CN_TEST_VAL 0x456
52 #define ulog(f, a...) fprintf(stdout, f, ##a)
54 #define ulog(f, a...) do {} while (0)
60 static int netlink_send(int s
, struct cn_msg
*msg
)
68 size
= NLMSG_SPACE(sizeof(struct cn_msg
) + msg
->len
);
70 nlh
= (struct nlmsghdr
*)buf
;
71 nlh
->nlmsg_seq
= seq
++;
72 nlh
->nlmsg_pid
= getpid();
73 nlh
->nlmsg_type
= NLMSG_DONE
;
74 nlh
->nlmsg_len
= NLMSG_LENGTH(size
- sizeof(*nlh
));
78 memcpy(m
, msg
, sizeof(*m
) + msg
->len
);
80 err
= send(s
, nlh
, size
, 0);
82 ulog("Failed to send: %s [%d].\n",
83 strerror(errno
), errno
);
88 static void usage(void)
91 "Usage: ucon [options] [output file]\n"
93 "\t-h\tthis help screen\n"
94 "\t-s\tsend buffers to the test module\n"
96 "The default behavior of ucon is to subscribe to the test module\n"
97 "and wait for state messages. Any ones received are dumped to the\n"
98 "specified output file (or stdout). The test module is assumed to\n"
99 "have an id of {%u.%u}\n"
101 "If you get no output, then verify the cn_test module id matches\n"
102 "the expected id above.\n"
103 , CN_TEST_IDX
, CN_TEST_VAL
107 int main(int argc
, char *argv
[])
112 struct nlmsghdr
*reply
;
113 struct sockaddr_nl l_local
;
118 bool send_msgs
= false;
120 while ((s
= getopt(argc
, argv
, "hs")) != -1) {
131 /* getopt() outputs an error for us */
137 if (argc
!= optind
) {
138 out
= fopen(argv
[optind
], "a+");
140 ulog("Unable to open %s for writing: %s\n",
141 argv
[1], strerror(errno
));
147 memset(buf
, 0, sizeof(buf
));
149 s
= socket(PF_NETLINK
, SOCK_DGRAM
, NETLINK_CONNECTOR
);
155 l_local
.nl_family
= AF_NETLINK
;
156 l_local
.nl_groups
= -1; /* bitmask of requested groups */
159 ulog("subscribing to %u.%u\n", CN_TEST_IDX
, CN_TEST_VAL
);
161 if (bind(s
, (struct sockaddr
*)&l_local
, sizeof(struct sockaddr_nl
)) == -1) {
170 memset(buf
, 0, sizeof(buf
));
172 data
= (struct cn_msg
*)buf
;
174 data
->id
.idx
= CN_TEST_IDX
;
175 data
->id
.val
= CN_TEST_VAL
;
180 for (j
=0; j
<10; ++j
) {
181 for (i
=0; i
<1000; ++i
) {
182 len
= netlink_send(s
, data
);
185 ulog("%d messages have been sent to %08x.%08x.\n", i
, data
->id
.idx
, data
->id
.val
);
197 switch (poll(&pfd
, 1, -1)) {
202 if (errno
!= EINTR
) {
211 memset(buf
, 0, sizeof(buf
));
212 len
= recv(s
, buf
, sizeof(buf
), 0);
218 reply
= (struct nlmsghdr
*)buf
;
220 switch (reply
->nlmsg_type
) {
222 fprintf(out
, "Error message received.\n");
226 data
= (struct cn_msg
*)NLMSG_DATA(reply
);
229 fprintf(out
, "%.24s : [%x.%x] [%08u.%08u].\n",
230 ctime(&tm
), data
->id
.idx
, data
->id
.val
, data
->seq
, data
->ack
);