cleanups
[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
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <execinfo.h>
31
32 #include "ustcomm.h"
33 #include "localerr.h"
34
35 #define UNIX_PATH_MAX 108
36
37 #define MSG_MAX 1000
38
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
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 //}
52
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
67 static int signal_process(pid_t pid)
68 {
69 int result;
70
71 result = kill(pid, UST_SIGNAL);
72 if(result == -1) {
73 PERROR("kill");
74 return -1;
75 }
76
77 /* FIXME: should wait in a better way */
78 //sleep(1);
79
80 return 0;
81 }
82
83 int pid_is_online(pid_t pid) {
84 return kill(pid, UST_SIGNAL) != -1;
85 }
86
87 static int send_message_fd(int fd, const char *msg)
88 {
89 int result;
90
91 result = send(fd, msg, strlen(msg), 0);
92 if(result == -1) {
93 PERROR("send");
94 return -1;
95 }
96 else if(result == 0) {
97 return 0;
98 }
99
100 return 1;
101 }
102
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;
109 char *msg=NULL;
110 int retval = 0;
111 struct ustcomm_connection conn;
112
113 result = snprintf(path, UNIX_PATH_MAX, "%s/ustd", SOCK_DIR);
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 /* @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)
192 {
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
202 list_for_each_entry(conn, &server->connections, list) {
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 */
213 fds[idx].fd = server->listen_fd;
214 fds[idx].events = POLLIN;
215 idx++;
216
217 list_for_each_entry(conn, &server->connections, list) {
218 fds[idx].fd = conn->fd;
219 fds[idx].events = POLLIN;
220 idx++;
221 }
222
223 while((result = poll(fds, n_fds, timeout)) == -1 && errno == EINTR)
224 /* nothing */;
225 if(result == -1) {
226 PERROR("poll");
227 return -1;
228 }
229
230 if(result == 0)
231 return 0;
232
233 if(fds[0].revents) {
234 struct ustcomm_connection *newconn;
235 int newfd;
236
237 result = newfd = accept(server->listen_fd, NULL, NULL);
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
251 list_add(&newconn->list, &server->connections);
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
261 list_for_each_entry(conn, &server->connections, list) {
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;
280 }
281
282 int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout)
283 {
284 return ustcomm_recv_message(&ustd->server, msg, src, timeout);
285 }
286
287 int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout)
288 {
289 return ustcomm_recv_message(&app->server, msg, src, timeout);
290 }
291
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
312 static int init_named_socket(const char *name, char **path_out)
313 {
314 int result;
315 int fd;
316
317 struct sockaddr_un addr;
318
319 result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
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
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
341 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
342 if(result == -1) {
343 PERROR("bind");
344 goto close_sock;
345 }
346
347 result = listen(fd, 1);
348 if(result == -1) {
349 PERROR("listen");
350 goto close_sock;
351 }
352
353 if(path_out) {
354 *path_out = "";
355 *path_out = strdupa(addr.sun_path);
356 }
357
358 return fd;
359
360 close_sock:
361 close(fd);
362
363 return -1;
364 }
365
366 int ustcomm_send_request(struct ustcomm_connection *conn, const char *req, char **reply)
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 }
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
394 int ustcomm_connect_path(const char *path, struct ustcomm_connection *conn, pid_t signalpid)
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
414 if(signalpid >= 0) {
415 result = signal_process(signalpid);
416 if(result == -1) {
417 ERR("could not signal process");
418 return -1;
419 }
420 }
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) {
446 ERR("string overflow allocating socket name");
447 return -1;
448 }
449
450 return ustcomm_connect_path(path, conn, pid);
451 }
452
453 /* Called by an application to initialize its server so daemons can
454 * connect to it.
455 */
456
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
468 handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath));
469 if(handle->server.listen_fd < 0) {
470 ERR("error initializing named socket");
471 goto free_name;
472 }
473 free(name);
474
475 INIT_LIST_HEAD(&handle->server.connections);
476
477 return 0;
478
479 free_name:
480 free(name);
481 return -1;
482 }
483
484 /* Used by the daemon to initialize its server so applications
485 * can connect to it.
486 */
487
488 int ustcomm_init_ustd(struct ustcomm_ustd *handle)
489 {
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
499 handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath);
500 if(handle->server.listen_fd < 0) {
501 ERR("error initializing named socket at %s", name);
502 goto free_name;
503 }
504 free(name);
505
506 INIT_LIST_HEAD(&handle->server.connections);
507
508 return 0;
509
510 free_name:
511 free(name);
512 return -1;
513 }
514
515 static char *find_tok(char *str)
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
594 asprintf(&retval, "%.*s", (int)(end-start), start);
595
596 return retval;
597 }
598
This page took 0.040613 seconds and 4 git commands to generate.