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