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