ustd/ustcomm: allow to choose custom socket path
[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
688760ef
PMF
194/* @timeout: max blocking time in milliseconds, -1 means infinity
195 *
196 * returns 1 to indicate a message was received
197 * returns 0 to indicate no message was received
198 * returns -1 to indicate an error
199 */
200
201int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src, int timeout)
b0540e11 202{
aca1ad90
PMF
203 struct pollfd *fds;
204 struct ustcomm_connection *conn;
205 int result;
206 int retval;
207
208 for(;;) {
209 int idx = 0;
210 int n_fds = 1;
211
811e4b93 212 list_for_each_entry(conn, &server->connections, list) {
aca1ad90
PMF
213 n_fds++;
214 }
215
216 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
217 if(fds == NULL) {
218 ERR("malloc returned NULL");
219 return -1;
220 }
221
222 /* special idx 0 is for listening socket */
811e4b93 223 fds[idx].fd = server->listen_fd;
aca1ad90
PMF
224 fds[idx].events = POLLIN;
225 idx++;
226
811e4b93 227 list_for_each_entry(conn, &server->connections, list) {
aca1ad90
PMF
228 fds[idx].fd = conn->fd;
229 fds[idx].events = POLLIN;
230 idx++;
231 }
232
69ba0156
PMF
233 while((result = poll(fds, n_fds, timeout)) == -1 && errno == EINTR)
234 /* nothing */;
aca1ad90
PMF
235 if(result == -1) {
236 PERROR("poll");
237 return -1;
238 }
239
688760ef
PMF
240 if(result == 0)
241 return 0;
242
aca1ad90
PMF
243 if(fds[0].revents) {
244 struct ustcomm_connection *newconn;
245 int newfd;
246
811e4b93 247 result = newfd = accept(server->listen_fd, NULL, NULL);
aca1ad90
PMF
248 if(result == -1) {
249 PERROR("accept");
250 return -1;
251 }
252
253 newconn = (struct ustcomm_connection *) malloc(sizeof(struct ustcomm_connection));
254 if(newconn == NULL) {
255 ERR("malloc returned NULL");
256 return -1;
257 }
258
259 newconn->fd = newfd;
260
811e4b93 261 list_add(&newconn->list, &server->connections);
aca1ad90
PMF
262 }
263
264 for(idx=1; idx<n_fds; idx++) {
265 if(fds[idx].revents) {
266 retval = recv_message_fd(fds[idx].fd, msg, src);
267 if(**msg == 0) {
268 /* connection finished */
269 close(fds[idx].fd);
270
811e4b93 271 list_for_each_entry(conn, &server->connections, list) {
aca1ad90
PMF
272 if(conn->fd == fds[idx].fd) {
273 list_del(&conn->list);
274 break;
275 }
276 }
277 }
278 else {
279 goto free_fds_return;
280 }
281 }
282 }
283
284 free(fds);
285 }
286
287free_fds_return:
288 free(fds);
289 return retval;
b0540e11
PMF
290}
291
688760ef 292int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout)
811e4b93 293{
688760ef 294 return ustcomm_recv_message(&ustd->server, msg, src, timeout);
811e4b93
PMF
295}
296
688760ef 297int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout)
b0540e11 298{
688760ef 299 return ustcomm_recv_message(&app->server, msg, src, timeout);
b0540e11
PMF
300}
301
46ef48cd
PMF
302/* This removes src from the list of active connections of app.
303 */
304
305int ustcomm_app_detach_client(struct ustcomm_app *app, struct ustcomm_source *src)
306{
307 struct ustcomm_server *server = (struct ustcomm_server *)app;
308 struct ustcomm_connection *conn;
309
310 list_for_each_entry(conn, &server->connections, list) {
311 if(conn->fd == src->fd) {
312 list_del(&conn->list);
313 goto found;
314 }
315 }
316
317 return -1;
318found:
319 return src->fd;
320}
321
08230db7 322static int init_named_socket(const char *name, char **path_out)
d0b5f2b9
PMF
323{
324 int result;
325 int fd;
326
327 struct sockaddr_un addr;
328
aca1ad90 329 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
d0b5f2b9
PMF
330 if(result == -1) {
331 PERROR("socket");
332 return -1;
333 }
334
335 addr.sun_family = AF_UNIX;
336
337 strncpy(addr.sun_path, name, UNIX_PATH_MAX);
338 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
339
aca1ad90
PMF
340 result = access(name, F_OK);
341 if(result == 0) {
342 /* file exists */
343 result = unlink(name);
344 if(result == -1) {
345 PERROR("unlink of socket file");
346 goto close_sock;
347 }
348 WARN("socket already exists; overwriting");
349 }
350
d0b5f2b9
PMF
351 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
352 if(result == -1) {
353 PERROR("bind");
354 goto close_sock;
355 }
356
aca1ad90
PMF
357 result = listen(fd, 1);
358 if(result == -1) {
359 PERROR("listen");
360 goto close_sock;
361 }
362
b0540e11
PMF
363 if(path_out) {
364 *path_out = "";
d0b5f2b9 365 *path_out = strdupa(addr.sun_path);
b0540e11 366 }
d0b5f2b9
PMF
367
368 return fd;
369
370 close_sock:
371 close(fd);
372
373 return -1;
374}
375
772030fe 376int ustcomm_send_request(struct ustcomm_connection *conn, const char *req, char **reply)
4e2a8808
PMF
377{
378 int result;
379
380 result = send(conn->fd, req, strlen(req), 0);
381 if(result == -1) {
382 PERROR("send");
383 return -1;
384 }
4e2a8808
PMF
385
386 if(!reply)
387 return 1;
388
389 *reply = (char *) malloc(MSG_MAX+1);
390 result = recv(conn->fd, *reply, MSG_MAX, 0);
391 if(result == -1) {
392 PERROR("recv");
393 return -1;
394 }
395 else if(result == 0) {
396 return 0;
397 }
398
399 (*reply)[result] = '\0';
400
401 return 1;
402}
403
08230db7 404int ustcomm_connect_path(const char *path, struct ustcomm_connection *conn, pid_t signalpid)
4e2a8808
PMF
405{
406 int fd;
407 int result;
408 struct sockaddr_un addr;
409
410 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
411 if(result == -1) {
412 PERROR("socket");
413 return -1;
414 }
415
416 addr.sun_family = AF_UNIX;
417
418 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path);
419 if(result >= UNIX_PATH_MAX) {
420 ERR("string overflow allocating socket name");
421 return -1;
422 }
423
52c51a47
PMF
424 if(signalpid >= 0) {
425 result = signal_process(signalpid);
426 if(result == -1) {
427 ERR("could not signal process");
428 return -1;
429 }
430 }
4e2a8808
PMF
431
432 result = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
433 if(result == -1) {
434 PERROR("connect");
435 return -1;
436 }
437
438 conn->fd = fd;
439
440 return 0;
441}
442
443int ustcomm_disconnect(struct ustcomm_connection *conn)
444{
445 return close(conn->fd);
446}
447
448int ustcomm_connect_app(pid_t pid, struct ustcomm_connection *conn)
449{
450 int result;
451 char path[UNIX_PATH_MAX];
452
453
454 result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
455 if(result >= UNIX_PATH_MAX) {
08230db7 456 ERR("string overflow allocating socket name");
4e2a8808
PMF
457 return -1;
458 }
459
460 return ustcomm_connect_path(path, conn, pid);
461}
462
08230db7
PMF
463/* Called by an application to initialize its server so daemons can
464 * connect to it.
465 */
4e2a8808 466
d0b5f2b9
PMF
467int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle)
468{
469 int result;
470 char *name;
471
472 result = asprintf(&name, "%s/%d", SOCK_DIR, (int)pid);
473 if(result >= UNIX_PATH_MAX) {
474 ERR("string overflow allocating socket name");
475 return -1;
476 }
477
811e4b93
PMF
478 handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath));
479 if(handle->server.listen_fd < 0) {
aca1ad90 480 ERR("error initializing named socket");
d0b5f2b9
PMF
481 goto free_name;
482 }
483 free(name);
484
811e4b93 485 INIT_LIST_HEAD(&handle->server.connections);
aca1ad90 486
d0b5f2b9
PMF
487 return 0;
488
489free_name:
490 free(name);
491 return -1;
492}
493
08230db7
PMF
494/* Used by the daemon to initialize its server so applications
495 * can connect to it.
496 */
497
c97d4437 498int ustcomm_init_ustd(struct ustcomm_ustd *handle, const char *sock_path)
d0b5f2b9 499{
3847c3ba 500 char *name;
c97d4437 501 int retval = 0;
3847c3ba 502
c97d4437
PMF
503 if(sock_path) {
504 asprintf(&name, "%s", sock_path);
505 }
506 else {
507 asprintf(&name, "%s/%s", SOCK_DIR, "ustd");
3847c3ba
PMF
508 }
509
811e4b93
PMF
510 handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath);
511 if(handle->server.listen_fd < 0) {
6cb88bc0 512 ERR("error initializing named socket at %s", name);
c97d4437 513 retval = -1;
aca1ad90
PMF
514 goto free_name;
515 }
d0b5f2b9 516
811e4b93 517 INIT_LIST_HEAD(&handle->server.connections);
aca1ad90 518
aca1ad90
PMF
519free_name:
520 free(name);
c97d4437
PMF
521
522 return retval;
d0b5f2b9 523}
b02e31e5 524
aca1ad90 525static char *find_tok(char *str)
b02e31e5
PMF
526{
527 while(*str == ' ') {
528 str++;
529
530 if(*str == 0)
531 return NULL;
532 }
533
534 return str;
535}
536
537static char *find_sep(char *str)
538{
539 while(*str != ' ') {
540 str++;
541
542 if(*str == 0)
543 break;
544 }
545
546 return str;
547}
548
549int nth_token_is(char *str, char *token, int tok_no)
550{
551 int i;
552 char *start;
553 char *end;
554
555 for(i=0; i<=tok_no; i++) {
556 str = find_tok(str);
557 if(str == NULL)
558 return -1;
559
560 start = str;
561
562 str = find_sep(str);
563 if(str == NULL)
564 return -1;
565
566 end = str;
567 }
568
569 if(end-start != strlen(token))
570 return 0;
571
572 if(strncmp(start, token, end-start))
573 return 0;
574
575 return 1;
576}
577
578char *nth_token(char *str, int tok_no)
579{
580 static char *retval = NULL;
581 int i;
582 char *start;
583 char *end;
584
585 for(i=0; i<=tok_no; i++) {
586 str = find_tok(str);
587 if(str == NULL)
588 return NULL;
589
590 start = str;
591
592 str = find_sep(str);
593 if(str == NULL)
594 return NULL;
595
596 end = str;
597 }
598
599 if(retval) {
600 free(retval);
601 retval = NULL;
602 }
603
aca1ad90 604 asprintf(&retval, "%.*s", (int)(end-start), start);
b02e31e5
PMF
605
606 return retval;
607}
608
This page took 0.047886 seconds and 4 git commands to generate.