ustcomm: move function to destroy app socket to ustcomm
[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
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
803a4f58
PMF
542void ustcomm_fini_app(struct ustcomm_app *handle)
543{
544 int result;
545 struct stat st;
546
547 /* Destroy socket */
548 ERR("socket path is: %s", handle->server.socketpath);
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.049887 seconds and 4 git commands to generate.