usterr.h: add DBG_raw() macro
[lttng-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>
803a4f58 26#include <sys/stat.h>
f9e5ce61
PMF
27
28#include <stdio.h>
29#include <stdlib.h>
d0b5f2b9 30#include <string.h>
b0540e11 31#include <execinfo.h>
d0b5f2b9
PMF
32
33#include "ustcomm.h"
6af64c43 34#include "usterr.h"
f9e5ce61
PMF
35
36#define UNIX_PATH_MAX 108
f9e5ce61 37
058a68cb 38#define MSG_MAX 10000
d0b5f2b9 39
aca1ad90
PMF
40/* FIXME: ustcomm blocks on message sending, which might be problematic in
41 * some cases. Fix the poll() usage so sends are buffered until they don't
42 * block.
43 */
44
3847c3ba
PMF
45//static void bt(void)
46//{
47// void *buffer[100];
48// int result;
49//
50// result = backtrace(&buffer, 100);
51// backtrace_symbols_fd(buffer, result, STDERR_FILENO);
52//}
b0540e11 53
688760ef
PMF
54char *strdup_malloc(const char *s)
55{
56 char *retval;
57
58 if(s == NULL)
59 return NULL;
60
61 retval = (char *) malloc(strlen(s)+1);
62
63 strcpy(retval, s);
64
65 return retval;
66}
67
52c51a47 68static int signal_process(pid_t pid)
f9e5ce61 69{
52c51a47 70 return 0;
f9e5ce61
PMF
71}
72
ab33e65c 73int pid_is_online(pid_t pid) {
2944a629 74 return 1;
ab33e65c
PP
75}
76
4e2a8808 77static int send_message_fd(int fd, const char *msg)
811e4b93
PMF
78{
79 int result;
80
60e57148 81 result = send(fd, msg, strlen(msg), MSG_NOSIGNAL);
811e4b93
PMF
82 if(result == -1) {
83 PERROR("send");
84 return -1;
85 }
688760ef
PMF
86 else if(result == 0) {
87 return 0;
88 }
811e4b93 89
688760ef 90 return 1;
811e4b93
PMF
91}
92
b0540e11
PMF
93/* Called by an app to ask the consumer daemon to connect to it. */
94
95int ustcomm_request_consumer(pid_t pid, const char *channel)
96{
97 char path[UNIX_PATH_MAX];
98 int result;
08230db7
PMF
99 char *msg=NULL;
100 int retval = 0;
101 struct ustcomm_connection conn;
c97d4437
PMF
102 char *explicit_daemon_socket_path;
103
104 explicit_daemon_socket_path = getenv("UST_DAEMON_SOCKET");
105 if(explicit_daemon_socket_path) {
106 /* user specified explicitly a socket path */
107 result = snprintf(path, UNIX_PATH_MAX, "%s", explicit_daemon_socket_path);
108 }
109 else {
110 /* just use the default path */
111 result = snprintf(path, UNIX_PATH_MAX, "%s/ustd", SOCK_DIR);
112 }
b0540e11 113
b0540e11 114 if(result >= UNIX_PATH_MAX) {
08230db7 115 ERR("string overflow allocating socket name");
b0540e11
PMF
116 return -1;
117 }
118
119 asprintf(&msg, "collect %d %s", pid, channel);
120
08230db7
PMF
121 /* don't signal it because it's the daemon */
122 result = ustcomm_connect_path(path, &conn, -1);
123 if(result == -1) {
124 WARN("ustcomm_connect_path failed");
125 retval = -1;
126 goto del_string;
127 }
128
129 result = ustcomm_send_request(&conn, msg, NULL);
130 if(result == -1) {
131 WARN("ustcomm_send_request failed");
132 retval = -1;
133 goto disconnect;
134 }
135
136 disconnect:
137 ustcomm_disconnect(&conn);
138 del_string:
b0540e11
PMF
139 free(msg);
140
08230db7 141 return retval;
b0540e11
PMF
142}
143
688760ef
PMF
144/* returns 1 to indicate a message was received
145 * returns 0 to indicate no message was received (cannot happen)
146 * returns -1 to indicate an error
147 */
811e4b93 148
b02e31e5 149static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src)
d0b5f2b9 150{
d0b5f2b9 151 int result;
d0b5f2b9
PMF
152
153 *msg = (char *) malloc(MSG_MAX+1);
b02e31e5 154
aca1ad90 155 result = recv(fd, *msg, MSG_MAX, 0);
d0b5f2b9 156 if(result == -1) {
688760ef 157 PERROR("recv");
d0b5f2b9
PMF
158 return -1;
159 }
b0540e11 160
d0b5f2b9 161 (*msg)[result] = '\0';
b0540e11
PMF
162
163 DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg));
164
811e4b93
PMF
165 if(src)
166 src->fd = fd;
167
688760ef 168 return 1;
d0b5f2b9
PMF
169}
170
811e4b93
PMF
171int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_source *src)
172{
173 int result;
174
4e2a8808 175 result = send_message_fd(src->fd, msg);
3a7b90de 176 if(result < 0) {
811e4b93
PMF
177 ERR("error in send_message_fd");
178 return -1;
179 }
180
181 return 0;
182}
183
99b72dc0
PMF
184/* Called after a fork. */
185
186int ustcomm_close_all_connections(struct ustcomm_server *server)
187{
188 struct ustcomm_connection *conn;
189 struct ustcomm_connection *deletable_conn = NULL;
190
191 list_for_each_entry(conn, &server->connections, list) {
192 free(deletable_conn);
193 deletable_conn = conn;
194 close(conn->fd);
195 list_del(&conn->list);
196 }
197
198 return 0;
199}
200
688760ef
PMF
201/* @timeout: max blocking time in milliseconds, -1 means infinity
202 *
203 * returns 1 to indicate a message was received
204 * returns 0 to indicate no message was received
205 * returns -1 to indicate an error
206 */
207
208int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src, int timeout)
b0540e11 209{
aca1ad90
PMF
210 struct pollfd *fds;
211 struct ustcomm_connection *conn;
212 int result;
213 int retval;
214
215 for(;;) {
216 int idx = 0;
217 int n_fds = 1;
218
811e4b93 219 list_for_each_entry(conn, &server->connections, list) {
aca1ad90
PMF
220 n_fds++;
221 }
222
223 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
224 if(fds == NULL) {
225 ERR("malloc returned NULL");
226 return -1;
227 }
228
229 /* special idx 0 is for listening socket */
811e4b93 230 fds[idx].fd = server->listen_fd;
aca1ad90
PMF
231 fds[idx].events = POLLIN;
232 idx++;
233
811e4b93 234 list_for_each_entry(conn, &server->connections, list) {
aca1ad90
PMF
235 fds[idx].fd = conn->fd;
236 fds[idx].events = POLLIN;
237 idx++;
238 }
239
69ba0156
PMF
240 while((result = poll(fds, n_fds, timeout)) == -1 && errno == EINTR)
241 /* nothing */;
aca1ad90
PMF
242 if(result == -1) {
243 PERROR("poll");
244 return -1;
245 }
246
688760ef
PMF
247 if(result == 0)
248 return 0;
249
aca1ad90
PMF
250 if(fds[0].revents) {
251 struct ustcomm_connection *newconn;
252 int newfd;
253
811e4b93 254 result = newfd = accept(server->listen_fd, NULL, NULL);
aca1ad90
PMF
255 if(result == -1) {
256 PERROR("accept");
257 return -1;
258 }
259
260 newconn = (struct ustcomm_connection *) malloc(sizeof(struct ustcomm_connection));
261 if(newconn == NULL) {
262 ERR("malloc returned NULL");
263 return -1;
264 }
265
266 newconn->fd = newfd;
267
811e4b93 268 list_add(&newconn->list, &server->connections);
aca1ad90
PMF
269 }
270
271 for(idx=1; idx<n_fds; idx++) {
272 if(fds[idx].revents) {
273 retval = recv_message_fd(fds[idx].fd, msg, src);
274 if(**msg == 0) {
275 /* connection finished */
276 close(fds[idx].fd);
277
811e4b93 278 list_for_each_entry(conn, &server->connections, list) {
aca1ad90
PMF
279 if(conn->fd == fds[idx].fd) {
280 list_del(&conn->list);
281 break;
282 }
283 }
284 }
285 else {
286 goto free_fds_return;
287 }
288 }
289 }
290
291 free(fds);
292 }
293
294free_fds_return:
295 free(fds);
296 return retval;
b0540e11
PMF
297}
298
688760ef 299int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout)
811e4b93 300{
688760ef 301 return ustcomm_recv_message(&ustd->server, msg, src, timeout);
811e4b93
PMF
302}
303
688760ef 304int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout)
b0540e11 305{
688760ef 306 return ustcomm_recv_message(&app->server, msg, src, timeout);
b0540e11
PMF
307}
308
46ef48cd
PMF
309/* This removes src from the list of active connections of app.
310 */
311
312int ustcomm_app_detach_client(struct ustcomm_app *app, struct ustcomm_source *src)
313{
314 struct ustcomm_server *server = (struct ustcomm_server *)app;
315 struct ustcomm_connection *conn;
316
317 list_for_each_entry(conn, &server->connections, list) {
318 if(conn->fd == src->fd) {
319 list_del(&conn->list);
320 goto found;
321 }
322 }
323
324 return -1;
325found:
326 return src->fd;
327}
328
08230db7 329static int init_named_socket(const char *name, char **path_out)
d0b5f2b9
PMF
330{
331 int result;
332 int fd;
333
334 struct sockaddr_un addr;
335
aca1ad90 336 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
d0b5f2b9
PMF
337 if(result == -1) {
338 PERROR("socket");
339 return -1;
340 }
341
342 addr.sun_family = AF_UNIX;
343
344 strncpy(addr.sun_path, name, UNIX_PATH_MAX);
345 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
346
aca1ad90
PMF
347 result = access(name, F_OK);
348 if(result == 0) {
349 /* file exists */
350 result = unlink(name);
351 if(result == -1) {
352 PERROR("unlink of socket file");
353 goto close_sock;
354 }
355 WARN("socket already exists; overwriting");
356 }
357
d0b5f2b9
PMF
358 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
359 if(result == -1) {
360 PERROR("bind");
361 goto close_sock;
362 }
363
aca1ad90
PMF
364 result = listen(fd, 1);
365 if(result == -1) {
366 PERROR("listen");
367 goto close_sock;
368 }
369
b0540e11 370 if(path_out) {
803a4f58 371 *path_out = strdup(addr.sun_path);
b0540e11 372 }
d0b5f2b9
PMF
373
374 return fd;
375
376 close_sock:
377 close(fd);
378
379 return -1;
380}
381
34b460e6
PMF
382/*
383 * Return value:
384 * 0: Success, but no reply because recv() returned 0
385 * 1: Success
386 * -1: Error
387 *
388 * On error, the error message is printed, except on
389 * ECONNRESET, which is normal when the application dies.
390 */
391
772030fe 392int ustcomm_send_request(struct ustcomm_connection *conn, const char *req, char **reply)
4e2a8808
PMF
393{
394 int result;
395
34b460e6 396 result = send(conn->fd, req, strlen(req), MSG_NOSIGNAL);
4e2a8808 397 if(result == -1) {
68ab7a5d 398 if(errno != EPIPE)
34b460e6 399 PERROR("send");
4e2a8808
PMF
400 return -1;
401 }
4e2a8808
PMF
402
403 if(!reply)
404 return 1;
405
406 *reply = (char *) malloc(MSG_MAX+1);
407 result = recv(conn->fd, *reply, MSG_MAX, 0);
408 if(result == -1) {
34b460e6
PMF
409 if(errno != ECONNRESET)
410 PERROR("recv");
4e2a8808
PMF
411 return -1;
412 }
413 else if(result == 0) {
414 return 0;
415 }
416
417 (*reply)[result] = '\0';
418
419 return 1;
420}
421
08230db7 422int ustcomm_connect_path(const char *path, struct ustcomm_connection *conn, pid_t signalpid)
4e2a8808
PMF
423{
424 int fd;
425 int result;
426 struct sockaddr_un addr;
427
428 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
429 if(result == -1) {
430 PERROR("socket");
431 return -1;
432 }
433
434 addr.sun_family = AF_UNIX;
435
436 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path);
437 if(result >= UNIX_PATH_MAX) {
438 ERR("string overflow allocating socket name");
439 return -1;
440 }
441
52c51a47
PMF
442 if(signalpid >= 0) {
443 result = signal_process(signalpid);
444 if(result == -1) {
445 ERR("could not signal process");
446 return -1;
447 }
448 }
4e2a8808
PMF
449
450 result = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
451 if(result == -1) {
452 PERROR("connect");
453 return -1;
454 }
455
456 conn->fd = fd;
457
458 return 0;
459}
460
461int ustcomm_disconnect(struct ustcomm_connection *conn)
462{
463 return close(conn->fd);
464}
465
466int ustcomm_connect_app(pid_t pid, struct ustcomm_connection *conn)
467{
468 int result;
469 char path[UNIX_PATH_MAX];
470
471
472 result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
473 if(result >= UNIX_PATH_MAX) {
08230db7 474 ERR("string overflow allocating socket name");
4e2a8808
PMF
475 return -1;
476 }
477
478 return ustcomm_connect_path(path, conn, pid);
479}
480
dce0b474
PMF
481static int ensure_dir_exists(const char *dir)
482{
483 struct stat st;
484 int result;
485
486 if(!strcmp(dir, ""))
487 return -1;
488
489 result = stat(dir, &st);
490 if(result == -1 && errno != ENOENT) {
491 return -1;
492 }
493 else if(result == -1) {
494 /* ENOENT */
495 char buf[200];
496 int result;
497
498 result = snprintf(buf, sizeof(buf), "mkdir -p \"%s\"", dir);
499 if(result >= sizeof(buf)) {
500 ERR("snprintf buffer overflow");
501 return -1;
502 }
503 result = system(buf);
504 if(result != 0) {
505 ERR("executing command %s", buf);
506 return -1;
507 }
508 }
509
510 return 0;
511}
512
08230db7
PMF
513/* Called by an application to initialize its server so daemons can
514 * connect to it.
515 */
4e2a8808 516
d0b5f2b9
PMF
517int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle)
518{
519 int result;
520 char *name;
521
522 result = asprintf(&name, "%s/%d", SOCK_DIR, (int)pid);
523 if(result >= UNIX_PATH_MAX) {
524 ERR("string overflow allocating socket name");
525 return -1;
526 }
527
dce0b474
PMF
528 result = ensure_dir_exists(SOCK_DIR);
529 if(result == -1) {
530 ERR("Unable to create socket directory %s", SOCK_DIR);
531 return -1;
532 }
533
811e4b93
PMF
534 handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath));
535 if(handle->server.listen_fd < 0) {
68ab7a5d 536 ERR("Error initializing named socket (%s). Check that directory exists and that it is writable.", name);
d0b5f2b9
PMF
537 goto free_name;
538 }
539 free(name);
540
811e4b93 541 INIT_LIST_HEAD(&handle->server.connections);
aca1ad90 542
d0b5f2b9
PMF
543 return 0;
544
545free_name:
546 free(name);
547 return -1;
548}
549
08230db7
PMF
550/* Used by the daemon to initialize its server so applications
551 * can connect to it.
552 */
553
c97d4437 554int ustcomm_init_ustd(struct ustcomm_ustd *handle, const char *sock_path)
d0b5f2b9 555{
3847c3ba 556 char *name;
c97d4437 557 int retval = 0;
3847c3ba 558
c97d4437
PMF
559 if(sock_path) {
560 asprintf(&name, "%s", sock_path);
561 }
562 else {
dce0b474
PMF
563 int result;
564
565 /* Only check if socket dir exists if we are using the default directory */
566 result = ensure_dir_exists(SOCK_DIR);
567 if(result == -1) {
568 ERR("Unable to create socket directory %s", SOCK_DIR);
569 return -1;
570 }
571
c97d4437 572 asprintf(&name, "%s/%s", SOCK_DIR, "ustd");
3847c3ba
PMF
573 }
574
811e4b93
PMF
575 handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath);
576 if(handle->server.listen_fd < 0) {
6cb88bc0 577 ERR("error initializing named socket at %s", name);
c97d4437 578 retval = -1;
aca1ad90
PMF
579 goto free_name;
580 }
d0b5f2b9 581
811e4b93 582 INIT_LIST_HEAD(&handle->server.connections);
aca1ad90 583
aca1ad90
PMF
584free_name:
585 free(name);
c97d4437
PMF
586
587 return retval;
d0b5f2b9 588}
b02e31e5 589
803a4f58
PMF
590void ustcomm_fini_app(struct ustcomm_app *handle)
591{
592 int result;
593 struct stat st;
594
595 /* Destroy socket */
803a4f58
PMF
596 result = stat(handle->server.socketpath, &st);
597 if(result == -1) {
598 PERROR("stat (%s)", handle->server.socketpath);
599 return;
600 }
601
602 /* Paranoid check before deleting. */
603 result = S_ISSOCK(st.st_mode);
604 if(!result) {
605 ERR("The socket we are about to delete is not a socket.");
606 return;
607 }
608
609 result = unlink(handle->server.socketpath);
610 if(result == -1) {
611 PERROR("unlink");
612 }
613}
614
7e92827d 615static const char *find_tok(const char *str)
b02e31e5
PMF
616{
617 while(*str == ' ') {
618 str++;
619
620 if(*str == 0)
621 return NULL;
622 }
623
624 return str;
625}
626
7e92827d 627static const char *find_sep(const char *str)
b02e31e5
PMF
628{
629 while(*str != ' ') {
630 str++;
631
632 if(*str == 0)
633 break;
634 }
635
636 return str;
637}
638
7e92827d 639int nth_token_is(const char *str, const char *token, int tok_no)
b02e31e5
PMF
640{
641 int i;
7e92827d
PMF
642 const char *start;
643 const char *end;
b02e31e5
PMF
644
645 for(i=0; i<=tok_no; i++) {
646 str = find_tok(str);
647 if(str == NULL)
648 return -1;
649
650 start = str;
651
652 str = find_sep(str);
653 if(str == NULL)
654 return -1;
655
656 end = str;
657 }
658
659 if(end-start != strlen(token))
660 return 0;
661
662 if(strncmp(start, token, end-start))
663 return 0;
664
665 return 1;
666}
667
7e92827d 668char *nth_token(const char *str, int tok_no)
b02e31e5
PMF
669{
670 static char *retval = NULL;
671 int i;
7e92827d
PMF
672 const char *start;
673 const char *end;
b02e31e5
PMF
674
675 for(i=0; i<=tok_no; i++) {
676 str = find_tok(str);
677 if(str == NULL)
678 return NULL;
679
680 start = str;
681
682 str = find_sep(str);
683 if(str == NULL)
684 return NULL;
685
686 end = str;
687 }
688
689 if(retval) {
690 free(retval);
691 retval = NULL;
692 }
693
aca1ad90 694 asprintf(&retval, "%.*s", (int)(end-start), start);
b02e31e5
PMF
695
696 return retval;
697}
698
This page took 0.06367 seconds and 4 git commands to generate.