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