start adding LGPL headers
[ust.git] / libustcomm / ustcomm.c
1 /* Copyright (C) 2009 Pierre-Marc Fournier
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #define _GNU_SOURCE
19 #include <sys/types.h>
20 #include <signal.h>
21 #include <errno.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <unistd.h>
25 #include <poll.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <execinfo.h>
31
32 #include "ustcomm.h"
33 #include "localerr.h"
34
35 #define UNIX_PATH_MAX 108
36 #define SOCK_DIR "/tmp/socks"
37 #define UST_SIGNAL SIGIO
38
39 #define MSG_MAX 1000
40
41 /* FIXME: ustcomm blocks on message sending, which might be problematic in
42 * some cases. Fix the poll() usage so sends are buffered until they don't
43 * block.
44 */
45
46 //static void bt(void)
47 //{
48 // void *buffer[100];
49 // int result;
50 //
51 // result = backtrace(&buffer, 100);
52 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
53 //}
54
55 char *strdup_malloc(const char *s)
56 {
57 char *retval;
58
59 if(s == NULL)
60 return NULL;
61
62 retval = (char *) malloc(strlen(s)+1);
63
64 strcpy(retval, s);
65
66 return retval;
67 }
68
69 static int signal_process(pid_t pid)
70 {
71 int result;
72
73 result = kill(pid, UST_SIGNAL);
74 if(result == -1) {
75 PERROR("kill");
76 return -1;
77 }
78
79 /* FIXME: should wait in a better way */
80 //sleep(1);
81
82 return 0;
83 }
84
85 static int send_message_fd(int fd, const char *msg)
86 {
87 int result;
88
89 result = send(fd, msg, strlen(msg), 0);
90 if(result == -1) {
91 PERROR("send");
92 return -1;
93 }
94 else if(result == 0) {
95 return 0;
96 }
97
98 return 1;
99
100 // *reply = (char *) malloc(MSG_MAX+1);
101 // result = recv(fd, *reply, MSG_MAX, 0);
102 // if(result == -1) {
103 // PERROR("recv");
104 // return -1;
105 // }
106 // else if(result == 0) {
107 // return 0;
108 // }
109 //
110 // (*reply)[result] = '\0';
111 //
112 // return 1;
113 }
114
115 static int send_message_path(const char *path, const char *msg, int signalpid)
116 {
117 int fd;
118 int result;
119 struct sockaddr_un addr;
120
121 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
122 if(result == -1) {
123 PERROR("socket");
124 return -1;
125 }
126
127 addr.sun_family = AF_UNIX;
128
129 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path);
130 if(result >= UNIX_PATH_MAX) {
131 ERR("string overflow allocating socket name");
132 return -1;
133 }
134
135 if(signalpid >= 0) {
136 result = signal_process(signalpid);
137 if(result == -1) {
138 ERR("could not signal process");
139 return -1;
140 }
141 }
142
143 result = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
144 if(result == -1) {
145 PERROR("connect");
146 return -1;
147 }
148
149 return send_message_fd(fd, msg);
150 }
151
152 ///* pid: the pid of the trace process that must receive the msg
153 // msg: pointer to a null-terminated message to send
154 // reply: location where to put the null-terminated string of the reply;
155 // it must be free'd after usage
156 // */
157 //
158 //int send_message_pid(pid_t pid, const char *msg, char **reply)
159 //{
160 // int result;
161 // char path[UNIX_PATH_MAX];
162 //
163 // result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
164 // if(result >= UNIX_PATH_MAX) {
165 // fprintf(stderr, "string overflow allocating socket name");
166 // return -1;
167 // }
168 //
169 // send_message_path(path, msg, reply, pid);
170 //
171 // return 0;
172 //}
173
174 /* Called by an app to ask the consumer daemon to connect to it. */
175
176 int ustcomm_request_consumer(pid_t pid, const char *channel)
177 {
178 char path[UNIX_PATH_MAX];
179 int result;
180 char *msg;
181
182 result = snprintf(path, UNIX_PATH_MAX, "%s/ustd", SOCK_DIR);
183 if(result >= UNIX_PATH_MAX) {
184 fprintf(stderr, "string overflow allocating socket name");
185 return -1;
186 }
187
188 asprintf(&msg, "collect %d %s", pid, channel);
189
190 send_message_path(path, msg, -1);
191 free(msg);
192
193 return 0;
194 }
195
196 /* returns 1 to indicate a message was received
197 * returns 0 to indicate no message was received (cannot happen)
198 * returns -1 to indicate an error
199 */
200
201 static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src)
202 {
203 int result;
204
205 *msg = (char *) malloc(MSG_MAX+1);
206
207 result = recv(fd, *msg, MSG_MAX, 0);
208 if(result == -1) {
209 PERROR("recv");
210 return -1;
211 }
212
213 (*msg)[result] = '\0';
214
215 DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg));
216
217 if(src)
218 src->fd = fd;
219
220 return 1;
221 }
222
223 int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_source *src)
224 {
225 int result;
226
227 result = send_message_fd(src->fd, msg);
228 if(result < 0) {
229 ERR("error in send_message_fd");
230 return -1;
231 }
232
233 return 0;
234 }
235
236 /* @timeout: max blocking time in milliseconds, -1 means infinity
237 *
238 * returns 1 to indicate a message was received
239 * returns 0 to indicate no message was received
240 * returns -1 to indicate an error
241 */
242
243 int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src, int timeout)
244 {
245 struct pollfd *fds;
246 struct ustcomm_connection *conn;
247 int result;
248 int retval;
249
250 for(;;) {
251 int idx = 0;
252 int n_fds = 1;
253
254 list_for_each_entry(conn, &server->connections, list) {
255 n_fds++;
256 }
257
258 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
259 if(fds == NULL) {
260 ERR("malloc returned NULL");
261 return -1;
262 }
263
264 /* special idx 0 is for listening socket */
265 fds[idx].fd = server->listen_fd;
266 fds[idx].events = POLLIN;
267 idx++;
268
269 list_for_each_entry(conn, &server->connections, list) {
270 fds[idx].fd = conn->fd;
271 fds[idx].events = POLLIN;
272 idx++;
273 }
274
275 result = poll(fds, n_fds, timeout);
276 if(result == -1) {
277 PERROR("poll");
278 return -1;
279 }
280
281 if(result == 0)
282 return 0;
283
284 if(fds[0].revents) {
285 struct ustcomm_connection *newconn;
286 int newfd;
287
288 result = newfd = accept(server->listen_fd, NULL, NULL);
289 if(result == -1) {
290 PERROR("accept");
291 return -1;
292 }
293
294 newconn = (struct ustcomm_connection *) malloc(sizeof(struct ustcomm_connection));
295 if(newconn == NULL) {
296 ERR("malloc returned NULL");
297 return -1;
298 }
299
300 newconn->fd = newfd;
301
302 list_add(&newconn->list, &server->connections);
303 }
304
305 for(idx=1; idx<n_fds; idx++) {
306 if(fds[idx].revents) {
307 retval = recv_message_fd(fds[idx].fd, msg, src);
308 if(**msg == 0) {
309 /* connection finished */
310 close(fds[idx].fd);
311
312 list_for_each_entry(conn, &server->connections, list) {
313 if(conn->fd == fds[idx].fd) {
314 list_del(&conn->list);
315 break;
316 }
317 }
318 }
319 else {
320 goto free_fds_return;
321 }
322 }
323 }
324
325 free(fds);
326 }
327
328 free_fds_return:
329 free(fds);
330 return retval;
331 }
332
333 int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout)
334 {
335 return ustcomm_recv_message(&ustd->server, msg, src, timeout);
336 }
337
338 int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout)
339 {
340 return ustcomm_recv_message(&app->server, msg, src, timeout);
341 }
342
343 /* This removes src from the list of active connections of app.
344 */
345
346 int ustcomm_app_detach_client(struct ustcomm_app *app, struct ustcomm_source *src)
347 {
348 struct ustcomm_server *server = (struct ustcomm_server *)app;
349 struct ustcomm_connection *conn;
350
351 list_for_each_entry(conn, &server->connections, list) {
352 if(conn->fd == src->fd) {
353 list_del(&conn->list);
354 goto found;
355 }
356 }
357
358 return -1;
359 found:
360 return src->fd;
361 }
362
363 static int init_named_socket(char *name, char **path_out)
364 {
365 int result;
366 int fd;
367
368 struct sockaddr_un addr;
369
370 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
371 if(result == -1) {
372 PERROR("socket");
373 return -1;
374 }
375
376 addr.sun_family = AF_UNIX;
377
378 strncpy(addr.sun_path, name, UNIX_PATH_MAX);
379 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
380
381 result = access(name, F_OK);
382 if(result == 0) {
383 /* file exists */
384 result = unlink(name);
385 if(result == -1) {
386 PERROR("unlink of socket file");
387 goto close_sock;
388 }
389 WARN("socket already exists; overwriting");
390 }
391
392 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
393 if(result == -1) {
394 PERROR("bind");
395 goto close_sock;
396 }
397
398 result = listen(fd, 1);
399 if(result == -1) {
400 PERROR("listen");
401 goto close_sock;
402 }
403
404 if(path_out) {
405 *path_out = "";
406 *path_out = strdupa(addr.sun_path);
407 }
408
409 return fd;
410
411 close_sock:
412 close(fd);
413
414 return -1;
415 }
416
417 int ustcomm_send_request(struct ustcomm_connection *conn, char *req, char **reply)
418 {
419 int result;
420
421 result = send(conn->fd, req, strlen(req), 0);
422 if(result == -1) {
423 PERROR("send");
424 return -1;
425 }
426
427 if(!reply)
428 return 1;
429
430 *reply = (char *) malloc(MSG_MAX+1);
431 result = recv(conn->fd, *reply, MSG_MAX, 0);
432 if(result == -1) {
433 PERROR("recv");
434 return -1;
435 }
436 else if(result == 0) {
437 return 0;
438 }
439
440 (*reply)[result] = '\0';
441
442 return 1;
443 }
444
445 int ustcomm_connect_path(char *path, struct ustcomm_connection *conn, pid_t signalpid)
446 {
447 int fd;
448 int result;
449 struct sockaddr_un addr;
450
451 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
452 if(result == -1) {
453 PERROR("socket");
454 return -1;
455 }
456
457 addr.sun_family = AF_UNIX;
458
459 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path);
460 if(result >= UNIX_PATH_MAX) {
461 ERR("string overflow allocating socket name");
462 return -1;
463 }
464
465 if(signalpid >= 0) {
466 result = signal_process(signalpid);
467 if(result == -1) {
468 ERR("could not signal process");
469 return -1;
470 }
471 }
472
473 result = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
474 if(result == -1) {
475 PERROR("connect");
476 return -1;
477 }
478
479 conn->fd = fd;
480
481 return 0;
482 }
483
484 int ustcomm_disconnect(struct ustcomm_connection *conn)
485 {
486 return close(conn->fd);
487 }
488
489 int ustcomm_connect_app(pid_t pid, struct ustcomm_connection *conn)
490 {
491 int result;
492 char path[UNIX_PATH_MAX];
493
494
495 result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
496 if(result >= UNIX_PATH_MAX) {
497 fprintf(stderr, "string overflow allocating socket name");
498 return -1;
499 }
500
501 return ustcomm_connect_path(path, conn, pid);
502 }
503
504 int ustcomm_disconnect_app(struct ustcomm_connection *conn)
505 {
506 close(conn->fd);
507 return 0;
508 }
509
510 int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle)
511 {
512 int result;
513 char *name;
514
515 result = asprintf(&name, "%s/%d", SOCK_DIR, (int)pid);
516 if(result >= UNIX_PATH_MAX) {
517 ERR("string overflow allocating socket name");
518 return -1;
519 }
520
521 handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath));
522 if(handle->server.listen_fd < 0) {
523 ERR("error initializing named socket");
524 goto free_name;
525 }
526 free(name);
527
528 INIT_LIST_HEAD(&handle->server.connections);
529
530 return 0;
531
532 free_name:
533 free(name);
534 return -1;
535 }
536
537 int ustcomm_init_ustd(struct ustcomm_ustd *handle)
538 {
539 int result;
540 char *name;
541
542 result = asprintf(&name, "%s/%s", SOCK_DIR, "ustd");
543 if(result >= UNIX_PATH_MAX) {
544 ERR("string overflow allocating socket name");
545 return -1;
546 }
547
548 handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath);
549 if(handle->server.listen_fd < 0) {
550 ERR("error initializing named socket at %s", name);
551 goto free_name;
552 }
553 free(name);
554
555 INIT_LIST_HEAD(&handle->server.connections);
556
557 return 0;
558
559 free_name:
560 free(name);
561 return -1;
562 }
563
564 static char *find_tok(char *str)
565 {
566 while(*str == ' ') {
567 str++;
568
569 if(*str == 0)
570 return NULL;
571 }
572
573 return str;
574 }
575
576 static char *find_sep(char *str)
577 {
578 while(*str != ' ') {
579 str++;
580
581 if(*str == 0)
582 break;
583 }
584
585 return str;
586 }
587
588 int nth_token_is(char *str, char *token, int tok_no)
589 {
590 int i;
591 char *start;
592 char *end;
593
594 for(i=0; i<=tok_no; i++) {
595 str = find_tok(str);
596 if(str == NULL)
597 return -1;
598
599 start = str;
600
601 str = find_sep(str);
602 if(str == NULL)
603 return -1;
604
605 end = str;
606 }
607
608 if(end-start != strlen(token))
609 return 0;
610
611 if(strncmp(start, token, end-start))
612 return 0;
613
614 return 1;
615 }
616
617 char *nth_token(char *str, int tok_no)
618 {
619 static char *retval = NULL;
620 int i;
621 char *start;
622 char *end;
623
624 for(i=0; i<=tok_no; i++) {
625 str = find_tok(str);
626 if(str == NULL)
627 return NULL;
628
629 start = str;
630
631 str = find_sep(str);
632 if(str == NULL)
633 return NULL;
634
635 end = str;
636 }
637
638 if(retval) {
639 free(retval);
640 retval = NULL;
641 }
642
643 asprintf(&retval, "%.*s", (int)(end-start), start);
644
645 return retval;
646 }
647
This page took 0.042365 seconds and 4 git commands to generate.