put poll() calls in loops in case they are interrupted by signals
[ust.git] / libustcomm / ustcomm.c
CommitLineData
c39c72ee
PMF
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
d0b5f2b9 18#define _GNU_SOURCE
f9e5ce61
PMF
19#include <sys/types.h>
20#include <signal.h>
21#include <errno.h>
22#include <sys/socket.h>
23#include <sys/un.h>
d0b5f2b9 24#include <unistd.h>
aca1ad90 25#include <poll.h>
f9e5ce61
PMF
26
27#include <stdio.h>
28#include <stdlib.h>
d0b5f2b9 29#include <string.h>
b0540e11 30#include <execinfo.h>
d0b5f2b9
PMF
31
32#include "ustcomm.h"
33#include "localerr.h"
f9e5ce61
PMF
34
35#define UNIX_PATH_MAX 108
36#define SOCK_DIR "/tmp/socks"
37#define UST_SIGNAL SIGIO
38
d0b5f2b9
PMF
39#define MSG_MAX 1000
40
aca1ad90
PMF
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
3847c3ba
PMF
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//}
b0540e11 54
688760ef
PMF
55char *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
52c51a47 69static int signal_process(pid_t pid)
f9e5ce61
PMF
70{
71 int result;
72
73 result = kill(pid, UST_SIGNAL);
74 if(result == -1) {
b0540e11 75 PERROR("kill");
52c51a47 76 return -1;
f9e5ce61
PMF
77 }
78
3bb56863 79 /* FIXME: should wait in a better way */
52c51a47
PMF
80 //sleep(1);
81
82 return 0;
f9e5ce61
PMF
83}
84
4e2a8808 85static int send_message_fd(int fd, const char *msg)
811e4b93
PMF
86{
87 int result;
88
89 result = send(fd, msg, strlen(msg), 0);
90 if(result == -1) {
91 PERROR("send");
92 return -1;
93 }
688760ef
PMF
94 else if(result == 0) {
95 return 0;
96 }
811e4b93 97
688760ef 98 return 1;
4e2a8808
PMF
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;
811e4b93
PMF
113}
114
4e2a8808 115static int send_message_path(const char *path, const char *msg, int signalpid)
f9e5ce61
PMF
116{
117 int fd;
118 int result;
119 struct sockaddr_un addr;
f9e5ce61 120
aca1ad90 121 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
f9e5ce61 122 if(result == -1) {
b0540e11 123 PERROR("socket");
d0b5f2b9 124 return -1;
f9e5ce61
PMF
125 }
126
127 addr.sun_family = AF_UNIX;
128
b0540e11 129 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path);
f9e5ce61 130 if(result >= UNIX_PATH_MAX) {
b0540e11 131 ERR("string overflow allocating socket name");
d0b5f2b9 132 return -1;
f9e5ce61
PMF
133 }
134
52c51a47
PMF
135 if(signalpid >= 0) {
136 result = signal_process(signalpid);
137 if(result == -1) {
138 ERR("could not signal process");
139 return -1;
140 }
141 }
f9e5ce61 142
aca1ad90 143 result = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
f9e5ce61 144 if(result == -1) {
aca1ad90
PMF
145 PERROR("connect");
146 return -1;
147 }
148
4e2a8808 149 return send_message_fd(fd, msg);
f9e5ce61
PMF
150}
151
4e2a8808
PMF
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//}
b0540e11
PMF
173
174/* Called by an app to ask the consumer daemon to connect to it. */
175
176int 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
4e2a8808 190 send_message_path(path, msg, -1);
b0540e11
PMF
191 free(msg);
192
193 return 0;
194}
195
688760ef
PMF
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 */
811e4b93 200
b02e31e5 201static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src)
d0b5f2b9 202{
d0b5f2b9 203 int result;
d0b5f2b9
PMF
204
205 *msg = (char *) malloc(MSG_MAX+1);
b02e31e5 206
aca1ad90 207 result = recv(fd, *msg, MSG_MAX, 0);
d0b5f2b9 208 if(result == -1) {
688760ef 209 PERROR("recv");
d0b5f2b9
PMF
210 return -1;
211 }
b0540e11 212
d0b5f2b9 213 (*msg)[result] = '\0';
b0540e11
PMF
214
215 DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg));
216
811e4b93
PMF
217 if(src)
218 src->fd = fd;
219
688760ef 220 return 1;
d0b5f2b9
PMF
221}
222
811e4b93
PMF
223int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_source *src)
224{
225 int result;
226
4e2a8808 227 result = send_message_fd(src->fd, msg);
3a7b90de 228 if(result < 0) {
811e4b93
PMF
229 ERR("error in send_message_fd");
230 return -1;
231 }
232
233 return 0;
234}
235
688760ef
PMF
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
243int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src, int timeout)
b0540e11 244{
aca1ad90
PMF
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
811e4b93 254 list_for_each_entry(conn, &server->connections, list) {
aca1ad90
PMF
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 */
811e4b93 265 fds[idx].fd = server->listen_fd;
aca1ad90
PMF
266 fds[idx].events = POLLIN;
267 idx++;
268
811e4b93 269 list_for_each_entry(conn, &server->connections, list) {
aca1ad90
PMF
270 fds[idx].fd = conn->fd;
271 fds[idx].events = POLLIN;
272 idx++;
273 }
274
69ba0156
PMF
275 while((result = poll(fds, n_fds, timeout)) == -1 && errno == EINTR)
276 /* nothing */;
aca1ad90
PMF
277 if(result == -1) {
278 PERROR("poll");
279 return -1;
280 }
281
688760ef
PMF
282 if(result == 0)
283 return 0;
284
aca1ad90
PMF
285 if(fds[0].revents) {
286 struct ustcomm_connection *newconn;
287 int newfd;
288
811e4b93 289 result = newfd = accept(server->listen_fd, NULL, NULL);
aca1ad90
PMF
290 if(result == -1) {
291 PERROR("accept");
292 return -1;
293 }
294
295 newconn = (struct ustcomm_connection *) malloc(sizeof(struct ustcomm_connection));
296 if(newconn == NULL) {
297 ERR("malloc returned NULL");
298 return -1;
299 }
300
301 newconn->fd = newfd;
302
811e4b93 303 list_add(&newconn->list, &server->connections);
aca1ad90
PMF
304 }
305
306 for(idx=1; idx<n_fds; idx++) {
307 if(fds[idx].revents) {
308 retval = recv_message_fd(fds[idx].fd, msg, src);
309 if(**msg == 0) {
310 /* connection finished */
311 close(fds[idx].fd);
312
811e4b93 313 list_for_each_entry(conn, &server->connections, list) {
aca1ad90
PMF
314 if(conn->fd == fds[idx].fd) {
315 list_del(&conn->list);
316 break;
317 }
318 }
319 }
320 else {
321 goto free_fds_return;
322 }
323 }
324 }
325
326 free(fds);
327 }
328
329free_fds_return:
330 free(fds);
331 return retval;
b0540e11
PMF
332}
333
688760ef 334int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout)
811e4b93 335{
688760ef 336 return ustcomm_recv_message(&ustd->server, msg, src, timeout);
811e4b93
PMF
337}
338
688760ef 339int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout)
b0540e11 340{
688760ef 341 return ustcomm_recv_message(&app->server, msg, src, timeout);
b0540e11
PMF
342}
343
46ef48cd
PMF
344/* This removes src from the list of active connections of app.
345 */
346
347int ustcomm_app_detach_client(struct ustcomm_app *app, struct ustcomm_source *src)
348{
349 struct ustcomm_server *server = (struct ustcomm_server *)app;
350 struct ustcomm_connection *conn;
351
352 list_for_each_entry(conn, &server->connections, list) {
353 if(conn->fd == src->fd) {
354 list_del(&conn->list);
355 goto found;
356 }
357 }
358
359 return -1;
360found:
361 return src->fd;
362}
363
d0b5f2b9
PMF
364static int init_named_socket(char *name, char **path_out)
365{
366 int result;
367 int fd;
368
369 struct sockaddr_un addr;
370
aca1ad90 371 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
d0b5f2b9
PMF
372 if(result == -1) {
373 PERROR("socket");
374 return -1;
375 }
376
377 addr.sun_family = AF_UNIX;
378
379 strncpy(addr.sun_path, name, UNIX_PATH_MAX);
380 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
381
aca1ad90
PMF
382 result = access(name, F_OK);
383 if(result == 0) {
384 /* file exists */
385 result = unlink(name);
386 if(result == -1) {
387 PERROR("unlink of socket file");
388 goto close_sock;
389 }
390 WARN("socket already exists; overwriting");
391 }
392
d0b5f2b9
PMF
393 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
394 if(result == -1) {
395 PERROR("bind");
396 goto close_sock;
397 }
398
aca1ad90
PMF
399 result = listen(fd, 1);
400 if(result == -1) {
401 PERROR("listen");
402 goto close_sock;
403 }
404
b0540e11
PMF
405 if(path_out) {
406 *path_out = "";
d0b5f2b9 407 *path_out = strdupa(addr.sun_path);
b0540e11 408 }
d0b5f2b9
PMF
409
410 return fd;
411
412 close_sock:
413 close(fd);
414
415 return -1;
416}
417
4e2a8808
PMF
418int ustcomm_send_request(struct ustcomm_connection *conn, char *req, char **reply)
419{
420 int result;
421
422 result = send(conn->fd, req, strlen(req), 0);
423 if(result == -1) {
424 PERROR("send");
425 return -1;
426 }
4e2a8808
PMF
427
428 if(!reply)
429 return 1;
430
431 *reply = (char *) malloc(MSG_MAX+1);
432 result = recv(conn->fd, *reply, MSG_MAX, 0);
433 if(result == -1) {
434 PERROR("recv");
435 return -1;
436 }
437 else if(result == 0) {
438 return 0;
439 }
440
441 (*reply)[result] = '\0';
442
443 return 1;
444}
445
446int ustcomm_connect_path(char *path, struct ustcomm_connection *conn, pid_t signalpid)
447{
448 int fd;
449 int result;
450 struct sockaddr_un addr;
451
452 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
453 if(result == -1) {
454 PERROR("socket");
455 return -1;
456 }
457
458 addr.sun_family = AF_UNIX;
459
460 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path);
461 if(result >= UNIX_PATH_MAX) {
462 ERR("string overflow allocating socket name");
463 return -1;
464 }
465
52c51a47
PMF
466 if(signalpid >= 0) {
467 result = signal_process(signalpid);
468 if(result == -1) {
469 ERR("could not signal process");
470 return -1;
471 }
472 }
4e2a8808
PMF
473
474 result = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
475 if(result == -1) {
476 PERROR("connect");
477 return -1;
478 }
479
480 conn->fd = fd;
481
482 return 0;
483}
484
485int ustcomm_disconnect(struct ustcomm_connection *conn)
486{
487 return close(conn->fd);
488}
489
490int ustcomm_connect_app(pid_t pid, struct ustcomm_connection *conn)
491{
492 int result;
493 char path[UNIX_PATH_MAX];
494
495
496 result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
497 if(result >= UNIX_PATH_MAX) {
498 fprintf(stderr, "string overflow allocating socket name");
499 return -1;
500 }
501
502 return ustcomm_connect_path(path, conn, pid);
503}
504
505int ustcomm_disconnect_app(struct ustcomm_connection *conn)
506{
507 close(conn->fd);
508 return 0;
509}
510
d0b5f2b9
PMF
511int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle)
512{
513 int result;
514 char *name;
515
516 result = asprintf(&name, "%s/%d", SOCK_DIR, (int)pid);
517 if(result >= UNIX_PATH_MAX) {
518 ERR("string overflow allocating socket name");
519 return -1;
520 }
521
811e4b93
PMF
522 handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath));
523 if(handle->server.listen_fd < 0) {
aca1ad90 524 ERR("error initializing named socket");
d0b5f2b9
PMF
525 goto free_name;
526 }
527 free(name);
528
811e4b93 529 INIT_LIST_HEAD(&handle->server.connections);
aca1ad90 530
d0b5f2b9
PMF
531 return 0;
532
533free_name:
534 free(name);
535 return -1;
536}
537
538int ustcomm_init_ustd(struct ustcomm_ustd *handle)
539{
3847c3ba
PMF
540 int result;
541 char *name;
542
543 result = asprintf(&name, "%s/%s", SOCK_DIR, "ustd");
544 if(result >= UNIX_PATH_MAX) {
545 ERR("string overflow allocating socket name");
546 return -1;
547 }
548
811e4b93
PMF
549 handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath);
550 if(handle->server.listen_fd < 0) {
6cb88bc0 551 ERR("error initializing named socket at %s", name);
aca1ad90
PMF
552 goto free_name;
553 }
3847c3ba 554 free(name);
d0b5f2b9 555
811e4b93 556 INIT_LIST_HEAD(&handle->server.connections);
aca1ad90 557
d0b5f2b9 558 return 0;
aca1ad90
PMF
559
560free_name:
561 free(name);
562 return -1;
d0b5f2b9 563}
b02e31e5 564
aca1ad90 565static char *find_tok(char *str)
b02e31e5
PMF
566{
567 while(*str == ' ') {
568 str++;
569
570 if(*str == 0)
571 return NULL;
572 }
573
574 return str;
575}
576
577static char *find_sep(char *str)
578{
579 while(*str != ' ') {
580 str++;
581
582 if(*str == 0)
583 break;
584 }
585
586 return str;
587}
588
589int nth_token_is(char *str, char *token, int tok_no)
590{
591 int i;
592 char *start;
593 char *end;
594
595 for(i=0; i<=tok_no; i++) {
596 str = find_tok(str);
597 if(str == NULL)
598 return -1;
599
600 start = str;
601
602 str = find_sep(str);
603 if(str == NULL)
604 return -1;
605
606 end = str;
607 }
608
609 if(end-start != strlen(token))
610 return 0;
611
612 if(strncmp(start, token, end-start))
613 return 0;
614
615 return 1;
616}
617
618char *nth_token(char *str, int tok_no)
619{
620 static char *retval = NULL;
621 int i;
622 char *start;
623 char *end;
624
625 for(i=0; i<=tok_no; i++) {
626 str = find_tok(str);
627 if(str == NULL)
628 return NULL;
629
630 start = str;
631
632 str = find_sep(str);
633 if(str == NULL)
634 return NULL;
635
636 end = str;
637 }
638
639 if(retval) {
640 free(retval);
641 retval = NULL;
642 }
643
aca1ad90 644 asprintf(&retval, "%.*s", (int)(end-start), start);
b02e31e5
PMF
645
646 return retval;
647}
648
This page took 0.048075 seconds and 4 git commands to generate.