ustcomm: move function to destroy app socket to ustcomm
[ust.git] / libustcomm / ustcomm.c
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
18 #define _GNU_SOURCE
19 #include <sys/types.h>
20 #include <signal.h>
21 #include <errno.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <unistd.h>
25 #include <poll.h>
26 #include <sys/stat.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <execinfo.h>
32
33 #include "ustcomm.h"
34 #include "localerr.h"
35
36 #define UNIX_PATH_MAX 108
37
38 #define MSG_MAX 1000
39
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
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 //}
53
54 char *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
68 static int signal_process(pid_t pid)
69 {
70 int result;
71
72 result = kill(pid, UST_SIGNAL);
73 if(result == -1) {
74 PERROR("kill");
75 return -1;
76 }
77
78 /* FIXME: should wait in a better way */
79 //sleep(1);
80
81 return 0;
82 }
83
84 int pid_is_online(pid_t pid) {
85 return kill(pid, UST_SIGNAL) != -1;
86 }
87
88 static int send_message_fd(int fd, const char *msg)
89 {
90 int result;
91
92 result = send(fd, msg, strlen(msg), 0);
93 if(result == -1) {
94 PERROR("send");
95 return -1;
96 }
97 else if(result == 0) {
98 return 0;
99 }
100
101 return 1;
102 }
103
104 /* Called by an app to ask the consumer daemon to connect to it. */
105
106 int ustcomm_request_consumer(pid_t pid, const char *channel)
107 {
108 char path[UNIX_PATH_MAX];
109 int result;
110 char *msg=NULL;
111 int retval = 0;
112 struct ustcomm_connection conn;
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 }
124
125 if(result >= UNIX_PATH_MAX) {
126 ERR("string overflow allocating socket name");
127 return -1;
128 }
129
130 asprintf(&msg, "collect %d %s", pid, channel);
131
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:
150 free(msg);
151
152 return retval;
153 }
154
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 */
159
160 static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src)
161 {
162 int result;
163
164 *msg = (char *) malloc(MSG_MAX+1);
165
166 result = recv(fd, *msg, MSG_MAX, 0);
167 if(result == -1) {
168 PERROR("recv");
169 return -1;
170 }
171
172 (*msg)[result] = '\0';
173
174 DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg));
175
176 if(src)
177 src->fd = fd;
178
179 return 1;
180 }
181
182 int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_source *src)
183 {
184 int result;
185
186 result = send_message_fd(src->fd, msg);
187 if(result < 0) {
188 ERR("error in send_message_fd");
189 return -1;
190 }
191
192 return 0;
193 }
194
195 /* Called after a fork. */
196
197 int 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
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
219 int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src, int timeout)
220 {
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
230 list_for_each_entry(conn, &server->connections, list) {
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 */
241 fds[idx].fd = server->listen_fd;
242 fds[idx].events = POLLIN;
243 idx++;
244
245 list_for_each_entry(conn, &server->connections, list) {
246 fds[idx].fd = conn->fd;
247 fds[idx].events = POLLIN;
248 idx++;
249 }
250
251 while((result = poll(fds, n_fds, timeout)) == -1 && errno == EINTR)
252 /* nothing */;
253 if(result == -1) {
254 PERROR("poll");
255 return -1;
256 }
257
258 if(result == 0)
259 return 0;
260
261 if(fds[0].revents) {
262 struct ustcomm_connection *newconn;
263 int newfd;
264
265 result = newfd = accept(server->listen_fd, NULL, NULL);
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
279 list_add(&newconn->list, &server->connections);
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
289 list_for_each_entry(conn, &server->connections, list) {
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
305 free_fds_return:
306 free(fds);
307 return retval;
308 }
309
310 int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout)
311 {
312 return ustcomm_recv_message(&ustd->server, msg, src, timeout);
313 }
314
315 int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout)
316 {
317 return ustcomm_recv_message(&app->server, msg, src, timeout);
318 }
319
320 /* This removes src from the list of active connections of app.
321 */
322
323 int 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;
336 found:
337 return src->fd;
338 }
339
340 static int init_named_socket(const char *name, char **path_out)
341 {
342 int result;
343 int fd;
344
345 struct sockaddr_un addr;
346
347 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
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
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
369 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
370 if(result == -1) {
371 PERROR("bind");
372 goto close_sock;
373 }
374
375 result = listen(fd, 1);
376 if(result == -1) {
377 PERROR("listen");
378 goto close_sock;
379 }
380
381 if(path_out) {
382 *path_out = strdup(addr.sun_path);
383 }
384
385 return fd;
386
387 close_sock:
388 close(fd);
389
390 return -1;
391 }
392
393 int ustcomm_send_request(struct ustcomm_connection *conn, const char *req, char **reply)
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 }
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
421 int ustcomm_connect_path(const char *path, struct ustcomm_connection *conn, pid_t signalpid)
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
441 if(signalpid >= 0) {
442 result = signal_process(signalpid);
443 if(result == -1) {
444 ERR("could not signal process");
445 return -1;
446 }
447 }
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
460 int ustcomm_disconnect(struct ustcomm_connection *conn)
461 {
462 return close(conn->fd);
463 }
464
465 int 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) {
473 ERR("string overflow allocating socket name");
474 return -1;
475 }
476
477 return ustcomm_connect_path(path, conn, pid);
478 }
479
480 /* Called by an application to initialize its server so daemons can
481 * connect to it.
482 */
483
484 int 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
495 handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath));
496 if(handle->server.listen_fd < 0) {
497 ERR("error initializing named socket");
498 goto free_name;
499 }
500 free(name);
501
502 INIT_LIST_HEAD(&handle->server.connections);
503
504 return 0;
505
506 free_name:
507 free(name);
508 return -1;
509 }
510
511 /* Used by the daemon to initialize its server so applications
512 * can connect to it.
513 */
514
515 int ustcomm_init_ustd(struct ustcomm_ustd *handle, const char *sock_path)
516 {
517 char *name;
518 int retval = 0;
519
520 if(sock_path) {
521 asprintf(&name, "%s", sock_path);
522 }
523 else {
524 asprintf(&name, "%s/%s", SOCK_DIR, "ustd");
525 }
526
527 handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath);
528 if(handle->server.listen_fd < 0) {
529 ERR("error initializing named socket at %s", name);
530 retval = -1;
531 goto free_name;
532 }
533
534 INIT_LIST_HEAD(&handle->server.connections);
535
536 free_name:
537 free(name);
538
539 return retval;
540 }
541
542 void 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
568 static char *find_tok(char *str)
569 {
570 while(*str == ' ') {
571 str++;
572
573 if(*str == 0)
574 return NULL;
575 }
576
577 return str;
578 }
579
580 static 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
592 int 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
621 char *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
647 asprintf(&retval, "%.*s", (int)(end-start), start);
648
649 return retval;
650 }
651
This page took 0.042407 seconds and 4 git commands to generate.