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