libustcomm: change char * to const char * where relevant
[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 "usterr.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 return 0;
71 }
72
73 int pid_is_online(pid_t pid) {
74 return 1;
75 }
76
77 static int send_message_fd(int fd, const char *msg)
78 {
79 int result;
80
81 result = send(fd, msg, strlen(msg), MSG_NOSIGNAL);
82 if(result == -1) {
83 PERROR("send");
84 return -1;
85 }
86 else if(result == 0) {
87 return 0;
88 }
89
90 return 1;
91 }
92
93 /* Called by an app to ask the consumer daemon to connect to it. */
94
95 int ustcomm_request_consumer(pid_t pid, const char *channel)
96 {
97 char path[UNIX_PATH_MAX];
98 int result;
99 char *msg=NULL;
100 int retval = 0;
101 struct ustcomm_connection conn;
102 char *explicit_daemon_socket_path;
103
104 explicit_daemon_socket_path = getenv("UST_DAEMON_SOCKET");
105 if(explicit_daemon_socket_path) {
106 /* user specified explicitly a socket path */
107 result = snprintf(path, UNIX_PATH_MAX, "%s", explicit_daemon_socket_path);
108 }
109 else {
110 /* just use the default path */
111 result = snprintf(path, UNIX_PATH_MAX, "%s/ustd", SOCK_DIR);
112 }
113
114 if(result >= UNIX_PATH_MAX) {
115 ERR("string overflow allocating socket name");
116 return -1;
117 }
118
119 asprintf(&msg, "collect %d %s", pid, channel);
120
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:
139 free(msg);
140
141 return retval;
142 }
143
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 */
148
149 static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src)
150 {
151 int result;
152
153 *msg = (char *) malloc(MSG_MAX+1);
154
155 result = recv(fd, *msg, MSG_MAX, 0);
156 if(result == -1) {
157 PERROR("recv");
158 return -1;
159 }
160
161 (*msg)[result] = '\0';
162
163 DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg));
164
165 if(src)
166 src->fd = fd;
167
168 return 1;
169 }
170
171 int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_source *src)
172 {
173 int result;
174
175 result = send_message_fd(src->fd, msg);
176 if(result < 0) {
177 ERR("error in send_message_fd");
178 return -1;
179 }
180
181 return 0;
182 }
183
184 /* Called after a fork. */
185
186 int ustcomm_close_all_connections(struct ustcomm_server *server)
187 {
188 struct ustcomm_connection *conn;
189 struct ustcomm_connection *deletable_conn = NULL;
190
191 list_for_each_entry(conn, &server->connections, list) {
192 free(deletable_conn);
193 deletable_conn = conn;
194 close(conn->fd);
195 list_del(&conn->list);
196 }
197
198 return 0;
199 }
200
201 /* @timeout: max blocking time in milliseconds, -1 means infinity
202 *
203 * returns 1 to indicate a message was received
204 * returns 0 to indicate no message was received
205 * returns -1 to indicate an error
206 */
207
208 int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src, int timeout)
209 {
210 struct pollfd *fds;
211 struct ustcomm_connection *conn;
212 int result;
213 int retval;
214
215 for(;;) {
216 int idx = 0;
217 int n_fds = 1;
218
219 list_for_each_entry(conn, &server->connections, list) {
220 n_fds++;
221 }
222
223 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
224 if(fds == NULL) {
225 ERR("malloc returned NULL");
226 return -1;
227 }
228
229 /* special idx 0 is for listening socket */
230 fds[idx].fd = server->listen_fd;
231 fds[idx].events = POLLIN;
232 idx++;
233
234 list_for_each_entry(conn, &server->connections, list) {
235 fds[idx].fd = conn->fd;
236 fds[idx].events = POLLIN;
237 idx++;
238 }
239
240 while((result = poll(fds, n_fds, timeout)) == -1 && errno == EINTR)
241 /* nothing */;
242 if(result == -1) {
243 PERROR("poll");
244 return -1;
245 }
246
247 if(result == 0)
248 return 0;
249
250 if(fds[0].revents) {
251 struct ustcomm_connection *newconn;
252 int newfd;
253
254 result = newfd = accept(server->listen_fd, NULL, NULL);
255 if(result == -1) {
256 PERROR("accept");
257 return -1;
258 }
259
260 newconn = (struct ustcomm_connection *) malloc(sizeof(struct ustcomm_connection));
261 if(newconn == NULL) {
262 ERR("malloc returned NULL");
263 return -1;
264 }
265
266 newconn->fd = newfd;
267
268 list_add(&newconn->list, &server->connections);
269 }
270
271 for(idx=1; idx<n_fds; idx++) {
272 if(fds[idx].revents) {
273 retval = recv_message_fd(fds[idx].fd, msg, src);
274 if(**msg == 0) {
275 /* connection finished */
276 close(fds[idx].fd);
277
278 list_for_each_entry(conn, &server->connections, list) {
279 if(conn->fd == fds[idx].fd) {
280 list_del(&conn->list);
281 break;
282 }
283 }
284 }
285 else {
286 goto free_fds_return;
287 }
288 }
289 }
290
291 free(fds);
292 }
293
294 free_fds_return:
295 free(fds);
296 return retval;
297 }
298
299 int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout)
300 {
301 return ustcomm_recv_message(&ustd->server, msg, src, timeout);
302 }
303
304 int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout)
305 {
306 return ustcomm_recv_message(&app->server, msg, src, timeout);
307 }
308
309 /* This removes src from the list of active connections of app.
310 */
311
312 int ustcomm_app_detach_client(struct ustcomm_app *app, struct ustcomm_source *src)
313 {
314 struct ustcomm_server *server = (struct ustcomm_server *)app;
315 struct ustcomm_connection *conn;
316
317 list_for_each_entry(conn, &server->connections, list) {
318 if(conn->fd == src->fd) {
319 list_del(&conn->list);
320 goto found;
321 }
322 }
323
324 return -1;
325 found:
326 return src->fd;
327 }
328
329 static int init_named_socket(const char *name, char **path_out)
330 {
331 int result;
332 int fd;
333
334 struct sockaddr_un addr;
335
336 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
337 if(result == -1) {
338 PERROR("socket");
339 return -1;
340 }
341
342 addr.sun_family = AF_UNIX;
343
344 strncpy(addr.sun_path, name, UNIX_PATH_MAX);
345 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
346
347 result = access(name, F_OK);
348 if(result == 0) {
349 /* file exists */
350 result = unlink(name);
351 if(result == -1) {
352 PERROR("unlink of socket file");
353 goto close_sock;
354 }
355 WARN("socket already exists; overwriting");
356 }
357
358 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
359 if(result == -1) {
360 PERROR("bind");
361 goto close_sock;
362 }
363
364 result = listen(fd, 1);
365 if(result == -1) {
366 PERROR("listen");
367 goto close_sock;
368 }
369
370 if(path_out) {
371 *path_out = strdup(addr.sun_path);
372 }
373
374 return fd;
375
376 close_sock:
377 close(fd);
378
379 return -1;
380 }
381
382 /*
383 * Return value:
384 * 0: Success, but no reply because recv() returned 0
385 * 1: Success
386 * -1: Error
387 *
388 * On error, the error message is printed, except on
389 * ECONNRESET, which is normal when the application dies.
390 */
391
392 int ustcomm_send_request(struct ustcomm_connection *conn, const char *req, char **reply)
393 {
394 int result;
395
396 result = send(conn->fd, req, strlen(req), MSG_NOSIGNAL);
397 if(result == -1) {
398 if(errno != EPIPE)
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 if(errno != ECONNRESET)
410 PERROR("recv");
411 return -1;
412 }
413 else if(result == 0) {
414 return 0;
415 }
416
417 (*reply)[result] = '\0';
418
419 return 1;
420 }
421
422 int ustcomm_connect_path(const char *path, struct ustcomm_connection *conn, pid_t signalpid)
423 {
424 int fd;
425 int result;
426 struct sockaddr_un addr;
427
428 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
429 if(result == -1) {
430 PERROR("socket");
431 return -1;
432 }
433
434 addr.sun_family = AF_UNIX;
435
436 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path);
437 if(result >= UNIX_PATH_MAX) {
438 ERR("string overflow allocating socket name");
439 return -1;
440 }
441
442 if(signalpid >= 0) {
443 result = signal_process(signalpid);
444 if(result == -1) {
445 ERR("could not signal process");
446 return -1;
447 }
448 }
449
450 result = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
451 if(result == -1) {
452 PERROR("connect");
453 return -1;
454 }
455
456 conn->fd = fd;
457
458 return 0;
459 }
460
461 int ustcomm_disconnect(struct ustcomm_connection *conn)
462 {
463 return close(conn->fd);
464 }
465
466 int ustcomm_connect_app(pid_t pid, struct ustcomm_connection *conn)
467 {
468 int result;
469 char path[UNIX_PATH_MAX];
470
471
472 result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
473 if(result >= UNIX_PATH_MAX) {
474 ERR("string overflow allocating socket name");
475 return -1;
476 }
477
478 return ustcomm_connect_path(path, conn, pid);
479 }
480
481 static int ensure_dir_exists(const char *dir)
482 {
483 struct stat st;
484 int result;
485
486 if(!strcmp(dir, ""))
487 return -1;
488
489 result = stat(dir, &st);
490 if(result == -1 && errno != ENOENT) {
491 return -1;
492 }
493 else if(result == -1) {
494 /* ENOENT */
495 char buf[200];
496 int result;
497
498 result = snprintf(buf, sizeof(buf), "mkdir -p \"%s\"", dir);
499 if(result >= sizeof(buf)) {
500 ERR("snprintf buffer overflow");
501 return -1;
502 }
503 result = system(buf);
504 if(result != 0) {
505 ERR("executing command %s", buf);
506 return -1;
507 }
508 }
509
510 return 0;
511 }
512
513 /* Called by an application to initialize its server so daemons can
514 * connect to it.
515 */
516
517 int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle)
518 {
519 int result;
520 char *name;
521
522 result = asprintf(&name, "%s/%d", SOCK_DIR, (int)pid);
523 if(result >= UNIX_PATH_MAX) {
524 ERR("string overflow allocating socket name");
525 return -1;
526 }
527
528 result = ensure_dir_exists(SOCK_DIR);
529 if(result == -1) {
530 ERR("Unable to create socket directory %s", SOCK_DIR);
531 return -1;
532 }
533
534 handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath));
535 if(handle->server.listen_fd < 0) {
536 ERR("Error initializing named socket (%s). Check that directory exists and that it is writable.", name);
537 goto free_name;
538 }
539 free(name);
540
541 INIT_LIST_HEAD(&handle->server.connections);
542
543 return 0;
544
545 free_name:
546 free(name);
547 return -1;
548 }
549
550 /* Used by the daemon to initialize its server so applications
551 * can connect to it.
552 */
553
554 int ustcomm_init_ustd(struct ustcomm_ustd *handle, const char *sock_path)
555 {
556 char *name;
557 int retval = 0;
558
559 if(sock_path) {
560 asprintf(&name, "%s", sock_path);
561 }
562 else {
563 int result;
564
565 /* Only check if socket dir exists if we are using the default directory */
566 result = ensure_dir_exists(SOCK_DIR);
567 if(result == -1) {
568 ERR("Unable to create socket directory %s", SOCK_DIR);
569 return -1;
570 }
571
572 asprintf(&name, "%s/%s", SOCK_DIR, "ustd");
573 }
574
575 handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath);
576 if(handle->server.listen_fd < 0) {
577 ERR("error initializing named socket at %s", name);
578 retval = -1;
579 goto free_name;
580 }
581
582 INIT_LIST_HEAD(&handle->server.connections);
583
584 free_name:
585 free(name);
586
587 return retval;
588 }
589
590 void ustcomm_fini_app(struct ustcomm_app *handle)
591 {
592 int result;
593 struct stat st;
594
595 /* Destroy socket */
596 result = stat(handle->server.socketpath, &st);
597 if(result == -1) {
598 PERROR("stat (%s)", handle->server.socketpath);
599 return;
600 }
601
602 /* Paranoid check before deleting. */
603 result = S_ISSOCK(st.st_mode);
604 if(!result) {
605 ERR("The socket we are about to delete is not a socket.");
606 return;
607 }
608
609 result = unlink(handle->server.socketpath);
610 if(result == -1) {
611 PERROR("unlink");
612 }
613 }
614
615 static const char *find_tok(const char *str)
616 {
617 while(*str == ' ') {
618 str++;
619
620 if(*str == 0)
621 return NULL;
622 }
623
624 return str;
625 }
626
627 static const char *find_sep(const char *str)
628 {
629 while(*str != ' ') {
630 str++;
631
632 if(*str == 0)
633 break;
634 }
635
636 return str;
637 }
638
639 int nth_token_is(const char *str, const char *token, int tok_no)
640 {
641 int i;
642 const char *start;
643 const char *end;
644
645 for(i=0; i<=tok_no; i++) {
646 str = find_tok(str);
647 if(str == NULL)
648 return -1;
649
650 start = str;
651
652 str = find_sep(str);
653 if(str == NULL)
654 return -1;
655
656 end = str;
657 }
658
659 if(end-start != strlen(token))
660 return 0;
661
662 if(strncmp(start, token, end-start))
663 return 0;
664
665 return 1;
666 }
667
668 char *nth_token(const char *str, int tok_no)
669 {
670 static char *retval = NULL;
671 int i;
672 const char *start;
673 const char *end;
674
675 for(i=0; i<=tok_no; i++) {
676 str = find_tok(str);
677 if(str == NULL)
678 return NULL;
679
680 start = str;
681
682 str = find_sep(str);
683 if(str == NULL)
684 return NULL;
685
686 end = str;
687 }
688
689 if(retval) {
690 free(retval);
691 retval = NULL;
692 }
693
694 asprintf(&retval, "%.*s", (int)(end-start), start);
695
696 return retval;
697 }
698
This page took 0.042456 seconds and 4 git commands to generate.