immediately create listener thread on process creation
[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"
34#include "localerr.h"
f9e5ce61
PMF
35
36#define UNIX_PATH_MAX 108
f9e5ce61 37
d0b5f2b9
PMF
38#define MSG_MAX 1000
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
08230db7
PMF
481/* Called by an application to initialize its server so daemons can
482 * connect to it.
483 */
4e2a8808 484
d0b5f2b9
PMF
485int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle)
486{
487 int result;
488 char *name;
489
490 result = asprintf(&name, "%s/%d", SOCK_DIR, (int)pid);
491 if(result >= UNIX_PATH_MAX) {
492 ERR("string overflow allocating socket name");
493 return -1;
494 }
495
811e4b93
PMF
496 handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath));
497 if(handle->server.listen_fd < 0) {
68ab7a5d 498 ERR("Error initializing named socket (%s). Check that directory exists and that it is writable.", name);
d0b5f2b9
PMF
499 goto free_name;
500 }
501 free(name);
502
811e4b93 503 INIT_LIST_HEAD(&handle->server.connections);
aca1ad90 504
d0b5f2b9
PMF
505 return 0;
506
507free_name:
508 free(name);
509 return -1;
510}
511
08230db7
PMF
512/* Used by the daemon to initialize its server so applications
513 * can connect to it.
514 */
515
c97d4437 516int ustcomm_init_ustd(struct ustcomm_ustd *handle, const char *sock_path)
d0b5f2b9 517{
3847c3ba 518 char *name;
c97d4437 519 int retval = 0;
3847c3ba 520
c97d4437
PMF
521 if(sock_path) {
522 asprintf(&name, "%s", sock_path);
523 }
524 else {
525 asprintf(&name, "%s/%s", SOCK_DIR, "ustd");
3847c3ba
PMF
526 }
527
811e4b93
PMF
528 handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath);
529 if(handle->server.listen_fd < 0) {
6cb88bc0 530 ERR("error initializing named socket at %s", name);
c97d4437 531 retval = -1;
aca1ad90
PMF
532 goto free_name;
533 }
d0b5f2b9 534
811e4b93 535 INIT_LIST_HEAD(&handle->server.connections);
aca1ad90 536
aca1ad90
PMF
537free_name:
538 free(name);
c97d4437
PMF
539
540 return retval;
d0b5f2b9 541}
b02e31e5 542
803a4f58
PMF
543void ustcomm_fini_app(struct ustcomm_app *handle)
544{
545 int result;
546 struct stat st;
547
548 /* Destroy socket */
803a4f58
PMF
549 result = stat(handle->server.socketpath, &st);
550 if(result == -1) {
551 PERROR("stat (%s)", handle->server.socketpath);
552 return;
553 }
554
555 /* Paranoid check before deleting. */
556 result = S_ISSOCK(st.st_mode);
557 if(!result) {
558 ERR("The socket we are about to delete is not a socket.");
559 return;
560 }
561
562 result = unlink(handle->server.socketpath);
563 if(result == -1) {
564 PERROR("unlink");
565 }
566}
567
aca1ad90 568static char *find_tok(char *str)
b02e31e5
PMF
569{
570 while(*str == ' ') {
571 str++;
572
573 if(*str == 0)
574 return NULL;
575 }
576
577 return str;
578}
579
580static char *find_sep(char *str)
581{
582 while(*str != ' ') {
583 str++;
584
585 if(*str == 0)
586 break;
587 }
588
589 return str;
590}
591
592int nth_token_is(char *str, char *token, int tok_no)
593{
594 int i;
595 char *start;
596 char *end;
597
598 for(i=0; i<=tok_no; i++) {
599 str = find_tok(str);
600 if(str == NULL)
601 return -1;
602
603 start = str;
604
605 str = find_sep(str);
606 if(str == NULL)
607 return -1;
608
609 end = str;
610 }
611
612 if(end-start != strlen(token))
613 return 0;
614
615 if(strncmp(start, token, end-start))
616 return 0;
617
618 return 1;
619}
620
621char *nth_token(char *str, int tok_no)
622{
623 static char *retval = NULL;
624 int i;
625 char *start;
626 char *end;
627
628 for(i=0; i<=tok_no; i++) {
629 str = find_tok(str);
630 if(str == NULL)
631 return NULL;
632
633 start = str;
634
635 str = find_sep(str);
636 if(str == NULL)
637 return NULL;
638
639 end = str;
640 }
641
642 if(retval) {
643 free(retval);
644 retval = NULL;
645 }
646
aca1ad90 647 asprintf(&retval, "%.*s", (int)(end-start), start);
b02e31e5
PMF
648
649 return retval;
650}
651
This page took 0.050543 seconds and 4 git commands to generate.