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