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