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