ust: improve message parsing
[ust.git] / libustcomm / ustcomm.c
1 #define _GNU_SOURCE
2 #include <sys/types.h>
3 #include <signal.h>
4 #include <errno.h>
5 #include <sys/socket.h>
6 #include <sys/un.h>
7 #include <unistd.h>
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <execinfo.h>
13
14 #include "ustcomm.h"
15 #include "localerr.h"
16
17 #define UNIX_PATH_MAX 108
18 #define SOCK_DIR "/tmp/socks"
19 #define UST_SIGNAL SIGIO
20
21 #define MSG_MAX 1000
22
23 //static void bt(void)
24 //{
25 // void *buffer[100];
26 // int result;
27 //
28 // result = backtrace(&buffer, 100);
29 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
30 //}
31
32 static void signal_process(pid_t pid)
33 {
34 int result;
35
36 result = kill(pid, UST_SIGNAL);
37 if(result == -1) {
38 PERROR("kill");
39 return;
40 }
41
42 sleep(1);
43 }
44
45 int send_message_path(const char *path, const char *msg, char **reply, int signalpid)
46 {
47 int fd;
48 int result;
49 struct sockaddr_un addr;
50
51 result = fd = socket(PF_UNIX, SOCK_DGRAM, 0);
52 if(result == -1) {
53 PERROR("socket");
54 return -1;
55 }
56
57 addr.sun_family = AF_UNIX;
58
59 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path);
60 if(result >= UNIX_PATH_MAX) {
61 ERR("string overflow allocating socket name");
62 return -1;
63 }
64
65 if(signalpid >= 0)
66 signal_process(signalpid);
67
68 result = sendto(fd, msg, strlen(msg), 0, (struct sockaddr *)&addr, sizeof(addr));
69 if(result == -1) {
70 PERROR("sendto");
71 return -1;
72 }
73
74 if(!reply)
75 return 0;
76
77 *reply = (char *) malloc(MSG_MAX+1);
78 result = recvfrom(fd, *reply, MSG_MAX, 0, NULL, NULL);
79 if(result == -1) {
80 PERROR("recvfrom");
81 return -1;
82 }
83
84 (*reply)[result] = '\0';
85
86 return 0;
87 }
88
89 /* pid: the pid of the trace process that must receive the msg
90 msg: pointer to a null-terminated message to send
91 reply: location where to put the null-terminated string of the reply;
92 it must be free'd after usage
93 */
94
95 int send_message(pid_t pid, const char *msg, char **reply)
96 {
97 int result;
98 char path[UNIX_PATH_MAX];
99
100 result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
101 if(result >= UNIX_PATH_MAX) {
102 fprintf(stderr, "string overflow allocating socket name");
103 return -1;
104 }
105
106 send_message_path(path, msg, reply, pid);
107
108 return 0;
109 }
110
111 /* Called by an app to ask the consumer daemon to connect to it. */
112
113 int ustcomm_request_consumer(pid_t pid, const char *channel)
114 {
115 char path[UNIX_PATH_MAX];
116 int result;
117 char *msg;
118
119 result = snprintf(path, UNIX_PATH_MAX, "%s/ustd", SOCK_DIR);
120 if(result >= UNIX_PATH_MAX) {
121 fprintf(stderr, "string overflow allocating socket name");
122 return -1;
123 }
124
125 asprintf(&msg, "collect %d %s", pid, channel);
126
127 send_message_path(path, msg, NULL, -1);
128 free(msg);
129
130 return 0;
131 }
132
133 static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src)
134 {
135 int result;
136 size_t initial_addrlen,addrlen;
137
138 *msg = (char *) malloc(MSG_MAX+1);
139
140 if(src) {
141 initial_addrlen = addrlen = sizeof(src->addr);
142
143 result = recvfrom(fd, *msg, MSG_MAX, 0, &src->addr, &addrlen);
144 if(initial_addrlen != addrlen) {
145 ERR("recvfrom: unexpected address length");
146 return -1;
147 }
148 }
149 else {
150 result = recvfrom(fd, *msg, MSG_MAX, 0, NULL, NULL);
151 }
152
153 if(result == -1) {
154 PERROR("recvfrom");
155 return -1;
156 }
157
158 (*msg)[result] = '\0';
159
160 DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg));
161
162 return 0;
163 }
164
165 int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src)
166 {
167 return recv_message_fd(ustd->fd, msg, src);
168 }
169
170 int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src)
171 {
172 return recv_message_fd(app->fd, msg, src);
173 }
174
175 static int init_named_socket(char *name, char **path_out)
176 {
177 int result;
178 int fd;
179
180 struct sockaddr_un addr;
181
182 result = fd = socket(PF_UNIX, SOCK_DGRAM, 0);
183 if(result == -1) {
184 PERROR("socket");
185 return -1;
186 }
187
188 addr.sun_family = AF_UNIX;
189
190 strncpy(addr.sun_path, name, UNIX_PATH_MAX);
191 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
192
193 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
194 if(result == -1) {
195 PERROR("bind");
196 goto close_sock;
197 }
198
199 if(path_out) {
200 *path_out = "";
201 *path_out = strdupa(addr.sun_path);
202 }
203
204 return fd;
205
206 close_sock:
207 close(fd);
208
209 return -1;
210 }
211
212 int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle)
213 {
214 int result;
215 char *name;
216
217 result = asprintf(&name, "%s/%d", SOCK_DIR, (int)pid);
218 if(result >= UNIX_PATH_MAX) {
219 ERR("string overflow allocating socket name");
220 return -1;
221 }
222
223 handle->fd = init_named_socket(name, &(handle->socketpath));
224 if(handle->fd < 0) {
225 goto free_name;
226 }
227 free(name);
228
229 return 0;
230
231 free_name:
232 free(name);
233 return -1;
234 }
235
236 int ustcomm_init_ustd(struct ustcomm_ustd *handle)
237 {
238 int result;
239 char *name;
240
241 result = asprintf(&name, "%s/%s", SOCK_DIR, "ustd");
242 if(result >= UNIX_PATH_MAX) {
243 ERR("string overflow allocating socket name");
244 return -1;
245 }
246
247 handle->fd = init_named_socket(name, &handle->socketpath);
248 if(handle->fd < 0)
249 return handle->fd;
250 free(name);
251
252 return 0;
253 }
254
255 char *find_tok(const char *str)
256 {
257 while(*str == ' ') {
258 str++;
259
260 if(*str == 0)
261 return NULL;
262 }
263
264 return str;
265 }
266
267 static char *find_sep(char *str)
268 {
269 while(*str != ' ') {
270 str++;
271
272 if(*str == 0)
273 break;
274 }
275
276 return str;
277 }
278
279 int nth_token_is(char *str, char *token, int tok_no)
280 {
281 int i;
282 char *start;
283 char *end;
284
285 for(i=0; i<=tok_no; i++) {
286 str = find_tok(str);
287 if(str == NULL)
288 return -1;
289
290 start = str;
291
292 str = find_sep(str);
293 if(str == NULL)
294 return -1;
295
296 end = str;
297 }
298
299 if(end-start != strlen(token))
300 return 0;
301
302 if(strncmp(start, token, end-start))
303 return 0;
304
305 return 1;
306 }
307
308 char *nth_token(char *str, int tok_no)
309 {
310 static char *retval = NULL;
311 int i;
312 char *start;
313 char *end;
314
315 for(i=0; i<=tok_no; i++) {
316 str = find_tok(str);
317 if(str == NULL)
318 return NULL;
319
320 start = str;
321
322 str = find_sep(str);
323 if(str == NULL)
324 return NULL;
325
326 end = str;
327 }
328
329 if(retval) {
330 free(retval);
331 retval = NULL;
332 }
333
334 retval = strndupa(start, end-start);
335
336 return retval;
337 }
338
This page took 0.035437 seconds and 4 git commands to generate.