ucon.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * ucon.c
  3. *
  4. * Copyright (c) 2004+ Evgeniy Polyakov <zbr@ioremap.net>
  5. *
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #include <asm/types.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <sys/poll.h>
  25. #include <linux/netlink.h>
  26. #include <linux/rtnetlink.h>
  27. #include <arpa/inet.h>
  28. #include <stdbool.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <time.h>
  35. #include <getopt.h>
  36. #include <linux/connector.h>
  37. #define DEBUG
  38. #define NETLINK_CONNECTOR 11
  39. /* Hopefully your userspace connector.h matches this kernel */
  40. #define CN_TEST_IDX CN_NETLINK_USERS + 3
  41. #define CN_TEST_VAL 0x456
  42. #ifdef DEBUG
  43. #define ulog(f, a...) fprintf(stdout, f, ##a)
  44. #else
  45. #define ulog(f, a...) do {} while (0)
  46. #endif
  47. static int need_exit;
  48. static __u32 seq;
  49. static int netlink_send(int s, struct cn_msg *msg)
  50. {
  51. struct nlmsghdr *nlh;
  52. unsigned int size;
  53. int err;
  54. char buf[128];
  55. struct cn_msg *m;
  56. size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
  57. nlh = (struct nlmsghdr *)buf;
  58. nlh->nlmsg_seq = seq++;
  59. nlh->nlmsg_pid = getpid();
  60. nlh->nlmsg_type = NLMSG_DONE;
  61. nlh->nlmsg_len = size;
  62. nlh->nlmsg_flags = 0;
  63. m = NLMSG_DATA(nlh);
  64. #if 0
  65. ulog("%s: [%08x.%08x] len=%u, seq=%u, ack=%u.\n",
  66. __func__, msg->id.idx, msg->id.val, msg->len, msg->seq, msg->ack);
  67. #endif
  68. memcpy(m, msg, sizeof(*m) + msg->len);
  69. err = send(s, nlh, size, 0);
  70. if (err == -1)
  71. ulog("Failed to send: %s [%d].\n",
  72. strerror(errno), errno);
  73. return err;
  74. }
  75. static void usage(void)
  76. {
  77. printf(
  78. "Usage: ucon [options] [output file]\n"
  79. "\n"
  80. "\t-h\tthis help screen\n"
  81. "\t-s\tsend buffers to the test module\n"
  82. "\n"
  83. "The default behavior of ucon is to subscribe to the test module\n"
  84. "and wait for state messages. Any ones received are dumped to the\n"
  85. "specified output file (or stdout). The test module is assumed to\n"
  86. "have an id of {%u.%u}\n"
  87. "\n"
  88. "If you get no output, then verify the cn_test module id matches\n"
  89. "the expected id above.\n"
  90. , CN_TEST_IDX, CN_TEST_VAL
  91. );
  92. }
  93. int main(int argc, char *argv[])
  94. {
  95. int s;
  96. char buf[1024];
  97. int len;
  98. struct nlmsghdr *reply;
  99. struct sockaddr_nl l_local;
  100. struct cn_msg *data;
  101. FILE *out;
  102. time_t tm;
  103. struct pollfd pfd;
  104. bool send_msgs = false;
  105. while ((s = getopt(argc, argv, "hs")) != -1) {
  106. switch (s) {
  107. case 's':
  108. send_msgs = true;
  109. break;
  110. case 'h':
  111. usage();
  112. return 0;
  113. default:
  114. /* getopt() outputs an error for us */
  115. usage();
  116. return 1;
  117. }
  118. }
  119. if (argc != optind) {
  120. out = fopen(argv[optind], "a+");
  121. if (!out) {
  122. ulog("Unable to open %s for writing: %s\n",
  123. argv[1], strerror(errno));
  124. out = stdout;
  125. }
  126. } else
  127. out = stdout;
  128. memset(buf, 0, sizeof(buf));
  129. s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  130. if (s == -1) {
  131. perror("socket");
  132. return -1;
  133. }
  134. l_local.nl_family = AF_NETLINK;
  135. l_local.nl_groups = -1; /* bitmask of requested groups */
  136. l_local.nl_pid = 0;
  137. ulog("subscribing to %u.%u\n", CN_TEST_IDX, CN_TEST_VAL);
  138. if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
  139. perror("bind");
  140. close(s);
  141. return -1;
  142. }
  143. #if 0
  144. {
  145. int on = 0x57; /* Additional group number */
  146. setsockopt(s, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &on, sizeof(on));
  147. }
  148. #endif
  149. if (send_msgs) {
  150. int i, j;
  151. memset(buf, 0, sizeof(buf));
  152. data = (struct cn_msg *)buf;
  153. data->id.idx = CN_TEST_IDX;
  154. data->id.val = CN_TEST_VAL;
  155. data->seq = seq++;
  156. data->ack = 0;
  157. data->len = 0;
  158. for (j=0; j<10; ++j) {
  159. for (i=0; i<1000; ++i) {
  160. len = netlink_send(s, data);
  161. }
  162. ulog("%d messages have been sent to %08x.%08x.\n", i, data->id.idx, data->id.val);
  163. }
  164. return 0;
  165. }
  166. pfd.fd = s;
  167. while (!need_exit) {
  168. pfd.events = POLLIN;
  169. pfd.revents = 0;
  170. switch (poll(&pfd, 1, -1)) {
  171. case 0:
  172. need_exit = 1;
  173. break;
  174. case -1:
  175. if (errno != EINTR) {
  176. need_exit = 1;
  177. break;
  178. }
  179. continue;
  180. }
  181. if (need_exit)
  182. break;
  183. memset(buf, 0, sizeof(buf));
  184. len = recv(s, buf, sizeof(buf), 0);
  185. if (len == -1) {
  186. perror("recv buf");
  187. close(s);
  188. return -1;
  189. }
  190. reply = (struct nlmsghdr *)buf;
  191. switch (reply->nlmsg_type) {
  192. case NLMSG_ERROR:
  193. fprintf(out, "Error message received.\n");
  194. fflush(out);
  195. break;
  196. case NLMSG_DONE:
  197. data = (struct cn_msg *)NLMSG_DATA(reply);
  198. time(&tm);
  199. fprintf(out, "%.24s : [%x.%x] [%08u.%08u].\n",
  200. ctime(&tm), data->id.idx, data->id.val, data->seq, data->ack);
  201. fflush(out);
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. close(s);
  208. return 0;
  209. }