fef0eae1680336dfe83eb05b8ca16b37d1d6fa39
[ust.git] / ustd / ustd.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
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/shm.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <pthread.h>
26 #include <signal.h>
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <assert.h>
33 #include <getopt.h>
34
35 #include "ustd.h"
36 #include "usterr.h"
37 #include "ustcomm.h"
38 #include "share.h"
39
40 /* return value: 0 = subbuffer is finished, it won't produce data anymore
41 * 1 = got subbuffer successfully
42 * <0 = error
43 */
44
45 #define GET_SUBBUF_OK 1
46 #define GET_SUBBUF_DONE 0
47 #define GET_SUBBUF_DIED 2
48
49 #define PUT_SUBBUF_OK 1
50 #define PUT_SUBBUF_DIED 0
51 #define PUT_SUBBUF_PUSHED 2
52
53 char *sock_path=NULL;
54 char *trace_path=NULL;
55 int daemon_mode = 0;
56 char *pidfile = NULL;
57
58 /* Number of active buffers and the mutex to protect it. */
59 int active_buffers = 0;
60 pthread_mutex_t active_buffers_mutex = PTHREAD_MUTEX_INITIALIZER;
61 /* Whether a request to end the program was received. */
62 sig_atomic_t terminate_req = 0;
63
64 int get_subbuffer(struct buffer_info *buf)
65 {
66 char *send_msg=NULL;
67 char *received_msg=NULL;
68 char *rep_code=NULL;
69 int retval;
70 int result;
71
72 asprintf(&send_msg, "get_subbuffer %s", buf->name);
73 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
74 if((result == -1 && errno == EPIPE) || result == 0) {
75 DBG("app died while being traced");
76 retval = GET_SUBBUF_DIED;
77 goto end;
78 }
79 else if(result < 0) {
80 ERR("get_subbuffer: ustcomm_send_request failed");
81 retval = -1;
82 goto end;
83 }
84
85 result = sscanf(received_msg, "%as %ld", &rep_code, &buf->consumed_old);
86 if(result != 2 && result != 1) {
87 ERR("unable to parse response to get_subbuffer");
88 retval = -1;
89 goto end_rep;
90 }
91
92 DBG("received msg is %s", received_msg);
93
94 if(!strcmp(rep_code, "OK")) {
95 DBG("got subbuffer %s", buf->name);
96 retval = GET_SUBBUF_OK;
97 }
98 else if(nth_token_is(received_msg, "END", 0) == 1) {
99 retval = GET_SUBBUF_DONE;
100 goto end_rep;
101 }
102 else {
103 DBG("error getting subbuffer %s", buf->name);
104 retval = -1;
105 }
106
107 /* FIMXE: free correctly the stuff */
108 end_rep:
109 if(rep_code)
110 free(rep_code);
111 end:
112 if(send_msg)
113 free(send_msg);
114 if(received_msg)
115 free(received_msg);
116
117 return retval;
118 }
119
120 int put_subbuffer(struct buffer_info *buf)
121 {
122 char *send_msg=NULL;
123 char *received_msg=NULL;
124 char *rep_code=NULL;
125 int retval;
126 int result;
127
128 asprintf(&send_msg, "put_subbuffer %s %ld", buf->name, buf->consumed_old);
129 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
130 if(result < 0 && errno == ECONNRESET) {
131 retval = PUT_SUBBUF_DIED;
132 goto end;
133 }
134 if(result < 0) {
135 ERR("put_subbuffer: send_message failed");
136 retval = -1;
137 goto end;
138 }
139
140 result = sscanf(received_msg, "%as", &rep_code);
141 if(result != 1) {
142 ERR("unable to parse response to put_subbuffer");
143 retval = -1;
144 goto end_rep;
145 }
146
147 if(!strcmp(rep_code, "OK")) {
148 DBG("subbuffer put %s", buf->name);
149 retval = PUT_SUBBUF_OK;
150 }
151 else {
152 DBG("put_subbuffer: received error, we were pushed");
153 retval = PUT_SUBBUF_PUSHED;
154 goto end_rep;
155 }
156
157 end_rep:
158 if(rep_code)
159 free(rep_code);
160
161 end:
162 if(send_msg)
163 free(send_msg);
164 if(received_msg)
165 free(received_msg);
166
167 return retval;
168 }
169
170 void decrement_active_buffers(void *arg)
171 {
172 pthread_mutex_lock(&active_buffers_mutex);
173 active_buffers--;
174 pthread_mutex_unlock(&active_buffers_mutex);
175 }
176
177 int create_dir_if_needed(char *dir)
178 {
179 int result;
180 result = mkdir(dir, 0777);
181 if(result == -1) {
182 if(errno != EEXIST) {
183 PERROR("mkdir");
184 return -1;
185 }
186 }
187
188 return 0;
189 }
190
191 int is_directory(const char *dir)
192 {
193 int result;
194 struct stat st;
195
196 result = stat(dir, &st);
197 if(result == -1) {
198 PERROR("stat");
199 return 0;
200 }
201
202 if(!S_ISDIR(st.st_mode)) {
203 return 0;
204 }
205
206 return 1;
207 }
208
209 struct buffer_info *connect_buffer(pid_t pid, const char *bufname)
210 {
211 struct buffer_info *buf;
212 char *send_msg;
213 char *received_msg;
214 int result;
215 char *tmp;
216 int fd;
217 struct shmid_ds shmds;
218
219 buf = (struct buffer_info *) malloc(sizeof(struct buffer_info));
220 if(buf == NULL) {
221 ERR("add_buffer: insufficient memory");
222 return NULL;
223 }
224
225 buf->name = bufname;
226 buf->pid = pid;
227
228 /* connect to app */
229 result = ustcomm_connect_app(buf->pid, &buf->conn);
230 if(result) {
231 WARN("unable to connect to process, it probably died before we were able to connect");
232 return NULL;
233 }
234
235 /* get pidunique */
236 asprintf(&send_msg, "get_pidunique");
237 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
238 free(send_msg);
239 if(result == -1) {
240 ERR("problem in ustcomm_send_request(get_pidunique)");
241 return NULL;
242 }
243
244 result = sscanf(received_msg, "%lld", &buf->pidunique);
245 if(result != 1) {
246 ERR("unable to parse response to get_pidunique");
247 return NULL;
248 }
249 free(received_msg);
250 DBG("got pidunique %lld", buf->pidunique);
251
252 /* get shmid */
253 asprintf(&send_msg, "get_shmid %s", buf->name);
254 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
255 free(send_msg);
256 if(result == -1) {
257 ERR("problem in ustcomm_send_request(get_shmid)");
258 return NULL;
259 }
260
261 result = sscanf(received_msg, "%d %d", &buf->shmid, &buf->bufstruct_shmid);
262 if(result != 2) {
263 ERR("unable to parse response to get_shmid");
264 return NULL;
265 }
266 free(received_msg);
267 DBG("got shmids %d %d", buf->shmid, buf->bufstruct_shmid);
268
269 /* get n_subbufs */
270 asprintf(&send_msg, "get_n_subbufs %s", buf->name);
271 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
272 free(send_msg);
273 if(result == -1) {
274 ERR("problem in ustcomm_send_request(g_n_subbufs)");
275 return NULL;
276 }
277
278 result = sscanf(received_msg, "%d", &buf->n_subbufs);
279 if(result != 1) {
280 ERR("unable to parse response to get_n_subbufs");
281 return NULL;
282 }
283 free(received_msg);
284 DBG("got n_subbufs %d", buf->n_subbufs);
285
286 /* get subbuf size */
287 asprintf(&send_msg, "get_subbuf_size %s", buf->name);
288 ustcomm_send_request(&buf->conn, send_msg, &received_msg);
289 free(send_msg);
290
291 result = sscanf(received_msg, "%d", &buf->subbuf_size);
292 if(result != 1) {
293 ERR("unable to parse response to get_subbuf_size");
294 return NULL;
295 }
296 free(received_msg);
297 DBG("got subbuf_size %d", buf->subbuf_size);
298
299 /* attach memory */
300 buf->mem = shmat(buf->shmid, NULL, 0);
301 if(buf->mem == (void *) 0) {
302 PERROR("shmat");
303 return NULL;
304 }
305 DBG("successfully attached buffer memory");
306
307 buf->bufstruct_mem = shmat(buf->bufstruct_shmid, NULL, 0);
308 if(buf->bufstruct_mem == (void *) 0) {
309 PERROR("shmat");
310 return NULL;
311 }
312 DBG("successfully attached buffer bufstruct memory");
313
314 /* obtain info on the memory segment */
315 result = shmctl(buf->shmid, IPC_STAT, &shmds);
316 if(result == -1) {
317 PERROR("shmctl");
318 return NULL;
319 }
320 buf->memlen = shmds.shm_segsz;
321
322 /* open file for output */
323 if(!trace_path) {
324 /* Only create the directory if using the default path, because
325 * of the risk of typo when using trace path override. We don't
326 * want to risk creating plenty of useless directories in that case.
327 */
328 result = create_dir_if_needed(USTD_DEFAULT_TRACE_PATH);
329 if(result == -1) {
330 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH);
331 return NULL;
332 }
333
334 trace_path = USTD_DEFAULT_TRACE_PATH;
335 }
336
337 asprintf(&tmp, "%s/%u_%lld", trace_path, buf->pid, buf->pidunique);
338 result = create_dir_if_needed(tmp);
339 if(result == -1) {
340 ERR("could not create directory %s", tmp);
341 free(tmp);
342 return NULL;
343 }
344 free(tmp);
345
346 asprintf(&tmp, "%s/%u_%lld/%s_0", trace_path, buf->pid, buf->pidunique, buf->name);
347 result = fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 00600);
348 if(result == -1) {
349 PERROR("open");
350 ERR("failed opening trace file %s", tmp);
351 return NULL;
352 }
353 buf->file_fd = fd;
354 free(tmp);
355
356 pthread_mutex_lock(&active_buffers_mutex);
357 active_buffers++;
358 pthread_mutex_unlock(&active_buffers_mutex);
359
360 return buf;
361 }
362
363 int consumer_loop(struct buffer_info *buf)
364 {
365 int result;
366
367 pthread_cleanup_push(decrement_active_buffers, NULL);
368
369 for(;;) {
370 /* get the subbuffer */
371 result = get_subbuffer(buf);
372 if(result == -1) {
373 ERR("error getting subbuffer");
374 continue;
375 }
376 else if(result == GET_SUBBUF_DONE) {
377 /* this is done */
378 break;
379 }
380 else if(result == GET_SUBBUF_DIED) {
381 finish_consuming_dead_subbuffer(buf);
382 break;
383 }
384
385 /* write data to file */
386 result = patient_write(buf->file_fd, buf->mem + (buf->consumed_old & (buf->n_subbufs * buf->subbuf_size-1)), buf->subbuf_size);
387 if(result == -1) {
388 PERROR("write");
389 /* FIXME: maybe drop this trace */
390 }
391
392 /* put the subbuffer */
393 /* FIXME: we actually should unput the buffer before consuming... */
394 result = put_subbuffer(buf);
395 if(result == -1) {
396 ERR("unknown error putting subbuffer (channel=%s)", buf->name);
397 break;
398 }
399 else if(result == PUT_SUBBUF_PUSHED) {
400 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf->name);
401 break;
402 }
403 else if(result == PUT_SUBBUF_DIED) {
404 WARN("application died while putting subbuffer");
405 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
406 finish_consuming_dead_subbuffer(buf);
407 break;
408 }
409 else if(result == PUT_SUBBUF_OK) {
410 }
411 }
412
413 DBG("thread for buffer %s is stopping", buf->name);
414
415 /* FIXME: destroy, unalloc... */
416
417 pthread_cleanup_pop(1);
418
419 return 0;
420 }
421
422 void free_buffer(struct buffer_info *buf)
423 {
424 }
425
426 struct consumer_thread_args {
427 pid_t pid;
428 const char *bufname;
429 };
430
431 void *consumer_thread(void *arg)
432 {
433 struct buffer_info *buf = (struct buffer_info *) arg;
434 struct consumer_thread_args *args = (struct consumer_thread_args *) arg;
435
436 DBG("GOT ARGS: pid %d bufname %s", args->pid, args->bufname);
437
438 buf = connect_buffer(args->pid, args->bufname);
439 if(buf == NULL) {
440 ERR("failed to connect to buffer");
441 goto end;
442 }
443
444 consumer_loop(buf);
445
446 free_buffer(buf);
447
448 end:
449 /* bufname is free'd in free_buffer() */
450 free(args);
451 return NULL;
452 }
453
454 int start_consuming_buffer(pid_t pid, const char *bufname)
455 {
456 pthread_t thr;
457 struct consumer_thread_args *args;
458
459 DBG("beginning of start_consuming_buffer: args: pid %d bufname %s", pid, bufname);
460
461 args = (struct consumer_thread_args *) malloc(sizeof(struct consumer_thread_args));
462
463 args->pid = pid;
464 args->bufname = strdup(bufname);
465 DBG("beginning2 of start_consuming_buffer: args: pid %d bufname %s", args->pid, args->bufname);
466
467 pthread_create(&thr, NULL, consumer_thread, args);
468 DBG("end of start_consuming_buffer: args: pid %d bufname %s", args->pid, args->bufname);
469
470 return 0;
471 }
472
473 void usage(void)
474 {
475 fprintf(stderr, "Usage:\nustd OPTIONS\n\nOptions:\n"
476 "\t-h\t\tDisplay this usage.\n"
477 "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
478 "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n"
479 "\t-d\t\tStart as a daemon.\n"
480 "\t--pidfile FILE\tWrite the PID in this file (when using -d).\n");
481 }
482
483 int parse_args(int argc, char **argv)
484 {
485 int c;
486
487 while (1) {
488 int option_index = 0;
489 static struct option long_options[] = {
490 {"pidfile", 1, 0, 'p'},
491 {"help", 0, 0, 'h'},
492 {"version", 0, 0, 'V'},
493 {0, 0, 0, 0}
494 };
495
496 c = getopt_long(argc, argv, "hs:o:d", long_options, &option_index);
497 if (c == -1)
498 break;
499
500 switch (c) {
501 case 0:
502 printf("option %s", long_options[option_index].name);
503 if (optarg)
504 printf(" with arg %s", optarg);
505 printf("\n");
506 break;
507 case 's':
508 sock_path = optarg;
509 break;
510 case 'o':
511 trace_path = optarg;
512 if(!is_directory(trace_path)) {
513 ERR("Not a valid directory. (%s)", trace_path);
514 return -1;
515 }
516 break;
517 case 'd':
518 daemon_mode = 1;
519 break;
520 case 'p':
521 pidfile = strdup(optarg);
522 break;
523 case 'h':
524 usage();
525 exit(0);
526 case 'V':
527 printf("Version 0.0\n");
528 break;
529
530 default:
531 /* unknown option or other error; error is
532 printed by getopt, just return */
533 return -1;
534 }
535 }
536
537 return 0;
538 }
539
540 void sigterm_handler(int sig)
541 {
542 terminate_req = 1;
543 }
544
545 int start_ustd(int fd)
546 {
547 struct ustcomm_ustd ustd;
548 int result;
549 sigset_t sigset;
550 struct sigaction sa;
551
552 result = sigemptyset(&sigset);
553 if(result == -1) {
554 PERROR("sigemptyset");
555 return 1;
556 }
557 sa.sa_handler = sigterm_handler;
558 sa.sa_mask = sigset;
559 sa.sa_flags = SA_RESTART;
560 result = sigaction(SIGTERM, &sa, NULL);
561 if(result == -1) {
562 PERROR("sigaction");
563 return 1;
564 }
565
566 result = ustcomm_init_ustd(&ustd, sock_path);
567 if(result == -1) {
568 ERR("failed to initialize socket");
569 return 1;
570 }
571
572 /* setup handler for SIGPIPE */
573 result = sigemptyset(&sigset);
574 if(result == -1) {
575 PERROR("sigemptyset");
576 return 1;
577 }
578 result = sigaddset(&sigset, SIGPIPE);
579 if(result == -1) {
580 PERROR("sigaddset");
581 return 1;
582 }
583 result = sigprocmask(SIG_BLOCK, &sigset, NULL);
584 if(result == -1) {
585 PERROR("sigprocmask");
586 return 1;
587 }
588
589 /* Notify parent that we are successfully started. */
590 if(fd != -1) {
591 /* write any one character */
592 result = write(fd, "!", 1);
593 if(result == -1) {
594 PERROR("write");
595 return -1;
596 }
597 if(result != 1) {
598 ERR("Problem sending confirmation of daemon start to parent");
599 return -1;
600 }
601 result = close(fd);
602 if(result == -1) {
603 PERROR("close");
604 }
605 }
606
607 /* app loop */
608 for(;;) {
609 char *recvbuf;
610
611 /* check for requests on our public socket */
612 result = ustcomm_ustd_recv_message(&ustd, &recvbuf, NULL, 100);
613 if(result == -1) {
614 ERR("error in ustcomm_ustd_recv_message");
615 goto loop_end;
616 }
617 if(result > 0) {
618 if(!strncmp(recvbuf, "collect", 7)) {
619 pid_t pid;
620 char *bufname;
621 int result;
622
623 result = sscanf(recvbuf, "%*s %d %50as", &pid, &bufname);
624 if(result != 2) {
625 ERR("parsing error: %s", recvbuf);
626 goto free_bufname;
627 }
628
629 result = start_consuming_buffer(pid, bufname);
630 if(result < 0) {
631 ERR("error in add_buffer");
632 goto free_bufname;
633 }
634
635 free_bufname:
636 free(bufname);
637 }
638
639 free(recvbuf);
640 }
641
642 loop_end:
643
644 if(terminate_req) {
645 pthread_mutex_lock(&active_buffers_mutex);
646 if(active_buffers == 0) {
647 pthread_mutex_unlock(&active_buffers_mutex);
648 break;
649 }
650 pthread_mutex_unlock(&active_buffers_mutex);
651 }
652 }
653
654 return 0;
655 }
656
657 int start_ustd_daemon()
658 {
659 int result;
660 int fd[2];
661 pid_t child_pid;
662
663 result = pipe(fd);
664
665 result = child_pid = fork();
666 if(result == -1) {
667 PERROR("fork");
668 return -1;
669 }
670 else if(result == 0) {
671 return start_ustd(fd[1]);
672 }
673 else {
674 char buf;
675 FILE *pidfp;
676
677 /* It's important to write the file *before*
678 * the parent ends, because the file may be
679 * read as soon as the parent ends.
680 */
681 if(pidfile) {
682 pidfp = fopen(pidfile, "w+");
683 if(!pidfp) {
684 PERROR("fopen (%s)", pidfile);
685 WARN("killing child process");
686 result = kill(child_pid, SIGTERM);
687 if(result == -1) {
688 PERROR("kill");
689 }
690 return -1;
691 }
692
693 fprintf(pidfp, "%d\n", child_pid);
694 fclose(pidfp);
695 }
696
697 result = read(fd[0], &buf, 1);
698 if(result == -1) {
699 PERROR("read");
700 return -1;
701 }
702 if(result != 1) {
703 ERR("did not receive valid confirmation that the daemon is started");
704 return -1;
705 }
706
707 result = close(fd[0]);
708 if(result == -1) {
709 PERROR("close");
710 }
711
712 DBG("The daemon is now successfully started");
713 }
714
715 /* Wait for confirmation that the server is ready. */
716
717
718 return 0;
719 }
720
721 int main(int argc, char **argv)
722 {
723 int result;
724
725 result = parse_args(argc, argv);
726 if(result == -1) {
727 exit(1);
728 }
729
730 if(daemon_mode) {
731 result = start_ustd_daemon();
732 }
733 else {
734 result = start_ustd(-1);
735 }
736
737 return result;
738 }
This page took 0.042565 seconds and 3 git commands to generate.