Blame view

Documentation/connector/ucon.c 5.11 KB
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
1
2
3
  /*
   * 	ucon.c
   *
acb9c1b2f   Evgeniy Polyakov   connector: mainta...
4
   * Copyright (c) 2004+ Evgeniy Polyakov <zbr@ioremap.net>
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   *
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   */
  
  #include <asm/types.h>
  
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <sys/poll.h>
  
  #include <linux/netlink.h>
  #include <linux/rtnetlink.h>
  
  #include <arpa/inet.h>
37cf2b8d1   Mike Frysinger   connector: get te...
32
  #include <stdbool.h>
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
33
34
35
36
37
38
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <string.h>
  #include <errno.h>
  #include <time.h>
37cf2b8d1   Mike Frysinger   connector: get te...
39
  #include <getopt.h>
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
40
41
42
43
44
  
  #include <linux/connector.h>
  
  #define DEBUG
  #define NETLINK_CONNECTOR 	11
37cf2b8d1   Mike Frysinger   connector: get te...
45
46
47
  /* Hopefully your userspace connector.h matches this kernel */
  #define CN_TEST_IDX		CN_NETLINK_USERS + 3
  #define CN_TEST_VAL		0x456
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  #ifdef DEBUG
  #define ulog(f, a...) fprintf(stdout, f, ##a)
  #else
  #define ulog(f, a...) do {} while (0)
  #endif
  
  static int need_exit;
  static __u32 seq;
  
  static int netlink_send(int s, struct cn_msg *msg)
  {
  	struct nlmsghdr *nlh;
  	unsigned int size;
  	int err;
  	char buf[128];
  	struct cn_msg *m;
  
  	size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
  
  	nlh = (struct nlmsghdr *)buf;
  	nlh->nlmsg_seq = seq++;
  	nlh->nlmsg_pid = getpid();
  	nlh->nlmsg_type = NLMSG_DONE;
  	nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
  	nlh->nlmsg_flags = 0;
  
  	m = NLMSG_DATA(nlh);
  #if 0
  	ulog("%s: [%08x.%08x] len=%u, seq=%u, ack=%u.
  ",
  	       __func__, msg->id.idx, msg->id.val, msg->len, msg->seq, msg->ack);
  #endif
  	memcpy(m, msg, sizeof(*m) + msg->len);
  
  	err = send(s, nlh, size, 0);
  	if (err == -1)
  		ulog("Failed to send: %s [%d].
  ",
  			strerror(errno), errno);
  
  	return err;
  }
37cf2b8d1   Mike Frysinger   connector: get te...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  static void usage(void)
  {
  	printf(
  		"Usage: ucon [options] [output file]
  "
  		"
  "
  		"\t-h\tthis help screen
  "
  		"\t-s\tsend buffers to the test module
  "
  		"
  "
  		"The default behavior of ucon is to subscribe to the test module
  "
  		"and wait for state messages.  Any ones received are dumped to the
  "
  		"specified output file (or stdout).  The test module is assumed to
  "
  		"have an id of {%u.%u}
  "
  		"
  "
  		"If you get no output, then verify the cn_test module id matches
  "
  		"the expected id above.
  "
  		, CN_TEST_IDX, CN_TEST_VAL
  	);
  }
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
120
121
122
123
124
125
126
127
128
129
130
  int main(int argc, char *argv[])
  {
  	int s;
  	char buf[1024];
  	int len;
  	struct nlmsghdr *reply;
  	struct sockaddr_nl l_local;
  	struct cn_msg *data;
  	FILE *out;
  	time_t tm;
  	struct pollfd pfd;
37cf2b8d1   Mike Frysinger   connector: get te...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  	bool send_msgs = false;
  
  	while ((s = getopt(argc, argv, "hs")) != -1) {
  		switch (s) {
  		case 's':
  			send_msgs = true;
  			break;
  
  		case 'h':
  			usage();
  			return 0;
  
  		default:
  			/* getopt() outputs an error for us */
  			usage();
  			return 1;
  		}
  	}
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
149

37cf2b8d1   Mike Frysinger   connector: get te...
150
151
  	if (argc != optind) {
  		out = fopen(argv[optind], "a+");
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
152
153
154
155
156
157
  		if (!out) {
  			ulog("Unable to open %s for writing: %s
  ",
  				argv[1], strerror(errno));
  			out = stdout;
  		}
37cf2b8d1   Mike Frysinger   connector: get te...
158
159
  	} else
  		out = stdout;
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
160
161
162
163
164
165
166
167
168
169
  
  	memset(buf, 0, sizeof(buf));
  
  	s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  	if (s == -1) {
  		perror("socket");
  		return -1;
  	}
  
  	l_local.nl_family = AF_NETLINK;
37cf2b8d1   Mike Frysinger   connector: get te...
170
  	l_local.nl_groups = -1; /* bitmask of requested groups */
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
171
  	l_local.nl_pid = 0;
37cf2b8d1   Mike Frysinger   connector: get te...
172
173
  	ulog("subscribing to %u.%u
  ", CN_TEST_IDX, CN_TEST_VAL);
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
174
175
176
177
178
179
180
181
182
183
184
185
  	if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
  		perror("bind");
  		close(s);
  		return -1;
  	}
  
  #if 0
  	{
  		int on = 0x57; /* Additional group number */
  		setsockopt(s, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &on, sizeof(on));
  	}
  #endif
37cf2b8d1   Mike Frysinger   connector: get te...
186
  	if (send_msgs) {
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
187
188
189
190
191
  		int i, j;
  
  		memset(buf, 0, sizeof(buf));
  
  		data = (struct cn_msg *)buf;
37cf2b8d1   Mike Frysinger   connector: get te...
192
193
  		data->id.idx = CN_TEST_IDX;
  		data->id.val = CN_TEST_VAL;
897522ea1   Evgeniy Polyakov   [CONNECTOR]: Add ...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  		data->seq = seq++;
  		data->ack = 0;
  		data->len = 0;
  
  		for (j=0; j<10; ++j) {
  			for (i=0; i<1000; ++i) {
  				len = netlink_send(s, data);
  			}
  
  			ulog("%d messages have been sent to %08x.%08x.
  ", i, data->id.idx, data->id.val);
  		}
  
  		return 0;
  	}
  
  
  	pfd.fd = s;
  
  	while (!need_exit) {
  		pfd.events = POLLIN;
  		pfd.revents = 0;
  		switch (poll(&pfd, 1, -1)) {
  			case 0:
  				need_exit = 1;
  				break;
  			case -1:
  				if (errno != EINTR) {
  					need_exit = 1;
  					break;
  				}
  				continue;
  		}
  		if (need_exit)
  			break;
  
  		memset(buf, 0, sizeof(buf));
  		len = recv(s, buf, sizeof(buf), 0);
  		if (len == -1) {
  			perror("recv buf");
  			close(s);
  			return -1;
  		}
  		reply = (struct nlmsghdr *)buf;
  
  		switch (reply->nlmsg_type) {
  		case NLMSG_ERROR:
  			fprintf(out, "Error message received.
  ");
  			fflush(out);
  			break;
  		case NLMSG_DONE:
  			data = (struct cn_msg *)NLMSG_DATA(reply);
  
  			time(&tm);
  			fprintf(out, "%.24s : [%x.%x] [%08u.%08u].
  ",
  				ctime(&tm), data->id.idx, data->id.val, data->seq, data->ack);
  			fflush(out);
  			break;
  		default:
  			break;
  		}
  	}
  
  	close(s);
  	return 0;
  }