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