improve error handling
[ust.git] / libustcomm / ustcomm.c
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
18 #define _GNU_SOURCE
19 #include <sys/types.h>
20 #include <signal.h>
21 #include <errno.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <unistd.h>
25 #include <poll.h>
26 #include <sys/stat.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <execinfo.h>
32
33 #include "ustcomm.h"
34 #include "localerr.h"
35
36 #define UNIX_PATH_MAX 108
37
38 #define MSG_MAX 1000
39
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
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 //}
53
54 char *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
68 static int signal_process(pid_t pid)
69 {
70 int result;
71
72 result = kill(pid, UST_SIGNAL);
73 if(result == -1) {
74 PERROR("kill");
75 return -1;
76 }
77
78 /* FIXME: should wait in a better way */
79 //sleep(1);
80
81 return 0;
82 }
83
84 int pid_is_online(pid_t pid) {
85 return kill(pid, UST_SIGNAL) != -1;
86 }
87
88 static int send_message_fd(int fd, const char *msg)
89 {
90 int result;
91
92 result = send(fd, msg, strlen(msg), 0);
93 if(result == -1) {
94 PERROR("send");
95 return -1;
96 }
97 else if(result == 0) {
98 return 0;
99 }
100
101 return 1;
102 }
103
104 /* Called by an app to ask the consumer daemon to connect to it. */
105
106 int ustcomm_request_consumer(pid_t pid, const char *channel)
107 {
108 char path[UNIX_PATH_MAX];
109 int result;
110 char *msg=NULL;
111 int retval = 0;
112 struct ustcomm_connection conn;
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 }
124
125 if(result >= UNIX_PATH_MAX) {
126 ERR("string overflow allocating socket name");
127 return -1;
128 }
129
130 asprintf(&msg, "collect %d %s", pid, channel);
131
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:
150 free(msg);
151
152 return retval;
153 }
154
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 */
159
160 static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src)
161 {
162 int result;
163
164 *msg = (char *) malloc(MSG_MAX+1);
165
166 result = recv(fd, *msg, MSG_MAX, 0);
167 if(result == -1) {
168 PERROR("recv");
169 return -1;
170 }
171
172 (*msg)[result] = '\0';
173
174 DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg));
175
176 if(src)
177 src->fd = fd;
178
179 return 1;
180 }
181
182 int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_source *src)
183 {
184 int result;
185
186 result = send_message_fd(src->fd, msg);
187 if(result < 0) {
188 ERR("error in send_message_fd");
189 return -1;
190 }
191
192 return 0;
193 }
194
195 /* Called after a fork. */
196
197 int 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
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
219 int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src, int timeout)
220 {
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
230 list_for_each_entry(conn, &server->connections, list) {
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 */
241 fds[idx].fd = server->listen_fd;
242 fds[idx].events = POLLIN;
243 idx++;
244
245 list_for_each_entry(conn, &server->connections, list) {
246 fds[idx].fd = conn->fd;
247 fds[idx].events = POLLIN;
248 idx++;
249 }
250
251 while((result = poll(fds, n_fds, timeout)) == -1 && errno == EINTR)
252 /* nothing */;
253 if(result == -1) {
254 PERROR("poll");
255 return -1;
256 }
257
258 if(result == 0)
259 return 0;
260
261 if(fds[0].revents) {
262 struct ustcomm_connection *newconn;
263 int newfd;
264
265 result = newfd = accept(server->listen_fd, NULL, NULL);
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
279 list_add(&newconn->list, &server->connections);
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
289 list_for_each_entry(conn, &server->connections, list) {
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
305 free_fds_return:
306 free(fds);
307 return retval;
308 }
309
310 int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout)
311 {
312 return ustcomm_recv_message(&ustd->server, msg, src, timeout);
313 }
314
315 int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout)
316 {
317 return ustcomm_recv_message(&app->server, msg, src, timeout);
318 }
319
320 /* This removes src from the list of active connections of app.
321 */
322
323 int 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;
336 found:
337 return src->fd;
338 }
339
340 static int init_named_socket(const char *name, char **path_out)
341 {
342 int result;
343 int fd;
344
345 struct sockaddr_un addr;
346
347 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
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
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
369 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
370 if(result == -1) {
371 PERROR("bind");
372 goto close_sock;
373 }
374
375 result = listen(fd, 1);
376 if(result == -1) {
377 PERROR("listen");
378 goto close_sock;
379 }
380
381 if(path_out) {
382 *path_out = strdup(addr.sun_path);
383 }
384
385 return fd;
386
387 close_sock:
388 close(fd);
389
390 return -1;
391 }
392
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
403 int ustcomm_send_request(struct ustcomm_connection *conn, const char *req, char **reply)
404 {
405 int result;
406
407 result = send(conn->fd, req, strlen(req), MSG_NOSIGNAL);
408 if(result == -1) {
409 if(errno != EPIPE)
410 PERROR("send");
411 return -1;
412 }
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) {
420 if(errno != ECONNRESET)
421 PERROR("recv");
422 return -1;
423 }
424 else if(result == 0) {
425 return 0;
426 }
427
428 (*reply)[result] = '\0';
429
430 return 1;
431 }
432
433 int ustcomm_connect_path(const char *path, struct ustcomm_connection *conn, pid_t signalpid)
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
453 if(signalpid >= 0) {
454 result = signal_process(signalpid);
455 if(result == -1) {
456 ERR("could not signal process");
457 return -1;
458 }
459 }
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
472 int ustcomm_disconnect(struct ustcomm_connection *conn)
473 {
474 return close(conn->fd);
475 }
476
477 int 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) {
485 ERR("string overflow allocating socket name");
486 return -1;
487 }
488
489 return ustcomm_connect_path(path, conn, pid);
490 }
491
492 /* Called by an application to initialize its server so daemons can
493 * connect to it.
494 */
495
496 int 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
507 handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath));
508 if(handle->server.listen_fd < 0) {
509 ERR("Error initializing named socket (%s). Check that directory exists and that it is writable.", name);
510 goto free_name;
511 }
512 free(name);
513
514 INIT_LIST_HEAD(&handle->server.connections);
515
516 return 0;
517
518 free_name:
519 free(name);
520 return -1;
521 }
522
523 /* Used by the daemon to initialize its server so applications
524 * can connect to it.
525 */
526
527 int ustcomm_init_ustd(struct ustcomm_ustd *handle, const char *sock_path)
528 {
529 char *name;
530 int retval = 0;
531
532 if(sock_path) {
533 asprintf(&name, "%s", sock_path);
534 }
535 else {
536 asprintf(&name, "%s/%s", SOCK_DIR, "ustd");
537 }
538
539 handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath);
540 if(handle->server.listen_fd < 0) {
541 ERR("error initializing named socket at %s", name);
542 retval = -1;
543 goto free_name;
544 }
545
546 INIT_LIST_HEAD(&handle->server.connections);
547
548 free_name:
549 free(name);
550
551 return retval;
552 }
553
554 void ustcomm_fini_app(struct ustcomm_app *handle)
555 {
556 int result;
557 struct stat st;
558
559 /* Destroy socket */
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
579 static char *find_tok(char *str)
580 {
581 while(*str == ' ') {
582 str++;
583
584 if(*str == 0)
585 return NULL;
586 }
587
588 return str;
589 }
590
591 static 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
603 int 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
632 char *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
658 asprintf(&retval, "%.*s", (int)(end-start), start);
659
660 return retval;
661 }
662
This page took 0.050671 seconds and 4 git commands to generate.